Source file src/cmd/link/internal/amd64/asm.go

     1  // Inferno utils/6l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/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/loader"
    38  	"cmd/link/internal/sym"
    39  	"debug/elf"
    40  	"log"
    41  )
    42  
    43  func PADDR(x uint32) uint32 {
    44  	return x &^ 0x80000000
    45  }
    46  
    47  func gentext(ctxt *ld.Link, ldr *loader.Loader) {
    48  	initfunc, addmoduledata := ld.PrepareAddmoduledata(ctxt)
    49  	if initfunc == nil {
    50  		return
    51  	}
    52  
    53  	o := func(op ...uint8) {
    54  		for _, op1 := range op {
    55  			initfunc.AddUint8(op1)
    56  		}
    57  	}
    58  
    59  	// 0000000000000000 <local.dso_init>:
    60  	//    0:	48 8d 3d 00 00 00 00 	lea    0x0(%rip),%rdi        # 7 <local.dso_init+0x7>
    61  	// 			3: R_X86_64_PC32	runtime.firstmoduledata-0x4
    62  	o(0x48, 0x8d, 0x3d)
    63  	initfunc.AddPCRelPlus(ctxt.Arch, ctxt.Moduledata, 0)
    64  	//    7:	e8 00 00 00 00       	callq  c <local.dso_init+0xc>
    65  	// 			8: R_X86_64_PLT32	runtime.addmoduledata-0x4
    66  	o(0xe8)
    67  	initfunc.AddSymRef(ctxt.Arch, addmoduledata, 0, objabi.R_CALL, 4)
    68  	//    c:	c3                   	retq
    69  	o(0xc3)
    70  }
    71  
    72  func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool {
    73  	targ := r.Sym()
    74  	var targType sym.SymKind
    75  	if targ != 0 {
    76  		targType = ldr.SymType(targ)
    77  	}
    78  
    79  	switch rt := r.Type(); rt {
    80  	default:
    81  		if rt >= objabi.ElfRelocOffset {
    82  			ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type()))
    83  			return false
    84  		}
    85  
    86  		// Handle relocations found in ELF object files.
    87  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_PC32):
    88  		if targType == sym.SDYNIMPORT {
    89  			ldr.Errorf(s, "unexpected R_X86_64_PC32 relocation for dynamic symbol %s", ldr.SymName(targ))
    90  		}
    91  		if targType == 0 || targType == sym.SXREF {
    92  			ldr.Errorf(s, "unknown symbol %s in pcrel", ldr.SymName(targ))
    93  		}
    94  		su := ldr.MakeSymbolUpdater(s)
    95  		su.SetRelocType(rIdx, objabi.R_PCREL)
    96  		su.SetRelocAdd(rIdx, r.Add()+4)
    97  		return true
    98  
    99  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_PC64):
   100  		if targType == sym.SDYNIMPORT {
   101  			ldr.Errorf(s, "unexpected R_X86_64_PC64 relocation for dynamic symbol %s", ldr.SymName(targ))
   102  		}
   103  		if targType == 0 || targType == sym.SXREF {
   104  			ldr.Errorf(s, "unknown symbol %s in pcrel", ldr.SymName(targ))
   105  		}
   106  		su := ldr.MakeSymbolUpdater(s)
   107  		su.SetRelocType(rIdx, objabi.R_PCREL)
   108  		su.SetRelocAdd(rIdx, r.Add()+8)
   109  		return true
   110  
   111  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_PLT32):
   112  		su := ldr.MakeSymbolUpdater(s)
   113  		su.SetRelocType(rIdx, objabi.R_PCREL)
   114  		su.SetRelocAdd(rIdx, r.Add()+4)
   115  		if targType == sym.SDYNIMPORT {
   116  			addpltsym(target, ldr, syms, targ)
   117  			su.SetRelocSym(rIdx, syms.PLT)
   118  			su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymPlt(targ)))
   119  		}
   120  
   121  		return true
   122  
   123  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_GOTPCREL),
   124  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_GOTPCRELX),
   125  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_REX_GOTPCRELX):
   126  		su := ldr.MakeSymbolUpdater(s)
   127  		if targType != sym.SDYNIMPORT {
   128  			// have symbol
   129  			sData := ldr.Data(s)
   130  			if r.Off() >= 2 && sData[r.Off()-2] == 0x8b {
   131  				su.MakeWritable()
   132  				// turn MOVQ of GOT entry into LEAQ of symbol itself
   133  				writeableData := su.Data()
   134  				writeableData[r.Off()-2] = 0x8d
   135  				su.SetRelocType(rIdx, objabi.R_PCREL)
   136  				su.SetRelocAdd(rIdx, r.Add()+4)
   137  				return true
   138  			}
   139  		}
   140  
   141  		// fall back to using GOT and hope for the best (CMOV*)
   142  		// TODO: just needs relocation, no need to put in .dynsym
   143  		ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_X86_64_GLOB_DAT))
   144  
   145  		su.SetRelocType(rIdx, objabi.R_PCREL)
   146  		su.SetRelocSym(rIdx, syms.GOT)
   147  		su.SetRelocAdd(rIdx, r.Add()+4+int64(ldr.SymGot(targ)))
   148  		return true
   149  
   150  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_64):
   151  		if targType == sym.SDYNIMPORT {
   152  			ldr.Errorf(s, "unexpected R_X86_64_64 relocation for dynamic symbol %s", ldr.SymName(targ))
   153  		}
   154  		su := ldr.MakeSymbolUpdater(s)
   155  		su.SetRelocType(rIdx, objabi.R_ADDR)
   156  		if target.IsPIE() && target.IsInternal() {
   157  			// For internal linking PIE, this R_ADDR relocation cannot
   158  			// be resolved statically. We need to generate a dynamic
   159  			// relocation. Let the code below handle it.
   160  			break
   161  		}
   162  		return true
   163  
   164  	// Handle relocations found in Mach-O object files.
   165  	case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0,
   166  		objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0,
   167  		objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0:
   168  		su := ldr.MakeSymbolUpdater(s)
   169  		su.SetRelocType(rIdx, objabi.R_ADDR)
   170  
   171  		if targType == sym.SDYNIMPORT {
   172  			ldr.Errorf(s, "unexpected reloc for dynamic symbol %s", ldr.SymName(targ))
   173  		}
   174  		if target.IsPIE() && target.IsInternal() {
   175  			// For internal linking PIE, this R_ADDR relocation cannot
   176  			// be resolved statically. We need to generate a dynamic
   177  			// relocation. Let the code below handle it.
   178  			if rt == objabi.MachoRelocOffset+ld.MACHO_X86_64_RELOC_UNSIGNED*2 {
   179  				break
   180  			} else {
   181  				// MACHO_X86_64_RELOC_SIGNED or MACHO_X86_64_RELOC_BRANCH
   182  				// Can this happen? The object is expected to be PIC.
   183  				ldr.Errorf(s, "unsupported relocation for PIE: %v", rt)
   184  			}
   185  		}
   186  		return true
   187  
   188  	case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SUBTRACTOR*2 + 0:
   189  		// X86_64_RELOC_SUBTRACTOR must be followed by X86_64_RELOC_UNSIGNED.
   190  		// The pair of relocations resolves to the difference between two
   191  		// symbol addresses (each relocation specifies a symbol).
   192  		// See Darwin's header file include/mach-o/x86_64/reloc.h.
   193  		// ".quad _foo - _bar" is expressed as
   194  		// r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_bar
   195  		// r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
   196  		//
   197  		// For the cases we care (the race syso), the symbol being subtracted
   198  		// out is always in the current section, so we can just convert it to
   199  		// a PC-relative relocation, with the addend adjusted.
   200  		// If later we need the more general form, we can introduce objabi.R_DIFF
   201  		// that works like this Mach-O relocation.
   202  		su := ldr.MakeSymbolUpdater(s)
   203  		outer, off := ld.FoldSubSymbolOffset(ldr, targ)
   204  		if outer != s {
   205  			ldr.Errorf(s, "unsupported X86_64_RELOC_SUBTRACTOR reloc: target %s, outer %s",
   206  				ldr.SymName(targ), ldr.SymName(outer))
   207  			break
   208  		}
   209  		relocs := su.Relocs()
   210  		if rIdx+1 >= relocs.Count() || relocs.At(rIdx+1).Type() != objabi.MachoRelocOffset+ld.MACHO_X86_64_RELOC_UNSIGNED*2+0 || relocs.At(rIdx+1).Off() != r.Off() {
   211  			ldr.Errorf(s, "unexpected X86_64_RELOC_SUBTRACTOR reloc, must be followed by X86_64_RELOC_UNSIGNED at the same offset: %d %v %d", rIdx, relocs.At(rIdx+1).Type(), relocs.At(rIdx+1).Off())
   212  		}
   213  		// The second relocation has the target symbol we want
   214  		su.SetRelocType(rIdx+1, objabi.R_PCREL)
   215  		su.SetRelocAdd(rIdx+1, r.Add()+int64(r.Off())-off)
   216  		// Remove the other relocation
   217  		su.SetRelocSiz(rIdx, 0)
   218  		return true
   219  
   220  	case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1:
   221  		if targType == sym.SDYNIMPORT {
   222  			addpltsym(target, ldr, syms, targ)
   223  			su := ldr.MakeSymbolUpdater(s)
   224  			su.SetRelocSym(rIdx, syms.PLT)
   225  			su.SetRelocType(rIdx, objabi.R_PCREL)
   226  			su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ)))
   227  			return true
   228  		}
   229  		fallthrough
   230  
   231  	case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1,
   232  		objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1,
   233  		objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1,
   234  		objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1,
   235  		objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1:
   236  		su := ldr.MakeSymbolUpdater(s)
   237  		su.SetRelocType(rIdx, objabi.R_PCREL)
   238  
   239  		if targType == sym.SDYNIMPORT {
   240  			ldr.Errorf(s, "unexpected pc-relative reloc for dynamic symbol %s", ldr.SymName(targ))
   241  		}
   242  		return true
   243  
   244  	case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1:
   245  		if targType != sym.SDYNIMPORT {
   246  			// have symbol
   247  			// turn MOVQ of GOT entry into LEAQ of symbol itself
   248  			sdata := ldr.Data(s)
   249  			if r.Off() < 2 || sdata[r.Off()-2] != 0x8b {
   250  				ldr.Errorf(s, "unexpected GOT_LOAD reloc for non-dynamic symbol %s", ldr.SymName(targ))
   251  				return false
   252  			}
   253  
   254  			su := ldr.MakeSymbolUpdater(s)
   255  			su.MakeWritable()
   256  			sdata = su.Data()
   257  			sdata[r.Off()-2] = 0x8d
   258  			su.SetRelocType(rIdx, objabi.R_PCREL)
   259  			return true
   260  		}
   261  		fallthrough
   262  
   263  	case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_GOT*2 + 1:
   264  		if targType != sym.SDYNIMPORT {
   265  			ldr.Errorf(s, "unexpected GOT reloc for non-dynamic symbol %s", ldr.SymName(targ))
   266  		}
   267  		ld.AddGotSym(target, ldr, syms, targ, 0)
   268  		su := ldr.MakeSymbolUpdater(s)
   269  		su.SetRelocType(rIdx, objabi.R_PCREL)
   270  		su.SetRelocSym(rIdx, syms.GOT)
   271  		su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ)))
   272  		return true
   273  	}
   274  
   275  	// Reread the reloc to incorporate any changes in type above.
   276  	relocs := ldr.Relocs(s)
   277  	r = relocs.At(rIdx)
   278  
   279  	switch r.Type() {
   280  	case objabi.R_CALL:
   281  		if targType != sym.SDYNIMPORT {
   282  			// nothing to do, the relocation will be laid out in reloc
   283  			return true
   284  		}
   285  		if target.IsExternal() {
   286  			// External linker will do this relocation.
   287  			return true
   288  		}
   289  		// Internal linking, for both ELF and Mach-O.
   290  		// Build a PLT entry and change the relocation target to that entry.
   291  		addpltsym(target, ldr, syms, targ)
   292  		su := ldr.MakeSymbolUpdater(s)
   293  		su.SetRelocSym(rIdx, syms.PLT)
   294  		su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ)))
   295  		return true
   296  
   297  	case objabi.R_PCREL:
   298  		if targType == sym.SDYNIMPORT && ldr.SymType(s).IsText() && target.IsDarwin() {
   299  			// Loading the address of a dynamic symbol. Rewrite to use GOT.
   300  			// turn LEAQ symbol address to MOVQ of GOT entry
   301  			if r.Add() != 0 {
   302  				ldr.Errorf(s, "unexpected nonzero addend for dynamic symbol %s", ldr.SymName(targ))
   303  				return false
   304  			}
   305  			su := ldr.MakeSymbolUpdater(s)
   306  			if r.Off() >= 2 && su.Data()[r.Off()-2] == 0x8d {
   307  				su.MakeWritable()
   308  				su.Data()[r.Off()-2] = 0x8b
   309  				if target.IsInternal() {
   310  					ld.AddGotSym(target, ldr, syms, targ, 0)
   311  					su.SetRelocSym(rIdx, syms.GOT)
   312  					su.SetRelocAdd(rIdx, int64(ldr.SymGot(targ)))
   313  				} else {
   314  					su.SetRelocType(rIdx, objabi.R_GOTPCREL)
   315  				}
   316  				return true
   317  			}
   318  			ldr.Errorf(s, "unexpected R_PCREL reloc for dynamic symbol %s: not preceded by LEAQ instruction", ldr.SymName(targ))
   319  		}
   320  
   321  	case objabi.R_ADDR:
   322  		if ldr.SymType(s).IsText() && target.IsElf() {
   323  			su := ldr.MakeSymbolUpdater(s)
   324  			if target.IsSolaris() {
   325  				addpltsym(target, ldr, syms, targ)
   326  				su.SetRelocSym(rIdx, syms.PLT)
   327  				su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymPlt(targ)))
   328  				return true
   329  			}
   330  			// The code is asking for the address of an external
   331  			// function. We provide it with the address of the
   332  			// correspondent GOT symbol.
   333  			ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_X86_64_GLOB_DAT))
   334  
   335  			su.SetRelocSym(rIdx, syms.GOT)
   336  			su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ)))
   337  			return true
   338  		}
   339  
   340  		// Process dynamic relocations for the data sections.
   341  		if target.IsPIE() && target.IsInternal() {
   342  			// When internally linking, generate dynamic relocations
   343  			// for all typical R_ADDR relocations. The exception
   344  			// are those R_ADDR that are created as part of generating
   345  			// the dynamic relocations and must be resolved statically.
   346  			//
   347  			// There are three phases relevant to understanding this:
   348  			//
   349  			//	dodata()  // we are here
   350  			//	address() // symbol address assignment
   351  			//	reloc()   // resolution of static R_ADDR relocs
   352  			//
   353  			// At this point symbol addresses have not been
   354  			// assigned yet (as the final size of the .rela section
   355  			// will affect the addresses), and so we cannot write
   356  			// the Elf64_Rela.r_offset now. Instead we delay it
   357  			// until after the 'address' phase of the linker is
   358  			// complete. We do this via Addaddrplus, which creates
   359  			// a new R_ADDR relocation which will be resolved in
   360  			// the 'reloc' phase.
   361  			//
   362  			// These synthetic static R_ADDR relocs must be skipped
   363  			// now, or else we will be caught in an infinite loop
   364  			// of generating synthetic relocs for our synthetic
   365  			// relocs.
   366  			//
   367  			// Furthermore, the rela sections contain dynamic
   368  			// relocations with R_ADDR relocations on
   369  			// Elf64_Rela.r_offset. This field should contain the
   370  			// symbol offset as determined by reloc(), not the
   371  			// final dynamically linked address as a dynamic
   372  			// relocation would provide.
   373  			switch ldr.SymName(s) {
   374  			case ".dynsym", ".rela", ".rela.plt", ".got.plt", ".dynamic":
   375  				return false
   376  			}
   377  		} else {
   378  			// Either internally linking a static executable,
   379  			// in which case we can resolve these relocations
   380  			// statically in the 'reloc' phase, or externally
   381  			// linking, in which case the relocation will be
   382  			// prepared in the 'reloc' phase and passed to the
   383  			// external linker in the 'asmb' phase.
   384  			if t := ldr.SymType(s); !t.IsDATA() && !t.IsRODATA() {
   385  				break
   386  			}
   387  		}
   388  
   389  		if target.IsElf() {
   390  			// Generate R_X86_64_RELATIVE relocations for best
   391  			// efficiency in the dynamic linker.
   392  			//
   393  			// As noted above, symbol addresses have not been
   394  			// assigned yet, so we can't generate the final reloc
   395  			// entry yet. We ultimately want:
   396  			//
   397  			// r_offset = s + r.Off
   398  			// r_info = R_X86_64_RELATIVE
   399  			// r_addend = targ + r.Add
   400  			//
   401  			// The dynamic linker will set *offset = base address +
   402  			// addend.
   403  			//
   404  			// AddAddrPlus is used for r_offset and r_addend to
   405  			// generate new R_ADDR relocations that will update
   406  			// these fields in the 'reloc' phase.
   407  			rela := ldr.MakeSymbolUpdater(syms.Rela)
   408  			rela.AddAddrPlus(target.Arch, s, int64(r.Off()))
   409  			if r.Siz() == 8 {
   410  				rela.AddUint64(target.Arch, elf.R_INFO(0, uint32(elf.R_X86_64_RELATIVE)))
   411  			} else {
   412  				ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ))
   413  			}
   414  			rela.AddAddrPlus(target.Arch, targ, int64(r.Add()))
   415  			// Not mark r done here. So we still apply it statically,
   416  			// so in the file content we'll also have the right offset
   417  			// to the relocation target. So it can be examined statically
   418  			// (e.g. go version).
   419  			return true
   420  		}
   421  
   422  		if target.IsDarwin() {
   423  			// Mach-O relocations are a royal pain to lay out.
   424  			// They use a compact stateful bytecode representation.
   425  			// Here we record what are needed and encode them later.
   426  			ld.MachoAddRebase(s, int64(r.Off()))
   427  			// Not mark r done here. So we still apply it statically,
   428  			// so in the file content we'll also have the right offset
   429  			// to the relocation target. So it can be examined statically
   430  			// (e.g. go version).
   431  			return true
   432  		}
   433  	case objabi.R_GOTPCREL:
   434  		if target.IsExternal() {
   435  			// External linker will do this relocation.
   436  			return true
   437  		}
   438  		// We only need to handle external linking mode, as R_GOTPCREL can
   439  		// only occur in plugin or shared build modes.
   440  	}
   441  
   442  	return false
   443  }
   444  
   445  func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool {
   446  	out.Write64(uint64(sectoff))
   447  
   448  	elfsym := ld.ElfSymForReloc(ctxt, r.Xsym)
   449  	siz := r.Size
   450  	switch r.Type {
   451  	default:
   452  		return false
   453  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   454  		if siz == 4 {
   455  			out.Write64(uint64(elf.R_X86_64_32) | uint64(elfsym)<<32)
   456  		} else if siz == 8 {
   457  			out.Write64(uint64(elf.R_X86_64_64) | uint64(elfsym)<<32)
   458  		} else {
   459  			return false
   460  		}
   461  	case objabi.R_TLS_LE:
   462  		if siz == 4 {
   463  			out.Write64(uint64(elf.R_X86_64_TPOFF32) | uint64(elfsym)<<32)
   464  		} else {
   465  			return false
   466  		}
   467  	case objabi.R_TLS_IE:
   468  		if siz == 4 {
   469  			out.Write64(uint64(elf.R_X86_64_GOTTPOFF) | uint64(elfsym)<<32)
   470  		} else {
   471  			return false
   472  		}
   473  	case objabi.R_CALL:
   474  		if siz == 4 {
   475  			if ldr.SymType(r.Xsym) == sym.SDYNIMPORT {
   476  				out.Write64(uint64(elf.R_X86_64_PLT32) | uint64(elfsym)<<32)
   477  			} else {
   478  				out.Write64(uint64(elf.R_X86_64_PC32) | uint64(elfsym)<<32)
   479  			}
   480  		} else {
   481  			return false
   482  		}
   483  	case objabi.R_PCREL:
   484  		if siz == 4 {
   485  			if ldr.SymType(r.Xsym) == sym.SDYNIMPORT && ldr.SymElfType(r.Xsym) == elf.STT_FUNC {
   486  				out.Write64(uint64(elf.R_X86_64_PLT32) | uint64(elfsym)<<32)
   487  			} else {
   488  				out.Write64(uint64(elf.R_X86_64_PC32) | uint64(elfsym)<<32)
   489  			}
   490  		} else {
   491  			return false
   492  		}
   493  	case objabi.R_GOTPCREL:
   494  		if siz == 4 {
   495  			out.Write64(uint64(elf.R_X86_64_GOTPCREL) | uint64(elfsym)<<32)
   496  		} else {
   497  			return false
   498  		}
   499  	}
   500  
   501  	out.Write64(uint64(r.Xadd))
   502  	return true
   503  }
   504  
   505  func machoreloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool {
   506  	var v uint32
   507  
   508  	rs := r.Xsym
   509  	rt := r.Type
   510  
   511  	if !ldr.SymType(s).IsDWARF() {
   512  		if ldr.SymDynid(rs) < 0 {
   513  			ldr.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymType(rs), ldr.SymType(rs))
   514  			return false
   515  		}
   516  
   517  		v = uint32(ldr.SymDynid(rs))
   518  		v |= 1 << 27 // external relocation
   519  	} else {
   520  		v = uint32(ldr.SymSect(rs).Extnum)
   521  		if v == 0 {
   522  			ldr.Errorf(s, "reloc %d (%s) to symbol %s in non-macho section %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymSect(rs).Name, ldr.SymType(rs), ldr.SymType(rs))
   523  			return false
   524  		}
   525  	}
   526  
   527  	switch rt {
   528  	default:
   529  		return false
   530  
   531  	case objabi.R_ADDR:
   532  		v |= ld.MACHO_X86_64_RELOC_UNSIGNED << 28
   533  
   534  	case objabi.R_CALL:
   535  		v |= 1 << 24 // pc-relative bit
   536  		v |= ld.MACHO_X86_64_RELOC_BRANCH << 28
   537  
   538  		// NOTE: Only works with 'external' relocation. Forced above.
   539  	case objabi.R_PCREL:
   540  		v |= 1 << 24 // pc-relative bit
   541  		v |= ld.MACHO_X86_64_RELOC_SIGNED << 28
   542  	case objabi.R_GOTPCREL:
   543  		v |= 1 << 24 // pc-relative bit
   544  		v |= ld.MACHO_X86_64_RELOC_GOT_LOAD << 28
   545  	}
   546  
   547  	switch r.Size {
   548  	default:
   549  		return false
   550  
   551  	case 1:
   552  		v |= 0 << 25
   553  
   554  	case 2:
   555  		v |= 1 << 25
   556  
   557  	case 4:
   558  		v |= 2 << 25
   559  
   560  	case 8:
   561  		v |= 3 << 25
   562  	}
   563  
   564  	out.Write32(uint32(sectoff))
   565  	out.Write32(v)
   566  	return true
   567  }
   568  
   569  func pereloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool {
   570  	var v uint32
   571  
   572  	rs := r.Xsym
   573  	rt := r.Type
   574  
   575  	if ldr.SymDynid(rs) < 0 {
   576  		ldr.Errorf(s, "reloc %d (%s) to non-coff symbol %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymType(rs), ldr.SymType(rs))
   577  		return false
   578  	}
   579  
   580  	out.Write32(uint32(sectoff))
   581  	out.Write32(uint32(ldr.SymDynid(rs)))
   582  
   583  	switch rt {
   584  	default:
   585  		return false
   586  
   587  	case objabi.R_DWARFSECREF:
   588  		v = ld.IMAGE_REL_AMD64_SECREL
   589  
   590  	case objabi.R_ADDR:
   591  		if r.Size == 8 {
   592  			v = ld.IMAGE_REL_AMD64_ADDR64
   593  		} else {
   594  			v = ld.IMAGE_REL_AMD64_ADDR32
   595  		}
   596  
   597  	case objabi.R_PEIMAGEOFF:
   598  		v = ld.IMAGE_REL_AMD64_ADDR32NB
   599  
   600  	case objabi.R_CALL,
   601  		objabi.R_PCREL:
   602  		v = ld.IMAGE_REL_AMD64_REL32
   603  	}
   604  
   605  	out.Write16(uint16(v))
   606  
   607  	return true
   608  }
   609  
   610  func archreloc(*ld.Target, *loader.Loader, *ld.ArchSyms, loader.Reloc, loader.Sym, int64) (int64, int, bool) {
   611  	return -1, 0, false
   612  }
   613  
   614  func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64, []byte) int64 {
   615  	log.Fatalf("unexpected relocation variant")
   616  	return -1
   617  }
   618  
   619  func elfsetupplt(ctxt *ld.Link, ldr *loader.Loader, plt, got *loader.SymbolBuilder, dynamic loader.Sym) {
   620  	if plt.Size() == 0 {
   621  		// pushq got+8(IP)
   622  		plt.AddUint8(0xff)
   623  
   624  		plt.AddUint8(0x35)
   625  		plt.AddPCRelPlus(ctxt.Arch, got.Sym(), 8)
   626  
   627  		// jmpq got+16(IP)
   628  		plt.AddUint8(0xff)
   629  
   630  		plt.AddUint8(0x25)
   631  		plt.AddPCRelPlus(ctxt.Arch, got.Sym(), 16)
   632  
   633  		// nopl 0(AX)
   634  		plt.AddUint32(ctxt.Arch, 0x00401f0f)
   635  
   636  		// assume got->size == 0 too
   637  		got.AddAddrPlus(ctxt.Arch, dynamic, 0)
   638  
   639  		got.AddUint64(ctxt.Arch, 0)
   640  		got.AddUint64(ctxt.Arch, 0)
   641  	}
   642  }
   643  
   644  func addpltsym(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) {
   645  	if ldr.SymPlt(s) >= 0 {
   646  		return
   647  	}
   648  
   649  	ld.Adddynsym(ldr, target, syms, s)
   650  
   651  	if target.IsElf() {
   652  		plt := ldr.MakeSymbolUpdater(syms.PLT)
   653  		got := ldr.MakeSymbolUpdater(syms.GOTPLT)
   654  		rela := ldr.MakeSymbolUpdater(syms.RelaPLT)
   655  		if plt.Size() == 0 {
   656  			panic("plt is not set up")
   657  		}
   658  
   659  		// jmpq *got+size(IP)
   660  		plt.AddUint8(0xff)
   661  
   662  		plt.AddUint8(0x25)
   663  		plt.AddPCRelPlus(target.Arch, got.Sym(), got.Size())
   664  
   665  		// add to got: pointer to current pos in plt
   666  		got.AddAddrPlus(target.Arch, plt.Sym(), plt.Size())
   667  
   668  		// pushq $x
   669  		plt.AddUint8(0x68)
   670  
   671  		plt.AddUint32(target.Arch, uint32((got.Size()-24-8)/8))
   672  
   673  		// jmpq .plt
   674  		plt.AddUint8(0xe9)
   675  
   676  		plt.AddUint32(target.Arch, uint32(-(plt.Size() + 4)))
   677  
   678  		// rela
   679  		rela.AddAddrPlus(target.Arch, got.Sym(), got.Size()-8)
   680  
   681  		sDynid := ldr.SymDynid(s)
   682  		rela.AddUint64(target.Arch, elf.R_INFO(uint32(sDynid), uint32(elf.R_X86_64_JMP_SLOT)))
   683  		rela.AddUint64(target.Arch, 0)
   684  
   685  		ldr.SetPlt(s, int32(plt.Size()-16))
   686  	} else if target.IsDarwin() {
   687  		ld.AddGotSym(target, ldr, syms, s, 0)
   688  
   689  		sDynid := ldr.SymDynid(s)
   690  		lep := ldr.MakeSymbolUpdater(syms.LinkEditPLT)
   691  		lep.AddUint32(target.Arch, uint32(sDynid))
   692  
   693  		plt := ldr.MakeSymbolUpdater(syms.PLT)
   694  		ldr.SetPlt(s, int32(plt.Size()))
   695  
   696  		// jmpq *got+size(IP)
   697  		plt.AddUint8(0xff)
   698  		plt.AddUint8(0x25)
   699  		plt.AddPCRelPlus(target.Arch, syms.GOT, int64(ldr.SymGot(s)))
   700  	} else {
   701  		ldr.Errorf(s, "addpltsym: unsupported binary format")
   702  	}
   703  }
   704  
   705  func tlsIEtoLE(P []byte, off, size int) {
   706  	// Transform the PC-relative instruction into a constant load.
   707  	// That is,
   708  	//
   709  	//	MOVQ X(IP), REG  ->  MOVQ $Y, REG
   710  	//
   711  	// To determine the instruction and register, we study the op codes.
   712  	// Consult an AMD64 instruction encoding guide to decipher this.
   713  	if off < 3 {
   714  		log.Fatal("R_X86_64_GOTTPOFF reloc not preceded by MOVQ or ADDQ instruction")
   715  	}
   716  	op := P[off-3 : off]
   717  	reg := op[2] >> 3
   718  
   719  	if op[1] == 0x8b || reg == 4 {
   720  		// MOVQ
   721  		if op[0] == 0x4c {
   722  			op[0] = 0x49
   723  		} else if size == 4 && op[0] == 0x44 {
   724  			op[0] = 0x41
   725  		}
   726  		if op[1] == 0x8b {
   727  			op[1] = 0xc7
   728  		} else {
   729  			op[1] = 0x81 // special case for SP
   730  		}
   731  		op[2] = 0xc0 | reg
   732  	} else {
   733  		// An alternate op is ADDQ. This is handled by GNU gold,
   734  		// but right now is not generated by the Go compiler:
   735  		//	ADDQ X(IP), REG  ->  ADDQ $Y, REG
   736  		// Consider adding support for it here.
   737  		log.Fatalf("expected TLS IE op to be MOVQ, got %v", op)
   738  	}
   739  }
   740  

View as plain text