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

View as plain text