Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/internal/obj/link.go

Documentation: cmd/internal/obj

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/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/goobj"
    37  	"cmd/internal/objabi"
    38  	"cmd/internal/src"
    39  	"cmd/internal/sys"
    40  	"fmt"
    41  	"sync"
    42  )
    43  
    44  // An Addr is an argument to an instruction.
    45  // The general forms and their encodings are:
    46  //
    47  //	sym±offset(symkind)(reg)(index*scale)
    48  //		Memory reference at address &sym(symkind) + offset + reg + index*scale.
    49  //		Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
    50  //		If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
    51  //		To force a parsing as index*scale, write (index*1).
    52  //		Encoding:
    53  //			type = TYPE_MEM
    54  //			name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
    55  //			sym = sym
    56  //			offset = ±offset
    57  //			reg = reg (REG_*)
    58  //			index = index (REG_*)
    59  //			scale = scale (1, 2, 4, 8)
    60  //
    61  //	$<mem>
    62  //		Effective address of memory reference <mem>, defined above.
    63  //		Encoding: same as memory reference, but type = TYPE_ADDR.
    64  //
    65  //	$<±integer value>
    66  //		This is a special case of $<mem>, in which only ±offset is present.
    67  //		It has a separate type for easy recognition.
    68  //		Encoding:
    69  //			type = TYPE_CONST
    70  //			offset = ±integer value
    71  //
    72  //	*<mem>
    73  //		Indirect reference through memory reference <mem>, defined above.
    74  //		Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
    75  //		pointer stored in the data word sym(SB), not a function named sym(SB).
    76  //		Encoding: same as above, but type = TYPE_INDIR.
    77  //
    78  //	$*$<mem>
    79  //		No longer used.
    80  //		On machines with actual SB registers, $*$<mem> forced the
    81  //		instruction encoding to use a full 32-bit constant, never a
    82  //		reference relative to SB.
    83  //
    84  //	$<floating point literal>
    85  //		Floating point constant value.
    86  //		Encoding:
    87  //			type = TYPE_FCONST
    88  //			val = floating point value
    89  //
    90  //	$<string literal, up to 8 chars>
    91  //		String literal value (raw bytes used for DATA instruction).
    92  //		Encoding:
    93  //			type = TYPE_SCONST
    94  //			val = string
    95  //
    96  //	<register name>
    97  //		Any register: integer, floating point, control, segment, and so on.
    98  //		If looking for specific register kind, must check type and reg value range.
    99  //		Encoding:
   100  //			type = TYPE_REG
   101  //			reg = reg (REG_*)
   102  //
   103  //	x(PC)
   104  //		Encoding:
   105  //			type = TYPE_BRANCH
   106  //			val = Prog* reference OR ELSE offset = target pc (branch takes priority)
   107  //
   108  //	$±x-±y
   109  //		Final argument to TEXT, specifying local frame size x and argument size y.
   110  //		In this form, x and y are integer literals only, not arbitrary expressions.
   111  //		This avoids parsing ambiguities due to the use of - as a separator.
   112  //		The ± are optional.
   113  //		If the final argument to TEXT omits the -±y, the encoding should still
   114  //		use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
   115  //		Encoding:
   116  //			type = TYPE_TEXTSIZE
   117  //			offset = x
   118  //			val = int32(y)
   119  //
   120  //	reg<<shift, reg>>shift, reg->shift, reg@>shift
   121  //		Shifted register value, for ARM and ARM64.
   122  //		In this form, reg must be a register and shift can be a register or an integer constant.
   123  //		Encoding:
   124  //			type = TYPE_SHIFT
   125  //		On ARM:
   126  //			offset = (reg&15) | shifttype<<5 | count
   127  //			shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
   128  //			count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
   129  //		On ARM64:
   130  //			offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
   131  //			shifttype = 0, 1, 2 for <<, >>, ->
   132  //
   133  //	(reg, reg)
   134  //		A destination register pair. When used as the last argument of an instruction,
   135  //		this form makes clear that both registers are destinations.
   136  //		Encoding:
   137  //			type = TYPE_REGREG
   138  //			reg = first register
   139  //			offset = second register
   140  //
   141  //	[reg, reg, reg-reg]
   142  //		Register list for ARM, ARM64, 386/AMD64.
   143  //		Encoding:
   144  //			type = TYPE_REGLIST
   145  //		On ARM:
   146  //			offset = bit mask of registers in list; R0 is low bit.
   147  //		On ARM64:
   148  //			offset = register count (Q:size) | arrangement (opcode) | first register
   149  //		On 386/AMD64:
   150  //			reg = range low register
   151  //			offset = 2 packed registers + kind tag (see x86.EncodeRegisterRange)
   152  //
   153  //	reg, reg
   154  //		Register pair for ARM.
   155  //		TYPE_REGREG2
   156  //
   157  //	(reg+reg)
   158  //		Register pair for PPC64.
   159  //		Encoding:
   160  //			type = TYPE_MEM
   161  //			reg = first register
   162  //			index = second register
   163  //			scale = 1
   164  //
   165  //	reg.[US]XT[BHWX]
   166  //		Register extension for ARM64
   167  //		Encoding:
   168  //			type = TYPE_REG
   169  //			reg = REG_[US]XT[BHWX] + register + shift amount
   170  //			offset = ((reg&31) << 16) | (exttype << 13) | (amount<<10)
   171  //
   172  //	reg.<T>
   173  //		Register arrangement for ARM64 SIMD register
   174  //		e.g.: V1.S4, V2.S2, V7.D2, V2.H4, V6.B16
   175  //		Encoding:
   176  //			type = TYPE_REG
   177  //			reg = REG_ARNG + register + arrangement
   178  //
   179  //	reg.<T>[index]
   180  //		Register element for ARM64
   181  //		Encoding:
   182  //			type = TYPE_REG
   183  //			reg = REG_ELEM + register + arrangement
   184  //			index = element index
   185  
   186  type Addr struct {
   187  	Reg    int16
   188  	Index  int16
   189  	Scale  int16 // Sometimes holds a register.
   190  	Type   AddrType
   191  	Name   AddrName
   192  	Class  int8
   193  	Offset int64
   194  	Sym    *LSym
   195  
   196  	// argument value:
   197  	//	for TYPE_SCONST, a string
   198  	//	for TYPE_FCONST, a float64
   199  	//	for TYPE_BRANCH, a *Prog (optional)
   200  	//	for TYPE_TEXTSIZE, an int32 (optional)
   201  	Val interface{}
   202  }
   203  
   204  type AddrName int8
   205  
   206  const (
   207  	NAME_NONE AddrName = iota
   208  	NAME_EXTERN
   209  	NAME_STATIC
   210  	NAME_AUTO
   211  	NAME_PARAM
   212  	// A reference to name@GOT(SB) is a reference to the entry in the global offset
   213  	// table for 'name'.
   214  	NAME_GOTREF
   215  	// Indicates that this is a reference to a TOC anchor.
   216  	NAME_TOCREF
   217  )
   218  
   219  //go:generate stringer -type AddrType
   220  
   221  type AddrType uint8
   222  
   223  const (
   224  	TYPE_NONE AddrType = iota
   225  	TYPE_BRANCH
   226  	TYPE_TEXTSIZE
   227  	TYPE_MEM
   228  	TYPE_CONST
   229  	TYPE_FCONST
   230  	TYPE_SCONST
   231  	TYPE_REG
   232  	TYPE_ADDR
   233  	TYPE_SHIFT
   234  	TYPE_REGREG
   235  	TYPE_REGREG2
   236  	TYPE_INDIR
   237  	TYPE_REGLIST
   238  )
   239  
   240  func (a *Addr) Target() *Prog {
   241  	if a.Type == TYPE_BRANCH && a.Val != nil {
   242  		return a.Val.(*Prog)
   243  	}
   244  	return nil
   245  }
   246  func (a *Addr) SetTarget(t *Prog) {
   247  	if a.Type != TYPE_BRANCH {
   248  		panic("setting branch target when type is not TYPE_BRANCH")
   249  	}
   250  	a.Val = t
   251  }
   252  
   253  // Prog describes a single machine instruction.
   254  //
   255  // The general instruction form is:
   256  //
   257  //	(1) As.Scond From [, ...RestArgs], To
   258  //	(2) As.Scond From, Reg [, ...RestArgs], To, RegTo2
   259  //
   260  // where As is an opcode and the others are arguments:
   261  // From, Reg are sources, and To, RegTo2 are destinations.
   262  // RestArgs can hold additional sources and destinations.
   263  // Usually, not all arguments are present.
   264  // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
   265  // The Scond field holds additional condition bits for systems (like arm)
   266  // that have generalized conditional execution.
   267  // (2) form is present for compatibility with older code,
   268  // to avoid too much changes in a single swing.
   269  // (1) scheme is enough to express any kind of operand combination.
   270  //
   271  // Jump instructions use the To.Val field to point to the target *Prog,
   272  // which must be in the same linked list as the jump instruction.
   273  //
   274  // The Progs for a given function are arranged in a list linked through the Link field.
   275  //
   276  // Each Prog is charged to a specific source line in the debug information,
   277  // specified by Pos.Line().
   278  // Every Prog has a Ctxt field that defines its context.
   279  // For performance reasons, Progs usually are usually bulk allocated, cached, and reused;
   280  // those bulk allocators should always be used, rather than new(Prog).
   281  //
   282  // The other fields not yet mentioned are for use by the back ends and should
   283  // be left zeroed by creators of Prog lists.
   284  type Prog struct {
   285  	Ctxt     *Link     // linker context
   286  	Link     *Prog     // next Prog in linked list
   287  	From     Addr      // first source operand
   288  	RestArgs []AddrPos // can pack any operands that not fit into {Prog.From, Prog.To}
   289  	To       Addr      // destination operand (second is RegTo2 below)
   290  	Pool     *Prog     // constant pool entry, for arm,arm64 back ends
   291  	Forwd    *Prog     // for x86 back end
   292  	Rel      *Prog     // for x86, arm back ends
   293  	Pc       int64     // for back ends or assembler: virtual or actual program counter, depending on phase
   294  	Pos      src.XPos  // source position of this instruction
   295  	Spadj    int32     // effect of instruction on stack pointer (increment or decrement amount)
   296  	As       As        // assembler opcode
   297  	Reg      int16     // 2nd source operand
   298  	RegTo2   int16     // 2nd destination operand
   299  	Mark     uint16    // bitmask of arch-specific items
   300  	Optab    uint16    // arch-specific opcode index
   301  	Scond    uint8     // bits that describe instruction suffixes (e.g. ARM conditions)
   302  	Back     uint8     // for x86 back end: backwards branch state
   303  	Ft       uint8     // for x86 back end: type index of Prog.From
   304  	Tt       uint8     // for x86 back end: type index of Prog.To
   305  	Isize    uint8     // for x86 back end: size of the instruction in bytes
   306  }
   307  
   308  // Pos indicates whether the oprand is the source or the destination.
   309  type AddrPos struct {
   310  	Addr
   311  	Pos OperandPos
   312  }
   313  
   314  type OperandPos int8
   315  
   316  const (
   317  	Source OperandPos = iota
   318  	Destination
   319  )
   320  
   321  // From3Type returns p.GetFrom3().Type, or TYPE_NONE when
   322  // p.GetFrom3() returns nil.
   323  //
   324  // Deprecated: for the same reasons as Prog.GetFrom3.
   325  func (p *Prog) From3Type() AddrType {
   326  	if p.RestArgs == nil {
   327  		return TYPE_NONE
   328  	}
   329  	return p.RestArgs[0].Type
   330  }
   331  
   332  // GetFrom3 returns second source operand (the first is Prog.From).
   333  // In combination with Prog.From and Prog.To it makes common 3 operand
   334  // case easier to use.
   335  //
   336  // Should be used only when RestArgs is set with SetFrom3.
   337  //
   338  // Deprecated: better use RestArgs directly or define backend-specific getters.
   339  // Introduced to simplify transition to []Addr.
   340  // Usage of this is discouraged due to fragility and lack of guarantees.
   341  func (p *Prog) GetFrom3() *Addr {
   342  	if p.RestArgs == nil {
   343  		return nil
   344  	}
   345  	return &p.RestArgs[0].Addr
   346  }
   347  
   348  // SetFrom3 assigns []Args{{a, 0}} to p.RestArgs.
   349  // In pair with Prog.GetFrom3 it can help in emulation of Prog.From3.
   350  //
   351  // Deprecated: for the same reasons as Prog.GetFrom3.
   352  func (p *Prog) SetFrom3(a Addr) {
   353  	p.RestArgs = []AddrPos{{a, Source}}
   354  }
   355  
   356  // SetTo2 assings []Args{{a, 1}} to p.RestArgs when the second destination
   357  // operand does not fit into prog.RegTo2.
   358  func (p *Prog) SetTo2(a Addr) {
   359  	p.RestArgs = []AddrPos{{a, Destination}}
   360  }
   361  
   362  // GetTo2 returns the second destination operand.
   363  func (p *Prog) GetTo2() *Addr {
   364  	if p.RestArgs == nil {
   365  		return nil
   366  	}
   367  	return &p.RestArgs[0].Addr
   368  }
   369  
   370  // SetRestArgs assigns more than one source operands to p.RestArgs.
   371  func (p *Prog) SetRestArgs(args []Addr) {
   372  	for i := range args {
   373  		p.RestArgs = append(p.RestArgs, AddrPos{args[i], Source})
   374  	}
   375  }
   376  
   377  // An As denotes an assembler opcode.
   378  // There are some portable opcodes, declared here in package obj,
   379  // that are common to all architectures.
   380  // However, the majority of opcodes are arch-specific
   381  // and are declared in their respective architecture's subpackage.
   382  type As int16
   383  
   384  // These are the portable opcodes.
   385  const (
   386  	AXXX As = iota
   387  	ACALL
   388  	ADUFFCOPY
   389  	ADUFFZERO
   390  	AEND
   391  	AFUNCDATA
   392  	AJMP
   393  	ANOP
   394  	APCALIGN
   395  	APCDATA
   396  	ARET
   397  	AGETCALLERPC
   398  	ATEXT
   399  	AUNDEF
   400  	A_ARCHSPECIFIC
   401  )
   402  
   403  // Each architecture is allotted a distinct subspace of opcode values
   404  // for declaring its arch-specific opcodes.
   405  // Within this subspace, the first arch-specific opcode should be
   406  // at offset A_ARCHSPECIFIC.
   407  //
   408  // Subspaces are aligned to a power of two so opcodes can be masked
   409  // with AMask and used as compact array indices.
   410  const (
   411  	ABase386 = (1 + iota) << 11
   412  	ABaseARM
   413  	ABaseAMD64
   414  	ABasePPC64
   415  	ABaseARM64
   416  	ABaseMIPS
   417  	ABaseRISCV
   418  	ABaseS390X
   419  	ABaseWasm
   420  
   421  	AllowedOpCodes = 1 << 11            // The number of opcodes available for any given architecture.
   422  	AMask          = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
   423  )
   424  
   425  // An LSym is the sort of symbol that is written to an object file.
   426  // It represents Go symbols in a flat pkg+"."+name namespace.
   427  type LSym struct {
   428  	Name string
   429  	Type objabi.SymKind
   430  	Attribute
   431  
   432  	Size   int64
   433  	Gotype *LSym
   434  	P      []byte
   435  	R      []Reloc
   436  
   437  	Extra *interface{} // *FuncInfo or *FileInfo, if present
   438  
   439  	Pkg    string
   440  	PkgIdx int32
   441  	SymIdx int32
   442  }
   443  
   444  // A FuncInfo contains extra fields for STEXT symbols.
   445  type FuncInfo struct {
   446  	Args     int32
   447  	Locals   int32
   448  	Align    int32
   449  	FuncID   objabi.FuncID
   450  	Text     *Prog
   451  	Autot    map[*LSym]struct{}
   452  	Pcln     Pcln
   453  	InlMarks []InlMark
   454  
   455  	dwarfInfoSym       *LSym
   456  	dwarfLocSym        *LSym
   457  	dwarfRangesSym     *LSym
   458  	dwarfAbsFnSym      *LSym
   459  	dwarfDebugLinesSym *LSym
   460  
   461  	GCArgs             *LSym
   462  	GCLocals           *LSym
   463  	StackObjects       *LSym
   464  	OpenCodedDeferInfo *LSym
   465  
   466  	FuncInfoSym *LSym
   467  }
   468  
   469  // NewFuncInfo allocates and returns a FuncInfo for LSym.
   470  func (s *LSym) NewFuncInfo() *FuncInfo {
   471  	if s.Extra != nil {
   472  		panic(fmt.Sprintf("invalid use of LSym - NewFuncInfo with Extra of type %T", *s.Extra))
   473  	}
   474  	f := new(FuncInfo)
   475  	s.Extra = new(interface{})
   476  	*s.Extra = f
   477  	return f
   478  }
   479  
   480  // Func returns the *FuncInfo associated with s, or else nil.
   481  func (s *LSym) Func() *FuncInfo {
   482  	if s.Extra == nil {
   483  		return nil
   484  	}
   485  	f, _ := (*s.Extra).(*FuncInfo)
   486  	return f
   487  }
   488  
   489  // A FileInfo contains extra fields for SDATA symbols backed by files.
   490  // (If LSym.Extra is a *FileInfo, LSym.P == nil.)
   491  type FileInfo struct {
   492  	Name string // name of file to read into object file
   493  	Size int64  // length of file
   494  }
   495  
   496  // NewFileInfo allocates and returns a FileInfo for LSym.
   497  func (s *LSym) NewFileInfo() *FileInfo {
   498  	if s.Extra != nil {
   499  		panic(fmt.Sprintf("invalid use of LSym - NewFileInfo with Extra of type %T", *s.Extra))
   500  	}
   501  	f := new(FileInfo)
   502  	s.Extra = new(interface{})
   503  	*s.Extra = f
   504  	return f
   505  }
   506  
   507  // File returns the *FileInfo associated with s, or else nil.
   508  func (s *LSym) File() *FileInfo {
   509  	if s.Extra == nil {
   510  		return nil
   511  	}
   512  	f, _ := (*s.Extra).(*FileInfo)
   513  	return f
   514  }
   515  
   516  type InlMark struct {
   517  	// When unwinding from an instruction in an inlined body, mark
   518  	// where we should unwind to.
   519  	// id records the global inlining id of the inlined body.
   520  	// p records the location of an instruction in the parent (inliner) frame.
   521  	p  *Prog
   522  	id int32
   523  }
   524  
   525  // Mark p as the instruction to set as the pc when
   526  // "unwinding" the inlining global frame id. Usually it should be
   527  // instruction with a file:line at the callsite, and occur
   528  // just before the body of the inlined function.
   529  func (fi *FuncInfo) AddInlMark(p *Prog, id int32) {
   530  	fi.InlMarks = append(fi.InlMarks, InlMark{p: p, id: id})
   531  }
   532  
   533  // Record the type symbol for an auto variable so that the linker
   534  // an emit DWARF type information for the type.
   535  func (fi *FuncInfo) RecordAutoType(gotype *LSym) {
   536  	if fi.Autot == nil {
   537  		fi.Autot = make(map[*LSym]struct{})
   538  	}
   539  	fi.Autot[gotype] = struct{}{}
   540  }
   541  
   542  //go:generate stringer -type ABI
   543  
   544  // ABI is the calling convention of a text symbol.
   545  type ABI uint8
   546  
   547  const (
   548  	// ABI0 is the stable stack-based ABI. It's important that the
   549  	// value of this is "0": we can't distinguish between
   550  	// references to data and ABI0 text symbols in assembly code,
   551  	// and hence this doesn't distinguish between symbols without
   552  	// an ABI and text symbols with ABI0.
   553  	ABI0 ABI = iota
   554  
   555  	// ABIInternal is the internal ABI that may change between Go
   556  	// versions. All Go functions use the internal ABI and the
   557  	// compiler generates wrappers for calls to and from other
   558  	// ABIs.
   559  	ABIInternal
   560  
   561  	ABICount
   562  )
   563  
   564  // ParseABI converts from a string representation in 'abistr' to the
   565  // corresponding ABI value. Second return value is TRUE if the
   566  // abi string is recognized, FALSE otherwise.
   567  func ParseABI(abistr string) (ABI, bool) {
   568  	switch abistr {
   569  	default:
   570  		return ABI0, false
   571  	case "ABI0":
   572  		return ABI0, true
   573  	case "ABIInternal":
   574  		return ABIInternal, true
   575  	}
   576  }
   577  
   578  // Attribute is a set of symbol attributes.
   579  type Attribute uint32
   580  
   581  const (
   582  	AttrDuplicateOK Attribute = 1 << iota
   583  	AttrCFunc
   584  	AttrNoSplit
   585  	AttrLeaf
   586  	AttrWrapper
   587  	AttrNeedCtxt
   588  	AttrNoFrame
   589  	AttrOnList
   590  	AttrStatic
   591  
   592  	// MakeTypelink means that the type should have an entry in the typelink table.
   593  	AttrMakeTypelink
   594  
   595  	// ReflectMethod means the function may call reflect.Type.Method or
   596  	// reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
   597  	// can be used through a custom interface), so ReflectMethod may be
   598  	// set in some cases when the reflect package is not called.
   599  	//
   600  	// Used by the linker to determine what methods can be pruned.
   601  	AttrReflectMethod
   602  
   603  	// Local means make the symbol local even when compiling Go code to reference Go
   604  	// symbols in other shared libraries, as in this mode symbols are global by
   605  	// default. "local" here means in the sense of the dynamic linker, i.e. not
   606  	// visible outside of the module (shared library or executable) that contains its
   607  	// definition. (When not compiling to support Go shared libraries, all symbols are
   608  	// local in this sense unless there is a cgo_export_* directive).
   609  	AttrLocal
   610  
   611  	// For function symbols; indicates that the specified function was the
   612  	// target of an inline during compilation
   613  	AttrWasInlined
   614  
   615  	// TopFrame means that this function is an entry point and unwinders should not
   616  	// keep unwinding beyond this frame.
   617  	AttrTopFrame
   618  
   619  	// Indexed indicates this symbol has been assigned with an index (when using the
   620  	// new object file format).
   621  	AttrIndexed
   622  
   623  	// Only applied on type descriptor symbols, UsedInIface indicates this type is
   624  	// converted to an interface.
   625  	//
   626  	// Used by the linker to determine what methods can be pruned.
   627  	AttrUsedInIface
   628  
   629  	// ContentAddressable indicates this is a content-addressable symbol.
   630  	AttrContentAddressable
   631  
   632  	// attrABIBase is the value at which the ABI is encoded in
   633  	// Attribute. This must be last; all bits after this are
   634  	// assumed to be an ABI value.
   635  	//
   636  	// MUST BE LAST since all bits above this comprise the ABI.
   637  	attrABIBase
   638  )
   639  
   640  func (a Attribute) DuplicateOK() bool        { return a&AttrDuplicateOK != 0 }
   641  func (a Attribute) MakeTypelink() bool       { return a&AttrMakeTypelink != 0 }
   642  func (a Attribute) CFunc() bool              { return a&AttrCFunc != 0 }
   643  func (a Attribute) NoSplit() bool            { return a&AttrNoSplit != 0 }
   644  func (a Attribute) Leaf() bool               { return a&AttrLeaf != 0 }
   645  func (a Attribute) OnList() bool             { return a&AttrOnList != 0 }
   646  func (a Attribute) ReflectMethod() bool      { return a&AttrReflectMethod != 0 }
   647  func (a Attribute) Local() bool              { return a&AttrLocal != 0 }
   648  func (a Attribute) Wrapper() bool            { return a&AttrWrapper != 0 }
   649  func (a Attribute) NeedCtxt() bool           { return a&AttrNeedCtxt != 0 }
   650  func (a Attribute) NoFrame() bool            { return a&AttrNoFrame != 0 }
   651  func (a Attribute) Static() bool             { return a&AttrStatic != 0 }
   652  func (a Attribute) WasInlined() bool         { return a&AttrWasInlined != 0 }
   653  func (a Attribute) TopFrame() bool           { return a&AttrTopFrame != 0 }
   654  func (a Attribute) Indexed() bool            { return a&AttrIndexed != 0 }
   655  func (a Attribute) UsedInIface() bool        { return a&AttrUsedInIface != 0 }
   656  func (a Attribute) ContentAddressable() bool { return a&AttrContentAddressable != 0 }
   657  
   658  func (a *Attribute) Set(flag Attribute, value bool) {
   659  	if value {
   660  		*a |= flag
   661  	} else {
   662  		*a &^= flag
   663  	}
   664  }
   665  
   666  func (a Attribute) ABI() ABI { return ABI(a / attrABIBase) }
   667  func (a *Attribute) SetABI(abi ABI) {
   668  	const mask = 1 // Only one ABI bit for now.
   669  	*a = (*a &^ (mask * attrABIBase)) | Attribute(abi)*attrABIBase
   670  }
   671  
   672  var textAttrStrings = [...]struct {
   673  	bit Attribute
   674  	s   string
   675  }{
   676  	{bit: AttrDuplicateOK, s: "DUPOK"},
   677  	{bit: AttrMakeTypelink, s: ""},
   678  	{bit: AttrCFunc, s: "CFUNC"},
   679  	{bit: AttrNoSplit, s: "NOSPLIT"},
   680  	{bit: AttrLeaf, s: "LEAF"},
   681  	{bit: AttrOnList, s: ""},
   682  	{bit: AttrReflectMethod, s: "REFLECTMETHOD"},
   683  	{bit: AttrLocal, s: "LOCAL"},
   684  	{bit: AttrWrapper, s: "WRAPPER"},
   685  	{bit: AttrNeedCtxt, s: "NEEDCTXT"},
   686  	{bit: AttrNoFrame, s: "NOFRAME"},
   687  	{bit: AttrStatic, s: "STATIC"},
   688  	{bit: AttrWasInlined, s: ""},
   689  	{bit: AttrTopFrame, s: "TOPFRAME"},
   690  	{bit: AttrIndexed, s: ""},
   691  	{bit: AttrContentAddressable, s: ""},
   692  }
   693  
   694  // TextAttrString formats a for printing in as part of a TEXT prog.
   695  func (a Attribute) TextAttrString() string {
   696  	var s string
   697  	for _, x := range textAttrStrings {
   698  		if a&x.bit != 0 {
   699  			if x.s != "" {
   700  				s += x.s + "|"
   701  			}
   702  			a &^= x.bit
   703  		}
   704  	}
   705  	switch a.ABI() {
   706  	case ABI0:
   707  	case ABIInternal:
   708  		s += "ABIInternal|"
   709  		a.SetABI(0) // Clear ABI so we don't print below.
   710  	}
   711  	if a != 0 {
   712  		s += fmt.Sprintf("UnknownAttribute(%d)|", a)
   713  	}
   714  	// Chop off trailing |, if present.
   715  	if len(s) > 0 {
   716  		s = s[:len(s)-1]
   717  	}
   718  	return s
   719  }
   720  
   721  func (s *LSym) String() string {
   722  	return s.Name
   723  }
   724  
   725  // The compiler needs *LSym to be assignable to cmd/compile/internal/ssa.Sym.
   726  func (s *LSym) CanBeAnSSASym() {
   727  }
   728  
   729  type Pcln struct {
   730  	// Aux symbols for pcln
   731  	Pcsp        *LSym
   732  	Pcfile      *LSym
   733  	Pcline      *LSym
   734  	Pcinline    *LSym
   735  	Pcdata      []*LSym
   736  	Funcdata    []*LSym
   737  	Funcdataoff []int64
   738  	UsedFiles   map[goobj.CUFileIndex]struct{} // file indices used while generating pcfile
   739  	InlTree     InlTree                        // per-function inlining tree extracted from the global tree
   740  }
   741  
   742  type Reloc struct {
   743  	Off  int32
   744  	Siz  uint8
   745  	Type objabi.RelocType
   746  	Add  int64
   747  	Sym  *LSym
   748  }
   749  
   750  type Auto struct {
   751  	Asym    *LSym
   752  	Aoffset int32
   753  	Name    AddrName
   754  	Gotype  *LSym
   755  }
   756  
   757  // Link holds the context for writing object code from a compiler
   758  // to be linker input or for reading that input into the linker.
   759  type Link struct {
   760  	Headtype           objabi.HeadType
   761  	Arch               *LinkArch
   762  	Debugasm           int
   763  	Debugvlog          bool
   764  	Debugpcln          string
   765  	Flag_shared        bool
   766  	Flag_dynlink       bool
   767  	Flag_linkshared    bool
   768  	Flag_optimize      bool
   769  	Flag_locationlists bool
   770  	Retpoline          bool // emit use of retpoline stubs for indirect jmp/call
   771  	Bso                *bufio.Writer
   772  	Pathname           string
   773  	Pkgpath            string           // the current package's import path, "" if unknown
   774  	hashmu             sync.Mutex       // protects hash, funchash
   775  	hash               map[string]*LSym // name -> sym mapping
   776  	funchash           map[string]*LSym // name -> sym mapping for ABIInternal syms
   777  	statichash         map[string]*LSym // name -> sym mapping for static syms
   778  	PosTable           src.PosTable
   779  	InlTree            InlTree // global inlining tree used by gc/inl.go
   780  	DwFixups           *DwarfFixupTable
   781  	Imports            []goobj.ImportedPkg
   782  	DiagFunc           func(string, ...interface{})
   783  	DiagFlush          func()
   784  	DebugInfo          func(fn *LSym, info *LSym, curfn interface{}) ([]dwarf.Scope, dwarf.InlCalls) // if non-nil, curfn is a *gc.Node
   785  	GenAbstractFunc    func(fn *LSym)
   786  	Errors             int
   787  
   788  	InParallel    bool // parallel backend phase in effect
   789  	UseBASEntries bool // use Base Address Selection Entries in location lists and PC ranges
   790  	IsAsm         bool // is the source assembly language, which may contain surprising idioms (e.g., call tables)
   791  
   792  	// state for writing objects
   793  	Text []*LSym
   794  	Data []*LSym
   795  
   796  	// ABIAliases are text symbols that should be aliased to all
   797  	// ABIs. These symbols may only be referenced and not defined
   798  	// by this object, since the need for an alias may appear in a
   799  	// different object than the definition. Hence, this
   800  	// information can't be carried in the symbol definition.
   801  	//
   802  	// TODO(austin): Replace this with ABI wrappers once the ABIs
   803  	// actually diverge.
   804  	ABIAliases []*LSym
   805  
   806  	// Constant symbols (e.g. $i64.*) are data symbols created late
   807  	// in the concurrent phase. To ensure a deterministic order, we
   808  	// add them to a separate list, sort at the end, and append it
   809  	// to Data.
   810  	constSyms []*LSym
   811  
   812  	// pkgIdx maps package path to index. The index is used for
   813  	// symbol reference in the object file.
   814  	pkgIdx map[string]int32
   815  
   816  	defs         []*LSym // list of defined symbols in the current package
   817  	hashed64defs []*LSym // list of defined short (64-bit or less) hashed (content-addressable) symbols
   818  	hasheddefs   []*LSym // list of defined hashed (content-addressable) symbols
   819  	nonpkgdefs   []*LSym // list of defined non-package symbols
   820  	nonpkgrefs   []*LSym // list of referenced non-package symbols
   821  
   822  	Fingerprint goobj.FingerprintType // fingerprint of symbol indices, to catch index mismatch
   823  }
   824  
   825  func (ctxt *Link) Diag(format string, args ...interface{}) {
   826  	ctxt.Errors++
   827  	ctxt.DiagFunc(format, args...)
   828  }
   829  
   830  func (ctxt *Link) Logf(format string, args ...interface{}) {
   831  	fmt.Fprintf(ctxt.Bso, format, args...)
   832  	ctxt.Bso.Flush()
   833  }
   834  
   835  // The smallest possible offset from the hardware stack pointer to a local
   836  // variable on the stack. Architectures that use a link register save its value
   837  // on the stack in the function prologue and so always have a pointer between
   838  // the hardware stack pointer and the local variable area.
   839  func (ctxt *Link) FixedFrameSize() int64 {
   840  	switch ctxt.Arch.Family {
   841  	case sys.AMD64, sys.I386, sys.Wasm:
   842  		return 0
   843  	case sys.PPC64:
   844  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   845  		// just use that much stack always on ppc64x.
   846  		return int64(4 * ctxt.Arch.PtrSize)
   847  	default:
   848  		return int64(ctxt.Arch.PtrSize)
   849  	}
   850  }
   851  
   852  // LinkArch is the definition of a single architecture.
   853  type LinkArch struct {
   854  	*sys.Arch
   855  	Init           func(*Link)
   856  	Preprocess     func(*Link, *LSym, ProgAlloc)
   857  	Assemble       func(*Link, *LSym, ProgAlloc)
   858  	Progedit       func(*Link, *Prog, ProgAlloc)
   859  	UnaryDst       map[As]bool // Instruction takes one operand, a destination.
   860  	DWARFRegisters map[int16]int16
   861  }
   862  

View as plain text