1
0
mirror of https://github.com/golang/go synced 2024-11-23 23:40:13 -07:00

cmd/link: avoid duplicate DT_NEEDED entries

When adding a new library entry, ensure we record it as seen to avoid
adding duplicates of it.

Fixes #39256

Change-Id: Id309adf80c533d78fd485517c18bc9ab5f1d29fb
Reviewed-on: https://go-review.googlesource.com/c/go/+/235257
Run-TryBot: Joel Sing <joel@sing.id.au>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Joel Sing 2020-05-26 14:40:44 +10:00
parent 681559e1f1
commit 9138a2a67f
4 changed files with 86 additions and 0 deletions

View File

@ -2378,6 +2378,7 @@ func elfadddynsym(ldr *loader.Loader, target *Target, syms *ArchSyms, s loader.S
if target.Arch.Family == sys.AMD64 && !cgoeDynamic && dil != "" && !seenlib[dil] {
du := ldr.MakeSymbolUpdater(syms.Dynamic)
Elfwritedynent(target.Arch, du, DT_NEEDED, uint64(dstru.Addstring(dil)))
seenlib[dil] = true
}
} else {

View File

@ -13,6 +13,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
@ -77,3 +78,57 @@ func main() {
t.Fatalf("Unexpected sh info, want greater than 0, got: %d", section.Info)
}
}
func TestNoDuplicateNeededEntries(t *testing.T) {
testenv.MustHaveGoBuild(t)
testenv.MustHaveCGO(t)
// run this test on just a small set of platforms (no need to test it
// across the board given the nature of the test).
pair := runtime.GOOS + "-" + runtime.GOARCH
switch pair {
case "linux-amd64", "freebsd-amd64", "openbsd-amd64":
default:
t.Skip("no need for test on " + pair)
}
t.Parallel()
dir, err := ioutil.TempDir("", "no-dup-needed")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(dir)
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
path := filepath.Join(dir, "x")
argv := []string{"build", "-o", path, filepath.Join(wd, "testdata", "issue39256")}
out, err := exec.Command(testenv.GoToolPath(t), argv...).CombinedOutput()
if err != nil {
t.Fatalf("Build failure: %s\n%s\n", err, string(out))
}
f, err := elf.Open(path)
if err != nil {
t.Fatalf("Failed to open ELF file: %v", err)
}
libs, err := f.ImportedLibraries()
if err != nil {
t.Fatalf("Failed to read imported libraries: %v", err)
}
var count int
for _, lib := range libs {
if lib == "libc.so" {
count++
}
}
if got, want := count, 1; got != want {
t.Errorf("Got %d entries for `libc.so`, want %d", got, want)
}
}

View File

@ -0,0 +1,20 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
_ "unsafe"
)
//go:cgo_import_dynamic libc_getpid getpid "libc.so"
//go:cgo_import_dynamic libc_kill kill "libc.so"
//go:cgo_import_dynamic libc_close close "libc.so"
//go:cgo_import_dynamic libc_open open "libc.so"
func trampoline()
func main() {
trampoline()
}

View File

@ -0,0 +1,10 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
TEXT ·trampoline(SB),0,$0
CALL libc_getpid(SB)
CALL libc_kill(SB)
CALL libc_open(SB)
CALL libc_close(SB)
RET