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