Facing linker issues while cross-compiling from Ubuntu/amd64 to darwin/amd64

229 views
Skip to first unread message

Kedar Babar

unread,
Jan 5, 2017, 10:28:16 AM1/5/17
to golang-nuts
Hi,
    I had written a small go program, which refers the 'C' code, following is the snippet

package main

/*
#include <netdb.h>
*/
import "C"
import "fmt"

const cgoAddrInfoFlags = (C.AI_CANONNAME | C.AI_V4MAPPED | C.AI_ALL) & C.AI_MASK

func main() {
    fmt.Println("Hello")
}

When i build the above program, i get the below linking errors for the target darwin/amd64.
/usr/lib/go-1.6/pkg/tool/linux_amd64/link: running o64-clang failed: exit status 1
ld: warning: ignoring file /tmp/go-link-601229876/000001.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): /tmp/go-link-601229876/000001.o
Undefined symbols for architecture x86_64:
  "__cgo_wait_runtime_init_done", referenced from:
      _callbackTrampoline in 000000.o
      _stepTrampoline in 000000.o
      _doneTrampoline in 000000.o
  "_x_cgo_free", referenced from:
      __cgo_free in go.o
  "_x_cgo_init", referenced from:
      __cgo_init in go.o
  "_x_cgo_malloc", referenced from:
      __cgo_malloc in go.o
  "_x_cgo_notify_runtime_init_done", referenced from:
      __cgo_notify_runtime_init_done in go.o
  "_x_cgo_setenv", referenced from:
      runtime._cgo_setenv in go.o
  "_x_cgo_thread_start", referenced from:
      __cgo_thread_start in go.o
  "_x_cgo_unsetenv", referenced from:
      runtime._cgo_unsetenv in go.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Following is the go command used to build the above stuff.
env CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC=o64-clang go build -x -work -ldflags="-extld=o64-clang"



Ian Lance Taylor

unread,
Jan 5, 2017, 10:37:27 AM1/5/17
to Kedar Babar, golang-nuts
I don't know what the problem is, but the first step is to look at the
direct invocations of the C compiler in the `go build -x` output and
make sure that they look right. Make sure that nothing is calling
"gcc" directly. Make sure that the invocations of "o64-clang" have
any required flags.

Ian

Koichi Shiraishi (zchee)

unread,
Jan 5, 2017, 7:34:30 PM1/5/17
to golang-nuts
Hi, Kedar.

Is o64-clang is probably osxcross? If so, it seems to the same situation on my zchee/docker-machine-driver-xhyve projects CircleCI build testing. yml is here. circle.yml
This project using "Hypervisor.framework" and "vmnet.framework"(which darwin specific Libraries), so also uses cgo build. But successfully on Ubuntu xenial in the docker container.

I'm made the small Dockerfile that tiny wrapper of osxcross runtime for this project, called osxcc.

It is not necessary to actually build your code with using it, and I don't know why undefined some symbols. But I hope you find shell commands in Dockerfile informative.

- zchee aka Koichi Shiraishi

Kedar Babar

unread,
Jan 6, 2017, 7:05:42 AM1/6/17
to golang-nuts, kedar...@gmail.com
Thanks for looking in the issue. I run the go build with the -x, but no where it is calling the gcc. Following is full output

