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

     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  		} else if target.IsPIE() && target.IsInternal() {
   325  			// For internal linking PIE, this R_ADDR relocation cannot
   326  			// be resolved statically. We need to generate a dynamic
   327  			// relocation. Let the code below handle it.
   328  			break
   329  		}
   330  		return true
   331  
   332  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16):
   333  		su := ldr.MakeSymbolUpdater(s)
   334  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   335  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_LO|sym.RV_CHECK_OVERFLOW)
   336  		return true
   337  
   338  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_LO):
   339  		su := ldr.MakeSymbolUpdater(s)
   340  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   341  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_LO)
   342  		return true
   343  
   344  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_HA):
   345  		su := ldr.MakeSymbolUpdater(s)
   346  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   347  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HA|sym.RV_CHECK_OVERFLOW)
   348  		return true
   349  
   350  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_HI):
   351  		su := ldr.MakeSymbolUpdater(s)
   352  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   353  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HI|sym.RV_CHECK_OVERFLOW)
   354  		return true
   355  
   356  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_DS):
   357  		su := ldr.MakeSymbolUpdater(s)
   358  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   359  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_DS|sym.RV_CHECK_OVERFLOW)
   360  		return true
   361  
   362  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_TOC16_LO_DS):
   363  		su := ldr.MakeSymbolUpdater(s)
   364  		su.SetRelocType(rIdx, objabi.R_POWER_TOC)
   365  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_DS)
   366  		return true
   367  
   368  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL16_LO):
   369  		su := ldr.MakeSymbolUpdater(s)
   370  		su.SetRelocType(rIdx, objabi.R_PCREL)
   371  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_LO)
   372  		su.SetRelocAdd(rIdx, r.Add()+2) // Compensate for relocation size of 2
   373  		return true
   374  
   375  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL16_HI):
   376  		su := ldr.MakeSymbolUpdater(s)
   377  		su.SetRelocType(rIdx, objabi.R_PCREL)
   378  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HI|sym.RV_CHECK_OVERFLOW)
   379  		su.SetRelocAdd(rIdx, r.Add()+2)
   380  		return true
   381  
   382  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_PPC64_REL16_HA):
   383  		su := ldr.MakeSymbolUpdater(s)
   384  		su.SetRelocType(rIdx, objabi.R_PCREL)
   385  		ldr.SetRelocVariant(s, rIdx, sym.RV_POWER_HA|sym.RV_CHECK_OVERFLOW)
   386  		su.SetRelocAdd(rIdx, r.Add()+2)
   387  		return true
   388  	}
   389  
   390  	// Handle references to ELF symbols from our own object files.
   391  	relocs := ldr.Relocs(s)
   392  	r = relocs.At(rIdx)
   393  
   394  	switch r.Type() {
   395  	case objabi.R_ADDR:
   396  		if ldr.SymType(s) == sym.STEXT {
   397  			log.Fatalf("R_ADDR relocation in text symbol %s is unsupported\n", ldr.SymName(s))
   398  		}
   399  		if target.IsPIE() && target.IsInternal() {
   400  			// When internally linking, generate dynamic relocations
   401  			// for all typical R_ADDR relocations. The exception
   402  			// are those R_ADDR that are created as part of generating
   403  			// the dynamic relocations and must be resolved statically.
   404  			//
   405  			// There are three phases relevant to understanding this:
   406  			//
   407  			//	dodata()  // we are here
   408  			//	address() // symbol address assignment
   409  			//	reloc()   // resolution of static R_ADDR relocs
   410  			//
   411  			// At this point symbol addresses have not been
   412  			// assigned yet (as the final size of the .rela section
   413  			// will affect the addresses), and so we cannot write
   414  			// the Elf64_Rela.r_offset now. Instead we delay it
   415  			// until after the 'address' phase of the linker is
   416  			// complete. We do this via Addaddrplus, which creates
   417  			// a new R_ADDR relocation which will be resolved in
   418  			// the 'reloc' phase.
   419  			//
   420  			// These synthetic static R_ADDR relocs must be skipped
   421  			// now, or else we will be caught in an infinite loop
   422  			// of generating synthetic relocs for our synthetic
   423  			// relocs.
   424  			//
   425  			// Furthermore, the rela sections contain dynamic
   426  			// relocations with R_ADDR relocations on
   427  			// Elf64_Rela.r_offset. This field should contain the
   428  			// symbol offset as determined by reloc(), not the
   429  			// final dynamically linked address as a dynamic
   430  			// relocation would provide.
   431  			switch ldr.SymName(s) {
   432  			case ".dynsym", ".rela", ".rela.plt", ".got.plt", ".dynamic":
   433  				return false
   434  			}
   435  		} else {
   436  			// Either internally linking a static executable,
   437  			// in which case we can resolve these relocations
   438  			// statically in the 'reloc' phase, or externally
   439  			// linking, in which case the relocation will be
   440  			// prepared in the 'reloc' phase and passed to the
   441  			// external linker in the 'asmb' phase.
   442  			if ldr.SymType(s) != sym.SDATA && ldr.SymType(s) != sym.SRODATA {
   443  				break
   444  			}
   445  		}
   446  		// Generate R_PPC64_RELATIVE relocations for best
   447  		// efficiency in the dynamic linker.
   448  		//
   449  		// As noted above, symbol addresses have not been
   450  		// assigned yet, so we can't generate the final reloc
   451  		// entry yet. We ultimately want:
   452  		//
   453  		// r_offset = s + r.Off
   454  		// r_info = R_PPC64_RELATIVE
   455  		// r_addend = targ + r.Add
   456  		//
   457  		// The dynamic linker will set *offset = base address +
   458  		// addend.
   459  		//
   460  		// AddAddrPlus is used for r_offset and r_addend to
   461  		// generate new R_ADDR relocations that will update
   462  		// these fields in the 'reloc' phase.
   463  		rela := ldr.MakeSymbolUpdater(syms.Rela)
   464  		rela.AddAddrPlus(target.Arch, s, int64(r.Off()))
   465  		if r.Siz() == 8 {
   466  			rela.AddUint64(target.Arch, elf.R_INFO(0, uint32(elf.R_PPC64_RELATIVE)))
   467  		} else {
   468  			ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ))
   469  		}
   470  		rela.AddAddrPlus(target.Arch, targ, int64(r.Add()))
   471  
   472  		// Not mark r done here. So we still apply it statically,
   473  		// so in the file content we'll also have the right offset
   474  		// to the relocation target. So it can be examined statically
   475  		// (e.g. go version).
   476  		return true
   477  	}
   478  
   479  	return false
   480  }
   481  
   482  func xcoffreloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool {
   483  	rs := r.Xsym
   484  
   485  	emitReloc := func(v uint16, off uint64) {
   486  		out.Write64(uint64(sectoff) + off)
   487  		out.Write32(uint32(ldr.SymDynid(rs)))
   488  		out.Write16(v)
   489  	}
   490  
   491  	var v uint16
   492  	switch r.Type {
   493  	default:
   494  		return false
   495  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   496  		v = ld.XCOFF_R_POS
   497  		if r.Size == 4 {
   498  			v |= 0x1F << 8
   499  		} else {
   500  			v |= 0x3F << 8
   501  		}
   502  		emitReloc(v, 0)
   503  	case objabi.R_ADDRPOWER_TOCREL:
   504  	case objabi.R_ADDRPOWER_TOCREL_DS:
   505  		emitReloc(ld.XCOFF_R_TOCU|(0x0F<<8), 2)
   506  		emitReloc(ld.XCOFF_R_TOCL|(0x0F<<8), 6)
   507  	case objabi.R_POWER_TLS_LE:
   508  		// This only supports 16b relocations.  It is fixed up in archreloc.
   509  		emitReloc(ld.XCOFF_R_TLS_LE|0x0F<<8, 2)
   510  	case objabi.R_CALLPOWER:
   511  		if r.Size != 4 {
   512  			return false
   513  		}
   514  		emitReloc(ld.XCOFF_R_RBR|0x19<<8, 0)
   515  	case objabi.R_XCOFFREF:
   516  		emitReloc(ld.XCOFF_R_REF|0x3F<<8, 0)
   517  	}
   518  	return true
   519  
   520  }
   521  
   522  func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool {
   523  	// Beware that bit0~bit15 start from the third byte of a instruction in Big-Endian machines.
   524  	rt := r.Type
   525  	if rt == objabi.R_ADDR || rt == objabi.R_POWER_TLS || rt == objabi.R_CALLPOWER {
   526  	} else {
   527  		if ctxt.Arch.ByteOrder == binary.BigEndian {
   528  			sectoff += 2
   529  		}
   530  	}
   531  	out.Write64(uint64(sectoff))
   532  
   533  	elfsym := ld.ElfSymForReloc(ctxt, r.Xsym)
   534  	switch rt {
   535  	default:
   536  		return false
   537  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   538  		switch r.Size {
   539  		case 4:
   540  			out.Write64(uint64(elf.R_PPC64_ADDR32) | uint64(elfsym)<<32)
   541  		case 8:
   542  			out.Write64(uint64(elf.R_PPC64_ADDR64) | uint64(elfsym)<<32)
   543  		default:
   544  			return false
   545  		}
   546  	case objabi.R_POWER_TLS:
   547  		out.Write64(uint64(elf.R_PPC64_TLS) | uint64(elfsym)<<32)
   548  	case objabi.R_POWER_TLS_LE:
   549  		out.Write64(uint64(elf.R_PPC64_TPREL16_HA) | uint64(elfsym)<<32)
   550  		out.Write64(uint64(r.Xadd))
   551  		out.Write64(uint64(sectoff + 4))
   552  		out.Write64(uint64(elf.R_PPC64_TPREL16_LO) | uint64(elfsym)<<32)
   553  	case objabi.R_POWER_TLS_IE:
   554  		out.Write64(uint64(elf.R_PPC64_GOT_TPREL16_HA) | uint64(elfsym)<<32)
   555  		out.Write64(uint64(r.Xadd))
   556  		out.Write64(uint64(sectoff + 4))
   557  		out.Write64(uint64(elf.R_PPC64_GOT_TPREL16_LO_DS) | uint64(elfsym)<<32)
   558  	case objabi.R_ADDRPOWER:
   559  		out.Write64(uint64(elf.R_PPC64_ADDR16_HA) | uint64(elfsym)<<32)
   560  		out.Write64(uint64(r.Xadd))
   561  		out.Write64(uint64(sectoff + 4))
   562  		out.Write64(uint64(elf.R_PPC64_ADDR16_LO) | uint64(elfsym)<<32)
   563  	case objabi.R_ADDRPOWER_DS:
   564  		out.Write64(uint64(elf.R_PPC64_ADDR16_HA) | uint64(elfsym)<<32)
   565  		out.Write64(uint64(r.Xadd))
   566  		out.Write64(uint64(sectoff + 4))
   567  		out.Write64(uint64(elf.R_PPC64_ADDR16_LO_DS) | uint64(elfsym)<<32)
   568  	case objabi.R_ADDRPOWER_GOT:
   569  		out.Write64(uint64(elf.R_PPC64_GOT16_HA) | uint64(elfsym)<<32)
   570  		out.Write64(uint64(r.Xadd))
   571  		out.Write64(uint64(sectoff + 4))
   572  		out.Write64(uint64(elf.R_PPC64_GOT16_LO_DS) | uint64(elfsym)<<32)
   573  	case objabi.R_ADDRPOWER_PCREL:
   574  		out.Write64(uint64(elf.R_PPC64_REL16_HA) | uint64(elfsym)<<32)
   575  		out.Write64(uint64(r.Xadd))
   576  		out.Write64(uint64(sectoff + 4))
   577  		out.Write64(uint64(elf.R_PPC64_REL16_LO) | uint64(elfsym)<<32)
   578  		r.Xadd += 4
   579  	case objabi.R_ADDRPOWER_TOCREL:
   580  		out.Write64(uint64(elf.R_PPC64_TOC16_HA) | uint64(elfsym)<<32)
   581  		out.Write64(uint64(r.Xadd))
   582  		out.Write64(uint64(sectoff + 4))
   583  		out.Write64(uint64(elf.R_PPC64_TOC16_LO) | uint64(elfsym)<<32)
   584  	case objabi.R_ADDRPOWER_TOCREL_DS:
   585  		out.Write64(uint64(elf.R_PPC64_TOC16_HA) | uint64(elfsym)<<32)
   586  		out.Write64(uint64(r.Xadd))
   587  		out.Write64(uint64(sectoff + 4))
   588  		out.Write64(uint64(elf.R_PPC64_TOC16_LO_DS) | uint64(elfsym)<<32)
   589  	case objabi.R_CALLPOWER:
   590  		if r.Size != 4 {
   591  			return false
   592  		}
   593  		out.Write64(uint64(elf.R_PPC64_REL24) | uint64(elfsym)<<32)
   594  
   595  	}
   596  	out.Write64(uint64(r.Xadd))
   597  
   598  	return true
   599  }
   600  
   601  func elfsetupplt(ctxt *ld.Link, plt, got *loader.SymbolBuilder, dynamic loader.Sym) {
   602  	if plt.Size() == 0 {
   603  		// The dynamic linker stores the address of the
   604  		// dynamic resolver and the DSO identifier in the two
   605  		// doublewords at the beginning of the .plt section
   606  		// before the PLT array. Reserve space for these.
   607  		plt.SetSize(16)
   608  	}
   609  }
   610  
   611  func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool {
   612  	return false
   613  }
   614  
   615  // Return the value of .TOC. for symbol s
   616  func symtoc(ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) int64 {
   617  	v := ldr.SymVersion(s)
   618  	if out := ldr.OuterSym(s); out != 0 {
   619  		v = ldr.SymVersion(out)
   620  	}
   621  
   622  	toc := syms.DotTOC[v]
   623  	if toc == 0 {
   624  		ldr.Errorf(s, "TOC-relative relocation in object without .TOC.")
   625  		return 0
   626  	}
   627  
   628  	return ldr.SymValue(toc)
   629  }
   630  
   631  // archreloctoc relocates a TOC relative symbol.
   632  func archreloctoc(ldr *loader.Loader, target *ld.Target, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) int64 {
   633  	rs := r.Sym()
   634  	var o1, o2 uint32
   635  	var t int64
   636  	useAddi := false
   637  
   638  	if target.IsBigEndian() {
   639  		o1 = uint32(val >> 32)
   640  		o2 = uint32(val)
   641  	} else {
   642  		o1 = uint32(val)
   643  		o2 = uint32(val >> 32)
   644  	}
   645  
   646  	// On AIX, TOC data accesses are always made indirectly against R2 (a sequence of addis+ld+load/store). If the
   647  	// The target of the load is known, the sequence can be written into addis+addi+load/store. On Linux,
   648  	// TOC data accesses are always made directly against R2 (e.g addis+load/store).
   649  	if target.IsAIX() {
   650  		if !strings.HasPrefix(ldr.SymName(rs), "TOC.") {
   651  			ldr.Errorf(s, "archreloctoc called for a symbol without TOC anchor")
   652  		}
   653  		relocs := ldr.Relocs(rs)
   654  		tarSym := relocs.At(0).Sym()
   655  
   656  		if target.IsInternal() && tarSym != 0 && ldr.AttrReachable(tarSym) && ldr.SymSect(tarSym).Seg == &ld.Segdata {
   657  			t = ldr.SymValue(tarSym) + r.Add() - ldr.SymValue(syms.TOC)
   658  			// change ld to addi in the second instruction
   659  			o2 = (o2 & 0x03FF0000) | 0xE<<26
   660  			useAddi = true
   661  		} else {
   662  			t = ldr.SymValue(rs) + r.Add() - ldr.SymValue(syms.TOC)
   663  		}
   664  	} else {
   665  		t = ldr.SymValue(rs) + r.Add() - symtoc(ldr, syms, s)
   666  	}
   667  
   668  	if t != int64(int32(t)) {
   669  		ldr.Errorf(s, "TOC relocation for %s is too big to relocate %s: 0x%x", ldr.SymName(s), rs, t)
   670  	}
   671  
   672  	if t&0x8000 != 0 {
   673  		t += 0x10000
   674  	}
   675  
   676  	o1 |= uint32((t >> 16) & 0xFFFF)
   677  
   678  	switch r.Type() {
   679  	case objabi.R_ADDRPOWER_TOCREL_DS:
   680  		if useAddi {
   681  			o2 |= uint32(t) & 0xFFFF
   682  		} else {
   683  			if t&3 != 0 {
   684  				ldr.Errorf(s, "bad DS reloc for %s: %d", ldr.SymName(s), ldr.SymValue(rs))
   685  			}
   686  			o2 |= uint32(t) & 0xFFFC
   687  		}
   688  	case objabi.R_ADDRPOWER_TOCREL:
   689  		o2 |= uint32(t) & 0xffff
   690  	default:
   691  		return -1
   692  	}
   693  
   694  	if target.IsBigEndian() {
   695  		return int64(o1)<<32 | int64(o2)
   696  	}
   697  	return int64(o2)<<32 | int64(o1)
   698  }
   699  
   700  // archrelocaddr relocates a symbol address.
   701  // This code is for linux only.
   702  func archrelocaddr(ldr *loader.Loader, target *ld.Target, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) int64 {
   703  	rs := r.Sym()
   704  	if target.IsAIX() {
   705  		ldr.Errorf(s, "archrelocaddr called for %s relocation\n", ldr.SymName(rs))
   706  	}
   707  	var o1, o2 uint32
   708  	if target.IsBigEndian() {
   709  		o1 = uint32(val >> 32)
   710  		o2 = uint32(val)
   711  	} else {
   712  		o1 = uint32(val)
   713  		o2 = uint32(val >> 32)
   714  	}
   715  
   716  	// We are spreading a 31-bit address across two instructions, putting the
   717  	// high (adjusted) part in the low 16 bits of the first instruction and the
   718  	// low part in the low 16 bits of the second instruction, or, in the DS case,
   719  	// bits 15-2 (inclusive) of the address into bits 15-2 of the second
   720  	// instruction (it is an error in this case if the low 2 bits of the address
   721  	// are non-zero).
   722  
   723  	t := ldr.SymAddr(rs) + r.Add()
   724  	if t < 0 || t >= 1<<31 {
   725  		ldr.Errorf(s, "relocation for %s is too big (>=2G): 0x%x", ldr.SymName(s), ldr.SymValue(rs))
   726  	}
   727  	if t&0x8000 != 0 {
   728  		t += 0x10000
   729  	}
   730  
   731  	switch r.Type() {
   732  	case objabi.R_ADDRPOWER:
   733  		o1 |= (uint32(t) >> 16) & 0xffff
   734  		o2 |= uint32(t) & 0xffff
   735  	case objabi.R_ADDRPOWER_DS:
   736  		o1 |= (uint32(t) >> 16) & 0xffff
   737  		if t&3 != 0 {
   738  			ldr.Errorf(s, "bad DS reloc for %s: %d", ldr.SymName(s), ldr.SymValue(rs))
   739  		}
   740  		o2 |= uint32(t) & 0xfffc
   741  	default:
   742  		return -1
   743  	}
   744  
   745  	if target.IsBigEndian() {
   746  		return int64(o1)<<32 | int64(o2)
   747  	}
   748  	return int64(o2)<<32 | int64(o1)
   749  }
   750  
   751  // Determine if the code was compiled so that the TOC register R2 is initialized and maintained
   752  func r2Valid(ctxt *ld.Link) bool {
   753  	switch ctxt.BuildMode {
   754  	case ld.BuildModeCArchive, ld.BuildModeCShared, ld.BuildModePIE, ld.BuildModeShared, ld.BuildModePlugin:
   755  		return true
   756  	}
   757  	// -linkshared option
   758  	return ctxt.IsSharedGoLink()
   759  }
   760  
   761  // resolve direct jump relocation r in s, and add trampoline if necessary
   762  func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) {
   763  
   764  	// Trampolines are created if the branch offset is too large and the linker cannot insert a call stub to handle it.
   765  	// For internal linking, trampolines are always created for long calls.
   766  	// For external linking, the linker can insert a call stub to handle a long call, but depends on having the TOC address in
   767  	// r2.  For those build modes with external linking where the TOC address is not maintained in r2, trampolines must be created.
   768  	if ctxt.IsExternal() && r2Valid(ctxt) {
   769  		// No trampolines needed since r2 contains the TOC
   770  		return
   771  	}
   772  
   773  	relocs := ldr.Relocs(s)
   774  	r := relocs.At(ri)
   775  	var t int64
   776  	// ldr.SymValue(rs) == 0 indicates a cross-package jump to a function that is not yet
   777  	// laid out. Conservatively use a trampoline. This should be rare, as we lay out packages
   778  	// in dependency order.
   779  	if ldr.SymValue(rs) != 0 {
   780  		t = ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   781  	}
   782  	switch r.Type() {
   783  	case objabi.R_CALLPOWER:
   784  
   785  		// If branch offset is too far then create a trampoline.
   786  
   787  		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)) {
   788  			var tramp loader.Sym
   789  			for i := 0; ; i++ {
   790  
   791  				// Using r.Add as part of the name is significant in functions like duffzero where the call
   792  				// target is at some offset within the function.  Calls to duff+8 and duff+256 must appear as
   793  				// distinct trampolines.
   794  
   795  				oName := ldr.SymName(rs)
   796  				name := oName
   797  				if r.Add() == 0 {
   798  					name += fmt.Sprintf("-tramp%d", i)
   799  				} else {
   800  					name += fmt.Sprintf("%+x-tramp%d", r.Add(), i)
   801  				}
   802  
   803  				// Look up the trampoline in case it already exists
   804  
   805  				tramp = ldr.LookupOrCreateSym(name, int(ldr.SymVersion(rs)))
   806  				if oName == "runtime.deferreturn" {
   807  					ldr.SetIsDeferReturnTramp(tramp, true)
   808  				}
   809  				if ldr.SymValue(tramp) == 0 {
   810  					break
   811  				}
   812  
   813  				t = ldr.SymValue(tramp) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   814  
   815  				// With internal linking, the trampoline can be used if it is not too far.
   816  				// With external linking, the trampoline must be in this section for it to be reused.
   817  				if (ctxt.IsInternal() && int64(int32(t<<6)>>6) == t) || (ctxt.IsExternal() && ldr.SymSect(s) == ldr.SymSect(tramp)) {
   818  					break
   819  				}
   820  			}
   821  			if ldr.SymType(tramp) == 0 {
   822  				if r2Valid(ctxt) {
   823  					// Should have returned for above cases
   824  					ctxt.Errorf(s, "unexpected trampoline for shared or dynamic linking")
   825  				} else {
   826  					trampb := ldr.MakeSymbolUpdater(tramp)
   827  					ctxt.AddTramp(trampb)
   828  					gentramp(ctxt, ldr, trampb, rs, r.Add())
   829  				}
   830  			}
   831  			sb := ldr.MakeSymbolUpdater(s)
   832  			relocs := sb.Relocs()
   833  			r := relocs.At(ri)
   834  			r.SetSym(tramp)
   835  			r.SetAdd(0) // This was folded into the trampoline target address
   836  		}
   837  	default:
   838  		ctxt.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
   839  	}
   840  }
   841  
   842  func gentramp(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) {
   843  	tramp.SetSize(16) // 4 instructions
   844  	P := make([]byte, tramp.Size())
   845  	t := ldr.SymValue(target) + offset
   846  	var o1, o2 uint32
   847  
   848  	if ctxt.IsAIX() {
   849  		// On AIX, the address is retrieved with a TOC symbol.
   850  		// For internal linking, the "Linux" way might still be used.
   851  		// However, all text symbols are accessed with a TOC symbol as
   852  		// text relocations aren't supposed to be possible.
   853  		// So, keep using the external linking way to be more AIX friendly.
   854  		o1 = uint32(0x3fe20000) // lis r2, toctargetaddr hi
   855  		o2 = uint32(0xebff0000) // ld r31, toctargetaddr lo
   856  
   857  		toctramp := ldr.CreateSymForUpdate("TOC."+ldr.SymName(tramp.Sym()), 0)
   858  		toctramp.SetType(sym.SXCOFFTOC)
   859  		toctramp.AddAddrPlus(ctxt.Arch, target, offset)
   860  
   861  		r, _ := tramp.AddRel(objabi.R_ADDRPOWER_TOCREL_DS)
   862  		r.SetOff(0)
   863  		r.SetSiz(8) // generates 2 relocations: HA + LO
   864  		r.SetSym(toctramp.Sym())
   865  	} else {
   866  		// Used for default build mode for an executable
   867  		// Address of the call target is generated using
   868  		// relocation and doesn't depend on r2 (TOC).
   869  		o1 = uint32(0x3fe00000) // lis r31,targetaddr hi
   870  		o2 = uint32(0x3bff0000) // addi r31,targetaddr lo
   871  
   872  		// With external linking, the target address must be
   873  		// relocated using LO and HA
   874  		if ctxt.IsExternal() || ldr.SymValue(target) == 0 {
   875  			r, _ := tramp.AddRel(objabi.R_ADDRPOWER)
   876  			r.SetOff(0)
   877  			r.SetSiz(8) // generates 2 relocations: HA + LO
   878  			r.SetSym(target)
   879  			r.SetAdd(offset)
   880  		} else {
   881  			// adjustment needed if lo has sign bit set
   882  			// when using addi to compute address
   883  			val := uint32((t & 0xffff0000) >> 16)
   884  			if t&0x8000 != 0 {
   885  				val += 1
   886  			}
   887  			o1 |= val                // hi part of addr
   888  			o2 |= uint32(t & 0xffff) // lo part of addr
   889  		}
   890  	}
   891  
   892  	o3 := uint32(0x7fe903a6) // mtctr r31
   893  	o4 := uint32(0x4e800420) // bctr
   894  	ctxt.Arch.ByteOrder.PutUint32(P, o1)
   895  	ctxt.Arch.ByteOrder.PutUint32(P[4:], o2)
   896  	ctxt.Arch.ByteOrder.PutUint32(P[8:], o3)
   897  	ctxt.Arch.ByteOrder.PutUint32(P[12:], o4)
   898  	tramp.SetData(P)
   899  }
   900  
   901  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) {
   902  	rs := r.Sym()
   903  	if target.IsExternal() {
   904  		// On AIX, relocations (except TLS ones) must be also done to the
   905  		// value with the current addresses.
   906  		switch rt := r.Type(); rt {
   907  		default:
   908  			if !target.IsAIX() {
   909  				return val, nExtReloc, false
   910  			}
   911  		case objabi.R_POWER_TLS:
   912  			nExtReloc = 1
   913  			return val, nExtReloc, true
   914  		case objabi.R_POWER_TLS_LE, objabi.R_POWER_TLS_IE:
   915  			if target.IsAIX() && rt == objabi.R_POWER_TLS_LE {
   916  				// Fixup val, an addis/addi pair of instructions, which generate a 32b displacement
   917  				// from the threadpointer (R13), into a 16b relocation. XCOFF only supports 16b
   918  				// TLS LE relocations. Likewise, verify this is an addis/addi sequence.
   919  				const expectedOpcodes = 0x3C00000038000000
   920  				const expectedOpmasks = 0xFC000000FC000000
   921  				if uint64(val)&expectedOpmasks != expectedOpcodes {
   922  					ldr.Errorf(s, "relocation for %s+%d is not an addis/addi pair: %16x", ldr.SymName(rs), r.Off(), uint64(val))
   923  				}
   924  				nval := (int64(uint32(0x380d0000)) | val&0x03e00000) << 32 // addi rX, r13, $0
   925  				nval |= int64(0x60000000)                                  // nop
   926  				val = nval
   927  				nExtReloc = 1
   928  			} else {
   929  				nExtReloc = 2
   930  			}
   931  			return val, nExtReloc, true
   932  		case objabi.R_ADDRPOWER,
   933  			objabi.R_ADDRPOWER_DS,
   934  			objabi.R_ADDRPOWER_TOCREL,
   935  			objabi.R_ADDRPOWER_TOCREL_DS,
   936  			objabi.R_ADDRPOWER_GOT,
   937  			objabi.R_ADDRPOWER_PCREL:
   938  			nExtReloc = 2 // need two ELF relocations, see elfreloc1
   939  			if !target.IsAIX() {
   940  				return val, nExtReloc, true
   941  			}
   942  		case objabi.R_CALLPOWER:
   943  			nExtReloc = 1
   944  			if !target.IsAIX() {
   945  				return val, nExtReloc, true
   946  			}
   947  		}
   948  	}
   949  
   950  	switch r.Type() {
   951  	case objabi.R_ADDRPOWER_TOCREL, objabi.R_ADDRPOWER_TOCREL_DS:
   952  		return archreloctoc(ldr, target, syms, r, s, val), nExtReloc, true
   953  	case objabi.R_ADDRPOWER, objabi.R_ADDRPOWER_DS:
   954  		return archrelocaddr(ldr, target, syms, r, s, val), nExtReloc, true
   955  	case objabi.R_CALLPOWER:
   956  		// Bits 6 through 29 = (S + A - P) >> 2
   957  
   958  		t := ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   959  
   960  		tgtName := ldr.SymName(rs)
   961  
   962  		// If we are linking PIE or shared code, all golang generated object files have an extra 2 instruction prologue
   963  		// to regenerate the TOC pointer from R12.  The exception are two special case functions tested below.  Note,
   964  		// local call offsets for externally generated objects are accounted for when converting into golang relocs.
   965  		if !ldr.IsExternal(rs) && ldr.AttrShared(rs) && tgtName != "runtime.duffzero" && tgtName != "runtime.duffcopy" {
   966  			// Furthermore, only apply the offset if the target looks like the start of a function call.
   967  			if r.Add() == 0 && ldr.SymType(rs) == sym.STEXT {
   968  				t += 8
   969  			}
   970  		}
   971  
   972  		if t&3 != 0 {
   973  			ldr.Errorf(s, "relocation for %s+%d is not aligned: %d", ldr.SymName(rs), r.Off(), t)
   974  		}
   975  		// If branch offset is too far then create a trampoline.
   976  
   977  		if int64(int32(t<<6)>>6) != t {
   978  			ldr.Errorf(s, "direct call too far: %s %x", ldr.SymName(rs), t)
   979  		}
   980  		return val | int64(uint32(t)&^0xfc000003), nExtReloc, true
   981  	case objabi.R_POWER_TOC: // S + A - .TOC.
   982  		return ldr.SymValue(rs) + r.Add() - symtoc(ldr, syms, s), nExtReloc, true
   983  
   984  	case objabi.R_ADDRPOWER_PCREL: // S + A - P
   985  		t := ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   986  		ha := uint16(((t + 0x8000) >> 16) & 0xFFFF)
   987  		l := uint16(t)
   988  		if target.IsBigEndian() {
   989  			val |= int64(l)
   990  			val |= int64(ha) << 32
   991  		} else {
   992  			val |= int64(ha)
   993  			val |= int64(l) << 32
   994  		}
   995  		return val, nExtReloc, true
   996  
   997  	case objabi.R_POWER_TLS:
   998  		const OP_ADD = 31<<26 | 266<<1
   999  		const MASK_OP_ADD = 0x3F<<26 | 0x1FF<<1
  1000  		if val&MASK_OP_ADD != OP_ADD {
  1001  			ldr.Errorf(s, "R_POWER_TLS reloc only supports XO form ADD, not %08X", val)
  1002  		}
  1003  		// Verify RB is R13 in ADD RA,RB,RT.
  1004  		if (val>>11)&0x1F != 13 {
  1005  			// If external linking is made to support this, it may expect the linker to rewrite RB.
  1006  			ldr.Errorf(s, "R_POWER_TLS reloc requires R13 in RB (%08X).", uint32(val))
  1007  		}
  1008  		return val, nExtReloc, true
  1009  
  1010  	case objabi.R_POWER_TLS_IE:
  1011  		// Convert TLS_IE relocation to TLS_LE if supported.
  1012  		if !(target.IsPIE() && target.IsElf()) {
  1013  			log.Fatalf("cannot handle R_POWER_TLS_IE (sym %s) when linking non-PIE, non-ELF binaries internally", ldr.SymName(s))
  1014  		}
  1015  
  1016  		// We are an ELF binary, we can safely convert to TLS_LE from:
  1017  		// addis to, r2, x@got@tprel@ha
  1018  		// ld to, to, x@got@tprel@l(to)
  1019  		//
  1020  		// to TLS_LE by converting to:
  1021  		// addis to, r0, x@tprel@ha
  1022  		// addi to, to, x@tprel@l(to)
  1023  
  1024  		const OP_ADDI = 14 << 26
  1025  		const OP_MASK = 0x3F << 26
  1026  		const OP_RA_MASK = 0x1F << 16
  1027  		uval := uint64(val)
  1028  		// convert r2 to r0, and ld to addi
  1029  		if target.IsBigEndian() {
  1030  			uval = uval &^ (OP_RA_MASK << 32)
  1031  			uval = (uval &^ OP_MASK) | OP_ADDI
  1032  		} else {
  1033  			uval = uval &^ (OP_RA_MASK)
  1034  			uval = (uval &^ (OP_MASK << 32)) | (OP_ADDI << 32)
  1035  		}
  1036  		val = int64(uval)
  1037  		// Treat this like an R_POWER_TLS_LE relocation now.
  1038  		fallthrough
  1039  
  1040  	case objabi.R_POWER_TLS_LE:
  1041  		// The thread pointer points 0x7000 bytes after the start of the
  1042  		// thread local storage area as documented in section "3.7.2 TLS
  1043  		// Runtime Handling" of "Power Architecture 64-Bit ELF V2 ABI
  1044  		// Specification".
  1045  		v := ldr.SymValue(rs) - 0x7000
  1046  		if target.IsAIX() {
  1047  			// On AIX, the thread pointer points 0x7800 bytes after
  1048  			// the TLS.
  1049  			v -= 0x800
  1050  		}
  1051  
  1052  		var o1, o2 uint32
  1053  		if int64(int32(v)) != v {
  1054  			ldr.Errorf(s, "TLS offset out of range %d", v)
  1055  		}
  1056  		if target.IsBigEndian() {
  1057  			o1 = uint32(val >> 32)
  1058  			o2 = uint32(val)
  1059  		} else {
  1060  			o1 = uint32(val)
  1061  			o2 = uint32(val >> 32)
  1062  		}
  1063  
  1064  		o1 |= uint32(((v + 0x8000) >> 16) & 0xFFFF)
  1065  		o2 |= uint32(v & 0xFFFF)
  1066  
  1067  		if target.IsBigEndian() {
  1068  			return int64(o1)<<32 | int64(o2), nExtReloc, true
  1069  		}
  1070  		return int64(o2)<<32 | int64(o1), nExtReloc, true
  1071  	}
  1072  
  1073  	return val, nExtReloc, false
  1074  }
  1075  
  1076  func archrelocvariant(target *ld.Target, ldr *loader.Loader, r loader.Reloc, rv sym.RelocVariant, s loader.Sym, t int64, p []byte) (relocatedOffset int64) {
  1077  	rs := r.Sym()
  1078  	switch rv & sym.RV_TYPE_MASK {
  1079  	default:
  1080  		ldr.Errorf(s, "unexpected relocation variant %d", rv)
  1081  		fallthrough
  1082  
  1083  	case sym.RV_NONE:
  1084  		return t
  1085  
  1086  	case sym.RV_POWER_LO:
  1087  		if rv&sym.RV_CHECK_OVERFLOW != 0 {
  1088  			// Whether to check for signed or unsigned
  1089  			// overflow depends on the instruction
  1090  			var o1 uint32
  1091  			if target.IsBigEndian() {
  1092  				o1 = binary.BigEndian.Uint32(p[r.Off()-2:])
  1093  
  1094  			} else {
  1095  				o1 = binary.LittleEndian.Uint32(p[r.Off():])
  1096  			}
  1097  			switch o1 >> 26 {
  1098  			case 24, // ori
  1099  				26, // xori
  1100  				28: // andi
  1101  				if t>>16 != 0 {
  1102  					goto overflow
  1103  				}
  1104  
  1105  			default:
  1106  				if int64(int16(t)) != t {
  1107  					goto overflow
  1108  				}
  1109  			}
  1110  		}
  1111  
  1112  		return int64(int16(t))
  1113  
  1114  	case sym.RV_POWER_HA:
  1115  		t += 0x8000
  1116  		fallthrough
  1117  
  1118  		// Fallthrough
  1119  	case sym.RV_POWER_HI:
  1120  		t >>= 16
  1121  
  1122  		if rv&sym.RV_CHECK_OVERFLOW != 0 {
  1123  			// Whether to check for signed or unsigned
  1124  			// overflow depends on the instruction
  1125  			var o1 uint32
  1126  			if target.IsBigEndian() {
  1127  				o1 = binary.BigEndian.Uint32(p[r.Off()-2:])
  1128  			} else {
  1129  				o1 = binary.LittleEndian.Uint32(p[r.Off():])
  1130  			}
  1131  			switch o1 >> 26 {
  1132  			case 25, // oris
  1133  				27, // xoris
  1134  				29: // andis
  1135  				if t>>16 != 0 {
  1136  					goto overflow
  1137  				}
  1138  
  1139  			default:
  1140  				if int64(int16(t)) != t {
  1141  					goto overflow
  1142  				}
  1143  			}
  1144  		}
  1145  
  1146  		return int64(int16(t))
  1147  
  1148  	case sym.RV_POWER_DS:
  1149  		var o1 uint32
  1150  		if target.IsBigEndian() {
  1151  			o1 = uint32(binary.BigEndian.Uint16(p[r.Off():]))
  1152  		} else {
  1153  			o1 = uint32(binary.LittleEndian.Uint16(p[r.Off():]))
  1154  		}
  1155  		if t&3 != 0 {
  1156  			ldr.Errorf(s, "relocation for %s+%d is not aligned: %d", ldr.SymName(rs), r.Off(), t)
  1157  		}
  1158  		if (rv&sym.RV_CHECK_OVERFLOW != 0) && int64(int16(t)) != t {
  1159  			goto overflow
  1160  		}
  1161  		return int64(o1)&0x3 | int64(int16(t))
  1162  	}
  1163  
  1164  overflow:
  1165  	ldr.Errorf(s, "relocation for %s+%d is too big: %d", ldr.SymName(rs), r.Off(), t)
  1166  	return t
  1167  }
  1168  
  1169  func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) {
  1170  	switch r.Type() {
  1171  	case objabi.R_POWER_TLS, objabi.R_POWER_TLS_LE, objabi.R_POWER_TLS_IE, objabi.R_CALLPOWER:
  1172  		return ld.ExtrelocSimple(ldr, r), true
  1173  	case objabi.R_ADDRPOWER,
  1174  		objabi.R_ADDRPOWER_DS,
  1175  		objabi.R_ADDRPOWER_TOCREL,
  1176  		objabi.R_ADDRPOWER_TOCREL_DS,
  1177  		objabi.R_ADDRPOWER_GOT,
  1178  		objabi.R_ADDRPOWER_PCREL:
  1179  		return ld.ExtrelocViaOuterSym(ldr, r, s), true
  1180  	}
  1181  	return loader.ExtReloc{}, false
  1182  }
  1183  
  1184  func addpltsym(ctxt *ld.Link, ldr *loader.Loader, s loader.Sym) {
  1185  	if ldr.SymPlt(s) >= 0 {
  1186  		return
  1187  	}
  1188  
  1189  	ld.Adddynsym(ldr, &ctxt.Target, &ctxt.ArchSyms, s)
  1190  
  1191  	if ctxt.IsELF {
  1192  		plt := ldr.MakeSymbolUpdater(ctxt.PLT)
  1193  		rela := ldr.MakeSymbolUpdater(ctxt.RelaPLT)
  1194  		if plt.Size() == 0 {
  1195  			panic("plt is not set up")
  1196  		}
  1197  
  1198  		// Create the glink resolver if necessary
  1199  		glink := ensureglinkresolver(ctxt, ldr)
  1200  
  1201  		// Write symbol resolver stub (just a branch to the
  1202  		// glink resolver stub)
  1203  		rel, _ := glink.AddRel(objabi.R_CALLPOWER)
  1204  		rel.SetOff(int32(glink.Size()))
  1205  		rel.SetSiz(4)
  1206  		rel.SetSym(glink.Sym())
  1207  		glink.AddUint32(ctxt.Arch, 0x48000000) // b .glink
  1208  
  1209  		// In the ppc64 ABI, the dynamic linker is responsible
  1210  		// for writing the entire PLT.  We just need to
  1211  		// reserve 8 bytes for each PLT entry and generate a
  1212  		// JMP_SLOT dynamic relocation for it.
  1213  		//
  1214  		// TODO(austin): ABI v1 is different
  1215  		ldr.SetPlt(s, int32(plt.Size()))
  1216  
  1217  		plt.Grow(plt.Size() + 8)
  1218  		plt.SetSize(plt.Size() + 8)
  1219  
  1220  		rela.AddAddrPlus(ctxt.Arch, plt.Sym(), int64(ldr.SymPlt(s)))
  1221  		rela.AddUint64(ctxt.Arch, elf.R_INFO(uint32(ldr.SymDynid(s)), uint32(elf.R_PPC64_JMP_SLOT)))
  1222  		rela.AddUint64(ctxt.Arch, 0)
  1223  	} else {
  1224  		ctxt.Errorf(s, "addpltsym: unsupported binary format")
  1225  	}
  1226  }
  1227  
  1228  // Generate the glink resolver stub if necessary and return the .glink section
  1229  func ensureglinkresolver(ctxt *ld.Link, ldr *loader.Loader) *loader.SymbolBuilder {
  1230  	glink := ldr.CreateSymForUpdate(".glink", 0)
  1231  	if glink.Size() != 0 {
  1232  		return glink
  1233  	}
  1234  
  1235  	// This is essentially the resolver from the ppc64 ELFv2 ABI.
  1236  	// At entry, r12 holds the address of the symbol resolver stub
  1237  	// for the target routine and the argument registers hold the
  1238  	// arguments for the target routine.
  1239  	//
  1240  	// PC-rel offsets are computed once the final codesize of the
  1241  	// resolver is known.
  1242  	//
  1243  	// This stub is PIC, so first get the PC of label 1 into r11.
  1244  	glink.AddUint32(ctxt.Arch, 0x7c0802a6) // mflr r0
  1245  	glink.AddUint32(ctxt.Arch, 0x429f0005) // bcl 20,31,1f
  1246  	glink.AddUint32(ctxt.Arch, 0x7d6802a6) // 1: mflr r11
  1247  	glink.AddUint32(ctxt.Arch, 0x7c0803a6) // mtlr r0
  1248  
  1249  	// Compute the .plt array index from the entry point address
  1250  	// into r0. This is computed relative to label 1 above.
  1251  	glink.AddUint32(ctxt.Arch, 0x38000000) // li r0,-(res_0-1b)
  1252  	glink.AddUint32(ctxt.Arch, 0x7c006214) // add r0,r0,r12
  1253  	glink.AddUint32(ctxt.Arch, 0x7c0b0050) // sub r0,r0,r11
  1254  	glink.AddUint32(ctxt.Arch, 0x7800f082) // srdi r0,r0,2
  1255  
  1256  	// Load the PC-rel offset of ".plt - 1b", and add it to 1b.
  1257  	// This is stored after this stub and before the resolvers.
  1258  	glink.AddUint32(ctxt.Arch, 0xe98b0000) // ld r12,res_0-1b-8(r11)
  1259  	glink.AddUint32(ctxt.Arch, 0x7d6b6214) // add r11,r11,r12
  1260  
  1261  	// Load r12 = dynamic resolver address and r11 = DSO
  1262  	// identifier from the first two doublewords of the PLT.
  1263  	glink.AddUint32(ctxt.Arch, 0xe98b0000) // ld r12,0(r11)
  1264  	glink.AddUint32(ctxt.Arch, 0xe96b0008) // ld r11,8(r11)
  1265  
  1266  	// Jump to the dynamic resolver
  1267  	glink.AddUint32(ctxt.Arch, 0x7d8903a6) // mtctr r12
  1268  	glink.AddUint32(ctxt.Arch, 0x4e800420) // bctr
  1269  
  1270  	// Store the PC-rel offset to the PLT
  1271  	r, _ := glink.AddRel(objabi.R_PCREL)
  1272  	r.SetSym(ctxt.PLT)
  1273  	r.SetSiz(8)
  1274  	r.SetOff(int32(glink.Size()))
  1275  	r.SetAdd(glink.Size())        // Adjust the offset to be relative to label 1 above.
  1276  	glink.AddUint64(ctxt.Arch, 0) // The offset to the PLT.
  1277  
  1278  	// Resolve PC-rel offsets above now the final size of the stub is known.
  1279  	res0m1b := glink.Size() - 8 // res_0 - 1b
  1280  	glink.SetUint32(ctxt.Arch, 16, 0x38000000|uint32(uint16(-res0m1b)))
  1281  	glink.SetUint32(ctxt.Arch, 32, 0xe98b0000|uint32(uint16(res0m1b-8)))
  1282  
  1283  	// The symbol resolvers must immediately follow.
  1284  	//   res_0:
  1285  
  1286  	// Add DT_PPC64_GLINK .dynamic entry, which points to 32 bytes
  1287  	// before the first symbol resolver stub.
  1288  	du := ldr.MakeSymbolUpdater(ctxt.Dynamic)
  1289  	ld.Elfwritedynentsymplus(ctxt, du, elf.DT_PPC64_GLINK, glink.Sym(), glink.Size()-32)
  1290  
  1291  	return glink
  1292  }
  1293  

View as plain text