From ffbf479ae1cc8d85022fd74b6ad35b419279b162 Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Wed, 17 Oct 2018 09:36:06 +0200 Subject: [PATCH] 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 Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot Reviewed-by: Heschi Kreinick --- src/cmd/link/internal/ld/dwarf.go | 4 +- src/cmd/link/internal/ld/dwarf_test.go | 73 ++++++++++++++++++++++++++ src/cmd/link/internal/ld/symtab.go | 11 ++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/cmd/link/internal/ld/dwarf.go b/src/cmd/link/internal/ld/dwarf.go index 743f4cedd4b..827a1d931af 100644 --- a/src/cmd/link/internal/ld/dwarf.go +++ b/src/cmd/link/internal/ld/dwarf.go @@ -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: diff --git a/src/cmd/link/internal/ld/dwarf_test.go b/src/cmd/link/internal/ld/dwarf_test.go index e5d99815909..42b598efefa 100644 --- a/src/cmd/link/internal/ld/dwarf_test.go +++ b/src/cmd/link/internal/ld/dwarf_test.go @@ -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) + } + } +} diff --git a/src/cmd/link/internal/ld/symtab.go b/src/cmd/link/internal/ld/symtab.go index 2a04ef3824e..d2737deca56 100644 --- a/src/cmd/link/internal/ld/symtab.go +++ b/src/cmd/link/internal/ld/symtab.go @@ -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_") +}