authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-02 00:20:56+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:10:23+01:00
log8055f687659de87ce0101cb3e459699b4541a8b7
tree19430e04b1d123a6f8946b8ea0fa88cd7452590b
parente8f522122ac83e94b2fe58aa369917f3686743c5

elf: make sure we never emit .got.zig relocs when linking object files


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

src/arch/x86_64/CodeGen.zig+2-2
......@@ -10235,7 +10235,7 @@ fn genCall(self: *Self, info: union(enum) {
1023510235 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
1023610236 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl);
1023710237 const sym = elf_file.symbol(sym_index);
10238 _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
10238 try sym.createZigGotEntry(sym_index, elf_file);
1023910239 if (self.bin_file.options.pic) {
1024010240 const callee_reg: Register = switch (resolved_cc) {
1024110241 .SysV => callee: {
......@@ -13103,7 +13103,7 @@ fn genLazySymbolRef(
1310313103 const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err|
1310413104 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
1310513105 const sym = elf_file.symbol(sym_index);
13106 _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
13106 try sym.createZigGotEntry(sym_index, elf_file);
1310713107
1310813108 if (self.bin_file.options.pic) {
1310913109 switch (tag) {
src/codegen.zig+1-1
......@@ -909,7 +909,7 @@ fn genDeclRef(
909909 }
910910 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index);
911911 const sym = elf_file.symbol(sym_index);
912 _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
912 try sym.createZigGotEntry(sym_index, elf_file);
913913 return GenResult.mcv(.{ .load_symbol = sym.esym_index });
914914 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
915915 if (is_extern) {
src/link/Elf/Symbol.zig+6
......@@ -168,11 +168,17 @@ const GetOrCreateZigGotEntryResult = struct {
168168};
169169
170170pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateZigGotEntryResult {
171 assert(!elf_file.isObject());
171172 if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.zig_got };
172173 const index = try elf_file.zig_got.addSymbol(symbol_index, elf_file);
173174 return .{ .found_existing = false, .index = index };
174175}
175176
177pub fn createZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !void {
178 if (elf_file.isObject()) return;
179 _ = try symbol.getOrCreateZigGotEntry(symbol_index, elf_file);
180}
181
176182pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) u64 {
177183 if (!symbol.flags.has_zig_got) return 0;
178184 const extras = symbol.extra(elf_file).?;
src/link/Elf/ZigObject.zig+19-17
......@@ -433,7 +433,6 @@ pub fn updateRelaSectionSizes(self: ZigObject, elf_file: *Elf) void {
433433}
434434
435435pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
436 _ = self;
437436 const gpa = elf_file.base.allocator;
438437
439438 for (&[_]?u16{
......@@ -454,18 +453,18 @@ pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
454453
455454 while (true) {
456455 for (atom.relocs(elf_file)) |rel| {
457 relocs.appendAssumeCapacity(switch (rel.r_type()) {
458 Elf.R_X86_64_ZIG_GOT32 => .{
459 .r_offset = rel.r_offset,
460 .r_addend = rel.r_addend,
461 .r_info = (@as(u64, @intCast(rel.r_sym())) << 32) | elf.R_X86_64_32,
462 },
463 Elf.R_X86_64_ZIG_GOTPCREL => .{
464 .r_offset = rel.r_offset,
465 .r_addend = rel.r_addend,
466 .r_info = (@as(u64, @intCast(rel.r_sym())) << 32) | elf.R_X86_64_PC32,
467 },
468 else => rel,
456 var r_sym = rel.r_sym() & symbol_mask;
457 if (self.isGlobal(rel.r_sym())) r_sym += @intCast(self.local_esyms.slice().len + 1);
458 const r_type = switch (rel.r_type()) {
459 Elf.R_X86_64_ZIG_GOT32,
460 Elf.R_X86_64_ZIG_GOTPCREL,
461 => unreachable, // Sanity check if we accidentally emitted those.
462 else => |r_type| r_type,
463 };
464 relocs.appendAssumeCapacity(.{
465 .r_offset = rel.r_offset,
466 .r_addend = rel.r_addend,
467 .r_info = (@as(u64, @intCast(r_sym)) << 32) | r_type,
469468 });
470469 }
471470 if (elf_file.atom(atom.prev_index)) |prev| {
......@@ -486,17 +485,20 @@ pub fn writeRelaSections(self: ZigObject, elf_file: *Elf) !void {
486485 }
487486}
488487
488pub fn isGlobal(self: ZigObject, index: Symbol.Index) bool {
489 _ = self;
490 return index & global_symbol_bit != 0;
491}
492
489493pub fn symbol(self: *ZigObject, index: Symbol.Index) Symbol.Index {
490 const is_global = index & global_symbol_bit != 0;
491494 const actual_index = index & symbol_mask;
492 if (is_global) return self.global_symbols.items[actual_index];
495 if (self.isGlobal(index)) return self.global_symbols.items[actual_index];
493496 return self.local_symbols.items[actual_index];
494497}
495498
496499pub fn elfSym(self: *ZigObject, index: Symbol.Index) *elf.Elf64_Sym {
497 const is_global = index & global_symbol_bit != 0;
498500 const actual_index = index & symbol_mask;
499 if (is_global) return &self.global_esyms.items(.elf_sym)[actual_index];
501 if (self.isGlobal(index)) return &self.global_esyms.items(.elf_sym)[actual_index];
500502 return &self.local_esyms.items(.elf_sym)[actual_index];
501503}
502504