github.com/jonasi/go@v0.0.0-20150930005915-e78e654c1de0/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  	Flags    uint32   // flag bits
   251  	Reguse   uint64   // registers implicitly used by this instruction
   252  	Regset   uint64   // registers implicitly set by this instruction
   253  	Regindex uint64   // registers used by addressing mode
   254  	_        struct{} // to prevent unkeyed literals
   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 (only used on arm currently, and not on android and darwin where tlsg is
   418  	// a regular variable) resolves to data needed to access the thread-local g. It is
   419  	// interpreted differently depending on toolchain flags to implement either the
   420  	// "local exec" or "inital exec" model for tls access.
   421  	// TODO(mwhudson): change to use R_TLS_LE or R_TLS_IE as appropriate, not having
   422  	// R_TLS do double duty.
   423  	R_TLS
   424  	// R_TLS_LE (only used on 386 and amd64 currently) resolves to the offset of the
   425  	// thread-local g from the thread local base and is used to implement the "local
   426  	// exec" model for tls access (r.Sym is not set by the compiler for this case but
   427  	// is set to Tlsg in the linker when externally linking).
   428  	R_TLS_LE
   429  	// R_TLS_IE (only used on 386 and amd64 currently) resolves to the PC-relative
   430  	// offset to a GOT slot containing the offset the thread-local g from the thread
   431  	// local base and is used to implemented the "initial exec" model for tls access
   432  	// (r.Sym is not set by the compiler for this case but is set to Tlsg in the
   433  	// linker when externally linking).
   434  	R_TLS_IE
   435  	R_GOTOFF
   436  	R_PLT0
   437  	R_PLT1
   438  	R_PLT2
   439  	R_USEFIELD
   440  	R_POWER_TOC
   441  	R_GOTPCREL
   442  )
   443  
   444  type Auto struct {
   445  	Asym    *LSym
   446  	Link    *Auto
   447  	Aoffset int32
   448  	Name    int16
   449  	Gotype  *LSym
   450  }
   451  
   452  // Auto.name
   453  const (
   454  	A_AUTO = 1 + iota
   455  	A_PARAM
   456  )
   457  
   458  type Pcdata struct {
   459  	P []byte
   460  }
   461  
   462  // Pcdata iterator.
   463  //      for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) }
   464  type Pciter struct {
   465  	d       Pcdata
   466  	p       []byte
   467  	pc      uint32
   468  	nextpc  uint32
   469  	pcscale uint32
   470  	value   int32
   471  	start   int
   472  	done    int
   473  }
   474  
   475  // symbol version, incremented each time a file is loaded.
   476  // version==1 is reserved for savehist.
   477  const (
   478  	HistVersion = 1
   479  )
   480  
   481  // Link holds the context for writing object code from a compiler
   482  // to be linker input or for reading that input into the linker.
   483  type Link struct {
   484  	Goarm              int32
   485  	Headtype           int
   486  	Arch               *LinkArch
   487  	Debugasm           int32
   488  	Debugvlog          int32
   489  	Debugdivmod        int32
   490  	Debugpcln          int32
   491  	Flag_shared        int32
   492  	Flag_dynlink       bool
   493  	Bso                *Biobuf
   494  	Pathname           string
   495  	Windows            int32
   496  	Goroot             string
   497  	Goroot_final       string
   498  	Enforce_data_order int32
   499  	Hash               map[SymVer]*LSym
   500  	LineHist           LineHist
   501  	Imports            []string
   502  	Plist              *Plist
   503  	Plast              *Plist
   504  	Sym_div            *LSym
   505  	Sym_divu           *LSym
   506  	Sym_mod            *LSym
   507  	Sym_modu           *LSym
   508  	Tlsg               *LSym
   509  	Plan9privates      *LSym
   510  	Curp               *Prog
   511  	Printp             *Prog
   512  	Blitrl             *Prog
   513  	Elitrl             *Prog
   514  	Rexflag            int
   515  	Rep                int
   516  	Repn               int
   517  	Lock               int
   518  	Asmode             int
   519  	Andptr             []byte
   520  	And                [100]uint8
   521  	Instoffset         int64
   522  	Autosize           int32
   523  	Armsize            int32
   524  	Pc                 int64
   525  	Diag               func(string, ...interface{})
   526  	Mode               int
   527  	Cursym             *LSym
   528  	Version            int
   529  	Textp              *LSym
   530  	Etextp             *LSym
   531  }
   532  
   533  type SymVer struct {
   534  	Name    string
   535  	Version int // TODO: make int16 to match LSym.Version?
   536  }
   537  
   538  // LinkArch is the definition of a single architecture.
   539  type LinkArch struct {
   540  	ByteOrder  binary.ByteOrder
   541  	Name       string
   542  	Thechar    int
   543  	Preprocess func(*Link, *LSym)
   544  	Assemble   func(*Link, *LSym)
   545  	Follow     func(*Link, *LSym)
   546  	Progedit   func(*Link, *Prog)
   547  	UnaryDst   map[int]bool // Instruction takes one operand, a destination.
   548  	Minlc      int
   549  	Ptrsize    int
   550  	Regsize    int
   551  }
   552  
   553  /* executable header types */
   554  const (
   555  	Hunknown = 0 + iota
   556  	Hdarwin
   557  	Hdragonfly
   558  	Helf
   559  	Hfreebsd
   560  	Hlinux
   561  	Hnacl
   562  	Hnetbsd
   563  	Hopenbsd
   564  	Hplan9
   565  	Hsolaris
   566  	Hwindows
   567  )
   568  
   569  type Plist struct {
   570  	Name    *LSym
   571  	Firstpc *Prog
   572  	Recur   int
   573  	Link    *Plist
   574  }
   575  
   576  /*
   577   * start a new Prog list.
   578   */
   579  func Linknewplist(ctxt *Link) *Plist {
   580  	pl := new(Plist)
   581  	if ctxt.Plist == nil {
   582  		ctxt.Plist = pl
   583  	} else {
   584  		ctxt.Plast.Link = pl
   585  	}
   586  	ctxt.Plast = pl
   587  	return pl
   588  }