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: go build -race should fail if cgo is not enabled #12122

Closed
gavrie opened this issue Aug 12, 2015 · 9 comments

Comments

Projects
None yet
5 participants
@gavrie
Copy link

commented Aug 12, 2015

I'm trying to cross-compile a trivial program on my Mac:

gavriep-mac: race $ cat race.go
package main

func main() {
}
gavriep-mac: race $ GOOS=linux GOARCH=amd64 go build -x
WORK=/var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-build285258632
mkdir -p $WORK/github.com/gavrie/race/_obj/
mkdir -p $WORK/github.com/gavrie/race/_obj/exe/
cd /Users/gavriep/source/go/src/github.com/gavrie/race
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/compile -o $WORK/github.com/gavrie/race.a -trimpath $WORK -p main -complete -buildid 0f9212d067be36f95ebf6ec130acb47e2826d503 -D _/Users/gavriep/source/go/src/github.com/gavrie/race -I $WORK -pack ./race.go
cd .
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link -o $WORK/github.com/gavrie/race/_obj/exe/a.out -L $WORK -extld=clang -buildmode=exe -buildid=0f9212d067be36f95ebf6ec130acb47e2826d503 $WORK/github.com/gavrie/race.a
mv $WORK/github.com/gavrie/race/_obj/exe/a.out race

The above works fine and creates a valid Linux executable. However, doing the same thing with the -race flag fails:

gavriep-mac: race $ GOOS=linux GOARCH=amd64 go build -race -x
WORK=/var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-build476204867
mkdir -p $WORK/github.com/gavrie/race/_obj/
mkdir -p $WORK/github.com/gavrie/race/_obj/exe/
cd /Users/gavriep/source/go/src/github.com/gavrie/race
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/compile -o $WORK/github.com/gavrie/race.a -trimpath $WORK -race -p main -complete -installsuffix race -buildid b801b3275b2b7f7079aca168cd9514526a6e411a -D _/Users/gavriep/source/go/src/github.com/gavrie/race -I $WORK -pack ./race.go
cd .
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link -o $WORK/github.com/gavrie/race/_obj/exe/a.out -L $WORK -installsuffix race -extld=clang -buildmode=exe -buildid=b801b3275b2b7f7079aca168cd9514526a6e411a -race $WORK/github.com/gavrie/race.a
# github.com/gavrie/race
runtime/race(.text): __libc_malloc: not defined
runtime/race(.text): getuid: not defined
runtime/race(.text): pthread_self: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): sleep: not defined
runtime/race(.text): usleep: not defined
runtime/race(.text): abort: not defined
runtime/race(.text): isatty: not defined
runtime/race(.text): __libc_free: not defined
runtime/race(.text): getrlimit: not defined
runtime/race(.text): pipe: not defined
runtime/race(.text): __libc_stack_end: not defined
runtime/race(.text): getrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): exit: not defined
runtime/race(.text.unlikely): __errno_location: not defined
runtime/race(.text): undefined: __libc_malloc
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link: too many errors

I have not set GOROOT, and the Go version is 1.5beta3 built with Homebrew.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Aug 12, 2015

Thanks for the -x output. Please run

GOOS=linux GOARCH=amd64 go build -race -x -ldflags=-v

Thanks.

@dvyukov

This comment has been minimized.

Copy link
Member

commented Aug 12, 2015

/cc

@rsc

This comment has been minimized.

Copy link
Contributor

commented Aug 12, 2015

I'm not sure whether this can be expected to work. Either way, it did not work in Go 1.4 either, so it's not critical enough to hurry a fix for Go 1.5.

@rsc rsc modified the milestones: Go1.6Early, Go1.5Maybe Aug 12, 2015

@rsc rsc changed the title Mac to Linux cross-compilation with the race detector fails cmd/link: GOOS=linux go build -race x.go fails with link errors on darwin Aug 12, 2015

@gavrie

This comment has been minimized.

Copy link
Author

commented Aug 13, 2015

@ianlancetaylor Here is the output you requested:

gavriep-mac: race $ GOOS=linux GOARCH=amd64 go build -race -x -ldflags=-v
WORK=/var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-build075242692
mkdir -p $WORK/github.com/gavrie/race/_obj/
mkdir -p $WORK/github.com/gavrie/race/_obj/exe/
cd /Users/gavriep/source/go/src/github.com/gavrie/race
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/compile -o $WORK/github.com/gavrie/race.a -trimpath $WORK -race -p main -complete -installsuffix race -buildid b801b3275b2b7f7079aca168cd9514526a6e411a -D _/Users/gavriep/source/go/src/github.com/gavrie/race -I $WORK -pack ./race.go
cd .
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link -o $WORK/github.com/gavrie/race/_obj/exe/a.out -L $WORK -installsuffix race -extld=clang -buildmode=exe -buildid=b801b3275b2b7f7079aca168cd9514526a6e411a -v -race $WORK/github.com/gavrie/race.a
# github.com/gavrie/race
HEADER = -H5 -T0x400c00 -D0x0 -R0x1000
searching for runtime.a in $WORK/runtime.a
searching for runtime.a in /usr/local/Cellar/go/1.5beta3/libexec/pkg/linux_amd64_race/runtime.a
searching for runtime/race.a in $WORK/runtime/race.a
searching for runtime/race.a in /usr/local/Cellar/go/1.5beta3/libexec/pkg/linux_amd64_race/runtime/race.a
 0.00 ldelf /usr/local/Cellar/go/1.5beta3/libexec/pkg/linux_amd64_race/runtime/race.a(race_linux_amd64)
 0.02 deadcode
 0.04 pclntab=227555 bytes, funcdata total 43116 bytes
 0.04 dodata
 0.04 reloc
