1
0
mirror of https://github.com/golang/go synced 2024-11-11 19:51:37 -07:00

[dev.regabi] cmd/compile: add Linksym helpers

Syms are meant to be just interned (pkg, name) tuples, and are a
purely abstract, Go-language concept. As such, associating them with
linker symbols (a low-level, implementation-oriented detail) is
inappropriate.

There's still work to be done before linker symbols can be directly
attached to their appropriate, higher-level objects instead. But in
the mean-time, we can at least add helper functions and discourage
folks from using Sym.Linksym directly. The next CL will mechanically
rewrite code to use these helpers where possible.

Passes toolstash -cmp.

Change-Id: I413bd1c80bce056304f9a7343526bd153f2b9c7d
Reviewed-on: https://go-review.googlesource.com/c/go/+/280639
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Matthew Dempsky 2020-12-28 19:14:39 -08:00
parent 289da2b33e
commit 25c613c02d
9 changed files with 39 additions and 23 deletions

View File

@ -260,7 +260,7 @@ func addGCLocals() {
} }
} }
func ggloblnod(nam ir.Node) { func ggloblnod(nam *ir.Name) {
s := nam.Sym().Linksym() s := nam.Sym().Linksym()
s.Gotype = reflectdata.TypeSym(nam.Type()).Linksym() s.Gotype = reflectdata.TypeSym(nam.Type()).Linksym()
flags := 0 flags := 0

View File

@ -78,7 +78,7 @@ type Func struct {
// Marks records scope boundary changes. // Marks records scope boundary changes.
Marks []Mark Marks []Mark
FieldTrack map[*types.Sym]struct{} FieldTrack map[*obj.LSym]struct{}
DebugInfo interface{} DebugInfo interface{}
LSym *obj.LSym LSym *obj.LSym
@ -119,12 +119,8 @@ func (f *Func) isStmt() {}
func (f *Func) Type() *types.Type { return f.typ } func (f *Func) Type() *types.Type { return f.typ }
func (f *Func) SetType(x *types.Type) { f.typ = x } func (f *Func) SetType(x *types.Type) { f.typ = x }
func (f *Func) Sym() *types.Sym { func (f *Func) Sym() *types.Sym { return f.Nname.Sym() }
if f.Nname != nil { func (f *Func) Linksym() *obj.LSym { return f.Nname.Linksym() }
return f.Nname.Sym()
}
return nil
}
// An Inline holds fields used for function bodies that can be inlined. // An Inline holds fields used for function bodies that can be inlined.
type Inline struct { type Inline struct {

View File

@ -7,6 +7,7 @@ package ir
import ( import (
"cmd/compile/internal/base" "cmd/compile/internal/base"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/obj"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/src" "cmd/internal/src"
@ -238,6 +239,8 @@ func (n *Name) SetFrameOffset(x int64) { n.Offset_ = x }
func (n *Name) Iota() int64 { return n.Offset_ } func (n *Name) Iota() int64 { return n.Offset_ }
func (n *Name) SetIota(x int64) { n.Offset_ = x } func (n *Name) SetIota(x int64) { n.Offset_ = x }
func (n *Name) Linksym() *obj.LSym { return n.sym.Linksym() }
func (*Name) CanBeNtype() {} func (*Name) CanBeNtype() {}
func (*Name) CanBeAnSSASym() {} func (*Name) CanBeAnSSASym() {}
func (*Name) CanBeAnSSAAux() {} func (*Name) CanBeAnSSAAux() {}

View File

@ -812,8 +812,8 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
// TrackSym returns the symbol for tracking use of field/method f, assumed // TrackSym returns the symbol for tracking use of field/method f, assumed
// to be a member of struct/interface type t. // to be a member of struct/interface type t.
func TrackSym(t *types.Type, f *types.Field) *types.Sym { func TrackSym(t *types.Type, f *types.Field) *obj.LSym {
return ir.Pkgs.Track.Lookup(t.ShortString() + "." + f.Sym.Name) return ir.Pkgs.Track.Lookup(t.ShortString() + "." + f.Sym.Name).Linksym()
} }
func TypeSymPrefix(prefix string, t *types.Type) *types.Sym { func TypeSymPrefix(prefix string, t *types.Type) *types.Sym {
@ -845,6 +845,18 @@ func TypeSym(t *types.Type) *types.Sym {
return s return s
} }
func TypeLinksymPrefix(prefix string, t *types.Type) *obj.LSym {
return TypeSymPrefix(prefix, t).Linksym()
}
func TypeLinksymLookup(name string) *obj.LSym {
return types.TypeSymLookup(name).Linksym()
}
func TypeLinksym(t *types.Type) *obj.LSym {
return TypeSym(t).Linksym()
}
func TypePtr(t *types.Type) *ir.AddrExpr { func TypePtr(t *types.Type) *ir.AddrExpr {
s := TypeSym(t) s := TypeSym(t)
if s.Def == nil { if s.Def == nil {

View File

@ -225,7 +225,7 @@ func StackOffset(slot ssa.LocalSlot) int32 {
// fieldtrack adds R_USEFIELD relocations to fnsym to record any // fieldtrack adds R_USEFIELD relocations to fnsym to record any
// struct fields that it used. // struct fields that it used.
func fieldtrack(fnsym *obj.LSym, tracked map[*types.Sym]struct{}) { func fieldtrack(fnsym *obj.LSym, tracked map[*obj.LSym]struct{}) {
if fnsym == nil { if fnsym == nil {
return return
} }
@ -233,24 +233,18 @@ func fieldtrack(fnsym *obj.LSym, tracked map[*types.Sym]struct{}) {
return return
} }
trackSyms := make([]*types.Sym, 0, len(tracked)) trackSyms := make([]*obj.LSym, 0, len(tracked))
for sym := range tracked { for sym := range tracked {
trackSyms = append(trackSyms, sym) trackSyms = append(trackSyms, sym)
} }
sort.Sort(symByName(trackSyms)) sort.Slice(trackSyms, func(i, j int) bool { return trackSyms[i].Name < trackSyms[j].Name })
for _, sym := range trackSyms { for _, sym := range trackSyms {
r := obj.Addrel(fnsym) r := obj.Addrel(fnsym)
r.Sym = sym.Linksym() r.Sym = sym
r.Type = objabi.R_USEFIELD r.Type = objabi.R_USEFIELD
} }
} }
type symByName []*types.Sym
func (a symByName) Len() int { return len(a) }
func (a symByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
func (a symByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// largeStack is info about a function whose stack frame is too large (rare). // largeStack is info about a function whose stack frame is too large (rare).
type largeStack struct { type largeStack struct {
locals int64 locals int64

View File

@ -2106,7 +2106,7 @@ func (s *state) expr(n ir.Node) *ssa.Value {
return s.newValue3(ssa.OpSliceMake, n.Type(), ptr, len, len) return s.newValue3(ssa.OpSliceMake, n.Type(), ptr, len, len)
case ir.OCFUNC: case ir.OCFUNC:
n := n.(*ir.UnaryExpr) n := n.(*ir.UnaryExpr)
aux := n.X.Sym().Linksym() aux := n.X.(*ir.Name).Linksym()
return s.entryNewValue1A(ssa.OpAddr, n.Type(), aux, s.sb) return s.entryNewValue1A(ssa.OpAddr, n.Type(), aux, s.sb)
case ir.ONAME: case ir.ONAME:
n := n.(*ir.Name) n := n.(*ir.Name)
@ -6826,7 +6826,7 @@ func AddAux2(a *obj.Addr, v *ssa.Value, offset int64) {
case *ir.Name: case *ir.Name:
if n.Class_ == ir.PPARAM || n.Class_ == ir.PPARAMOUT { if n.Class_ == ir.PPARAM || n.Class_ == ir.PPARAMOUT {
a.Name = obj.NAME_PARAM a.Name = obj.NAME_PARAM
a.Sym = ir.Orig(n).Sym().Linksym() a.Sym = ir.Orig(n).(*ir.Name).Linksym()
a.Offset += n.FrameOffset() a.Offset += n.FrameOffset()
break break
} }

View File

@ -258,6 +258,13 @@ func FuncSym(s *types.Sym) *types.Sym {
return sf return sf
} }
func FuncLinksym(n *ir.Name) *obj.LSym {
if n.Op() != ir.ONAME || n.Class_ != ir.PFUNC {
base.Fatalf("expected func name: %v", n)
}
return FuncSym(n.Sym()).Linksym()
}
// NeedFuncSym ensures that s·f is exported. // NeedFuncSym ensures that s·f is exported.
// It is only used with -dynlink. // It is only used with -dynlink.
// When not compiling for dynamic linking, // When not compiling for dynamic linking,

View File

@ -74,6 +74,10 @@ func (sym *Sym) LinksymName() string {
return sym.Pkg.Prefix + "." + sym.Name return sym.Pkg.Prefix + "." + sym.Name
} }
// Deprecated: This method should not be used directly. Instead, use a
// higher-level abstraction that directly returns the linker symbol
// for a named object. For example, reflectdata.TypeLinksym(t) instead
// of reflectdata.TypeSym(t).Linksym().
func (sym *Sym) Linksym() *obj.LSym { func (sym *Sym) Linksym() *obj.LSym {
if sym == nil { if sym == nil {
return nil return nil

View File

@ -975,7 +975,7 @@ func usefield(n *ir.SelectorExpr) {
sym := reflectdata.TrackSym(outer, field) sym := reflectdata.TrackSym(outer, field)
if ir.CurFunc.FieldTrack == nil { if ir.CurFunc.FieldTrack == nil {
ir.CurFunc.FieldTrack = make(map[*types.Sym]struct{}) ir.CurFunc.FieldTrack = make(map[*obj.LSym]struct{})
} }
ir.CurFunc.FieldTrack[sym] = struct{}{} ir.CurFunc.FieldTrack[sym] = struct{}{}
} }