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

View as plain text