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/link: "relocation target * not defined" for .syso symbol with LinkMode=external #30890

Open
pwaller opened this issue Mar 17, 2019 · 1 comment
Milestone

Comments

@pwaller
Copy link
Contributor

@pwaller pwaller commented Mar 17, 2019

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

$ go version
go version devel +14c3692502 Sat Mar 16 14:16:39 2019 +0000 linux/amd64

(Also tested with go1.12 and go1.12.1).

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
GOARCH="amd64"
GOBIN="/home/pwaller/.local/bin"
GOCACHE="/home/pwaller/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pwaller/go"
GOPROXY=""
GORACE=""
GOROOT="/home/pwaller/sdk/go1.12"
GOTMPDIR=""
GOTOOLDIR="/home/pwaller/sdk/go1.12/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/pwaller/.local/src/github.com/pwaller/bench-syso-broken/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-build380714510=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Tried to use the toolchain trick described in "Use syso file to embed arbitrary self-contained C code".

  1. Put code.go, code.s and code.syso in a package with the following described below.
  2. Run go build.

code.syso

I generated a .syso file in the same directory as the package with the following command: gcc -x c -c -o code.syso - <<<'void nop1() {}'. Note, I'm aware of the limitations of syso files with respect to calling convention, I'm just using this as an easy way to get an object file for purposes of demonstrating that linking is not working as I'd expect.

code.go

package main

// Comment out this line, and the main binary compiles successfully(!?).
import _ "golang.org/x/sys/unix"

func main() {
	nop()
}

func nop()

code.s

TEXT ·nop(SB),$0-0
  JMP nop1(SB)

Then I ran go build.

What did you expect to see?

A successfully compiled binary.

What did you see instead?

# github.com/pwaller/syso-broken
main.nop: relocation target nop1 not defined

Additional notes

  • If I remove the otherwise unused import of golang.org/x/sys/unix, then the binary compiles.
  • I can reproduce with both old-style GOPATH or modules.

/cc @ianlancetaylor for the linker.

@gopherbot label needsinvestigation

@pwaller

This comment has been minimized.

Copy link
Contributor Author

@pwaller pwaller commented Mar 17, 2019

By looking at the trace output what seems to be going on is that the symbols in the syso file are not being considered when using an external linker. The LinkMode is being set to external when I import x/sys/unix.

I have discovered that if I set GO_EXTLINK_ENABLED=0, then the binary does build.

go build -ldflags='-v -v' and supplementing the output by editing the linker code.

@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone Mar 17, 2019
@pwaller pwaller changed the title cmd/link: relocation target * not defined when importing a package cmd/link: "relocation target * not defined" for .syso symbol with LinkMode=external Mar 18, 2019
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.