golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.87k stars 17.65k forks source link

cmd/link: cgo generates incorrect symbols on macOS 13 beta 5 #54482

Closed kevmo314 closed 2 years ago

kevmo314 commented 2 years ago

What version of Go are you using (go version)?

% go version
go version go1.19 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
% go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kevin/Library/Caches/go-build"
GOENV="/Users/kevin/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/kevin/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kevin/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9s/xwtpw2qs21163sxc3_s54_cw0000gn/T/go-build1102081114=/tmp/go-build -gno-record-gcc-switches -fno-common"
% sw_vers
ProductName:            macOS
ProductVersion:         13.0
BuildVersion:           22A5321d

What did you do?

cpoc.go

package main

// #include "cpoc.h"
import "C"
import (
    "fmt"
)

//export goLogger
func goLogger() {
    fmt.Println("goLogger")
}

func main() {
    C.cLogger()
}

cpoc.c

#include "cpoc.h"

void cLogger(void)
{
    printf("cLogger\n");
    goLogger();
}

cpoc.h

#include <stdlib.h>
#include <stdio.h>

extern void goLogger(void);

void cLogger(void);

What did you expect to see?

Correctly compiled cgo binary.

What did you see instead?

% go run cpoc.go 
# command-line-arguments
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: -no_pie is deprecated when targeting new OS versions
ld: warning: non-standard -pagezero_size is deprecated when targeting macOS 13.0 or later
Undefined symbols for architecture x86_64:
  "_cLogger", referenced from:
      __cgo_267e1da4216b_Cfunc_cLogger in 000001.o
     (maybe you meant: __cgo_267e1da4216b_Cfunc_cLogger)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm guessing this is due to the beta-ness of the OS. Happy to provide more debug info to get this working.

cherrymui commented 2 years ago

What version of the C linker are you using? I.e. the output of ld -v. Thanks.

kevmo314 commented 2 years ago
% ld -v
@(#)PROGRAM:ld  PROJECT:ld64-819.6
BUILD 15:07:11 Aug  3 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.29.102) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)
maraino commented 2 years ago

Installing Xcode-beta on macOS 12, you get the first error, but the build succeeds:

$ go build main.go
# command-line-arguments
ld: warning: -no_pie is deprecated when targeting new OS versions
$ ld -v
@(#)PROGRAM:ld  PROJECT:ld64-819.1.2
BUILD 18:34:18 Jun 30 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.28.1) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)
ping-localhost commented 2 years ago

I am running into the same issue, with the addition that it causes GoLand to fail my tests.

$ go version
go version go1.19.1 darwin/amd64
$ ld -v

@(#)PROGRAM:ld  PROJECT:ld64-819.6
BUILD 14:58:37 Aug  5 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.29.102) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)

image

k-yang commented 2 years ago

I'm having this issue too. Seems like it started happening after installing XCode 14.

go version

go version go1.19.1 darwin/amd64
ld -v

@(#)PROGRAM:ld  PROJECT:ld64-819.6
BUILD 14:58:37 Aug  5 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.29.102) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)

MacOS 12.5 (21G72)

duvis-alejandro commented 2 years ago

Tengo el mismo problema, mis configuraciones son las siguientes:

MacOs version 12.6

go version go version go1.18 darwin/amd64

