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

View as plain text