Commit 8a0cb7002e

Jakub Konka <kubkon@jakubkonka.com>
2024-08-15 10:05:41
elf: introduce Symbol.flags.is_extern_ptr for refs potentially needing GOT
1 parent 2e8351c
Changed files (4)
src/arch/x86_64/Emit.zig
@@ -114,7 +114,7 @@ pub fn emitMir(emit: *Emit) Error!void {
                     const atom = zo.symbol(data.atom_index).atom(elf_file).?;
                     const sym = zo.symbol(data.sym_index);
                     if (emit.lower.pic) {
-                        const r_type: u32 = if (sym.flags.needs_got)
+                        const r_type: u32 = if (sym.flags.is_extern_ptr)
                             @intFromEnum(std.elf.R_X86_64.GOTPCREL)
                         else
                             @intFromEnum(std.elf.R_X86_64.PC32);
@@ -124,7 +124,7 @@ pub fn emitMir(emit: *Emit) Error!void {
                             .r_addend = -4,
                         });
                     } else {
-                        const r_type: u32 = if (sym.flags.needs_got)
+                        const r_type: u32 = if (sym.flags.is_extern_ptr)
                             @intFromEnum(std.elf.R_X86_64.GOT32)
                         else if (sym.flags.is_tls)
                             @intFromEnum(std.elf.R_X86_64.TPOFF32)
src/link/Elf/Symbol.zig
@@ -447,16 +447,18 @@ pub const Flags = packed struct {
     needs_tlsdesc: bool = false,
     has_tlsdesc: bool = false,
 
-    /// Whether the symbol is a TLS variable.
-    /// TODO this is really not needed if only we operated on esyms between
-    /// codegen and ZigObject.
-    is_tls: bool = false,
-
     /// Whether the symbol is a merge subsection.
     merge_subsection: bool = false,
 
+    /// ZigObject specific flags
     /// Whether the symbol has a trampoline.
     has_trampoline: bool = false,
+
+    /// Whether the symbol is a TLS variable.
+    is_tls: bool = false,
+
+    /// Whether the symbol is an extern pointer (as opposed to function).
+    is_extern_ptr: bool = false,
 };
 
 pub const Extra = struct {
src/link/Elf/ZigObject.zig
@@ -1141,13 +1141,12 @@ pub fn updateNav(
         .variable => |variable| Value.fromInterned(variable.init),
         .@"extern" => |@"extern"| {
             if (ip.isFunctionType(@"extern".ty)) return;
-            // Extern variable gets a .got entry only.
             const sym_index = try self.getGlobalSymbol(
                 elf_file,
                 nav.name.toSlice(ip),
                 @"extern".lib_name.toSlice(ip),
             );
-            self.symbol(sym_index).flags.needs_got = true;
+            self.symbol(sym_index).flags.is_extern_ptr = true;
             return;
         },
         else => nav_val,
src/codegen.zig
@@ -898,9 +898,8 @@ fn genNavRef(
     if (lf.cast(.elf)) |elf_file| {
         const zo = elf_file.zigObjectPtr().?;
         if (is_extern) {
-            // TODO audit this
             const sym_index = try elf_file.getGlobalSymbol(name.toSlice(ip), lib_name.toSlice(ip));
-            zo.symbol(sym_index).flags.needs_got = true;
+            zo.symbol(sym_index).flags.is_extern_ptr = true;
             return GenResult.mcv(.{ .load_symbol = sym_index });
         }
         const sym_index = try zo.getOrCreateMetadataForNav(elf_file, nav_index);