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

net: macOS link of Go c-archive now requires -lresolv in 1.20 #58159

Closed
bradfitz opened this issue Jan 31, 2023 · 26 comments
Closed

net: macOS link of Go c-archive now requires -lresolv in 1.20 #58159

bradfitz opened this issue Jan 31, 2023 · 26 comments
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@bradfitz
Copy link
Contributor

Go 1.20rc3.

I tried to convert our application (@tailscale) over to Go 1.20 but hit macOS compilation errors in Xcode for our target (a network extension) that links in Go code:

    /Applications/Xcode-13.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -target arm64-apple-macos10.13 -isysroot /Applications/Xcode-13.4.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -L/Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/BuildProductsPath/Release -F/Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/BuildProductsPath/Release -filelist /Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/IntermediateBuildFilesPath/IPN.build/Release/IPNExtension-macOS.build/Objects-normal/arm64/IPNExtension.LinkFileList -exported_symbols_list IPNExtension/exported-symbols.exp -Xlinker -rpath -Xlinker /usr/lib/swift -Xlinker -rpath -Xlinker @executable_path/../Frameworks -Xlinker -rpath -Xlinker @executable_path/../../../../Frameworks -dead_strip -Xlinker -object_path_lto -Xlinker /Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/IntermediateBuildFilesPath/IPN.build/Release/IPNExtension-macOS.build/Objects-normal/arm64/IPNExtension_lto.o -fapplication-extension -fobjc-link-runtime -L/Applications/Xcode-13.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -L/usr/lib/swift -Xlinker -add_ast_path -Xlinker /Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/IntermediateBuildFilesPath/IPN.build/Release/IPNExtension-macOS.build/Objects-normal/arm64/IPNExtension.swiftmodule -e _NSExtensionMain -lipn-go-arm64\ x86_64 -framework NetworkExtension -Xlinker -dependency_info -Xlinker /Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/IntermediateBuildFilesPath/IPN.build/Release/IPNExtension-macOS.build/Objects-normal/arm64/IPNExtension_dependency_info.dat -o /Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/IntermediateBuildFilesPath/IPN.build/Release/IPNExtension-macOS.build/Objects-normal/arm64/Binary/IPNExtension
