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/cgo: cannot link (cgo) on linux/arm with -no-pie #45940

Open
jochen42 opened this issue May 4, 2021 · 2 comments
Open

cmd/cgo: cannot link (cgo) on linux/arm with -no-pie #45940

jochen42 opened this issue May 4, 2021 · 2 comments

Comments

@jochen42
Copy link
Contributor

@jochen42 jochen42 commented May 4, 2021

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

$ go version

go version go1.16.3 linux/arm

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

linux/arm

go env Output
$ go env

GO111MODULE=""
GOARCH="arm"
GOBIN=""
GOCACHE="/home/pi/.cache/go-build"
GOENV="/home/pi/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/pi/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/pi/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
GOARM="6"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/pi/_src/somefancyapp/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 -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1652971902=/tmp/go-build -gno-record-gcc-switches"

What did you do?

compiling a go-module with cgo and .cxx (swig generated) with the following command on linux/arm:

CGO_LDFLAGS="-no-pie" go build \
	-v -x \
	main.go

What did you expect to see?

the cxx compiler (g++) command as a linker frontend is started with the -no-pie parameter and the without the -pie paramater

What did you see instead?

as you can see, g++ ist started with -no-pie and -pie. the second ond overwrites the first one. But my static library (mylib1) is compiled without -fPIC, so the command fails with "/usr/bin/ld: somefancyapp/libs/linux/arm/libmylib1.a(lib1.o): relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC"

g++ -I ./somefancyapp -fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b035=/tmp/go-build -gno-record-gcc-switches -o $WORK/b035/_cgo_.o $WORK/b035/_cgo_main.o $WORK/b035/_x001.o $WORK/b035/_x002.o $WORK/b035/_x003.o $WORK/b035/_x004.o -no-pie -L/home/pi/_src/somefancyapp/somepackage -L/home/pi/_src/somefancyapp/somepackage/libs/linux/arm -lMTFxpro -lmylib1 -pie

The reason

In the toolchain is a hardcoded condition wich add's -pie to the command.

I can't find any reason for the statement we need to use -pie for Linux/ARM to get accurate imported sym.

Additionaly i checked out the master branch, uncommented ot this lines, build a local patched go toolchain.
Compiling and testing my app with the patched toolchain works well.

@cagedmantis cagedmantis changed the title cannot link (cgo) on linux/arm with -no-pie cmd/cgo: cannot link (cgo) on linux/arm with -no-pie May 4, 2021
@cagedmantis cagedmantis added this to the Backlog milestone May 5, 2021
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented May 5, 2021

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented May 6, 2021

Change https://golang.org/cl/317569 mentions this issue: cmd/cgo: remove hardcoded '-pie' ldflag for linux/arm

Loading

gopherbot pushed a commit that referenced this issue Sep 20, 2021
a minimally invasive fix proposal for #45940. which keeps the fix for #26197.

an alternative for (#26197) could be to fail if we have both flags. adding/removing a flag without an message to the user is inconvenient.

Change-Id: I6ac2524d81ff57202fbe3032a53afd5106270a9e
GitHub-Last-Rev: edaf02f
GitHub-Pull-Request: #45989
Reviewed-on: https://go-review.googlesource.com/c/go/+/317569
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Bryan C. Mills <bcmills@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants