authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-10-03 17:18:28+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-09 12:38:53-07:00
logef7bac4aa58abcf860c222f15eaba8e9c8c702a4
tree96c5036fda7981313ac92819d5cccdeb003e47ed
parent3d315f45d8237dabb9c0a1782177c92265dfc20b

elf: move setting section size back to Elf.growSection


2 files changed, 36 insertions(+), 38 deletions(-)

src/link/Dwarf.zig+2-4
......@@ -393,12 +393,10 @@ pub const Section = struct {
393393 const needed_size = len;
394394 const min_alignment = sec.alignment.toByteUnits().?;
395395 try elf_file.growSection(shndx, needed_size, min_alignment);
396 const shdr = &elf_file.sections.items(.shdr)[shndx];
397 shdr.sh_size = needed_size;
398 elf_file.markDirty(shndx);
396 const shdr = elf_file.sections.items(.shdr)[shndx];
399397 atom.size = needed_size;
400398 atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign);
401 sec.len = len;
399 sec.len = needed_size;
402400 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
403401 const header = if (macho_file.d_sym) |*d_sym| header: {
404402 try d_sym.growSection(@intCast(sec.index), len, true, macho_file);
src/link/Elf.zig+34-34
......@@ -558,43 +558,47 @@ pub fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) !u64 {
558558
559559pub fn growSection(self: *Elf, shdr_index: u32, needed_size: u64, min_alignment: u64) !void {
560560 const shdr = &self.sections.items(.shdr)[shdr_index];
561 assert(shdr.sh_type != elf.SHT_NOBITS);
562561
563 const allocated_size = self.allocatedSize(shdr.sh_offset);
564 log.debug("allocated size {x} of '{s}', needed size {x}", .{
565 allocated_size,
566 self.getShString(shdr.sh_name),
567 needed_size,
568 });
569
570 if (needed_size > allocated_size) {
571 const existing_size = shdr.sh_size;
572 shdr.sh_size = 0;
573 // Must move the entire section.
574 const new_offset = try self.findFreeSpace(needed_size, min_alignment);
575
576 log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{
562 if (shdr.sh_type != elf.SHT_NOBITS) {
563 const allocated_size = self.allocatedSize(shdr.sh_offset);
564 log.debug("allocated size {x} of '{s}', needed size {x}", .{
565 allocated_size,
577566 self.getShString(shdr.sh_name),
578 new_offset,
579 new_offset + existing_size,
567 needed_size,
580568 });
581569
582 const amt = try self.base.file.?.copyRangeAll(
583 shdr.sh_offset,
584 self.base.file.?,
585 new_offset,
586 existing_size,
587 );
588 // TODO figure out what to about this error condition - how to communicate it up.
589 if (amt != existing_size) return error.InputOutput;
570 if (needed_size > allocated_size) {
571 const existing_size = shdr.sh_size;
572 shdr.sh_size = 0;
573 // Must move the entire section.
574 const new_offset = try self.findFreeSpace(needed_size, min_alignment);
575
576 log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{
577 self.getShString(shdr.sh_name),
578 new_offset,
579 new_offset + existing_size,
580 });
581
582 const amt = try self.base.file.?.copyRangeAll(
583 shdr.sh_offset,
584 self.base.file.?,
585 new_offset,
586 existing_size,
587 );
588 // TODO figure out what to about this error condition - how to communicate it up.
589 if (amt != existing_size) return error.InputOutput;
590590
591 shdr.sh_offset = new_offset;
592 } else if (shdr.sh_offset + allocated_size == std.math.maxInt(u64)) {
593 try self.base.file.?.setEndPos(shdr.sh_offset + needed_size);
591 shdr.sh_offset = new_offset;
592 } else if (shdr.sh_offset + allocated_size == std.math.maxInt(u64)) {
593 try self.base.file.?.setEndPos(shdr.sh_offset + needed_size);
594 }
594595 }
596
597 shdr.sh_size = needed_size;
598 self.markDirty(shdr_index);
595599}
596600
597pub fn markDirty(self: *Elf, shdr_index: u32) void {
601fn markDirty(self: *Elf, shdr_index: u32) void {
598602 if (self.zigObjectPtr()) |zo| {
599603 for ([_]?Symbol.Index{
600604 zo.debug_info_index,
......@@ -700,11 +704,7 @@ pub fn allocateChunk(self: *Elf, args: struct {
700704 true;
701705 if (expand_section) {
702706 const needed_size = res.value + args.size;
703 if (shdr.sh_type != elf.SHT_NOBITS) {
704 try self.growSection(args.shndx, needed_size, args.alignment.toByteUnits().?);
705 }
706 shdr.sh_size = needed_size;
707 self.markDirty(args.shndx);
707 try self.growSection(args.shndx, needed_size, args.alignment.toByteUnits().?);
708708 }
709709
710710 return res;