authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-15 10:05:41+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-15 10:05:41+02:00
log8a0cb7002e6b8f7b50fdf7ee40311ce93fbef009
treea3a0aa5bdd1c02cb4b8193000f1c3b1b9c6f1ae5
parent2e8351cc9e30c28a9415821ed99d664b76eb2062

elf: introduce Symbol.flags.is_extern_ptr for refs potentially needing GOT


4 files changed, 11 insertions(+), 11 deletions(-)

src/arch/x86_64/Emit.zig+2-2
...@@ -114,7 +114,7 @@ pub fn emitMir(emit: *Emit) Error!void {...@@ -114,7 +114,7 @@ pub fn emitMir(emit: *Emit) Error!void {
114 const atom = zo.symbol(data.atom_index).atom(elf_file).?;114 const atom = zo.symbol(data.atom_index).atom(elf_file).?;
115 const sym = zo.symbol(data.sym_index);115 const sym = zo.symbol(data.sym_index);
116 if (emit.lower.pic) {116 if (emit.lower.pic) {
117 const r_type: u32 = if (sym.flags.needs_got)117 const r_type: u32 = if (sym.flags.is_extern_ptr)
118 @intFromEnum(std.elf.R_X86_64.GOTPCREL)118 @intFromEnum(std.elf.R_X86_64.GOTPCREL)
119 else119 else
120 @intFromEnum(std.elf.R_X86_64.PC32);120 @intFromEnum(std.elf.R_X86_64.PC32);
...@@ -124,7 +124,7 @@ pub fn emitMir(emit: *Emit) Error!void {...@@ -124,7 +124,7 @@ pub fn emitMir(emit: *Emit) Error!void {
124 .r_addend = -4,124 .r_addend = -4,
125 });125 });
126 } else {126 } else {
127 const r_type: u32 = if (sym.flags.needs_got)127 const r_type: u32 = if (sym.flags.is_extern_ptr)
128 @intFromEnum(std.elf.R_X86_64.GOT32)128 @intFromEnum(std.elf.R_X86_64.GOT32)
129 else if (sym.flags.is_tls)129 else if (sym.flags.is_tls)
130 @intFromEnum(std.elf.R_X86_64.TPOFF32)130 @intFromEnum(std.elf.R_X86_64.TPOFF32)
src/codegen.zig+1-2
...@@ -898,9 +898,8 @@ fn genNavRef(...@@ -898,9 +898,8 @@ fn genNavRef(
898 if (lf.cast(.elf)) |elf_file| {898 if (lf.cast(.elf)) |elf_file| {
899 const zo = elf_file.zigObjectPtr().?;899 const zo = elf_file.zigObjectPtr().?;
900 if (is_extern) {900 if (is_extern) {
901 // TODO audit this
902 const sym_index = try elf_file.getGlobalSymbol(name.toSlice(ip), lib_name.toSlice(ip));901 const sym_index = try elf_file.getGlobalSymbol(name.toSlice(ip), lib_name.toSlice(ip));
903 zo.symbol(sym_index).flags.needs_got = true;902 zo.symbol(sym_index).flags.is_extern_ptr = true;
904 return GenResult.mcv(.{ .load_symbol = sym_index });903 return GenResult.mcv(.{ .load_symbol = sym_index });
905 }904 }
906 const sym_index = try zo.getOrCreateMetadataForNav(elf_file, nav_index);905 const sym_index = try zo.getOrCreateMetadataForNav(elf_file, nav_index);
src/link/Elf/Symbol.zig+7-5
...@@ -447,16 +447,18 @@ pub const Flags = packed struct {...@@ -447,16 +447,18 @@ pub const Flags = packed struct {
447 needs_tlsdesc: bool = false,447 needs_tlsdesc: bool = false,
448 has_tlsdesc: bool = false,448 has_tlsdesc: bool = false,
449449
450 /// Whether the symbol is a TLS variable.
451 /// TODO this is really not needed if only we operated on esyms between
452 /// codegen and ZigObject.
453 is_tls: bool = false,
454
455 /// Whether the symbol is a merge subsection.450 /// Whether the symbol is a merge subsection.
456 merge_subsection: bool = false,451 merge_subsection: bool = false,
457452
453 /// ZigObject specific flags
458 /// Whether the symbol has a trampoline.454 /// Whether the symbol has a trampoline.
459 has_trampoline: bool = false,455 has_trampoline: bool = false,
456
457 /// Whether the symbol is a TLS variable.
458 is_tls: bool = false,
459
460 /// Whether the symbol is an extern pointer (as opposed to function).
461 is_extern_ptr: bool = false,
460};462};
461463
462pub const Extra = struct {464pub const Extra = struct {
src/link/Elf/ZigObject.zig+1-2
...@@ -1141,13 +1141,12 @@ pub fn updateNav(...@@ -1141,13 +1141,12 @@ pub fn updateNav(
1141 .variable => |variable| Value.fromInterned(variable.init),1141 .variable => |variable| Value.fromInterned(variable.init),
1142 .@"extern" => |@"extern"| {1142 .@"extern" => |@"extern"| {
1143 if (ip.isFunctionType(@"extern".ty)) return;1143 if (ip.isFunctionType(@"extern".ty)) return;
1144 // Extern variable gets a .got entry only.
1145 const sym_index = try self.getGlobalSymbol(1144 const sym_index = try self.getGlobalSymbol(
1146 elf_file,1145 elf_file,
1147 nav.name.toSlice(ip),1146 nav.name.toSlice(ip),
1148 @"extern".lib_name.toSlice(ip),1147 @"extern".lib_name.toSlice(ip),
1149 );1148 );
1150 self.symbol(sym_index).flags.needs_got = true;1149 self.symbol(sym_index).flags.is_extern_ptr = true;
1151 return;1150 return;
1152 },1151 },
1153 else => nav_val,1152 else => nav_val,