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

cmd/cgo, cmd/link: with zig as CC/CXX, relocation target not defined, fatal error, SIGSEGV when compiling net/http #44695

Open
zikaeroh opened this issue Feb 28, 2021 · 7 comments

Comments

@zikaeroh
Copy link
Contributor

@zikaeroh zikaeroh commented Feb 28, 2021

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

$ go version
go version go1.16 linux/amd64

Does this issue reproduce with the latest release?

Yes, on tip too.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jake/.cache/go-build"
GOENV="/home/jake/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jake/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jake/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jake/testproj/helloworld/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2412854241=/tmp/go-build -gno-record-gcc-switches"
GOROOT/bin/go version: go version go1.16 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.16
uname -sr: Linux 5.11.2-zen1-1-zen
/usr/lib/libc.so.6: GNU C Library (GNU libc) release release version 2.33.
gdb --version: GNU gdb (GDB) 10.1

What did you do?

package main

import _ "net/http"

func main() {}

In my PATH, I have two scripts to work around #43078:

#!/bin/sh
exec zig cc $@
#!/bin/sh
exec zig c++ $@

Then, I compiled with CC="zcc" CXX="zxx" go build.

What did you expect to see?

A successful compilation.

What did you see instead?

# some.tld/helloworld
net(.text): relocation target __errno_location not defined
net(.text): relocation target getaddrinfo not defined
net(.text): relocation target freeaddrinfo not defined
net(.text): relocation target gai_strerror not defined
runtime/cgo(.text): relocation target stderr not defined
runtime/cgo(.text): relocation target fwrite not defined
runtime/cgo(.text): relocation target vfprintf not defined
runtime/cgo(.text): relocation target fputc not defined
runtime/cgo(.text): relocation target abort not defined
runtime/cgo(.text): relocation target pthread_create not defined
runtime/cgo(.text): relocation target nanosleep not defined
runtime/cgo(.text): relocation target pthread_detach not defined
runtime/cgo(.text): relocation target stderr not defined
runtime/cgo(.text): relocation target strerror not defined
runtime/cgo(.text): relocation target fprintf not defined
runtime/cgo(.text): relocation target abort not defined
runtime/cgo(.text): relocation target pthread_mutex_lock not defined
runtime/cgo(.text): relocation target pthread_cond_wait not defined
runtime/cgo(.text): relocation target pthread_mutex_unlock not defined
runtime/cgo(.text): relocation target pthread_cond_broadcast not defined
runtime/cgo(.text): relocation target malloc not defined
/usr/lib/go/pkg/tool/linux_amd64/link: too many errors
unexpected fault address 0x7f6e97cabafe
fatal error: fault
unexpected fault address 0x7f6e97df1cf0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x1 addr=0x7f6e97cabafe pc=0x4eb5c1]

goroutine 258 [running]:
runtime.throw(0x6dcc2d, 0x5)
	/usr/lib/go/src/runtime/panic.go:1117 +0x72 fp=0xc0009f3b08 sp=0xc0009f3ad8 pc=0x436e52
runtime.sigpanic()
	/usr/lib/go/src/runtime/signal_unix.go:741 +0x268 fp=0xc0009f3b40 sp=0xc0009f3b08 pc=0x44e548
encoding/binary.littleEndian.PutUint32(...)
	/usr/lib/go/src/encoding/binary/binary.go:73
encoding/binary.(*littleEndian).PutUint32(0x8be3c0, 0x7f6e97cabafe, 0xadc, 0x246fae, 0x4233e)
	<autogenerated>:1 +0x41 fp=0xc0009f3b60 sp=0xc0009f3b40 pc=0x4eb5c1
