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