diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2024-10-03 17:18:28 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-10-09 12:38:53 -0700 |
| commit | ef7bac4aa58abcf860c222f15eaba8e9c8c702a4 (patch) | |
| tree | 96c5036fda7981313ac92819d5cccdeb003e47ed | |
| parent | 3d315f45d8237dabb9c0a1782177c92265dfc20b (diff) | |
| download | zig-ef7bac4aa58abcf860c222f15eaba8e9c8c702a4.tar.gz zig-ef7bac4aa58abcf860c222f15eaba8e9c8c702a4.zip | |
elf: move setting section size back to Elf.growSection
| -rw-r--r-- | src/link/Dwarf.zig | 6 | ||||
| -rw-r--r-- | src/link/Elf.zig | 68 |
2 files changed, 36 insertions, 38 deletions
diff --git a/src/link/Dwarf.zig b/src/link/Dwarf.zig index 840cef69fe..cde40294b4 100644 --- a/src/link/Dwarf.zig +++ b/src/link/Dwarf.zig @@ -393,12 +393,10 @@ pub const Section = struct { const needed_size = len; const min_alignment = sec.alignment.toByteUnits().?; try elf_file.growSection(shndx, needed_size, min_alignment); - const shdr = &elf_file.sections.items(.shdr)[shndx]; - shdr.sh_size = needed_size; - elf_file.markDirty(shndx); + const shdr = elf_file.sections.items(.shdr)[shndx]; atom.size = needed_size; atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign); - sec.len = len; + sec.len = needed_size; } else if (dwarf.bin_file.cast(.macho)) |macho_file| { const header = if (macho_file.d_sym) |*d_sym| header: { try d_sym.growSection(@intCast(sec.index), len, true, macho_file); diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 5478441482..c8faaa33b6 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -558,43 +558,47 @@ pub fn findFreeSpace(self: *Elf, object_size: u64, min_alignment: u64) !u64 { pub fn growSection(self: *Elf, shdr_index: u32, needed_size: u64, min_alignment: u64) !void { const shdr = &self.sections.items(.shdr)[shdr_index]; - assert(shdr.sh_type != elf.SHT_NOBITS); - const allocated_size = self.allocatedSize(shdr.sh_offset); - log.debug("allocated size {x} of '{s}', needed size {x}", .{ - allocated_size, - self.getShString(shdr.sh_name), - needed_size, - }); - - if (needed_size > allocated_size) { - const existing_size = shdr.sh_size; - shdr.sh_size = 0; - // Must move the entire section. - const new_offset = try self.findFreeSpace(needed_size, min_alignment); - - log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{ + if (shdr.sh_type != elf.SHT_NOBITS) { + const allocated_size = self.allocatedSize(shdr.sh_offset); + log.debug("allocated size {x} of '{s}', needed size {x}", .{ + allocated_size, self.getShString(shdr.sh_name), - new_offset, - new_offset + existing_size, + needed_size, }); - const amt = try self.base.file.?.copyRangeAll( - shdr.sh_offset, - self.base.file.?, - new_offset, - existing_size, - ); - // TODO figure out what to about this error condition - how to communicate it up. - if (amt != existing_size) return error.InputOutput; + if (needed_size > allocated_size) { + const existing_size = shdr.sh_size; + shdr.sh_size = 0; + // Must move the entire section. + const new_offset = try self.findFreeSpace(needed_size, min_alignment); + + log.debug("new '{s}' file offset 0x{x} to 0x{x}", .{ + self.getShString(shdr.sh_name), + new_offset, + new_offset + existing_size, + }); + + const amt = try self.base.file.?.copyRangeAll( + shdr.sh_offset, + self.base.file.?, + new_offset, + existing_size, + ); + // TODO figure out what to about this error condition - how to communicate it up. + if (amt != existing_size) return error.InputOutput; - shdr.sh_offset = new_offset; - } else if (shdr.sh_offset + allocated_size == std.math.maxInt(u64)) { - try self.base.file.?.setEndPos(shdr.sh_offset + needed_size); + shdr.sh_offset = new_offset; + } else if (shdr.sh_offset + allocated_size == std.math.maxInt(u64)) { + try self.base.file.?.setEndPos(shdr.sh_offset + needed_size); + } } + + shdr.sh_size = needed_size; + self.markDirty(shdr_index); } -pub fn markDirty(self: *Elf, shdr_index: u32) void { +fn markDirty(self: *Elf, shdr_index: u32) void { if (self.zigObjectPtr()) |zo| { for ([_]?Symbol.Index{ zo.debug_info_index, @@ -700,11 +704,7 @@ pub fn allocateChunk(self: *Elf, args: struct { true; if (expand_section) { const needed_size = res.value + args.size; - if (shdr.sh_type != elf.SHT_NOBITS) { - try self.growSection(args.shndx, needed_size, args.alignment.toByteUnits().?); - } - shdr.sh_size = needed_size; - self.markDirty(args.shndx); + try self.growSection(args.shndx, needed_size, args.alignment.toByteUnits().?); } return res; |
