1
0
mirror of https://github.com/golang/go synced 2024-11-06 14:16:15 -07:00

cmd/compile,cmd/link: remove statictmp variables from symbol table.

Removes statictmp variables from debug_info and the final symbol table.

Fixes #27800

Change-Id: I302c59a04bc3f460e7085fef241f937bbf30421d
Reviewed-on: https://go-review.googlesource.com/c/142577
Run-TryBot: Alessandro Arzilli <alessandro.arzilli@gmail.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Alessandro Arzilli 2018-10-17 09:36:06 +02:00 committed by Heschi Kreinick
parent dca769dca9
commit ffbf479ae1
3 changed files with 87 additions and 1 deletions

View File

@ -863,7 +863,9 @@ func defdwsymb(ctxt *Link, s *sym.Symbol, str string, t SymbolType, v int64, got
default:
return
}
if ctxt.LinkMode != LinkExternal && isStaticTemp(s.Name) {
return
}
dwarfDefineGlobal(ctxt, s, str, v, gotype)
case AutoSym, ParamSym, DeletedAutoSym:

View File

@ -1062,3 +1062,76 @@ func main() {
}
}
}
func TestStaticTmp(t *testing.T) {
// Checks that statictmp variables do not appear in debug_info or the
// symbol table.
// Also checks that statictmp variables do not collide with user defined
// variables (issue #25113)
testenv.MustHaveGoBuild(t)
if runtime.GOOS == "plan9" {
t.Skip("skipping on plan9; no DWARF symbol table in executables")
}
dir, err := ioutil.TempDir("", "go-build")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
const prog = `package main
var stmp_0 string
var a []int
func init() {
a = []int{ 7 }
}
func main() {
println(a[0])
}
`
f := gobuild(t, dir, prog, NoOpt)
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
rdr := d.Reader()
for {
e, err := rdr.Next()
if err != nil {
t.Fatal(err)
}
if e == nil {
break
}
if e.Tag != dwarf.TagVariable {
continue
}
name, ok := e.Val(dwarf.AttrName).(string)
if !ok {
continue
}
if strings.Contains(name, "stmp") {
t.Errorf("statictmp variable found in debug_info: %s at %x", name, e.Offset)
}
}
syms, err := f.Symbols()
if err != nil {
t.Fatalf("error reading symbols: %v", err)
}
for _, sym := range syms {
if strings.Contains(sym.Name, "stmp") {
t.Errorf("statictmp variable found in symbol table: %s", sym.Name)
}
}
}

View File

@ -432,6 +432,10 @@ func (ctxt *Link) symtab() {
// just defined above will be first.
// hide the specific symbols.
for _, s := range ctxt.Syms.Allsym {
if ctxt.LinkMode != LinkExternal && isStaticTemp(s.Name) {
s.Attr |= sym.AttrNotInSymbolTable
}
if !s.Attr.Reachable() || s.Attr.Special() || s.Type != sym.SRODATA {
continue
}
@ -676,3 +680,10 @@ func (ctxt *Link) symtab() {
lastmoduledatap.AddAddr(ctxt.Arch, moduledata)
}
}
func isStaticTemp(name string) bool {
if i := strings.LastIndex(name, "/"); i >= 0 {
name = name[i:]
}
return strings.Contains(name, "..stmp_")
}