Commit 5c951cd211

Jakub Konka <kubkon@jakubkonka.com>
2024-01-19 13:47:39
macho: again fix symbol index dereference in codegen wrt ZigObject
1 parent a8629fb
Changed files (5)
src/arch/x86_64/Emit.zig
@@ -51,10 +51,11 @@ pub fn emitMir(emit: *Emit) Error!void {
                 } else if (emit.lower.bin_file.cast(link.File.MachO)) |macho_file| {
                     // Add relocation to the decl.
                     const atom = macho_file.getSymbol(symbol.atom_index).getAtom(macho_file).?;
+                    const sym_index = macho_file.getZigObject().?.symbols.items[symbol.sym_index];
                     try atom.addReloc(macho_file, .{
                         .tag = .@"extern",
                         .offset = end_offset - 4,
-                        .target = symbol.sym_index,
+                        .target = sym_index,
                         .addend = 0,
                         .type = .branch,
                         .meta = .{
@@ -170,7 +171,7 @@ pub fn emitMir(emit: *Emit) Error!void {
                     try atom.addReloc(macho_file, .{
                         .tag = .@"extern",
                         .offset = @intCast(end_offset - 4),
-                        .target = data.sym_index,
+                        .target = sym_index,
                         .addend = 0,
                         .type = @"type",
                         .meta = .{
src/arch/x86_64/Lower.zig
@@ -424,7 +424,8 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
                             else => unreachable,
                         };
                     } else if (lower.bin_file.cast(link.File.MachO)) |macho_file| {
-                        const macho_sym = macho_file.getSymbol(sym.sym_index);
+                        const sym_index = macho_file.getZigObject().?.symbols.items[sym.sym_index];
+                        const macho_sym = macho_file.getSymbol(sym_index);
 
                         if (macho_sym.flags.tlv) {
                             @panic("TODO lower TLS access on macOS");
src/link/MachO/synthetic.zig
@@ -24,6 +24,7 @@ pub const ZigGotSection = struct {
         const entry = &zig_got.entries.items[index];
         entry.* = sym_index;
         const symbol = macho_file.getSymbol(sym_index);
+        assert(symbol.flags.needs_zig_got);
         symbol.flags.has_zig_got = true;
         try symbol.addExtra(.{ .zig_got = index }, macho_file);
         return index;
src/link/MachO/ZigObject.zig
@@ -346,7 +346,7 @@ pub fn getDeclVAddr(
     try parent_atom.addReloc(macho_file, .{
         .tag = .@"extern",
         .offset = @intCast(reloc_info.offset),
-        .target = sym.nlist_idx,
+        .target = sym_index,
         .addend = reloc_info.addend,
         .type = .unsigned,
         .meta = .{
@@ -372,7 +372,7 @@ pub fn getAnonDeclVAddr(
     try parent_atom.addReloc(macho_file, .{
         .tag = .@"extern",
         .offset = @intCast(reloc_info.offset),
-        .target = sym.nlist_idx,
+        .target = sym_index,
         .addend = reloc_info.addend,
         .type = .unsigned,
         .meta = .{
@@ -1102,15 +1102,17 @@ pub fn getOrCreateMetadataForDecl(
     const gop = try self.decls.getOrPut(gpa, decl_index);
     if (!gop.found_existing) {
         const any_non_single_threaded = macho_file.base.comp.config.any_non_single_threaded;
+        _ = any_non_single_threaded;
         const sym_index = try self.addAtom(macho_file);
         const mod = macho_file.base.comp.module.?;
         const decl = mod.declPtr(decl_index);
+        _ = decl;
         const sym = macho_file.getSymbol(sym_index);
-        if (decl.getOwnedVariable(mod)) |variable| {
-            if (variable.is_threadlocal and any_non_single_threaded) {
-                sym.flags.tlv = true;
-            }
-        }
+        // if (decl.getOwnedVariable(mod)) |variable| {
+        //     if (variable.is_threadlocal and any_non_single_threaded) {
+        //         sym.flags.tlv = true;
+        //     }
+        // }
         if (!sym.flags.tlv) {
             sym.flags.needs_zig_got = true;
         }
src/link/MachO.zig
@@ -1617,6 +1617,8 @@ fn scanRelocs(self: *MachO) !void {
     const tracy = trace(@src());
     defer tracy.end();
 
+    if (self.getZigObject()) |zo| try zo.scanRelocs(self);
+
     for (self.objects.items) |index| {
         try self.getFile(index).?.object.scanRelocs(self);
     }