github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/internal/obj/link.go (about)

     1  // Derived from Inferno utils/6l/l.h and related files.
     2  // http://code.google.com/p/inferno-os/source/browse/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 "encoding/binary"
    34  
    35  // An Addr is an argument to an instruction.
    36  // The general forms and their encodings are:
    37  //
    38  //	sym±offset(symkind)(reg)(index*scale)
    39  //		Memory reference at address &sym(symkind) + offset + reg + index*scale.
    40  //		Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
    41  //		If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
    42  //		To force a parsing as index*scale, write (index*1).
    43  //		Encoding:
    44  //			type = TYPE_MEM
    45  //			name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
    46  //			sym = sym
    47  //			offset = ±offset
    48  //			reg = reg (REG_*)
    49  //			index = index (REG_*)
    50  //			scale = scale (1, 2, 4, 8)
    51  //
    52  //	$<mem>
    53  //		Effective address of memory reference <mem>, defined above.
    54  //		Encoding: same as memory reference, but type = TYPE_ADDR.
    55  //
    56  //	$<±integer value>
    57  //		This is a special case of $<mem>, in which only ±offset is present.
    58  //		It has a separate type for easy recognition.
    59  //		Encoding:
    60  //			type = TYPE_CONST
    61  //			offset = ±integer value
    62  //
    63  //	*<mem>
    64  //		Indirect reference through memory reference <mem>, defined above.
    65  //		Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
    66  //		pointer stored in the data word sym(SB), not a function named sym(SB).
    67  //		Encoding: same as above, but type = TYPE_INDIR.
    68  //
    69  //	$*$<mem>
    70  //		No longer used.
    71  //		On machines with actual SB registers, $*$<mem> forced the
    72  //		instruction encoding to use a full 32-bit constant, never a
    73  //		reference relative to SB.
    74  //
    75  //	$<floating point literal>
    76  //		Floating point constant value.
    77  //		Encoding:
    78  //			type = TYPE_FCONST
    79  //			val = floating point value
    80  //
    81  //	$<string literal, up to 8 chars>
    82  //		String literal value (raw bytes used for DATA instruction).
    83  //		Encoding:
    84  //			type = TYPE_SCONST
    85  //			val = string
    86  //
    87  //	<register name>
    88  //		Any register: integer, floating point, control, segment, and so on.
    89  //		If looking for specific register kind, must check type and reg value range.
    90  //		Encoding:
    91  //			type = TYPE_REG
    92  //			reg = reg (REG_*)
    93  //
    94  //	x(PC)
    95  //		Encoding:
    96  //			type = TYPE_BRANCH
    97  //			val = Prog* reference OR ELSE offset = target pc (branch takes priority)
    98  //
    99  //	$±x-±y
   100  //		Final argument to TEXT, specifying local frame size x and argument size y.
   101  //		In this form, x and y are integer literals only, not arbitrary expressions.
   102  //		This avoids parsing ambiguities due to the use of - as a separator.
   103  //		The ± are optional.
   104  //		If the final argument to TEXT omits the -±y, the encoding should still
   105  //		use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
   106  //		Encoding:
   107  //			type = TYPE_TEXTSIZE
   108  //			offset = x
   109  //			val = int32(y)
   110  //
   111  //	reg<<shift, reg>>shift, reg->shift, reg@>shift
   112  //		Shifted register value, for ARM.
   113  //		In this form, reg must be a register and shift can be a register or an integer constant.
   114  //		Encoding:
   115  //			type = TYPE_SHIFT
   116  //			offset = (reg&15) | shifttype<<5 | count
   117  //			shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
   118  //			count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
   119  //
   120  //	(reg, reg)
   121  //		A destination register pair. When used as the last argument of an instruction,
   122  //		this form makes clear that both registers are destinations.
   123  //		Encoding:
   124  //			type = TYPE_REGREG
   125  //			reg = first register
   126  //			offset = second register
   127  //
   128  //	[reg, reg, reg-reg]
   129  //		Register list for ARM.
   130  //		Encoding:
   131  //			type = TYPE_REGLIST
   132  //			offset = bit mask of registers in list; R0 is low bit.
   133  //
   134  //	reg, reg
   135  //		Register pair for ARM.
   136  //		TYPE_REGREG2
   137  //
   138  //	(reg+reg)
   139  //		Register pair for PPC64.
   140  //		Encoding:
   141  //			type = TYPE_MEM
   142  //			reg = first register
   143  //			index = second register
   144  //			scale = 1
   145  //
   146  type Addr struct {
   147  	Type   int16
   148  	Reg    int16
   149  	Index  int16
   150  	Scale  int16 // Sometimes holds a register.
   151  	Name   int8
   152  	Class  int8
   153  	Etype  uint8
   154  	Offset int64
   155  	Width  int64
   156  	Sym    *LSym
   157  	Gotype *LSym
   158  
   159  	// argument value:
   160  	//	for TYPE_SCONST, a string
   161  	//	for TYPE_FCONST, a float64
   162  	//	for TYPE_BRANCH, a *Prog (optional)
   163  	//	for TYPE_TEXTSIZE, an int32 (optional)
   164  	Val interface{}
   165  
   166  	Node interface{} // for use by compiler
   167  }
   168  
   169  const (
   170  	NAME_NONE = 0 + iota
   171  	NAME_EXTERN
   172  	NAME_STATIC
   173  	NAME_AUTO
   174  	NAME_PARAM
   175  	// A reference to name@GOT(SB) is a reference to the entry in the global offset
   176  	// table for 'name'.
   177  	NAME_GOTREF
   178  )
   179  
   180  const (
   181  	TYPE_NONE = 0
   182  )
   183  
   184  const (
   185  	TYPE_BRANCH = 5 + iota
   186  	TYPE_TEXTSIZE
   187  	TYPE_MEM
   188  	TYPE_CONST
   189  	TYPE_FCONST
   190  	TYPE_SCONST
   191  	TYPE_REG
   192  	TYPE_ADDR
   193  	TYPE_SHIFT
   194  	TYPE_REGREG
   195  	TYPE_REGREG2
   196  	TYPE_INDIR
   197  	TYPE_REGLIST
   198  )
   199  
   200  // TODO(rsc): Describe prog.
   201  // TODO(rsc): Describe TEXT/GLOBL flag in from3, DATA width in from3.
   202  type Prog struct {
   203  	Ctxt   *Link
   204  	Link   *Prog
   205  	From   Addr
   206  	From3  *Addr // optional
   207  	To     Addr
   208  	Opt    interface{}
   209  	Forwd  *Prog
   210  	Pcond  *Prog
   211  	Rel    *Prog // Source of forward jumps on x86; pcrel on arm
   212  	Pc     int64
   213  	Lineno int32
   214  	Spadj  int32
   215  	As     int16
   216  	Reg    int16
   217  	RegTo2 int16 // 2nd register output operand
   218  	Mark   uint16
   219  	Optab  uint16
   220  	Scond  uint8
   221  	Back   uint8
   222  	Ft     uint8
   223  	Tt     uint8
   224  	Isize  uint8
   225  	Mode   int8
   226  
   227  	Info ProgInfo
   228  }
   229  
   230  // From3Type returns From3.Type, or TYPE_NONE when From3 is nil.
   231  func (p *Prog) From3Type() int16 {
   232  	if p.From3 == nil {
   233  		return TYPE_NONE
   234  	}
   235  	return p.From3.Type
   236  }
   237  
   238  // From3Offset returns From3.Offset, or 0 when From3 is nil.
   239  func (p *Prog) From3Offset() int64 {
   240  	if p.From3 == nil {
   241  		return 0
   242  	}
   243  	return p.From3.Offset
   244  }
   245  
   246  // ProgInfo holds information about the instruction for use
   247  // by clients such as the compiler. The exact meaning of this
   248  // data is up to the client and is not interpreted by the cmd/internal/obj/... packages.
   249  type ProgInfo struct {
   250  	_        struct{} // to prevent unkeyed literals. Trailing zero-sized field will take space.
   251  	Flags    uint32   // flag bits
   252  	Reguse   uint64   // registers implicitly used by this instruction
   253  	Regset   uint64   // registers implicitly set by this instruction
   254  	Regindex uint64   // registers used by addressing mode
   255  }
   256  
   257  // Prog.as opcodes.
   258  // These are the portable opcodes, common to all architectures.
   259  // Each architecture defines many more arch-specific opcodes,
   260  // with values starting at A_ARCHSPECIFIC.
   261  // Each architecture adds an offset to this so each machine has
   262  // distinct space for its instructions. The offset is a power of
   263  // two so it can be masked to return to origin zero.
   264  // See the definitions of ABase386 etc.
   265  const (
   266  	AXXX = 0 + iota
   267  	ACALL
   268  	ACHECKNIL
   269  	ADATA
   270  	ADUFFCOPY
   271  	ADUFFZERO
   272  	AEND
   273  	AFUNCDATA
   274  	AGLOBL
   275  	AJMP
   276  	ANOP
   277  	APCDATA
   278  	ARET
   279  	ATEXT
   280  	ATYPE
   281  	AUNDEF
   282  	AUSEFIELD
   283  	AVARDEF
   284  	AVARKILL
   285  	A_ARCHSPECIFIC
   286  )
   287  
   288  // An LSym is the sort of symbol that is written to an object file.
   289  type LSym struct {
   290  	Name      string
   291  	Type      int16
   292  	Version   int16
   293  	Dupok     uint8
   294  	Cfunc     uint8
   295  	Nosplit   uint8
   296  	Leaf      uint8
   297  	Seenglobl uint8
   298  	Onlist    uint8
   299  	// Local means make the symbol local even when compiling Go code to reference Go
   300  	// symbols in other shared libraries, as in this mode symbols are global by
   301  	// default. "local" here means in the sense of the dynamic linker, i.e. not
   302  	// visible outside of the module (shared library or executable) that contains its
   303  	// definition. (When not compiling to support Go shared libraries, all symbols are
   304  	// local in this sense unless there is a cgo_export_* directive).
   305  	Local  bool
   306  	Args   int32
   307  	Locals int32
   308  	Value  int64
   309  	Size   int64
   310  	Next   *LSym
   311  	Gotype *LSym
   312  	Autom  *Auto
   313  	Text   *Prog
   314  	Etext  *Prog
   315  	Pcln   *Pcln
   316  	P      []byte
   317  	R      []Reloc
   318  }
   319  
   320  type Pcln struct {
   321  	Pcsp        Pcdata
   322  	Pcfile      Pcdata
   323  	Pcline      Pcdata
   324  	Pcdata      []Pcdata
   325  	Funcdata    []*LSym
   326  	Funcdataoff []int64
   327  	File        []*LSym
   328  	Lastfile    *LSym
   329  	Lastindex   int
   330  }
   331  
   332  // LSym.type
   333  const (
   334  	Sxxx = iota
   335  	STEXT
   336  	SELFRXSECT
   337  
   338  	STYPE
   339  	SSTRING
   340  	SGOSTRING
   341  	SGOFUNC
   342  	SGCBITS
   343  	SRODATA
   344  	SFUNCTAB
   345  
   346  	// Types STYPE-SFUNCTAB above are written to the .rodata section by default.
   347  	// When linking a shared object, some conceptually "read only" types need to
   348  	// be written to by relocations and putting them in a section called
   349  	// ".rodata" interacts poorly with the system linkers. The GNU linkers
   350  	// support this situation by arranging for sections of the name
   351  	// ".data.rel.ro.XXX" to be mprotected read only by the dynamic linker after
   352  	// relocations have applied, so when the Go linker is creating a shared
   353  	// object it checks all objects of the above types and bumps any object that
   354  	// has a relocation to it to the corresponding type below, which are then
   355  	// written to sections with appropriate magic names.
   356  	STYPERELRO
   357  	SSTRINGRELRO
   358  	SGOSTRINGRELRO
   359  	SGOFUNCRELRO
   360  	SGCBITSRELRO
   361  	SRODATARELRO
   362  	SFUNCTABRELRO
   363  
   364  	STYPELINK
   365  	SSYMTAB
   366  	SPCLNTAB
   367  	SELFROSECT
   368  	SMACHOPLT
   369  	SELFSECT
   370  	SMACHO
   371  	SMACHOGOT
   372  	SWINDOWS
   373  	SELFGOT
   374  	SNOPTRDATA
   375  	SINITARR
   376  	SDATA
   377  	SBSS
   378  	SNOPTRBSS
   379  	STLSBSS
   380  	SXREF
   381  	SMACHOSYMSTR
   382  	SMACHOSYMTAB
   383  	SMACHOINDIRECTPLT
   384  	SMACHOINDIRECTGOT
   385  	SFILE
   386  	SFILEPATH
   387  	SCONST
   388  	SDYNIMPORT
   389  	SHOSTOBJ
   390  	SSUB       = 1 << 8
   391  	SMASK      = SSUB - 1
   392  	SHIDDEN    = 1 << 9
   393  	SCONTAINER = 1 << 10 // has a sub-symbol
   394  )
   395  
   396  type Reloc struct {
   397  	Off  int32
   398  	Siz  uint8
   399  	Type int32
   400  	Add  int64
   401  	Sym  *LSym
   402  }
   403  
   404  // Reloc.type
   405  const (
   406  	R_ADDR = 1 + iota
   407  	R_ADDRPOWER
   408  	R_ADDRARM64
   409  	R_SIZE
   410  	R_CALL
   411  	R_CALLARM
   412  	R_CALLARM64
   413  	R_CALLIND
   414  	R_CALLPOWER
   415  	R_CONST
   416  	R_PCREL
   417  	// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
   418  	// thread-local symbol from the thread local base and is used to implement the
   419  	// "local exec" model for tls access (r.Sym is not set on intel platforms but is
   420  	// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
   421  	R_TLS_LE
   422  	// R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT
   423  	// slot containing the offset from the thread-local symbol from the thread local
   424  	// base and is used to implemented the "initial exec" model for tls access (r.Sym
   425  	// is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in
   426  	// the linker when externally linking).
   427  	R_TLS_IE
   428  	R_GOTOFF
   429  	R_PLT0
   430  	R_PLT1
   431  	R_PLT2
   432  	R_USEFIELD
   433  	R_POWER_TOC
   434  	R_GOTPCREL
   435  
   436  	// Platform dependent relocations. Architectures with fixed width instructions
   437  	// have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be
   438  	// stuffed into a 32-bit instruction, so an address needs to be spread across
   439  	// several instructions, and in turn this requires a sequence of relocations, each
   440  	// updating a part of an instruction.  This leads to relocation codes that are
   441  	// inherently processor specific.
   442  
   443  	// Arm64.
   444  
   445  	// Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread
   446  	// local base to the thread local variable defined by the referenced (thread
   447  	// local) symbol. Error if the offset does not fit into 16 bits.
   448  	R_ARM64_TLS_LE
   449  
   450  	// Relocates an ADRP; LD64 instruction sequence to load the offset between
   451  	// the thread local base and the thread local variable defined by the
   452  	// referenced (thread local) symbol from the GOT.
   453  	R_ARM64_TLS_IE
   454  
   455  	// PPC64.
   456  
   457  	// R_POWER_TLS_LE is used to implement the "local exec" model for tls
   458  	// access. It resolves to the offset of the thread-local symbol from the
   459  	// thread pointer (R13) and inserts this value into the low 16 bits of an
   460  	// instruction word.
   461  	R_POWER_TLS_LE
   462  )
   463  
   464  type Auto struct {
   465  	Asym    *LSym
   466  	Link    *Auto
   467  	Aoffset int32
   468  	Name    int16
   469  	Gotype  *LSym
   470  }
   471  
   472  // Auto.name
   473  const (
   474  	A_AUTO = 1 + iota
   475  	A_PARAM
   476  )
   477  
   478  type Pcdata struct {
   479  	P []byte
   480  }
   481  
   482  // Pcdata iterator.
   483  //      for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) }
   484  type Pciter struct {
   485  	d       Pcdata
   486  	p       []byte
   487  	pc      uint32
   488  	nextpc  uint32
   489  	pcscale uint32
   490  	value   int32
   491  	start   int
   492  	done    int
   493  }
   494  
   495  // symbol version, incremented each time a file is loaded.
   496  // version==1 is reserved for savehist.
   497  const (
   498  	HistVersion = 1
   499  )
   500  
   501  // Link holds the context for writing object code from a compiler
   502  // to be linker input or for reading that input into the linker.
   503  type Link struct {
   504  	Goarm              int32
   505  	Headtype           int
   506  	Arch               *LinkArch
   507  	Debugasm           int32
   508  	Debugvlog          int32
   509  	Debugdivmod        int32
   510  	Debugpcln          int32
   511  	Flag_shared        int32
   512  	Flag_dynlink       bool
   513  	Bso                *Biobuf
   514  	Pathname           string
   515  	Windows            int32
   516  	Goroot             string
   517  	Goroot_final       string
   518  	Enforce_data_order int32
   519  	Hash               map[SymVer]*LSym
   520  	LineHist           LineHist
   521  	Imports            []string
   522  	Plist              *Plist
   523  	Plast              *Plist
   524  	Sym_div            *LSym
   525  	Sym_divu           *LSym
   526  	Sym_mod            *LSym
   527  	Sym_modu           *LSym
   528  	Plan9privates      *LSym
   529  	Curp               *Prog
   530  	Printp             *Prog
   531  	Blitrl             *Prog
   532  	Elitrl             *Prog
   533  	Rexflag            int
   534  	Vexflag            int
   535  	Rep                int
   536  	Repn               int
   537  	Lock               int
   538  	Asmode             int
   539  	Andptr             []byte
   540  	And                [100]uint8
   541  	Instoffset         int64
   542  	Autosize           int32
   543  	Armsize            int32
   544  	Pc                 int64
   545  	Diag               func(string, ...interface{})
   546  	Mode               int
   547  	Cursym             *LSym
   548  	Version            int
   549  	Textp              *LSym
   550  	Etextp             *LSym
   551  }
   552  
   553  // The smallest possible offset from the hardware stack pointer to a local
   554  // variable on the stack. Architectures that use a link register save its value
   555  // on the stack in the function prologue and so always have a pointer between
   556  // the hardware stack pointer and the local variable area.
   557  func (ctxt *Link) FixedFrameSize() int64 {
   558  	switch ctxt.Arch.Thechar {
   559  	case '6', '8':
   560  		return 0
   561  	default:
   562  		return int64(ctxt.Arch.Ptrsize)
   563  	}
   564  }
   565  
   566  type SymVer struct {
   567  	Name    string
   568  	Version int // TODO: make int16 to match LSym.Version?
   569  }
   570  
   571  // LinkArch is the definition of a single architecture.
   572  type LinkArch struct {
   573  	ByteOrder  binary.ByteOrder
   574  	Name       string
   575  	Thechar    int
   576  	Preprocess func(*Link, *LSym)
   577  	Assemble   func(*Link, *LSym)
   578  	Follow     func(*Link, *LSym)
   579  	Progedit   func(*Link, *Prog)
   580  	UnaryDst   map[int]bool // Instruction takes one operand, a destination.
   581  	Minlc      int
   582  	Ptrsize    int
   583  	Regsize    int
   584  }
   585  
   586  /* executable header types */
   587  const (
   588  	Hunknown = 0 + iota
   589  	Hdarwin
   590  	Hdragonfly
   591  	Helf
   592  	Hfreebsd
   593  	Hlinux
   594  	Hnacl
   595  	Hnetbsd
   596  	Hopenbsd
   597  	Hplan9
   598  	Hsolaris
   599  	Hwindows
   600  )
   601  
   602  type Plist struct {
   603  	Name    *LSym
   604  	Firstpc *Prog
   605  	Recur   int
   606  	Link    *Plist
   607  }
   608  
   609  /*
   610   * start a new Prog list.
   611   */
   612  func Linknewplist(ctxt *Link) *Plist {
   613  	pl := new(Plist)
   614  	if ctxt.Plist == nil {
   615  		ctxt.Plist = pl
   616  	} else {
   617  		ctxt.Plast.Link = pl
   618  	}
   619  	ctxt.Plast = pl
   620  	return pl
   621  }