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 {...@@ -338,150 +338,54 @@ fn populateMissingMetadata(self: *Coff) !void {
338 });338 });
339339
340 if (self.text_section_index == null) {340 if (self.text_section_index == null) {
341 self.text_section_index = @intCast(u16, self.sections.slice().len);
342 const file_size = @intCast(u32, self.base.options.program_code_size_hint);341 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 pointers342 self.text_section_index = try self.allocateSection(".text", file_size, .{
344 log.debug("found .text free space 0x{x} to 0x{x}", .{ off, off + file_size });343 .CNT_CODE = 1,
345 var header = coff.SectionHeader{344 .MEM_EXECUTE = 1,
346 .name = undefined,345 .MEM_READ = 1,
347 .virtual_size = file_size,346 });
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 });
363 }347 }
364348
365 if (self.got_section_index == null) {349 if (self.got_section_index == null) {
366 self.got_section_index = @intCast(u16, self.sections.slice().len);
367 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * self.ptr_width.abiSize();350 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);351 self.got_section_index = try self.allocateSection(".got", file_size, .{
369 log.debug("found .got free space 0x{x} to 0x{x}", .{ off, off + file_size });352 .CNT_INITIALIZED_DATA = 1,
370 var header = coff.SectionHeader{353 .MEM_READ = 1,
371 .name = undefined,354 });
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 });
387 }355 }
388356
389 if (self.rdata_section_index == null) {357 if (self.rdata_section_index == null) {
390 self.rdata_section_index = @intCast(u16, self.sections.slice().len);
391 const file_size: u32 = 1024;358 const file_size: u32 = 1024;
392 const off = self.findFreeSpace(file_size, self.page_size);359 self.rdata_section_index = try self.allocateSection(".rdata", file_size, .{
393 log.debug("found .rdata free space 0x{x} to 0x{x}", .{ off, off + file_size });360 .CNT_INITIALIZED_DATA = 1,
394 var header = coff.SectionHeader{361 .MEM_READ = 1,
395 .name = undefined,362 });
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 });
411 }363 }
412364
413 if (self.data_section_index == null) {365 if (self.data_section_index == null) {
414 self.data_section_index = @intCast(u16, self.sections.slice().len);
415 const file_size: u32 = 1024;366 const file_size: u32 = 1024;
416 const off = self.findFreeSpace(file_size, self.page_size);367 self.data_section_index = try self.allocateSection(".data", file_size, .{
417 log.debug("found .data free space 0x{x} to 0x{x}", .{ off, off + file_size });368 .CNT_INITIALIZED_DATA = 1,
418 var header = coff.SectionHeader{369 .MEM_READ = 1,
419 .name = undefined,370 .MEM_WRITE = 1,
420 .virtual_size = file_size,371 });
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 });
436 }372 }
437373
438 if (self.reloc_section_index == null) {374 if (self.reloc_section_index == null) {
439 self.reloc_section_index = @intCast(u16, self.sections.slice().len);
440 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * @sizeOf(coff.BaseRelocation);375 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * @sizeOf(coff.BaseRelocation);
441 const off = self.findFreeSpace(file_size, self.page_size);376 self.reloc_section_index = try self.allocateSection(".reloc", file_size, .{
442 log.debug("found .reloc free space 0x{x} to 0x{x}", .{ off, off + file_size });377 .CNT_INITIALIZED_DATA = 1,
443 var header = coff.SectionHeader{378 .MEM_DISCARDABLE = 1,
444 .name = undefined,379 .MEM_READ = 1,
445 .virtual_size = file_size,380 });
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 });
461 }381 }
462382
463 if (self.idata_section_index == null) {383 if (self.idata_section_index == null) {
464 self.idata_section_index = @intCast(u16, self.sections.slice().len);
465 const file_size = @intCast(u32, self.base.options.symbol_count_hint) * self.ptr_width.abiSize();384 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);385 self.idata_section_index = try self.allocateSection(".idata", file_size, .{
467 log.debug("found .idata free space 0x{x} to 0x{x}", .{ off, off + file_size });386 .CNT_INITIALIZED_DATA = 1,
468 var header = coff.SectionHeader{387 .MEM_READ = 1,
469 .name = undefined,388 });
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 });
485 }389 }
486390
487 if (self.strtab_offset == null) {391 if (self.strtab_offset == null) {
...@@ -505,6 +409,27 @@ fn populateMissingMetadata(self: *Coff) !void {...@@ -505,6 +409,27 @@ fn populateMissingMetadata(self: *Coff) !void {
505 }409 }
506}410}
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
508pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void {433pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void {
509 if (self.llvm_object) |_| return;434 if (self.llvm_object) |_| return;
510 const decl = self.base.options.module.?.declPtr(decl_index);435 const decl = self.base.options.module.?.declPtr(decl_index);