github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/link/internal/ppc64/asm.go (about)

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