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: add internal linking support for pie build mode on arm #50405

Open
hollowaykeanho opened this issue Jan 1, 2022 · 8 comments
Open
Labels
arch-arm Issues solely affecting the 32-bit arm architecture. compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@hollowaykeanho
Copy link

hollowaykeanho commented Jan 1, 2022

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

$ go version
go version go1.17.5 linux/amd64

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
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/u0/Documents/monteur/.monteurFS/bin/gopath/bin"
GOCACHE="/home/u0/Documents/monteur/.monteurFS/bin/gocache"
GOENV="/home/u0/Documents/monteur/.monteurFS/bin/goenv"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/u0/Documents/monteur/.monteurFS/bin/gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/u0/Documents/monteur/.monteurFS/bin/gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/u0/Documents/monteur/.monteurFS/bin/golang"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/u0/Documents/monteur/.monteurFS/bin/golang/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/u0/Documents/monteur/gopkg/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/user/1000/go-build3616176871=/tmp/go-build -gno-record-gcc-switches"

What did you do?

So I try to cross-compile a pure Go repository to armv7 CPU on amd64 host system with -buildmode=pie, the compiler keeps on seeking for arm based gcc compiler eventhough the entire project does not have cgo codes at all. The command I issued:

GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
go build -buildmode=pie \
-ldflags "-s -w" \
 -o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"

I even tried to explicitly instruct the compiler that I'm not using Cgo:

CGO_ENABLED=0 \
GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
go build -buildmode=pie \
-ldflags "-s -w" \
 -o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"

What did you expect to see?

Should be working fine without needing to install arm compiler tool-chain like the workaround (see below).

What did you see instead?

Both the above produced the following outputs:

# command-line-arguments
loadinternal: cannot find runtime/cgo
/home/u0/Documents/monteur/.monteurFS/bin/golang/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
gcc: error: unrecognized command-line option ‘-marm’; did you mean ‘-mabm’?

There are 2 ways to workaround the issue:

Number 1: don't build pie mode

GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
go build \
-ldflags "-s -w" \
 -o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"

However, some packagers are great at complaining non-pie binary (e.g. debuild).

Number 2: install arm gcc toolchain

On Debian:

$ su
$ apt install gcc-arm-linux-gnueabi binutils-arm-linux-gnueabi -y
$ exit

Then:

CGO_ENABLED=0 \ 
GOOS="linux" \
GOARCH="arm" \
GOARM="7" \
CC=arm-linux-gnueabi-gcc \
go build \
-ldflags "-s -w" \
 -o "/home/u0/Documents/monteur/.monteurFS/tmp/build/linux-arm" \
"/home/u0/Documents/monteur/gopkg/app/monteur/main.go"

Gone through Resources

Gone through the following resources but found no outcome yet... still searching...

  1. https://www.reddit.com/r/golang/comments/gszi0d/best_way_to_cross_compile_a_golang_app_onto_a/
  2. https://groups.google.com/g/golang-nuts/c/gQ6ArCTlPGU
  3. https://stackoverflow.com/questions/61515186/when-using-cgo-enabled-is-must-and-what-happens
  4. https://dubo-dubon-duponey.medium.com/a-beginners-guide-to-cross-compiling-static-cgo-pie-binaries-golang-1-16-792eea92d5aa
  5. https://groups.google.com/g/golang-nuts/c/cXhRsmNsMwo/m/VUZlLqo9AwAJ
  6. https://groups.google.com/g/golang-nuts/c/Jd9tlNc6jUE/m/Z9ldF6vPEAAJ
@seankhliao
Copy link
Member

what if you set CGO_ENABLED=0 ?

@hollowaykeanho
Copy link
Author

hollowaykeanho commented Jan 1, 2022

what if you set CGO_ENABLED=0 ?

Same result. See above. Possible Bug?

@ALTree
Copy link
Member

ALTree commented Jan 1, 2022

-buildmode=pie defaults to external linking except for linux/amd64 and linux/arm64:

The linker now defaults to internal linking mode for -buildmode=pie on linux/amd64 and linux/arm64, so these configurations no longer require a C linker.

arm, which you're targeting, uses external linking, so it requires a cross-compilation toolchain for pie, regardless of cgo usage.

@hollowaykeanho
Copy link
Author

arm, which you're targeting, uses external linking, so it requires a cross-compilation toolchain for pie

Noted. Any plans to make the linking internally? It's a very useful tool without depending on external tool-chains. Will close this ticket after the reply.

@ALTree
Copy link
Member

ALTree commented Jan 2, 2022

I'm not sure if there are any plans to extend internal linking to other GOARCHs, maybe @cherrymui knows.

@erifan
Copy link

erifan commented Jan 2, 2022

Will close this ticket after the reply.

Don't need to close the issue, maybe change the title to something like "cmd/link: add internal linking support for pie build mode on arm"

@hollowaykeanho hollowaykeanho changed the title go build with -buildmode=pie cross-compile to GOARM=7 always seek out CGo for a Pure Go Project cmd/link: add internal linking support for pie build mode on arm Jan 2, 2022
@hollowaykeanho
Copy link
Author

hollowaykeanho commented Jan 2, 2022

Noted. Updated title.

Thanks for the feedback and happy new year @seankhliao, @ALTree , and @erifan. Let's wait after Jan 4th for the quiet week. You guys need it. =)

@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 4, 2022
@cherrymui
Copy link
Member

I'm not sure if there are any plans to extend internal linking to other GOARCHs

I'm not aware of any plan. But CLs are always welcome. Thanks.

@cherrymui cherrymui added FeatureRequest arch-arm Issues solely affecting the 32-bit arm architecture. labels Jan 4, 2022
@cherrymui cherrymui added this to the Unplanned milestone Jan 4, 2022
vbouchaud added a commit to vbouchaud/k8s-ldap-auth that referenced this issue Mar 29, 2022
vbouchaud added a commit to vbouchaud/k8s-ldap-auth that referenced this issue Mar 29, 2022
vbouchaud added a commit to vbouchaud/nfs-client-provisioner that referenced this issue May 9, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-arm Issues solely affecting the 32-bit arm architecture. compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants