Source file src/cmd/compile/internal/ssagen/abi.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssagen
     6  
     7  import (
     8  	"fmt"
     9  	"internal/buildcfg"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"strings"
    14  
    15  	"cmd/compile/internal/base"
    16  	"cmd/compile/internal/ir"
    17  	"cmd/compile/internal/staticdata"
    18  	"cmd/compile/internal/typecheck"
    19  	"cmd/compile/internal/types"
    20  	"cmd/internal/obj"
    21  	"cmd/internal/objabi"
    22  )
    23  
    24  // SymABIs records information provided by the assembler about symbol
    25  // definition ABIs and reference ABIs.
    26  type SymABIs struct {
    27  	defs map[string]obj.ABI
    28  	refs map[string]obj.ABISet
    29  
    30  	localPrefix string
    31  }
    32  
    33  func NewSymABIs(myimportpath string) *SymABIs {
    34  	var localPrefix string
    35  	if myimportpath != "" {
    36  		localPrefix = objabi.PathToPrefix(myimportpath) + "."
    37  	}
    38  
    39  	return &SymABIs{
    40  		defs:        make(map[string]obj.ABI),
    41  		refs:        make(map[string]obj.ABISet),
    42  		localPrefix: localPrefix,
    43  	}
    44  }
    45  
    46  // canonicalize returns the canonical name used for a linker symbol in
    47  // s's maps. Symbols in this package may be written either as "".X or
    48  // with the package's import path already in the symbol. This rewrites
    49  // both to `"".`, which matches compiler-generated linker symbol names.
    50  func (s *SymABIs) canonicalize(linksym string) string {
    51  	// If the symbol is already prefixed with localPrefix,
    52  	// rewrite it to start with "" so it matches the
    53  	// compiler's internal symbol names.
    54  	if s.localPrefix != "" && strings.HasPrefix(linksym, s.localPrefix) {
    55  		return `"".` + linksym[len(s.localPrefix):]
    56  	}
    57  	return linksym
    58  }
    59  
    60  // ReadSymABIs reads a symabis file that specifies definitions and
    61  // references of text symbols by ABI.
    62  //
    63  // The symabis format is a set of lines, where each line is a sequence
    64  // of whitespace-separated fields. The first field is a verb and is
    65  // either "def" for defining a symbol ABI or "ref" for referencing a
    66  // symbol using an ABI. For both "def" and "ref", the second field is
    67  // the symbol name and the third field is the ABI name, as one of the
    68  // named cmd/internal/obj.ABI constants.
    69  func (s *SymABIs) ReadSymABIs(file string) {
    70  	data, err := ioutil.ReadFile(file)
    71  	if err != nil {
    72  		log.Fatalf("-symabis: %v", err)
    73  	}
    74  
    75  	for lineNum, line := range strings.Split(string(data), "\n") {
    76  		lineNum++ // 1-based
    77  		line = strings.TrimSpace(line)
    78  		if line == "" || strings.HasPrefix(line, "#") {
    79  			continue
    80  		}
    81  
    82  		parts := strings.Fields(line)
    83  		switch parts[0] {
    84  		case "def", "ref":
    85  			// Parse line.
    86  			if len(parts) != 3 {
    87  				log.Fatalf(`%s:%d: invalid symabi: syntax is "%s sym abi"`, file, lineNum, parts[0])
    88  			}
    89  			sym, abistr := parts[1], parts[2]
    90  			abi, valid := obj.ParseABI(abistr)
    91  			if !valid {
    92  				log.Fatalf(`%s:%d: invalid symabi: unknown abi "%s"`, file, lineNum, abistr)
    93  			}
    94  
    95  			sym = s.canonicalize(sym)
    96  
    97  			// Record for later.
    98  			if parts[0] == "def" {
    99  				s.defs[sym] = abi
   100  			} else {
   101  				s.refs[sym] |= obj.ABISetOf(abi)
   102  			}
   103  		default:
   104  			log.Fatalf(`%s:%d: invalid symabi type "%s"`, file, lineNum, parts[0])
   105  		}
   106  	}
   107  }
   108  
   109  // GenABIWrappers applies ABI information to Funcs and generates ABI
   110  // wrapper functions where necessary.
   111  func (s *SymABIs) GenABIWrappers() {
   112  	// For cgo exported symbols, we tell the linker to export the
   113  	// definition ABI to C. That also means that we don't want to
   114  	// create ABI wrappers even if there's a linkname.
   115  	//
   116  	// TODO(austin): Maybe we want to create the ABI wrappers, but
   117  	// ensure the linker exports the right ABI definition under
   118  	// the unmangled name?
   119  	cgoExports := make(map[string][]*[]string)
   120  	for i, prag := range typecheck.Target.CgoPragmas {
   121  		switch prag[0] {
   122  		case "cgo_export_static", "cgo_export_dynamic":
   123  			symName := s.canonicalize(prag[1])
   124  			pprag := &typecheck.Target.CgoPragmas[i]
   125  			cgoExports[symName] = append(cgoExports[symName], pprag)
   126  		}
   127  	}
   128  
   129  	// Apply ABI defs and refs to Funcs and generate wrappers.
   130  	//
   131  	// This may generate new decls for the wrappers, but we
   132  	// specifically *don't* want to visit those, lest we create
   133  	// wrappers for wrappers.
   134  	for _, fn := range typecheck.Target.Decls {
   135  		if fn.Op() != ir.ODCLFUNC {
   136  			continue
   137  		}
   138  		fn := fn.(*ir.Func)
   139  		nam := fn.Nname
   140  		if ir.IsBlank(nam) {
   141  			continue
   142  		}
   143  		sym := nam.Sym()
   144  		var symName string
   145  		if sym.Linkname != "" {
   146  			symName = s.canonicalize(sym.Linkname)
   147  		} else {
   148  			// These names will already be canonical.
   149  			symName = sym.Pkg.Prefix + "." + sym.Name
   150  		}
   151  
   152  		// Apply definitions.
   153  		defABI, hasDefABI := s.defs[symName]
   154  		if hasDefABI {
   155  			if len(fn.Body) != 0 {
   156  				base.ErrorfAt(fn.Pos(), "%v defined in both Go and assembly", fn)
   157  			}
   158  			fn.ABI = defABI
   159  		}
   160  
   161  		if fn.Pragma&ir.CgoUnsafeArgs != 0 {
   162  			// CgoUnsafeArgs indicates the function (or its callee) uses
   163  			// offsets to dispatch arguments, which currently using ABI0
   164  			// frame layout. Pin it to ABI0.
   165  			fn.ABI = obj.ABI0
   166  		}
   167  
   168  		// If cgo-exported, add the definition ABI to the cgo
   169  		// pragmas.
   170  		cgoExport := cgoExports[symName]
   171  		for _, pprag := range cgoExport {
   172  			// The export pragmas have the form:
   173  			//
   174  			//   cgo_export_* <local> [<remote>]
   175  			//
   176  			// If <remote> is omitted, it's the same as
   177  			// <local>.
   178  			//
   179  			// Expand to
   180  			//
   181  			//   cgo_export_* <local> <remote> <ABI>
   182  			if len(*pprag) == 2 {
   183  				*pprag = append(*pprag, (*pprag)[1])
   184  			}
   185  			// Add the ABI argument.
   186  			*pprag = append(*pprag, fn.ABI.String())
   187  		}
   188  
   189  		// Apply references.
   190  		if abis, ok := s.refs[symName]; ok {
   191  			fn.ABIRefs |= abis
   192  		}
   193  		// Assume all functions are referenced at least as
   194  		// ABIInternal, since they may be referenced from
   195  		// other packages.
   196  		fn.ABIRefs.Set(obj.ABIInternal, true)
   197  
   198  		// If a symbol is defined in this package (either in
   199  		// Go or assembly) and given a linkname, it may be
   200  		// referenced from another package, so make it
   201  		// callable via any ABI. It's important that we know
   202  		// it's defined in this package since other packages
   203  		// may "pull" symbols using linkname and we don't want
   204  		// to create duplicate ABI wrappers.
   205  		//
   206  		// However, if it's given a linkname for exporting to
   207  		// C, then we don't make ABI wrappers because the cgo
   208  		// tool wants the original definition.
   209  		hasBody := len(fn.Body) != 0
   210  		if sym.Linkname != "" && (hasBody || hasDefABI) && len(cgoExport) == 0 {
   211  			fn.ABIRefs |= obj.ABISetCallable
   212  		}
   213  
   214  		// Double check that cgo-exported symbols don't get
   215  		// any wrappers.
   216  		if len(cgoExport) > 0 && fn.ABIRefs&^obj.ABISetOf(fn.ABI) != 0 {
   217  			base.Fatalf("cgo exported function %s cannot have ABI wrappers", fn)
   218  		}
   219  
   220  		if !buildcfg.Experiment.RegabiWrappers {
   221  			continue
   222  		}
   223  
   224  		forEachWrapperABI(fn, makeABIWrapper)
   225  	}
   226  }
   227  
   228  // InitLSym defines f's obj.LSym and initializes it based on the
   229  // properties of f. This includes setting the symbol flags and ABI and
   230  // creating and initializing related DWARF symbols.
   231  //
   232  // InitLSym must be called exactly once per function and must be
   233  // called for both functions with bodies and functions without bodies.
   234  // For body-less functions, we only create the LSym; for functions
   235  // with bodies call a helper to setup up / populate the LSym.
   236  func InitLSym(f *ir.Func, hasBody bool) {
   237  	if f.LSym != nil {
   238  		base.FatalfAt(f.Pos(), "InitLSym called twice on %v", f)
   239  	}
   240  
   241  	if nam := f.Nname; !ir.IsBlank(nam) {
   242  		f.LSym = nam.LinksymABI(f.ABI)
   243  		if f.Pragma&ir.Systemstack != 0 {
   244  			f.LSym.Set(obj.AttrCFunc, true)
   245  		}
   246  		if f.ABI == obj.ABIInternal || !buildcfg.Experiment.RegabiWrappers {
   247  			// Function values can only point to
   248  			// ABIInternal entry points. This will create
   249  			// the funcsym for either the defining
   250  			// function or its wrapper as appropriate.
   251  			//
   252  			// If we're not using ABI wrappers, we only
   253  			// InitLSym for the defining ABI of a function,
   254  			// so we make the funcsym when we see that.
   255  			staticdata.NeedFuncSym(f)
   256  		}
   257  	}
   258  	if hasBody {
   259  		setupTextLSym(f, 0)
   260  	}
   261  }
   262  
   263  func forEachWrapperABI(fn *ir.Func, cb func(fn *ir.Func, wrapperABI obj.ABI)) {
   264  	need := fn.ABIRefs &^ obj.ABISetOf(fn.ABI)
   265  	if need == 0 {
   266  		return
   267  	}
   268  
   269  	for wrapperABI := obj.ABI(0); wrapperABI < obj.ABICount; wrapperABI++ {
   270  		if !need.Get(wrapperABI) {
   271  			continue
   272  		}
   273  		cb(fn, wrapperABI)
   274  	}
   275  }
   276  
   277  // makeABIWrapper creates a new function that will be called with
   278  // wrapperABI and calls "f" using f.ABI.
   279  func makeABIWrapper(f *ir.Func, wrapperABI obj.ABI) {
   280  	if base.Debug.ABIWrap != 0 {
   281  		fmt.Fprintf(os.Stderr, "=-= %v to %v wrapper for %v\n", wrapperABI, f.ABI, f)
   282  	}
   283  
   284  	// Q: is this needed?
   285  	savepos := base.Pos
   286  	savedclcontext := typecheck.DeclContext
   287  	savedcurfn := ir.CurFunc
   288  
   289  	base.Pos = base.AutogeneratedPos
   290  	typecheck.DeclContext = ir.PEXTERN
   291  
   292  	// At the moment we don't support wrapping a method, we'd need machinery
   293  	// below to handle the receiver. Panic if we see this scenario.
   294  	ft := f.Nname.Type()
   295  	if ft.NumRecvs() != 0 {
   296  		panic("makeABIWrapper support for wrapping methods not implemented")
   297  	}
   298  
   299  	// Manufacture a new func type to use for the wrapper.
   300  	var noReceiver *ir.Field
   301  	tfn := ir.NewFuncType(base.Pos,
   302  		noReceiver,
   303  		typecheck.NewFuncParams(ft.Params(), true),
   304  		typecheck.NewFuncParams(ft.Results(), false))
   305  
   306  	// Reuse f's types.Sym to create a new ODCLFUNC/function.
   307  	fn := typecheck.DeclFunc(f.Nname.Sym(), tfn)
   308  	fn.ABI = wrapperABI
   309  
   310  	fn.SetABIWrapper(true)
   311  	fn.SetDupok(true)
   312  
   313  	// ABI0-to-ABIInternal wrappers will be mainly loading params from
   314  	// stack into registers (and/or storing stack locations back to
   315  	// registers after the wrapped call); in most cases they won't
   316  	// need to allocate stack space, so it should be OK to mark them
   317  	// as NOSPLIT in these cases. In addition, my assumption is that
   318  	// functions written in assembly are NOSPLIT in most (but not all)
   319  	// cases. In the case of an ABIInternal target that has too many
   320  	// parameters to fit into registers, the wrapper would need to
   321  	// allocate stack space, but this seems like an unlikely scenario.
   322  	// Hence: mark these wrappers NOSPLIT.
   323  	//
   324  	// ABIInternal-to-ABI0 wrappers on the other hand will be taking
   325  	// things in registers and pushing them onto the stack prior to
   326  	// the ABI0 call, meaning that they will always need to allocate
   327  	// stack space. If the compiler marks them as NOSPLIT this seems
   328  	// as though it could lead to situations where the linker's
   329  	// nosplit-overflow analysis would trigger a link failure. On the
   330  	// other hand if they not tagged NOSPLIT then this could cause
   331  	// problems when building the runtime (since there may be calls to
   332  	// asm routine in cases where it's not safe to grow the stack). In
   333  	// most cases the wrapper would be (in effect) inlined, but are
   334  	// there (perhaps) indirect calls from the runtime that could run
   335  	// into trouble here.
   336  	// FIXME: at the moment all.bash does not pass when I leave out
   337  	// NOSPLIT for these wrappers, so all are currently tagged with NOSPLIT.
   338  	fn.Pragma |= ir.Nosplit
   339  
   340  	// Generate call. Use tail call if no params and no returns,
   341  	// but a regular call otherwise.
   342  	//
   343  	// Note: ideally we would be using a tail call in cases where
   344  	// there are params but no returns for ABI0->ABIInternal wrappers,
   345  	// provided that all params fit into registers (e.g. we don't have
   346  	// to allocate any stack space). Doing this will require some
   347  	// extra work in typecheck/walk/ssa, might want to add a new node
   348  	// OTAILCALL or something to this effect.
   349  	tailcall := tfn.Type().NumResults() == 0 && tfn.Type().NumParams() == 0 && tfn.Type().NumRecvs() == 0
   350  	if base.Ctxt.Arch.Name == "ppc64le" && base.Ctxt.Flag_dynlink {
   351  		// cannot tailcall on PPC64 with dynamic linking, as we need
   352  		// to restore R2 after call.
   353  		tailcall = false
   354  	}
   355  	if base.Ctxt.Arch.Name == "amd64" && wrapperABI == obj.ABIInternal {
   356  		// cannot tailcall from ABIInternal to ABI0 on AMD64, as we need
   357  		// to special registers (X15) when returning to ABIInternal.
   358  		tailcall = false
   359  	}
   360  
   361  	var tail ir.Node
   362  	call := ir.NewCallExpr(base.Pos, ir.OCALL, f.Nname, nil)
   363  	call.Args = ir.ParamNames(tfn.Type())
   364  	call.IsDDD = tfn.Type().IsVariadic()
   365  	tail = call
   366  	if tailcall {
   367  		tail = ir.NewTailCallStmt(base.Pos, call)
   368  	} else if tfn.Type().NumResults() > 0 {
   369  		n := ir.NewReturnStmt(base.Pos, nil)
   370  		n.Results = []ir.Node{call}
   371  		tail = n
   372  	}
   373  	fn.Body.Append(tail)
   374  
   375  	typecheck.FinishFuncBody()
   376  	if base.Debug.DclStack != 0 {
   377  		types.CheckDclstack()
   378  	}
   379  
   380  	typecheck.Func(fn)
   381  	ir.CurFunc = fn
   382  	typecheck.Stmts(fn.Body)
   383  
   384  	typecheck.Target.Decls = append(typecheck.Target.Decls, fn)
   385  
   386  	// Restore previous context.
   387  	base.Pos = savepos
   388  	typecheck.DeclContext = savedclcontext
   389  	ir.CurFunc = savedcurfn
   390  }
   391  
   392  // setupTextLsym initializes the LSym for a with-body text symbol.
   393  func setupTextLSym(f *ir.Func, flag int) {
   394  	if f.Dupok() {
   395  		flag |= obj.DUPOK
   396  	}
   397  	if f.Wrapper() {
   398  		flag |= obj.WRAPPER
   399  	}
   400  	if f.ABIWrapper() {
   401  		flag |= obj.ABIWRAPPER
   402  	}
   403  	if f.Needctxt() {
   404  		flag |= obj.NEEDCTXT
   405  	}
   406  	if f.Pragma&ir.Nosplit != 0 {
   407  		flag |= obj.NOSPLIT
   408  	}
   409  	if f.ReflectMethod() {
   410  		flag |= obj.REFLECTMETHOD
   411  	}
   412  
   413  	// Clumsy but important.
   414  	// For functions that could be on the path of invoking a deferred
   415  	// function that can recover (runtime.reflectcall, reflect.callReflect,
   416  	// and reflect.callMethod), we want the panic+recover special handling.
   417  	// See test/recover.go for test cases and src/reflect/value.go
   418  	// for the actual functions being considered.
   419  	//
   420  	// runtime.reflectcall is an assembly function which tailcalls
   421  	// WRAPPER functions (runtime.callNN). Its ABI wrapper needs WRAPPER
   422  	// flag as well.
   423  	fnname := f.Sym().Name
   424  	if base.Ctxt.Pkgpath == "runtime" && fnname == "reflectcall" {
   425  		flag |= obj.WRAPPER
   426  	} else if base.Ctxt.Pkgpath == "reflect" {
   427  		switch fnname {
   428  		case "callReflect", "callMethod":
   429  			flag |= obj.WRAPPER
   430  		}
   431  	}
   432  
   433  	base.Ctxt.InitTextSym(f.LSym, flag)
   434  }
   435  

View as plain text