github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/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   AddrName
   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 AddrName int8
   176  
   177  const (
   178  	NAME_NONE AddrName = 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  type AddrType uint8
   189  
   190  const (
   191  	TYPE_NONE AddrType = iota
   192  	TYPE_BRANCH
   193  	TYPE_TEXTSIZE
   194  	TYPE_MEM
   195  	TYPE_CONST
   196  	TYPE_FCONST
   197  	TYPE_SCONST
   198  	TYPE_REG
   199  	TYPE_ADDR
   200  	TYPE_SHIFT
   201  	TYPE_REGREG
   202  	TYPE_REGREG2
   203  	TYPE_INDIR
   204  	TYPE_REGLIST
   205  )
   206  
   207  // Prog describes a single machine instruction.
   208  //
   209  // The general instruction form is:
   210  //
   211  //	As.Scond From, Reg, From3, To, RegTo2
   212  //
   213  // where As is an opcode and the others are arguments:
   214  // From, Reg, From3 are sources, and To, RegTo2 are destinations.
   215  // Usually, not all arguments are present.
   216  // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
   217  // The Scond field holds additional condition bits for systems (like arm)
   218  // that have generalized conditional execution.
   219  //
   220  // Jump instructions use the Pcond field to point to the target instruction,
   221  // which must be in the same linked list as the jump instruction.
   222  //
   223  // The Progs for a given function are arranged in a list linked through the Link field.
   224  //
   225  // Each Prog is charged to a specific source line in the debug information,
   226  // specified by Pos.Line().
   227  // Every Prog has a Ctxt field that defines its context.
   228  // Progs should be allocated using ctxt.NewProg(), not new(Prog).
   229  //
   230  // The other fields not yet mentioned are for use by the back ends and should
   231  // be left zeroed by creators of Prog lists.
   232  type Prog struct {
   233  	Ctxt   *Link       // linker context
   234  	Link   *Prog       // next Prog in linked list
   235  	From   Addr        // first source operand
   236  	From3  *Addr       // third source operand (second is Reg below)
   237  	To     Addr        // destination operand (second is RegTo2 below)
   238  	Pcond  *Prog       // target of conditional jump
   239  	Opt    interface{} // available to optimization passes to hold per-Prog state
   240  	Forwd  *Prog       // for x86 back end
   241  	Rel    *Prog       // for x86, arm back ends
   242  	Pc     int64       // for back ends or assembler: virtual or actual program counter, depending on phase
   243  	Pos    src.XPos    // source position of this instruction
   244  	Spadj  int32       // effect of instruction on stack pointer (increment or decrement amount)
   245  	As     As          // assembler opcode
   246  	Reg    int16       // 2nd source operand
   247  	RegTo2 int16       // 2nd destination operand
   248  	Mark   uint16      // bitmask of arch-specific items
   249  	Optab  uint16      // arch-specific opcode index
   250  	Scond  uint8       // condition bits for conditional instruction (e.g., on ARM)
   251  	Back   uint8       // for x86 back end: backwards branch state
   252  	Ft     uint8       // for x86 back end: type index of Prog.From
   253  	Tt     uint8       // for x86 back end: type index of Prog.To
   254  	Isize  uint8       // for x86 back end: size of the instruction in bytes
   255  	Mode   int8        // for x86 back end: 32- or 64-bit mode
   256  }
   257  
   258  // From3Type returns From3.Type, or TYPE_NONE when From3 is nil.
   259  func (p *Prog) From3Type() AddrType {
   260  	if p.From3 == nil {
   261  		return TYPE_NONE
   262  	}
   263  	return p.From3.Type
   264  }
   265  
   266  // From3Offset returns From3.Offset, or 0 when From3 is nil.
   267  func (p *Prog) From3Offset() int64 {
   268  	if p.From3 == nil {
   269  		return 0
   270  	}
   271  	return p.From3.Offset
   272  }
   273  
   274  // An As denotes an assembler opcode.
   275  // There are some portable opcodes, declared here in package obj,
   276  // that are common to all architectures.
   277  // However, the majority of opcodes are arch-specific
   278  // and are declared in their respective architecture's subpackage.
   279  type As int16
   280  
   281  // These are the portable opcodes.
   282  const (
   283  	AXXX As = iota
   284  	ACALL
   285  	ADUFFCOPY
   286  	ADUFFZERO
   287  	AEND
   288  	AFUNCDATA
   289  	AJMP
   290  	ANOP
   291  	APCDATA
   292  	ARET
   293  	ATEXT
   294  	AUNDEF
   295  	AUSEFIELD
   296  	AVARDEF
   297  	AVARKILL
   298  	AVARLIVE
   299  	A_ARCHSPECIFIC
   300  )
   301  
   302  // Each architecture is allotted a distinct subspace of opcode values
   303  // for declaring its arch-specific opcodes.
   304  // Within this subspace, the first arch-specific opcode should be
   305  // at offset A_ARCHSPECIFIC.
   306  //
   307  // Subspaces are aligned to a power of two so opcodes can be masked
   308  // with AMask and used as compact array indices.
   309  const (
   310  	ABase386 = (1 + iota) << 10
   311  	ABaseARM
   312  	ABaseAMD64
   313  	ABasePPC64
   314  	ABaseARM64
   315  	ABaseMIPS
   316  	ABaseS390X
   317  
   318  	AllowedOpCodes = 1 << 10            // The number of opcodes available for any given architecture.
   319  	AMask          = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
   320  )
   321  
   322  // An LSym is the sort of symbol that is written to an object file.
   323  type LSym struct {
   324  	Name    string
   325  	Type    SymKind
   326  	Version int16
   327  	Attribute
   328  
   329  	RefIdx int // Index of this symbol in the symbol reference list.
   330  	Args   int32
   331  	Locals int32
   332  	Size   int64
   333  	Gotype *LSym
   334  	Autom  *Auto
   335  	Text   *Prog
   336  	Pcln   *Pcln
   337  	P      []byte
   338  	R      []Reloc
   339  }
   340  
   341  // Attribute is a set of symbol attributes.
   342  type Attribute int16
   343  
   344  const (
   345  	AttrDuplicateOK Attribute = 1 << iota
   346  	AttrCFunc
   347  	AttrNoSplit
   348  	AttrLeaf
   349  	AttrSeenGlobl
   350  	AttrOnList
   351  
   352  	// MakeTypelink means that the type should have an entry in the typelink table.
   353  	AttrMakeTypelink
   354  
   355  	// ReflectMethod means the function may call reflect.Type.Method or
   356  	// reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
   357  	// can be used through a custom interface), so ReflectMethod may be
   358  	// set in some cases when the reflect package is not called.
   359  	//
   360  	// Used by the linker to determine what methods can be pruned.
   361  	AttrReflectMethod
   362  
   363  	// Local means make the symbol local even when compiling Go code to reference Go
   364  	// symbols in other shared libraries, as in this mode symbols are global by
   365  	// default. "local" here means in the sense of the dynamic linker, i.e. not
   366  	// visible outside of the module (shared library or executable) that contains its
   367  	// definition. (When not compiling to support Go shared libraries, all symbols are
   368  	// local in this sense unless there is a cgo_export_* directive).
   369  	AttrLocal
   370  )
   371  
   372  func (a Attribute) DuplicateOK() bool   { return a&AttrDuplicateOK != 0 }
   373  func (a Attribute) MakeTypelink() bool  { return a&AttrMakeTypelink != 0 }
   374  func (a Attribute) CFunc() bool         { return a&AttrCFunc != 0 }
   375  func (a Attribute) NoSplit() bool       { return a&AttrNoSplit != 0 }
   376  func (a Attribute) Leaf() bool          { return a&AttrLeaf != 0 }
   377  func (a Attribute) SeenGlobl() bool     { return a&AttrSeenGlobl != 0 }
   378  func (a Attribute) OnList() bool        { return a&AttrOnList != 0 }
   379  func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 }
   380  func (a Attribute) Local() bool         { return a&AttrLocal != 0 }
   381  
   382  func (a *Attribute) Set(flag Attribute, value bool) {
   383  	if value {
   384  		*a |= flag
   385  	} else {
   386  		*a &^= flag
   387  	}
   388  }
   389  
   390  // The compiler needs LSym to satisfy fmt.Stringer, because it stores
   391  // an LSym in ssa.ExternSymbol.
   392  func (s *LSym) String() string {
   393  	return s.Name
   394  }
   395  
   396  type Pcln struct {
   397  	Pcsp        Pcdata
   398  	Pcfile      Pcdata
   399  	Pcline      Pcdata
   400  	Pcdata      []Pcdata
   401  	Funcdata    []*LSym
   402  	Funcdataoff []int64
   403  	File        []*LSym
   404  	Lastfile    *LSym
   405  	Lastindex   int
   406  }
   407  
   408  // A SymKind describes the kind of memory represented by a symbol.
   409  type SymKind int16
   410  
   411  // Defined SymKind values.
   412  //
   413  // TODO(rsc): Give idiomatic Go names.
   414  // TODO(rsc): Reduce the number of symbol types in the object files.
   415  //go:generate stringer -type=SymKind
   416  const (
   417  	Sxxx SymKind = iota
   418  	STEXT
   419  	SELFRXSECT
   420  
   421  	// Read-only sections.
   422  	STYPE
   423  	SSTRING
   424  	SGOSTRING
   425  	SGOFUNC
   426  	SGCBITS
   427  	SRODATA
   428  	SFUNCTAB
   429  
   430  	SELFROSECT
   431  	SMACHOPLT
   432  
   433  	// Read-only sections with relocations.
   434  	//
   435  	// Types STYPE-SFUNCTAB above are written to the .rodata section by default.
   436  	// When linking a shared object, some conceptually "read only" types need to
   437  	// be written to by relocations and putting them in a section called
   438  	// ".rodata" interacts poorly with the system linkers. The GNU linkers
   439  	// support this situation by arranging for sections of the name
   440  	// ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after
   441  	// relocations have applied, so when the Go linker is creating a shared
   442  	// object it checks all objects of the above types and bumps any object that
   443  	// has a relocation to it to the corresponding type below, which are then
   444  	// written to sections with appropriate magic names.
   445  	STYPERELRO
   446  	SSTRINGRELRO
   447  	SGOSTRINGRELRO
   448  	SGOFUNCRELRO
   449  	SGCBITSRELRO
   450  	SRODATARELRO
   451  	SFUNCTABRELRO
   452  
   453  	// Part of .data.rel.ro if it exists, otherwise part of .rodata.
   454  	STYPELINK
   455  	SITABLINK
   456  	SSYMTAB
   457  	SPCLNTAB
   458  
   459  	// Writable sections.
   460  	SELFSECT
   461  	SMACHO
   462  	SMACHOGOT
   463  	SWINDOWS
   464  	SELFGOT
   465  	SNOPTRDATA
   466  	SINITARR
   467  	SDATA
   468  	SBSS
   469  	SNOPTRBSS
   470  	STLSBSS
   471  	SXREF
   472  	SMACHOSYMSTR
   473  	SMACHOSYMTAB
   474  	SMACHOINDIRECTPLT
   475  	SMACHOINDIRECTGOT
   476  	SFILE
   477  	SFILEPATH
   478  	SCONST
   479  	SDYNIMPORT
   480  	SHOSTOBJ
   481  	SDWARFSECT
   482  	SDWARFINFO
   483  	SSUB       = SymKind(1 << 8)
   484  	SMASK      = SymKind(SSUB - 1)
   485  	SHIDDEN    = SymKind(1 << 9)
   486  	SCONTAINER = SymKind(1 << 10) // has a sub-symbol
   487  )
   488  
   489  // ReadOnly are the symbol kinds that form read-only sections. In some
   490  // cases, if they will require relocations, they are transformed into
   491  // rel-ro sections using RelROMap.
   492  var ReadOnly = []SymKind{
   493  	STYPE,
   494  	SSTRING,
   495  	SGOSTRING,
   496  	SGOFUNC,
   497  	SGCBITS,
   498  	SRODATA,
   499  	SFUNCTAB,
   500  }
   501  
   502  // RelROMap describes the transformation of read-only symbols to rel-ro
   503  // symbols.
   504  var RelROMap = map[SymKind]SymKind{
   505  	STYPE:     STYPERELRO,
   506  	SSTRING:   SSTRINGRELRO,
   507  	SGOSTRING: SGOSTRINGRELRO,
   508  	SGOFUNC:   SGOFUNCRELRO,
   509  	SGCBITS:   SGCBITSRELRO,
   510  	SRODATA:   SRODATARELRO,
   511  	SFUNCTAB:  SFUNCTABRELRO,
   512  }
   513  
   514  type Reloc struct {
   515  	Off  int32
   516  	Siz  uint8
   517  	Type RelocType
   518  	Add  int64
   519  	Sym  *LSym
   520  }
   521  
   522  type RelocType int32
   523  
   524  //go:generate stringer -type=RelocType
   525  const (
   526  	R_ADDR RelocType = 1 + iota
   527  	// R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit
   528  	// immediates in the low half of the instruction word), usually addis followed by
   529  	// another add or a load, inserting the "high adjusted" 16 bits of the address of
   530  	// the referenced symbol into the immediate field of the first instruction and the
   531  	// low 16 bits into that of the second instruction.
   532  	R_ADDRPOWER
   533  	// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
   534  	// referenced symbol.
   535  	R_ADDRARM64
   536  	// R_ADDRMIPS (only used on mips/mips64) resolves to the low 16 bits of an external
   537  	// address, by encoding it into the instruction.
   538  	R_ADDRMIPS
   539  	// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
   540  	// holding the data being relocated to the referenced symbol.
   541  	R_ADDROFF
   542  	// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
   543  	// A weak relocation does not make the symbol it refers to reachable,
   544  	// and is only honored by the linker if the symbol is in some other way
   545  	// reachable.
   546  	R_WEAKADDROFF
   547  	R_SIZE
   548  	R_CALL
   549  	R_CALLARM
   550  	R_CALLARM64
   551  	R_CALLIND
   552  	R_CALLPOWER
   553  	// R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address
   554  	// of a CALL (JAL) instruction, by encoding the address into the instruction.
   555  	R_CALLMIPS
   556  	R_CONST
   557  	R_PCREL
   558  	// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
   559  	// thread-local symbol from the thread local base and is used to implement the
   560  	// "local exec" model for tls access (r.Sym is not set on intel platforms but is
   561  	// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
   562  	R_TLS_LE
   563  	// R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT
   564  	// slot containing the offset from the thread-local symbol from the thread local
   565  	// base and is used to implemented the "initial exec" model for tls access (r.Sym
   566  	// is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in
   567  	// the linker when externally linking).
   568  	R_TLS_IE
   569  	R_GOTOFF
   570  	R_PLT0
   571  	R_PLT1
   572  	R_PLT2
   573  	R_USEFIELD
   574  	// R_USETYPE resolves to an *rtype, but no relocation is created. The
   575  	// linker uses this as a signal that the pointed-to type information
   576  	// should be linked into the final binary, even if there are no other
   577  	// direct references. (This is used for types reachable by reflection.)
   578  	R_USETYPE
   579  	// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
   580  	// holding the data being relocated to the referenced symbol.
   581  	// It is a variant of R_ADDROFF used when linking from the uncommonType of a
   582  	// *rtype, and may be set to zero by the linker if it determines the method
   583  	// text is unreachable by the linked program.
   584  	R_METHODOFF
   585  	R_POWER_TOC
   586  	R_GOTPCREL
   587  	// R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address
   588  	// of a JMP instruction, by encoding the address into the instruction.
   589  	// The stack nosplit check ignores this since it is not a function call.
   590  	R_JMPMIPS
   591  	// R_DWARFREF resolves to the offset of the symbol from its section.
   592  	R_DWARFREF
   593  
   594  	// Platform dependent relocations. Architectures with fixed width instructions
   595  	// have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be
   596  	// stuffed into a 32-bit instruction, so an address needs to be spread across
   597  	// several instructions, and in turn this requires a sequence of relocations, each
   598  	// updating a part of an instruction. This leads to relocation codes that are
   599  	// inherently processor specific.
   600  
   601  	// Arm64.
   602  
   603  	// Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread
   604  	// local base to the thread local variable defined by the referenced (thread
   605  	// local) symbol. Error if the offset does not fit into 16 bits.
   606  	R_ARM64_TLS_LE
   607  
   608  	// Relocates an ADRP; LD64 instruction sequence to load the offset between
   609  	// the thread local base and the thread local variable defined by the
   610  	// referenced (thread local) symbol from the GOT.
   611  	R_ARM64_TLS_IE
   612  
   613  	// R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT
   614  	// slot of the referenced symbol.
   615  	R_ARM64_GOTPCREL
   616  
   617  	// PPC64.
   618  
   619  	// R_POWER_TLS_LE is used to implement the "local exec" model for tls
   620  	// access. It resolves to the offset of the thread-local symbol from the
   621  	// thread pointer (R13) and inserts this value into the low 16 bits of an
   622  	// instruction word.
   623  	R_POWER_TLS_LE
   624  
   625  	// R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It
   626  	// relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It
   627  	// inserts to the offset of GOT slot for the thread-local symbol from the TOC (the
   628  	// GOT slot is filled by the dynamic linker with the offset of the thread-local
   629  	// symbol from the thread pointer (R13)).
   630  	R_POWER_TLS_IE
   631  
   632  	// R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as
   633  	// accessing a particular thread-local symbol. It does not affect code generation
   634  	// but is used by the system linker when relaxing "initial exec" model code to
   635  	// "local exec" model code.
   636  	R_POWER_TLS
   637  
   638  	// R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second
   639  	// instruction is a "DS-form" instruction, which has an immediate field occupying
   640  	// bits [15:2] of the instruction word. Bits [15:2] of the address of the
   641  	// relocated symbol are inserted into this field; it is an error if the last two
   642  	// bits of the address are not 0.
   643  	R_ADDRPOWER_DS
   644  
   645  	// R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like
   646  	// R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol
   647  	// from the TOC rather than the symbol's address.
   648  	R_ADDRPOWER_GOT
   649  
   650  	// R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but
   651  	// inserts the displacement from the place being relocated to the address of the
   652  	// the relocated symbol instead of just its address.
   653  	R_ADDRPOWER_PCREL
   654  
   655  	// R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but
   656  	// inserts the offset from the TOC to the address of the the relocated symbol
   657  	// rather than the symbol's address.
   658  	R_ADDRPOWER_TOCREL
   659  
   660  	// R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like
   661  	// R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the
   662  	// relocated symbol rather than the symbol's address.
   663  	R_ADDRPOWER_TOCREL_DS
   664  
   665  	// R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses.
   666  	// TODO(mundaym): remove once variants can be serialized - see issue 14218.
   667  	R_PCRELDBL
   668  
   669  	// R_ADDRMIPSU (only used on mips/mips64) resolves to the sign-adjusted "upper" 16
   670  	// bits (bit 16-31) of an external address, by encoding it into the instruction.
   671  	R_ADDRMIPSU
   672  	// R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS
   673  	// address (offset from thread pointer), by encoding it into the instruction.
   674  	R_ADDRMIPSTLS
   675  )
   676  
   677  // IsDirectJump returns whether r is a relocation for a direct jump.
   678  // A direct jump is a CALL or JMP instruction that takes the target address
   679  // as immediate. The address is embedded into the instruction, possibly
   680  // with limited width.
   681  // An indirect jump is a CALL or JMP instruction that takes the target address
   682  // in register or memory.
   683  func (r RelocType) IsDirectJump() bool {
   684  	switch r {
   685  	case R_CALL, R_CALLARM, R_CALLARM64, R_CALLPOWER, R_CALLMIPS, R_JMPMIPS:
   686  		return true
   687  	}
   688  	return false
   689  }
   690  
   691  type Auto struct {
   692  	Asym    *LSym
   693  	Link    *Auto
   694  	Aoffset int32
   695  	Name    AddrName
   696  	Gotype  *LSym
   697  }
   698  
   699  // Auto.name
   700  const (
   701  	A_AUTO = 1 + iota
   702  	A_PARAM
   703  )
   704  
   705  type Pcdata struct {
   706  	P []byte
   707  }
   708  
   709  // symbol version, incremented each time a file is loaded.
   710  // version==1 is reserved for savehist.
   711  const (
   712  	HistVersion = 1
   713  )
   714  
   715  // Link holds the context for writing object code from a compiler
   716  // to be linker input or for reading that input into the linker.
   717  type Link struct {
   718  	Headtype      HeadType
   719  	Arch          *LinkArch
   720  	Debugasm      int32
   721  	Debugvlog     int32
   722  	Debugdivmod   int32
   723  	Debugpcln     int32
   724  	Flag_shared   bool
   725  	Flag_dynlink  bool
   726  	Flag_optimize bool
   727  	Bso           *bufio.Writer
   728  	Pathname      string
   729  	Hash          map[SymVer]*LSym
   730  	PosTable      src.PosTable
   731  	Imports       []string
   732  	Plists        []*Plist
   733  	Sym_div       *LSym
   734  	Sym_divu      *LSym
   735  	Sym_mod       *LSym
   736  	Sym_modu      *LSym
   737  	Plan9privates *LSym
   738  	Curp          *Prog
   739  	Printp        *Prog
   740  	Blitrl        *Prog
   741  	Elitrl        *Prog
   742  	Rexflag       int
   743  	Vexflag       int
   744  	Rep           int
   745  	Repn          int
   746  	Lock          int
   747  	Asmode        int
   748  	AsmBuf        AsmBuf // instruction buffer for x86
   749  	Instoffset    int64
   750  	Autosize      int32
   751  	Armsize       int32
   752  	Pc            int64
   753  	DiagFunc      func(string, ...interface{})
   754  	Mode          int
   755  	Cursym        *LSym
   756  	Version       int
   757  	Errors        int
   758  
   759  	Framepointer_enabled bool
   760  
   761  	// state for writing objects
   762  	Text []*LSym
   763  	Data []*LSym
   764  
   765  	// Cache of Progs
   766  	allocIdx int
   767  	progs    [10000]Prog
   768  }
   769  
   770  func (ctxt *Link) Diag(format string, args ...interface{}) {
   771  	ctxt.Errors++
   772  	ctxt.DiagFunc(format, args...)
   773  }
   774  
   775  func (ctxt *Link) Logf(format string, args ...interface{}) {
   776  	fmt.Fprintf(ctxt.Bso, format, args...)
   777  	ctxt.Bso.Flush()
   778  }
   779  
   780  // The smallest possible offset from the hardware stack pointer to a local
   781  // variable on the stack. Architectures that use a link register save its value
   782  // on the stack in the function prologue and so always have a pointer between
   783  // the hardware stack pointer and the local variable area.
   784  func (ctxt *Link) FixedFrameSize() int64 {
   785  	switch ctxt.Arch.Family {
   786  	case sys.AMD64, sys.I386:
   787  		return 0
   788  	case sys.PPC64:
   789  		// PIC code on ppc64le requires 32 bytes of stack, and it's easier to
   790  		// just use that much stack always on ppc64x.
   791  		return int64(4 * ctxt.Arch.PtrSize)
   792  	default:
   793  		return int64(ctxt.Arch.PtrSize)
   794  	}
   795  }
   796  
   797  type SymVer struct {
   798  	Name    string
   799  	Version int // TODO: make int16 to match LSym.Version?
   800  }
   801  
   802  // LinkArch is the definition of a single architecture.
   803  type LinkArch struct {
   804  	*sys.Arch
   805  	Preprocess func(*Link, *LSym)
   806  	Assemble   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] }