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 {...@@ -887,7 +887,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {
887 } });887 } });
888 self.zig_module_index = index;888 self.zig_module_index = index;
889 const zig_module = self.file(index).?.zig_module;889 const zig_module = self.file(index).?.zig_module;
890890 try zig_module.atoms.append(gpa, 0); // null input section
891 const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_mod.root_src_path));891 const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_mod.root_src_path));
892 const symbol_index = try self.addSymbol();892 const symbol_index = try self.addSymbol();
893 try zig_module.local_symbols.append(gpa, symbol_index);893 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...@@ -1319,7 +1319,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1319 // the relocations, and commit objects to file.1319 // the relocations, and commit objects to file.
1320 if (self.zig_module_index) |index| {1320 if (self.zig_module_index) |index| {
1321 const zig_module = self.file(index).?.zig_module;1321 const zig_module = self.file(index).?.zig_module;
1322 for (zig_module.atoms.keys()) |atom_index| {1322 for (zig_module.atoms.items) |atom_index| {
1323 const atom_ptr = self.atom(atom_index).?;1323 const atom_ptr = self.atom(atom_index).?;
1324 if (!atom_ptr.flags.alive) continue;1324 if (!atom_ptr.flags.alive) continue;
1325 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];1325 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];
...@@ -3245,7 +3245,7 @@ pub fn updateDeclExports(...@@ -3245,7 +3245,7 @@ pub fn updateDeclExports(
3245 };3245 };
3246 const esym = &zig_module.global_esyms.items[sym_index & 0x0fffffff];3246 const esym = &zig_module.global_esyms.items[sym_index & 0x0fffffff];
3247 esym.st_value = decl_sym.value;3247 esym.st_value = decl_sym.value;
3248 esym.st_shndx = decl_sym.atom_index;3248 esym.st_shndx = decl_esym.st_shndx;
3249 esym.st_info = (stb_bits << 4) | stt_bits;3249 esym.st_info = (stb_bits << 4) | stt_bits;
3250 esym.st_name = name_off;3250 esym.st_name = name_off;
3251 }3251 }
src/link/Elf/Atom.zig+6-8
...@@ -14,13 +14,13 @@ size: u64 = 0,...@@ -14,13 +14,13 @@ size: u64 = 0,
14alignment: Alignment = .@"1",14alignment: Alignment = .@"1",
1515
16/// Index of the input section.16/// Index of the input section.
17input_section_index: Index = 0,17input_section_index: u16 = 0,
1818
19/// Index of the output section.19/// Index of the output section.
20output_section_index: u16 = 0,20output_section_index: u16 = 0,
2121
22/// Index of the input section containing this atom's relocs.22/// Index of the input section containing this atom's relocs.
23relocs_section_index: Index = 0,23relocs_section_index: u16 = 0,
2424
25/// Index of this atom in the linker's atoms table.25/// Index of this atom in the linker's atoms table.
26atom_index: Index = 0,26atom_index: Index = 0,
...@@ -216,7 +216,6 @@ pub fn free(self: *Atom, elf_file: *Elf) void {...@@ -216,7 +216,6 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
216 log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) });216 log.debug("freeAtom {d} ({s})", .{ self.atom_index, self.name(elf_file) });
217217
218 const gpa = elf_file.base.allocator;218 const gpa = elf_file.base.allocator;
219 const zig_module = self.file(elf_file).?.zig_module;
220 const shndx = self.outputShndx().?;219 const shndx = self.outputShndx().?;
221 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;220 const meta = elf_file.last_atom_and_free_list_table.getPtr(shndx).?;
222 const free_list = &meta.free_list;221 const free_list = &meta.free_list;
...@@ -267,7 +266,9 @@ pub fn free(self: *Atom, elf_file: *Elf) void {...@@ -267,7 +266,9 @@ pub fn free(self: *Atom, elf_file: *Elf) void {
267266
268 // TODO create relocs free list267 // TODO create relocs free list
269 self.freeRelocs(elf_file);268 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));
271 self.* = .{};272 self.* = .{};
272}273}
273274
...@@ -698,10 +699,7 @@ fn format2(...@@ -698,10 +699,7 @@ fn format2(
698 }699 }
699}700}
700701
701// TODO this has to be u32 but for now, to avoid redesigning elfSym machinery for702pub const Index = u32;
702// ZigModule, keep it at u16 with the intention of bumping it to u32 in the near
703// future.
704pub const Index = u16;
705703
706pub const Flags = packed struct {704pub const Flags = packed struct {
707 /// Specifies whether this atom is alive or has been garbage collected.705 /// 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) = .{},...@@ -13,7 +13,7 @@ local_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
13global_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},13global_symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
14globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{},14globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{},
1515
16atoms: std.AutoArrayHashMapUnmanaged(Atom.Index, void) = .{},16atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
17relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(elf.Elf64_Rela)) = .{},17relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(elf.Elf64_Rela)) = .{},
1818
19output_symtab_size: Elf.SymtabSize = .{},19output_symtab_size: Elf.SymtabSize = .{},
...@@ -56,7 +56,8 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {...@@ -56,7 +56,8 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {
56 const symbol_index = try elf_file.addSymbol();56 const symbol_index = try elf_file.addSymbol();
57 const esym_index = try self.addLocalEsym(gpa);57 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);
60 try self.local_symbols.append(gpa, symbol_index);61 try self.local_symbols.append(gpa, symbol_index);
6162
62 const atom_ptr = elf_file.atom(atom_index).?;63 const atom_ptr = elf_file.atom(atom_index).?;
...@@ -67,10 +68,10 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {...@@ -67,10 +68,10 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {
67 symbol_ptr.atom_index = atom_index;68 symbol_ptr.atom_index = atom_index;
6869
69 const esym = &self.local_esyms.items[esym_index];70 const esym = &self.local_esyms.items[esym_index];
70 esym.st_shndx = atom_index;71 esym.st_shndx = shndx;
71 symbol_ptr.esym_index = esym_index;72 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));
74 const relocs = try self.relocs.addOne(gpa);75 const relocs = try self.relocs.addOne(gpa);
75 relocs.* = .{};76 relocs.* = .{};
76 atom_ptr.relocs_section_index = relocs_index;77 atom_ptr.relocs_section_index = relocs_index;
...@@ -86,7 +87,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {...@@ -86,7 +87,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {
86 if (esym.st_shndx == elf.SHN_UNDEF) continue;87 if (esym.st_shndx == elf.SHN_UNDEF) continue;
8788
88 if (esym.st_shndx != elf.SHN_ABS and esym.st_shndx != elf.SHN_COMMON) {89 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];
90 const atom = elf_file.atom(atom_index) orelse continue;91 const atom = elf_file.atom(atom_index) orelse continue;
91 if (!atom.flags.alive) continue;92 if (!atom.flags.alive) continue;
92 }93 }
...@@ -95,7 +96,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {...@@ -95,7 +96,7 @@ pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {
95 if (self.asFile().symbolRank(esym, false) < global.symbolRank(elf_file)) {96 if (self.asFile().symbolRank(esym, false) < global.symbolRank(elf_file)) {
96 const atom_index = switch (esym.st_shndx) {97 const atom_index = switch (esym.st_shndx) {
97 elf.SHN_ABS, elf.SHN_COMMON => 0,98 elf.SHN_ABS, elf.SHN_COMMON => 0,
98 else => esym.st_shndx,99 else => self.atoms.items[esym.st_shndx],
99 };100 };
100 const output_section_index = if (elf_file.atom(atom_index)) |atom|101 const output_section_index = if (elf_file.atom(atom_index)) |atom|
101 atom.outputShndx().?102 atom.outputShndx().?
...@@ -141,7 +142,7 @@ pub fn claimUnresolved(self: *ZigModule, elf_file: *Elf) void {...@@ -141,7 +142,7 @@ pub fn claimUnresolved(self: *ZigModule, elf_file: *Elf) void {
141}142}
142143
143pub fn scanRelocs(self: *ZigModule, elf_file: *Elf, undefs: anytype) !void {144pub fn scanRelocs(self: *ZigModule, elf_file: *Elf, undefs: anytype) !void {
144 for (self.atoms.keys()) |atom_index| {145 for (self.atoms.items) |atom_index| {
145 const atom = elf_file.atom(atom_index) orelse continue;146 const atom = elf_file.atom(atom_index) orelse continue;
146 if (!atom.flags.alive) continue;147 if (!atom.flags.alive) continue;
147 if (try atom.scanRelocsRequiresCode(elf_file)) {148 if (try atom.scanRelocsRequiresCode(elf_file)) {
...@@ -324,7 +325,7 @@ fn formatAtoms(...@@ -324,7 +325,7 @@ fn formatAtoms(
324 _ = unused_fmt_string;325 _ = unused_fmt_string;
325 _ = options;326 _ = options;
326 try writer.writeAll(" atoms\n");327 try writer.writeAll(" atoms\n");
327 for (ctx.self.atoms.keys()) |atom_index| {328 for (ctx.self.atoms.items) |atom_index| {
328 const atom = ctx.elf_file.atom(atom_index) orelse continue;329 const atom = ctx.elf_file.atom(atom_index) orelse continue;
329 try writer.print(" {}\n", .{atom.fmt(ctx.elf_file)});330 try writer.print(" {}\n", .{atom.fmt(ctx.elf_file)});
330 }331 }
src/link/Elf/file.zig+1-1
...@@ -92,7 +92,7 @@ pub const File = union(enum) {...@@ -92,7 +92,7 @@ pub const File = union(enum) {
92 pub fn atoms(file: File) []const Atom.Index {92 pub fn atoms(file: File) []const Atom.Index {
93 return switch (file) {93 return switch (file) {
94 .linker_defined => unreachable,94 .linker_defined => unreachable,
95 .zig_module => |x| x.atoms.keys(),95 .zig_module => |x| x.atoms.items,
96 .object => |x| x.atoms.items,96 .object => |x| x.atoms.items,
97 };97 };
98 }98 }