github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/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/sys"
    36  	"fmt"
    37  )
    38  
    39  // An Addr is an argument to an instruction.
    40  // The general forms and their encodings are:
    41  //
    42  //	sym±offset(symkind)(reg)(index*scale)
    43  //		Memory reference at address &sym(symkind) + offset + reg + index*scale.
    44  //		Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
    45  //		If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
    46  //		To force a parsing as index*scale, write (index*1).
    47  //		Encoding:
    48  //			type = TYPE_MEM
    49  //			name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
    50  //			sym = sym
    51  //			offset = ±offset
    52  //			reg = reg (REG_*)
    53  //			index = index (REG_*)
    54  //			scale = scale (1, 2, 4, 8)
    55  //
    56  //	$<mem>
    57  //		Effective address of memory reference <mem>, defined above.
    58  //		Encoding: same as memory reference, but type = TYPE_ADDR.
    59  //
    60  //	$<±integer value>
    61  //		This is a special case of $<mem>, in which only ±offset is present.
    62  //		It has a separate type for easy recognition.
    63  //		Encoding:
    64  //			type = TYPE_CONST
    65  //			offset = ±integer value
    66  //
    67  //	*<mem>
    68  //		Indirect reference through memory reference <mem>, defined above.
    69  //		Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
    70  //		pointer stored in the data word sym(SB), not a function named sym(SB).
    71  //		Encoding: same as above, but type = TYPE_INDIR.
    72  //
    73  //	$*$<mem>
    74  //		No longer used.
    75  //		On machines with actual SB registers, $*$<mem> forced the
    76  //		instruction encoding to use a full 32-bit constant, never a
    77  //		reference relative to SB.
    78  //
    79  //	$<floating point literal>
    80  //		Floating point constant value.
    81  //		Encoding:
    82  //			type = TYPE_FCONST
    83  //			val = floating point value
    84  //
    85  //	$<string literal, up to 8 chars>
    86  //		String literal value (raw bytes used for DATA instruction).
    87  //		Encoding:
    88  //			type = TYPE_SCONST
    89  //			val = string
    90  //
    91  //	<register name>
    92  //		Any register: integer, floating point, control, segment, and so on.
    93  //		If looking for specific register kind, must check type and reg value range.
    94  //		Encoding:
    95  //			type = TYPE_REG
    96  //			reg = reg (REG_*)
    97  //
    98  //	x(PC)
    99  //		Encoding:
   100  //			type = TYPE_BRANCH
   101  //			val = Prog* reference OR ELSE offset = target pc (branch takes priority)
   102  //
   103  //	$±x-±y
   104  //		Final argument to TEXT, specifying local frame size x and argument size y.
   105  //		In this form, x and y are integer literals only, not arbitrary expressions.
   106  //		This avoids parsing ambiguities due to the use of - as a separator.
   107  //		The ± are optional.
   108  //		If the final argument to TEXT omits the -±y, the encoding should still
   109  //		use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
   110  //		Encoding:
   111  //			type = TYPE_TEXTSIZE
   112  //			offset = x
   113  //			val = int32(y)
   114  //
   115  //	reg<<shift, reg>>shift, reg->shift, reg@>shift
   116  //		Shifted register value, for ARM and ARM64.
   117  //		In this form, reg must be a register and shift can be a register or an integer constant.
   118  //		Encoding:
   119  //			type = TYPE_SHIFT
   120  //		On ARM:
   121  //			offset = (reg&15) | shifttype<<5 | count
   122  //			shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
   123  //			count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
   124  //		On ARM64:
   125  //			offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
   126  //			shifttype = 0, 1, 2 for <<, >>, ->
   127  //
   128  //	(reg, reg)
   129  //		A destination register pair. When used as the last argument of an instruction,
   130  //		this form makes clear that both registers are destinations.
   131  //		Encoding:
   132  //			type = TYPE_REGREG
   133  //			reg = first register
   134  //			offset = second register
   135  //
   136  //	[reg, reg, reg-reg]
   137  //		Register list for ARM.
   138  //		Encoding:
   139  //			type = TYPE_REGLIST
   140  //			offset = bit mask of registers in list; R0 is low bit.
   141  //
   142  //	reg, reg
   143  //		Register pair for ARM.
   144  //		TYPE_REGREG2
   145  //
   146  //	(reg+reg)
   147  //		Register pair for PPC64.
   148  //		Encoding:
   149  //			type = TYPE_MEM
   150  //			reg = first register
   151  //			index = second register
   152  //			scale = 1
   153  //
   154  type Addr struct {
   155  	Reg    int16
   156  	Index  int16
   157  	Scale  int16 // Sometimes holds a register.
   158  	Type   AddrType
   159  	Name   int8
   160  	Class  int8
   161  	Offset int64
   162  	Sym    *LSym
   163  
   164  	// argument value:
   165  	//	for TYPE_SCONST, a string
   166  	//	for TYPE_FCONST, a float64
   167  	//	for TYPE_BRANCH, a *Prog (optional)
   168  	//	for TYPE_TEXTSIZE, an int32 (optional)
   169  	Val interface{}
   170  
   171  	Node interface{} // for use by compiler
   172  }
   173  
   174  type AddrType uint8
   175  
   176  const (
   177  	NAME_NONE = 0 + iota
   178  	NAME_EXTERN
   179  	NAME_STATIC
   180  	NAME_AUTO
   181  	NAME_PARAM
   182  	// A reference to name@GOT(SB) is a reference to the entry in the global offset
   183  	// table for 'name'.
   184  	NAME_GOTREF
   185  )
   186  
   187  const (
   188  	TYPE_NONE AddrType = 0
   189  
   190  	TYPE_BRANCH AddrType = 5 + iota
   191  	TYPE_TEXTSIZE
   192  	TYPE_MEM
   193  	TYPE_CONST
   194  	TYPE_FCONST
   195  	TYPE_SCONST
   196  	TYPE_REG
   197  	TYPE_ADDR
   198  	TYPE_SHIFT
   199  	TYPE_REGREG
   200  	TYPE_REGREG2
   201  	TYPE_INDIR
   202  	TYPE_REGLIST
   203  )
   204  
   205  // Prog describes a single machine instruction.
   206  //
   207  // The general instruction form is:
   208  //
   209  //	As.Scond From, Reg, From3, To, RegTo2
   210  //
   211  // where As is an opcode and the others are arguments:
   212  // From, Reg, From3 are sources, and To, RegTo2 are destinations.
   213  // Usually, not all arguments are present.
   214  // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
   215  // The Scond field holds additional condition bits for systems (like arm)
   216  // that have generalized conditional execution.
   217  //
   218  // Jump instructions use the Pcond field to point to the target instruction,
   219  // which must be in the same linked list as the jump instruction.
   220  //
   221  // The Progs for a given function are arranged in a list linked through the Link field.
   222  //
   223  // Each Prog is charged to a specific source line in the debug information,
   224  // specified by Lineno, an index into the line history (see LineHist).
   225  // Every Prog has a Ctxt field that defines various context, including the current LineHist.
   226  // Progs should be allocated using ctxt.NewProg(), not new(Prog).
   227  //
   228  // The other fields not yet mentioned are for use by the back ends and should
   229  // be left zeroed by creators of Prog lists.
   230  type Prog struct {
   231  	Ctxt   *Link       // linker context
   232  	Link   *Prog       // next Prog in linked list
   233  	From   Addr        // first source operand
   234  	From3  *Addr       // third source operand (second is Reg below)
   235  	To     Addr        // destination operand (second is RegTo2 below)
   236  	Pcond  *Prog       // target of conditional jump
   237  	Opt    interface{} // available to optimization passes to hold per-Prog state
   238  	Forwd  *Prog       // for x86 back end
   239  	Rel    *Prog       // for x86, arm back ends
   240  	Pc     int64       // for back ends or assembler: virtual or actual program counter, depending on phase
   241  	Lineno int32       // line number of this instruction
   242  	Spadj  int32       // effect of instruction on stack pointer (increment or decrement amount)
   243  	As     As          // assembler opcode
   244  	Reg    int16       // 2nd source operand
   245  	RegTo2 int16       // 2nd destination operand
   246  	Mark   uint16      // bitmask of arch-specific items
   247  	Optab  uint16      // arch-specific opcode index
   248  	Scond  uint8       // condition bits for conditional instruction (e.g., on ARM)
   249  	Back   uint8       // for x86 back end: backwards branch state
   250  	Ft     uint8       // for x86 back end: type index of Prog.From
   251  	Tt     uint8       // for x86 back end: type index of Prog.To
   252  	Isize  uint8       // for x86 back end: size of the instruction in bytes
   253  	Mode   int8        // for x86 back end: 32- or 64-bit mode
   254  }
   255  
   256  // From3Type returns From3.Type, or TYPE_NONE when From3 is nil.
   257  func (p *Prog) From3Type() AddrType {
   258  	if p.From3 == nil {
   259  		return TYPE_NONE
   260  	}
   261  	return p.From3.Type
   262  }
   263  
   264  // From3Offset returns From3.Offset, or 0 when From3 is nil.
   265  func (p *Prog) From3Offset() int64 {
   266  	if p.From3 == nil {
   267  		return 0
   268  	}
   269  	return p.From3.Offset
   270  }
   271  
   272  // An As denotes an assembler opcode.
   273  // There are some portable opcodes, declared here in package obj,
   274  // that are common to all architectures.
   275  // However, the majority of opcodes are arch-specific
   276  // and are declared in their respective architecture's subpackage.
   277  type As int16
   278  
   279  // These are the portable opcodes.
   280  const (
   281  	AXXX As = iota
   282  	ACALL
   283  	ADUFFCOPY
   284  	ADUFFZERO
   285  	AEND
   286  	AFUNCDATA
   287  	AJMP
   288  	ANOP
   289  	APCDATA
   290  	ARET
   291  	ATEXT
   292  	ATYPE
   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  	LineHist      LineHist
   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  	Follow     func(*Link, *LSym)
   807  	Progedit   func(*Link, *Prog)
   808  	UnaryDst   map[As]bool // Instruction takes one operand, a destination.
   809  }
   810  
   811  // HeadType is the executable header type.
   812  type HeadType uint8
   813  
   814  const (
   815  	Hunknown HeadType = iota
   816  	Hdarwin
   817  	Hdragonfly
   818  	Hfreebsd
   819  	Hlinux
   820  	Hnacl
   821  	Hnetbsd
   822  	Hopenbsd
   823  	Hplan9
   824  	Hsolaris
   825  	Hwindows
   826  	Hwindowsgui
   827  )
   828  
   829  func (h *HeadType) Set(s string) error {
   830  	switch s {
   831  	case "darwin":
   832  		*h = Hdarwin
   833  	case "dragonfly":
   834  		*h = Hdragonfly
   835  	case "freebsd":
   836  		*h = Hfreebsd
   837  	case "linux", "android":
   838  		*h = Hlinux
   839  	case "nacl":
   840  		*h = Hnacl
   841  	case "netbsd":
   842  		*h = Hnetbsd
   843  	case "openbsd":
   844  		*h = Hopenbsd
   845  	case "plan9":
   846  		*h = Hplan9
   847  	case "solaris":
   848  		*h = Hsolaris
   849  	case "windows":
   850  		*h = Hwindows
   851  	case "windowsgui":
   852  		*h = Hwindowsgui
   853  	default:
   854  		return fmt.Errorf("invalid headtype: %q", s)
   855  	}
   856  	return nil
   857  }
   858  
   859  func (h *HeadType) String() string {
   860  	switch *h {
   861  	case Hdarwin:
   862  		return "darwin"
   863  	case Hdragonfly:
   864  		return "dragonfly"
   865  	case Hfreebsd:
   866  		return "freebsd"
   867  	case Hlinux:
   868  		return "linux"
   869  	case Hnacl:
   870  		return "nacl"
   871  	case Hnetbsd:
   872  		return "netbsd"
   873  	case Hopenbsd:
   874  		return "openbsd"
   875  	case Hplan9:
   876  		return "plan9"
   877  	case Hsolaris:
   878  		return "solaris"
   879  	case Hwindows:
   880  		return "windows"
   881  	case Hwindowsgui:
   882  		return "windowsgui"
   883  	}
   884  	return fmt.Sprintf("HeadType(%d)", *h)
   885  }
   886  
   887  // AsmBuf is a simple buffer to assemble variable-length x86 instructions into.
   888  type AsmBuf struct {
   889  	buf [100]byte
   890  	off int
   891  }
   892  
   893  // Put1 appends one byte to the end of the buffer.
   894  func (a *AsmBuf) Put1(x byte) {
   895  	a.buf[a.off] = x
   896  	a.off++
   897  }
   898  
   899  // Put2 appends two bytes to the end of the buffer.
   900  func (a *AsmBuf) Put2(x, y byte) {
   901  	a.buf[a.off+0] = x
   902  	a.buf[a.off+1] = y
   903  	a.off += 2
   904  }
   905  
   906  // Put3 appends three bytes to the end of the buffer.
   907  func (a *AsmBuf) Put3(x, y, z byte) {
   908  	a.buf[a.off+0] = x
   909  	a.buf[a.off+1] = y
   910  	a.buf[a.off+2] = z
   911  	a.off += 3
   912  }
   913  
   914  // Put4 appends four bytes to the end of the buffer.
   915  func (a *AsmBuf) Put4(x, y, z, w byte) {
   916  	a.buf[a.off+0] = x
   917  	a.buf[a.off+1] = y
   918  	a.buf[a.off+2] = z
   919  	a.buf[a.off+3] = w
   920  	a.off += 4
   921  }
   922  
   923  // PutInt16 writes v into the buffer using little-endian encoding.
   924  func (a *AsmBuf) PutInt16(v int16) {
   925  	a.buf[a.off+0] = byte(v)
   926  	a.buf[a.off+1] = byte(v >> 8)
   927  	a.off += 2
   928  }
   929  
   930  // PutInt32 writes v into the buffer using little-endian encoding.
   931  func (a *AsmBuf) PutInt32(v int32) {
   932  	a.buf[a.off+0] = byte(v)
   933  	a.buf[a.off+1] = byte(v >> 8)
   934  	a.buf[a.off+2] = byte(v >> 16)
   935  	a.buf[a.off+3] = byte(v >> 24)
   936  	a.off += 4
   937  }
   938  
   939  // PutInt64 writes v into the buffer using little-endian encoding.
   940  func (a *AsmBuf) PutInt64(v int64) {
   941  	a.buf[a.off+0] = byte(v)
   942  	a.buf[a.off+1] = byte(v >> 8)
   943  	a.buf[a.off+2] = byte(v >> 16)
   944  	a.buf[a.off+3] = byte(v >> 24)
   945  	a.buf[a.off+4] = byte(v >> 32)
   946  	a.buf[a.off+5] = byte(v >> 40)
   947  	a.buf[a.off+6] = byte(v >> 48)
   948  	a.buf[a.off+7] = byte(v >> 56)
   949  	a.off += 8
   950  }
   951  
   952  // Put copies b into the buffer.
   953  func (a *AsmBuf) Put(b []byte) {
   954  	copy(a.buf[a.off:], b)
   955  	a.off += len(b)
   956  }
   957  
   958  // Insert inserts b at offset i.
   959  func (a *AsmBuf) Insert(i int, b byte) {
   960  	a.off++
   961  	copy(a.buf[i+1:a.off], a.buf[i:a.off-1])
   962  	a.buf[i] = b
   963  }
   964  
   965  // Last returns the byte at the end of the buffer.
   966  func (a *AsmBuf) Last() byte { return a.buf[a.off-1] }
   967  
   968  // Len returns the length of the buffer.
   969  func (a *AsmBuf) Len() int { return a.off }
   970  
   971  // Bytes returns the contents of the buffer.
   972  func (a *AsmBuf) Bytes() []byte { return a.buf[:a.off] }
   973  
   974  // Reset empties the buffer.
   975  func (a *AsmBuf) Reset() { a.off = 0 }
   976  
   977  // Peek returns the byte at offset i.
   978  func (a *AsmBuf) Peek(i int) byte { return a.buf[i] }