authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-03 00:22:11+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
log6faed6269fca56d583d2bc5bf35f55a51eb54cdf
tree4a05d233aff0aad4fe2ed85c9ec18257ddaea786
parent14cff77d87c1580e10fce60b6e07022ea9456948

elf: update section sizes accumulated from objects


2 files changed, 17 insertions(+), 37 deletions(-)

src/link/Elf.zig+4-27
......@@ -18,9 +18,6 @@ objects: std.ArrayListUnmanaged(File.Index) = .{},
1818/// Stored in native-endian format, depending on target endianness needs to be bswapped on read/write.
1919/// Same order as in the file.
2020shdrs: std.ArrayListUnmanaged(elf.Elf64_Shdr) = .{},
21/// Given index to a section, returns a list of atoms allocated within it.
22/// Excludes incrementally allocated atoms - for those, use linked-list approach.
23atoms_by_shdr_table: std.AutoArrayHashMapUnmanaged(u16, AtomList) = .{},
2421/// Given index to a section, pulls index of containing phdr if any.
2522phdr_to_shdr_table: std.AutoHashMapUnmanaged(u16, u16) = .{},
2623/// File offset into the shdr table.
......@@ -319,12 +316,6 @@ pub fn deinit(self: *Elf) void {
319316 self.objects.deinit(gpa);
320317
321318 self.shdrs.deinit(gpa);
322
323 for (self.atoms_by_shdr_table.values()) |*list| {
324 list.deinit(gpa);
325 }
326 self.atoms_by_shdr_table.deinit(gpa);
327
328319 self.phdr_to_shdr_table.deinit(gpa);
329320 self.phdrs.deinit(gpa);
330321 self.shstrtab.deinit(gpa);
......@@ -1254,7 +1245,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
12541245 // Generate and emit non-incremental sections.
12551246 try self.initSections();
12561247 try self.sortSections();
1257 try self.addAtomsToSections();
1248 try self.updateSectionSizes();
12581249
12591250 // Dump the state for easy debugging.
12601251 // State can be dumped via `--debug-log link_state`.
......@@ -1266,7 +1257,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
12661257 // linker-defined synthetic symbols.
12671258 try self.allocateObjects();
12681259 self.allocateLinkerDefinedSymbols();
1269 try self.updateSyntheticSectionSizes();
12701260
12711261 // Look for entry address in objects if not set by the incremental compiler.
12721262 if (self.entry_addr == null) {
......@@ -3598,26 +3588,13 @@ fn sortSections(self: *Elf) !void {
35983588 }
35993589}
36003590
3601fn addAtomsToSections(self: *Elf) !void {
3602 const gpa = self.base.allocator;
3591fn updateSectionSizes(self: *Elf) !void {
36033592 for (self.objects.items) |index| {
3604 for (self.file(index).?.atoms()) |atom_index| {
3605 const atom_ptr = self.atom(atom_index) orelse continue;
3606 if (!atom_ptr.flags.alive) continue;
3607 const gop = try self.atoms_by_shdr_table.getOrPut(gpa, atom_ptr.output_section_index);
3608 if (!gop.found_existing) gop.value_ptr.* = .{};
3609 try gop.value_ptr.append(gpa, atom_index);
3610 }
3593 self.file(index).?.object.updateSectionSizes(self);
36113594 }
3612}
36133595
3614fn updateSyntheticSectionSizes(self: *Elf) !void {
36153596 if (self.got_section_index) |index| {
3616 if (self.got.dirty) {
3617 try self.growAllocSection(index, self.got.size(self));
3618 self.got.dirty = false;
3619 self.got_addresses_dirty = true;
3620 }
3597 self.shdrs.items[index].sh_size = self.got.size(self);
36213598 }
36223599
36233600 if (self.symtab_section_index != null) {
src/link/Elf/Object.zig+13-10
......@@ -179,7 +179,6 @@ fn addAtom(
179179 atom.name_offset = try elf_file.strtab.insert(elf_file.base.allocator, name);
180180 atom.file_index = self.index;
181181 atom.input_section_index = shndx;
182 atom.output_section_index = try self.getOutputSectionIndex(elf_file, shdr);
183182 atom.flags.alive = true;
184183 self.atoms.items[shndx] = atom_index;
185184
......@@ -279,10 +278,6 @@ fn initSymtab(self: *Object, elf_file: *Elf) !void {
279278 sym_ptr.esym_index = @as(u32, @intCast(i));
280279 sym_ptr.atom_index = if (sym.st_shndx == elf.SHN_ABS) 0 else self.atoms.items[sym.st_shndx];
281280 sym_ptr.file_index = self.index;
282 sym_ptr.output_section_index = if (sym_ptr.atom(elf_file)) |atom_ptr|
283 atom_ptr.outputShndx().?
284 else
285 elf.SHN_UNDEF;
286281 }
287282
288283 for (self.symtab[first_global..]) |sym| {
......@@ -453,15 +448,10 @@ pub fn resolveSymbols(self: *Object, elf_file: *Elf) void {
453448 elf.SHN_ABS, elf.SHN_COMMON => 0,
454449 else => self.atoms.items[esym.st_shndx],
455450 };
456 const output_section_index = if (elf_file.atom(atom_index)) |atom|
457 atom.outputShndx().?
458 else
459 elf.SHN_UNDEF;
460451 global.value = esym.st_value;
461452 global.atom_index = atom_index;
462453 global.esym_index = esym_index;
463454 global.file_index = self.index;
464 global.output_section_index = output_section_index;
465455 global.version_index = elf_file.default_sym_version;
466456 if (esym.st_bind() == elf.STB_WEAK) global.flags.weak = true;
467457 }
......@@ -611,6 +601,19 @@ pub fn convertCommonSymbols(self: *Object, elf_file: *Elf) !void {
611601 }
612602}
613603
604pub fn updateSectionSizes(self: Object, elf_file: *Elf) void {
605 for (self.atoms.items) |atom_index| {
606 const atom = elf_file.atom(atom_index) orelse continue;
607 if (!atom.flags.alive) continue;
608 const shdr = &elf_file.shdrs.items[atom.output_section_index];
609 const offset = atom.alignment.forward(shdr.sh_size);
610 const padding = offset - shdr.sh_size;
611 atom.value = offset;
612 shdr.sh_size += padding + atom.size;
613 shdr.sh_addralign = @max(shdr.sh_addralign, atom.alignment.toByteUnits(1));
614 }
615}
616
614617pub fn updateSymtabSize(self: *Object, elf_file: *Elf) void {
615618 for (self.locals()) |local_index| {
616619 const local = elf_file.symbol(local_index);