github.com/muesli/go@v0.0.0-20170208044820-e410d2a81ef2/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/src"
    36  	"cmd/internal/sys"
    37  	"fmt"
    38  )
    39  
    40  // An Addr is an argument to an instruction.
    41  // The general forms and their encodings are:
    42  //
    43  //	sym±offset(symkind)(reg)(index*scale)
    44  //		Memory reference at address &sym(symkind) + offset + reg + index*scale.
    45  //		Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
    46  //		If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
    47  //		To force a parsing as index*scale, write (index*1).
    48  //		Encoding:
    49  //			type = TYPE_MEM
    50  //			name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
    51  //			sym = sym
    52  //			offset = ±offset
    53  //			reg = reg (REG_*)
    54  //			index = index (REG_*)
    55  //			scale = scale (1, 2, 4, 8)
    56  //
    57  //	$<mem>
    58  //		Effective address of memory reference <mem>, defined above.
    59  //		Encoding: same as memory reference, but type = TYPE_ADDR.
    60  //
    61  //	$<±integer value>
    62  //		This is a special case of $<mem>, in which only ±offset is present.
    63  //		It has a separate type for easy recognition.
    64  //		Encoding:
    65  //			type = TYPE_CONST
    66  //			offset = ±integer value
    67  //
    68  //	*<mem>
    69  //		Indirect reference through memory reference <mem>, defined above.
    70  //		Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
    71  //		pointer stored in the data word sym(SB), not a function named sym(SB).
    72  //		Encoding: same as above, but type = TYPE_INDIR.
    73  //
    74  //	$*$<mem>
    75  //		No longer used.
    76  //		On machines with actual SB registers, $*$<mem> forced the
    77  //		instruction encoding to use a full 32-bit constant, never a
    78  //		reference relative to SB.
    79  //
    80  //	$<floating point literal>
    81  //		Floating point constant value.
    82  //		Encoding:
    83  //			type = TYPE_FCONST
    84  //			val = floating point value
    85  //
    86  //	$<string literal, up to 8 chars>
    87  //		String literal value (raw bytes used for DATA instruction).
    88  //		Encoding:
    89  //			type = TYPE_SCONST
    90  //			val = string
    91  //
    92  //	<register name>
    93  //		Any register: integer, floating point, control, segment, and so on.
    94  //		If looking for specific register kind, must check type and reg value range.
    95  //		Encoding:
    96  //			type = TYPE_REG
    97  //			reg = reg (REG_*)
    98  //
    99  //	x(PC)
   100  //		Encoding:
   101  //			type = TYPE_BRANCH
   102  //			val = Prog* reference OR ELSE offset = target pc (branch takes priority)
   103  //
   104  //	$±x-±y
   105  //		Final argument to TEXT, specifying local frame size x and argument size y.
   106  //		In this form, x and y are integer literals only, not arbitrary expressions.
   107  //		This avoids parsing ambiguities due to the use of - as a separator.
   108  //		The ± are optional.
   109  //		If the final argument to TEXT omits the -±y, the encoding should still
   110  //		use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
   111  //		Encoding:
   112  //			type = TYPE_TEXTSIZE
   113  //			offset = x
   114  //			val = int32(y)
   115  //
   116  //	reg<<shift, reg>>shift, reg->shift, reg@>shift
   117  //		Shifted register value, for ARM and ARM64.
   118  //		In this form, reg must be a register and shift can be a register or an integer constant.
   119  //		Encoding:
   120  //			type = TYPE_SHIFT
   121  //		On ARM:
   122  //			offset = (reg&15) | shifttype<<5 | count
   123  //			shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
   124  //			count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
   125  //		On ARM64:
   126  //			offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
   127  //			shifttype = 0, 1, 2 for <<, >>, ->
   128  //
   129  //	(reg, reg)
   130  //		A destination register pair. When used as the last argument of an instruction,
   131  //		this form makes clear that both registers are destinations.
   132  //		Encoding:
   133  //			type = TYPE_REGREG
   134  //			reg = first register
   135  //			offset = second register
   136  //
   137  //	[reg, reg, reg-reg]
   138  //		Register list for ARM.
   139  //		Encoding:
   140  //			type = TYPE_REGLIST
   141  //			offset = bit mask of registers in list; R0 is low bit.
   142  //
   143  //	reg, reg
   144  //		Register pair for ARM.
   145  //		TYPE_REGREG2
   146  //
   147  //	(reg+reg)
   148  //		Register pair for PPC64.
   149  //		Encoding:
   150  //			type = TYPE_MEM
   151  //			reg = first register
   152  //			index = second register
   153  //			scale = 1
   154  //
   155  type Addr struct {
   156  	Reg    int16
   157  	Index  int16
   158  	Scale  int16 // Sometimes holds a register.
   159  	Type   AddrType
   160  	Name   int8
   161  	Class  int8
   162  	Offset int64
   163  	Sym    *LSym
   164  
   165  	// argument value:
   166  	//	for TYPE_SCONST, a string
   167  	//	for TYPE_FCONST, a float64
   168  	//	for TYPE_BRANCH, a *Prog (optional)
   169  	//	for TYPE_TEXTSIZE, an int32 (optional)
   170  	Val interface{}
   171  
   172  	Node interface{} // for use by compiler
   173  }
   174  
   175  type AddrType uint8
   176  
   177  const (
   178  	NAME_NONE = 0 + iota
   179  	NAME_EXTERN
   180  	NAME_STATIC
   181  	NAME_AUTO
   182  	NAME_PARAM
   183  	// A reference to name@GOT(SB) is a reference to the entry in the global offset
   184  	// table for 'name'.
   185  	NAME_GOTREF
   186  )
   187  
   188  const (
   189  	TYPE_NONE AddrType = 0
   190  
   191  	TYPE_BRANCH AddrType = 5 + iota
   192  	TYPE_TEXTSIZE
   193  	TYPE_MEM
   194  	TYPE_CONST
   195  	TYPE_FCONST
   196  	TYPE_SCONST
   197  	TYPE_REG
   198  	TYPE_ADDR
   199  	TYPE_SHIFT
   200  	TYPE_REGREG
   201  	TYPE_REGREG2
   202  	TYPE_INDIR
   203  	TYPE_REGLIST
   204  )
   205  
   206  // Prog describes a single machine instruction.
   207  //
   208  // The general instruction form is:
   209  //
   210  //	As.Scond From, Reg, From3, To, RegTo2
   211  //
   212  // where As is an opcode and the others are arguments:
   213  // From, Reg, From3 are sources, and To, RegTo2 are destinations.
   214  // Usually, not all arguments are present.
   215  // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
   216  // The Scond field holds additional condition bits for systems (like arm)
   217  // that have generalized conditional execution.
   218  //
   219  // Jump instructions use the Pcond field to point to the target instruction,
   220  // which must be in the same linked list as the jump instruction.
   221  //
   222  // The Progs for a given function are arranged in a list linked through the Link field.
   223  //
   224  // Each Prog is charged to a specific source line in the debug information,
   225  // specified by Pos.Line().
   226  // Every Prog has a Ctxt field that defines its context.
   227  // Progs should be allocated using ctxt.NewProg(), not new(Prog).
   228  //
   229  // The other fields not yet mentioned are for use by the back ends and should
   230  // be left zeroed by creators of Prog lists.
   231  type Prog struct {
   232  	Ctxt   *Link       // linker context
   233  	Link   *Prog       // next Prog in linked list
   234  	From   Addr        // first source operand
   235  	From3  *Addr       // third source operand (second is Reg below)
   236  	To     Addr        // destination operand (second is RegTo2 below)
   237  	Pcond  *Prog       // target of conditional jump
   238  	Opt    interface{} // available to optimization passes to hold per-Prog state
   239  	Forwd  *Prog       // for x86 back end
   240  	Rel    *Prog       // for x86, arm back ends
   241  	Pc     int64       // for back ends or assembler: virtual or actual program counter, depending on phase
   242  	Pos    src.XPos    // source position of this instruction
   243  	Spadj  int32       // effect of instruction on stack pointer (increment or decrement amount)
   244  	As     As          // assembler opcode
   245  	Reg    int16       // 2nd source operand
   246  	RegTo2 int16       // 2nd destination operand
   247  	Mark   uint16      // bitmask of arch-specific items
   248  	Optab  uint16      // arch-specific opcode index
   249  	Scond  uint8       // condition bits for conditional instruction (e.g., on ARM)
   250  	Back   uint8       // for x86 back end: backwards branch state
   251  	Ft     uint8       // for x86 back end: type index of Prog.From
   252  	Tt     uint8       // for x86 back end: type index of Prog.To
   253  	Isize  uint8       // for x86 back end: size of the instruction in bytes
   254  	Mode   int8        // for x86 back end: 32- or 64-bit mode
   255  }
   256  
   257  // From3Type returns From3.Type, or TYPE_NONE when From3 is nil.
   258  func (p *Prog) From3Type() AddrType {
   259  	if p.From3 == nil {
   260  		return TYPE_NONE
   261  	}
   262  	return p.From3.Type
   263  }
   264  
   265  // From3Offset returns From3.Offset, or 0 when From3 is nil.
   266  func (p *Prog) From3Offset() int64 {
   267  	if p.From3 == nil {
   268  		return 0
   269  	}
   270  	return p.From3.Offset
   271  }
   272  
   273  // An As denotes an assembler opcode.
   274  // There are some portable opcodes, declared here in package obj,
   275  // that are common to all architectures.
   276  // However, the majority of opcodes are arch-specific
   277  // and are declared in their respective architecture's subpackage.
   278  type As int16
   279  
   280  // These are the portable opcodes.
   281  const (
   282  	AXXX As = iota
   283  	ACALL
   284  	ADUFFCOPY
   285  	ADUFFZERO
   286  	AEND
   287  	AFUNCDATA
   288  	AJMP
   289  	ANOP
   290  	APCDATA
   291  	ARET
   292  	ATEXT
   293  	AUNDEF
   294  	AUSEFIELD
   295  	AVARDEF
   296  	AVARKILL
   297  	AVARLIVE
   298  	A_ARCHSPECIFIC
   299  )
   300  
   301  // Each architecture is allotted a distinct subspace of opcode values
   302  // for declaring its arch-specific opcodes.
   303  // Within this subspace, the first arch-specific opcode should be
   304  // at offset A_ARCHSPECIFIC.
   305  //
   306  // Subspaces are aligned to a power of two so opcodes can be masked
   307  // with AMask and used as compact array indices.
   308  const (
   309  	ABase386 = (1 + iota) << 10
   310  	ABaseARM
   311  	ABaseAMD64
   312  	ABasePPC64
   313  	ABaseARM64
   314  	ABaseMIPS
   315  	ABaseS390X
   316  
   317  	AllowedOpCodes = 1 << 10            // The number of opcodes available for any given architecture.
   318  	AMask          = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
   319  )
   320  
   321  // An LSym is the sort of symbol that is written to an object file.
   322  type LSym struct {
   323  	Name    string
   324  	Type    SymKind
   325  	Version int16
   326  	Attribute
   327  
   328  	RefIdx int // Index of this symbol in the symbol reference list.
   329  	Args   int32
   330  	Locals int32
   331  	Size   int64
   332  	Gotype *LSym
   333  	Autom  *Auto
   334  	Text   *Prog
   335  	Pcln   *Pcln
   336  	P      []byte
   337  	R      []Reloc
   338  }
   339  
   340  // Attribute is a set of symbol attributes.
   341  type Attribute int16
   342  
   343  const (
   344  	AttrDuplicateOK Attribute = 1 << iota
   345  	AttrCFunc
   346  	AttrNoSplit
   347  	AttrLeaf
   348  	AttrSeenGlobl
   349  	AttrOnList
   350  
   351  	// MakeTypelink means that the type should have an entry in the typelink table.
   352  	AttrMakeTypelink
   353  
   354  	// ReflectMethod means the function may call reflect.Type.Method or
   355  	// reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
   356  	// can be used through a custom interface), so ReflectMethod may be
   357  	// set in some cases when the reflect package is not called.
   358  	//
   359  	// Used by the linker to determine what methods can be pruned.
   360  	AttrReflectMethod
   361  
   362  	// Local means make the symbol local even when compiling Go code to reference Go
   363  	// symbols in other shared libraries, as in this mode symbols are global by
   364  	// default. "local" here means in the sense of the dynamic linker, i.e. not
   365  	// visible outside of the module (shared library or executable) that contains its
   366  	// definition. (When not compiling to support Go shared libraries, all symbols are
   367  	// local in this sense unless there is a cgo_export_* directive).
   368  	AttrLocal
   369  )
   370  
   371  func (a Attribute) DuplicateOK() bool   { return a&AttrDuplicateOK != 0 }
   372  func (a Attribute) MakeTypelink() bool  { return a&AttrMakeTypelink != 0 }
   373  func (a Attribute) CFunc() bool         { return a&AttrCFunc != 0 }
   374  func (a Attribute) NoSplit() bool       { return a&AttrNoSplit != 0 }
   375  func (a Attribute) Leaf() bool          { return a&AttrLeaf != 0 }
   376  func (a Attribute) SeenGlobl() bool     { return a&AttrSeenGlobl != 0 }
   377  func (a Attribute) OnList() bool        { return a&AttrOnList != 0 }
   378  func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 }
   379  func (a Attribute) Local() bool         { return a&AttrLocal != 0 }
   380  
   381  func (a *Attribute) Set(flag Attribute, value bool) {
   382  	if value {
   383  		*a |= flag
   384  	} else {
   385  		*a &^= flag
   386  	}
   387  }
   388  
   389  // The compiler needs LSym to satisfy fmt.Stringer, because it stores
   390  // an LSym in ssa.ExternSymbol.
   391  func (s *LSym) String() string {
   392  	return s.Name
   393  }
   394  
   395  type Pcln struct {
   396  	Pcsp        Pcdata
   397  	Pcfile      Pcdata
   398  	Pcline      Pcdata
   399  	Pcdata      []Pcdata
   400  	Funcdata    []*LSym
   401  	Funcdataoff []int64
   402  	File        []*LSym
   403  	Lastfile    *LSym
   404  	Lastindex   int
   405  }
   406  
   407  // A SymKind describes the kind of memory represented by a symbol.
   408  type SymKind int16
   409  
   410  // Defined SymKind values.
   411  //
   412  // TODO(rsc): Give idiomatic Go names.
   413  // TODO(rsc): Reduce the number of symbol types in the object files.
   414  //go:generate stringer -type=SymKind
   415  const (
   416  	Sxxx SymKind = iota
   417  	STEXT
   418  	SELFRXSECT
   419  
   420  	// Read-only sections.
   421  	STYPE
   422  	SSTRING
   423  	SGOSTRING
   424  	SGOFUNC
   425  	SGCBITS
   426  	SRODATA
   427  	SFUNCTAB
   428  
   429  	SELFROSECT
   430  	SMACHOPLT
   431  
   432  	// Read-only sections with relocations.
   433  	//
   434  	// Types STYPE-SFUNCTAB above are written to the .rodata section by default.
   435  	// When linking a shared object, some conceptually "read only" types need to
   436  	// be written to by relocations and putting them in a section called
   437  	// ".rodata" interacts poorly with the system linkers. The GNU linkers
   438  	// support this situation by arranging for sections of the name
   439  	// ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after
   440  	// relocations have applied, so when the Go linker is creating a shared
   441  	// object it checks all objects of the above types and bumps any object that
   442  	// has a relocation to it to the corresponding type below, which are then
   443  	// written to sections with appropriate magic names.
   444  	STYPERELRO
   445  	SSTRINGRELRO
   446  	SGOSTRINGRELRO
   447  	SGOFUNCRELRO
   448  	SGCBITSRELRO
   449  	SRODATARELRO
   450  	SFUNCTABRELRO
   451  
   452  	// Part of .data.rel.ro if it exists, otherwise part of .rodata.
   453  	STYPELINK
   454  	SITABLINK
   455  	SSYMTAB
   456  	SPCLNTAB
   457  
   458  	// Writable sections.
   459  	SELFSECT
   460  	SMACHO
   461  	SMACHOGOT
   462  	SWINDOWS
   463  	SELFGOT
   464  	SNOPTRDATA
   465  	SINITARR
   466  	SDATA
   467  	SBSS
   468  	SNOPTRBSS
   469  	STLSBSS
   470  	SXREF
   471  	SMACHOSYMSTR
   472  	SMACHOSYMTAB
   473  	SMACHOINDIRECTPLT
   474  	SMACHOINDIRECTGOT
   475  	SFILE
   476  	SFILEPATH
   477  	SCONST
   478  	SDYNIMPORT
   479  	SHOSTOBJ
   480  	SDWARFSECT
   481  	SDWARFINFO
   482  	SSUB       = SymKind(1 << 8)
   483  	SMASK      = SymKind(SSUB - 1)
   484  	SHIDDEN    = SymKind(1 << 9)
   485  	SCONTAINER = SymKind(1 << 10) // has a sub-symbol
   486  )
   487  
   488  // ReadOnly are the symbol kinds that form read-only sections. In some
   489  // cases, if they will require relocations, they are transformed into
   490  // rel-ro sections using RelROMap.
   491  var ReadOnly = []SymKind{
   492  	STYPE,
   493  	SSTRING,
   494  	SGOSTRING,
   495  	SGOFUNC,
   496  	SGCBITS,
   497  	SRODATA,
   498  	SFUNCTAB,
   499  }
   500  
   501  // RelROMap describes the transformation of read-only symbols to rel-ro
   502  // symbols.
   503  var RelROMap = map[SymKind]SymKind{
   504  	STYPE:     STYPERELRO,
   505  	SSTRING:   SSTRINGRELRO,
   506  	SGOSTRING: SGOSTRINGRELRO,
   507  	SGOFUNC:   SGOFUNCRELRO,
   508  	SGCBITS:   SGCBITSRELRO,
   509  	SRODATA:   SRODATARELRO,
   510  	SFUNCTAB:  SFUNCTABRELRO,
   511  }
   512  
   513  type Reloc struct {
   514  	Off  int32
   515  	Siz  uint8
   516  	Type RelocType
   517  	Add  int64
   518  	Sym  *LSym
   519  }
   520  
   521  type RelocType int32
   522  
   523  //go:generate stringer -type=RelocType
   524  const (
   525  	R_ADDR RelocType = 1 + iota
   526  	// R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit
   527  	// immediates in the low half of the instruction word), usually addis followed by
   528  	// another add or a load, inserting the "high adjusted" 16 bits of the address of
   529  	// the referenced symbol into the immediate field of the first instruction and the
   530  	// low 16 bits into that of the second instruction.
   531  	R_ADDRPOWER
   532  	// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
   533  	// referenced symbol.
   534  	R_ADDRARM64
   535  	// R_ADDRMIPS (only used on mips/mips64) resolves to the low 16 bits of an external
   536  	// address, by encoding it into the instruction.
   537  	R_ADDRMIPS
   538  	// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
   539  	// holding the data being relocated to the referenced symbol.
   540  	R_ADDROFF
   541  	// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
   542  	// A weak relocation does not make the symbol it refers to reachable,
   543  	// and is only honored by the linker if the symbol is in some other way
   544  	// reachable.
   545  	R_WEAKADDROFF
   546  	R_SIZE
   547  	R_CALL
   548  	R_CALLARM
   549  	R_CALLARM64
   550  	R_CALLIND
   551  	R_CALLPOWER
   552  	// R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address
   553  	// of a CALL (JAL) instruction, by encoding the address into the instruction.
   554  	R_CALLMIPS
   555  	R_CONST
   556  	R_PCREL
   557  	// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
   558  	// thread-local symbol from the thread local base and is used to implement the
   559  	// "local exec" model for tls access (r.Sym is not set on intel platforms but is
   560  	// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
   561  	R_TLS_LE
   562  	// R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT
   563  	// slot containing the offset from the thread-local symbol from the thread local
   564  	// base and is used to implemented the "initial exec" model for tls access (r.Sym
   565  	// is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in
   566  	// the linker when externally linking).
   567  	R_TLS_IE
   568  	R_GOTOFF
   569  	R_PLT0
   570  	R_PLT1
   571  	R_PLT2
   572  	R_USEFIELD
   573  	// R_USETYPE resolves to an *rtype, but no relocation is created. The
   574  	// linker uses this as a signal that the pointed-to type information
   575  	// should be linked into the final binary, even if there are no other
   576  	// direct references. (This is used for types reachable by reflection.)
   577  	R_USETYPE
   578  	// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
   579  	// holding the data being relocated to the referenced symbol.
   580  	// It is a variant of R_ADDROFF used when linking from the uncommonType of a
   581  	// *rtype, and may be set to zero by the linker if it determines the method
   582  	// text is unreachable by the linked program.
   583  	R_METHODOFF
   584  	R_POWER_TOC
   585  	R_GOTPCREL
   586  	// R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address
   587  	// of a JMP instruction, by encoding the address into the instruction.
   588  	// The stack nosplit check ignores this since it is not a function call.
   589  	R_JMPMIPS
   590  	// R_DWARFREF resolves to the offset of the symbol from its section.
   591  	R_DWARFREF
   592  
   593  	// Platform dependent relocations. Architectures with fixed width instructions
   594  	// have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be
   595  	// stuffed into a 32-bit instruction, so an address needs to be spread across
   596  	// several instructions, and in turn this requires a sequence of relocations, each
   597  	// updating a part of an instruction. This leads to relocation codes that are
   598  	// inherently processor specific.
   599  
   600  	// Arm64.
   601  
   602  	// Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread
   603  	// local base to the thread local variable defined by the referenced (thread
   604  	// local) symbol. Error if the offset does not fit into 16 bits.
   605  	R_ARM64_TLS_LE
   606  
   607  	// Relocates an ADRP; LD64 instruction sequence to load the offset between
   608  	// the thread local base and the thread local variable defined by the
   609  	// referenced (thread local) symbol from the GOT.
   610  	R_ARM64_TLS_IE
   611  
   612  	// R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT
   613  	// slot of the referenced symbol.
   614  	R_ARM64_GOTPCREL
   615  
   616  	// PPC64.
   617  
   618  	// R_POWER_TLS_LE is used to implement the "local exec" model for tls
   619  	// access. It resolves to the offset of the thread-local symbol from the
   620  	// thread pointer (R13) and inserts this value into the low 16 bits of an
   621  	// instruction word.
   622  	R_POWER_TLS_LE
   623  
   624  	// R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It
   625  	// relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It
   626  	// inserts to the offset of GOT slot for the thread-local symbol from the TOC (the
   627  	// GOT slot is filled by the dynamic linker with the offset of the thread-local
   628  	// symbol from the thread pointer (R13)).
   629  	R_POWER_TLS_IE
   630  
   631  	// R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as
   632  	// accessing a particular thread-local symbol. It does not affect code generation
   633  	// but is used by the system linker when relaxing "initial exec" model code to
   634  	// "local exec" model code.
   635  	R_POWER_TLS
   636  
   637  	// R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second
   638  	// instruction is a "DS-form" instruction, which has an immediate field occupying
   639  	// bits [15:2] of the instruction word. Bits [15:2] of the address of the
   640  	// relocated symbol are inserted into this field; it is an error if the last two
   641  	// bits of the address are not 0.
   642  	R_ADDRPOWER_DS
   643  
   644  	// R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like
   645  	// R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol
   646  	// from the TOC rather than the symbol's address.
   647  	R_ADDRPOWER_GOT
   648  
   649  	// R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but
   650  	// inserts the displacement from the place being relocated to the address of the
   651  	// the relocated symbol instead of just its address.
   652  	R_ADDRPOWER_PCREL
   653  
   654  	// R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but
   655  	// inserts the offset from the TOC to the address of the the relocated symbol
   656  	// rather than the symbol's address.
   657  	R_ADDRPOWER_TOCREL
   658  
   659  	// R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like
   660  	// R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the
   661  	// relocated symbol rather than the symbol's address.
   662  	R_ADDRPOWER_TOCREL_DS
   663  
   664  	// R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses.
   665  	// TODO(mundaym): remove once variants can be serialized - see issue 14218.
   666  	R_PCRELDBL
   667  
   668  	// R_ADDRMIPSU (only used on mips/mips64) resolves to the sign-adjusted "upper" 16
   669  	// bits (bit 16-31) of an external address, by encoding it into the instruction.
   670  	R_ADDRMIPSU
   671  	// R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS
   672  	// address (offset from thread pointer), by encoding it into the instruction.
   673  	R_ADDRMIPSTLS
   674  )
   675  
   676  // IsDirectJump returns whether r is a relocation for a direct jump.
   677  // A direct jump is a CALL or JMP instruction that takes the target address
   678  // as immediate. The address is embedded into the instruction, possibly
   679  // with limited width.
   680  // An indirect jump is a CALL or JMP instruction that takes the target address
   681  // in register or memory.
   682  func (r RelocType) IsDirectJump() bool {
   683  	switch r {
   684  	case R_CALL, R_CALLARM, R_CALLARM64, R_CALLPOWER, R_CALLMIPS, R_JMPMIPS:
   685  		return true
   686  	}
   687  	return false
   688  }
   689  
   690  type Auto struct {
   691  	Asym    *LSym
   692  	Link    *Auto
   693  	Aoffset int32
   694  	Name    int16
   695  	Gotype  *LSym
   696  }
   697  
   698  // Auto.name
   699  const (
   700  	A_AUTO = 1 + iota
   701  	A_PARAM
   702  )
   703  
   704  type Pcdata struct {
   705  	P []byte
   706  }
   707  
   708  // symbol version, incremented each time a file is loaded.
   709  // version==1 is reserved for savehist.
   710  const (
   711  	HistVersion = 1
   712  )
   713  
   714  // Link holds the context for writing object code from a compiler
   715  // to be linker input or for reading that input into the linker.
   716  type Link struct {
   717  	Headtype      HeadType
   718  	Arch          *LinkArch
   719  	Debugasm      int32
   720  	Debugvlog     int32
   721  	Debugdivmod   int32
   722  	Debugpcln     int32
   723  	Flag_shared   bool
   724  	Flag_dynlink  bool
   725  	Flag_optimize bool
   726  	Bso           *bufio.Writer
   727  	Pathname      string
   728  	Hash          map[SymVer]*LSym
   729  	PosTable      src.PosTable
   730  	Imports       []string
   731  	Plists        []*Plist
   732  	Sym_div       *LSym
   733  	Sym_divu      *LSym
   734  	Sym_mod       *LSym
   735  	Sym_modu      *LSym
   736  	Plan9privates *LSym
   737  	Curp          *Prog
   738  	Printp        *Prog
   739  	Blitrl        *Prog
   740  	Elitrl        *Prog
   741  	Rexflag       int
   742  	Vexflag       int
   743  	Rep           int
   744  	Repn          int
   745  	Lock          int
   746  	Asmode        int
   747  	AsmBuf        AsmBuf // instruction buffer for x86
   748  	Instoffset    int64
   749  	Autosize      int32
   750  	Armsize       int32
   751  	Pc            int64
   752  	DiagFunc      func(string, ...interface{})
   753  	Mode          int
   754  	Cursym        *LSym
   755  	Version       int
   756  	Errors        int
   757  
   758  	Framepointer_enabled bool
   759  
   760  	// state for writing objects
   761  	Text []*LSym
   762  	Data []*LSym
   763  
   764  	// Cache of Progs
   765  	allocIdx int
   766  	progs    [10000]Prog
   767  }
   768  
   769  func (ctxt *Link) Diag(format string, args ...interface{}) {
   770  	ctxt.Errors++
   771  	ctxt.DiagFunc(format, args...)
   772  }
   773  
   774  func (ctxt *Link) Logf(format string, args ...interface{}) {
   775  	fmt.Fprintf(ctxt.Bso, format, args...)
   776  	ctxt.Bso.Flush()
   777  }
   778  
   779  // The smallest possible offset from the hardware stack pointer to a local
   780  // variable on the stack. Architectures that use a link register save its value
   781  // on the stack in the function prologue and so always have a pointer between
   782  // the hardware stack pointer and the local variable area.
   783  func (ctxt *Link) FixedFrameSize() int64 {
   784  	switch ctxt.Arch.Family {
   785  	case sys.AMD64, sys.I386:
   786  		return 0
   787  	case sys.PPC64:
   788  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   789  		// just use that much stack always on ppc64x.
   790  		return int64(4 * ctxt.Arch.PtrSize)
   791  	default:
   792  		return int64(ctxt.Arch.PtrSize)
   793  	}
   794  }
   795  
   796  type SymVer struct {
   797  	Name    string
   798  	Version int // TODO: make int16 to match LSym.Version?
   799  }
   800  
   801  // LinkArch is the definition of a single architecture.
   802  type LinkArch struct {
   803  	*sys.Arch
   804  	Preprocess func(*Link, *LSym)
   805  	Assemble   func(*Link, *LSym)
   806  	Progedit   func(*Link, *Prog)
   807  	UnaryDst   map[As]bool // Instruction takes one operand, a destination.
   808  }
   809  
   810  // HeadType is the executable header type.
   811  type HeadType uint8
   812  
   813  const (
   814  	Hunknown HeadType = iota
   815  	Hdarwin
   816  	Hdragonfly
   817  	Hfreebsd
   818  	Hlinux
   819  	Hnacl
   820  	Hnetbsd
   821  	Hopenbsd
   822  	Hplan9
   823  	Hsolaris
   824  	Hwindows
   825  	Hwindowsgui
   826  )
   827  
   828  func (h *HeadType) Set(s string) error {
   829  	switch s {
   830  	case "darwin":
   831  		*h = Hdarwin
   832  	case "dragonfly":
   833  		*h = Hdragonfly
   834  	case "freebsd":
   835  		*h = Hfreebsd
   836  	case "linux", "android":
   837  		*h = Hlinux
   838  	case "nacl":
   839  		*h = Hnacl
   840  	case "netbsd":
   841  		*h = Hnetbsd
   842  	case "openbsd":
   843  		*h = Hopenbsd
   844  	case "plan9":
   845  		*h = Hplan9
   846  	case "solaris":
   847  		*h = Hsolaris
   848  	case "windows":
   849  		*h = Hwindows
   850  	case "windowsgui":
   851  		*h = Hwindowsgui
   852  	default:
   853  		return fmt.Errorf("invalid headtype: %q", s)
   854  	}
   855  	return nil
   856  }
   857  
   858  func (h *HeadType) String() string {
   859  	switch *h {
   860  	case Hdarwin:
   861  		return "darwin"
   862  	case Hdragonfly:
   863  		return "dragonfly"
   864  	case Hfreebsd:
   865  		return "freebsd"
   866  	case Hlinux:
   867  		return "linux"
   868  	case Hnacl:
   869  		return "nacl"
   870  	case Hnetbsd:
   871  		return "netbsd"
   872  	case Hopenbsd:
   873  		return "openbsd"
   874  	case Hplan9:
   875  		return "plan9"
   876  	case Hsolaris:
   877  		return "solaris"
   878  	case Hwindows:
   879  		return "windows"
   880  	case Hwindowsgui:
   881  		return "windowsgui"
   882  	}
   883  	return fmt.Sprintf("HeadType(%d)", *h)
   884  }
   885  
   886  // AsmBuf is a simple buffer to assemble variable-length x86 instructions into.
   887  type AsmBuf struct {
   888  	buf [100]byte
   889  	off int
   890  }
   891  
   892  // Put1 appends one byte to the end of the buffer.
   893  func (a *AsmBuf) Put1(x byte) {
   894  	a.buf[a.off] = x
   895  	a.off++
   896  }
   897  
   898  // Put2 appends two bytes to the end of the buffer.
   899  func (a *AsmBuf) Put2(x, y byte) {
   900  	a.buf[a.off+0] = x
   901  	a.buf[a.off+1] = y
   902  	a.off += 2
   903  }
   904  
   905  // Put3 appends three bytes to the end of the buffer.
   906  func (a *AsmBuf) Put3(x, y, z byte) {
   907  	a.buf[a.off+0] = x
   908  	a.buf[a.off+1] = y
   909  	a.buf[a.off+2] = z
   910  	a.off += 3
   911  }
   912  
   913  // Put4 appends four bytes to the end of the buffer.
   914  func (a *AsmBuf) Put4(x, y, z, w byte) {
   915  	a.buf[a.off+0] = x
   916  	a.buf[a.off+1] = y
   917  	a.buf[a.off+2] = z
   918  	a.buf[a.off+3] = w
   919  	a.off += 4
   920  }
   921  
   922  // PutInt16 writes v into the buffer using little-endian encoding.
   923  func (a *AsmBuf) PutInt16(v int16) {
   924  	a.buf[a.off+0] = byte(v)
   925  	a.buf[a.off+1] = byte(v >> 8)
   926  	a.off += 2
   927  }
   928  
   929  // PutInt32 writes v into the buffer using little-endian encoding.
   930  func (a *AsmBuf) PutInt32(v int32) {
   931  	a.buf[a.off+0] = byte(v)
   932  	a.buf[a.off+1] = byte(v >> 8)
   933  	a.buf[a.off+2] = byte(v >> 16)
   934  	a.buf[a.off+3] = byte(v >> 24)
   935  	a.off += 4
   936  }
   937  
   938  // PutInt64 writes v into the buffer using little-endian encoding.
   939  func (a *AsmBuf) PutInt64(v int64) {
   940  	a.buf[a.off+0] = byte(v)
   941  	a.buf[a.off+1] = byte(v >> 8)
   942  	a.buf[a.off+2] = byte(v >> 16)
   943  	a.buf[a.off+3] = byte(v >> 24)
   944  	a.buf[a.off+4] = byte(v >> 32)
   945  	a.buf[a.off+5] = byte(v >> 40)
   946  	a.buf[a.off+6] = byte(v >> 48)
   947  	a.buf[a.off+7] = byte(v >> 56)
   948  	a.off += 8
   949  }
   950  
   951  // Put copies b into the buffer.
   952  func (a *AsmBuf) Put(b []byte) {
   953  	copy(a.buf[a.off:], b)
   954  	a.off += len(b)
   955  }
   956  
   957  // Insert inserts b at offset i.
   958  func (a *AsmBuf) Insert(i int, b byte) {
   959  	a.off++
   960  	copy(a.buf[i+1:a.off], a.buf[i:a.off-1])
   961  	a.buf[i] = b
   962  }
   963  
   964  // Last returns the byte at the end of the buffer.
   965  func (a *AsmBuf) Last() byte { return a.buf[a.off-1] }
   966  
   967  // Len returns the length of the buffer.
   968  func (a *AsmBuf) Len() int { return a.off }
   969  
   970  // Bytes returns the contents of the buffer.
   971  func (a *AsmBuf) Bytes() []byte { return a.buf[:a.off] }
   972  
   973  // Reset empties the buffer.
   974  func (a *AsmBuf) Reset() { a.off = 0 }
   975  
   976  // Peek returns the byte at offset i.
   977  func (a *AsmBuf) Peek(i int) byte { return a.buf[i] }