github.com/alash3al/go@v0.0.0-20150827002835-d497eeb00540/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  	STYPE
   338  	SSTRING
   339  	SGOSTRING
   340  	SGOFUNC
   341  	SGCBITS
   342  	SRODATA
   343  	SFUNCTAB
   344  	STYPELINK
   345  	SSYMTAB
   346  	SPCLNTAB
   347  	SELFROSECT
   348  	SMACHOPLT
   349  	SELFSECT
   350  	SMACHO
   351  	SMACHOGOT
   352  	SWINDOWS
   353  	SELFGOT
   354  	SNOPTRDATA
   355  	SINITARR
   356  	SDATA
   357  	SBSS
   358  	SNOPTRBSS
   359  	STLSBSS
   360  	SXREF
   361  	SMACHOSYMSTR
   362  	SMACHOSYMTAB
   363  	SMACHOINDIRECTPLT
   364  	SMACHOINDIRECTGOT
   365  	SFILE
   366  	SFILEPATH
   367  	SCONST
   368  	SDYNIMPORT
   369  	SHOSTOBJ
   370  	SSUB       = 1 << 8
   371  	SMASK      = SSUB - 1
   372  	SHIDDEN    = 1 << 9
   373  	SCONTAINER = 1 << 10 // has a sub-symbol
   374  )
   375  
   376  type Reloc struct {
   377  	Off  int32
   378  	Siz  uint8
   379  	Type int32
   380  	Add  int64
   381  	Sym  *LSym
   382  }
   383  
   384  // Reloc.type
   385  const (
   386  	R_ADDR = 1 + iota
   387  	R_ADDRPOWER
   388  	R_ADDRARM64
   389  	R_SIZE
   390  	R_CALL
   391  	R_CALLARM
   392  	R_CALLARM64
   393  	R_CALLIND
   394  	R_CALLPOWER
   395  	R_CONST
   396  	R_PCREL
   397  	// R_TLS (only used on arm currently, and not on android and darwin where tlsg is
   398  	// a regular variable) resolves to data needed to access the thread-local g. It is
   399  	// interpreted differently depending on toolchain flags to implement either the
   400  	// "local exec" or "inital exec" model for tls access.
   401  	// TODO(mwhudson): change to use R_TLS_LE or R_TLS_IE as appropriate, not having
   402  	// R_TLS do double duty.
   403  	R_TLS
   404  	// R_TLS_LE (only used on 386 and amd64 currently) resolves to the offset of the
   405  	// thread-local g from the thread local base and is used to implement the "local
   406  	// exec" model for tls access (r.Sym is not set by the compiler for this case but
   407  	// is set to Tlsg in the linker when externally linking).
   408  	R_TLS_LE
   409  	// R_TLS_IE (only used on 386 and amd64 currently) resolves to the PC-relative
   410  	// offset to a GOT slot containing the offset the thread-local g from the thread
   411  	// local base and is used to implemented the "initial exec" model for tls access
   412  	// (r.Sym is not set by the compiler for this case but is set to Tlsg in the
   413  	// linker when externally linking).
   414  	R_TLS_IE
   415  	R_GOTOFF
   416  	R_PLT0
   417  	R_PLT1
   418  	R_PLT2
   419  	R_USEFIELD
   420  	R_POWER_TOC
   421  	R_GOTPCREL
   422  )
   423  
   424  type Auto struct {
   425  	Asym    *LSym
   426  	Link    *Auto
   427  	Aoffset int32
   428  	Name    int16
   429  	Gotype  *LSym
   430  }
   431  
   432  // Auto.name
   433  const (
   434  	A_AUTO = 1 + iota
   435  	A_PARAM
   436  )
   437  
   438  type Pcdata struct {
   439  	P []byte
   440  }
   441  
   442  // Pcdata iterator.
   443  //      for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) }
   444  type Pciter struct {
   445  	d       Pcdata
   446  	p       []byte
   447  	pc      uint32
   448  	nextpc  uint32
   449  	pcscale uint32
   450  	value   int32
   451  	start   int
   452  	done    int
   453  }
   454  
   455  // symbol version, incremented each time a file is loaded.
   456  // version==1 is reserved for savehist.
   457  const (
   458  	HistVersion = 1
   459  )
   460  
   461  // Link holds the context for writing object code from a compiler
   462  // to be linker input or for reading that input into the linker.
   463  type Link struct {
   464  	Goarm              int32
   465  	Headtype           int
   466  	Arch               *LinkArch
   467  	Debugasm           int32
   468  	Debugvlog          int32
   469  	Debugdivmod        int32
   470  	Debugpcln          int32
   471  	Flag_shared        int32
   472  	Flag_dynlink       bool
   473  	Bso                *Biobuf
   474  	Pathname           string
   475  	Windows            int32
   476  	Goroot             string
   477  	Goroot_final       string
   478  	Enforce_data_order int32
   479  	Hash               map[SymVer]*LSym
   480  	LineHist           LineHist
   481  	Imports            []string
   482  	Plist              *Plist
   483  	Plast              *Plist
   484  	Sym_div            *LSym
   485  	Sym_divu           *LSym
   486  	Sym_mod            *LSym
   487  	Sym_modu           *LSym
   488  	Tlsg               *LSym
   489  	Plan9privates      *LSym
   490  	Curp               *Prog
   491  	Printp             *Prog
   492  	Blitrl             *Prog
   493  	Elitrl             *Prog
   494  	Rexflag            int
   495  	Rep                int
   496  	Repn               int
   497  	Lock               int
   498  	Asmode             int
   499  	Andptr             []byte
   500  	And                [100]uint8
   501  	Instoffset         int64
   502  	Autosize           int32
   503  	Armsize            int32
   504  	Pc                 int64
   505  	Tlsoffset          int
   506  	Diag               func(string, ...interface{})
   507  	Mode               int
   508  	Cursym             *LSym
   509  	Version            int
   510  	Textp              *LSym
   511  	Etextp             *LSym
   512  }
   513  
   514  type SymVer struct {
   515  	Name    string
   516  	Version int // TODO: make int16 to match LSym.Version?
   517  }
   518  
   519  // LinkArch is the definition of a single architecture.
   520  type LinkArch struct {
   521  	ByteOrder  binary.ByteOrder
   522  	Name       string
   523  	Thechar    int
   524  	Preprocess func(*Link, *LSym)
   525  	Assemble   func(*Link, *LSym)
   526  	Follow     func(*Link, *LSym)
   527  	Progedit   func(*Link, *Prog)
   528  	UnaryDst   map[int]bool // Instruction takes one operand, a destination.
   529  	Minlc      int
   530  	Ptrsize    int
   531  	Regsize    int
   532  }
   533  
   534  /* executable header types */
   535  const (
   536  	Hunknown = 0 + iota
   537  	Hdarwin
   538  	Hdragonfly
   539  	Helf
   540  	Hfreebsd
   541  	Hlinux
   542  	Hnacl
   543  	Hnetbsd
   544  	Hopenbsd
   545  	Hplan9
   546  	Hsolaris
   547  	Hwindows
   548  )
   549  
   550  type Plist struct {
   551  	Name    *LSym
   552  	Firstpc *Prog
   553  	Recur   int
   554  	Link    *Plist
   555  }
   556  
   557  /*
   558   * start a new Prog list.
   559   */
   560  func Linknewplist(ctxt *Link) *Plist {
   561  	pl := new(Plist)
   562  	if ctxt.Plist == nil {
   563  		ctxt.Plist = pl
   564  	} else {
   565  		ctxt.Plast.Link = pl
   566  	}
   567  	ctxt.Plast = pl
   568  	return pl
   569  }