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) {...@@ -10235,7 +10235,7 @@ fn genCall(self: *Self, info: union(enum) {
10235 if (self.bin_file.cast(link.File.Elf)) |elf_file| {10235 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
10236 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl);10236 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl);
10237 const sym = elf_file.symbol(sym_index);10237 const sym = elf_file.symbol(sym_index);
10238 try sym.createZigGotEntry(sym_index, elf_file);10238 sym.flags.needs_zig_got = true;
10239 if (self.bin_file.options.pic) {10239 if (self.bin_file.options.pic) {
10240 const callee_reg: Register = switch (resolved_cc) {10240 const callee_reg: Register = switch (resolved_cc) {
10241 .SysV => callee: {10241 .SysV => callee: {
...@@ -13103,8 +13103,7 @@ fn genLazySymbolRef(...@@ -13103,8 +13103,7 @@ fn genLazySymbolRef(
13103 const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err|13103 const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err|
13104 return self.fail("{s} creating lazy symbol", .{@errorName(err)});13104 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
13105 const sym = elf_file.symbol(sym_index);13105 const sym = elf_file.symbol(sym_index);
13106 try sym.createZigGotEntry(sym_index, elf_file);13106 sym.flags.needs_zig_got = true;
13107
13108 if (self.bin_file.options.pic) {13107 if (self.bin_file.options.pic) {
13109 switch (tag) {13108 switch (tag) {
13110 .lea, .call => try self.genSetReg(reg, Type.usize, .{13109 .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 {...@@ -86,9 +86,13 @@ pub fn emitMir(emit: *Emit) Error!void {
86 }),86 }),
87 .linker_reloc => |data| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| {87 .linker_reloc => |data| if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| {
88 const atom = elf_file.symbol(data.atom_index).atom(elf_file).?;88 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 }
90 if (emit.lower.bin_file.options.pic) {94 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)
92 link.File.Elf.R_X86_64_ZIG_GOTPCREL96 link.File.Elf.R_X86_64_ZIG_GOTPCREL
93 else if (sym.flags.needs_got)97 else if (sym.flags.needs_got)
94 std.elf.R_X86_64_GOTPCREL98 std.elf.R_X86_64_GOTPCREL
...@@ -100,7 +104,7 @@ pub fn emitMir(emit: *Emit) Error!void {...@@ -100,7 +104,7 @@ pub fn emitMir(emit: *Emit) Error!void {
100 .r_addend = -4,104 .r_addend = -4,
101 });105 });
102 } else {106 } else {
103 const r_type: u32 = if (sym.flags.has_zig_got)107 const r_type: u32 = if (sym.flags.needs_zig_got)
104 link.File.Elf.R_X86_64_ZIG_GOT32108 link.File.Elf.R_X86_64_ZIG_GOT32
105 else if (sym.flags.needs_got)109 else if (sym.flags.needs_got)
106 std.elf.R_X86_64_GOT32110 std.elf.R_X86_64_GOT32
src/codegen.zig+1-1
...@@ -909,7 +909,7 @@ fn genDeclRef(...@@ -909,7 +909,7 @@ fn genDeclRef(
909 }909 }
910 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index);910 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index);
911 const sym = elf_file.symbol(sym_index);911 const sym = elf_file.symbol(sym_index);
912 try sym.createZigGotEntry(sym_index, elf_file);912 sym.flags.needs_zig_got = true;
913 return GenResult.mcv(.{ .load_symbol = sym.esym_index });913 return GenResult.mcv(.{ .load_symbol = sym.esym_index });
914 } else if (bin_file.cast(link.File.MachO)) |macho_file| {914 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
915 if (is_extern) {915 if (is_extern) {
src/link/Elf/Symbol.zig+2-5
...@@ -169,16 +169,12 @@ const GetOrCreateZigGotEntryResult = struct {...@@ -169,16 +169,12 @@ const GetOrCreateZigGotEntryResult = struct {
169169
170pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateZigGotEntryResult {170pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, elf_file: *Elf) !GetOrCreateZigGotEntryResult {
171 assert(!elf_file.isObject());171 assert(!elf_file.isObject());
172 assert(symbol.flags.needs_zig_got);
172 if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.zig_got };173 if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.extra(elf_file).?.zig_got };
173 const index = try elf_file.zig_got.addSymbol(symbol_index, elf_file);174 const index = try elf_file.zig_got.addSymbol(symbol_index, elf_file);
174 return .{ .found_existing = false, .index = index };175 return .{ .found_existing = false, .index = index };
175}176}
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
182pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) u64 {178pub fn zigGotAddress(symbol: Symbol, elf_file: *Elf) u64 {
183 if (!symbol.flags.has_zig_got) return 0;179 if (!symbol.flags.has_zig_got) return 0;
184 const extras = symbol.extra(elf_file).?;180 const extras = symbol.extra(elf_file).?;
...@@ -385,6 +381,7 @@ pub const Flags = packed struct {...@@ -385,6 +381,7 @@ pub const Flags = packed struct {
385 has_tlsdesc: bool = false,381 has_tlsdesc: bool = false,
386382
387 /// Whether the symbol contains .zig.got indirection.383 /// Whether the symbol contains .zig.got indirection.
384 needs_zig_got: bool = false,
388 has_zig_got: bool = false,385 has_zig_got: bool = false,
389};386};
390387
src/link/Elf/ZigObject.zig+2
...@@ -901,6 +901,7 @@ fn updateDeclCode(...@@ -901,6 +901,7 @@ fn updateDeclCode(
901 errdefer self.freeDeclMetadata(elf_file, sym_index);901 errdefer self.freeDeclMetadata(elf_file, sym_index);
902902
903 sym.value = atom_ptr.value;903 sym.value = atom_ptr.value;
904 sym.flags.needs_zig_got = true;
904 esym.st_value = atom_ptr.value;905 esym.st_value = atom_ptr.value;
905906
906 if (!elf_file.isObject()) {907 if (!elf_file.isObject()) {
...@@ -1156,6 +1157,7 @@ fn updateLazySymbol(...@@ -1156,6 +1157,7 @@ fn updateLazySymbol(
1156 errdefer self.freeDeclMetadata(elf_file, symbol_index);1157 errdefer self.freeDeclMetadata(elf_file, symbol_index);
11571158
1158 local_sym.value = atom_ptr.value;1159 local_sym.value = atom_ptr.value;
1160 local_sym.flags.needs_zig_got = true;
1159 local_esym.st_value = atom_ptr.value;1161 local_esym.st_value = atom_ptr.value;
11601162
1161 if (!elf_file.isObject()) {1163 if (!elf_file.isObject()) {