authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-02 12:46:37+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:11:29+01:00
logccb2afacc00264897cc474befc13f36239c91b03
tree9721a74a2212395012b11ea5224ee6bc502c9de8
parent5affd29b4799712d4cbf00231ed52de4034b345d

elf: postpone creation of .got.zig entry until code emit


5 files changed, 14 insertions(+), 12 deletions(-)

src/arch/x86_64/CodeGen.zig+2-3
......@@ -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.createZigGotEntry(sym_index, elf_file);
10238 sym.flags.needs_zig_got = true;
1023910239 if (self.bin_file.options.pic) {
1024010240 const callee_reg: Register = switch (resolved_cc) {
1024110241 .SysV => callee: {
......@@ -13103,8 +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.createZigGotEntry(sym_index, elf_file);
13107
13106 sym.flags.needs_zig_got = true;
1310813107 if (self.bin_file.options.pic) {
1310913108 switch (tag) {
1311013109 .lea, .call => try self.genSetReg(reg, Type.usize, .{
src/arch/x86_64/Emit.zig+7-3
......@@ -86,9 +86,13 @@ pub fn emitMir(emit: *Emit) Error!void {
8686 }),
8787 .linker_reloc => |data| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| {
8888 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;
89 const sym = elf_file.symbol(elf_file.zigObjectPtr().?.symbol(data.sym_index));
89 const sym_index = elf_file.zigObjectPtr().?.symbol(data.sym_index);
90 const sym = elf_file.symbol(sym_index);
91 if (sym.flags.needs_zig_got and emit.lower.bin_file.options.effectiveOutputMode() != .Obj) {
92 _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
93 }
9094 if (emit.lower.bin_file.options.pic) {
91 const r_type: u32 = if (sym.flags.has_zig_got)
95 const r_type: u32 = if (sym.flags.needs_zig_got)
9296 link.File.Elf.R_X86_64_ZIG_GOTPCREL
9397 else if (sym.flags.needs_got)
9498 std.elf.R_X86_64_GOTPCREL
......@@ -100,7 +104,7 @@ pub fn emitMir(emit: *Emit) Error!void {
100104 .r_addend = -4,
101105 });
102106 } else {
103 const r_type: u32 = if (sym.flags.has_zig_got)
107 const r_type: u32 = if (sym.flags.needs_zig_got)
104108 link.File.Elf.R_X86_64_ZIG_GOT32
105109 else if (sym.flags.needs_got)
106110 std.elf.R_X86_64_GOT32
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.createZigGotEntry(sym_index, elf_file);
912 sym.flags.needs_zig_got = true;
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+2-5
......@@ -169,16 +169,12 @@ const GetOrCreateZigGotEntryResult = struct {
169169
170170pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateZigGotEntryResult {
171171 assert(!elf_file.isObject());
172 assert(symbol.flags.needs_zig_got);
172173 if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.zig_got };
173174 const index = try elf_file.zig_got.addSymbol(symbol_index, elf_file);
174175 return .{ .found_existing = false, .index = index };
175176}
176177
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
182178pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) u64 {
183179 if (!symbol.flags.has_zig_got) return 0;
184180 const extras = symbol.extra(elf_file).?;
......@@ -385,6 +381,7 @@ pub const Flags = packed struct {
385381 has_tlsdesc: bool = false,
386382
387383 /// Whether the symbol contains .zig.got indirection.
384 needs_zig_got: bool = false,
388385 has_zig_got: bool = false,
389386};
390387
src/link/Elf/ZigObject.zig+2
......@@ -901,6 +901,7 @@ fn updateDeclCode(
901901 errdefer self.freeDeclMetadata(elf_file, sym_index);
902902
903903 sym.value = atom_ptr.value;
904 sym.flags.needs_zig_got = true;
904905 esym.st_value = atom_ptr.value;
905906
906907 if (!elf_file.isObject()) {
......@@ -1156,6 +1157,7 @@ fn updateLazySymbol(
11561157 errdefer self.freeDeclMetadata(elf_file, symbol_index);
11571158
11581159 local_sym.value = atom_ptr.value;
1160 local_sym.flags.needs_zig_got = true;
11591161 local_esym.st_value = atom_ptr.value;
11601162
11611163 if (!elf_file.isObject()) {