cmd/compile: track funcsyms by ir.Name instead of types.Sym

This is a cleanup to bring funcsym tracking a little closer to the
ir.Func. (I thought I needed this for a later change. That turned out
not to be the case, but it's a nice cleanup.)

Change-Id: I53e692f5d7ba4be56d42d8e0aefc06284cea0661
Reviewed-on: https://go-review.googlesource.com/c/go/+/305270
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Austin Clements 2021-03-24 14:28:18 -04:00
parent 2ba296da47
commit 33b4ffc357
2 changed files with 19 additions and 18 deletions

View File

@ -143,7 +143,7 @@ func InitLSym(f *ir.Func, hasBody bool) {
if f.LSym != nil && objabi.Experiment.RegabiWrappers {
return
}
staticdata.NeedFuncSym(f.Sym())
staticdata.NeedFuncSym(f)
selectLSym(f, hasBody)
if hasBody {
setupTextLSym(f, 0)

View File

@ -214,11 +214,16 @@ func dstringdata(s *obj.LSym, off int, t string, pos src.XPos, what string) int
var (
funcsymsmu sync.Mutex // protects funcsyms and associated package lookups (see func funcsym)
funcsyms []*types.Sym
funcsyms []*ir.Name // functions that need function value symbols
)
// FuncSym returns s·f.
func FuncSym(s *types.Sym) *types.Sym {
// FuncLinksym returns n·f, the function value symbol for n.
func FuncLinksym(n *ir.Name) *obj.LSym {
if n.Op() != ir.ONAME || n.Class != ir.PFUNC {
base.Fatalf("expected func name: %v", n)
}
s := n.Sym()
// funcsymsmu here serves to protect not just mutations of funcsyms (below),
// but also the package lookup of the func sym name,
// since this function gets called concurrently from the backend.
@ -235,17 +240,11 @@ func FuncSym(s *types.Sym) *types.Sym {
// symbols will be created explicitly with NeedFuncSym.
// See the NeedFuncSym comment for details.
if !base.Ctxt.Flag_dynlink && !existed {
funcsyms = append(funcsyms, s)
funcsyms = append(funcsyms, n)
}
funcsymsmu.Unlock()
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()
return sf.Linksym()
}
func GlobalLinksym(n *ir.Name) *obj.LSym {
@ -255,16 +254,16 @@ func GlobalLinksym(n *ir.Name) *obj.LSym {
return n.Linksym()
}
// NeedFuncSym ensures that s·f is exported, if needed.
// NeedFuncSym ensures that fn·f is exported, if needed.
// It is only used with -dynlink.
// When not compiling for dynamic linking,
// the funcsyms are created as needed by
// the packages that use them.
// Normally we emit the s·f stubs as DUPOK syms,
// Normally we emit the fn·f stubs as DUPOK syms,
// but DUPOK doesn't work across shared library boundaries.
// So instead, when dynamic linking, we only create
// the s·f stubs in s's package.
func NeedFuncSym(s *types.Sym) {
// the fn·f stubs in fn's package.
func NeedFuncSym(fn *ir.Func) {
if base.Ctxt.InParallel {
// The append below probably just needs to lock
// funcsymsmu, like in FuncSym.
@ -273,6 +272,7 @@ func NeedFuncSym(s *types.Sym) {
if !base.Ctxt.Flag_dynlink {
return
}
s := fn.Nname.Sym()
if s.IsBlank() {
return
}
@ -282,14 +282,15 @@ func NeedFuncSym(s *types.Sym) {
// get funcsyms.
return
}
funcsyms = append(funcsyms, s)
funcsyms = append(funcsyms, fn.Nname)
}
func WriteFuncSyms() {
sort.Slice(funcsyms, func(i, j int) bool {
return funcsyms[i].Linksym().Name < funcsyms[j].Linksym().Name
})
for _, s := range funcsyms {
for _, nam := range funcsyms {
s := nam.Sym()
sf := s.Pkg.Lookup(ir.FuncSymName(s)).Linksym()
objw.SymPtr(sf, 0, s.Linksym(), 0)
objw.Global(sf, int32(types.PtrSize), obj.DUPOK|obj.RODATA)