Skip to contentSkip to conversation

go-cross: Fix host contamination for x86_64 host to x86_64 target

PATCH In review Jason Wessel 2 participants 3 messages Oct 6, 2016 – Oct 12, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 go-cross: Fix host contamination for x86_64 host to x86_64 target

The go-cross package is explicitly for compiling target libraries on the host system. When the target architecture matches the host architecture it will actually use the host's linker and compiler however which can result in the generation of the cgo.a library having linker symbols which might not work properly when compiling other packages.

A typical error looks like this when building consul-migrate:

/opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/lib/x86_64-linux/go/pkg/tool/linux_amd64/link: running x86_64-yocto-linux-gcc failed: exit status 1 /opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/libexec/x86_64-yocto-linux/gcc/x86_64-yocto-linux/5.2.0/ld: /opt/build-intel-x86/tmp/work/core2-64-yocto-linux/consul-migrate/git-r0/build-tmp/go-link-956548052/000002.o: unrecognized relocation (0x2a) in section `.text' /opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/libexec/x86_64-yocto-linux/gcc/x86_64-yocto-linux/5.2.0/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

The fix is to use the make.bash --target-only option to properly build the libraries with the target toolchain. The "go" binaries are thrown away but the cross compilation libraries are preserved. The sysroot strip also has to be disabled because it will run the host strip on the target binaries.

---
 recipes-devtools/go-cross/go-cross.inc | 9 +++++++++
 1 file changed, 9 insertions(+)
recipes-devtools/go-cross/go-cross.inc +9 -0
index 613e9c7..181f4aa 100644
@@ -2,6 +2,10 @@ inherit cross
 
 DEPENDS += "go-native"
 
+# Prevent runstrip from running because you get errors when the host arch != target arch
+#INHIBIT_PACKAGE_STRIP = "1"
+STRIP = "echo"
+
 export GOHOSTOS = "${BUILD_GOOS}"
 export GOHOSTARCH = "${BUILD_GOARCH}"
 export GOOS = "${TARGET_GOOS}"
@@ -26,6 +30,11 @@ do_compile() {
 
     cd src
     ./make.bash --host-only
+    # Ensure cgo.a is built with the target toolchain
+    export GOBIN="${B}/target/bin"
+    rm -rf ${GOBIN}
+    mkdir -p ${GOBIN}
+    GO_FLAGS="-a" ./make.bash
 }
 
 do_install() {
Signature · 1 line
1.9.1
#1 lore raw
Quoted 14 lines · Jason Wessel

The go-cross package is explicitly for compiling target libraries on the host system. When the target architecture matches the host architecture it will actually use the host's linker and compiler however which can result in the generation of the cgo.a library having linker symbols which might not work properly when compiling other packages.

A typical error looks like this when building consul-migrate:

/opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/lib/x86_64-linux/go/pkg/tool/linux_amd64/link: running x86_64-yocto-linux-gcc failed: exit status 1 /opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/libexec/x86_64-yocto-linux/gcc/x86_64-yocto-linux/5.2.0/ld: /opt/build-intel-x86/tmp/work/core2-64-yocto-linux/consul-migrate/git-r0/build-tmp/go-link-956548052/000002.o: unrecognized relocation (0x2a) in section `.text' /opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/libexec/x86_64-yocto-linux/gcc/x86_64-yocto-linux/5.2.0/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

Quoted 5 lines

The fix is to use the make.bash --target-only option to properly build the libraries with the target toolchain. The "go" binaries are thrown away but the cross compilation libraries are preserved. The sysroot strip also has to be disabled because it will run the host strip on the target binaries.

The v2 patch was further tested against additional failure cases that were revealed by the v1 patch. The v1 patch had the problem that the target binary was later run on the host and it could fail to load its shared libraries. This time around the binaries are separated from the cross compilation libraries.

It is worth noting that when using a x86_64 host and a ARM target architecture a new build will fail due to sysroot contamination checks because go-cross is actually properly populating the sysroot now. A clean build is the right answer if you hit the error or manually fixing each error instance by removing the offending files.

Cheers,
Jason.

#2 lore raw
In reply to Jason Wessel

merged

Bruce

Quoted 71 lines · Jason Wessel

The go-cross package is explicitly for compiling target libraries on the host system. When the target architecture matches the host architecture it will actually use the host's linker and compiler however which can result in the generation of the cgo.a library having linker symbols which might not work properly when compiling other packages.

A typical error looks like this when building consul-migrate:

/opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/lib/ x86_64-linux/go/pkg/tool/linux_amd64/link: running x86_64-yocto-linux-gcc
failed: exit status 1 /opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/ libexec/x86_64-yocto-linux/gcc/x86_64-yocto-linux/5.2.0/ld:
/opt/build-intel-x86/tmp/work/core2-64-yocto-linux/consul- migrate/git-r0/build-tmp/go-link-956548052/000002.o: unrecognized relocation (0x2a) in section `.text' /opt/build-intel-x86/tmp/sysroots/x86_64-linux/usr/ libexec/x86_64-yocto-linux/gcc/x86_64-yocto-linux/5.2.0/ld: final link
failed: Bad value
collect2: error: ld returned 1 exit status

The fix is to use the make.bash --target-only option to properly build the libraries with the target toolchain. The "go" binaries are thrown away but the cross compilation libraries are preserved. The sysroot strip also has to be disabled because it will run the host strip on the target binaries.

---
 recipes-devtools/go-cross/go-cross.inc | 9 +++++++++
 1 file changed, 9 insertions(+)
recipes-devtools/go-cross/go-cross.inc +1 -0
index 613e9c7..181f4aa 100644
@@ -2,6 +2,10 @@ inherit cross
 
 DEPENDS += "go-native"
 
+# Prevent runstrip from running because you get errors when the host arch

!= target arch

recipes-devtools/go-cross/go-cross.inc +8 -1
+#INHIBIT_PACKAGE_STRIP = "1"
+STRIP = "echo"
+
 export GOHOSTOS = "${BUILD_GOOS}"
 export GOHOSTARCH = "${BUILD_GOARCH}"
 export GOOS = "${TARGET_GOOS}"
@@ -26,6 +30,11 @@ do_compile() {
 
     cd src
     ./make.bash --host-only
+    # Ensure cgo.a is built with the target toolchain
+    export GOBIN="${B}/target/bin"
+    rm -rf ${GOBIN}
+    mkdir -p ${GOBIN}
+    GO_FLAGS="-a" ./make.bash
 }
 
 do_install() {
--

1.9.1

--
_______________________________________________
meta-virtualization mailing list
[email protected]
https://lists.yoctoproject.org/listinfo/meta-virtualization

Signature · 2 lines
"Thou shalt not follow the NULL pointer, for chaos and madness await thee
at its end"