Skip to content

Commit 19a932c

Browse files
committed
cmd/link: don't mark shared library symbols reachable unconditionally
During the transitioning period, we mark symbols from Go shared libraries reachable unconditionally. That might be useful when there was still a large portion of the linker using sym.Symbols, and only reachable symbols were converted to sym.Symbols. Marking them reachable brings them to the dynamic symbol table, even if they are not needed, increased the binary size unexpectedly. That time has passed. Now we largely operate on loader symbols, and it is not needed to mark them reachable anymore. Fixes #40416. Change-Id: I1e2bdb93a960ba7dc96575fabe15af93d8e95329 Reviewed-on: https://go-review.googlesource.com/c/go/+/244839 Run-TryBot: Cherry Zhang <[email protected]> Reviewed-by: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 8696ae8 commit 19a932c

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

misc/cgo/testshared/shared_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ func TestTrivialExecutable(t *testing.T) {
462462
run(t, "trivial executable", "../../bin/trivial")
463463
AssertIsLinkedTo(t, "../../bin/trivial", soname)
464464
AssertHasRPath(t, "../../bin/trivial", gorootInstallDir)
465+
checkSize(t, "../../bin/trivial", 100000) // it is 19K on linux/amd64, 100K should be enough
465466
}
466467

467468
// Build a trivial program in PIE mode that links against the shared runtime and check it runs.
@@ -470,6 +471,18 @@ func TestTrivialExecutablePIE(t *testing.T) {
470471
run(t, "trivial executable", "./trivial.pie")
471472
AssertIsLinkedTo(t, "./trivial.pie", soname)
472473
AssertHasRPath(t, "./trivial.pie", gorootInstallDir)
474+
checkSize(t, "./trivial.pie", 100000) // it is 19K on linux/amd64, 100K should be enough
475+
}
476+
477+
// Check that the file size does not exceed a limit.
478+
func checkSize(t *testing.T, f string, limit int64) {
479+
fi, err := os.Stat(f)
480+
if err != nil {
481+
t.Fatalf("stat failed: %v", err)
482+
}
483+
if sz := fi.Size(); sz > limit {
484+
t.Errorf("file too large: got %d, want <= %d", sz, limit)
485+
}
473486
}
474487

475488
// Build a division test program and check it runs.

src/cmd/link/internal/ld/lib.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,17 +2191,6 @@ func ldshlibsyms(ctxt *Link, shlib string) {
21912191
l.SetSymElfType(s, elf.ST_TYPE(elfsym.Info))
21922192
su.SetSize(int64(elfsym.Size))
21932193
if elfsym.Section != elf.SHN_UNDEF {
2194-
// If it's not undefined, mark the symbol as reachable
2195-
// so as to protect it from dead code elimination,
2196-
// even if there aren't any explicit references to it.
2197-
// Under the previous sym.Symbol based regime this
2198-
// wasn't necessary, but for the loader-based deadcode
2199-
// it is definitely needed.
2200-
//
2201-
// FIXME: have a more general/flexible mechanism for this?
2202-
//
2203-
l.SetAttrReachable(s, true)
2204-
22052194
// Set .File for the library that actually defines the symbol.
22062195
l.SetSymPkg(s, libpath)
22072196

0 commit comments

Comments
 (0)