Commit ffbf479a authored by Alessandro Arzilli's avatar Alessandro Arzilli Committed by Heschi Kreinick

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: default avatarHeschi Kreinick <heschi@google.com>
parent dca769dc
......@@ -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:
......
......@@ -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)
}
}
}
......@@ -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_")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment