Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgo build failed when cc='zig cc' #20243

Open
jiacai2050 opened this issue Jun 9, 2024 · 5 comments
Open

cgo build failed when cc='zig cc' #20243

jiacai2050 opened this issue Jun 9, 2024 · 5 comments
Labels
bug Observed behavior contradicts documented or intended behavior linking os-macos
Milestone

Comments

@jiacai2050
Copy link
Contributor

jiacai2050 commented Jun 9, 2024

Zig Version

0.14.0-dev.6+0ba64e9ce

Steps to Reproduce and Observed Behavior

package main

// #include <stdio.h>
// #include <stdlib.h>
//
// static void myprint(char* s) {
//   printf("%s\n", s);
// }
import "C"
import "unsafe"

func main() {
	cs := C.CString("Hello from stdio")
	C.myprint(cs)
	C.free(unsafe.Pointer(cs))
}

Given this file, compile with

CC='zig cc' go build main.go

it will throw following errors on my machine:

# command-line-arguments
/Users/jiacai/.asdf/installs/golang/1.22.4/go/pkg/tool/darwin_arm64/link: running zig failed: exit status 1
error: symbol _runtime.covctrs not attached to any (sub)section
    note: while parsing /var/folders/50/12rf3hkj2n10whhf8l63nnyh0000gn/T/go-link-3691128890/go.o

Expected Behavior

Build OK

$ go version
go version go1.22.4 darwin/arm64
@jiacai2050 jiacai2050 added the bug Observed behavior contradicts documented or intended behavior label Jun 9, 2024
@JamesParrott
Copy link

JamesParrott commented Jun 10, 2024

I can reproduce this with Zig 0.13, on both MacOS 12 and 13.

Run go env -w "CC=zig cc"
  go env -w "CC=zig cc"
  go build -o hello main.go
  shell: /bin/bash -e {0}
# command-line-arguments
/Users/runner/hostedtoolcache/go/1.22.3/x64/pkg/tool/darwin_amd64/link: running zig failed: exit status 1
error: symbol _runtime.covctrs not attached to any (sub)section
    note: while parsing /var/folders/vy/h7r6h43j203gstfh6fj9_tyh0000gn/T/go-link-1939405894/go.o

https://github.com/JamesParrott/CGo_Zig_bug/actions/runs/9445915792/job/26014475941

On MacOS 14, compilation was thwarted by something different (see below). I think the free MacOS 14 runners might be ARM, possibly even Apple-M1. CGo plus Zig seems to require more configuration on MacOS than on Ubuntu or Windows, whatever the host arch is.

  go env -w "CC=zig cc"
  go build  -o hello main.go
  shell: /bin/bash -e {0}
# runtime/cgo
error: unknown target CPU 'westmere'
...
(list of cpu names)

@dan-smetana
Copy link

@kubkon

@kubkon kubkon added this to the 0.14.0 milestone Jul 1, 2024
@dan-smetana
Copy link

@kubkon For me this happens when targeting x86_64 as well.

@kubkon kubkon removed the arch-aarch64 64-bit ARM label Jul 1, 2024
@kubkon
Copy link
Member

kubkon commented Jul 1, 2024

@kubkon For me this happens when targeting x86_64 as well.

Fixed labels, thanks.

@dan-smetana
Copy link

I'm sure this is not the proper way to fix this, but this patch (essentially doing the reverse of @kubkon 's commit that introduced this error) does result in fully functional binaries for me.

diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig
index c856c65d4e..0b246a22fa 100644
--- a/src/link/MachO/Object.zig
+++ b/src/link/MachO/Object.zig
@@ -647,7 +647,7 @@ pub fn findAtom(self: Object, addr: u64) ?Atom.Index {
     return null;
 }
 
-fn findAtomInSection(self: Object, addr: u64, n_sect: u8) ?Atom.Index {
+fn findAtomInSection(self: Object, addr: u64, n_sect: u8) Atom.Index {
     const tracy = trace(@src());
     defer tracy.end();
     const slice = self.sections.slice();
@@ -682,22 +682,15 @@ fn findAtomInSection(self: Object, addr: u64, n_sect: u8) ?Atom.Index {
         if (sub_addr == addr or (sub_addr < addr and addr < sub_addr + sub_size)) return sub.atom;
     }
 
-    return null;
+    return subsections.items[subsections.items.len - 1].atom;
 }
 
-fn linkNlistToAtom(self: *Object, macho_file: *MachO) !void {
+fn linkNlistToAtom(self: *Object, _: *MachO) !void {
     const tracy = trace(@src());
     defer tracy.end();
     for (self.symtab.items(.nlist), self.symtab.items(.atom)) |nlist, *atom| {
         if (!nlist.stab() and nlist.sect()) {
-            if (self.findAtomInSection(nlist.n_value, nlist.n_sect - 1)) |atom_index| {
-                atom.* = atom_index;
-            } else {
-                try macho_file.reportParseError2(self.index, "symbol {s} not attached to any (sub)section", .{
-                    self.getString(nlist.n_strx),
-                });
-                return error.MalformedObject;
-            }
+            atom.* = self.findAtomInSection(nlist.n_value, nlist.n_sect - 1);
         }
     }
 }
@@ -2339,12 +2332,7 @@ const x86_64 = struct {
                     @as(i64, @intCast(sect.addr)) + rel.r_address + addend + 4
                 else
                     addend;
-                const target = self.findAtomInSection(@intCast(taddr), @intCast(nsect)) orelse {
-                    try macho_file.reportParseError2(self.index, "{s},{s}: 0x{x}: bad relocation", .{
-                        sect.segName(), sect.sectName(), rel.r_address,
-                    });
-                    return error.MalformedObject;
-                };
+                const target = self.findAtomInSection(@intCast(taddr), @intCast(nsect));
                 addend = taddr - @as(i64, @intCast(macho_file.getAtom(target).?.getInputAddress(macho_file)));
                 break :blk target;
             } else self.symbols.items[rel.r_symbolnum];
@@ -2526,12 +2514,7 @@ const aarch64 = struct {
                     @as(i64, @intCast(sect.addr)) + rel.r_address + addend
                 else
                     addend;
-                const target = self.findAtomInSection(@intCast(taddr), @intCast(nsect)) orelse {
-                    try macho_file.reportParseError2(self.index, "{s},{s}: 0x{x}: bad relocation", .{
-                        sect.segName(), sect.sectName(), rel.r_address,
-                    });
-                    return error.MalformedObject;
-                };
+                const target = self.findAtomInSection(@intCast(taddr), @intCast(nsect));
                 addend = taddr - @as(i64, @intCast(macho_file.getAtom(target).?.getInputAddress(macho_file)));
                 break :blk target;
             } else self.symbols.items[rel.r_symbolnum];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior linking os-macos
Projects
None yet
Development

No branches or pull requests

4 participants