Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/link/internal/ppc64

     1  // Inferno utils/5l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/5l/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 ppc64
    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  	"encoding/binary"
    41  	"fmt"
    42  	"log"
    43  	"strings"
    44  )
    45  
    46  func genplt(ctxt *ld.Link, ldr *loader.Loader) {
    47  	// The ppc64 ABI PLT has similar concepts to other
    48  	// architectures, but is laid out quite differently. When we
    49  	// see an R_PPC64_REL24 relocation to a dynamic symbol
    50  	// (indicating that the call needs to go through the PLT), we
    51  	// generate up to three stubs and reserve a PLT slot.
    52  	//
    53  	// 1) The call site will be bl x; nop (where the relocation
    54  	//    applies to the bl).  We rewrite this to bl x_stub; ld
    55  	//    r2,24(r1).  The ld is necessary because x_stub will save
    56  	//    r2 (the TOC pointer) at 24(r1) (the "TOC save slot").
    57  	//
    58  	// 2) We reserve space for a pointer in the .plt section (once
    59  	//    per referenced dynamic function).  .plt is a data
    60  	//    section filled solely by the dynamic linker (more like
    61  	//    .plt.got on other architectures).  Initially, the
    62  	//    dynamic linker will fill each slot with a pointer to the
    63  	//    corresponding x@plt entry point.
    64  	//
    65  	// 3) We generate the "call stub" x_stub (once per dynamic
    66  	//    function/object file pair).  This saves the TOC in the
    67  	//    TOC save slot, reads the function pointer from x's .plt
    68  	//    slot and calls it like any other global entry point
    69  	//    (including setting r12 to the function address).
    70  	//
    71  	// 4) We generate the "symbol resolver stub" x@plt (once per
    72  	//    dynamic function).  This is solely a branch to the glink
    73  	//    resolver stub.
    74  	//
    75  	// 5) We generate the glink resolver stub (only once).  This
    76  	//    computes which symbol resolver stub we came through and
    77  	//    invokes the dynamic resolver via a pointer provided by
    78  	//    the dynamic linker. This will patch up the .plt slot to
    79  	//    point directly at the function so future calls go
    80  	//    straight from the call stub to the real function, and
    81  	//    then call the function.
    82  
    83  	// NOTE: It's possible we could make ppc64 closer to other
    84  	// architectures: ppc64's .plt is like .plt.got on other
    85  	// platforms and ppc64's .glink is like .plt on other
    86  	// platforms.
    87  
    88  	// Find all R_PPC64_REL24 relocations that reference dynamic
    89  	// imports. Reserve PLT entries for these symbols and
    90  	// generate call stubs. The call stubs need to live in .text,
    91  	// which is why we need to do this pass this early.
    92  	//
    93  	// This assumes "case 1" from the ABI, where the caller needs
    94  	// us to save and restore the TOC pointer.
    95  	var stubs []loader.Sym
    96  	for _, s := range ctxt.Textp {
    97  		relocs := ldr.Relocs(s)
    98  		for i := 0; i < relocs.Count(); i++ {
    99  			r := relocs.At(i)
   100  			if r.Type() != objabi.ElfRelocOffset+objabi.RelocType(elf.R_PPC64_REL24) || ldr.SymType(r.Sym()) != sym.SDYNIMPORT {
   101  				continue
   102  			}
   103  
   104  			// Reserve PLT entry and generate symbol
   105  			// resolver
   106  			addpltsym(ctxt, ldr, r.Sym())
   107  
   108  			// Generate call stub. Important to note that we're looking
   109  			// up the stub using the same version as the parent symbol (s),
   110  			// needed so that symtoc() will select the right .TOC. symbol
   111  			// when processing the stub.  In older versions of the linker
   112  			// this was done by setting stub.Outer to the parent, but
   113  			// if the stub has the right version initially this is not needed.
   114  			n := fmt.Sprintf("%s.%s", ldr.SymName(s), ldr.SymName(r.Sym()))
   115  			stub := ldr.CreateSymForUpdate(n, ldr.SymVersion(s))
   116  			if stub.Size() == 0 {
   117  				stubs = append(stubs, stub.Sym())
   118  				gencallstub(ctxt, ldr, 1, stub, r.Sym())
   119  			}
   120  
   121  			// Update the relocation to use the call stub
   122  			r.SetSym(stub.Sym())
   123  
   124  			// Make the symbol writeable so we can fixup toc.
   125  			su := ldr.MakeSymbolUpdater(s)
   126  			su.MakeWritable()
   127  			p := su.Data()
   128  
   129  			// Check for toc restore slot (a nop), and replace with toc restore.
   130  			var nop uint32
   131  			if len(p) >= int(r.Off()+8) {
   132  				nop = ctxt.Arch.ByteOrder.Uint32(p[r.Off()+4:])
   133  			}
   134  			if nop != 0x60000000 {
   135  				ldr.Errorf(s, "Symbol %s is missing toc restoration slot at offset %d", ldr.SymName(s), r.Off()+4)
   136  			}
   137  			const o1 = 0xe8410018 // ld r2,24(r1)
   138  			ctxt.Arch.ByteOrder.PutUint32(p[r.Off()+4:], o1)
   139  		}
   140  	}
   141  	// Put call stubs at the beginning (instead of the end).
   142  	// So when resolving the relocations to calls to the stubs,
   143  	// the addresses are known and trampolines can be inserted
   144  	// when necessary.
   145  	ctxt.Textp = append(stubs, ctxt.Textp...)
   146  }
   147  
   148  func genaddmoduledata(ctxt *ld.Link, ldr *loader.Loader) {
   149  	initfunc, addmoduledata := ld.PrepareAddmoduledata(ctxt)
   150  	if initfunc == nil {
   151  		return
   152  	}
   153  
   154  	o := func(op uint32) {
   155  		initfunc.AddUint32(ctxt.Arch, op)
   156  	}
   157  
   158  	// addis r2, r12, .TOC.-func@ha
   159  	toc := ctxt.DotTOC[0]
   160  	rel1, _ := initfunc.AddRel(objabi.R_ADDRPOWER_PCREL)
   161  	rel1.SetOff(0)
   162  	rel1.SetSiz(8)
   163  	rel1.SetSym(toc)
   164  	o(0x3c4c0000)
   165  	// addi r2, r2, .TOC.-func@l
   166  	o(0x38420000)
   167  	// mflr r31
   168  	o(0x7c0802a6)
   169  	// stdu r31, -32(r1)
   170  	o(0xf801ffe1)
   171  	// addis r3, r2, local.moduledata@got@ha
   172  	var tgt loader.Sym
   173  	if s := ldr.Lookup("local.moduledata", 0); s != 0 {
   174  		tgt = s
   175  	} else if s := ldr.Lookup("local.pluginmoduledata", 0); s != 0 {
   176  		tgt = s
   177  	} else {
   178  		tgt = ldr.LookupOrCreateSym("runtime.firstmoduledata", 0)
   179  	}
   180  	rel2, _ := initfunc.AddRel(objabi.R_ADDRPOWER_GOT)
   181  	rel2.SetOff(int32(initfunc.Size()))
   182  	rel2.SetSiz(8)
   183  	rel2.SetSym(tgt)
   184  	o(0x3c620000)
   185  	// ld r3, local.moduledata@got@l(r3)
   186  	o(0xe8630000)
   187  	// bl runtime.addmoduledata
   188  	rel3, _ := initfunc.AddRel(objabi.R_CALLPOWER)
   189  	rel3.SetOff(int32(initfunc.Size()))
   190  	rel3.SetSiz(4)
   191  	rel3.SetSym(addmoduledata)
   192  	o(0x48000001)
   193  	// nop
   194  	o(0x60000000)
   195  	// ld r31, 0(r1)
   196  	o(0xe8010000)
   197  	// mtlr r31
   198  	o(0x7c0803a6)
   199  	// addi r1,r1,32
   200  	o(0x38210020)
   201  	// blr
   202  	o(0x4e800020)
   203  }
   204  
   205  func gentext(ctxt *ld.Link, ldr *loader.Loader) {
   206  	if ctxt.DynlinkingGo() {
   207  		genaddmoduledata(ctxt, ldr)
   208  	}
   209  
   210  	if ctxt.LinkMode == ld.LinkInternal {
   211  		genplt(ctxt, ldr)
   212  	}
   213  }
   214  
   215  // Construct a call stub in stub that calls symbol targ via its PLT
   216  // entry.
   217  func gencallstub(ctxt *ld.Link, ldr *loader.Loader, abicase int, stub *loader.SymbolBuilder, targ loader.Sym) {
   218  	if abicase != 1 {
   219  		// If we see R_PPC64_TOCSAVE or R_PPC64_REL24_NOTOC
   220  		// relocations, we'll need to implement cases 2 and 3.
   221  		log.Fatalf("gencallstub only implements case 1 calls")
   222  	}
   223  
   224  	plt := ctxt.PLT
   225  
   226  	stub.SetType(sym.STEXT)
   227  
   228  	// Save TOC pointer in TOC save slot
   229  	stub.AddUint32(ctxt.Arch, 0xf8410018) // std r2,24(r1)
   230  
   231  	// Load the function pointer from the PLT.
   232  	rel, ri1 := stub.AddRel(objabi.R_POWER_TOC)
   233  	rel.SetOff(int32(stub.Size()))
   234  	rel.SetSiz(2)
   235  	rel.SetAdd(int64(ldr.SymPlt(targ)))
   236  	rel.SetSym(plt)
   237  	if ctxt.Arch.ByteOrder == binary.BigEndian {
   238  		rel.SetOff(rel.Off() + int32(rel.Siz()))
   239  	}
   240  	ldr.SetRelocVariant(stub.Sym(), int(ri1), sym.RV_POWER_HA)
   241  	stub.AddUint32(ctxt.Arch, 0x3d820000) // addis r12,r2,targ@plt@toc@ha
   242  
   243  	rel2, ri2 := stub.AddRel(objabi.R_POWER_TOC)
   244  	rel2.SetOff(int32(stub.Size()))
   245  	rel2.SetSiz(2)
   246  	rel2.SetAdd(int64(ldr.SymPlt(targ)))
   247  	rel2.SetSym(plt)
   248  	if ctxt.Arch.ByteOrder == binary.BigEndian {
   249  		rel2.SetOff(rel2.Off() + int32(rel2.Siz()))
   250  	}
   251  	ldr.SetRelocVariant(stub.Sym(), int(ri2), sym.RV_POWER_LO)
   252  	stub.AddUint32(ctxt.Arch, 0xe98c0000) // ld r12,targ@plt@toc@l(r12)
   253  
   254  	// Jump to the loaded pointer
   255  	stub.AddUint32(ctxt.Arch, 0x7d8903a6) // mtctr r12
   256  	stub.AddUint32(ctxt.Arch, 0x4e800420) // bctr
   257  }
   258  
   259  func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool {
   260  	if target.IsElf() {
   261  		return addelfdynrel(target, ldr, syms, s, r, rIdx)
   262  	} else if target.IsAIX() {
   263  		return ld.Xcoffadddynrel(target, ldr, syms, s, r, rIdx)
   264  	}
   265  	return false
   266  }
   267  
   268  func addelfdynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool {
   269  	targ := r.Sym()
   270  	var targType sym.SymKind
   271  	if targ != 0 {
   272  		targType = ldr.SymType(targ)
   273  	}
   274  
   275  	switch r.Type() {
   276  	default:
   277  		if r.Type() >= objabi.ElfRelocOffset {
   278  			ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type()))
   279  			return false
   280  		}
   281  
   282  		// Handle relocations found in ELF object files.
   283  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL24):
   284  		su := ldr.MakeSymbolUpdater(s)
   285  		su.SetRelocType(rIdx, objabi.R_CALLPOWER)
   286  
   287  		// This is a local call, so the caller isn't setting
   288  		// up r12 and r2 is the same for the caller and
   289  		// callee. Hence, we need to go to the local entry
   290  		// point.  (If we don't do this, the callee will try
   291  		// to use r12 to compute r2.)
   292  		su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymLocalentry(targ))*4)
   293  
   294  		if targType == sym.SDYNIMPORT {
   295  			// Should have been handled in elfsetupplt
   296  			ldr.Errorf(s, "unexpected R_PPC64_REL24 for dyn import")
   297  		}
   298  
   299  		return true
   300  
   301  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC_REL32):
   302  		su := ldr.MakeSymbolUpdater(s)
   303  		su.SetRelocType(rIdx, objabi.R_PCREL)
   304  		su.SetRelocAdd(rIdx, r.Add()+4)
   305  
   306  		if targType == sym.SDYNIMPORT {
   307  			ldr.Errorf(s, "unexpected R_PPC_REL32 for dyn import")
   308  		}
   309  
   310  		return true
   311  
   312  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_ADDR64):
   313  		su := ldr.MakeSymbolUpdater(s)
   314  		su.SetRelocType(rIdx, objabi.R_ADDR)
   315  		if targType == sym.SDYNIMPORT {
   316  			// These happen in .toc sections
   317  			ld.Adddynsym(ldr, target, syms, targ)
   318  
   319  			rela := ldr.MakeSymbolUpdater(syms.Rela)
   320  			rela.AddAddrPlus(target.Arch, s, int64(r.Off()))
   321  			rela.AddUint64(target.Arch, elf.R_INFO(uint32(ldr.SymDynid(targ)), uint32(elf.R_PPC64_ADDR64)))
   322  			rela.AddUint64(target.Arch, uint64(r.Add()))
   323  			su.SetRelocType(rIdx, objabi.ElfRelocOffset) // ignore during relocsym
   324  		}
   325  		return true
   326  
   327  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16):
   328  		su := ldr.MakeSymbolUpdater(s)
   329  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   330  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_LO|sym.RV_CHECK_OVERFLOW)
   331  		return true
   332  
   333  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_LO):
   334  		su := ldr.MakeSymbolUpdater(s)
   335  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   336  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_LO)
   337  		return true
   338  
   339  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_HA):
   340  		su := ldr.MakeSymbolUpdater(s)
   341  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   342  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HA|sym.RV_CHECK_OVERFLOW)
   343  		return true
   344  
   345  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_HI):
   346  		su := ldr.MakeSymbolUpdater(s)
   347  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   348  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HI|sym.RV_CHECK_OVERFLOW)
   349  		return true
   350  
   351  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_DS):
   352  		su := ldr.MakeSymbolUpdater(s)
   353  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   354  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_DS|sym.RV_CHECK_OVERFLOW)
   355  		return true
   356  
   357  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_LO_DS):
   358  		su := ldr.MakeSymbolUpdater(s)
   359  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   360  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_DS)
   361  		return true
   362  
   363  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL16_LO):
   364  		su := ldr.MakeSymbolUpdater(s)
   365  		su.SetRelocType(rIdx, objabi.R_PCREL)
   366  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_LO)
   367  		su.SetRelocAdd(rIdx, r.Add()+2) // Compensate for relocation size of 2
   368  		return true
   369  
   370  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL16_HI):
   371  		su := ldr.MakeSymbolUpdater(s)
   372  		su.SetRelocType(rIdx, objabi.R_PCREL)
   373  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HI|sym.RV_CHECK_OVERFLOW)
   374  		su.SetRelocAdd(rIdx, r.Add()+2)
   375  		return true
   376  
   377  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL16_HA):
   378  		su := ldr.MakeSymbolUpdater(s)
   379  		su.SetRelocType(rIdx, objabi.R_PCREL)
   380  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HA|sym.RV_CHECK_OVERFLOW)
   381  		su.SetRelocAdd(rIdx, r.Add()+2)
   382  		return true
   383  	}
   384  
   385  	// Handle references to ELF symbols from our own object files.
   386  	if targType != sym.SDYNIMPORT {
   387  		return true
   388  	}
   389  
   390  	// TODO(austin): Translate our relocations to ELF
   391  
   392  	return false
   393  }
   394  
   395  func xcoffreloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool {
   396  	rs := r.Xsym
   397  
   398  	emitReloc := func(v uint16, off uint64) {
   399  		out.Write64(uint64(sectoff) + off)
   400  		out.Write32(uint32(ldr.SymDynid(rs)))
   401  		out.Write16(v)
   402  	}
   403  
   404  	var v uint16
   405  	switch r.Type {
   406  	default:
   407  		return false
   408  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   409  		v = ld.XCOFF_R_POS
   410  		if r.Size == 4 {
   411  			v |= 0x1F << 8
   412  		} else {
   413  			v |= 0x3F << 8
   414  		}
   415  		emitReloc(v, 0)
   416  	case objabi.R_ADDRPOWER_TOCREL:
   417  	case objabi.R_ADDRPOWER_TOCREL_DS:
   418  		emitReloc(ld.XCOFF_R_TOCU|(0x0F<<8), 2)
   419  		emitReloc(ld.XCOFF_R_TOCL|(0x0F<<8), 6)
   420  	case objabi.R_POWER_TLS_LE:
   421  		// This only supports 16b relocations.  It is fixed up in archreloc.
   422  		emitReloc(ld.XCOFF_R_TLS_LE|0x0F<<8, 2)
   423  	case objabi.R_CALLPOWER:
   424  		if r.Size != 4 {
   425  			return false
   426  		}
   427  		emitReloc(ld.XCOFF_R_RBR|0x19<<8, 0)
   428  	case objabi.R_XCOFFREF:
   429  		emitReloc(ld.XCOFF_R_REF|0x3F<<8, 0)
   430  	}
   431  	return true
   432  
   433  }
   434  
   435  func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool {
   436  	// Beware that bit0~bit15 start from the third byte of a instruction in Big-Endian machines.
   437  	rt := r.Type
   438  	if rt == objabi.R_ADDR || rt == objabi.R_POWER_TLS || rt == objabi.R_CALLPOWER {
   439  	} else {
   440  		if ctxt.Arch.ByteOrder == binary.BigEndian {
   441  			sectoff += 2
   442  		}
   443  	}
   444  	out.Write64(uint64(sectoff))
   445  
   446  	elfsym := ld.ElfSymForReloc(ctxt, r.Xsym)
   447  	switch rt {
   448  	default:
   449  		return false
   450  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   451  		switch r.Size {
   452  		case 4:
   453  			out.Write64(uint64(elf.R_PPC64_ADDR32) | uint64(elfsym)<<32)
   454  		case 8:
   455  			out.Write64(uint64(elf.R_PPC64_ADDR64) | uint64(elfsym)<<32)
   456  		default:
   457  			return false
   458  		}
   459  	case objabi.R_POWER_TLS:
   460  		out.Write64(uint64(elf.R_PPC64_TLS) | uint64(elfsym)<<32)
   461  	case objabi.R_POWER_TLS_LE:
   462  		out.Write64(uint64(elf.R_PPC64_TPREL16_HA) | uint64(elfsym)<<32)
   463  		out.Write64(uint64(r.Xadd))
   464  		out.Write64(uint64(sectoff + 4))
   465  		out.Write64(uint64(elf.R_PPC64_TPREL16_LO) | uint64(elfsym)<<32)
   466  	case objabi.R_POWER_TLS_IE:
   467  		out.Write64(uint64(elf.R_PPC64_GOT_TPREL16_HA) | uint64(elfsym)<<32)
   468  		out.Write64(uint64(r.Xadd))
   469  		out.Write64(uint64(sectoff + 4))
   470  		out.Write64(uint64(elf.R_PPC64_GOT_TPREL16_LO_DS) | uint64(elfsym)<<32)
   471  	case objabi.R_ADDRPOWER:
   472  		out.Write64(uint64(elf.R_PPC64_ADDR16_HA) | uint64(elfsym)<<32)
   473  		out.Write64(uint64(r.Xadd))
   474  		out.Write64(uint64(sectoff + 4))
   475  		out.Write64(uint64(elf.R_PPC64_ADDR16_LO) | uint64(elfsym)<<32)
   476  	case objabi.R_ADDRPOWER_DS:
   477  		out.Write64(uint64(elf.R_PPC64_ADDR16_HA) | uint64(elfsym)<<32)
   478  		out.Write64(uint64(r.Xadd))
   479  		out.Write64(uint64(sectoff + 4))
   480  		out.Write64(uint64(elf.R_PPC64_ADDR16_LO_DS) | uint64(elfsym)<<32)
   481  	case objabi.R_ADDRPOWER_GOT:
   482  		out.Write64(uint64(elf.R_PPC64_GOT16_HA) | uint64(elfsym)<<32)
   483  		out.Write64(uint64(r.Xadd))
   484  		out.Write64(uint64(sectoff + 4))
   485  		out.Write64(uint64(elf.R_PPC64_GOT16_LO_DS) | uint64(elfsym)<<32)
   486  	case objabi.R_ADDRPOWER_PCREL:
   487  		out.Write64(uint64(elf.R_PPC64_REL16_HA) | uint64(elfsym)<<32)
   488  		out.Write64(uint64(r.Xadd))
   489  		out.Write64(uint64(sectoff + 4))
   490  		out.Write64(uint64(elf.R_PPC64_REL16_LO) | uint64(elfsym)<<32)
   491  		r.Xadd += 4
   492  	case objabi.R_ADDRPOWER_TOCREL:
   493  		out.Write64(uint64(elf.R_PPC64_TOC16_HA) | uint64(elfsym)<<32)
   494  		out.Write64(uint64(r.Xadd))
   495  		out.Write64(uint64(sectoff + 4))
   496  		out.Write64(uint64(elf.R_PPC64_TOC16_LO) | uint64(elfsym)<<32)
   497  	case objabi.R_ADDRPOWER_TOCREL_DS:
   498  		out.Write64(uint64(elf.R_PPC64_TOC16_HA) | uint64(elfsym)<<32)
   499  		out.Write64(uint64(r.Xadd))
   500  		out.Write64(uint64(sectoff + 4))
   501  		out.Write64(uint64(elf.R_PPC64_TOC16_LO_DS) | uint64(elfsym)<<32)
   502  	case objabi.R_CALLPOWER:
   503  		if r.Size != 4 {
   504  			return false
   505  		}
   506  		out.Write64(uint64(elf.R_PPC64_REL24) | uint64(elfsym)<<32)
   507  
   508  	}
   509  	out.Write64(uint64(r.Xadd))
   510  
   511  	return true
   512  }
   513  
   514  func elfsetupplt(ctxt *ld.Link, plt, got *loader.SymbolBuilder, dynamic loader.Sym) {
   515  	if plt.Size() == 0 {
   516  		// The dynamic linker stores the address of the
   517  		// dynamic resolver and the DSO identifier in the two
   518  		// doublewords at the beginning of the .plt section
   519  		// before the PLT array. Reserve space for these.
   520  		plt.SetSize(16)
   521  	}
   522  }
   523  
   524  func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool {
   525  	return false
   526  }
   527  
   528  // Return the value of .TOC. for symbol s
   529  func symtoc(ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) int64 {
   530  	v := ldr.SymVersion(s)
   531  	if out := ldr.OuterSym(s); out != 0 {
   532  		v = ldr.SymVersion(out)
   533  	}
   534  
   535  	toc := syms.DotTOC[v]
   536  	if toc == 0 {
   537  		ldr.Errorf(s, "TOC-relative relocation in object without .TOC.")
   538  		return 0
   539  	}
   540  
   541  	return ldr.SymValue(toc)
   542  }
   543  
   544  // archreloctoc relocates a TOC relative symbol.
   545  // If the symbol pointed by this TOC relative symbol is in .data or .bss, the
   546  // default load instruction can be changed to an addi instruction and the
   547  // symbol address can be used directly.
   548  // This code is for AIX only.
   549  func archreloctoc(ldr *loader.Loader, target *ld.Target, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) int64 {
   550  	rs := ldr.ResolveABIAlias(r.Sym())
   551  	if target.IsLinux() {
   552  		ldr.Errorf(s, "archrelocaddr called for %s relocation\n", ldr.SymName(rs))
   553  	}
   554  	var o1, o2 uint32
   555  
   556  	o1 = uint32(val >> 32)
   557  	o2 = uint32(val)
   558  
   559  	if !strings.HasPrefix(ldr.SymName(rs), "TOC.") {
   560  		ldr.Errorf(s, "archreloctoc called for a symbol without TOC anchor")
   561  	}
   562  	var t int64
   563  	useAddi := false
   564  	relocs := ldr.Relocs(rs)
   565  	tarSym := ldr.ResolveABIAlias(relocs.At(0).Sym())
   566  
   567  	if target.IsInternal() && tarSym != 0 && ldr.AttrReachable(tarSym) && ldr.SymSect(tarSym).Seg == &ld.Segdata {
   568  		t = ldr.SymValue(tarSym) + r.Add() - ldr.SymValue(syms.TOC)
   569  		// change ld to addi in the second instruction
   570  		o2 = (o2 & 0x03FF0000) | 0xE<<26
   571  		useAddi = true
   572  	} else {
   573  		t = ldr.SymValue(rs) + r.Add() - ldr.SymValue(syms.TOC)
   574  	}
   575  
   576  	if t != int64(int32(t)) {
   577  		ldr.Errorf(s, "TOC relocation for %s is too big to relocate %s: 0x%x", ldr.SymName(s), rs, t)
   578  	}
   579  
   580  	if t&0x8000 != 0 {
   581  		t += 0x10000
   582  	}
   583  
   584  	o1 |= uint32((t >> 16) & 0xFFFF)
   585  
   586  	switch r.Type() {
   587  	case objabi.R_ADDRPOWER_TOCREL_DS:
   588  		if useAddi {
   589  			o2 |= uint32(t) & 0xFFFF
   590  		} else {
   591  			if t&3 != 0 {
   592  				ldr.Errorf(s, "bad DS reloc for %s: %d", ldr.SymName(s), ldr.SymValue(rs))
   593  			}
   594  			o2 |= uint32(t) & 0xFFFC
   595  		}
   596  	default:
   597  		return -1
   598  	}
   599  
   600  	return int64(o1)<<32 | int64(o2)
   601  }
   602  
   603  // archrelocaddr relocates a symbol address.
   604  // This code is for AIX only.
   605  func archrelocaddr(ldr *loader.Loader, target *ld.Target, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) int64 {
   606  	rs := ldr.ResolveABIAlias(r.Sym())
   607  	if target.IsAIX() {
   608  		ldr.Errorf(s, "archrelocaddr called for %s relocation\n", ldr.SymName(rs))
   609  	}
   610  	var o1, o2 uint32
   611  	if target.IsBigEndian() {
   612  		o1 = uint32(val >> 32)
   613  		o2 = uint32(val)
   614  	} else {
   615  		o1 = uint32(val)
   616  		o2 = uint32(val >> 32)
   617  	}
   618  
   619  	// We are spreading a 31-bit address across two instructions, putting the
   620  	// high (adjusted) part in the low 16 bits of the first instruction and the
   621  	// low part in the low 16 bits of the second instruction, or, in the DS case,
   622  	// bits 15-2 (inclusive) of the address into bits 15-2 of the second
   623  	// instruction (it is an error in this case if the low 2 bits of the address
   624  	// are non-zero).
   625  
   626  	t := ldr.SymAddr(rs) + r.Add()
   627  	if t < 0 || t >= 1<<31 {
   628  		ldr.Errorf(s, "relocation for %s is too big (>=2G): 0x%x", ldr.SymName(s), ldr.SymValue(rs))
   629  	}
   630  	if t&0x8000 != 0 {
   631  		t += 0x10000
   632  	}
   633  
   634  	switch r.Type() {
   635  	case objabi.R_ADDRPOWER:
   636  		o1 |= (uint32(t) >> 16) & 0xffff
   637  		o2 |= uint32(t) & 0xffff
   638  	case objabi.R_ADDRPOWER_DS:
   639  		o1 |= (uint32(t) >> 16) & 0xffff
   640  		if t&3 != 0 {
   641  			ldr.Errorf(s, "bad DS reloc for %s: %d", ldr.SymName(s), ldr.SymValue(rs))
   642  		}
   643  		o2 |= uint32(t) & 0xfffc
   644  	default:
   645  		return -1
   646  	}
   647  
   648  	if target.IsBigEndian() {
   649  		return int64(o1)<<32 | int64(o2)
   650  	}
   651  	return int64(o2)<<32 | int64(o1)
   652  }
   653  
   654  // Determine if the code was compiled so that the TOC register R2 is initialized and maintained
   655  func r2Valid(ctxt *ld.Link) bool {
   656  	switch ctxt.BuildMode {
   657  	case ld.BuildModeCArchive, ld.BuildModeCShared, ld.BuildModePIE, ld.BuildModeShared, ld.BuildModePlugin:
   658  		return true
   659  	}
   660  	// -linkshared option
   661  	return ctxt.IsSharedGoLink()
   662  }
   663  
   664  // resolve direct jump relocation r in s, and add trampoline if necessary
   665  func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) {
   666  
   667  	// Trampolines are created if the branch offset is too large and the linker cannot insert a call stub to handle it.
   668  	// For internal linking, trampolines are always created for long calls.
   669  	// For external linking, the linker can insert a call stub to handle a long call, but depends on having the TOC address in
   670  	// r2.  For those build modes with external linking where the TOC address is not maintained in r2, trampolines must be created.
   671  	if ctxt.IsExternal() && r2Valid(ctxt) {
   672  		// No trampolines needed since r2 contains the TOC
   673  		return
   674  	}
   675  
   676  	relocs := ldr.Relocs(s)
   677  	r := relocs.At(ri)
   678  	var t int64
   679  	// ldr.SymValue(rs) == 0 indicates a cross-package jump to a function that is not yet
   680  	// laid out. Conservatively use a trampoline. This should be rare, as we lay out packages
   681  	// in dependency order.
   682  	if ldr.SymValue(rs) != 0 {
   683  		t = ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   684  	}
   685  	switch r.Type() {
   686  	case objabi.R_CALLPOWER:
   687  
   688  		// If branch offset is too far then create a trampoline.
   689  
   690  		if (ctxt.IsExternal() && ldr.SymSect(s) != ldr.SymSect(rs)) || (ctxt.IsInternal() && int64(int32(t<<6)>>6) != t) || ldr.SymValue(rs) == 0 || (*ld.FlagDebugTramp > 1 && ldr.SymPkg(s) != ldr.SymPkg(rs)) {
   691  			var tramp loader.Sym
   692  			for i := 0; ; i++ {
   693  
   694  				// Using r.Add as part of the name is significant in functions like duffzero where the call
   695  				// target is at some offset within the function.  Calls to duff+8 and duff+256 must appear as
   696  				// distinct trampolines.
   697  
   698  				oName := ldr.SymName(rs)
   699  				name := oName
   700  				if r.Add() == 0 {
   701  					name += fmt.Sprintf("-tramp%d", i)
   702  				} else {
   703  					name += fmt.Sprintf("%+x-tramp%d", r.Add(), i)
   704  				}
   705  
   706  				// Look up the trampoline in case it already exists
   707  
   708  				tramp = ldr.LookupOrCreateSym(name, int(ldr.SymVersion(rs)))
   709  				if oName == "runtime.deferreturn" {
   710  					ldr.SetIsDeferReturnTramp(tramp, true)
   711  				}
   712  				if ldr.SymValue(tramp) == 0 {
   713  					break
   714  				}
   715  
   716  				t = ldr.SymValue(tramp) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   717  
   718  				// With internal linking, the trampoline can be used if it is not too far.
   719  				// With external linking, the trampoline must be in this section for it to be reused.
   720  				if (ctxt.IsInternal() && int64(int32(t<<6)>>6) == t) || (ctxt.IsExternal() && ldr.SymSect(s) == ldr.SymSect(tramp)) {
   721  					break
   722  				}
   723  			}
   724  			if ldr.SymType(tramp) == 0 {
   725  				if r2Valid(ctxt) {
   726  					// Should have returned for above cases
   727  					ctxt.Errorf(s, "unexpected trampoline for shared or dynamic linking")
   728  				} else {
   729  					trampb := ldr.MakeSymbolUpdater(tramp)
   730  					ctxt.AddTramp(trampb)
   731  					gentramp(ctxt, ldr, trampb, rs, r.Add())
   732  				}
   733  			}
   734  			sb := ldr.MakeSymbolUpdater(s)
   735  			relocs := sb.Relocs()
   736  			r := relocs.At(ri)
   737  			r.SetSym(tramp)
   738  			r.SetAdd(0) // This was folded into the trampoline target address
   739  		}
   740  	default:
   741  		ctxt.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
   742  	}
   743  }
   744  
   745  func gentramp(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) {
   746  	tramp.SetSize(16) // 4 instructions
   747  	P := make([]byte, tramp.Size())
   748  	t := ldr.SymValue(target) + offset
   749  	var o1, o2 uint32
   750  
   751  	if ctxt.IsAIX() {
   752  		// On AIX, the address is retrieved with a TOC symbol.
   753  		// For internal linking, the "Linux" way might still be used.
   754  		// However, all text symbols are accessed with a TOC symbol as
   755  		// text relocations aren't supposed to be possible.
   756  		// So, keep using the external linking way to be more AIX friendly.
   757  		o1 = uint32(0x3fe20000) // lis r2, toctargetaddr hi
   758  		o2 = uint32(0xebff0000) // ld r31, toctargetaddr lo
   759  
   760  		toctramp := ldr.CreateSymForUpdate("TOC."+ldr.SymName(tramp.Sym()), 0)
   761  		toctramp.SetType(sym.SXCOFFTOC)
   762  		toctramp.AddAddrPlus(ctxt.Arch, target, offset)
   763  
   764  		r, _ := tramp.AddRel(objabi.R_ADDRPOWER_TOCREL_DS)
   765  		r.SetOff(0)
   766  		r.SetSiz(8) // generates 2 relocations: HA + LO
   767  		r.SetSym(toctramp.Sym())
   768  	} else {
   769  		// Used for default build mode for an executable
   770  		// Address of the call target is generated using
   771  		// relocation and doesn't depend on r2 (TOC).
   772  		o1 = uint32(0x3fe00000) // lis r31,targetaddr hi
   773  		o2 = uint32(0x3bff0000) // addi r31,targetaddr lo
   774  
   775  		// With external linking, the target address must be
   776  		// relocated using LO and HA
   777  		if ctxt.IsExternal() || ldr.SymValue(target) == 0 {
   778  			r, _ := tramp.AddRel(objabi.R_ADDRPOWER)
   779  			r.SetOff(0)
   780  			r.SetSiz(8) // generates 2 relocations: HA + LO
   781  			r.SetSym(target)
   782  			r.SetAdd(offset)
   783  		} else {
   784  			// adjustment needed if lo has sign bit set
   785  			// when using addi to compute address
   786  			val := uint32((t & 0xffff0000) >> 16)
   787  			if t&0x8000 != 0 {
   788  				val += 1
   789  			}
   790  			o1 |= val                // hi part of addr
   791  			o2 |= uint32(t & 0xffff) // lo part of addr
   792  		}
   793  	}
   794  
   795  	o3 := uint32(0x7fe903a6) // mtctr r31
   796  	o4 := uint32(0x4e800420) // bctr
   797  	ctxt.Arch.ByteOrder.PutUint32(P, o1)
   798  	ctxt.Arch.ByteOrder.PutUint32(P[4:], o2)
   799  	ctxt.Arch.ByteOrder.PutUint32(P[8:], o3)
   800  	ctxt.Arch.ByteOrder.PutUint32(P[12:], o4)
   801  	tramp.SetData(P)
   802  }
   803  
   804  func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) (relocatedOffset int64, nExtReloc int, ok bool) {
   805  	rs := ldr.ResolveABIAlias(r.Sym())
   806  	if target.IsExternal() {
   807  		// On AIX, relocations (except TLS ones) must be also done to the
   808  		// value with the current addresses.
   809  		switch rt := r.Type(); rt {
   810  		default:
   811  			if !target.IsAIX() {
   812  				return val, nExtReloc, false
   813  			}
   814  		case objabi.R_POWER_TLS:
   815  			nExtReloc = 1
   816  			return val, nExtReloc, true
   817  		case objabi.R_POWER_TLS_LE, objabi.R_POWER_TLS_IE:
   818  			if target.IsAIX() && rt == objabi.R_POWER_TLS_LE {
   819  				// Fixup val, an addis/addi pair of instructions, which generate a 32b displacement
   820  				// from the threadpointer (R13), into a 16b relocation. XCOFF only supports 16b
   821  				// TLS LE relocations. Likewise, verify this is an addis/addi sequence.
   822  				const expectedOpcodes = 0x3C00000038000000
   823  				const expectedOpmasks = 0xFC000000FC000000
   824  				if uint64(val)&expectedOpmasks != expectedOpcodes {
   825  					ldr.Errorf(s, "relocation for %s+%d is not an addis/addi pair: %16x", ldr.SymName(rs), r.Off(), uint64(val))
   826  				}
   827  				nval := (int64(uint32(0x380d0000)) | val&0x03e00000) << 32 // addi rX, r13, $0
   828  				nval |= int64(0x60000000)                                  // nop
   829  				val = nval
   830  				nExtReloc = 1
   831  			} else {
   832  				nExtReloc = 2
   833  			}
   834  			return val, nExtReloc, true
   835  		case objabi.R_ADDRPOWER,
   836  			objabi.R_ADDRPOWER_DS,
   837  			objabi.R_ADDRPOWER_TOCREL,
   838  			objabi.R_ADDRPOWER_TOCREL_DS,
   839  			objabi.R_ADDRPOWER_GOT,
   840  			objabi.R_ADDRPOWER_PCREL:
   841  			nExtReloc = 2 // need two ELF relocations, see elfreloc1
   842  			if !target.IsAIX() {
   843  				return val, nExtReloc, true
   844  			}
   845  		case objabi.R_CALLPOWER:
   846  			nExtReloc = 1
   847  			if !target.IsAIX() {
   848  				return val, nExtReloc, true
   849  			}
   850  		}
   851  	}
   852  
   853  	switch r.Type() {
   854  	case objabi.R_ADDRPOWER_TOCREL, objabi.R_ADDRPOWER_TOCREL_DS:
   855  		return archreloctoc(ldr, target, syms, r, s, val), nExtReloc, true
   856  	case objabi.R_ADDRPOWER, objabi.R_ADDRPOWER_DS:
   857  		return archrelocaddr(ldr, target, syms, r, s, val), nExtReloc, true
   858  	case objabi.R_CALLPOWER:
   859  		// Bits 6 through 29 = (S + A - P) >> 2
   860  
   861  		t := ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   862  
   863  		if t&3 != 0 {
   864  			ldr.Errorf(s, "relocation for %s+%d is not aligned: %d", ldr.SymName(rs), r.Off(), t)
   865  		}
   866  		// If branch offset is too far then create a trampoline.
   867  
   868  		if int64(int32(t<<6)>>6) != t {
   869  			ldr.Errorf(s, "direct call too far: %s %x", ldr.SymName(rs), t)
   870  		}
   871  		return val | int64(uint32(t)&^0xfc000003), nExtReloc, true
   872  	case objabi.R_POWER_TOC: // S + A - .TOC.
   873  		return ldr.SymValue(rs) + r.Add() - symtoc(ldr, syms, s), nExtReloc, true
   874  
   875  	case objabi.R_POWER_TLS_LE:
   876  		// The thread pointer points 0x7000 bytes after the start of the
   877  		// thread local storage area as documented in section "3.7.2 TLS
   878  		// Runtime Handling" of "Power Architecture 64-Bit ELF V2 ABI
   879  		// Specification".
   880  		v := ldr.SymValue(rs) - 0x7000
   881  		if target.IsAIX() {
   882  			// On AIX, the thread pointer points 0x7800 bytes after
   883  			// the TLS.
   884  			v -= 0x800
   885  		}
   886  
   887  		var o1, o2 uint32
   888  		if int64(int32(v)) != v {
   889  			ldr.Errorf(s, "TLS offset out of range %d", v)
   890  		}
   891  		if target.IsBigEndian() {
   892  			o1 = uint32(val >> 32)
   893  			o2 = uint32(val)
   894  		} else {
   895  			o1 = uint32(val)
   896  			o2 = uint32(val >> 32)
   897  		}
   898  
   899  		o1 |= uint32(((v + 0x8000) >> 16) & 0xFFFF)
   900  		o2 |= uint32(v & 0xFFFF)
   901  
   902  		if target.IsBigEndian() {
   903  			return int64(o1)<<32 | int64(o2), nExtReloc, true
   904  		}
   905  		return int64(o2)<<32 | int64(o1), nExtReloc, true
   906  	}
   907  
   908  	return val, nExtReloc, false
   909  }
   910  
   911  func archrelocvariant(target *ld.Target, ldr *loader.Loader, r loader.Reloc, rv sym.RelocVariant, s loader.Sym, t int64, p []byte) (relocatedOffset int64) {
   912  	rs := ldr.ResolveABIAlias(r.Sym())
   913  	switch rv & sym.RV_TYPE_MASK {
   914  	default:
   915  		ldr.Errorf(s, "unexpected relocation variant %d", rv)
   916  		fallthrough
   917  
   918  	case sym.RV_NONE:
   919  		return t
   920  
   921  	case sym.RV_POWER_LO:
   922  		if rv&sym.RV_CHECK_OVERFLOW != 0 {
   923  			// Whether to check for signed or unsigned
   924  			// overflow depends on the instruction
   925  			var o1 uint32
   926  			if target.IsBigEndian() {
   927  				o1 = binary.BigEndian.Uint32(p[r.Off()-2:])
   928  
   929  			} else {
   930  				o1 = binary.LittleEndian.Uint32(p[r.Off():])
   931  			}
   932  			switch o1 >> 26 {
   933  			case 24, // ori
   934  				26, // xori
   935  				28: // andi
   936  				if t>>16 != 0 {
   937  					goto overflow
   938  				}
   939  
   940  			default:
   941  				if int64(int16(t)) != t {
   942  					goto overflow
   943  				}
   944  			}
   945  		}
   946  
   947  		return int64(int16(t))
   948  
   949  	case sym.RV_POWER_HA:
   950  		t += 0x8000
   951  		fallthrough
   952  
   953  		// Fallthrough
   954  	case sym.RV_POWER_HI:
   955  		t >>= 16
   956  
   957  		if rv&sym.RV_CHECK_OVERFLOW != 0 {
   958  			// Whether to check for signed or unsigned
   959  			// overflow depends on the instruction
   960  			var o1 uint32
   961  			if target.IsBigEndian() {
   962  				o1 = binary.BigEndian.Uint32(p[r.Off()-2:])
   963  			} else {
   964  				o1 = binary.LittleEndian.Uint32(p[r.Off():])
   965  			}
   966  			switch o1 >> 26 {
   967  			case 25, // oris
   968  				27, // xoris
   969  				29: // andis
   970  				if t>>16 != 0 {
   971  					goto overflow
   972  				}
   973  
   974  			default:
   975  				if int64(int16(t)) != t {
   976  					goto overflow
   977  				}
   978  			}
   979  		}
   980  
   981  		return int64(int16(t))
   982  
   983  	case sym.RV_POWER_DS:
   984  		var o1 uint32
   985  		if target.IsBigEndian() {
   986  			o1 = uint32(binary.BigEndian.Uint16(p[r.Off():]))
   987  		} else {
   988  			o1 = uint32(binary.LittleEndian.Uint16(p[r.Off():]))
   989  		}
   990  		if t&3 != 0 {
   991  			ldr.Errorf(s, "relocation for %s+%d is not aligned: %d", ldr.SymName(rs), r.Off(), t)
   992  		}
   993  		if (rv&sym.RV_CHECK_OVERFLOW != 0) && int64(int16(t)) != t {
   994  			goto overflow
   995  		}
   996  		return int64(o1)&0x3 | int64(int16(t))
   997  	}
   998  
   999  overflow:
  1000  	ldr.Errorf(s, "relocation for %s+%d is too big: %d", ldr.SymName(rs), r.Off(), t)
  1001  	return t
  1002  }
  1003  
  1004  func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) {
  1005  	switch r.Type() {
  1006  	case objabi.R_POWER_TLS, objabi.R_POWER_TLS_LE, objabi.R_POWER_TLS_IE, objabi.R_CALLPOWER:
  1007  		return ld.ExtrelocSimple(ldr, r), true
  1008  	case objabi.R_ADDRPOWER,
  1009  		objabi.R_ADDRPOWER_DS,
  1010  		objabi.R_ADDRPOWER_TOCREL,
  1011  		objabi.R_ADDRPOWER_TOCREL_DS,
  1012  		objabi.R_ADDRPOWER_GOT,
  1013  		objabi.R_ADDRPOWER_PCREL:
  1014  		return ld.ExtrelocViaOuterSym(ldr, r, s), true
  1015  	}
  1016  	return loader.ExtReloc{}, false
  1017  }
  1018  
  1019  func addpltsym(ctxt *ld.Link, ldr *loader.Loader, s loader.Sym) {
  1020  	if ldr.SymPlt(s) >= 0 {
  1021  		return
  1022  	}
  1023  
  1024  	ld.Adddynsym(ldr, &ctxt.Target, &ctxt.ArchSyms, s)
  1025  
  1026  	if ctxt.IsELF {
  1027  		plt := ldr.MakeSymbolUpdater(ctxt.PLT)
  1028  		rela := ldr.MakeSymbolUpdater(ctxt.RelaPLT)
  1029  		if plt.Size() == 0 {
  1030  			panic("plt is not set up")
  1031  		}
  1032  
  1033  		// Create the glink resolver if necessary
  1034  		glink := ensureglinkresolver(ctxt, ldr)
  1035  
  1036  		// Write symbol resolver stub (just a branch to the
  1037  		// glink resolver stub)
  1038  		rel, _ := glink.AddRel(objabi.R_CALLPOWER)
  1039  		rel.SetOff(int32(glink.Size()))
  1040  		rel.SetSiz(4)
  1041  		rel.SetSym(glink.Sym())
  1042  		glink.AddUint32(ctxt.Arch, 0x48000000) // b .glink
  1043  
  1044  		// In the ppc64 ABI, the dynamic linker is responsible
  1045  		// for writing the entire PLT.  We just need to
  1046  		// reserve 8 bytes for each PLT entry and generate a
  1047  		// JMP_SLOT dynamic relocation for it.
  1048  		//
  1049  		// TODO(austin): ABI v1 is different
  1050  		ldr.SetPlt(s, int32(plt.Size()))
  1051  
  1052  		plt.Grow(plt.Size() + 8)
  1053  		plt.SetSize(plt.Size() + 8)
  1054  
  1055  		rela.AddAddrPlus(ctxt.Arch, plt.Sym(), int64(ldr.SymPlt(s)))
  1056  		rela.AddUint64(ctxt.Arch, elf.R_INFO(uint32(ldr.SymDynid(s)), uint32(elf.R_PPC64_JMP_SLOT)))
  1057  		rela.AddUint64(ctxt.Arch, 0)
  1058  	} else {
  1059  		ctxt.Errorf(s, "addpltsym: unsupported binary format")
  1060  	}
  1061  }
  1062  
  1063  // Generate the glink resolver stub if necessary and return the .glink section
  1064  func ensureglinkresolver(ctxt *ld.Link, ldr *loader.Loader) *loader.SymbolBuilder {
  1065  	glink := ldr.CreateSymForUpdate(".glink", 0)
  1066  	if glink.Size() != 0 {
  1067  		return glink
  1068  	}
  1069  
  1070  	// This is essentially the resolver from the ppc64 ELF ABI.
  1071  	// At entry, r12 holds the address of the symbol resolver stub
  1072  	// for the target routine and the argument registers hold the
  1073  	// arguments for the target routine.
  1074  	//
  1075  	// This stub is PIC, so first get the PC of label 1 into r11.
  1076  	// Other things will be relative to this.
  1077  	glink.AddUint32(ctxt.Arch, 0x7c0802a6) // mflr r0
  1078  	glink.AddUint32(ctxt.Arch, 0x429f0005) // bcl 20,31,1f
  1079  	glink.AddUint32(ctxt.Arch, 0x7d6802a6) // 1: mflr r11
  1080  	glink.AddUint32(ctxt.Arch, 0x7c0803a6) // mtlf r0
  1081  
  1082  	// Compute the .plt array index from the entry point address.
  1083  	// Because this is PIC, everything is relative to label 1b (in
  1084  	// r11):
  1085  	//   r0 = ((r12 - r11) - (res_0 - r11)) / 4 = (r12 - res_0) / 4
  1086  	glink.AddUint32(ctxt.Arch, 0x3800ffd0) // li r0,-(res_0-1b)=-48
  1087  	glink.AddUint32(ctxt.Arch, 0x7c006214) // add r0,r0,r12
  1088  	glink.AddUint32(ctxt.Arch, 0x7c0b0050) // sub r0,r0,r11
  1089  	glink.AddUint32(ctxt.Arch, 0x7800f082) // srdi r0,r0,2
  1090  
  1091  	// r11 = address of the first byte of the PLT
  1092  	r, _ := glink.AddRel(objabi.R_ADDRPOWER)
  1093  	r.SetSym(ctxt.PLT)
  1094  	r.SetSiz(8)
  1095  	r.SetOff(int32(glink.Size()))
  1096  	r.SetAdd(0)
  1097  	glink.AddUint32(ctxt.Arch, 0x3d600000) // addis r11,0,.plt@ha
  1098  	glink.AddUint32(ctxt.Arch, 0x396b0000) // addi r11,r11,.plt@l
  1099  
  1100  	// Load r12 = dynamic resolver address and r11 = DSO
  1101  	// identifier from the first two doublewords of the PLT.
  1102  	glink.AddUint32(ctxt.Arch, 0xe98b0000) // ld r12,0(r11)
  1103  	glink.AddUint32(ctxt.Arch, 0xe96b0008) // ld r11,8(r11)
  1104  
  1105  	// Jump to the dynamic resolver
  1106  	glink.AddUint32(ctxt.Arch, 0x7d8903a6) // mtctr r12
  1107  	glink.AddUint32(ctxt.Arch, 0x4e800420) // bctr
  1108  
  1109  	// The symbol resolvers must immediately follow.
  1110  	//   res_0:
  1111  
  1112  	// Add DT_PPC64_GLINK .dynamic entry, which points to 32 bytes
  1113  	// before the first symbol resolver stub.
  1114  	du := ldr.MakeSymbolUpdater(ctxt.Dynamic)
  1115  	ld.Elfwritedynentsymplus(ctxt, du, elf.DT_PPC64_GLINK, glink.Sym(), glink.Size()-32)
  1116  
  1117  	return glink
  1118  }
  1119  

View as plain text