github.com/zxy12/go_duplicate_1_12@v0.0.0-20200217043740-b1636fc0368b/src/cmd/internal/obj/link.go (about)

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/l.h
     3  //
     4  //  Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //  Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //  Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //  Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //  Portions Copyright © 2004,2006 Bruce Ellis
     9  //  Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //  Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //  Portions Copyright © 2009 The Go Authors. All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  package obj
    32  
    33  import (
    34  	"bufio"
    35  	"cmd/internal/dwarf"
    36  	"cmd/internal/objabi"
    37  	"cmd/internal/src"
    38  	"cmd/internal/sys"
    39  	"fmt"
    40  	"sync"
    41  )
    42  
    43  // An Addr is an argument to an instruction.
    44  // The general forms and their encodings are:
    45  //
    46  //  sym±offset(symkind)(reg)(index*scale)
    47  //      Memory reference at address &sym(symkind) + offset + reg + index*scale.
    48  //      Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
    49  //      If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
    50  //      To force a parsing as index*scale, write (index*1).
    51  //      Encoding:
    52  //          type = TYPE_MEM
    53  //          name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
    54  //          sym = sym
    55  //          offset = ±offset
    56  //          reg = reg (REG_*)
    57  //          index = index (REG_*)
    58  //          scale = scale (1, 2, 4, 8)
    59  //
    60  //  $<mem>
    61  //      Effective address of memory reference <mem>, defined above.
    62  //      Encoding: same as memory reference, but type = TYPE_ADDR.
    63  //
    64  //  $<±integer value>
    65  //      This is a special case of $<mem>, in which only ±offset is present.
    66  //      It has a separate type for easy recognition.
    67  //      Encoding:
    68  //          type = TYPE_CONST
    69  //          offset = ±integer value
    70  //
    71  //  *<mem>
    72  //      Indirect reference through memory reference <mem>, defined above.
    73  //      Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
    74  //      pointer stored in the data word sym(SB), not a function named sym(SB).
    75  //      Encoding: same as above, but type = TYPE_INDIR.
    76  //
    77  //  $*$<mem>
    78  //      No longer used.
    79  //      On machines with actual SB registers, $*$<mem> forced the
    80  //      instruction encoding to use a full 32-bit constant, never a
    81  //      reference relative to SB.
    82  //
    83  //  $<floating point literal>
    84  //      Floating point constant value.
    85  //      Encoding:
    86  //          type = TYPE_FCONST
    87  //          val = floating point value
    88  //
    89  //  $<string literal, up to 8 chars>
    90  //      String literal value (raw bytes used for DATA instruction).
    91  //      Encoding:
    92  //          type = TYPE_SCONST
    93  //          val = string
    94  //
    95  //  <register name>
    96  //      Any register: integer, floating point, control, segment, and so on.
    97  //      If looking for specific register kind, must check type and reg value range.
    98  //      Encoding:
    99  //          type = TYPE_REG
   100  //          reg = reg (REG_*)
   101  //
   102  //  x(PC)
   103  //      Encoding:
   104  //          type = TYPE_BRANCH
   105  //          val = Prog* reference OR ELSE offset = target pc (branch takes priority)
   106  //
   107  //  $±x-±y
   108  //      Final argument to TEXT, specifying local frame size x and argument size y.
   109  //      In this form, x and y are integer literals only, not arbitrary expressions.
   110  //      This avoids parsing ambiguities due to the use of - as a separator.
   111  //      The ± are optional.
   112  //      If the final argument to TEXT omits the -±y, the encoding should still
   113  //      use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
   114  //      Encoding:
   115  //          type = TYPE_TEXTSIZE
   116  //          offset = x
   117  //          val = int32(y)
   118  //
   119  //  reg<<shift, reg>>shift, reg->shift, reg@>shift
   120  //      Shifted register value, for ARM and ARM64.
   121  //      In this form, reg must be a register and shift can be a register or an integer constant.
   122  //      Encoding:
   123  //          type = TYPE_SHIFT
   124  //      On ARM:
   125  //          offset = (reg&15) | shifttype<<5 | count
   126  //          shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
   127  //          count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
   128  //      On ARM64:
   129  //          offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
   130  //          shifttype = 0, 1, 2 for <<, >>, ->
   131  //
   132  //  (reg, reg)
   133  //      A destination register pair. When used as the last argument of an instruction,
   134  //      this form makes clear that both registers are destinations.
   135  //      Encoding:
   136  //          type = TYPE_REGREG
   137  //          reg = first register
   138  //          offset = second register
   139  //
   140  //  [reg, reg, reg-reg]
   141  //      Register list for ARM, ARM64, 386/AMD64.
   142  //      Encoding:
   143  //          type = TYPE_REGLIST
   144  //      On ARM:
   145  //          offset = bit mask of registers in list; R0 is low bit.
   146  //      On ARM64:
   147  //          offset = register count (Q:size) | arrangement (opcode) | first register
   148  //      On 386/AMD64:
   149  //          reg = range low register
   150  //          offset = 2 packed registers + kind tag (see x86.EncodeRegisterRange)
   151  //
   152  //  reg, reg
   153  //      Register pair for ARM.
   154  //      TYPE_REGREG2
   155  //
   156  //  (reg+reg)
   157  //      Register pair for PPC64.
   158  //      Encoding:
   159  //          type = TYPE_MEM
   160  //          reg = first register
   161  //          index = second register
   162  //          scale = 1
   163  //
   164  //  reg.[US]XT[BHWX]
   165  //      Register extension for ARM64
   166  //      Encoding:
   167  //          type = TYPE_REG
   168  //          reg = REG_[US]XT[BHWX] + register + shift amount
   169  //          offset = ((reg&31) << 16) | (exttype << 13) | (amount<<10)
   170  //
   171  //  reg.<T>
   172  //      Register arrangement for ARM64 SIMD register
   173  //      e.g.: V1.S4, V2.S2, V7.D2, V2.H4, V6.B16
   174  //      Encoding:
   175  //          type = TYPE_REG
   176  //          reg = REG_ARNG + register + arrangement
   177  //
   178  //  reg.<T>[index]
   179  //      Register element for ARM64
   180  //      Encoding:
   181  //          type = TYPE_REG
   182  //          reg = REG_ELEM + register + arrangement
   183  //          index = element index
   184  
   185  type Addr struct {
   186  	Reg    int16
   187  	Index  int16
   188  	Scale  int16 // Sometimes holds a register.
   189  	Type   AddrType
   190  	Name   AddrName
   191  	Class  int8
   192  	Offset int64
   193  	Sym    *LSym
   194  
   195  	// argument value:
   196  	//  for TYPE_SCONST, a string
   197  	//  for TYPE_FCONST, a float64
   198  	//  for TYPE_BRANCH, a *Prog (optional)
   199  	//  for TYPE_TEXTSIZE, an int32 (optional)
   200  	Val interface{}
   201  }
   202  
   203  type AddrName int8
   204  
   205  const (
   206  	NAME_NONE AddrName = iota
   207  	NAME_EXTERN
   208  	NAME_STATIC
   209  	NAME_AUTO
   210  	NAME_PARAM
   211  	// A reference to name@GOT(SB) is a reference to the entry in the global offset
   212  	// table for 'name'.
   213  	NAME_GOTREF
   214  	// Indicates auto that was optimized away, but whose type
   215  	// we want to preserve in the DWARF debug info.
   216  	NAME_DELETED_AUTO
   217  	// Indicates that this is a reference to a TOC anchor.
   218  	NAME_TOCREF
   219  )
   220  
   221  //go:generate stringer -type AddrType
   222  
   223  type AddrType uint8
   224  
   225  const (
   226  	TYPE_NONE AddrType = iota
   227  	TYPE_BRANCH
   228  	TYPE_TEXTSIZE
   229  	TYPE_MEM
   230  	TYPE_CONST
   231  	TYPE_FCONST
   232  	TYPE_SCONST
   233  	TYPE_REG
   234  	TYPE_ADDR
   235  	TYPE_SHIFT
   236  	TYPE_REGREG
   237  	TYPE_REGREG2
   238  	TYPE_INDIR
   239  	TYPE_REGLIST
   240  )
   241  
   242  // Prog describes a single machine instruction.
   243  //
   244  // The general instruction form is:
   245  //
   246  //  (1) As.Scond From [, ...RestArgs], To
   247  //  (2) As.Scond From, Reg [, ...RestArgs], To, RegTo2
   248  //
   249  // where As is an opcode and the others are arguments:
   250  // From, Reg are sources, and To, RegTo2 are destinations.
   251  // RestArgs can hold additional sources and destinations.
   252  // Usually, not all arguments are present.
   253  // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
   254  // The Scond field holds additional condition bits for systems (like arm)
   255  // that have generalized conditional execution.
   256  // (2) form is present for compatibility with older code,
   257  // to avoid too much changes in a single swing.
   258  // (1) scheme is enough to express any kind of operand combination.
   259  //
   260  // Jump instructions use the Pcond field to point to the target instruction,
   261  // which must be in the same linked list as the jump instruction.
   262  //
   263  // The Progs for a given function are arranged in a list linked through the Link field.
   264  //
   265  // Each Prog is charged to a specific source line in the debug information,
   266  // specified by Pos.Line().
   267  // Every Prog has a Ctxt field that defines its context.
   268  // For performance reasons, Progs usually are usually bulk allocated, cached, and reused;
   269  // those bulk allocators should always be used, rather than new(Prog).
   270  //
   271  // The other fields not yet mentioned are for use by the back ends and should
   272  // be left zeroed by creators of Prog lists.
   273  type Prog struct {
   274  	Ctxt     *Link    // linker context
   275  	Link     *Prog    // next Prog in linked list
   276  	From     Addr     // first source operand
   277  	RestArgs []Addr   // can pack any operands that not fit into {Prog.From, Prog.To}
   278  	To       Addr     // destination operand (second is RegTo2 below)
   279  	Pcond    *Prog    // target of conditional jump
   280  	Forwd    *Prog    // for x86 back end
   281  	Rel      *Prog    // for x86, arm back ends
   282  	Pc       int64    // for back ends or assembler: virtual or actual program counter, depending on phase
   283  	Pos      src.XPos // source position of this instruction
   284  	Spadj    int32    // effect of instruction on stack pointer (increment or decrement amount)
   285  	As       As       // assembler opcode
   286  	Reg      int16    // 2nd source operand
   287  	RegTo2   int16    // 2nd destination operand
   288  	Mark     uint16   // bitmask of arch-specific items
   289  	Optab    uint16   // arch-specific opcode index
   290  	Scond    uint8    // bits that describe instruction suffixes (e.g. ARM conditions)
   291  	Back     uint8    // for x86 back end: backwards branch state
   292  	Ft       uint8    // for x86 back end: type index of Prog.From
   293  	Tt       uint8    // for x86 back end: type index of Prog.To
   294  	Isize    uint8    // for x86 back end: size of the instruction in bytes
   295  }
   296  
   297  // From3Type returns p.GetFrom3().Type, or TYPE_NONE when
   298  // p.GetFrom3() returns nil.
   299  //
   300  // Deprecated: for the same reasons as Prog.GetFrom3.
   301  func (p *Prog) From3Type() AddrType {
   302  	if p.RestArgs == nil {
   303  		return TYPE_NONE
   304  	}
   305  	return p.RestArgs[0].Type
   306  }
   307  
   308  // GetFrom3 returns second source operand (the first is Prog.From).
   309  // In combination with Prog.From and Prog.To it makes common 3 operand
   310  // case easier to use.
   311  //
   312  // Should be used only when RestArgs is set with SetFrom3.
   313  //
   314  // Deprecated: better use RestArgs directly or define backend-specific getters.
   315  // Introduced to simplify transition to []Addr.
   316  // Usage of this is discouraged due to fragility and lack of guarantees.
   317  func (p *Prog) GetFrom3() *Addr {
   318  	if p.RestArgs == nil {
   319  		return nil
   320  	}
   321  	return &p.RestArgs[0]
   322  }
   323  
   324  // SetFrom3 assigns []Addr{a} to p.RestArgs.
   325  // In pair with Prog.GetFrom3 it can help in emulation of Prog.From3.
   326  //
   327  // Deprecated: for the same reasons as Prog.GetFrom3.
   328  func (p *Prog) SetFrom3(a Addr) {
   329  	p.RestArgs = []Addr{a}
   330  }
   331  
   332  // An As denotes an assembler opcode.
   333  // There are some portable opcodes, declared here in package obj,
   334  // that are common to all architectures.
   335  // However, the majority of opcodes are arch-specific
   336  // and are declared in their respective architecture's subpackage.
   337  type As int16
   338  
   339  // These are the portable opcodes.
   340  const (
   341  	AXXX As = iota
   342  	ACALL
   343  	ADUFFCOPY
   344  	ADUFFZERO
   345  	AEND
   346  	AFUNCDATA
   347  	AJMP
   348  	ANOP
   349  	APCALIGN
   350  	APCDATA
   351  	ARET
   352  	AGETCALLERPC
   353  	ATEXT
   354  	AUNDEF
   355  	A_ARCHSPECIFIC
   356  )
   357  
   358  // Each architecture is allotted a distinct subspace of opcode values
   359  // for declaring its arch-specific opcodes.
   360  // Within this subspace, the first arch-specific opcode should be
   361  // at offset A_ARCHSPECIFIC.
   362  //
   363  // Subspaces are aligned to a power of two so opcodes can be masked
   364  // with AMask and used as compact array indices.
   365  const (
   366  	ABase386 = (1 + iota) << 11
   367  	ABaseARM
   368  	ABaseAMD64
   369  	ABasePPC64
   370  	ABaseARM64
   371  	ABaseMIPS
   372  	ABaseS390X
   373  	ABaseWasm
   374  
   375  	AllowedOpCodes = 1 << 11            // The number of opcodes available for any given architecture.
   376  	AMask          = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
   377  )
   378  
   379  // An LSym is the sort of symbol that is written to an object file.
   380  type LSym struct {
   381  	Name string
   382  	Type objabi.SymKind
   383  	Attribute
   384  
   385  	RefIdx int // Index of this symbol in the symbol reference list.
   386  	Size   int64
   387  	Gotype *LSym
   388  	P      []byte
   389  	R      []Reloc
   390  
   391  	Func *FuncInfo
   392  }
   393  
   394  // A FuncInfo contains extra fields for STEXT symbols.
   395  type FuncInfo struct {
   396  	Args     int32
   397  	Locals   int32
   398  	Text     *Prog
   399  	Autom    []*Auto
   400  	Pcln     Pcln
   401  	InlMarks []InlMark
   402  
   403  	dwarfInfoSym   *LSym
   404  	dwarfLocSym    *LSym
   405  	dwarfRangesSym *LSym
   406  	dwarfAbsFnSym  *LSym
   407  	dwarfIsStmtSym *LSym
   408  
   409  	GCArgs       *LSym
   410  	GCLocals     *LSym
   411  	GCRegs       *LSym
   412  	StackObjects *LSym
   413  }
   414  
   415  type InlMark struct {
   416  	// When unwinding from an instruction in an inlined body, mark
   417  	// where we should unwind to.
   418  	// id records the global inlining id of the inlined body.
   419  	// p records the location of an instruction in the parent (inliner) frame.
   420  	p  *Prog
   421  	id int32
   422  }
   423  
   424  // Mark p as the instruction to set as the pc when
   425  // "unwinding" the inlining global frame id. Usually it should be
   426  // instruction with a file:line at the callsite, and occur
   427  // just before the body of the inlined function.
   428  func (fi *FuncInfo) AddInlMark(p *Prog, id int32) {
   429  	fi.InlMarks = append(fi.InlMarks, InlMark{p: p, id: id})
   430  }
   431  
   432  //go:generate stringer -type ABI
   433  
   434  // ABI is the calling convention of a text symbol.
   435  type ABI uint8
   436  
   437  const (
   438  	// ABI0 is the stable stack-based ABI. It's important that the
   439  	// value of this is "0": we can't distinguish between
   440  	// references to data and ABI0 text symbols in assembly code,
   441  	// and hence this doesn't distinguish between symbols without
   442  	// an ABI and text symbols with ABI0.
   443  	ABI0 ABI = iota
   444  
   445  	// ABIInternal is the internal ABI that may change between Go
   446  	// versions. All Go functions use the internal ABI and the
   447  	// compiler generates wrappers for calls to and from other
   448  	// ABIs.
   449  	ABIInternal
   450  
   451  	ABICount
   452  )
   453  
   454  // Attribute is a set of symbol attributes.
   455  type Attribute uint16
   456  
   457  const (
   458  	AttrDuplicateOK Attribute = 1 << iota
   459  	AttrCFunc
   460  	AttrNoSplit
   461  	AttrLeaf
   462  	AttrWrapper
   463  	AttrNeedCtxt
   464  	AttrNoFrame
   465  	AttrSeenGlobl
   466  	AttrOnList
   467  	AttrStatic
   468  
   469  	// MakeTypelink means that the type should have an entry in the typelink table.
   470  	AttrMakeTypelink
   471  
   472  	// ReflectMethod means the function may call reflect.Type.Method or
   473  	// reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
   474  	// can be used through a custom interface), so ReflectMethod may be
   475  	// set in some cases when the reflect package is not called.
   476  	//
   477  	// Used by the linker to determine what methods can be pruned.
   478  	AttrReflectMethod
   479  
   480  	// Local means make the symbol local even when compiling Go code to reference Go
   481  	// symbols in other shared libraries, as in this mode symbols are global by
   482  	// default. "local" here means in the sense of the dynamic linker, i.e. not
   483  	// visible outside of the module (shared library or executable) that contains its
   484  	// definition. (When not compiling to support Go shared libraries, all symbols are
   485  	// local in this sense unless there is a cgo_export_* directive).
   486  	AttrLocal
   487  
   488  	// For function symbols; indicates that the specified function was the
   489  	// target of an inline during compilation
   490  	AttrWasInlined
   491  
   492  	// attrABIBase is the value at which the ABI is encoded in
   493  	// Attribute. This must be last; all bits after this are
   494  	// assumed to be an ABI value.
   495  	//
   496  	// MUST BE LAST since all bits above this comprise the ABI.
   497  	attrABIBase
   498  )
   499  
   500  func (a Attribute) DuplicateOK() bool   { return a&AttrDuplicateOK != 0 }
   501  func (a Attribute) MakeTypelink() bool  { return a&AttrMakeTypelink != 0 }
   502  func (a Attribute) CFunc() bool         { return a&AttrCFunc != 0 }
   503  func (a Attribute) NoSplit() bool       { return a&AttrNoSplit != 0 }
   504  func (a Attribute) Leaf() bool          { return a&AttrLeaf != 0 }
   505  func (a Attribute) SeenGlobl() bool     { return a&AttrSeenGlobl != 0 }
   506  func (a Attribute) OnList() bool        { return a&AttrOnList != 0 }
   507  func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 }
   508  func (a Attribute) Local() bool         { return a&AttrLocal != 0 }
   509  func (a Attribute) Wrapper() bool       { return a&AttrWrapper != 0 }
   510  func (a Attribute) NeedCtxt() bool      { return a&AttrNeedCtxt != 0 }
   511  func (a Attribute) NoFrame() bool       { return a&AttrNoFrame != 0 }
   512  func (a Attribute) Static() bool        { return a&AttrStatic != 0 }
   513  func (a Attribute) WasInlined() bool    { return a&AttrWasInlined != 0 }
   514  
   515  func (a *Attribute) Set(flag Attribute, value bool) {
   516  	if value {
   517  		*a |= flag
   518  	} else {
   519  		*a &^= flag
   520  	}
   521  }
   522  
   523  func (a Attribute) ABI() ABI { return ABI(a / attrABIBase) }
   524  func (a *Attribute) SetABI(abi ABI) {
   525  	const mask = 1 // Only one ABI bit for now.
   526  	*a = (*a &^ (mask * attrABIBase)) | Attribute(abi)*attrABIBase
   527  }
   528  
   529  var textAttrStrings = [...]struct {
   530  	bit Attribute
   531  	s   string
   532  }{
   533  	{bit: AttrDuplicateOK, s: "DUPOK"},
   534  	{bit: AttrMakeTypelink, s: ""},
   535  	{bit: AttrCFunc, s: "CFUNC"},
   536  	{bit: AttrNoSplit, s: "NOSPLIT"},
   537  	{bit: AttrLeaf, s: "LEAF"},
   538  	{bit: AttrSeenGlobl, s: ""},
   539  	{bit: AttrOnList, s: ""},
   540  	{bit: AttrReflectMethod, s: "REFLECTMETHOD"},
   541  	{bit: AttrLocal, s: "LOCAL"},
   542  	{bit: AttrWrapper, s: "WRAPPER"},
   543  	{bit: AttrNeedCtxt, s: "NEEDCTXT"},
   544  	{bit: AttrNoFrame, s: "NOFRAME"},
   545  	{bit: AttrStatic, s: "STATIC"},
   546  	{bit: AttrWasInlined, s: ""},
   547  }
   548  
   549  // TextAttrString formats a for printing in as part of a TEXT prog.
   550  func (a Attribute) TextAttrString() string {
   551  	var s string
   552  	for _, x := range textAttrStrings {
   553  		if a&x.bit != 0 {
   554  			if x.s != "" {
   555  				s += x.s + "|"
   556  			}
   557  			a &^= x.bit
   558  		}
   559  	}
   560  	switch a.ABI() {
   561  	case ABI0:
   562  	case ABIInternal:
   563  		s += "ABIInternal|"
   564  		a.SetABI(0) // Clear ABI so we don't print below.
   565  	}
   566  	if a != 0 {
   567  		s += fmt.Sprintf("UnknownAttribute(%d)|", a)
   568  	}
   569  	// Chop off trailing |, if present.
   570  	if len(s) > 0 {
   571  		s = s[:len(s)-1]
   572  	}
   573  	return s
   574  }
   575  
   576  // The compiler needs LSym to satisfy fmt.Stringer, because it stores
   577  // an LSym in ssa.ExternSymbol.
   578  func (s *LSym) String() string {
   579  	return s.Name
   580  }
   581  
   582  type Pcln struct {
   583  	Pcsp        Pcdata
   584  	Pcfile      Pcdata
   585  	Pcline      Pcdata
   586  	Pcinline    Pcdata
   587  	Pcdata      []Pcdata
   588  	Funcdata    []*LSym
   589  	Funcdataoff []int64
   590  	File        []string
   591  	Lastfile    string
   592  	Lastindex   int
   593  	InlTree     InlTree // per-function inlining tree extracted from the global tree
   594  }
   595  
   596  type Reloc struct {
   597  	Off  int32
   598  	Siz  uint8
   599  	Type objabi.RelocType
   600  	Add  int64
   601  	Sym  *LSym
   602  }
   603  
   604  type Auto struct {
   605  	Asym    *LSym
   606  	Aoffset int32
   607  	Name    AddrName
   608  	Gotype  *LSym
   609  }
   610  
   611  type Pcdata struct {
   612  	P []byte
   613  }
   614  
   615  // Link holds the context for writing object code from a compiler
   616  // to be linker input or for reading that input into the linker.
   617  type Link struct {
   618  	Headtype           objabi.HeadType
   619  	Arch               *LinkArch
   620  	Debugasm           int
   621  	Debugvlog          bool
   622  	Debugpcln          string
   623  	Flag_shared        bool
   624  	Flag_dynlink       bool
   625  	Flag_optimize      bool
   626  	Flag_locationlists bool
   627  	Bso                *bufio.Writer
   628  	Pathname           string
   629  	hashmu             sync.Mutex       // protects hash, funchash
   630  	hash               map[string]*LSym // name -> sym mapping
   631  	funchash           map[string]*LSym // name -> sym mapping for ABIInternal syms
   632  	statichash         map[string]*LSym // name -> sym mapping for static syms
   633  	PosTable           src.PosTable
   634  	InlTree            InlTree // global inlining tree used by gc/inl.go
   635  	DwFixups           *DwarfFixupTable
   636  	Imports            []string
   637  	DiagFunc           func(string, ...interface{})
   638  	DiagFlush          func()
   639  	DebugInfo          func(fn *LSym, curfn interface{}) ([]dwarf.Scope, dwarf.InlCalls) // if non-nil, curfn is a *gc.Node
   640  	GenAbstractFunc    func(fn *LSym)
   641  	Errors             int
   642  
   643  	InParallel           bool // parallel backend phase in effect
   644  	Framepointer_enabled bool
   645  
   646  	// state for writing objects
   647  	Text []*LSym
   648  	Data []*LSym
   649  
   650  	// ABIAliases are text symbols that should be aliased to all
   651  	// ABIs. These symbols may only be referenced and not defined
   652  	// by this object, since the need for an alias may appear in a
   653  	// different object than the definition. Hence, this
   654  	// information can't be carried in the symbol definition.
   655  	//
   656  	// TODO(austin): Replace this with ABI wrappers once the ABIs
   657  	// actually diverge.
   658  	ABIAliases []*LSym
   659  }
   660  
   661  func (ctxt *Link) Diag(format string, args ...interface{}) {
   662  	ctxt.Errors++
   663  	ctxt.DiagFunc(format, args...)
   664  }
   665  
   666  func (ctxt *Link) Logf(format string, args ...interface{}) {
   667  	fmt.Fprintf(ctxt.Bso, format, args...)
   668  	ctxt.Bso.Flush()
   669  }
   670  
   671  // The smallest possible offset from the hardware stack pointer to a local
   672  // variable on the stack. Architectures that use a link register save its value
   673  // on the stack in the function prologue and so always have a pointer between
   674  // the hardware stack pointer and the local variable area.
   675  func (ctxt *Link) FixedFrameSize() int64 {
   676  	switch ctxt.Arch.Family {
   677  	case sys.AMD64, sys.I386, sys.Wasm:
   678  		return 0
   679  	case sys.PPC64:
   680  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   681  		// just use that much stack always on ppc64x.
   682  		return int64(4 * ctxt.Arch.PtrSize)
   683  	default:
   684  		return int64(ctxt.Arch.PtrSize)
   685  	}
   686  }
   687  
   688  // LinkArch is the definition of a single architecture.
   689  type LinkArch struct {
   690  	*sys.Arch
   691  	Init           func(*Link)
   692  	Preprocess     func(*Link, *LSym, ProgAlloc)
   693  	Assemble       func(*Link, *LSym, ProgAlloc)
   694  	Progedit       func(*Link, *Prog, ProgAlloc)
   695  	UnaryDst       map[As]bool // Instruction takes one operand, a destination.
   696  	DWARFRegisters map[int16]int16
   697  }