Skip to content

Commit 12dbd20

Browse files
cuonglmianlancetaylor
authored andcommitted
cmd/link: set .dynsym info field
.dynsym section info field is the index of first non-local symbol, mean the number of local symbols. The go linker have never ever set it before, so just set it. Fixes #33358 Change-Id: Ifde2deb7c15471b04d565861f5d81daffb0c0d3d Reviewed-on: https://go-review.googlesource.com/c/go/+/187979 Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent c96d794 commit 12dbd20

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1942,8 +1942,17 @@ func Asmbelf(ctxt *Link, symo int64) {
19421942
sh.addralign = uint64(ctxt.Arch.RegSize)
19431943
sh.link = uint32(elfshname(".dynstr").shnum)
19441944

1945-
// sh->info = index of first non-local symbol (number of local symbols)
1946-
shsym(sh, ctxt.Syms.Lookup(".dynsym", 0))
1945+
// sh.info is the index of first non-local symbol (number of local symbols)
1946+
s := ctxt.Syms.Lookup(".dynsym", 0)
1947+
i := uint32(0)
1948+
for sub := s; sub != nil; sub = sub.Sub {
1949+
i++
1950+
if !sub.Attr.Local() {
1951+
sh.info = i
1952+
break
1953+
}
1954+
}
1955+
shsym(sh, s)
19471956

19481957
sh = elfshname(".dynstr")
19491958
sh.type_ = SHT_STRTAB

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

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// +build cgo
2+
3+
// Copyright 2019 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package ld
8+
9+
import (
10+
"debug/elf"
11+
"internal/testenv"
12+
"io/ioutil"
13+
"os"
14+
"os/exec"
15+
"path/filepath"
16+
"testing"
17+
)
18+
19+
func TestDynSymShInfo(t *testing.T) {
20+
t.Parallel()
21+
testenv.MustHaveGoBuild(t)
22+
dir, err := ioutil.TempDir("", "go-build-issue33358")
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
defer os.RemoveAll(dir)
27+
28+
const prog = `
29+
package main
30+
31+
import "net"
32+
33+
func main() {
34+
net.Dial("", "")
35+
}
36+
`
37+
src := filepath.Join(dir, "issue33358.go")
38+
if err := ioutil.WriteFile(src, []byte(prog), 0666); err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
binFile := filepath.Join(dir, "issue33358")
43+
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", binFile, src)
44+
if out, err := cmd.CombinedOutput(); err != nil {
45+
t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
46+
}
47+
48+
fi, err := os.Open(binFile)
49+
if err != nil {
50+
t.Fatalf("failed to open built file: %v", err)
51+
}
52+
53+
elfFile, err := elf.NewFile(fi)
54+
if err != nil {
55+
t.Skip("The system may not support ELF, skipped.")
56+
}
57+
58+
section := elfFile.Section(".dynsym")
59+
if section == nil {
60+
t.Fatal("no dynsym")
61+
}
62+
63+
symbols, err := elfFile.DynamicSymbols()
64+
if err != nil {
65+
t.Fatalf("failed to get dynamic symbols: %v", err)
66+
}
67+
68+
var numLocalSymbols uint32
69+
for i, s := range symbols {
70+
if elf.ST_BIND(s.Info) != elf.STB_LOCAL {
71+
numLocalSymbols = uint32(i + 1)
72+
break
73+
}
74+
}
75+
76+
if section.Info != numLocalSymbols {
77+
t.Fatalf("Unexpected sh info, want greater than 0, got: %d", section.Info)
78+
}
79+
}

0 commit comments

Comments
 (0)