authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-30 12:09:16+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:04+02:00
log75f4420c2db46cc3d9fe9c75ac035c588bbcf4bf
tree5a450dd667f9454133cad9b9e1b2163554c36590
parent605e3eb08cffd40af98ff1bb2080d3e5f29da861

elf: increase Atom.Index resolution to u32


4 files changed, 19 insertions(+), 20 deletions(-)

src/link/Elf.zig+3-3
......@@ -887,7 +887,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {
887887 } });
888888 self.zig_module_index = index;
889889 const zig_module = self.file(index).?.zig_module;
890
890 try zig_module.atoms.append(gpa, 0); // null input section
891891 const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_mod.root_src_path));
892892 const symbol_index = try self.addSymbol();
893893 try zig_module.local_symbols.append(gpa, symbol_index);
......@@ -1319,7 +1319,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
13191319 // the relocations, and commit objects to file.
13201320 if (self.zig_module_index) |index| {
13211321 const zig_module = self.file(index).?.zig_module;
1322 for (zig_module.atoms.keys()) |atom_index| {
1322 for (zig_module.atoms.items) |atom_index| {
13231323 const atom_ptr = self.atom(atom_index).?;
13241324 if (!atom_ptr.flags.alive) continue;
13251325 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];
......@@ -3245,7 +3245,7 @@ pub fn updateDeclExports(
32453245 };
32463246 const esym = &zig_module.global_esyms.items[sym_index & 0x0fffffff];
32473247 esym.st_value = decl_sym.value;
3248 esym.st_shndx = decl_sym.atom_index;
3248 esym.st_shndx = decl_esym.st_shndx;
32493249 esym.st_info = (stb_bits << 4) | stt_bits;
32503250 esym.st_name = name_off;
32513251 }
src/link/Elf/Atom.zig+6-8
......@@ -14,13 +14,13 @@ size: u64 = 0,
1414alignment: Alignment = .@"1",
1515
1616/// Index of the input section.
17input_section_index: Index = 0,
17input_section_index: u16 = 0,
1818
1919/// Index of the output section.
2020output_section_index: u16 = 0,
2121
2222/// Index of the input section containing this atom's relocs.
23relocs_section_index: Index = 0,
23relocs_section_index: u16 = 0,
2424
2525/// Index of this atom in the linker's atoms table.
2626atom_index: Index = 0,
......@@ -216,7 +216,6 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
216216 log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) });
217217
218218 const gpa = elf_file.base.allocator;
219 const zig_module = self.file(elf_file).?.zig_module;
220219 const shndx = self.outputShndx().?;
221220 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;
222221 const free_list = &meta.free_list;
......@@ -267,7 +266,9 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
267266
268267 // TODO create relocs free list
269268 self.freeRelocs(elf_file);
270 assert(zig_module.atoms.swapRemove(self.atom_index));
269 // TODO figure out how to free input section mappind in ZigModule
270 // const zig_module = self.file(elf_file).?.zig_module;
271 // assert(zig_module.atoms.swapRemove(self.atom_index));
271272 self.* = .{};
272273}
273274
......@@ -698,10 +699,7 @@ fn format2(
698699 }
699700}
700701
701// TODO this has to be u32 but for now, to avoid redesigning elfSym machinery for
702// ZigModule, keep it at u16 with the intention of bumping it to u32 in the near
703// future.
704pub const Index = u16;
702pub const Index = u32;
705703
706704pub const Flags = packed struct {
707705 /// Specifies whether this atom is alive or has been garbage collected.
src/link/Elf/ZigModule.zig+9-8
......@@ -13,7 +13,7 @@ local_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
1313global_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
1414globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{},
1515
16atoms: std.AutoArrayHashMapUnmanaged(Atom.Index, void) = .{},
16atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
1717relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(elf.Elf64_Rela)) = .{},
1818
1919output_symtab_size: Elf.SymtabSize = .{},
......@@ -56,7 +56,8 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {
5656 const symbol_index = try elf_file.addSymbol();
5757 const esym_index = try self.addLocalEsym(gpa);
5858
59 try self.atoms.putNoClobber(gpa, atom_index, {});
59 const shndx = @as(u16, @intCast(self.atoms.items.len));
60 try self.atoms.append(gpa, atom_index);
6061 try self.local_symbols.append(gpa, symbol_index);
6162
6263 const atom_ptr = elf_file.atom(atom_index).?;
......@@ -67,10 +68,10 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {
6768 symbol_ptr.atom_index = atom_index;
6869
6970 const esym = &self.local_esyms.items[esym_index];
70 esym.st_shndx = atom_index;
71 esym.st_shndx = shndx;
7172 symbol_ptr.esym_index = esym_index;
7273
73 const relocs_index = @as(Atom.Index, @intCast(self.relocs.items.len));
74 const relocs_index = @as(u16, @intCast(self.relocs.items.len));
7475 const relocs = try self.relocs.addOne(gpa);
7576 relocs.* = .{};
7677 atom_ptr.relocs_section_index = relocs_index;
......@@ -86,7 +87,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {
8687 if (esym.st_shndx == elf.SHN_UNDEF) continue;
8788
8889 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {
89 const atom_index = esym.st_shndx;
90 const atom_index = self.atoms.items[esym.st_shndx];
9091 const atom = elf_file.atom(atom_index) orelse continue;
9192 if (!atom.flags.alive) continue;
9293 }
......@@ -95,7 +96,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {
9596 if (self.asFile().symbolRank(esym, false) < global.symbolRank(elf_file)) {
9697 const atom_index = switch (esym.st_shndx) {
9798 elf.SHN_ABS, elf.SHN_COMMON => 0,
98 else => esym.st_shndx,
99 else => self.atoms.items[esym.st_shndx],
99100 };
100101 const output_section_index = if (elf_file.atom(atom_index)) |atom|
101102 atom.outputShndx().?
......@@ -141,7 +142,7 @@ pub fn claimUnresolved(self: *ZigModule, elf_file: *Elf) void {
141142}
142143
143144pub fn scanRelocs(self: *ZigModule, elf_file: *Elf, undefs: anytype) !void {
144 for (self.atoms.keys()) |atom_index| {
145 for (self.atoms.items) |atom_index| {
145146 const atom = elf_file.atom(atom_index) orelse continue;
146147 if (!atom.flags.alive) continue;
147148 if (try atom.scanRelocsRequiresCode(elf_file)) {
......@@ -324,7 +325,7 @@ fn formatAtoms(
324325 _ = unused_fmt_string;
325326 _ = options;
326327 try writer.writeAll(" atoms\n");
327 for (ctx.self.atoms.keys()) |atom_index| {
328 for (ctx.self.atoms.items) |atom_index| {
328329 const atom = ctx.elf_file.atom(atom_index) orelse continue;
329330 try writer.print(" {}\n", .{atom.fmt(ctx.elf_file)});
330331 }
src/link/Elf/file.zig+1-1
......@@ -92,7 +92,7 @@ pub const File = union(enum) {
9292 pub fn atoms(file: File) []const Atom.Index {
9393 return switch (file) {
9494 .linker_defined => unreachable,
95 .zig_module => |x| x.atoms.keys(),
95 .zig_module => |x| x.atoms.items,
9696 .object => |x| x.atoms.items,
9797 };
9898 }