authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-05 10:34:16+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-07 22:42:58+02:00
log08f6546c8405dd7d9da80f857122f43f4d627a22
tree5a3565f8b3570bd1e0957611a92bec94cf304ba6
parentf1bdf3f62f05388b75d6816b65c9cc5caec71cd9

coff: create a helper for allocating sections


1 files changed, 48 insertions(+), 123 deletions(-)

src/link/Coff.zig+48-123
......@@ -338,150 +338,54 @@ fn populateMissingMetadata(self: *Coff) !void {
338338 });
339339
340340 if (self.text_section_index == null) {
341 self.text_section_index = @intCast(u16, self.sections.slice().len);
342341 const file_size = @intCast(u32, self.base.options.program_code_size_hint);
343 const off = self.findFreeSpace(file_size, self.page_size); // TODO we are over-aligning in file; we should track both in file and in memory pointers
344 log.debug("found .text free space 0x{x} to 0x{x}", .{ off, off + file_size });
345 var header = coff.SectionHeader{
346 .name = undefined,
347 .virtual_size = file_size,
348 .virtual_address = off,
349 .size_of_raw_data = file_size,
350 .pointer_to_raw_data = off,
351 .pointer_to_relocations = 0,
352 .pointer_to_linenumbers = 0,
353 .number_of_relocations = 0,
354 .number_of_linenumbers = 0,
355 .flags = .{
356 .CNT_CODE = 1,
357 .MEM_EXECUTE = 1,
358 .MEM_READ = 1,
359 },
360 };
361 try self.setSectionName(&header, ".text");
362 try self.sections.append(gpa, .{ .header = header });
342 self.text_section_index = try self.allocateSection(".text", file_size, .{
343 .CNT_CODE = 1,
344 .MEM_EXECUTE = 1,
345 .MEM_READ = 1,
346 });
363347 }
364348
365349 if (self.got_section_index == null) {
366 self.got_section_index = @intCast(u16, self.sections.slice().len);
367350 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * self.ptr_width.abiSize();
368 const off = self.findFreeSpace(file_size, self.page_size);
369 log.debug("found .got free space 0x{x} to 0x{x}", .{ off, off + file_size });
370 var header = coff.SectionHeader{
371 .name = undefined,
372 .virtual_size = file_size,
373 .virtual_address = off,
374 .size_of_raw_data = file_size,
375 .pointer_to_raw_data = off,
376 .pointer_to_relocations = 0,
377 .pointer_to_linenumbers = 0,
378 .number_of_relocations = 0,
379 .number_of_linenumbers = 0,
380 .flags = .{
381 .CNT_INITIALIZED_DATA = 1,
382 .MEM_READ = 1,
383 },
384 };
385 try self.setSectionName(&header, ".got");
386 try self.sections.append(gpa, .{ .header = header });
351 self.got_section_index = try self.allocateSection(".got", file_size, .{
352 .CNT_INITIALIZED_DATA = 1,
353 .MEM_READ = 1,
354 });
387355 }
388356
389357 if (self.rdata_section_index == null) {
390 self.rdata_section_index = @intCast(u16, self.sections.slice().len);
391358 const file_size: u32 = 1024;
392 const off = self.findFreeSpace(file_size, self.page_size);
393 log.debug("found .rdata free space 0x{x} to 0x{x}", .{ off, off + file_size });
394 var header = coff.SectionHeader{
395 .name = undefined,
396 .virtual_size = file_size,
397 .virtual_address = off,
398 .size_of_raw_data = file_size,
399 .pointer_to_raw_data = off,
400 .pointer_to_relocations = 0,
401 .pointer_to_linenumbers = 0,
402 .number_of_relocations = 0,
403 .number_of_linenumbers = 0,
404 .flags = .{
405 .CNT_INITIALIZED_DATA = 1,
406 .MEM_READ = 1,
407 },
408 };
409 try self.setSectionName(&header, ".rdata");
410 try self.sections.append(gpa, .{ .header = header });
359 self.rdata_section_index = try self.allocateSection(".rdata", file_size, .{
360 .CNT_INITIALIZED_DATA = 1,
361 .MEM_READ = 1,
362 });
411363 }
412364
413365 if (self.data_section_index == null) {
414 self.data_section_index = @intCast(u16, self.sections.slice().len);
415366 const file_size: u32 = 1024;
416 const off = self.findFreeSpace(file_size, self.page_size);
417 log.debug("found .data free space 0x{x} to 0x{x}", .{ off, off + file_size });
418 var header = coff.SectionHeader{
419 .name = undefined,
420 .virtual_size = file_size,
421 .virtual_address = off,
422 .size_of_raw_data = file_size,
423 .pointer_to_raw_data = off,
424 .pointer_to_relocations = 0,
425 .pointer_to_linenumbers = 0,
426 .number_of_relocations = 0,
427 .number_of_linenumbers = 0,
428 .flags = .{
429 .CNT_INITIALIZED_DATA = 1,
430 .MEM_READ = 1,
431 .MEM_WRITE = 1,
432 },
433 };
434 try self.setSectionName(&header, ".data");
435 try self.sections.append(gpa, .{ .header = header });
367 self.data_section_index = try self.allocateSection(".data", file_size, .{
368 .CNT_INITIALIZED_DATA = 1,
369 .MEM_READ = 1,
370 .MEM_WRITE = 1,
371 });
436372 }
437373
438374 if (self.reloc_section_index == null) {
439 self.reloc_section_index = @intCast(u16, self.sections.slice().len);
440375 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * @sizeOf(coff.BaseRelocation);
441 const off = self.findFreeSpace(file_size, self.page_size);
442 log.debug("found .reloc free space 0x{x} to 0x{x}", .{ off, off + file_size });
443 var header = coff.SectionHeader{
444 .name = undefined,
445 .virtual_size = file_size,
446 .virtual_address = off,
447 .size_of_raw_data = file_size,
448 .pointer_to_raw_data = off,
449 .pointer_to_relocations = 0,
450 .pointer_to_linenumbers = 0,
451 .number_of_relocations = 0,
452 .number_of_linenumbers = 0,
453 .flags = .{
454 .CNT_INITIALIZED_DATA = 1,
455 .MEM_DISCARDABLE = 1,
456 .MEM_READ = 1,
457 },
458 };
459 try self.setSectionName(&header, ".reloc");
460 try self.sections.append(gpa, .{ .header = header });
376 self.reloc_section_index = try self.allocateSection(".reloc", file_size, .{
377 .CNT_INITIALIZED_DATA = 1,
378 .MEM_DISCARDABLE = 1,
379 .MEM_READ = 1,
380 });
461381 }
462382
463383 if (self.idata_section_index == null) {
464 self.idata_section_index = @intCast(u16, self.sections.slice().len);
465384 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * self.ptr_width.abiSize();
466 const off = self.findFreeSpace(file_size, self.page_size);
467 log.debug("found .idata free space 0x{x} to 0x{x}", .{ off, off + file_size });
468 var header = coff.SectionHeader{
469 .name = undefined,
470 .virtual_size = file_size,
471 .virtual_address = off,
472 .size_of_raw_data = file_size,
473 .pointer_to_raw_data = off,
474 .pointer_to_relocations = 0,
475 .pointer_to_linenumbers = 0,
476 .number_of_relocations = 0,
477 .number_of_linenumbers = 0,
478 .flags = .{
479 .CNT_INITIALIZED_DATA = 1,
480 .MEM_READ = 1,
481 },
482 };
483 try self.setSectionName(&header, ".idata");
484 try self.sections.append(gpa, .{ .header = header });
385 self.idata_section_index = try self.allocateSection(".idata", file_size, .{
386 .CNT_INITIALIZED_DATA = 1,
387 .MEM_READ = 1,
388 });
485389 }
486390
487391 if (self.strtab_offset == null) {
......@@ -505,6 +409,27 @@ fn populateMissingMetadata(self: *Coff) !void {
505409 }
506410}
507411
412fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.SectionHeaderFlags) !u16 {
413 const index = @intCast(u16, self.sections.slice().len);
414 const off = self.findFreeSpace(size, self.page_size); // TODO: we overalign here
415 log.debug("found {s} free space 0x{x} to 0x{x}", .{ name, off, off + size });
416 var header = coff.SectionHeader{
417 .name = undefined,
418 .virtual_size = size,
419 .virtual_address = off,
420 .size_of_raw_data = size,
421 .pointer_to_raw_data = off,
422 .pointer_to_relocations = 0,
423 .pointer_to_linenumbers = 0,
424 .number_of_relocations = 0,
425 .number_of_linenumbers = 0,
426 .flags = flags,
427 };
428 try self.setSectionName(&header, name);
429 try self.sections.append(self.base.allocator, .{ .header = header });
430 return index;
431}
432
508433pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void {
509434 if (self.llvm_object) |_| return;
510435 const decl = self.base.options.module.?.declPtr(decl_index);