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: -buildmode=pie -linkshared generates large binaries #40416

Closed
kiap opened this issue Jul 26, 2020 · 7 comments
Closed

cmd/link: -buildmode=pie -linkshared generates large binaries #40416

kiap opened this issue Jul 26, 2020 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@kiap
Copy link

kiap commented Jul 26, 2020

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

$ go version

go version go1.15rc1 linux/amd64

Does this issue reproduce with the latest release?

Yes

This was working before on 1.14.6 and was working on every every version

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/usr/src/.cache"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/usr/src/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/src/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/src/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/src/go/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-build647301757=/tmp/go-build -gno-record-gcc-switches"

What did you do?

go install -v -ldflags '-s -w' -gcflags '-trimpath /usr/src/go' -buildmode=shared -linkshared -pkgdir /usr/src/lib std

go install -v -ldflags '-s -w' -gcflags '-trimpath /usr/src/go' -buildmode=pie -linkshared -pkgdir /usr/src/lib prog

What did you expect to see?

The binary should be linked correctly and builds linked to standard library

What did you see instead?

static executable

@ianlancetaylor
Copy link
Contributor

CC @thanm @cherrymui @jeremyfaller

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 26, 2020
@ianlancetaylor ianlancetaylor added this to the Go1.15 milestone Jul 26, 2020
@ianlancetaylor
Copy link
Contributor

Marking as a release blocker for 1.15. We should investigate this and decide whether it is a problem that needs to be addressed for 1.1.5

@cherrymui
Copy link
Member

Sorry, I cannot reproduce it on my machine:

$ go install -buildmode=shared -linkshared -pkgdir /tmp/lib std
$ go build -buildmode=pie -linkshared -pkgdir /tmp/lib hello.go 
$ file hello
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=b2e8af86bc78040632dfe47c792077a16659905d, for GNU/Linux 3.2.0, not stripped
$ ldd hello | grep libstd
	libstd.so => /tmp/lib/libstd.so (0x00007ffa3ff33000)

The generated binary is dynamically linked and linked against libstd.so.

@cherrymui cherrymui added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 27, 2020
@kiap
Copy link
Author

kiap commented Jul 27, 2020

Have you checked the executable file size?

despite ldd's output,

When I build hello world shared linked with 1.14.6 the size is 19 KB
with 1.15rc1 it is 4.8M if I use trimpath and ldflags; without them it is 9.4 MB

It seems the file also contains statically linked modules, try the same on 1.14.6 and compare executable file sizes and content of a simple hello world.

When I build statically the size is 2.3 MB

To the contrary the shared executable must be smaller in size

@aclements
Copy link
Member

@kiap , just the clarify, can you state the exact problem you're seeing more clearly? It's not clear from your original bug report. Is it specifically that the shared linkage executable is significantly larger than it used to be, indicating the linker included a lot of things it shouldn't have? Are you seeing problems besides the size? (When you say it "generates incorrect code", that suggests a crash or panic when you try to run it.)

@gopherbot
Copy link

Change https://golang.org/cl/244839 mentions this issue: cmd/link: don't mark shared library symbols reachable unconditionally

@cherrymui
Copy link
Member

Thanks for the note about the size. It is not that we linked in a static copy, but we accidentally put a large number of symbols into the dynamic symbol table.

@cherrymui cherrymui removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 27, 2020
@aclements aclements changed the title cmd/link: -buildmode=pie -linkshared generates incorrect code cmd/link: -buildmode=pie -linkshared generates large binaries Jul 27, 2020
@golang golang locked and limited conversation to collaborators Jul 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Projects
None yet
Development

No branches or pull requests

5 participants