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...@@ -4316,7 +4316,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {4316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4317 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);4317 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
4318 const sym = elf_file.symbol(sym_index);4318 const sym = elf_file.symbol(sym_index);
4319 _ = try sym.getOrCreateGotEntry(elf_file);4319 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
4320 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));4320 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));
4321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });4321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });
4322 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {4322 } 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...@@ -4296,7 +4296,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {4296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4297 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);4297 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
4298 const sym = elf_file.symbol(sym_index);4298 const sym = elf_file.symbol(sym_index);
4299 _ = try sym.getOrCreateGotEntry(elf_file);4299 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
4300 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));4300 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));
4301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });4301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });
4302 } else if (self.bin_file.cast(link.File.MachO)) |_| {4302 } 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...@@ -1749,7 +1749,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
1749 .func => |func| {1749 .func => |func| {
1750 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);1750 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
1751 const sym = elf_file.symbol(sym_index);1751 const sym = elf_file.symbol(sym_index);
1752 _ = try sym.getOrCreateGotEntry(elf_file);1752 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
1753 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));1753 const got_addr = @as(u32, @intCast(sym.gotAddress(elf_file)));
1754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });1754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });
1755 _ = try self.addInst(.{1755 _ = 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...@@ -1351,7 +1351,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
1351 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {1351 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1352 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);1352 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
1353 const sym = elf_file.symbol(sym_index);1353 const sym = elf_file.symbol(sym_index);
1354 _ = try sym.getOrCreateGotEntry(elf_file);1354 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
1355 break :blk @as(u32, @intCast(sym.gotAddress(elf_file)));1355 break :blk @as(u32, @intCast(sym.gotAddress(elf_file)));
1356 } else unreachable;1356 } 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...@@ -8157,7 +8157,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
8157 const sym_index = try elf_file.getOrCreateMetadataForDecl(owner_decl);8157 const sym_index = try elf_file.getOrCreateMetadataForDecl(owner_decl);
8158 const sym = elf_file.symbol(sym_index);8158 const sym = elf_file.symbol(sym_index);
8159 sym.flags.needs_got = true;8159 sym.flags.needs_got = true;
8160 _ = try sym.getOrCreateGotEntry(elf_file);8160 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
8161 const got_addr = sym.gotAddress(elf_file);8161 const got_addr = sym.gotAddress(elf_file);
8162 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{8162 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{
8163 .base = .{ .reg = .ds },8163 .base = .{ .reg = .ds },
...@@ -10236,7 +10236,7 @@ fn genLazySymbolRef(...@@ -10236,7 +10236,7 @@ fn genLazySymbolRef(
10236 return self.fail("{s} creating lazy symbol", .{@errorName(err)});10236 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
10237 const sym = elf_file.symbol(sym_index);10237 const sym = elf_file.symbol(sym_index);
10238 sym.flags.needs_got = true;10238 sym.flags.needs_got = true;
10239 _ = try sym.getOrCreateGotEntry(elf_file);10239 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
10240 const got_addr = sym.gotAddress(elf_file);10240 const got_addr = sym.gotAddress(elf_file);
10241 const got_mem =10241 const got_mem =
10242 Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = @intCast(got_addr) });10242 Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = @intCast(got_addr) });
src/codegen.zig+1-1
...@@ -861,7 +861,7 @@ fn genDeclRef(...@@ -861,7 +861,7 @@ fn genDeclRef(
861 const sym_index = try elf_file.getOrCreateMetadataForDecl(decl_index);861 const sym_index = try elf_file.getOrCreateMetadataForDecl(decl_index);
862 const sym = elf_file.symbol(sym_index);862 const sym = elf_file.symbol(sym_index);
863 sym.flags.needs_got = true;863 sym.flags.needs_got = true;
864 _ = try sym.getOrCreateGotEntry(elf_file);864 _ = try sym.getOrCreateGotEntry(sym_index, elf_file);
865 return GenResult.mcv(.{ .memory = sym.gotAddress(elf_file) });865 return GenResult.mcv(.{ .memory = sym.gotAddress(elf_file) });
866 } else if (bin_file.cast(link.File.MachO)) |macho_file| {866 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
867 const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);867 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...@@ -1082,9 +1082,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1082 if (comp.compiler_rt_obj) |x| break :blk x.full_object_path;1082 if (comp.compiler_rt_obj) |x| break :blk x.full_object_path;
1083 break :blk null;1083 break :blk null;
1084 };1084 };
1085 if (compiler_rt_path) |path| {1085 _ = compiler_rt_path;
1086 try positionals.append(.{ .path = path });1086 // if (compiler_rt_path) |path| {
1087 }1087 // try positionals.append(.{ .path = path });
1088 // }
10881089
1089 for (positionals.items) |obj| {1090 for (positionals.items) |obj| {
1090 const in_file = try std.fs.cwd().openFile(obj.path, .{});1091 const in_file = try std.fs.cwd().openFile(obj.path, .{});
...@@ -1643,11 +1644,11 @@ fn scanRelocs(self: *Elf) !void {...@@ -1643,11 +1644,11 @@ fn scanRelocs(self: *Elf) !void {
16431644
1644 try self.reportUndefined(&undefs);1645 try self.reportUndefined(&undefs);
16451646
1646 for (self.symbols.items) |*sym| {1647 for (self.symbols.items, 0..) |*sym, sym_index| {
1647 if (sym.flags.needs_got) {1648 if (sym.flags.needs_got) {
1648 log.debug("'{s}' needs GOT", .{sym.name(self)});1649 log.debug("'{s}' needs GOT", .{sym.name(self)});
1649 // TODO how can we tell we need to write it again, aka the entry is dirty?1650 // 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);
1651 try self.got.writeEntry(self, gop.index);1652 try self.got.writeEntry(self, gop.index);
1652 }1653 }
1653 }1654 }
...@@ -2695,7 +2696,7 @@ fn updateDeclCode(...@@ -2695,7 +2696,7 @@ fn updateDeclCode(
2695 esym.st_value = atom_ptr.value;2696 esym.st_value = atom_ptr.value;
26962697
2697 sym.flags.needs_got = true;2698 sym.flags.needs_got = true;
2698 const gop = try sym.getOrCreateGotEntry(self);2699 const gop = try sym.getOrCreateGotEntry(sym_index, self);
2699 try self.got.writeEntry(self, gop.index);2700 try self.got.writeEntry(self, gop.index);
2700 }2701 }
27012702
...@@ -2930,7 +2931,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol....@@ -2930,7 +2931,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.
2930 local_esym.st_value = atom_ptr.value;2931 local_esym.st_value = atom_ptr.value;
29312932
2932 local_sym.flags.needs_got = true;2933 local_sym.flags.needs_got = true;
2933 const gop = try local_sym.getOrCreateGotEntry(self);2934 const gop = try local_sym.getOrCreateGotEntry(symbol_index, self);
2934 try self.got.writeEntry(self, gop.index);2935 try self.got.writeEntry(self, gop.index);
29352936
2936 const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr;2937 const section_offset = atom_ptr.value - self.phdrs.items[phdr_index].p_vaddr;
...@@ -3801,7 +3802,7 @@ pub fn addSymbol(self: *Elf) !Symbol.Index {...@@ -3801,7 +3802,7 @@ pub fn addSymbol(self: *Elf) !Symbol.Index {
3801 break :blk index;3802 break :blk index;
3802 }3803 }
3803 };3804 };
3804 self.symbols.items[index] = .{ .index = index };3805 self.symbols.items[index] = .{};
3805 return index;3806 return index;
3806}3807}
38073808
src/link/Elf/Atom.zig+14-6
...@@ -322,11 +322,12 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {...@@ -322,11 +322,12 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
322322
323 if (rel.r_type() == elf.R_X86_64_NONE) continue;323 if (rel.r_type() == elf.R_X86_64_NONE) continue;
324324
325 const symbol = switch (file_ptr) {325 const symbol_index = switch (file_ptr) {
326 .zig_module => |x| elf_file.symbol(x.symbol(rel.r_sym())),326 .zig_module => |x| x.symbol(rel.r_sym()),
327 .object => |x| elf_file.symbol(x.symbols.items[rel.r_sym()]),327 .object => |x| x.symbols.items[rel.r_sym()],
328 else => unreachable,328 else => unreachable,
329 };329 };
330 const symbol = elf_file.symbol(symbol_index);
330331
331 // Check for violation of One Definition Rule for COMDATs.332 // Check for violation of One Definition Rule for COMDATs.
332 if (symbol.file(elf_file) == null) {333 if (symbol.file(elf_file) == null) {
...@@ -340,7 +341,7 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {...@@ -340,7 +341,7 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
340 }341 }
341342
342 // Report an undefined symbol.343 // 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
345 // While traversing relocations, mark symbols that require special handling such as346 // While traversing relocations, mark symbols that require special handling such as
346 // pointer indirection via GOT, or a stub trampoline via PLT.347 // 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 {...@@ -379,7 +380,14 @@ pub fn scanRelocs(self: Atom, elf_file: *Elf, undefs: anytype) !void {
379}380}
380381
381// This function will report any undefined non-weak symbols that are not imports.382// 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 {
383 const rel_esym = switch (elf_file.file(self.file_index).?) {391 const rel_esym = switch (elf_file.file(self.file_index).?) {
384 .zig_module => |x| x.elfSym(rel.r_sym()).*,392 .zig_module => |x| x.elfSym(rel.r_sym()).*,
385 .object => |x| x.symtab[rel.r_sym()],393 .object => |x| x.symtab[rel.r_sym()],
...@@ -392,7 +400,7 @@ fn reportUndefined(self: Atom, elf_file: *Elf, sym: *const Symbol, rel: elf.Elf6...@@ -392,7 +400,7 @@ fn reportUndefined(self: Atom, elf_file: *Elf, sym: *const Symbol, rel: elf.Elf6
392 !sym.flags.import and400 !sym.flags.import and
393 esym.st_shndx == elf.SHN_UNDEF)401 esym.st_shndx == elf.SHN_UNDEF)
394 {402 {
395 const gop = try undefs.getOrPut(sym.index);403 const gop = try undefs.getOrPut(sym_index);
396 if (!gop.found_existing) {404 if (!gop.found_existing) {
397 gop.value_ptr.* = std.ArrayList(Atom.Index).init(elf_file.base.allocator);405 gop.value_ptr.* = std.ArrayList(Atom.Index).init(elf_file.base.allocator);
398 }406 }
src/link/Elf/Symbol.zig+3-5
...@@ -1,7 +1,5 @@...@@ -1,7 +1,5 @@
1//! Represents a defined symbol.1//! Represents a defined symbol.
22
3index: Index = 0,
4
5/// Allocated address value of this symbol.3/// Allocated address value of this symbol.
6value: u64 = 0,4value: u64 = 0,
75
...@@ -117,10 +115,10 @@ const GetOrCreateGotEntryResult = struct {...@@ -117,10 +115,10 @@ const GetOrCreateGotEntryResult = struct {
117 index: GotSection.Index,115 index: GotSection.Index,
118};116};
119117
120pub fn getOrCreateGotEntry(symbol: *Symbol, elf_file: *Elf) !GetOrCreateGotEntryResult {118pub fn getOrCreateGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateGotEntryResult {
121 assert(symbol.flags.needs_got);119 assert(symbol.flags.needs_got);
122 if (symbol.flags.has_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.got };120 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);
124 symbol.flags.has_got = true;122 symbol.flags.has_got = true;
125 return .{ .found_existing = false, .index = index };123 return .{ .found_existing = false, .index = index };
126}124}
...@@ -270,7 +268,7 @@ fn format2(...@@ -270,7 +268,7 @@ fn format2(
270 _ = options;268 _ = options;
271 _ = unused_fmt_string;269 _ = unused_fmt_string;
272 const symbol = ctx.symbol;270 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 });
274 if (symbol.file(ctx.elf_file)) |file_ptr| {272 if (symbol.file(ctx.elf_file)) |file_ptr| {
275 if (symbol.isAbs(ctx.elf_file)) {273 if (symbol.isAbs(ctx.elf_file)) {
276 if (symbol.elfSym(ctx.elf_file).st_shndx == elf.SHN_UNDEF) {274 if (symbol.elfSym(ctx.elf_file).st_shndx == elf.SHN_UNDEF) {