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

View as plain text