runtime/race(.text): __libc_malloc: not defined
runtime/race(.text): getuid: not defined
runtime/race(.text): pthread_self: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): madvise: not defined
runtime/race(.text): sleep: not defined
runtime/race(.text): usleep: not defined
runtime/race(.text): abort: not defined
runtime/race(.text): isatty: not defined
runtime/race(.text): __libc_free: not defined
runtime/race(.text): getrlimit: not defined
runtime/race(.text): pipe: not defined
runtime/race(.text): __libc_stack_end: not defined
runtime/race(.text): getrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): setrlimit: not defined
runtime/race(.text): exit: not defined
runtime/race(.text.unlikely): __errno_location: not defined
 0.05 asmb
 0.05 codeblk
 0.05 rodatblk
 0.05 datblk
 0.05 sym
 0.06 symsize = 15024
 0.06 symsize = 60696
 0.06 dwarf
 0.06 symsize = 60696
 0.08 dwarf pass 2.
 0.09 headr
runtime/race(.text): undefined: __libc_malloc
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link: too many errors
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Aug 13, 2015

Does this work?

GOOS=linux GOARCH=amd64 go build -race -ldflags=-linkmode=external

@gavrie

This comment has been minimized.

Copy link
Author

commented Aug 13, 2015

No, that fails with yet another error:

gavriep-mac: race $ GOOS=linux GOARCH=amd64 go build -race -ldflags=-linkmode=external
# github.com/gavrie/race
warning: unable to find runtime/cgo.a
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ld: warning: ignoring file /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-783024164/000000.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-783024164/000000.oignoring file /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-783024164/go.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-783024164/go.o

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

gavriep-mac: race $ GOOS=linux GOARCH=amd64 go build -race -ldflags='-linkmode=external -v'
# github.com/gavrie/race
HEADER = -H5 -T0x400c00 -D0x0 -R0x1000
searching for runtime.a in $WORK/runtime.a
searching for runtime.a in /usr/local/Cellar/go/1.5beta3/libexec/pkg/linux_amd64_race/runtime.a
searching for runtime/race.a in $WORK/runtime/race.a
searching for runtime/race.a in /usr/local/Cellar/go/1.5beta3/libexec/pkg/linux_amd64_race/runtime/race.a
searching for runtime/cgo.a in $WORK/runtime/cgo.a
searching for runtime/cgo.a in /usr/local/Cellar/go/1.5beta3/libexec/pkg/linux_amd64_race/runtime/cgo.a
warning: unable to find runtime/cgo.a
 0.00 deadcode
 0.01 pclntab=169243 bytes, funcdata total 43116 bytes
 0.01 dodata
 0.02 reloc
 0.02 asmb
 0.02 codeblk
 0.03 datblk
 0.03 sym
 0.03 symsize = 34392
 0.03 symsize = 35136
 0.03 dwarf
 0.03 symsize = 35136
 0.06 dwarf pass 2.
 0.08 headr
host link: "clang" "-m64" "-gdwarf-2" "-o" "/var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-build413908530/github.com/gavrie/race/_obj/exe/a.out" "-rdynamic" "-Qunused-arguments" "/var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-087480099/000000.o" "/var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-087480099/go.o"
/usr/local/Cellar/go/1.5beta3/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ld: warning: ignoring file /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-087480099/000000.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-087480099/000000.oignoring file /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-087480099/go.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/38/ky2kf2yx73qc52gryv3dzbx00000gq/T/go-link-087480099/go.o

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Aug 13, 2015

Do you have a Darwin -> GNU/Linux C cross-compiler installed on your system?

If not, this isn't going to work no matter what we do. The race detector is not written in pure Go, it requires some libc support.

@gavrie

This comment has been minimized.

Copy link
Author

commented Aug 16, 2015

No, I don't have a cross-compiler installed and wasn't aware I needed to. Now it makes sense.

@rsc rsc changed the title cmd/link: GOOS=linux go build -race x.go fails with link errors on darwin cmd/link: go build -race should fail if cgo is not enabled Nov 4, 2015

@rsc rsc modified the milestones: Go1.6, Go1.6Early Nov 4, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 17, 2015

Dup of #12844 (fixed Oct 6).

@rsc rsc closed this Dec 17, 2015

@golang golang locked and limited conversation to collaborators Dec 29, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.