cmd/link/internal/ld.(*relocSymState).relocsym(0xc0009f3ea0, 0x552f, 0x7f6e97cab120, 0x14ba, 0x24798c)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:534 +0x703 fp=0xc0009f3de8 sp=0xc0009f3b60 pc=0x584183
cmd/link/internal/ld.writeBlock(0xc000114000, 0xc000e2a070, 0xc00050e000, 0xc001960418, 0x187, 0x37d, 0x507120, 0x2de50, 0xc0000b80c5, 0x1, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:953 +0x1e7 fp=0xc0009f3f10 sp=0xc0009f3de8 pc=0x58a2a7
cmd/link/internal/ld.writeBlocks.func1(0xc000114000, 0xc0023e0000, 0xc000112000, 0xc000e2a070, 0xc00050e000, 0xc001960418, 0x187, 0x37d, 0x501220, 0x2de50, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:909 +0xb1 fp=0xc0009f3f78 sp=0xc0009f3f10 pc=0x609af1
runtime.goexit()
	/usr/lib/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc0009f3f80 sp=0xc0009f3f78 pc=0x46c9e1
created by cmd/link/internal/ld.writeBlocks
	/usr/lib/go/src/cmd/link/internal/ld/data.go:908 +0x565

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc0025420a8)
	/usr/lib/go/src/runtime/sema.go:56 +0x45
sync.(*WaitGroup).Wait(0xc0025420a0)
	/usr/lib/go/src/sync/waitgroup.go:130 +0x65
cmd/link/internal/ld.asmb(0xc000114000)
	/usr/lib/go/src/cmd/link/internal/ld/asmb.go:73 +0x235
cmd/link/internal/ld.Main(0x87f340, 0x20, 0x20, 0x1, 0x7, 0x10, 0x0, 0x0, 0x6e865e, 0x1b, ...)
	/usr/lib/go/src/cmd/link/internal/ld/main.go:339 +0x1465
main.main()
	/usr/lib/go/src/cmd/link/main.go:68 +0x258

goroutine 239 [semacquire]:
sync.runtime_Semacquire(0xc0023e0008)
	/usr/lib/go/src/runtime/sema.go:56 +0x45
sync.(*WaitGroup).Wait(0xc0023e0000)
	/usr/lib/go/src/sync/waitgroup.go:130 +0x65
cmd/link/internal/ld.writeBlocks(0xc000114000, 0xc00010f340, 0xc000112000, 0xc00050e000, 0xc001960418, 0x187, 0x37d, 0x501220, 0x12e070, 0xc0000b80c5, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:924 +0x736
cmd/link/internal/ld.CodeblkPad(...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:834
cmd/link/internal/ld.asmb.func1(0xc000114000, 0xc00010f340, 0x401000, 0x12e070)
	/usr/lib/go/src/cmd/link/internal/ld/asmb.go:39 +0xd3
cmd/link/internal/ld.writeParallel.func1(0xc0025420a0, 0x6f43d0, 0xc000114000, 0xc00010f340, 0x401000, 0x12e070)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:988 +0x7a
created by cmd/link/internal/ld.writeParallel
	/usr/lib/go/src/cmd/link/internal/ld/data.go:986 +0x152

goroutine 257 [runnable]:
syscall.Syscall(0xb, 0x7f6e97ba4000, 0x34eaac, 0x0, 0x0, 0x0, 0x0)
	/usr/lib/go/src/syscall/asm_linux_amd64.s:20 +0x5
syscall.munmap(0x7f6e97ba4000, 0x34eaac, 0x7f6e97ef2aab, 0xc0001301c0)
	/usr/lib/go/src/syscall/zsyscall_linux_amd64.go:957 +0x4b
syscall.(*mmapper).Munmap(0x87b3a0, 0x7f6e97ba4000, 0x34eaac, 0x34eaac, 0x0, 0x0)
	/usr/lib/go/src/syscall/syscall_unix.go:94 +0x151
syscall.Munmap(...)
	/usr/lib/go/src/syscall/syscall_linux.go:1234
cmd/link/internal/ld.(*OutBuf).munmap(...)
	/usr/lib/go/src/cmd/link/internal/ld/outbuf_mmap.go:57
cmd/link/internal/ld.(*OutBuf).Close(0xc00010e070, 0x37, 0x0)
	/usr/lib/go/src/cmd/link/internal/ld/outbuf.go:117 +0x179
cmd/link/internal/ld.linknew.func1()
	/usr/lib/go/src/cmd/link/internal/ld/sym.go:62 +0x45
cmd/link/internal/ld.runAtExitFuncs(...)
	/usr/lib/go/src/cmd/link/internal/ld/util.go:23
cmd/link/internal/ld.Exit(0x2)
	/usr/lib/go/src/cmd/link/internal/ld/util.go:30 +0x3b
cmd/link/internal/ld.Exitf(0x6e1a43, 0xf, 0x0, 0x0, 0x0)
	/usr/lib/go/src/cmd/link/internal/ld/util.go:38 +0x106
cmd/link/internal/ld.afterErrorAction()
	/usr/lib/go/src/cmd/link/internal/ld/util.go:49 +0x7a
cmd/link/internal/loader.(*ErrorReporter).Errorf(0xc000114010, 0x23c7a, 0x6eaffe, 0x20, 0xc0009e9ae8, 0x1, 0x1)
	/usr/lib/go/src/cmd/link/internal/loader/loader.go:2619 +0x143
cmd/link/internal/ld.(*ErrorReporter).errorUnresolved(0xc000114010, 0xc00050e000, 0x23c7a, 0x23c85)
	/usr/lib/go/src/cmd/link/internal/ld/errors.go:63 +0x4e9
cmd/link/internal/ld.(*relocSymState).relocsym(0xc0009e9ea0, 0x23c7a, 0x7f6e97ba5400, 0x18b, 0x34d6ac)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:199 +0x338a
cmd/link/internal/ld.writeBlock(0xc000114000, 0xc000e2a000, 0xc00050e000, 0xc00195c000, 0xa0a, 0xc00, 0x401400, 0x100220, 0xc0000b80c5, 0x1, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:953 +0x1e7
cmd/link/internal/ld.writeBlocks.func1(0xc000114000, 0xc0023e0000, 0xc000112000, 0xc000e2a000, 0xc00050e000, 0xc00195c000, 0xa0a, 0xc00, 0x401000, 0x100220, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:909 +0xb1
created by cmd/link/internal/ld.writeBlocks
	/usr/lib/go/src/cmd/link/internal/ld/data.go:908 +0x565

goroutine 242 [semacquire]:
sync.runtime_Semacquire(0xc00249a018)
	/usr/lib/go/src/runtime/sema.go:56 +0x45
sync.(*WaitGroup).Wait(0xc00249a010)
	/usr/lib/go/src/sync/waitgroup.go:130 +0x65
cmd/link/internal/ld.writeBlocks(0xc000114000, 0xc00010f490, 0xc000112000, 0xc00050e000, 0xc0024b2640, 0x6bd, 0x6bd, 0x649000, 0x331e0, 0x8bf2e0, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:924 +0x736
cmd/link/internal/ld.writeDatblkToOutBuf(...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:1006
cmd/link/internal/ld.datblk(0xc000114000, 0xc00010f490, 0x649000, 0x331e0)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:994 +0xad
cmd/link/internal/ld.writeParallel.func1(0xc0025420a0, 0x6f43d8, 0xc000114000, 0xc00010f490, 0x649000, 0x331e0)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:988 +0x7a
created by cmd/link/internal/ld.writeParallel
	/usr/lib/go/src/cmd/link/internal/ld/data.go:986 +0x152

goroutine 35 [running]:
	goroutine running on other thread; stack unavailable
created by cmd/link/internal/ld.writeBlocks
	/usr/lib/go/src/cmd/link/internal/ld/data.go:908 +0x565

goroutine 241 [runnable]:
cmd/link/internal/loader.(*Loader).OuterSym(...)
	/usr/lib/go/src/cmd/link/internal/loader/loader.go:1733
cmd/link/internal/loader.(*Loader).AttrSubSymbol(...)
	/usr/lib/go/src/cmd/link/internal/loader/loader.go:1157
cmd/link/internal/ld.writeBlocks(0xc000114000, 0xc00010f420, 0xc000112000, 0xc00050e000, 0xc00249e008, 0x2f84, 0x2f84, 0x530000, 0x118e68, 0x8bf2e0, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:858 +0x1c7
cmd/link/internal/ld.writeDatblkToOutBuf(...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:1006
cmd/link/internal/ld.datblk(0xc000114000, 0xc00010f420, 0x530000, 0x118e68)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:994 +0xad
cmd/link/internal/ld.writeParallel.func1(0xc0025420a0, 0x6f43d8, 0xc000114000, 0xc00010f420, 0x530000, 0x118e68)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:988 +0x7a
created by cmd/link/internal/ld.writeParallel
	/usr/lib/go/src/cmd/link/internal/ld/data.go:986 +0x152
[signal SIGSEGV: segmentation violation code=0x1 addr=0x7f6e97df1cf0 pc=0x4eb642]

goroutine 35 [running]:
runtime.throw(0x6dcc2d, 0x5)
	/usr/lib/go/src/runtime/panic.go:1117 +0x72 fp=0xc0009e4b08 sp=0xc0009e4ad8 pc=0x436e52
runtime.sigpanic()
	/usr/lib/go/src/runtime/signal_unix.go:741 +0x268 fp=0xc0009e4b40 sp=0xc0009e4b08 pc=0x44e548
encoding/binary.littleEndian.PutUint64(...)
	/usr/lib/go/src/encoding/binary/binary.go:91
encoding/binary.(*littleEndian).PutUint64(0x8be3c0, 0x7f6e97df1cf0, 0x18, 0x100dbc, 0x4d04c0)
	<autogenerated>:1 +0x42 fp=0xc0009e4b60 sp=0xc0009e4b40 pc=0x4eb642
cmd/link/internal/ld.(*relocSymState).relocsym(0xc0009e4ea0, 0x7346, 0x7f6e97df1c60, 0xa8, 0x100e4c)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:536 +0x9f8 fp=0xc0009e4de8 sp=0xc0009e4b60 pc=0x584478
cmd/link/internal/ld.writeBlock(0xc000114000, 0xc00257c070, 0xc00050e000, 0xc0024b2640, 0x6bd, 0x6bd, 0x64dc60, 0x331e0, 0x8bf2e0, 0x200, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:953 +0x1e7 fp=0xc0009e4f10 sp=0xc0009e4de8 pc=0x58a2a7
cmd/link/internal/ld.writeBlocks.func1(0xc000114000, 0xc00249a010, 0xc000112000, 0xc00257c070, 0xc00050e000, 0xc0024b2640, 0x6bd, 0x6bd, 0x649000, 0x331e0, ...)
	/usr/lib/go/src/cmd/link/internal/ld/data.go:909 +0xb1 fp=0xc0009e4f78 sp=0xc0009e4f10 pc=0x609af1
runtime.goexit()
	/usr/lib/go/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc0009e4f80 sp=0xc0009e4f78 pc=0x46c9e1
created by cmd/link/internal/ld.writeBlocks
	/usr/lib/go/src/cmd/link/internal/ld/data.go:908 +0x565

If I change my code to just import net only, the panic goes away, but it still fails:

# some.tld/helloworld
runtime/cgo(.text): relocation target stderr not defined
runtime/cgo(.text): relocation target fwrite not defined
runtime/cgo(.text): relocation target vfprintf not defined
runtime/cgo(.text): relocation target fputc not defined
runtime/cgo(.text): relocation target abort not defined
runtime/cgo(.text): relocation target pthread_create not defined
runtime/cgo(.text): relocation target nanosleep not defined
runtime/cgo(.text): relocation target pthread_detach not defined
runtime/cgo(.text): relocation target stderr not defined
runtime/cgo(.text): relocation target strerror not defined
runtime/cgo(.text): relocation target fprintf not defined
runtime/cgo(.text): relocation target abort not defined
runtime/cgo(.text): relocation target pthread_mutex_lock not defined
runtime/cgo(.text): relocation target pthread_cond_wait not defined
runtime/cgo(.text): relocation target pthread_mutex_unlock not defined
runtime/cgo(.text): relocation target pthread_cond_broadcast not defined
runtime/cgo(.text): relocation target malloc not defined
runtime/cgo(.text): relocation target __errno_location not defined
runtime/cgo(.text): relocation target strerror not defined
runtime/cgo(.text): relocation target pthread_attr_init not defined
runtime/cgo(.text): relocation target pthread_attr_getstacksize not defined
/usr/lib/go/pkg/tool/linux_amd64/link: too many errors

I don't quite know the issue prefix for the title, or if this is two issues together (the relocation failing and the linker panicking/segfaulting). It's not clear to me if this is an issue with Go or with zig either; others have gotten this to work just fine (given the other thread and blog posts about it), but I have not. Compiling with GOOS=windows or GOOS=darwin with this setup works; only Linux appears to have troubles.

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Mar 1, 2021

Can you confirm how are you building for windows and darwin?
Cause if you are cross compiling you need to set CGO_ENABLED="1" or go will not use cgo and thus not use zig when compiling.
Which would explain why windows and darwin compiled fine.

@zikaeroh
Copy link
Contributor Author

@zikaeroh zikaeroh commented Mar 1, 2021

Yes, for Windows I get:

$ CGO_ENABLED=1 CC="zcc" CXX="zxx" GOOS="windows" go build
$ ls
go.mod
helloworld.exe
main.go

Darwin actually did fail, it just wasn't clear.

$ CGO_ENABLED=1 CC="zcc" CXX="zxx" GOOS="darwin" go build 
# runtime/cgo
zig: error: argument unused during compilation: '-arch x86_64' [-Werror,-Wunused-command-line-argument]

@zikaeroh
Copy link
Contributor Author

@zikaeroh zikaeroh commented Mar 1, 2021

But, I think that my Windows example isn't actually using cgo; I did a go build -x and grepped the output; zcc was never invoked, so my use of net/http doesn't actually bring in CGo, probably because Windows doesn't use the system resolver.

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Mar 1, 2021

There's a issue for it not working on windows. #43886

@zikaeroh
Copy link
Contributor Author

@zikaeroh zikaeroh commented Mar 1, 2021

Yes, thanks. I had seen that, but I wanted to start with my Linux example. We can just ignore my Darwin and Windows comments 🙂

@dmitshur dmitshur changed the title cgo, link: with zig as CC/CXX, relocation target not defined, fatal error, SIGSEGV when compiling net/http cmd/cgo, cmd/link: with zig as CC/CXX, relocation target not defined, fatal error, SIGSEGV when compiling net/http Mar 2, 2021
@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Mar 5, 2021

@dmitshur dmitshur added this to the Backlog milestone Mar 5, 2021
@motiejus
Copy link

@motiejus motiejus commented Jun 8, 2021

Another datapoint: I can't use cgo+zig to cross-compile from amd64-linux to amd64-darwin:

EDIT: created #46644.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants