authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-13 19:51:19+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-13 21:51:43+02:00
logce88df497c096f5a35200d975de855ba4502a4d8
tree05c1f0f612f5149c0a258377d704fef81389dcaa
parentdbde746f9d95f2dbc8872e2d6283c2db64ac7519

elf: do not store Symbol's index in Symbol


9 files changed, 33 insertions(+), 26 deletions(-)

src/arch/aarch64/CodeGen.zig+1-1
......@@ -4316,7 +4316,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43164316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
43174317 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
43184318 const sym = elf_file.symbol(sym_index);
4319 _ = try sym.getOrCreateGotEntry(elf_file);
4319 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
43204320 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));
43214321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });
43224322 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
src/arch/arm/CodeGen.zig+1-1
......@@ -4296,7 +4296,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
42964296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
42974297 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
42984298 const sym = elf_file.symbol(sym_index);
4299 _ = try sym.getOrCreateGotEntry(elf_file);
4299 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
43004300 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));
43014301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });
43024302 } else if (self.bin_file.cast(link.File.MachO)) |_| {
src/arch/riscv64/CodeGen.zig+1-1
......@@ -1749,7 +1749,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
17491749 .func => |func| {
17501750 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
17511751 const sym = elf_file.symbol(sym_index);
1752 _ = try sym.getOrCreateGotEntry(elf_file);
1752 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
17531753 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));
17541754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });
17551755 _ = try self.addInst(.{
src/arch/sparc64/CodeGen.zig+1-1
......@@ -1351,7 +1351,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
13511351 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
13521352 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
13531353 const sym = elf_file.symbol(sym_index);
1354 _ = try sym.getOrCreateGotEntry(elf_file);
1354 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
13551355 break :blk @as(u32, @intCast(sym.gotAddress(elf_file)));
13561356 } else unreachable;
13571357
src/arch/x86_64/CodeGen.zig+2-2
......@@ -8157,7 +8157,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
81578157 const sym_index = try elf_file.getOrCreateMetadataForDecl(owner_decl);
81588158 const sym = elf_file.symbol(sym_index);
81598159 sym.flags.needs_got = true;
8160 _ = try sym.getOrCreateGotEntry(elf_file);
8160 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
81618161 const got_addr = sym.gotAddress(elf_file);
81628162 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{
81638163 .base = .{ .reg = .ds },
......@@ -10236,7 +10236,7 @@ fn genLazySymbolRef(
1023610236 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
1023710237 const sym = elf_file.symbol(sym_index);
1023810238 sym.flags.needs_got = true;
10239 _ = try sym.getOrCreateGotEntry(elf_file);
10239 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
1024010240 const got_addr = sym.gotAddress(elf_file);
1024110241 const got_mem =
1024210242 Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = @intCast(got_addr) });
src/codegen.zig+1-1
......@@ -861,7 +861,7 @@ fn genDeclRef(
861861 const sym_index = try elf_file.getOrCreateMetadataForDecl(decl_index);
862862 const sym = elf_file.symbol(sym_index);
863863 sym.flags.needs_got = true;
864 _ = try sym.getOrCreateGotEntry(elf_file);
864 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
865865 return GenResult.mcv(.{ .memory = sym.gotAddress(elf_file) });
866866 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
867867 const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);
src/link/Elf.zig+9-8
......@@ -1082,9 +1082,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10821082 if (comp.compiler_rt_obj) |x| break :blk x.full_object_path;
10831083 break :blk null;
10841084 };
1085 if (compiler_rt_path) |path| {
1086 try positionals.append(.{ .path = path });
1087 }
1085 _ = compiler_rt_path;
1086 // if (compiler_rt_path) |path| {
1087 // try positionals.append(.{ .path = path });
1088 // }
10881089
10891090 for (positionals.items) |obj| {
10901091 const in_file = try std.fs.cwd().openFile(obj.path, .{});
......@@ -1643,11 +1644,11 @@ fn scanRelocs(self: *Elf) !void {
16431644
16441645 try self.reportUndefined(&undefs);
16451646
1646 for (self.symbols.items) |*sym| {
1647 for (self.symbols.items, 0..) |*sym, sym_index| {
16471648 if (sym.flags.needs_got) {
16481649 log.debug("'{s}' needs GOT", .{sym.name(self)});
16491650 // TODO how can we tell we need to write it again, aka the entry is dirty?
1650 const gop = try sym.getOrCreateGotEntry(self);
1651 const gop = try sym.getOrCreateGotEntry(@intCast(sym_index), self);
16511652 try self.got.writeEntry(self, gop.index);
16521653 }
16531654 }
......@@ -2695,7 +2696,7 @@ fn updateDeclCode(
26952696 esym.st_value = atom_ptr.value;
26962697
26972698 sym.flags.needs_got = true;
2698 const gop = try sym.getOrCreateGotEntry(self);
2699 const gop = try sym.getOrCreateGotEntry(sym_index, self);
26992700 try self.got.writeEntry(self, gop.index);
27002701 }
27012702
......@@ -2930,7 +2931,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
29302931 local_esym.st_value = atom_ptr.value;
29312932
29322933 local_sym.flags.needs_got = true;
2933 const gop = try local_sym.getOrCreateGotEntry(self);
2934 const gop = try local_sym.getOrCreateGotEntry(symbol_index, self);
29342935 try self.got.writeEntry(self, gop.index);
29352936
29362937 const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr;
......@@ -3801,7 +3802,7 @@ pub fn addSymbol(self: *Elf) !Symbol.Index {
38013802 break :blk index;
38023803 }
38033804 };
3804 self.symbols.items[index] = .{ .index = index };
3805 self.symbols.items[index] = .{};
38053806 return index;
38063807}
38073808
src/link/Elf/Atom.zig+14-6
......@@ -322,11 +322,12 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
322322
323323 if (rel.r_type() == elf.R_X86_64_NONE) continue;
324324
325 const symbol = switch (file_ptr) {
326 .zig_module => |x| elf_file.symbol(x.symbol(rel.r_sym())),
327 .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]),
325 const symbol_index = switch (file_ptr) {
326 .zig_module => |x| x.symbol(rel.r_sym()),
327 .object => |x| x.symbols.items[rel.r_sym()],
328328 else => unreachable,
329329 };
330 const symbol = elf_file.symbol(symbol_index);
330331
331332 // Check for violation of One Definition Rule for COMDATs.
332333 if (symbol.file(elf_file) == null) {
......@@ -340,7 +341,7 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
340341 }
341342
342343 // Report an undefined symbol.
343 try self.reportUndefined(elf_file, symbol, rel, undefs);
344 try self.reportUndefined(elf_file, symbol, symbol_index, rel, undefs);
344345
345346 // While traversing relocations, mark symbols that require special handling such as
346347 // pointer indirection via GOT, or a stub trampoline via PLT.
......@@ -379,7 +380,14 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
379380}
380381
381382// This function will report any undefined non-weak symbols that are not imports.
382fn reportUndefined(self: Atom, elf_file: *Elf, sym: *const Symbol, rel: elf.Elf64_Rela, undefs: anytype) !void {
383fn reportUndefined(
384 self: Atom,
385 elf_file: *Elf,
386 sym: *const Symbol,
387 sym_index: Symbol.Index,
388 rel: elf.Elf64_Rela,
389 undefs: anytype,
390) !void {
383391 const rel_esym = switch (elf_file.file(self.file_index).?) {
384392 .zig_module => |x| x.elfSym(rel.r_sym()).*,
385393 .object => |x| x.symtab[rel.r_sym()],
......@@ -392,7 +400,7 @@ fn reportUndefined(self: Atom, elf_file: *Elf, sym: *const Symbol, rel: elf.Elf6
392400 !sym.flags.import and
393401 esym.st_shndx == elf.SHN_UNDEF)
394402 {
395 const gop = try undefs.getOrPut(sym.index);
403 const gop = try undefs.getOrPut(sym_index);
396404 if (!gop.found_existing) {
397405 gop.value_ptr.* = std.ArrayList(Atom.Index).init(elf_file.base.allocator);
398406 }
src/link/Elf/Symbol.zig+3-5
......@@ -1,7 +1,5 @@
11//! Represents a defined symbol.
22
3index: Index = 0,
4
53/// Allocated address value of this symbol.
64value: u64 = 0,
75
......@@ -117,10 +115,10 @@ const GetOrCreateGotEntryResult = struct {
117115 index: GotSection.Index,
118116};
119117
120pub fn getOrCreateGotEntry(symbol: *Symbol, elf_file: *Elf) !GetOrCreateGotEntryResult {
118pub fn getOrCreateGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateGotEntryResult {
121119 assert(symbol.flags.needs_got);
122120 if (symbol.flags.has_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.got };
123 const index = try elf_file.got.addGotSymbol(symbol.index, elf_file);
121 const index = try elf_file.got.addGotSymbol(symbol_index, elf_file);
124122 symbol.flags.has_got = true;
125123 return .{ .found_existing = false, .index = index };
126124}
......@@ -270,7 +268,7 @@ fn format2(
270268 _ = options;
271269 _ = unused_fmt_string;
272270 const symbol = ctx.symbol;
273 try writer.print("%{d} : {s} : @{x}", .{ symbol.index, symbol.fmtName(ctx.elf_file), symbol.value });
271 try writer.print("%{d} : {s} : @{x}", .{ symbol.esym_index, symbol.fmtName(ctx.elf_file), symbol.value });
274272 if (symbol.file(ctx.elf_file)) |file_ptr| {
275273 if (symbol.isAbs(ctx.elf_file)) {
276274 if (symbol.elfSym(ctx.elf_file).st_shndx == elf.SHN_UNDEF) {