github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/cmd/link/internal/amd64/asm.go (about)

     1  // Inferno utils/6l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/asm.c
     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 amd64
    32  
    33  import (
    34  	"cmd/internal/objabi"
    35  	"cmd/link/internal/ld"
    36  	"debug/elf"
    37  	"log"
    38  )
    39  
    40  func PADDR(x uint32) uint32 {
    41  	return x &^ 0x80000000
    42  }
    43  
    44  func Addcall(ctxt *ld.Link, s *ld.Symbol, t *ld.Symbol) int64 {
    45  	s.Attr |= ld.AttrReachable
    46  	i := s.Size
    47  	s.Size += 4
    48  	ld.Symgrow(s, s.Size)
    49  	r := ld.Addrel(s)
    50  	r.Sym = t
    51  	r.Off = int32(i)
    52  	r.Type = objabi.R_CALL
    53  	r.Siz = 4
    54  	return i + int64(r.Siz)
    55  }
    56  
    57  func gentext(ctxt *ld.Link) {
    58  	if !ctxt.DynlinkingGo() {
    59  		return
    60  	}
    61  	addmoduledata := ctxt.Syms.Lookup("runtime.addmoduledata", 0)
    62  	if addmoduledata.Type == ld.STEXT && ld.Buildmode != ld.BuildmodePlugin {
    63  		// we're linking a module containing the runtime -> no need for
    64  		// an init function
    65  		return
    66  	}
    67  	addmoduledata.Attr |= ld.AttrReachable
    68  	initfunc := ctxt.Syms.Lookup("go.link.addmoduledata", 0)
    69  	initfunc.Type = ld.STEXT
    70  	initfunc.Attr |= ld.AttrLocal
    71  	initfunc.Attr |= ld.AttrReachable
    72  	o := func(op ...uint8) {
    73  		for _, op1 := range op {
    74  			ld.Adduint8(ctxt, initfunc, op1)
    75  		}
    76  	}
    77  	// 0000000000000000 <local.dso_init>:
    78  	//    0:	48 8d 3d 00 00 00 00 	lea    0x0(%rip),%rdi        # 7 <local.dso_init+0x7>
    79  	// 			3: R_X86_64_PC32	runtime.firstmoduledata-0x4
    80  	o(0x48, 0x8d, 0x3d)
    81  	ld.Addpcrelplus(ctxt, initfunc, ctxt.Moduledata, 0)
    82  	//    7:	e8 00 00 00 00       	callq  c <local.dso_init+0xc>
    83  	// 			8: R_X86_64_PLT32	runtime.addmoduledata-0x4
    84  	o(0xe8)
    85  	Addcall(ctxt, initfunc, addmoduledata)
    86  	//    c:	c3                   	retq
    87  	o(0xc3)
    88  	if ld.Buildmode == ld.BuildmodePlugin {
    89  		ctxt.Textp = append(ctxt.Textp, addmoduledata)
    90  	}
    91  	ctxt.Textp = append(ctxt.Textp, initfunc)
    92  	initarray_entry := ctxt.Syms.Lookup("go.link.addmoduledatainit", 0)
    93  	initarray_entry.Attr |= ld.AttrReachable
    94  	initarray_entry.Attr |= ld.AttrLocal
    95  	initarray_entry.Type = ld.SINITARR
    96  	ld.Addaddr(ctxt, initarray_entry, initfunc)
    97  }
    98  
    99  func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) bool {
   100  	targ := r.Sym
   101  
   102  	switch r.Type {
   103  	default:
   104  		if r.Type >= 256 {
   105  			ld.Errorf(s, "unexpected relocation type %d (%s)", r.Type, ld.RelocName(r.Type))
   106  			return false
   107  		}
   108  
   109  		// Handle relocations found in ELF object files.
   110  	case 256 + ld.R_X86_64_PC32:
   111  		if targ.Type == ld.SDYNIMPORT {
   112  			ld.Errorf(s, "unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name)
   113  		}
   114  		if targ.Type == 0 || targ.Type == ld.SXREF {
   115  			ld.Errorf(s, "unknown symbol %s in pcrel", targ.Name)
   116  		}
   117  		r.Type = objabi.R_PCREL
   118  		r.Add += 4
   119  		return true
   120  
   121  	case 256 + ld.R_X86_64_PC64:
   122  		if targ.Type == ld.SDYNIMPORT {
   123  			ld.Errorf(s, "unexpected R_X86_64_PC64 relocation for dynamic symbol %s", targ.Name)
   124  		}
   125  		if targ.Type == 0 || targ.Type == ld.SXREF {
   126  			ld.Errorf(s, "unknown symbol %s in pcrel", targ.Name)
   127  		}
   128  		r.Type = objabi.R_PCREL
   129  		r.Add += 8
   130  		return true
   131  
   132  	case 256 + ld.R_X86_64_PLT32:
   133  		r.Type = objabi.R_PCREL
   134  		r.Add += 4
   135  		if targ.Type == ld.SDYNIMPORT {
   136  			addpltsym(ctxt, targ)
   137  			r.Sym = ctxt.Syms.Lookup(".plt", 0)
   138  			r.Add += int64(targ.Plt)
   139  		}
   140  
   141  		return true
   142  
   143  	case 256 + ld.R_X86_64_GOTPCREL, 256 + ld.R_X86_64_GOTPCRELX, 256 + ld.R_X86_64_REX_GOTPCRELX:
   144  		if targ.Type != ld.SDYNIMPORT {
   145  			// have symbol
   146  			if r.Off >= 2 && s.P[r.Off-2] == 0x8b {
   147  				// turn MOVQ of GOT entry into LEAQ of symbol itself
   148  				s.P[r.Off-2] = 0x8d
   149  
   150  				r.Type = objabi.R_PCREL
   151  				r.Add += 4
   152  				return true
   153  			}
   154  		}
   155  
   156  		// fall back to using GOT and hope for the best (CMOV*)
   157  		// TODO: just needs relocation, no need to put in .dynsym
   158  		addgotsym(ctxt, targ)
   159  
   160  		r.Type = objabi.R_PCREL
   161  		r.Sym = ctxt.Syms.Lookup(".got", 0)
   162  		r.Add += 4
   163  		r.Add += int64(targ.Got)
   164  		return true
   165  
   166  	case 256 + ld.R_X86_64_64:
   167  		if targ.Type == ld.SDYNIMPORT {
   168  			ld.Errorf(s, "unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name)
   169  		}
   170  		r.Type = objabi.R_ADDR
   171  		return true
   172  
   173  	// Handle relocations found in Mach-O object files.
   174  	case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0,
   175  		512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0,
   176  		512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0:
   177  		// TODO: What is the difference between all these?
   178  		r.Type = objabi.R_ADDR
   179  
   180  		if targ.Type == ld.SDYNIMPORT {
   181  			ld.Errorf(s, "unexpected reloc for dynamic symbol %s", targ.Name)
   182  		}
   183  		return true
   184  
   185  	case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1:
   186  		if targ.Type == ld.SDYNIMPORT {
   187  			addpltsym(ctxt, targ)
   188  			r.Sym = ctxt.Syms.Lookup(".plt", 0)
   189  			r.Add = int64(targ.Plt)
   190  			r.Type = objabi.R_PCREL
   191  			return true
   192  		}
   193  		fallthrough
   194  
   195  		// fall through
   196  	case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1,
   197  		512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1,
   198  		512 + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1,
   199  		512 + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1,
   200  		512 + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1:
   201  		r.Type = objabi.R_PCREL
   202  
   203  		if targ.Type == ld.SDYNIMPORT {
   204  			ld.Errorf(s, "unexpected pc-relative reloc for dynamic symbol %s", targ.Name)
   205  		}
   206  		return true
   207  
   208  	case 512 + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1:
   209  		if targ.Type != ld.SDYNIMPORT {
   210  			// have symbol
   211  			// turn MOVQ of GOT entry into LEAQ of symbol itself
   212  			if r.Off < 2 || s.P[r.Off-2] != 0x8b {
   213  				ld.Errorf(s, "unexpected GOT_LOAD reloc for non-dynamic symbol %s", targ.Name)
   214  				return false
   215  			}
   216  
   217  			s.P[r.Off-2] = 0x8d
   218  			r.Type = objabi.R_PCREL
   219  			return true
   220  		}
   221  		fallthrough
   222  
   223  		// fall through
   224  	case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1:
   225  		if targ.Type != ld.SDYNIMPORT {
   226  			ld.Errorf(s, "unexpected GOT reloc for non-dynamic symbol %s", targ.Name)
   227  		}
   228  		addgotsym(ctxt, targ)
   229  		r.Type = objabi.R_PCREL
   230  		r.Sym = ctxt.Syms.Lookup(".got", 0)
   231  		r.Add += int64(targ.Got)
   232  		return true
   233  	}
   234  
   235  	switch r.Type {
   236  	case objabi.R_CALL,
   237  		objabi.R_PCREL:
   238  		if targ.Type != ld.SDYNIMPORT {
   239  			// nothing to do, the relocation will be laid out in reloc
   240  			return true
   241  		}
   242  		if ld.Headtype == objabi.Hwindows {
   243  			// nothing to do, the relocation will be laid out in pereloc1
   244  			return true
   245  		} else {
   246  			// for both ELF and Mach-O
   247  			addpltsym(ctxt, targ)
   248  			r.Sym = ctxt.Syms.Lookup(".plt", 0)
   249  			r.Add = int64(targ.Plt)
   250  			return true
   251  		}
   252  
   253  	case objabi.R_ADDR:
   254  		if s.Type == ld.STEXT && ld.Iself {
   255  			if ld.Headtype == objabi.Hsolaris {
   256  				addpltsym(ctxt, targ)
   257  				r.Sym = ctxt.Syms.Lookup(".plt", 0)
   258  				r.Add += int64(targ.Plt)
   259  				return true
   260  			}
   261  			// The code is asking for the address of an external
   262  			// function. We provide it with the address of the
   263  			// correspondent GOT symbol.
   264  			addgotsym(ctxt, targ)
   265  
   266  			r.Sym = ctxt.Syms.Lookup(".got", 0)
   267  			r.Add += int64(targ.Got)
   268  			return true
   269  		}
   270  
   271  		// Process dynamic relocations for the data sections.
   272  		if ld.Buildmode == ld.BuildmodePIE && ld.Linkmode == ld.LinkInternal {
   273  			// When internally linking, generate dynamic relocations
   274  			// for all typical R_ADDR relocations. The exception
   275  			// are those R_ADDR that are created as part of generating
   276  			// the dynamic relocations and must be resolved statically.
   277  			//
   278  			// There are three phases relevant to understanding this:
   279  			//
   280  			//	dodata()  // we are here
   281  			//	address() // symbol address assignment
   282  			//	reloc()   // resolution of static R_ADDR relocs
   283  			//
   284  			// At this point symbol addresses have not been
   285  			// assigned yet (as the final size of the .rela section
   286  			// will affect the addresses), and so we cannot write
   287  			// the Elf64_Rela.r_offset now. Instead we delay it
   288  			// until after the 'address' phase of the linker is
   289  			// complete. We do this via Addaddrplus, which creates
   290  			// a new R_ADDR relocation which will be resolved in
   291  			// the 'reloc' phase.
   292  			//
   293  			// These synthetic static R_ADDR relocs must be skipped
   294  			// now, or else we will be caught in an infinite loop
   295  			// of generating synthetic relocs for our synthetic
   296  			// relocs.
   297  			//
   298  			// Furthermore, the rela sections contain dynamic
   299  			// relocations with R_ADDR relocations on
   300  			// Elf64_Rela.r_offset. This field should contain the
   301  			// symbol offset as determined by reloc(), not the
   302  			// final dynamically linked address as a dynamic
   303  			// relocation would provide.
   304  			switch s.Name {
   305  			case ".dynsym", ".rela", ".rela.plt", ".got.plt", ".dynamic":
   306  				return false
   307  			}
   308  		} else {
   309  			// Either internally linking a static executable,
   310  			// in which case we can resolve these relocations
   311  			// statically in the 'reloc' phase, or externally
   312  			// linking, in which case the relocation will be
   313  			// prepared in the 'reloc' phase and passed to the
   314  			// external linker in the 'asmb' phase.
   315  			if s.Type != ld.SDATA && s.Type != ld.SRODATA {
   316  				break
   317  			}
   318  		}
   319  
   320  		if ld.Iself {
   321  			// TODO: We generate a R_X86_64_64 relocation for every R_ADDR, even
   322  			// though it would be more efficient (for the dynamic linker) if we
   323  			// generated R_X86_RELATIVE instead.
   324  			ld.Adddynsym(ctxt, targ)
   325  			rela := ctxt.Syms.Lookup(".rela", 0)
   326  			ld.Addaddrplus(ctxt, rela, s, int64(r.Off))
   327  			if r.Siz == 8 {
   328  				ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_64))
   329  			} else {
   330  				// TODO: never happens, remove.
   331  				ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_32))
   332  			}
   333  			ld.Adduint64(ctxt, rela, uint64(r.Add))
   334  			r.Type = 256 // ignore during relocsym
   335  			return true
   336  		}
   337  
   338  		if ld.Headtype == objabi.Hdarwin && s.Size == int64(ld.SysArch.PtrSize) && r.Off == 0 {
   339  			// Mach-O relocations are a royal pain to lay out.
   340  			// They use a compact stateful bytecode representation
   341  			// that is too much bother to deal with.
   342  			// Instead, interpret the C declaration
   343  			//	void *_Cvar_stderr = &stderr;
   344  			// as making _Cvar_stderr the name of a GOT entry
   345  			// for stderr. This is separate from the usual GOT entry,
   346  			// just in case the C code assigns to the variable,
   347  			// and of course it only works for single pointers,
   348  			// but we only need to support cgo and that's all it needs.
   349  			ld.Adddynsym(ctxt, targ)
   350  
   351  			got := ctxt.Syms.Lookup(".got", 0)
   352  			s.Type = got.Type | ld.SSUB
   353  			s.Outer = got
   354  			s.Sub = got.Sub
   355  			got.Sub = s
   356  			s.Value = got.Size
   357  			ld.Adduint64(ctxt, got, 0)
   358  			ld.Adduint32(ctxt, ctxt.Syms.Lookup(".linkedit.got", 0), uint32(targ.Dynid))
   359  			r.Type = 256 // ignore during relocsym
   360  			return true
   361  		}
   362  
   363  		if ld.Headtype == objabi.Hwindows {
   364  			// nothing to do, the relocation will be laid out in pereloc1
   365  			return true
   366  		}
   367  	}
   368  
   369  	return false
   370  }
   371  
   372  func elfreloc1(ctxt *ld.Link, r *ld.Reloc, sectoff int64) bool {
   373  	ld.Thearch.Vput(uint64(sectoff))
   374  
   375  	elfsym := r.Xsym.ElfsymForReloc()
   376  	switch r.Type {
   377  	default:
   378  		return false
   379  	case objabi.R_ADDR:
   380  		if r.Siz == 4 {
   381  			ld.Thearch.Vput(ld.R_X86_64_32 | uint64(elfsym)<<32)
   382  		} else if r.Siz == 8 {
   383  			ld.Thearch.Vput(ld.R_X86_64_64 | uint64(elfsym)<<32)
   384  		} else {
   385  			return false
   386  		}
   387  	case objabi.R_TLS_LE:
   388  		if r.Siz == 4 {
   389  			ld.Thearch.Vput(ld.R_X86_64_TPOFF32 | uint64(elfsym)<<32)
   390  		} else {
   391  			return false
   392  		}
   393  	case objabi.R_TLS_IE:
   394  		if r.Siz == 4 {
   395  			ld.Thearch.Vput(ld.R_X86_64_GOTTPOFF | uint64(elfsym)<<32)
   396  		} else {
   397  			return false
   398  		}
   399  	case objabi.R_CALL:
   400  		if r.Siz == 4 {
   401  			if r.Xsym.Type == ld.SDYNIMPORT {
   402  				if ctxt.DynlinkingGo() {
   403  					ld.Thearch.Vput(ld.R_X86_64_PLT32 | uint64(elfsym)<<32)
   404  				} else {
   405  					ld.Thearch.Vput(ld.R_X86_64_GOTPCREL | uint64(elfsym)<<32)
   406  				}
   407  			} else {
   408  				ld.Thearch.Vput(ld.R_X86_64_PC32 | uint64(elfsym)<<32)
   409  			}
   410  		} else {
   411  			return false
   412  		}
   413  	case objabi.R_PCREL:
   414  		if r.Siz == 4 {
   415  			if r.Xsym.Type == ld.SDYNIMPORT && r.Xsym.ElfType == elf.STT_FUNC {
   416  				ld.Thearch.Vput(ld.R_X86_64_PLT32 | uint64(elfsym)<<32)
   417  			} else {
   418  				ld.Thearch.Vput(ld.R_X86_64_PC32 | uint64(elfsym)<<32)
   419  			}
   420  		} else {
   421  			return false
   422  		}
   423  	case objabi.R_GOTPCREL:
   424  		if r.Siz == 4 {
   425  			ld.Thearch.Vput(ld.R_X86_64_GOTPCREL | uint64(elfsym)<<32)
   426  		} else {
   427  			return false
   428  		}
   429  	}
   430  
   431  	ld.Thearch.Vput(uint64(r.Xadd))
   432  	return true
   433  }
   434  
   435  func machoreloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool {
   436  	var v uint32
   437  
   438  	rs := r.Xsym
   439  
   440  	if rs.Type == ld.SHOSTOBJ || r.Type == objabi.R_PCREL || r.Type == objabi.R_GOTPCREL {
   441  		if rs.Dynid < 0 {
   442  			ld.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", r.Type, ld.RelocName(r.Type), rs.Name, rs.Type, rs.Type)
   443  			return false
   444  		}
   445  
   446  		v = uint32(rs.Dynid)
   447  		v |= 1 << 27 // external relocation
   448  	} else {
   449  		v = uint32(rs.Sect.Extnum)
   450  		if v == 0 {
   451  			ld.Errorf(s, "reloc %d (%s) to symbol %s in non-macho section %s type=%d (%s)", r.Type, ld.RelocName(r.Type), rs.Name, rs.Sect.Name, rs.Type, rs.Type)
   452  			return false
   453  		}
   454  	}
   455  
   456  	switch r.Type {
   457  	default:
   458  		return false
   459  
   460  	case objabi.R_ADDR:
   461  		v |= ld.MACHO_X86_64_RELOC_UNSIGNED << 28
   462  
   463  	case objabi.R_CALL:
   464  		v |= 1 << 24 // pc-relative bit
   465  		v |= ld.MACHO_X86_64_RELOC_BRANCH << 28
   466  
   467  		// NOTE: Only works with 'external' relocation. Forced above.
   468  	case objabi.R_PCREL:
   469  		v |= 1 << 24 // pc-relative bit
   470  		v |= ld.MACHO_X86_64_RELOC_SIGNED << 28
   471  	case objabi.R_GOTPCREL:
   472  		v |= 1 << 24 // pc-relative bit
   473  		v |= ld.MACHO_X86_64_RELOC_GOT_LOAD << 28
   474  	}
   475  
   476  	switch r.Siz {
   477  	default:
   478  		return false
   479  
   480  	case 1:
   481  		v |= 0 << 25
   482  
   483  	case 2:
   484  		v |= 1 << 25
   485  
   486  	case 4:
   487  		v |= 2 << 25
   488  
   489  	case 8:
   490  		v |= 3 << 25
   491  	}
   492  
   493  	ld.Thearch.Lput(uint32(sectoff))
   494  	ld.Thearch.Lput(v)
   495  	return true
   496  }
   497  
   498  func pereloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool {
   499  	var v uint32
   500  
   501  	rs := r.Xsym
   502  
   503  	if rs.Dynid < 0 {
   504  		ld.Errorf(s, "reloc %d (%s) to non-coff symbol %s type=%d (%s)", r.Type, ld.RelocName(r.Type), rs.Name, rs.Type, rs.Type)
   505  		return false
   506  	}
   507  
   508  	ld.Thearch.Lput(uint32(sectoff))
   509  	ld.Thearch.Lput(uint32(rs.Dynid))
   510  
   511  	switch r.Type {
   512  	default:
   513  		return false
   514  
   515  	case objabi.R_DWARFREF:
   516  		v = ld.IMAGE_REL_AMD64_SECREL
   517  
   518  	case objabi.R_ADDR:
   519  		if r.Siz == 8 {
   520  			v = ld.IMAGE_REL_AMD64_ADDR64
   521  		} else {
   522  			v = ld.IMAGE_REL_AMD64_ADDR32
   523  		}
   524  
   525  	case objabi.R_CALL,
   526  		objabi.R_PCREL:
   527  		v = ld.IMAGE_REL_AMD64_REL32
   528  	}
   529  
   530  	ld.Thearch.Wput(uint16(v))
   531  
   532  	return true
   533  }
   534  
   535  func archreloc(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) bool {
   536  	return false
   537  }
   538  
   539  func archrelocvariant(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, t int64) int64 {
   540  	log.Fatalf("unexpected relocation variant")
   541  	return t
   542  }
   543  
   544  func elfsetupplt(ctxt *ld.Link) {
   545  	plt := ctxt.Syms.Lookup(".plt", 0)
   546  	got := ctxt.Syms.Lookup(".got.plt", 0)
   547  	if plt.Size == 0 {
   548  		// pushq got+8(IP)
   549  		ld.Adduint8(ctxt, plt, 0xff)
   550  
   551  		ld.Adduint8(ctxt, plt, 0x35)
   552  		ld.Addpcrelplus(ctxt, plt, got, 8)
   553  
   554  		// jmpq got+16(IP)
   555  		ld.Adduint8(ctxt, plt, 0xff)
   556  
   557  		ld.Adduint8(ctxt, plt, 0x25)
   558  		ld.Addpcrelplus(ctxt, plt, got, 16)
   559  
   560  		// nopl 0(AX)
   561  		ld.Adduint32(ctxt, plt, 0x00401f0f)
   562  
   563  		// assume got->size == 0 too
   564  		ld.Addaddrplus(ctxt, got, ctxt.Syms.Lookup(".dynamic", 0), 0)
   565  
   566  		ld.Adduint64(ctxt, got, 0)
   567  		ld.Adduint64(ctxt, got, 0)
   568  	}
   569  }
   570  
   571  func addpltsym(ctxt *ld.Link, s *ld.Symbol) {
   572  	if s.Plt >= 0 {
   573  		return
   574  	}
   575  
   576  	ld.Adddynsym(ctxt, s)
   577  
   578  	if ld.Iself {
   579  		plt := ctxt.Syms.Lookup(".plt", 0)
   580  		got := ctxt.Syms.Lookup(".got.plt", 0)
   581  		rela := ctxt.Syms.Lookup(".rela.plt", 0)
   582  		if plt.Size == 0 {
   583  			elfsetupplt(ctxt)
   584  		}
   585  
   586  		// jmpq *got+size(IP)
   587  		ld.Adduint8(ctxt, plt, 0xff)
   588  
   589  		ld.Adduint8(ctxt, plt, 0x25)
   590  		ld.Addpcrelplus(ctxt, plt, got, got.Size)
   591  
   592  		// add to got: pointer to current pos in plt
   593  		ld.Addaddrplus(ctxt, got, plt, plt.Size)
   594  
   595  		// pushq $x
   596  		ld.Adduint8(ctxt, plt, 0x68)
   597  
   598  		ld.Adduint32(ctxt, plt, uint32((got.Size-24-8)/8))
   599  
   600  		// jmpq .plt
   601  		ld.Adduint8(ctxt, plt, 0xe9)
   602  
   603  		ld.Adduint32(ctxt, plt, uint32(-(plt.Size + 4)))
   604  
   605  		// rela
   606  		ld.Addaddrplus(ctxt, rela, got, got.Size-8)
   607  
   608  		ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_JMP_SLOT))
   609  		ld.Adduint64(ctxt, rela, 0)
   610  
   611  		s.Plt = int32(plt.Size - 16)
   612  	} else if ld.Headtype == objabi.Hdarwin {
   613  		// To do lazy symbol lookup right, we're supposed
   614  		// to tell the dynamic loader which library each
   615  		// symbol comes from and format the link info
   616  		// section just so. I'm too lazy (ha!) to do that
   617  		// so for now we'll just use non-lazy pointers,
   618  		// which don't need to be told which library to use.
   619  		//
   620  		// http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html
   621  		// has details about what we're avoiding.
   622  
   623  		addgotsym(ctxt, s)
   624  		plt := ctxt.Syms.Lookup(".plt", 0)
   625  
   626  		ld.Adduint32(ctxt, ctxt.Syms.Lookup(".linkedit.plt", 0), uint32(s.Dynid))
   627  
   628  		// jmpq *got+size(IP)
   629  		s.Plt = int32(plt.Size)
   630  
   631  		ld.Adduint8(ctxt, plt, 0xff)
   632  		ld.Adduint8(ctxt, plt, 0x25)
   633  		ld.Addpcrelplus(ctxt, plt, ctxt.Syms.Lookup(".got", 0), int64(s.Got))
   634  	} else {
   635  		ld.Errorf(s, "addpltsym: unsupported binary format")
   636  	}
   637  }
   638  
   639  func addgotsym(ctxt *ld.Link, s *ld.Symbol) {
   640  	if s.Got >= 0 {
   641  		return
   642  	}
   643  
   644  	ld.Adddynsym(ctxt, s)
   645  	got := ctxt.Syms.Lookup(".got", 0)
   646  	s.Got = int32(got.Size)
   647  	ld.Adduint64(ctxt, got, 0)
   648  
   649  	if ld.Iself {
   650  		rela := ctxt.Syms.Lookup(".rela", 0)
   651  		ld.Addaddrplus(ctxt, rela, got, int64(s.Got))
   652  		ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_GLOB_DAT))
   653  		ld.Adduint64(ctxt, rela, 0)
   654  	} else if ld.Headtype == objabi.Hdarwin {
   655  		ld.Adduint32(ctxt, ctxt.Syms.Lookup(".linkedit.got", 0), uint32(s.Dynid))
   656  	} else {
   657  		ld.Errorf(s, "addgotsym: unsupported binary format")
   658  	}
   659  }
   660  
   661  func asmb(ctxt *ld.Link) {
   662  	if ctxt.Debugvlog != 0 {
   663  		ctxt.Logf("%5.2f asmb\n", ld.Cputime())
   664  	}
   665  
   666  	if ctxt.Debugvlog != 0 {
   667  		ctxt.Logf("%5.2f codeblk\n", ld.Cputime())
   668  	}
   669  
   670  	if ld.Iself {
   671  		ld.Asmbelfsetup()
   672  	}
   673  
   674  	sect := ld.Segtext.Sections[0]
   675  	ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   676  	// 0xCC is INT $3 - breakpoint instruction
   677  	ld.CodeblkPad(ctxt, int64(sect.Vaddr), int64(sect.Length), []byte{0xCC})
   678  	for _, sect = range ld.Segtext.Sections[1:] {
   679  		ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   680  		ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   681  	}
   682  
   683  	if ld.Segrodata.Filelen > 0 {
   684  		if ctxt.Debugvlog != 0 {
   685  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   686  		}
   687  		ld.Cseek(int64(ld.Segrodata.Fileoff))
   688  		ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   689  	}
   690  	if ld.Segrelrodata.Filelen > 0 {
   691  		if ctxt.Debugvlog != 0 {
   692  			ctxt.Logf("%5.2f relrodatblk\n", ld.Cputime())
   693  		}
   694  		ld.Cseek(int64(ld.Segrelrodata.Fileoff))
   695  		ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen))
   696  	}
   697  
   698  	if ctxt.Debugvlog != 0 {
   699  		ctxt.Logf("%5.2f datblk\n", ld.Cputime())
   700  	}
   701  
   702  	ld.Cseek(int64(ld.Segdata.Fileoff))
   703  	ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   704  
   705  	ld.Cseek(int64(ld.Segdwarf.Fileoff))
   706  	ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   707  
   708  	machlink := int64(0)
   709  	if ld.Headtype == objabi.Hdarwin {
   710  		machlink = ld.Domacholink(ctxt)
   711  	}
   712  
   713  	switch ld.Headtype {
   714  	default:
   715  		ld.Errorf(nil, "unknown header type %v", ld.Headtype)
   716  		fallthrough
   717  
   718  	case objabi.Hplan9:
   719  		break
   720  
   721  	case objabi.Hdarwin:
   722  		ld.Flag8 = true /* 64-bit addresses */
   723  
   724  	case objabi.Hlinux,
   725  		objabi.Hfreebsd,
   726  		objabi.Hnetbsd,
   727  		objabi.Hopenbsd,
   728  		objabi.Hdragonfly,
   729  		objabi.Hsolaris:
   730  		ld.Flag8 = true /* 64-bit addresses */
   731  
   732  	case objabi.Hnacl,
   733  		objabi.Hwindows:
   734  		break
   735  	}
   736  
   737  	ld.Symsize = 0
   738  	ld.Spsize = 0
   739  	ld.Lcsize = 0
   740  	symo := int64(0)
   741  	if !*ld.FlagS {
   742  		if ctxt.Debugvlog != 0 {
   743  			ctxt.Logf("%5.2f sym\n", ld.Cputime())
   744  		}
   745  		switch ld.Headtype {
   746  		default:
   747  		case objabi.Hplan9:
   748  			*ld.FlagS = true
   749  			symo = int64(ld.Segdata.Fileoff + ld.Segdata.Filelen)
   750  
   751  		case objabi.Hdarwin:
   752  			symo = int64(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(*ld.FlagRound))) + uint64(machlink))
   753  
   754  		case objabi.Hlinux,
   755  			objabi.Hfreebsd,
   756  			objabi.Hnetbsd,
   757  			objabi.Hopenbsd,
   758  			objabi.Hdragonfly,
   759  			objabi.Hsolaris,
   760  			objabi.Hnacl:
   761  			symo = int64(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   762  			symo = ld.Rnd(symo, int64(*ld.FlagRound))
   763  
   764  		case objabi.Hwindows:
   765  			symo = int64(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   766  			symo = ld.Rnd(symo, ld.PEFILEALIGN)
   767  		}
   768  
   769  		ld.Cseek(symo)
   770  		switch ld.Headtype {
   771  		default:
   772  			if ld.Iself {
   773  				ld.Cseek(symo)
   774  				ld.Asmelfsym(ctxt)
   775  				ld.Cflush()
   776  				ld.Cwrite(ld.Elfstrdat)
   777  
   778  				if ctxt.Debugvlog != 0 {
   779  					ctxt.Logf("%5.2f dwarf\n", ld.Cputime())
   780  				}
   781  
   782  				if ld.Linkmode == ld.LinkExternal {
   783  					ld.Elfemitreloc(ctxt)
   784  				}
   785  			}
   786  
   787  		case objabi.Hplan9:
   788  			ld.Asmplan9sym(ctxt)
   789  			ld.Cflush()
   790  
   791  			sym := ctxt.Syms.Lookup("pclntab", 0)
   792  			if sym != nil {
   793  				ld.Lcsize = int32(len(sym.P))
   794  				for i := 0; int32(i) < ld.Lcsize; i++ {
   795  					ld.Cput(sym.P[i])
   796  				}
   797  
   798  				ld.Cflush()
   799  			}
   800  
   801  		case objabi.Hwindows:
   802  			if ctxt.Debugvlog != 0 {
   803  				ctxt.Logf("%5.2f dwarf\n", ld.Cputime())
   804  			}
   805  
   806  		case objabi.Hdarwin:
   807  			if ld.Linkmode == ld.LinkExternal {
   808  				ld.Machoemitreloc(ctxt)
   809  			}
   810  		}
   811  	}
   812  
   813  	if ctxt.Debugvlog != 0 {
   814  		ctxt.Logf("%5.2f headr\n", ld.Cputime())
   815  	}
   816  	ld.Cseek(0)
   817  	switch ld.Headtype {
   818  	default:
   819  	case objabi.Hplan9: /* plan9 */
   820  		magic := int32(4*26*26 + 7)
   821  
   822  		magic |= 0x00008000                  /* fat header */
   823  		ld.Lputb(uint32(magic))              /* magic */
   824  		ld.Lputb(uint32(ld.Segtext.Filelen)) /* sizes */
   825  		ld.Lputb(uint32(ld.Segdata.Filelen))
   826  		ld.Lputb(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
   827  		ld.Lputb(uint32(ld.Symsize)) /* nsyms */
   828  		vl := ld.Entryvalue(ctxt)
   829  		ld.Lputb(PADDR(uint32(vl))) /* va of entry */
   830  		ld.Lputb(uint32(ld.Spsize)) /* sp offsets */
   831  		ld.Lputb(uint32(ld.Lcsize)) /* line offsets */
   832  		ld.Vputb(uint64(vl))        /* va of entry */
   833  
   834  	case objabi.Hdarwin:
   835  		ld.Asmbmacho(ctxt)
   836  
   837  	case objabi.Hlinux,
   838  		objabi.Hfreebsd,
   839  		objabi.Hnetbsd,
   840  		objabi.Hopenbsd,
   841  		objabi.Hdragonfly,
   842  		objabi.Hsolaris,
   843  		objabi.Hnacl:
   844  		ld.Asmbelf(ctxt, symo)
   845  
   846  	case objabi.Hwindows:
   847  		ld.Asmbpe(ctxt)
   848  	}
   849  
   850  	ld.Cflush()
   851  }
   852  
   853  func tlsIEtoLE(s *ld.Symbol, off, size int) {
   854  	// Transform the PC-relative instruction into a constant load.
   855  	// That is,
   856  	//
   857  	//	MOVQ X(IP), REG  ->  MOVQ $Y, REG
   858  	//
   859  	// To determine the instruction and register, we study the op codes.
   860  	// Consult an AMD64 instruction encoding guide to decipher this.
   861  	if off < 3 {
   862  		log.Fatal("R_X86_64_GOTTPOFF reloc not preceded by MOVQ or ADDQ instruction")
   863  	}
   864  	op := s.P[off-3 : off]
   865  	reg := op[2] >> 3
   866  
   867  	if op[1] == 0x8b || reg == 4 {
   868  		// MOVQ
   869  		if op[0] == 0x4c {
   870  			op[0] = 0x49
   871  		} else if size == 4 && op[0] == 0x44 {
   872  			op[0] = 0x41
   873  		}
   874  		if op[1] == 0x8b {
   875  			op[1] = 0xc7
   876  		} else {
   877  			op[1] = 0x81 // special case for SP
   878  		}
   879  		op[2] = 0xc0 | reg
   880  	} else {
   881  		// An alternate op is ADDQ. This is handled by GNU gold,
   882  		// but right now is not generated by the Go compiler:
   883  		//	ADDQ X(IP), REG  ->  ADDQ $Y, REG
   884  		// Consider adding support for it here.
   885  		log.Fatalf("expected TLS IE op to be MOVQ, got %v", op)
   886  	}
   887  }