WORK=/tmp/go-build815513256
mkdir -p $WORK/goresearch/mynet/_obj/
mkdir -p $WORK/goresearch/mynet/_obj/exe/
cd /home/administrator/GoWork/src/goresearch/mynet
CGO_LDFLAGS="-g" "-O2" "-lm" "-ldl" "-lstdc++" "-ll" "-lc++" "-lc" "-lexslt" "-lgmalloc" "-lobjc" "-lpcap" "-lpthread" "-lSystem" "-ltls" "-lutil" "-lz" "-lxml2" /usr/lib/go-1.6/pkg/tool/linux_amd64/cgo -objdir $WORK/goresearch/mynet/_obj/ -importpath goresearch/mynet -- -I $WORK/goresearch/mynet/_obj/ -std=gnu99 mynet.go
o64-clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common -I $WORK/goresearch/mynet/_obj/ -g -O2 -std=gnu99 -o $WORK/goresearch/mynet/_obj/_cgo_main.o -c $WORK/goresearch/mynet/_obj/_cgo_main.c
o64-clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common -I $WORK/goresearch/mynet/_obj/ -g -O2 -std=gnu99 -o $WORK/goresearch/mynet/_obj/_cgo_export.o -c $WORK/goresearch/mynet/_obj/_cgo_export.c
o64-clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common -I $WORK/goresearch/mynet/_obj/ -g -O2 -std=gnu99 -o $WORK/goresearch/mynet/_obj/mynet.cgo2.o -c $WORK/goresearch/mynet/_obj/mynet.cgo2.c
o64-clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common -o $WORK/goresearch/mynet/_obj/_cgo_.o $WORK/goresearch/mynet/_obj/_cgo_main.o $WORK/goresearch/mynet/_obj/_cgo_export.o $WORK/goresearch/mynet/_obj/mynet.cgo2.o -g -O2 -lm -ldl -lstdc++ -ll -lc++ -lc -lexslt -lgmalloc -lobjc -lpcap -lpthread -lSystem -ltls -lutil -lz -lxml2
/usr/lib/go-1.6/pkg/tool/linux_amd64/cgo -objdir $WORK/goresearch/mynet/_obj/ -dynpackage main -dynimport $WORK/goresearch/mynet/_obj/_cgo_.o -dynout $WORK/goresearch/mynet/_obj/_cgo_import.go
o64-clang -I . -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common -o $WORK/goresearch/mynet/_obj/_all.o $WORK/goresearch/mynet/_obj/_cgo_export.o $WORK/goresearch/mynet/_obj/mynet.cgo2.o -g -O2 -Wl,-r -nostdlib
/usr/lib/go-1.6/pkg/tool/linux_amd64/compile -o $WORK/goresearch/mynet.a -trimpath $WORK -p main -buildid a01d97609ce6d71f052c7746576b9b68357f6099 -D _/home/administrator/GoWork/src/goresearch/mynet -I $WORK -pack $WORK/goresearch/mynet/_obj/_cgo_gotypes.go $WORK/goresearch/mynet/_obj/mynet.cgo1.go $WORK/goresearch/mynet/_obj/_cgo_import.go
pack r $WORK/goresearch/mynet.a $WORK/goresearch/mynet/_obj/_all.o # internal
cd .
/usr/lib/go-1.6/pkg/tool/linux_amd64/link -o $WORK/goresearch/mynet/_obj/exe/a.out -L $WORK -extld=o64-clang -buildmode=exe -buildid=a01d97609ce6d71f052c7746576b9b68357f6099 -extld=o64-clang $WORK/goresearch/mynet.a
# goresearch/mynet

/usr/lib/go-1.6/pkg/tool/linux_amd64/link: running o64-clang failed: exit status 1
ld: warning: ignoring file /tmp/go-link-989097505/000001.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): /tmp/go-link-989097505/000001.o

Undefined symbols for architecture x86_64:
  "_x_cgo_free", referenced from:
      __cgo_free in go.o
  "_x_cgo_init", referenced from:
      __cgo_init in go.o
  "_x_cgo_malloc", referenced from:
      __cgo_malloc in go.o
  "_x_cgo_notify_runtime_init_done", referenced from:
      __cgo_notify_runtime_init_done in go.o
  "_x_cgo_setenv", referenced from:
      runtime._cgo_setenv in go.o
  "_x_cgo_thread_start", referenced from:
      __cgo_thread_start in go.o
  "_x_cgo_unsetenv", referenced from:
      runtime._cgo_unsetenv in go.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


The above output states that it is creating the ELF file format, which it is not able to understand. I tried different linker options also, but did not succeed.

Ian Lance Taylor

unread,
Jan 6, 2017, 10:38:55 AM1/6/17
to Kedar Babar, golang-nuts
I'm not sure but I think this error means that you have an existing
installed package that was built without using o64-clang. Try using
`go build -a` to force all packages to be rebuilt.

Ian
Reply all
Reply to author
Forward
0 new messages