ld -v @(#)PROGRAM:ld PROJECT:ld64-819.6 BUILD 14:58:37 Aug 5 2022 configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em LTO support using: LLVM version 14.0.0, (clang-1400.0.29.102) (static support for 29, runtime is 29) TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)

Mensaje de error obtenido: /usr/local/go/bin/go test -json -race ./... ld: warning: -no_pie is deprecated when targeting new OS versions

MitchStephan commented 2 years ago

Same problem after installing mac updates today :(

go version                    
go version go1.18 darwin/amd64
ld -v
@(#)PROGRAM:ld  PROJECT:ld64-764
BUILD 11:22:50 Apr 28 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 13.1.6, (clang-1316.0.21.2.5) (static support for 28, runtime is 28)
TAPI support using: Apple TAPI version 13.1.6 (tapi-1316.0.7.3)
sw_vers
ProductName:    macOS
ProductVersion: 12.6
BuildVersion:   21G115
cherrymui commented 2 years ago

For anyone who see the "same problem", are you seeing the Undefined symbols error, or just the warning about -no_pie etc.? I think they are separate issues.

I can reproduce the warning, but I have never been able to reproduce the error.

The warnings are not errors. The build should still succeed, generate a binary that should run fine. (We still want to address that. But it doesn't block you from building your code.)

Thanks.

MitchStephan commented 2 years ago

Hi! Thanks for the reply.

I was referring to ld: warning: -no_pie is deprecated when targeting new OS versions. The warning also causes go tests to fail when run via Intellij/GoLand as @ping-localhost also mentioned.

cherrymui commented 2 years ago

@MitchStephan thanks! Yeah, although it is just a warning we still want to address it. It is also affects tests in the Go distribution.

kevmo314 commented 2 years ago

@cherrymui any debug info I can help with? I'm still seeing the error which prevents the listed code snippet from running

duvis-alejandro commented 2 years ago

I don't know if this can help, however, after upgrading to MacOs Monterrey 12.6 I started getting the warning message ld: warning: -no_pie is deprecated when targeting new OS versions

In itself this shouldn't be a problem, when I run a test for a single file it shows the warning but runs the tests normally, just as I would expect. SuccessTest

However, when I try to run all the tests of the application, the warning message is taken as an error, the command used is the following: /usr/local/go/bin/go test -json -race ./...

And the result obtained is: Failed test

gopherbot commented 2 years ago

Change https://go.dev/cl/430936 mentions this issue: cmd/link: stop passing -pagezero_size to darwin linker

cherrymui commented 2 years ago

@duvis-alejandro

Does the go test command make it an error, or the IDE? Could you try running the same go test -json -race ./... command in a terminal and see if it passes (zero status)? For me the warning doesn't make it fail.

(To be clear, we still want to fix the warning. Just for better understanding. Thanks.)

gopherbot commented 2 years ago

Change https://go.dev/cl/430937 mentions this issue: cmd/link: suppress -no_pie deprecation warning on darwin

krohmag commented 2 years ago

@duvis-alejandro

Does the go test command make it an error, or the IDE? Could you try running the same go test -json -race ./... command in a terminal and see if it passes (zero status)? For me the warning doesn't make it fail.

(To be clear, we still want to fix the warning. Just for better understanding. Thanks.)

If I run tests outside of my IDE, I still get the warning, but I do not get the fail. I only get the fail on bulk test runs within the scope of my IDE, making me think it's a configuration issue in the IDE and not go that's failing the test runs.

duvis-alejandro commented 2 years ago

@krohmag When I run the tests from the terminal I get the following: Executed command test

Same warning message as in IDE, and no evidence of test execution either.

cherrymui commented 2 years ago

With the two CLs above submitted it should no longer emit the warnings. I'll work on backport the fix to 1.18 and 1.19 releases. Thanks.

gopherbot commented 2 years ago

Change https://go.dev/cl/431515 mentions this issue: [release-branch.go1.19] cmd/link: stop passing -pagezero_size to darwin linker

gopherbot commented 2 years ago

Change https://go.dev/cl/431516 mentions this issue: [release-branch.go1.18] cmd/link: stop passing -pagezero_size to darwin linker

gopherbot commented 2 years ago

Change https://go.dev/cl/431517 mentions this issue: [release-branch.go1.19] cmd/link: suppress -no_pie deprecation warning on darwin

gopherbot commented 2 years ago

Change https://go.dev/cl/431518 mentions this issue: [release-branch.go1.18] cmd/link: suppress -no_pie deprecation warning on darwin

cherrymui commented 2 years ago

@kevmo314 could you try updating Xcode? Also run go clean -cache to make sure we start from a clean state. If you still see the errors, could you provide the C compiler and linker version (clang -v, ld- v) (after update)?

kevmo314 commented 2 years ago

Sure, I updated to Xcode 14 (not beta) and am still seeing the error

kevin@MugitBook cpoc % go clean -cache
kevin@MugitBook cpoc % go run cpoc.go
# command-line-arguments
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: -no_pie is deprecated when targeting new OS versions
ld: warning: non-standard -pagezero_size is deprecated when targeting macOS 13.0 or later
Undefined symbols for architecture x86_64:
  "_cLogger", referenced from:
      __cgo_d51494f6b2b9_Cfunc_cLogger in 000001.o
     (maybe you meant: __cgo_d51494f6b2b9_Cfunc_cLogger)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

kevin@MugitBook cpoc % clang -v
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
kevin@MugitBook cpoc % ld -v
@(#)PROGRAM:ld  PROJECT:ld64-819.6
BUILD 14:58:37 Aug  5 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.29.102) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)
cherrymui commented 2 years ago

go run cpoc.go

With this command the go command only sees cpoc.go file, not the cpoc.c file. You'll need to put your go, c, and h files in a directory and use go run .. You'll need to create a go.mod file in that directory if not already (go mod init <whatever>).

kevmo314 commented 2 years ago

go run cpoc.go

With this command the go command only sees cpoc.go file, not the cpoc.c file. You'll need to put your go, c, and h files in a directory and use go run .. You'll need to create a go.mod file in that directory if not already (go mod init <whatever>).

Ohhhhh okay that solved it. That was somewhat unexpected, thanks!

duvis-alejandro commented 2 years ago

Con las dos CL anteriores enviadas, ya no debería emitir las advertencias. Trabajaré en la adaptación de la corrección a las versiones 1.18 y 1.19. Gracias.

Excuse me @cherrymui, what should I do to prevent the warnings from appearing? I have tried to reinstall go v1.18 but I get the same message

cherrymui commented 2 years ago

Excuse me, what should I do to prevent the warnings from appearing?

There are a few options:

mojotx commented 2 years ago

FWIW, I see this warning when building Go from the branch release-branch.go1.19 on macOS Monterey 12.6, with the Xcode 14 command-line tools installed. The warning causes some of the stdio tests to fail.

$ pkgutil --pkg-info=com.apple.pkg.CLTools_Executables
package-id: com.apple.pkg.CLTools_Executables
version: 14.0.0.0.1.1661618636
volume: /
location: /
install-time: 1663186173
groups: com.apple.FindSystemFiles.pkg-group
$ ld -v
@(#)PROGRAM:ld  PROJECT:ld64-819.6
BUILD 14:58:37 Aug  5 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.29.102) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)
$ sw_vers
ProductName:    macOS
ProductVersion: 12.6
BuildVersion:   21G115
##### ../misc/cgo/stdio
--- FAIL: TestTestRun (3.15s)
    stdio_test.go:60: go run /usr/local/go/test/run.go - .: exit status 1
        # go run run.go -- chain.go
        output does not match expected in chain.out. Instead saw
        # command-line-arguments
        ld: warning: -no_pie is deprecated when targeting new OS versions
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        0
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10

        FAIL    chain.go    2.347s
        # go run run.go -- fib.go
        output does not match expected in fib.out. Instead saw
        # command-line-arguments
        ld: warning: -no_pie is deprecated when targeting new OS versions
        0
        1
        1
        2
        3
        5
        8
        13
        21
        34
        55
        89
        144
        233
        377
        610
        987
        1597
        2584
        4181
        6765
        10946
        17711
        28657
        46368
        75025
        121393
        196418
        317811
        514229
        832040
        1346269
        2178309
        3524578
        5702887
        9227465
        14930352
        24157817
        39088169
        63245986
        102334155
        165580141
        267914296
        433494437
        701408733
        1134903170
        1836311903
        2971215073
        4807526976
        7778742049
        12586269025
        20365011074
        32951280099
        53316291173
        86267571272
        139583862445
        225851433717
        365435296162
        591286729879
        956722026041
        1548008755920
        2504730781961
        4052739537881
        6557470319842
        10610209857723
        17167680177565
        27777890035288
        44945570212853
        72723460248141
        117669030460994
        190392490709135
        308061521170129
        498454011879264
        806515533049393
        1304969544928657
        2111485077978050
        3416454622906707
        5527939700884757
        8944394323791464
        14472334024676221
        23416728348467685
        37889062373143906
        61305790721611591
        99194853094755497
        160500643816367088
        259695496911122585
        420196140727489673
        679891637638612258
        1100087778366101931
        1779979416004714189
        2880067194370816120

        FAIL    fib.go  2.237s
        # go run run.go -- hello.go
        output does not match expected in hello.out. Instead saw
        # command-line-arguments
        ld: warning: -no_pie is deprecated when targeting new OS versions
        hello, world

        FAIL    hello.go    2.124s
        exit status 1
FAIL
go tool dist: Failed: exit status 1
huanghy6268 commented 2 years ago

Excuse me @cherrymui, if I need to use -race flag as well in the command go test like this go test -buildmode=pie -race, is will tigger another error:

-buildmode=pie not supported when -race is enabled

have any suggestion to solve this conflict if I want prevent the warnings and pass -race?

Deleplace commented 1 year ago

I upgraded from go1.19 darwin/amd64 to go1.19.3 darwin/arm64 and this solved the problem for me. Thank you

gopherbot commented 1 year ago

Change https://go.dev/cl/461697 mentions this issue: cmd/go: default to PIE linking on darwin/amd64