golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.01k stars 17.36k forks source link

cmd/link: c-shared build mode not working with UPX #42238

Open ghost opened 3 years ago

ghost commented 3 years ago

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

go version go1.15.2 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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/go-build986805596=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I wrote a simple Golang file. is as follows:

package main
func main() {
}

Then build it : go build -buildmode=c-shared -o library.so main.go

After library.so created I want to compress it with UPX

run upx -9 library.so and got error

What did you expect to see?

92200 -> 49976 54.20% linux/amd64 library.so

What did you see instead?

upx: library.so: CantPackException: PT_NOTE 0xa71cc above stub

I opened a issue in GitHub UPX. They told me the problem the library.so does not have the correct format to allow compression.

This problem only when use -buildmode=c-shared in command-line.

Thanks.

ianlancetaylor commented 3 years ago

That's odd, since we are invoking the C linker to create the shared library.

gopherbot commented 3 years ago

Change https://golang.org/cl/265957 mentions this issue: cmd/link: merge note sections into one segment

mengzhuo commented 3 years ago

@esmailzadeh1 update ld (binutils) to 2.32 and you should able to compress c-shared binaries by upx.