Undefined symbols for architecture arm64:
  "_res_9_nsearch", referenced from:
      _internal/syscall/unix.libresolv_res_9_nsearch_trampoline.abi0 in libipn-go-arm64 x86_64.a(go.o)
  "_res_9_nclose", referenced from:
      _internal/syscall/unix.libresolv_res_9_nclose_trampoline.abi0 in libipn-go-arm64 x86_64.a(go.o)
  "_res_9_ninit", referenced from:
      _internal/syscall/unix.libresolv_res_9_ninit_trampoline.abi0 in libipn-go-arm64 x86_64.a(go.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

...

The following build commands failed:
	Ld /Users/github-ci/Library/Developer/Xcode/DerivedData/IPN-dcmqhkiofztpxzcjkertpymoivvw/Build/Intermediates.noindex/ArchiveIntermediates/IPN-macOS/IntermediateBuildFilesPath/IPN.build/Release/IPNExtension-macOS.build/Objects-normal/arm64/Binary/IPNExtension normal arm64 (in target 'IPNExtension-macOS' from project 'IPN')

I assume this is fallout from https://tip.golang.org/doc/go1.20#cgo ...

On macOS, the net and os/user packages have been rewritten not to use cgo: the same code is now used for cgo and non-cgo builds as well as cross-compiled builds.

Unfortunately I don't have a small easy repro. (Nothing is small or easy with Xcode)

/cc @rsc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

Can you outline the overall build here? For example, is this using -buildmode=c-archive and linking that into a C program? Are you passing -lresolv when linking the C program?

@mknyszek
Copy link
Contributor

CC @neild

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 31, 2023
@mknyszek mknyszek added this to the Go1.21 milestone Jan 31, 2023
@bcmills
Copy link
Contributor

bcmills commented Jan 31, 2023

The reference to res_9_nclose was added in CL 446179 for #50101.

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 31, 2023
@bradfitz
Copy link
Contributor Author

Yeah, this is using -buildmode=c-archive, ultimately linked into a Swift program (but Swift thinks the Go is C). I'll try to trace through the build mess to see where/whether -lresolv is set.

@ianlancetaylor
Copy link
Contributor

When linking a Go program, -lresolv is added by the line //go:cgo_ldflag "-lresolv" in internal/syscall/unix/net_darwin.go. In the past a Go c-archive built without cgo support would not require it. However, in Go 1.20, it is now required. But as far as I can recall we don't have a way for a Go c-archive to communicate which libraries it depends on to the C program.

If adding -lresolv to your command line fixes the problem, this should likely be added to the release notes and the net package documentation.

@bradfitz
Copy link
Contributor Author

bradfitz commented Jan 31, 2023

Thanks! Adding -lresolv seemed to fix it. It at least is getting further now. Still waiting for the build to finish.

Edit: yup, that did it.

@bradfitz bradfitz changed the title net: macOS compilation errors with Go 1.20rc3 (Undefined symbols ... _res_9_nsearch, etc) net: macOS link of Go c-archive now requires -lresolve in 1.20 Jan 31, 2023
@bradfitz bradfitz changed the title net: macOS link of Go c-archive now requires -lresolve in 1.20 net: macOS link of Go c-archive now requires -lresolv in 1.20 Jan 31, 2023
@xDragonZ
Copy link

xDragonZ commented Feb 8, 2023

After compile for ios using macos throwing this error too Undefined symbol: _res_9_nclose

Any idea where to add the -lresolv for gomobile?

flag provided but not defined: -lresolv

gomobile: iossimulator/amd64: go build -v -ldflags -s -w -lresolv -buildmode=c-archive -o test-iossimulator-amd64.a ./gobind failed: exit status 1

@bcmills
Copy link
Contributor

bcmills commented Feb 8, 2023

@xDragonZ, I would suggest filing a separate issue for the x/mobile maintainers to follow up on, with exact steps to reproduce the error.

(The -lresolv flag needs to be passed to the C linker, however that happens in gomobile.)

@ghost
Copy link

ghost commented Feb 9, 2023

same issue for me

CGO_ENABLED=1 \
GOOS=ios \
GOARCH=arm64 \
SDK=iphoneos \
CC=$(shell go env GOROOT)/misc/ios/clangwrap.sh \
CGO_CFLAGS="-fembed-bitcode" \
go build -trimpath -ldflags="-w -s" -lresolv -buildmode=c-archive

resut:

flag provided but not defined: -lresolv
usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
make: *** [ios-arm64] Error 2

i need help !

@bradfitz
Copy link
Contributor Author

bradfitz commented Feb 9, 2023

Try -ldflags="-w -s -lresolv" with it in the quoted ldflags part.

@ghost
Copy link

ghost commented Feb 9, 2023

-ldflags="-w -s -lresolv"在引用的 ldflags 部分尝试使用它。

CGO_ENABLED=1 \
GOOS=ios \
GOARCH=arm64 \
SDK=iphoneos \
CC=$(shell go env GOROOT)/misc/ios/clangwrap.sh \
CGO_CFLAGS="-fembed-bitcode" \
go build -trimpath -ldflags="-w -s -lresolv"  -buildmode=c-archive

no luck,it give me
flag provided but not defined: -lresolv

@ghost
Copy link

ghost commented Feb 9, 2023

btw,add CGO_LDFLAG="-lresolv" in cmd line can not fix it

@bradfitz
Copy link
Contributor Author

bradfitz commented Feb 9, 2023

Oh, extldflags. Something like go build -trimpath -buildmode=c-archive -ldflags '-w -s -extldflags "-lresolve"' IIRC but I didn't test that either.

@ghost
Copy link

ghost commented Feb 9, 2023

Oh, extldflags. Something like go build -trimpath -buildmode=c-archive -ldflags '-w -s -extldflags "-lresolve"' IIRC but I didn't test that either.

go build -trimpath -buildmode=c-archive -ldflags '-w -s "-extldflags=-lresolv" ' build success,but still got res_9_nsearch error

@bcmills
Copy link
Contributor

bcmills commented Feb 9, 2023

still got res_9_nsearch error

What was the exact error message?

@bcmills bcmills added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Feb 9, 2023
@ghost
Copy link

ghost commented Feb 10, 2023

仍然有 res_9_nsearch 错误

确切的错误信息是什么?

xcode build show errors
image
image

@jsoneaday
Copy link

When I run the given command go build -trimpath -buildmode=c-archive -ldflags '-w -s -extldflags "-lresolve"' ./src/golang it completes without error and gives me a golang.a file.
However I'm trying to do something a bit diff in that I want to get a C binary to use in Nodejs. So I tried calling go build -trimpath -buildmode=c-shared -ldflags '-w -s -extldflags "-lresolve"' ./src/golang

It gives me error

# archwayhq/keyring-go/src/golang
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: library not found for -lresolve
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am on a M1 Mac so I tried changing GOARCH to arm64, but that gives me this error package archwayhq/keyring-go/src/golang: build constraints exclude all Go files in /Users/davidchoi/Work/philabs/keyring/keyring-go/src/golang

@ianlancetaylor
Copy link
Contributor

It's -lresolv, not -lresolve.

@jsoneaday
Copy link

I just copied and pasted what was there

@ianlancetaylor
Copy link
Contributor

What you cut and pasted shows you using -lresolve, and getting an error saying library not found for -lresolve. I'm telling you that instead of using -lresolve you should use -lresolv. You are adding an "e" that shouldn't be there.

@jsoneaday
Copy link

Yeah I got it. Relax.

Not sure why lserve worked for c-archive, but removing e is working for c-shared. But now only getting the one binary, I called it keyring.so, but no h header file.

@gopherbot
Copy link

Change https://go.dev/cl/470917 mentions this issue: net: document that on macOS c-archive requires -lresolv

@jsoneaday
Copy link

Yes thank you. But I'm doing a c-shared, not c-archive. It did build but I see no h header file. If I understand correctly I need a .cc file that imports the C header and exposes the stuff to Nodejs.

Is this something new in 1.20.1 where a h header is not needed or something?

@ianlancetaylor
Copy link
Contributor

Nothing should have changed in 1.20 as far as generating a header file for c-shared goes. That sounds like a different problem that should be discussed in a different issue. Thanks.

@gopherbot
Copy link

Change https://go.dev/cl/470856 mentions this issue: doc/go1.20: mention -lresolv requirement for c-archive on macOS

gopherbot pushed a commit that referenced this issue Feb 24, 2023
For #58159

Change-Id: Iff8e017c568be111c494992cb6fe62d469e0f981
Reviewed-on: https://go-review.googlesource.com/c/go/+/470917
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopherbot pushed a commit to golang/website that referenced this issue Mar 2, 2023
For golang/go#58159

Change-Id: I4099bba337acdb82e61cddcf099aa4c1c464d387
Reviewed-on: https://go-review.googlesource.com/c/website/+/470856
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
@ianlancetaylor
Copy link
Contributor

This is now documented.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants