Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/link/internal/ld/data.go

Documentation: cmd/link/internal/ld

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/obj.c
     3  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/span.c
     4  //
     5  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     6  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     7  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     8  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     9  //	Portions Copyright © 2004,2006 Bruce Ellis
    10  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    11  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    12  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    13  //
    14  // Permission is hereby granted, free of charge, to any person obtaining a copy
    15  // of this software and associated documentation files (the "Software"), to deal
    16  // in the Software without restriction, including without limitation the rights
    17  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    18  // copies of the Software, and to permit persons to whom the Software is
    19  // furnished to do so, subject to the following conditions:
    20  //
    21  // The above copyright notice and this permission notice shall be included in
    22  // all copies or substantial portions of the Software.
    23  //
    24  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    25  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    26  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    27  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    28  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    29  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    30  // THE SOFTWARE.
    31  
    32  package ld
    33  
    34  import (
    35  	"bytes"
    36  	"cmd/internal/gcprog"
    37  	"cmd/internal/objabi"
    38  	"cmd/internal/sys"
    39  	"cmd/link/internal/loader"
    40  	"cmd/link/internal/sym"
    41  	"compress/zlib"
    42  	"encoding/binary"
    43  	"fmt"
    44  	"log"
    45  	"os"
    46  	"sort"
    47  	"strconv"
    48  	"strings"
    49  	"sync"
    50  	"sync/atomic"
    51  )
    52  
    53  // isRuntimeDepPkg reports whether pkg is the runtime package or its dependency
    54  func isRuntimeDepPkg(pkg string) bool {
    55  	switch pkg {
    56  	case "runtime",
    57  		"sync/atomic",      // runtime may call to sync/atomic, due to go:linkname
    58  		"internal/bytealg", // for IndexByte
    59  		"internal/cpu":     // for cpu features
    60  		return true
    61  	}
    62  	return strings.HasPrefix(pkg, "runtime/internal/") && !strings.HasSuffix(pkg, "_test")
    63  }
    64  
    65  // Estimate the max size needed to hold any new trampolines created for this function. This
    66  // is used to determine when the section can be split if it becomes too large, to ensure that
    67  // the trampolines are in the same section as the function that uses them.
    68  func maxSizeTrampolinesPPC64(ldr *loader.Loader, s loader.Sym, isTramp bool) uint64 {
    69  	// If thearch.Trampoline is nil, then trampoline support is not available on this arch.
    70  	// A trampoline does not need any dependent trampolines.
    71  	if thearch.Trampoline == nil || isTramp {
    72  		return 0
    73  	}
    74  
    75  	n := uint64(0)
    76  	relocs := ldr.Relocs(s)
    77  	for ri := 0; ri < relocs.Count(); ri++ {
    78  		r := relocs.At(ri)
    79  		if r.Type().IsDirectCallOrJump() {
    80  			n++
    81  		}
    82  	}
    83  	// Trampolines in ppc64 are 4 instructions.
    84  	return n * 16
    85  }
    86  
    87  // detect too-far jumps in function s, and add trampolines if necessary
    88  // ARM, PPC64 & PPC64LE support trampoline insertion for internal and external linking
    89  // On PPC64 & PPC64LE the text sections might be split but will still insert trampolines
    90  // where necessary.
    91  func trampoline(ctxt *Link, s loader.Sym) {
    92  	if thearch.Trampoline == nil {
    93  		return // no need or no support of trampolines on this arch
    94  	}
    95  
    96  	ldr := ctxt.loader
    97  	relocs := ldr.Relocs(s)
    98  	for ri := 0; ri < relocs.Count(); ri++ {
    99  		r := relocs.At(ri)
   100  		if !r.Type().IsDirectCallOrJump() {
   101  			continue
   102  		}
   103  		rs := r.Sym()
   104  		if !ldr.AttrReachable(rs) || ldr.SymType(rs) == sym.Sxxx {
   105  			continue // something is wrong. skip it here and we'll emit a better error later
   106  		}
   107  		rs = ldr.ResolveABIAlias(rs)
   108  		if ldr.SymValue(rs) == 0 && (ldr.SymType(rs) != sym.SDYNIMPORT && ldr.SymType(rs) != sym.SUNDEFEXT) {
   109  			if ldr.SymPkg(rs) != ldr.SymPkg(s) {
   110  				if !isRuntimeDepPkg(ldr.SymPkg(s)) || !isRuntimeDepPkg(ldr.SymPkg(rs)) {
   111  					ctxt.Errorf(s, "unresolved inter-package jump to %s(%s) from %s", ldr.SymName(rs), ldr.SymPkg(rs), ldr.SymPkg(s))
   112  				}
   113  				// runtime and its dependent packages may call to each other.
   114  				// they are fine, as they will be laid down together.
   115  			}
   116  			continue
   117  		}
   118  
   119  		thearch.Trampoline(ctxt, ldr, ri, rs, s)
   120  	}
   121  
   122  }
   123  
   124  // FoldSubSymbolOffset computes the offset of symbol s to its top-level outer
   125  // symbol. Returns the top-level symbol and the offset.
   126  // This is used in generating external relocations.
   127  func FoldSubSymbolOffset(ldr *loader.Loader, s loader.Sym) (loader.Sym, int64) {
   128  	outer := ldr.OuterSym(s)
   129  	off := int64(0)
   130  	if outer != 0 {
   131  		off += ldr.SymValue(s) - ldr.SymValue(outer)
   132  		s = outer
   133  	}
   134  	return s, off
   135  }
   136  
   137  // relocsym resolve relocations in "s", updating the symbol's content
   138  // in "P".
   139  // The main loop walks through the list of relocations attached to "s"
   140  // and resolves them where applicable. Relocations are often
   141  // architecture-specific, requiring calls into the 'archreloc' and/or
   142  // 'archrelocvariant' functions for the architecture. When external
   143  // linking is in effect, it may not be  possible to completely resolve
   144  // the address/offset for a symbol, in which case the goal is to lay
   145  // the groundwork for turning a given relocation into an external reloc
   146  // (to be applied by the external linker). For more on how relocations
   147  // work in general, see
   148  //
   149  //  "Linkers and Loaders", by John R. Levine (Morgan Kaufmann, 1999), ch. 7
   150  //
   151  // This is a performance-critical function for the linker; be careful
   152  // to avoid introducing unnecessary allocations in the main loop.
   153  func (st *relocSymState) relocsym(s loader.Sym, P []byte) {
   154  	ldr := st.ldr
   155  	relocs := ldr.Relocs(s)
   156  	if relocs.Count() == 0 {
   157  		return
   158  	}
   159  	target := st.target
   160  	syms := st.syms
   161  	nExtReloc := 0 // number of external relocations
   162  	for ri := 0; ri < relocs.Count(); ri++ {
   163  		r := relocs.At(ri)
   164  		off := r.Off()
   165  		siz := int32(r.Siz())
   166  		rs := r.Sym()
   167  		rs = ldr.ResolveABIAlias(rs)
   168  		rt := r.Type()
   169  		if off < 0 || off+siz > int32(len(P)) {
   170  			rname := ""
   171  			if rs != 0 {
   172  				rname = ldr.SymName(rs)
   173  			}
   174  			st.err.Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(P))
   175  			continue
   176  		}
   177  		if siz == 0 { // informational relocation - no work to do
   178  			continue
   179  		}
   180  
   181  		var rst sym.SymKind
   182  		if rs != 0 {
   183  			rst = ldr.SymType(rs)
   184  		}
   185  
   186  		if rs != 0 && ((rst == sym.Sxxx && !ldr.AttrVisibilityHidden(rs)) || rst == sym.SXREF) {
   187  			// When putting the runtime but not main into a shared library
   188  			// these symbols are undefined and that's OK.
   189  			if target.IsShared() || target.IsPlugin() {
   190  				if ldr.SymName(rs) == "main.main" || (!target.IsPlugin() && ldr.SymName(rs) == "main..inittask") {
   191  					sb := ldr.MakeSymbolUpdater(rs)
   192  					sb.SetType(sym.SDYNIMPORT)
   193  				} else if strings.HasPrefix(ldr.SymName(rs), "go.info.") {
   194  					// Skip go.info symbols. They are only needed to communicate
   195  					// DWARF info between the compiler and linker.
   196  					continue
   197  				}
   198  			} else {
   199  				st.err.errorUnresolved(ldr, s, rs)
   200  				continue
   201  			}
   202  		}
   203  
   204  		if rt >= objabi.ElfRelocOffset {
   205  			continue
   206  		}
   207  
   208  		// We need to be able to reference dynimport symbols when linking against
   209  		// shared libraries, and AIX, Darwin, OpenBSD and Solaris always need it.
   210  		if !target.IsAIX() && !target.IsDarwin() && !target.IsSolaris() && !target.IsOpenbsd() && rs != 0 && rst == sym.SDYNIMPORT && !target.IsDynlinkingGo() && !ldr.AttrSubSymbol(rs) {
   211  			if !(target.IsPPC64() && target.IsExternal() && ldr.SymName(rs) == ".TOC.") {
   212  				st.err.Errorf(s, "unhandled relocation for %s (type %d (%s) rtype %d (%s))", ldr.SymName(rs), rst, rst, rt, sym.RelocName(target.Arch, rt))
   213  			}
   214  		}
   215  		if rs != 0 && rst != sym.STLSBSS && rt != objabi.R_WEAKADDROFF && rt != objabi.R_METHODOFF && !ldr.AttrReachable(rs) {
   216  			st.err.Errorf(s, "unreachable sym in relocation: %s", ldr.SymName(rs))
   217  		}
   218  
   219  		var rv sym.RelocVariant
   220  		if target.IsPPC64() || target.IsS390X() {
   221  			rv = ldr.RelocVariant(s, ri)
   222  		}
   223  
   224  		// TODO(mundaym): remove this special case - see issue 14218.
   225  		if target.IsS390X() {
   226  			switch rt {
   227  			case objabi.R_PCRELDBL:
   228  				rt = objabi.R_PCREL
   229  				rv = sym.RV_390_DBL
   230  			case objabi.R_CALL:
   231  				rv = sym.RV_390_DBL
   232  			}
   233  		}
   234  
   235  		var o int64
   236  		switch rt {
   237  		default:
   238  			switch siz {
   239  			default:
   240  				st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs))
   241  			case 1:
   242  				o = int64(P[off])
   243  			case 2:
   244  				o = int64(target.Arch.ByteOrder.Uint16(P[off:]))
   245  			case 4:
   246  				o = int64(target.Arch.ByteOrder.Uint32(P[off:]))
   247  			case 8:
   248  				o = int64(target.Arch.ByteOrder.Uint64(P[off:]))
   249  			}
   250  			out, n, ok := thearch.Archreloc(target, ldr, syms, r, s, o)
   251  			if target.IsExternal() {
   252  				nExtReloc += n
   253  			}
   254  			if ok {
   255  				o = out
   256  			} else {
   257  				st.err.Errorf(s, "unknown reloc to %v: %d (%s)", ldr.SymName(rs), rt, sym.RelocName(target.Arch, rt))
   258  			}
   259  		case objabi.R_TLS_LE:
   260  			if target.IsExternal() && target.IsElf() {
   261  				nExtReloc++
   262  				o = 0
   263  				if !target.IsAMD64() {
   264  					o = r.Add()
   265  				}
   266  				break
   267  			}
   268  
   269  			if target.IsElf() && target.IsARM() {
   270  				// On ELF ARM, the thread pointer is 8 bytes before
   271  				// the start of the thread-local data block, so add 8
   272  				// to the actual TLS offset (r->sym->value).
   273  				// This 8 seems to be a fundamental constant of
   274  				// ELF on ARM (or maybe Glibc on ARM); it is not
   275  				// related to the fact that our own TLS storage happens
   276  				// to take up 8 bytes.
   277  				o = 8 + ldr.SymValue(rs)
   278  			} else if target.IsElf() || target.IsPlan9() || target.IsDarwin() {
   279  				o = int64(syms.Tlsoffset) + r.Add()
   280  			} else if target.IsWindows() {
   281  				o = r.Add()
   282  			} else {
   283  				log.Fatalf("unexpected R_TLS_LE relocation for %v", target.HeadType)
   284  			}
   285  		case objabi.R_TLS_IE:
   286  			if target.IsExternal() && target.IsElf() {
   287  				nExtReloc++
   288  				o = 0
   289  				if !target.IsAMD64() {
   290  					o = r.Add()
   291  				}
   292  				if target.Is386() {
   293  					nExtReloc++ // need two ELF relocations on 386, see ../x86/asm.go:elfreloc1
   294  				}
   295  				break
   296  			}
   297  			if target.IsPIE() && target.IsElf() {
   298  				// We are linking the final executable, so we
   299  				// can optimize any TLS IE relocation to LE.
   300  				if thearch.TLSIEtoLE == nil {
   301  					log.Fatalf("internal linking of TLS IE not supported on %v", target.Arch.Family)
   302  				}
   303  				thearch.TLSIEtoLE(P, int(off), int(siz))
   304  				o = int64(syms.Tlsoffset)
   305  			} else {
   306  				log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", ldr.SymName(s))
   307  			}
   308  		case objabi.R_ADDR:
   309  			if target.IsExternal() {
   310  				nExtReloc++
   311  
   312  				// set up addend for eventual relocation via outer symbol.
   313  				rs := rs
   314  				rs, off := FoldSubSymbolOffset(ldr, rs)
   315  				xadd := r.Add() + off
   316  				rst := ldr.SymType(rs)
   317  				if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil {
   318  					st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs))
   319  				}
   320  
   321  				o = xadd
   322  				if target.IsElf() {
   323  					if target.IsAMD64() {
   324  						o = 0
   325  					}
   326  				} else if target.IsDarwin() {
   327  					if ldr.SymType(rs) != sym.SHOSTOBJ {
   328  						o += ldr.SymValue(rs)
   329  					}
   330  				} else if target.IsWindows() {
   331  					// nothing to do
   332  				} else if target.IsAIX() {
   333  					o = ldr.SymValue(rs) + xadd
   334  				} else {
   335  					st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType)
   336  				}
   337  
   338  				break
   339  			}
   340  
   341  			// On AIX, a second relocation must be done by the loader,
   342  			// as section addresses can change once loaded.
   343  			// The "default" symbol address is still needed by the loader so
   344  			// the current relocation can't be skipped.
   345  			if target.IsAIX() && rst != sym.SDYNIMPORT {
   346  				// It's not possible to make a loader relocation in a
   347  				// symbol which is not inside .data section.
   348  				// FIXME: It should be forbidden to have R_ADDR from a
   349  				// symbol which isn't in .data. However, as .text has the
   350  				// same address once loaded, this is possible.
   351  				if ldr.SymSect(s).Seg == &Segdata {
   352  					Xcoffadddynrel(target, ldr, syms, s, r, ri)
   353  				}
   354  			}
   355  
   356  			o = ldr.SymValue(rs) + r.Add()
   357  
   358  			// On amd64, 4-byte offsets will be sign-extended, so it is impossible to
   359  			// access more than 2GB of static data; fail at link time is better than
   360  			// fail at runtime. See https://golang.org/issue/7980.
   361  			// Instead of special casing only amd64, we treat this as an error on all
   362  			// 64-bit architectures so as to be future-proof.
   363  			if int32(o) < 0 && target.Arch.PtrSize > 4 && siz == 4 {
   364  				st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x (%#x + %#x)", ldr.SymName(rs), uint64(o), ldr.SymValue(rs), r.Add())
   365  				errorexit()
   366  			}
   367  		case objabi.R_DWARFSECREF:
   368  			if ldr.SymSect(rs) == nil {
   369  				st.err.Errorf(s, "missing DWARF section for relocation target %s", ldr.SymName(rs))
   370  			}
   371  
   372  			if target.IsExternal() {
   373  				// On most platforms, the external linker needs to adjust DWARF references
   374  				// as it combines DWARF sections. However, on Darwin, dsymutil does the
   375  				// DWARF linking, and it understands how to follow section offsets.
   376  				// Leaving in the relocation records confuses it (see
   377  				// https://golang.org/issue/22068) so drop them for Darwin.
   378  				if !target.IsDarwin() {
   379  					nExtReloc++
   380  				}
   381  
   382  				xadd := r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr)
   383  
   384  				o = xadd
   385  				if target.IsElf() && target.IsAMD64() {
   386  					o = 0
   387  				}
   388  				break
   389  			}
   390  			o = ldr.SymValue(rs) + r.Add() - int64(ldr.SymSect(rs).Vaddr)
   391  		case objabi.R_WEAKADDROFF, objabi.R_METHODOFF:
   392  			if !ldr.AttrReachable(rs) {
   393  				if rt == objabi.R_METHODOFF {
   394  					// Set it to a sentinel value. The runtime knows this is not pointing to
   395  					// anything valid.
   396  					o = -1
   397  					break
   398  				}
   399  				continue
   400  			}
   401  			fallthrough
   402  		case objabi.R_ADDROFF:
   403  			// The method offset tables using this relocation expect the offset to be relative
   404  			// to the start of the first text section, even if there are multiple.
   405  			if ldr.SymSect(rs).Name == ".text" {
   406  				o = ldr.SymValue(rs) - int64(Segtext.Sections[0].Vaddr) + r.Add()
   407  			} else {
   408  				o = ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) + r.Add()
   409  			}
   410  
   411  		case objabi.R_ADDRCUOFF:
   412  			// debug_range and debug_loc elements use this relocation type to get an
   413  			// offset from the start of the compile unit.
   414  			o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(loader.Sym(ldr.SymUnit(rs).Textp[0]))
   415  
   416  		// r.Sym() can be 0 when CALL $(constant) is transformed from absolute PC to relative PC call.
   417  		case objabi.R_GOTPCREL:
   418  			if target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 {
   419  				nExtReloc++
   420  				o = r.Add()
   421  				break
   422  			}
   423  			if target.Is386() && target.IsExternal() && target.IsELF {
   424  				nExtReloc++ // need two ELF relocations on 386, see ../x86/asm.go:elfreloc1
   425  			}
   426  			fallthrough
   427  		case objabi.R_CALL, objabi.R_PCREL:
   428  			if target.IsExternal() && rs != 0 && rst == sym.SUNDEFEXT {
   429  				// pass through to the external linker.
   430  				nExtReloc++
   431  				o = 0
   432  				break
   433  			}
   434  			if target.IsExternal() && rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) {
   435  				nExtReloc++
   436  
   437  				// set up addend for eventual relocation via outer symbol.
   438  				rs := rs
   439  				rs, off := FoldSubSymbolOffset(ldr, rs)
   440  				xadd := r.Add() + off - int64(siz) // relative to address after the relocated chunk
   441  				rst := ldr.SymType(rs)
   442  				if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && ldr.SymSect(rs) == nil {
   443  					st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs))
   444  				}
   445  
   446  				o = xadd
   447  				if target.IsElf() {
   448  					if target.IsAMD64() {
   449  						o = 0
   450  					}
   451  				} else if target.IsDarwin() {
   452  					if rt == objabi.R_CALL {
   453  						if target.IsExternal() && rst == sym.SDYNIMPORT {
   454  							if target.IsAMD64() {
   455  								// AMD64 dynamic relocations are relative to the end of the relocation.
   456  								o += int64(siz)
   457  							}
   458  						} else {
   459  							if rst != sym.SHOSTOBJ {
   460  								o += int64(uint64(ldr.SymValue(rs)) - ldr.SymSect(rs).Vaddr)
   461  							}
   462  							o -= int64(off) // relative to section offset, not symbol
   463  						}
   464  					} else {
   465  						o += int64(siz)
   466  					}
   467  				} else if target.IsWindows() && target.IsAMD64() { // only amd64 needs PCREL
   468  					// PE/COFF's PC32 relocation uses the address after the relocated
   469  					// bytes as the base. Compensate by skewing the addend.
   470  					o += int64(siz)
   471  				} else {
   472  					st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType)
   473  				}
   474  
   475  				break
   476  			}
   477  
   478  			o = 0
   479  			if rs != 0 {
   480  				o = ldr.SymValue(rs)
   481  			}
   482  
   483  			o += r.Add() - (ldr.SymValue(s) + int64(off) + int64(siz))
   484  		case objabi.R_SIZE:
   485  			o = ldr.SymSize(rs) + r.Add()
   486  
   487  		case objabi.R_XCOFFREF:
   488  			if !target.IsAIX() {
   489  				st.err.Errorf(s, "find XCOFF R_REF on non-XCOFF files")
   490  			}
   491  			if !target.IsExternal() {
   492  				st.err.Errorf(s, "find XCOFF R_REF with internal linking")
   493  			}
   494  			nExtReloc++
   495  			continue
   496  
   497  		case objabi.R_DWARFFILEREF:
   498  			// We don't renumber files in dwarf.go:writelines anymore.
   499  			continue
   500  
   501  		case objabi.R_CONST:
   502  			o = r.Add()
   503  
   504  		case objabi.R_GOTOFF:
   505  			o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(syms.GOT)
   506  		}
   507  
   508  		if target.IsPPC64() || target.IsS390X() {
   509  			if rv != sym.RV_NONE {
   510  				o = thearch.Archrelocvariant(target, ldr, r, rv, s, o)
   511  			}
   512  		}
   513  
   514  		switch siz {
   515  		default:
   516  			st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs))
   517  		case 1:
   518  			P[off] = byte(int8(o))
   519  		case 2:
   520  			if o != int64(int16(o)) {
   521  				st.err.Errorf(s, "relocation address for %s is too big: %#x", ldr.SymName(rs), o)
   522  			}
   523  			target.Arch.ByteOrder.PutUint16(P[off:], uint16(o))
   524  		case 4:
   525  			if rt == objabi.R_PCREL || rt == objabi.R_CALL {
   526  				if o != int64(int32(o)) {
   527  					st.err.Errorf(s, "pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), o)
   528  				}
   529  			} else {
   530  				if o != int64(int32(o)) && o != int64(uint32(o)) {
   531  					st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), uint64(o))
   532  				}
   533  			}
   534  			target.Arch.ByteOrder.PutUint32(P[off:], uint32(o))
   535  		case 8:
   536  			target.Arch.ByteOrder.PutUint64(P[off:], uint64(o))
   537  		}
   538  	}
   539  	if target.IsExternal() {
   540  		// We'll stream out the external relocations in asmb2 (e.g. elfrelocsect)
   541  		// and we only need the count here.
   542  		atomic.AddUint32(&ldr.SymSect(s).Relcount, uint32(nExtReloc))
   543  	}
   544  }
   545  
   546  // Convert a Go relocation to an external relocation.
   547  func extreloc(ctxt *Link, ldr *loader.Loader, s loader.Sym, r loader.Reloc) (loader.ExtReloc, bool) {
   548  	var rr loader.ExtReloc
   549  	target := &ctxt.Target
   550  	siz := int32(r.Siz())
   551  	if siz == 0 { // informational relocation - no work to do
   552  		return rr, false
   553  	}
   554  
   555  	rt := r.Type()
   556  	if rt >= objabi.ElfRelocOffset {
   557  		return rr, false
   558  	}
   559  	rr.Type = rt
   560  	rr.Size = uint8(siz)
   561  
   562  	// TODO(mundaym): remove this special case - see issue 14218.
   563  	if target.IsS390X() {
   564  		switch rt {
   565  		case objabi.R_PCRELDBL:
   566  			rt = objabi.R_PCREL
   567  		}
   568  	}
   569  
   570  	switch rt {
   571  	default:
   572  		return thearch.Extreloc(target, ldr, r, s)
   573  
   574  	case objabi.R_TLS_LE, objabi.R_TLS_IE:
   575  		if target.IsElf() {
   576  			rs := ldr.ResolveABIAlias(r.Sym())
   577  			rr.Xsym = rs
   578  			if rr.Xsym == 0 {
   579  				rr.Xsym = ctxt.Tlsg
   580  			}
   581  			rr.Xadd = r.Add()
   582  			break
   583  		}
   584  		return rr, false
   585  
   586  	case objabi.R_ADDR:
   587  		// set up addend for eventual relocation via outer symbol.
   588  		rs := ldr.ResolveABIAlias(r.Sym())
   589  		rs, off := FoldSubSymbolOffset(ldr, rs)
   590  		rr.Xadd = r.Add() + off
   591  		rr.Xsym = rs
   592  
   593  	case objabi.R_DWARFSECREF:
   594  		// On most platforms, the external linker needs to adjust DWARF references
   595  		// as it combines DWARF sections. However, on Darwin, dsymutil does the
   596  		// DWARF linking, and it understands how to follow section offsets.
   597  		// Leaving in the relocation records confuses it (see
   598  		// https://golang.org/issue/22068) so drop them for Darwin.
   599  		if target.IsDarwin() {
   600  			return rr, false
   601  		}
   602  		rs := ldr.ResolveABIAlias(r.Sym())
   603  		rr.Xsym = loader.Sym(ldr.SymSect(rs).Sym)
   604  		rr.Xadd = r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr)
   605  
   606  	// r.Sym() can be 0 when CALL $(constant) is transformed from absolute PC to relative PC call.
   607  	case objabi.R_GOTPCREL, objabi.R_CALL, objabi.R_PCREL:
   608  		rs := ldr.ResolveABIAlias(r.Sym())
   609  		if rt == objabi.R_GOTPCREL && target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 {
   610  			rr.Xadd = r.Add()
   611  			rr.Xadd -= int64(siz) // relative to address after the relocated chunk
   612  			rr.Xsym = rs
   613  			break
   614  		}
   615  		if rs != 0 && ldr.SymType(rs) == sym.SUNDEFEXT {
   616  			// pass through to the external linker.
   617  			rr.Xadd = 0
   618  			if target.IsElf() {
   619  				rr.Xadd -= int64(siz)
   620  			}
   621  			rr.Xsym = rs
   622  			break
   623  		}
   624  		if rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) {
   625  			// set up addend for eventual relocation via outer symbol.
   626  			rs := rs
   627  			rs, off := FoldSubSymbolOffset(ldr, rs)
   628  			rr.Xadd = r.Add() + off
   629  			rr.Xadd -= int64(siz) // relative to address after the relocated chunk
   630  			rr.Xsym = rs
   631  			break
   632  		}
   633  		return rr, false
   634  
   635  	case objabi.R_XCOFFREF:
   636  		return ExtrelocSimple(ldr, r), true
   637  
   638  	// These reloc types don't need external relocations.
   639  	case objabi.R_ADDROFF, objabi.R_WEAKADDROFF, objabi.R_METHODOFF, objabi.R_ADDRCUOFF,
   640  		objabi.R_SIZE, objabi.R_CONST, objabi.R_GOTOFF:
   641  		return rr, false
   642  	}
   643  	return rr, true
   644  }
   645  
   646  // ExtrelocSimple creates a simple external relocation from r, with the same
   647  // symbol and addend.
   648  func ExtrelocSimple(ldr *loader.Loader, r loader.Reloc) loader.ExtReloc {
   649  	var rr loader.ExtReloc
   650  	rs := ldr.ResolveABIAlias(r.Sym())
   651  	rr.Xsym = rs
   652  	rr.Xadd = r.Add()
   653  	rr.Type = r.Type()
   654  	rr.Size = r.Siz()
   655  	return rr
   656  }
   657  
   658  // ExtrelocViaOuterSym creates an external relocation from r targeting the
   659  // outer symbol and folding the subsymbol's offset into the addend.
   660  func ExtrelocViaOuterSym(ldr *loader.Loader, r loader.Reloc, s loader.Sym) loader.ExtReloc {
   661  	// set up addend for eventual relocation via outer symbol.
   662  	var rr loader.ExtReloc
   663  	rs := ldr.ResolveABIAlias(r.Sym())
   664  	rs, off := FoldSubSymbolOffset(ldr, rs)
   665  	rr.Xadd = r.Add() + off
   666  	rst := ldr.SymType(rs)
   667  	if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil {
   668  		ldr.Errorf(s, "missing section for %s", ldr.SymName(rs))
   669  	}
   670  	rr.Xsym = rs
   671  	rr.Type = r.Type()
   672  	rr.Size = r.Siz()
   673  	return rr
   674  }
   675  
   676  // relocSymState hold state information needed when making a series of
   677  // successive calls to relocsym(). The items here are invariant
   678  // (meaning that they are set up once initially and then don't change
   679  // during the execution of relocsym), with the exception of a slice
   680  // used to facilitate batch allocation of external relocations. Calls
   681  // to relocsym happen in parallel; the assumption is that each
   682  // parallel thread will have its own state object.
   683  type relocSymState struct {
   684  	target *Target
   685  	ldr    *loader.Loader
   686  	err    *ErrorReporter
   687  	syms   *ArchSyms
   688  }
   689  
   690  // makeRelocSymState creates a relocSymState container object to
   691  // pass to relocsym(). If relocsym() calls happen in parallel,
   692  // each parallel thread should have its own state object.
   693  func (ctxt *Link) makeRelocSymState() *relocSymState {
   694  	return &relocSymState{
   695  		target: &ctxt.Target,
   696  		ldr:    ctxt.loader,
   697  		err:    &ctxt.ErrorReporter,
   698  		syms:   &ctxt.ArchSyms,
   699  	}
   700  }
   701  
   702  func windynrelocsym(ctxt *Link, rel *loader.SymbolBuilder, s loader.Sym) {
   703  	var su *loader.SymbolBuilder
   704  	relocs := ctxt.loader.Relocs(s)
   705  	for ri := 0; ri < relocs.Count(); ri++ {
   706  		r := relocs.At(ri)
   707  		if r.IsMarker() {
   708  			continue // skip marker relocations
   709  		}
   710  		targ := r.Sym()
   711  		if targ == 0 {
   712  			continue
   713  		}
   714  		rt := r.Type()
   715  		if !ctxt.loader.AttrReachable(targ) {
   716  			if rt == objabi.R_WEAKADDROFF {
   717  				continue
   718  			}
   719  			ctxt.Errorf(s, "dynamic relocation to unreachable symbol %s",
   720  				ctxt.loader.SymName(targ))
   721  		}
   722  
   723  		tplt := ctxt.loader.SymPlt(targ)
   724  		tgot := ctxt.loader.SymGot(targ)
   725  		if tplt == -2 && tgot != -2 { // make dynimport JMP table for PE object files.
   726  			tplt := int32(rel.Size())
   727  			ctxt.loader.SetPlt(targ, tplt)
   728  
   729  			if su == nil {
   730  				su = ctxt.loader.MakeSymbolUpdater(s)
   731  			}
   732  			r.SetSym(rel.Sym())
   733  			r.SetAdd(int64(tplt))
   734  
   735  			// jmp *addr
   736  			switch ctxt.Arch.Family {
   737  			default:
   738  				ctxt.Errorf(s, "unsupported arch %v", ctxt.Arch.Family)
   739  				return
   740  			case sys.I386:
   741  				rel.AddUint8(0xff)
   742  				rel.AddUint8(0x25)
   743  				rel.AddAddrPlus(ctxt.Arch, targ, 0)
   744  				rel.AddUint8(0x90)
   745  				rel.AddUint8(0x90)
   746  			case sys.AMD64:
   747  				rel.AddUint8(0xff)
   748  				rel.AddUint8(0x24)
   749  				rel.AddUint8(0x25)
   750  				rel.AddAddrPlus4(ctxt.Arch, targ, 0)
   751  				rel.AddUint8(0x90)
   752  			}
   753  		} else if tplt >= 0 {
   754  			if su == nil {
   755  				su = ctxt.loader.MakeSymbolUpdater(s)
   756  			}
   757  			r.SetSym(rel.Sym())
   758  			r.SetAdd(int64(tplt))
   759  		}
   760  	}
   761  }
   762  
   763  // windynrelocsyms generates jump table to C library functions that will be
   764  // added later. windynrelocsyms writes the table into .rel symbol.
   765  func (ctxt *Link) windynrelocsyms() {
   766  	if !(ctxt.IsWindows() && iscgo && ctxt.IsInternal()) {
   767  		return
   768  	}
   769  
   770  	rel := ctxt.loader.CreateSymForUpdate(".rel", 0)
   771  	rel.SetType(sym.STEXT)
   772  
   773  	for _, s := range ctxt.Textp {
   774  		windynrelocsym(ctxt, rel, s)
   775  	}
   776  
   777  	ctxt.Textp = append(ctxt.Textp, rel.Sym())
   778  }
   779  
   780  func dynrelocsym(ctxt *Link, s loader.Sym) {
   781  	target := &ctxt.Target
   782  	ldr := ctxt.loader
   783  	syms := &ctxt.ArchSyms
   784  	relocs := ldr.Relocs(s)
   785  	for ri := 0; ri < relocs.Count(); ri++ {
   786  		r := relocs.At(ri)
   787  		if r.IsMarker() {
   788  			continue // skip marker relocations
   789  		}
   790  		if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal {
   791  			// It's expected that some relocations will be done
   792  			// later by relocsym (R_TLS_LE, R_ADDROFF), so
   793  			// don't worry if Adddynrel returns false.
   794  			thearch.Adddynrel(target, ldr, syms, s, r, ri)
   795  			continue
   796  		}
   797  
   798  		rSym := r.Sym()
   799  		if rSym != 0 && ldr.SymType(rSym) == sym.SDYNIMPORT || r.Type() >= objabi.ElfRelocOffset {
   800  			if rSym != 0 && !ldr.AttrReachable(rSym) {
   801  				ctxt.Errorf(s, "dynamic relocation to unreachable symbol %s", ldr.SymName(rSym))
   802  			}
   803  			if !thearch.Adddynrel(target, ldr, syms, s, r, ri) {
   804  				ctxt.Errorf(s, "unsupported dynamic relocation for symbol %s (type=%d (%s) stype=%d (%s))", ldr.SymName(rSym), r.Type(), sym.RelocName(ctxt.Arch, r.Type()), ldr.SymType(rSym), ldr.SymType(rSym))
   805  			}
   806  		}
   807  	}
   808  }
   809  
   810  func (state *dodataState) dynreloc(ctxt *Link) {
   811  	if ctxt.HeadType == objabi.Hwindows {
   812  		return
   813  	}
   814  	// -d suppresses dynamic loader format, so we may as well not
   815  	// compute these sections or mark their symbols as reachable.
   816  	if *FlagD {
   817  		return
   818  	}
   819  
   820  	for _, s := range ctxt.Textp {
   821  		dynrelocsym(ctxt, s)
   822  	}
   823  	for _, syms := range state.data {
   824  		for _, s := range syms {
   825  			dynrelocsym(ctxt, s)
   826  		}
   827  	}
   828  	if ctxt.IsELF {
   829  		elfdynhash(ctxt)
   830  	}
   831  }
   832  
   833  func CodeblkPad(ctxt *Link, out *OutBuf, addr int64, size int64, pad []byte) {
   834  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.Textp, addr, size, pad)
   835  }
   836  
   837  const blockSize = 1 << 20 // 1MB chunks written at a time.
   838  
   839  // writeBlocks writes a specified chunk of symbols to the output buffer. It
   840  // breaks the write up into ≥blockSize chunks to write them out, and schedules
   841  // as many goroutines as necessary to accomplish this task. This call then
   842  // blocks, waiting on the writes to complete. Note that we use the sem parameter
   843  // to limit the number of concurrent writes taking place.
   844  func writeBlocks(ctxt *Link, out *OutBuf, sem chan int, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) {
   845  	for i, s := range syms {
   846  		if ldr.SymValue(s) >= addr && !ldr.AttrSubSymbol(s) {
   847  			syms = syms[i:]
   848  			break
   849  		}
   850  	}
   851  
   852  	var wg sync.WaitGroup
   853  	max, lastAddr, written := int64(blockSize), addr+size, int64(0)
   854  	for addr < lastAddr {
   855  		// Find the last symbol we'd write.
   856  		idx := -1
   857  		for i, s := range syms {
   858  			if ldr.AttrSubSymbol(s) {
   859  				continue
   860  			}
   861  
   862  			// If the next symbol's size would put us out of bounds on the total length,
   863  			// stop looking.
   864  			end := ldr.SymValue(s) + ldr.SymSize(s)
   865  			if end > lastAddr {
   866  				break
   867  			}
   868  
   869  			// We're gonna write this symbol.
   870  			idx = i
   871  
   872  			// If we cross over the max size, we've got enough symbols.
   873  			if end > addr+max {
   874  				break
   875  			}
   876  		}
   877  
   878  		// If we didn't find any symbols to write, we're done here.
   879  		if idx < 0 {
   880  			break
   881  		}
   882  
   883  		// Compute the length to write, including padding.
   884  		// We need to write to the end address (lastAddr), or the next symbol's
   885  		// start address, whichever comes first. If there is no more symbols,
   886  		// just write to lastAddr. This ensures we don't leave holes between the
   887  		// blocks or at the end.
   888  		length := int64(0)
   889  		if idx+1 < len(syms) {
   890  			// Find the next top-level symbol.
   891  			// Skip over sub symbols so we won't split a containter symbol
   892  			// into two blocks.
   893  			next := syms[idx+1]
   894  			for ldr.AttrSubSymbol(next) {
   895  				idx++
   896  				next = syms[idx+1]
   897  			}
   898  			length = ldr.SymValue(next) - addr
   899  		}
   900  		if length == 0 || length > lastAddr-addr {
   901  			length = lastAddr - addr
   902  		}
   903  
   904  		// Start the block output operator.
   905  		if o, err := out.View(uint64(out.Offset() + written)); err == nil {
   906  			sem <- 1
   907  			wg.Add(1)
   908  			go func(o *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) {
   909  				writeBlock(ctxt, o, ldr, syms, addr, size, pad)
   910  				wg.Done()
   911  				<-sem
   912  			}(o, ldr, syms, addr, length, pad)
   913  		} else { // output not mmaped, don't parallelize.
   914  			writeBlock(ctxt, out, ldr, syms, addr, length, pad)
   915  		}
   916  
   917  		// Prepare for the next loop.
   918  		if idx != -1 {
   919  			syms = syms[idx+1:]
   920  		}
   921  		written += length
   922  		addr += length
   923  	}
   924  	wg.Wait()
   925  }
   926  
   927  func writeBlock(ctxt *Link, out *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) {
   928  
   929  	st := ctxt.makeRelocSymState()
   930  
   931  	// This doesn't distinguish the memory size from the file
   932  	// size, and it lays out the file based on Symbol.Value, which
   933  	// is the virtual address. DWARF compression changes file sizes,
   934  	// so dwarfcompress will fix this up later if necessary.
   935  	eaddr := addr + size
   936  	for _, s := range syms {
   937  		if ldr.AttrSubSymbol(s) {
   938  			continue
   939  		}
   940  		val := ldr.SymValue(s)
   941  		if val >= eaddr {
   942  			break
   943  		}
   944  		if val < addr {
   945  			ldr.Errorf(s, "phase error: addr=%#x but sym=%#x type=%v sect=%v", addr, val, ldr.SymType(s), ldr.SymSect(s).Name)
   946  			errorexit()
   947  		}
   948  		if addr < val {
   949  			out.WriteStringPad("", int(val-addr), pad)
   950  			addr = val
   951  		}
   952  		P := out.WriteSym(ldr, s)
   953  		st.relocsym(s, P)
   954  		if f, ok := ctxt.generatorSyms[s]; ok {
   955  			f(ctxt, s)
   956  		}
   957  		addr += int64(len(P))
   958  		siz := ldr.SymSize(s)
   959  		if addr < val+siz {
   960  			out.WriteStringPad("", int(val+siz-addr), pad)
   961  			addr = val + siz
   962  		}
   963  		if addr != val+siz {
   964  			ldr.Errorf(s, "phase error: addr=%#x value+size=%#x", addr, val+siz)
   965  			errorexit()
   966  		}
   967  		if val+siz >= eaddr {
   968  			break
   969  		}
   970  	}
   971  
   972  	if addr < eaddr {
   973  		out.WriteStringPad("", int(eaddr-addr), pad)
   974  	}
   975  }
   976  
   977  type writeFn func(*Link, *OutBuf, int64, int64)
   978  
   979  // writeParallel handles scheduling parallel execution of data write functions.
   980  func writeParallel(wg *sync.WaitGroup, fn writeFn, ctxt *Link, seek, vaddr, length uint64) {
   981  	if out, err := ctxt.Out.View(seek); err != nil {
   982  		ctxt.Out.SeekSet(int64(seek))
   983  		fn(ctxt, ctxt.Out, int64(vaddr), int64(length))
   984  	} else {
   985  		wg.Add(1)
   986  		go func() {
   987  			defer wg.Done()
   988  			fn(ctxt, out, int64(vaddr), int64(length))
   989  		}()
   990  	}
   991  }
   992  
   993  func datblk(ctxt *Link, out *OutBuf, addr, size int64) {
   994  	writeDatblkToOutBuf(ctxt, out, addr, size)
   995  }
   996  
   997  // Used only on Wasm for now.
   998  func DatblkBytes(ctxt *Link, addr int64, size int64) []byte {
   999  	buf := make([]byte, size)
  1000  	out := &OutBuf{heap: buf}
  1001  	writeDatblkToOutBuf(ctxt, out, addr, size)
  1002  	return buf
  1003  }
  1004  
  1005  func writeDatblkToOutBuf(ctxt *Link, out *OutBuf, addr int64, size int64) {
  1006  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.datap, addr, size, zeros[:])
  1007  }
  1008  
  1009  func dwarfblk(ctxt *Link, out *OutBuf, addr int64, size int64) {
  1010  	// Concatenate the section symbol lists into a single list to pass
  1011  	// to writeBlocks.
  1012  	//
  1013  	// NB: ideally we would do a separate writeBlocks call for each
  1014  	// section, but this would run the risk of undoing any file offset
  1015  	// adjustments made during layout.
  1016  	n := 0
  1017  	for i := range dwarfp {
  1018  		n += len(dwarfp[i].syms)
  1019  	}
  1020  	syms := make([]loader.Sym, 0, n)
  1021  	for i := range dwarfp {
  1022  		syms = append(syms, dwarfp[i].syms...)
  1023  	}
  1024  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, syms, addr, size, zeros[:])
  1025  }
  1026  
  1027  var zeros [512]byte
  1028  
  1029  var (
  1030  	strdata  = make(map[string]string)
  1031  	strnames []string
  1032  )
  1033  
  1034  func addstrdata1(ctxt *Link, arg string) {
  1035  	eq := strings.Index(arg, "=")
  1036  	dot := strings.LastIndex(arg[:eq+1], ".")
  1037  	if eq < 0 || dot < 0 {
  1038  		Exitf("-X flag requires argument of the form importpath.name=value")
  1039  	}
  1040  	pkg := arg[:dot]
  1041  	if ctxt.BuildMode == BuildModePlugin && pkg == "main" {
  1042  		pkg = *flagPluginPath
  1043  	}
  1044  	pkg = objabi.PathToPrefix(pkg)
  1045  	name := pkg + arg[dot:eq]
  1046  	value := arg[eq+1:]
  1047  	if _, ok := strdata[name]; !ok {
  1048  		strnames = append(strnames, name)
  1049  	}
  1050  	strdata[name] = value
  1051  }
  1052  
  1053  // addstrdata sets the initial value of the string variable name to value.
  1054  func addstrdata(arch *sys.Arch, l *loader.Loader, name, value string) {
  1055  	s := l.Lookup(name, 0)
  1056  	if s == 0 {
  1057  		return
  1058  	}
  1059  	if goType := l.SymGoType(s); goType == 0 {
  1060  		return
  1061  	} else if typeName := l.SymName(goType); typeName != "type.string" {
  1062  		Errorf(nil, "%s: cannot set with -X: not a var of type string (%s)", name, typeName)
  1063  		return
  1064  	}
  1065  	if !l.AttrReachable(s) {
  1066  		return // don't bother setting unreachable variable
  1067  	}
  1068  	bld := l.MakeSymbolUpdater(s)
  1069  	if bld.Type() == sym.SBSS {
  1070  		bld.SetType(sym.SDATA)
  1071  	}
  1072  
  1073  	p := fmt.Sprintf("%s.str", name)
  1074  	sbld := l.CreateSymForUpdate(p, 0)
  1075  	sbld.Addstring(value)
  1076  	sbld.SetType(sym.SRODATA)
  1077  
  1078  	bld.SetSize(0)
  1079  	bld.SetData(make([]byte, 0, arch.PtrSize*2))
  1080  	bld.SetReadOnly(false)
  1081  	bld.ResetRelocs()
  1082  	bld.AddAddrPlus(arch, sbld.Sym(), 0)
  1083  	bld.AddUint(arch, uint64(len(value)))
  1084  }
  1085  
  1086  func (ctxt *Link) dostrdata() {
  1087  	for _, name := range strnames {
  1088  		addstrdata(ctxt.Arch, ctxt.loader, name, strdata[name])
  1089  	}
  1090  }
  1091  
  1092  // addgostring adds str, as a Go string value, to s. symname is the name of the
  1093  // symbol used to define the string data and must be unique per linked object.
  1094  func addgostring(ctxt *Link, ldr *loader.Loader, s *loader.SymbolBuilder, symname, str string) {
  1095  	sdata := ldr.CreateSymForUpdate(symname, 0)
  1096  	if sdata.Type() != sym.Sxxx {
  1097  		ctxt.Errorf(s.Sym(), "duplicate symname in addgostring: %s", symname)
  1098  	}
  1099  	sdata.SetLocal(true)
  1100  	sdata.SetType(sym.SRODATA)
  1101  	sdata.SetSize(int64(len(str)))
  1102  	sdata.SetData([]byte(str))
  1103  	s.AddAddr(ctxt.Arch, sdata.Sym())
  1104  	s.AddUint(ctxt.Arch, uint64(len(str)))
  1105  }
  1106  
  1107  func addinitarrdata(ctxt *Link, ldr *loader.Loader, s loader.Sym) {
  1108  	p := ldr.SymName(s) + ".ptr"
  1109  	sp := ldr.CreateSymForUpdate(p, 0)
  1110  	sp.SetType(sym.SINITARR)
  1111  	sp.SetSize(0)
  1112  	sp.SetDuplicateOK(true)
  1113  	sp.AddAddr(ctxt.Arch, s)
  1114  }
  1115  
  1116  // symalign returns the required alignment for the given symbol s.
  1117  func symalign(ldr *loader.Loader, s loader.Sym) int32 {
  1118  	min := int32(thearch.Minalign)
  1119  	align := ldr.SymAlign(s)
  1120  	if align >= min {
  1121  		return align
  1122  	} else if align != 0 {
  1123  		return min
  1124  	}
  1125  	// FIXME: figure out a way to avoid checking by name here.
  1126  	sname := ldr.SymName(s)
  1127  	if strings.HasPrefix(sname, "go.string.") || strings.HasPrefix(sname, "type..namedata.") {
  1128  		// String data is just bytes.
  1129  		// If we align it, we waste a lot of space to padding.
  1130  		return min
  1131  	}
  1132  	align = int32(thearch.Maxalign)
  1133  	ssz := ldr.SymSize(s)
  1134  	for int64(align) > ssz && align > min {
  1135  		align >>= 1
  1136  	}
  1137  	ldr.SetSymAlign(s, align)
  1138  	return align
  1139  }
  1140  
  1141  func aligndatsize(state *dodataState, datsize int64, s loader.Sym) int64 {
  1142  	return Rnd(datsize, int64(symalign(state.ctxt.loader, s)))
  1143  }
  1144  
  1145  const debugGCProg = false
  1146  
  1147  type GCProg struct {
  1148  	ctxt *Link
  1149  	sym  *loader.SymbolBuilder
  1150  	w    gcprog.Writer
  1151  }
  1152  
  1153  func (p *GCProg) Init(ctxt *Link, name string) {
  1154  	p.ctxt = ctxt
  1155  	p.sym = ctxt.loader.CreateSymForUpdate(name, 0)
  1156  	p.w.Init(p.writeByte())
  1157  	if debugGCProg {
  1158  		fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name)
  1159  		p.w.Debug(os.Stderr)
  1160  	}
  1161  }
  1162  
  1163  func (p *GCProg) writeByte() func(x byte) {
  1164  	return func(x byte) {
  1165  		p.sym.AddUint8(x)
  1166  	}
  1167  }
  1168  
  1169  func (p *GCProg) End(size int64) {
  1170  	p.w.ZeroUntil(size / int64(p.ctxt.Arch.PtrSize))
  1171  	p.w.End()
  1172  	if debugGCProg {
  1173  		fmt.Fprintf(os.Stderr, "ld: end GCProg\n")
  1174  	}
  1175  }
  1176  
  1177  func (p *GCProg) AddSym(s loader.Sym) {
  1178  	ldr := p.ctxt.loader
  1179  	typ := ldr.SymGoType(s)
  1180  
  1181  	// Things without pointers should be in sym.SNOPTRDATA or sym.SNOPTRBSS;
  1182  	// everything we see should have pointers and should therefore have a type.
  1183  	if typ == 0 {
  1184  		switch ldr.SymName(s) {
  1185  		case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss":
  1186  			// Ignore special symbols that are sometimes laid out
  1187  			// as real symbols. See comment about dyld on darwin in
  1188  			// the address function.
  1189  			return
  1190  		}
  1191  		p.ctxt.Errorf(p.sym.Sym(), "missing Go type information for global symbol %s: size %d", ldr.SymName(s), ldr.SymSize(s))
  1192  		return
  1193  	}
  1194  
  1195  	ptrsize := int64(p.ctxt.Arch.PtrSize)
  1196  	typData := ldr.Data(typ)
  1197  	nptr := decodetypePtrdata(p.ctxt.Arch, typData) / ptrsize
  1198  
  1199  	if debugGCProg {
  1200  		fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", ldr.SymName(s), ldr.SymValue(s), ldr.SymValue(s)/ptrsize, nptr)
  1201  	}
  1202  
  1203  	sval := ldr.SymValue(s)
  1204  	if decodetypeUsegcprog(p.ctxt.Arch, typData) == 0 {
  1205  		// Copy pointers from mask into program.
  1206  		mask := decodetypeGcmask(p.ctxt, typ)
  1207  		for i := int64(0); i < nptr; i++ {
  1208  			if (mask[i/8]>>uint(i%8))&1 != 0 {
  1209  				p.w.Ptr(sval/ptrsize + i)
  1210  			}
  1211  		}
  1212  		return
  1213  	}
  1214  
  1215  	// Copy program.
  1216  	prog := decodetypeGcprog(p.ctxt, typ)
  1217  	p.w.ZeroUntil(sval / ptrsize)
  1218  	p.w.Append(prog[4:], nptr)
  1219  }
  1220  
  1221  // cutoff is the maximum data section size permitted by the linker
  1222  // (see issue #9862).
  1223  const cutoff = 2e9 // 2 GB (or so; looks better in errors than 2^31)
  1224  
  1225  func (state *dodataState) checkdatsize(symn sym.SymKind) {
  1226  	if state.datsize > cutoff {
  1227  		Errorf(nil, "too much data in section %v (over %v bytes)", symn, cutoff)
  1228  	}
  1229  }
  1230  
  1231  // fixZeroSizedSymbols gives a few special symbols with zero size some space.
  1232  func fixZeroSizedSymbols(ctxt *Link) {
  1233  	// The values in moduledata are filled out by relocations
  1234  	// pointing to the addresses of these special symbols.
  1235  	// Typically these symbols have no size and are not laid
  1236  	// out with their matching section.
  1237  	//
  1238  	// However on darwin, dyld will find the special symbol
  1239  	// in the first loaded module, even though it is local.
  1240  	//
  1241  	// (An hypothesis, formed without looking in the dyld sources:
  1242  	// these special symbols have no size, so their address
  1243  	// matches a real symbol. The dynamic linker assumes we
  1244  	// want the normal symbol with the same address and finds
  1245  	// it in the other module.)
  1246  	//
  1247  	// To work around this we lay out the symbls whose
  1248  	// addresses are vital for multi-module programs to work
  1249  	// as normal symbols, and give them a little size.
  1250  	//
  1251  	// On AIX, as all DATA sections are merged together, ld might not put
  1252  	// these symbols at the beginning of their respective section if there
  1253  	// aren't real symbols, their alignment might not match the
  1254  	// first symbol alignment. Therefore, there are explicitly put at the
  1255  	// beginning of their section with the same alignment.
  1256  	if !(ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) && !(ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) {
  1257  		return
  1258  	}
  1259  
  1260  	ldr := ctxt.loader
  1261  	bss := ldr.CreateSymForUpdate("runtime.bss", 0)
  1262  	bss.SetSize(8)
  1263  	ldr.SetAttrSpecial(bss.Sym(), false)
  1264  
  1265  	ebss := ldr.CreateSymForUpdate("runtime.ebss", 0)
  1266  	ldr.SetAttrSpecial(ebss.Sym(), false)
  1267  
  1268  	data := ldr.CreateSymForUpdate("runtime.data", 0)
  1269  	data.SetSize(8)
  1270  	ldr.SetAttrSpecial(data.Sym(), false)
  1271  
  1272  	edata := ldr.CreateSymForUpdate("runtime.edata", 0)
  1273  	ldr.SetAttrSpecial(edata.Sym(), false)
  1274  
  1275  	if ctxt.HeadType == objabi.Haix {
  1276  		// XCOFFTOC symbols are part of .data section.
  1277  		edata.SetType(sym.SXCOFFTOC)
  1278  	}
  1279  
  1280  	types := ldr.CreateSymForUpdate("runtime.types", 0)
  1281  	types.SetType(sym.STYPE)
  1282  	types.SetSize(8)
  1283  	ldr.SetAttrSpecial(types.Sym(), false)
  1284  
  1285  	etypes := ldr.CreateSymForUpdate("runtime.etypes", 0)
  1286  	etypes.SetType(sym.SFUNCTAB)
  1287  	ldr.SetAttrSpecial(etypes.Sym(), false)
  1288  
  1289  	if ctxt.HeadType == objabi.Haix {
  1290  		rodata := ldr.CreateSymForUpdate("runtime.rodata", 0)
  1291  		rodata.SetType(sym.SSTRING)
  1292  		rodata.SetSize(8)
  1293  		ldr.SetAttrSpecial(rodata.Sym(), false)
  1294  
  1295  		erodata := ldr.CreateSymForUpdate("runtime.erodata", 0)
  1296  		ldr.SetAttrSpecial(erodata.Sym(), false)
  1297  	}
  1298  }
  1299  
  1300  // makeRelroForSharedLib creates a section of readonly data if necessary.
  1301  func (state *dodataState) makeRelroForSharedLib(target *Link) {
  1302  	if !target.UseRelro() {
  1303  		return
  1304  	}
  1305  
  1306  	// "read only" data with relocations needs to go in its own section
  1307  	// when building a shared library. We do this by boosting objects of
  1308  	// type SXXX with relocations to type SXXXRELRO.
  1309  	ldr := target.loader
  1310  	for _, symnro := range sym.ReadOnly {
  1311  		symnrelro := sym.RelROMap[symnro]
  1312  
  1313  		ro := []loader.Sym{}
  1314  		relro := state.data[symnrelro]
  1315  
  1316  		for _, s := range state.data[symnro] {
  1317  			relocs := ldr.Relocs(s)
  1318  			isRelro := relocs.Count() > 0
  1319  			switch state.symType(s) {
  1320  			case sym.STYPE, sym.STYPERELRO, sym.SGOFUNCRELRO:
  1321  				// Symbols are not sorted yet, so it is possible
  1322  				// that an Outer symbol has been changed to a
  1323  				// relro Type before it reaches here.
  1324  				isRelro = true
  1325  			case sym.SFUNCTAB:
  1326  				if ldr.SymName(s) == "runtime.etypes" {
  1327  					// runtime.etypes must be at the end of
  1328  					// the relro data.
  1329  					isRelro = true
  1330  				}
  1331  			}
  1332  			if isRelro {
  1333  				state.setSymType(s, symnrelro)
  1334  				if outer := ldr.OuterSym(s); outer != 0 {
  1335  					state.setSymType(outer, symnrelro)
  1336  				}
  1337  				relro = append(relro, s)
  1338  			} else {
  1339  				ro = append(ro, s)
  1340  			}
  1341  		}
  1342  
  1343  		// Check that we haven't made two symbols with the same .Outer into
  1344  		// different types (because references two symbols with non-nil Outer
  1345  		// become references to the outer symbol + offset it's vital that the
  1346  		// symbol and the outer end up in the same section).
  1347  		for _, s := range relro {
  1348  			if outer := ldr.OuterSym(s); outer != 0 {
  1349  				st := state.symType(s)
  1350  				ost := state.symType(outer)
  1351  				if st != ost {
  1352  					state.ctxt.Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)",
  1353  						ldr.SymName(outer), st, ost)
  1354  				}
  1355  			}
  1356  		}
  1357  
  1358  		state.data[symnro] = ro
  1359  		state.data[symnrelro] = relro
  1360  	}
  1361  }
  1362  
  1363  // dodataState holds bits of state information needed by dodata() and the
  1364  // various helpers it calls. The lifetime of these items should not extend
  1365  // past the end of dodata().
  1366  type dodataState struct {
  1367  	// Link context
  1368  	ctxt *Link
  1369  	// Data symbols bucketed by type.
  1370  	data [sym.SXREF][]loader.Sym
  1371  	// Max alignment for each flavor of data symbol.
  1372  	dataMaxAlign [sym.SXREF]int32
  1373  	// Overridden sym type
  1374  	symGroupType []sym.SymKind
  1375  	// Current data size so far.
  1376  	datsize int64
  1377  }
  1378  
  1379  // A note on symType/setSymType below:
  1380  //
  1381  // In the legacy linker, the types of symbols (notably data symbols) are
  1382  // changed during the symtab() phase so as to insure that similar symbols
  1383  // are bucketed together, then their types are changed back again during
  1384  // dodata. Symbol to section assignment also plays tricks along these lines
  1385  // in the case where a relro segment is needed.
  1386  //
  1387  // The value returned from setType() below reflects the effects of
  1388  // any overrides made by symtab and/or dodata.
  1389  
  1390  // symType returns the (possibly overridden) type of 's'.
  1391  func (state *dodataState) symType(s loader.Sym) sym.SymKind {
  1392  	if int(s) < len(state.symGroupType) {
  1393  		if override := state.symGroupType[s]; override != 0 {
  1394  			return override
  1395  		}
  1396  	}
  1397  	return state.ctxt.loader.SymType(s)
  1398  }
  1399  
  1400  // setSymType sets a new override type for 's'.
  1401  func (state *dodataState) setSymType(s loader.Sym, kind sym.SymKind) {
  1402  	if s == 0 {
  1403  		panic("bad")
  1404  	}
  1405  	if int(s) < len(state.symGroupType) {
  1406  		state.symGroupType[s] = kind
  1407  	} else {
  1408  		su := state.ctxt.loader.MakeSymbolUpdater(s)
  1409  		su.SetType(kind)
  1410  	}
  1411  }
  1412  
  1413  func (ctxt *Link) dodata(symGroupType []sym.SymKind) {
  1414  
  1415  	// Give zeros sized symbols space if necessary.
  1416  	fixZeroSizedSymbols(ctxt)
  1417  
  1418  	// Collect data symbols by type into data.
  1419  	state := dodataState{ctxt: ctxt, symGroupType: symGroupType}
  1420  	ldr := ctxt.loader
  1421  	for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ {
  1422  		if !ldr.AttrReachable(s) || ldr.AttrSpecial(s) || ldr.AttrSubSymbol(s) ||
  1423  			!ldr.TopLevelSym(s) {
  1424  			continue
  1425  		}
  1426  
  1427  		st := state.symType(s)
  1428  
  1429  		if st <= sym.STEXT || st >= sym.SXREF {
  1430  			continue
  1431  		}
  1432  		state.data[st] = append(state.data[st], s)
  1433  
  1434  		// Similarly with checking the onlist attr.
  1435  		if ldr.AttrOnList(s) {
  1436  			log.Fatalf("symbol %s listed multiple times", ldr.SymName(s))
  1437  		}
  1438  		ldr.SetAttrOnList(s, true)
  1439  	}
  1440  
  1441  	// Now that we have the data symbols, but before we start
  1442  	// to assign addresses, record all the necessary
  1443  	// dynamic relocations. These will grow the relocation
  1444  	// symbol, which is itself data.
  1445  	//
  1446  	// On darwin, we need the symbol table numbers for dynreloc.
  1447  	if ctxt.HeadType == objabi.Hdarwin {
  1448  		machosymorder(ctxt)
  1449  	}
  1450  	state.dynreloc(ctxt)
  1451  
  1452  	// Move any RO data with relocations to a separate section.
  1453  	state.makeRelroForSharedLib(ctxt)
  1454  
  1455  	// Set alignment for the symbol with the largest known index,
  1456  	// so as to trigger allocation of the loader's internal
  1457  	// alignment array. This will avoid data races in the parallel
  1458  	// section below.
  1459  	lastSym := loader.Sym(ldr.NSym() - 1)
  1460  	ldr.SetSymAlign(lastSym, ldr.SymAlign(lastSym))
  1461  
  1462  	// Sort symbols.
  1463  	var wg sync.WaitGroup
  1464  	for symn := range state.data {
  1465  		symn := sym.SymKind(symn)
  1466  		wg.Add(1)
  1467  		go func() {
  1468  			state.data[symn], state.dataMaxAlign[symn] = state.dodataSect(ctxt, symn, state.data[symn])
  1469  			wg.Done()
  1470  		}()
  1471  	}
  1472  	wg.Wait()
  1473  
  1474  	if ctxt.IsELF {
  1475  		// Make .rela and .rela.plt contiguous, the ELF ABI requires this
  1476  		// and Solaris actually cares.
  1477  		syms := state.data[sym.SELFROSECT]
  1478  		reli, plti := -1, -1
  1479  		for i, s := range syms {
  1480  			switch ldr.SymName(s) {
  1481  			case ".rel.plt", ".rela.plt":
  1482  				plti = i
  1483  			case ".rel", ".rela":
  1484  				reli = i
  1485  			}
  1486  		}
  1487  		if reli >= 0 && plti >= 0 && plti != reli+1 {
  1488  			var first, second int
  1489  			if plti > reli {
  1490  				first, second = reli, plti
  1491  			} else {
  1492  				first, second = plti, reli
  1493  			}
  1494  			rel, plt := syms[reli], syms[plti]
  1495  			copy(syms[first+2:], syms[first+1:second])
  1496  			syms[first+0] = rel
  1497  			syms[first+1] = plt
  1498  
  1499  			// Make sure alignment doesn't introduce a gap.
  1500  			// Setting the alignment explicitly prevents
  1501  			// symalign from basing it on the size and
  1502  			// getting it wrong.
  1503  			ldr.SetSymAlign(rel, int32(ctxt.Arch.RegSize))
  1504  			ldr.SetSymAlign(plt, int32(ctxt.Arch.RegSize))
  1505  		}
  1506  		state.data[sym.SELFROSECT] = syms
  1507  	}
  1508  
  1509  	if ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal {
  1510  		// These symbols must have the same alignment as their section.
  1511  		// Otherwize, ld might change the layout of Go sections.
  1512  		ldr.SetSymAlign(ldr.Lookup("runtime.data", 0), state.dataMaxAlign[sym.SDATA])
  1513  		ldr.SetSymAlign(ldr.Lookup("runtime.bss", 0), state.dataMaxAlign[sym.SBSS])
  1514  	}
  1515  
  1516  	// Create *sym.Section objects and assign symbols to sections for
  1517  	// data/rodata (and related) symbols.
  1518  	state.allocateDataSections(ctxt)
  1519  
  1520  	// Create *sym.Section objects and assign symbols to sections for
  1521  	// DWARF symbols.
  1522  	state.allocateDwarfSections(ctxt)
  1523  
  1524  	/* number the sections */
  1525  	n := int16(1)
  1526  
  1527  	for _, sect := range Segtext.Sections {
  1528  		sect.Extnum = n
  1529  		n++
  1530  	}
  1531  	for _, sect := range Segrodata.Sections {
  1532  		sect.Extnum = n
  1533  		n++
  1534  	}
  1535  	for _, sect := range Segrelrodata.Sections {
  1536  		sect.Extnum = n
  1537  		n++
  1538  	}
  1539  	for _, sect := range Segdata.Sections {
  1540  		sect.Extnum = n
  1541  		n++
  1542  	}
  1543  	for _, sect := range Segdwarf.Sections {
  1544  		sect.Extnum = n
  1545  		n++
  1546  	}
  1547  }
  1548  
  1549  // allocateDataSectionForSym creates a new sym.Section into which a a
  1550  // single symbol will be placed. Here "seg" is the segment into which
  1551  // the section will go, "s" is the symbol to be placed into the new
  1552  // section, and "rwx" contains permissions for the section.
  1553  func (state *dodataState) allocateDataSectionForSym(seg *sym.Segment, s loader.Sym, rwx int) *sym.Section {
  1554  	ldr := state.ctxt.loader
  1555  	sname := ldr.SymName(s)
  1556  	sect := addsection(ldr, state.ctxt.Arch, seg, sname, rwx)
  1557  	sect.Align = symalign(ldr, s)
  1558  	state.datsize = Rnd(state.datsize, int64(sect.Align))
  1559  	sect.Vaddr = uint64(state.datsize)
  1560  	return sect
  1561  }
  1562  
  1563  // allocateNamedDataSection creates a new sym.Section for a category
  1564  // of data symbols. Here "seg" is the segment into which the section
  1565  // will go, "sName" is the name to give to the section, "types" is a
  1566  // range of symbol types to be put into the section, and "rwx"
  1567  // contains permissions for the section.
  1568  func (state *dodataState) allocateNamedDataSection(seg *sym.Segment, sName string, types []sym.SymKind, rwx int) *sym.Section {
  1569  	sect := addsection(state.ctxt.loader, state.ctxt.Arch, seg, sName, rwx)
  1570  	if len(types) == 0 {
  1571  		sect.Align = 1
  1572  	} else if len(types) == 1 {
  1573  		sect.Align = state.dataMaxAlign[types[0]]
  1574  	} else {
  1575  		for _, symn := range types {
  1576  			align := state.dataMaxAlign[symn]
  1577  			if sect.Align < align {
  1578  				sect.Align = align
  1579  			}
  1580  		}
  1581  	}
  1582  	state.datsize = Rnd(state.datsize, int64(sect.Align))
  1583  	sect.Vaddr = uint64(state.datsize)
  1584  	return sect
  1585  }
  1586  
  1587  // assignDsymsToSection assigns a collection of data symbols to a
  1588  // newly created section. "sect" is the section into which to place
  1589  // the symbols, "syms" holds the list of symbols to assign,
  1590  // "forceType" (if non-zero) contains a new sym type to apply to each
  1591  // sym during the assignment, and "aligner" is a hook to call to
  1592  // handle alignment during the assignment process.
  1593  func (state *dodataState) assignDsymsToSection(sect *sym.Section, syms []loader.Sym, forceType sym.SymKind, aligner func(state *dodataState, datsize int64, s loader.Sym) int64) {
  1594  	ldr := state.ctxt.loader
  1595  	for _, s := range syms {
  1596  		state.datsize = aligner(state, state.datsize, s)
  1597  		ldr.SetSymSect(s, sect)
  1598  		if forceType != sym.Sxxx {
  1599  			state.setSymType(s, forceType)
  1600  		}
  1601  		ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr))
  1602  		state.datsize += ldr.SymSize(s)
  1603  	}
  1604  	sect.Length = uint64(state.datsize) - sect.Vaddr
  1605  }
  1606  
  1607  func (state *dodataState) assignToSection(sect *sym.Section, symn sym.SymKind, forceType sym.SymKind) {
  1608  	state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize)
  1609  	state.checkdatsize(symn)
  1610  }
  1611  
  1612  // allocateSingleSymSections walks through the bucketed data symbols
  1613  // with type 'symn', creates a new section for each sym, and assigns
  1614  // the sym to a newly created section. Section name is set from the
  1615  // symbol name. "Seg" is the segment into which to place the new
  1616  // section, "forceType" is the new sym.SymKind to assign to the symbol
  1617  // within the section, and "rwx" holds section permissions.
  1618  func (state *dodataState) allocateSingleSymSections(seg *sym.Segment, symn sym.SymKind, forceType sym.SymKind, rwx int) {
  1619  	ldr := state.ctxt.loader
  1620  	for _, s := range state.data[symn] {
  1621  		sect := state.allocateDataSectionForSym(seg, s, rwx)
  1622  		ldr.SetSymSect(s, sect)
  1623  		state.setSymType(s, forceType)
  1624  		ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr))
  1625  		state.datsize += ldr.SymSize(s)
  1626  		sect.Length = uint64(state.datsize) - sect.Vaddr
  1627  	}
  1628  	state.checkdatsize(symn)
  1629  }
  1630  
  1631  // allocateNamedSectionAndAssignSyms creates a new section with the
  1632  // specified name, then walks through the bucketed data symbols with
  1633  // type 'symn' and assigns each of them to this new section. "Seg" is
  1634  // the segment into which to place the new section, "secName" is the
  1635  // name to give to the new section, "forceType" (if non-zero) contains
  1636  // a new sym type to apply to each sym during the assignment, and
  1637  // "rwx" holds section permissions.
  1638  func (state *dodataState) allocateNamedSectionAndAssignSyms(seg *sym.Segment, secName string, symn sym.SymKind, forceType sym.SymKind, rwx int) *sym.Section {
  1639  
  1640  	sect := state.allocateNamedDataSection(seg, secName, []sym.SymKind{symn}, rwx)
  1641  	state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize)
  1642  	return sect
  1643  }
  1644  
  1645  // allocateDataSections allocates sym.Section objects for data/rodata
  1646  // (and related) symbols, and then assigns symbols to those sections.
  1647  func (state *dodataState) allocateDataSections(ctxt *Link) {
  1648  	// Allocate sections.
  1649  	// Data is processed before segtext, because we need
  1650  	// to see all symbols in the .data and .bss sections in order
  1651  	// to generate garbage collection information.
  1652  
  1653  	// Writable data sections that do not need any specialized handling.
  1654  	writable := []sym.SymKind{
  1655  		sym.SBUILDINFO,
  1656  		sym.SELFSECT,
  1657  		sym.SMACHO,
  1658  		sym.SMACHOGOT,
  1659  		sym.SWINDOWS,
  1660  	}
  1661  	for _, symn := range writable {
  1662  		state.allocateSingleSymSections(&Segdata, symn, sym.SDATA, 06)
  1663  	}
  1664  	ldr := ctxt.loader
  1665  
  1666  	// .got (and .toc on ppc64)
  1667  	if len(state.data[sym.SELFGOT]) > 0 {
  1668  		sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".got", sym.SELFGOT, sym.SDATA, 06)
  1669  		if ctxt.IsPPC64() {
  1670  			for _, s := range state.data[sym.SELFGOT] {
  1671  				// Resolve .TOC. symbol for this object file (ppc64)
  1672  
  1673  				toc := ldr.Lookup(".TOC.", int(ldr.SymVersion(s)))
  1674  				if toc != 0 {
  1675  					ldr.SetSymSect(toc, sect)
  1676  					ldr.AddInteriorSym(s, toc)
  1677  					ldr.SetSymValue(toc, 0x8000)
  1678  				}
  1679  			}
  1680  		}
  1681  	}
  1682  
  1683  	/* pointer-free data */
  1684  	sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrdata", sym.SNOPTRDATA, sym.SDATA, 06)
  1685  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrdata", 0), sect)
  1686  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrdata", 0), sect)
  1687  
  1688  	hasinitarr := ctxt.linkShared
  1689  
  1690  	/* shared library initializer */
  1691  	switch ctxt.BuildMode {
  1692  	case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin:
  1693  		hasinitarr = true
  1694  	}
  1695  
  1696  	if ctxt.HeadType == objabi.Haix {
  1697  		if len(state.data[sym.SINITARR]) > 0 {
  1698  			Errorf(nil, "XCOFF format doesn't allow .init_array section")
  1699  		}
  1700  	}
  1701  
  1702  	if hasinitarr && len(state.data[sym.SINITARR]) > 0 {
  1703  		state.allocateNamedSectionAndAssignSyms(&Segdata, ".init_array", sym.SINITARR, sym.Sxxx, 06)
  1704  	}
  1705  
  1706  	/* data */
  1707  	sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".data", sym.SDATA, sym.SDATA, 06)
  1708  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.data", 0), sect)
  1709  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.edata", 0), sect)
  1710  	dataGcEnd := state.datsize - int64(sect.Vaddr)
  1711  
  1712  	// On AIX, TOC entries must be the last of .data
  1713  	// These aren't part of gc as they won't change during the runtime.
  1714  	state.assignToSection(sect, sym.SXCOFFTOC, sym.SDATA)
  1715  	state.checkdatsize(sym.SDATA)
  1716  	sect.Length = uint64(state.datsize) - sect.Vaddr
  1717  
  1718  	/* bss */
  1719  	sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".bss", sym.SBSS, sym.Sxxx, 06)
  1720  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.bss", 0), sect)
  1721  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ebss", 0), sect)
  1722  	bssGcEnd := state.datsize - int64(sect.Vaddr)
  1723  
  1724  	// Emit gcdata for bss symbols now that symbol values have been assigned.
  1725  	gcsToEmit := []struct {
  1726  		symName string
  1727  		symKind sym.SymKind
  1728  		gcEnd   int64
  1729  	}{
  1730  		{"runtime.gcdata", sym.SDATA, dataGcEnd},
  1731  		{"runtime.gcbss", sym.SBSS, bssGcEnd},
  1732  	}
  1733  	for _, g := range gcsToEmit {
  1734  		var gc GCProg
  1735  		gc.Init(ctxt, g.symName)
  1736  		for _, s := range state.data[g.symKind] {
  1737  			gc.AddSym(s)
  1738  		}
  1739  		gc.End(g.gcEnd)
  1740  	}
  1741  
  1742  	/* pointer-free bss */
  1743  	sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrbss", sym.SNOPTRBSS, sym.Sxxx, 06)
  1744  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrbss", 0), sect)
  1745  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrbss", 0), sect)
  1746  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.end", 0), sect)
  1747  
  1748  	// Coverage instrumentation counters for libfuzzer.
  1749  	if len(state.data[sym.SLIBFUZZER_EXTRA_COUNTER]) > 0 {
  1750  		state.allocateNamedSectionAndAssignSyms(&Segdata, "__libfuzzer_extra_counters", sym.SLIBFUZZER_EXTRA_COUNTER, sym.Sxxx, 06)
  1751  	}
  1752  
  1753  	if len(state.data[sym.STLSBSS]) > 0 {
  1754  		var sect *sym.Section
  1755  		// FIXME: not clear why it is sometimes necessary to suppress .tbss section creation.
  1756  		if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && (ctxt.LinkMode == LinkExternal || !*FlagD) {
  1757  			sect = addsection(ldr, ctxt.Arch, &Segdata, ".tbss", 06)
  1758  			sect.Align = int32(ctxt.Arch.PtrSize)
  1759  			// FIXME: why does this need to be set to zero?
  1760  			sect.Vaddr = 0
  1761  		}
  1762  		state.datsize = 0
  1763  
  1764  		for _, s := range state.data[sym.STLSBSS] {
  1765  			state.datsize = aligndatsize(state, state.datsize, s)
  1766  			if sect != nil {
  1767  				ldr.SetSymSect(s, sect)
  1768  			}
  1769  			ldr.SetSymValue(s, state.datsize)
  1770  			state.datsize += ldr.SymSize(s)
  1771  		}
  1772  		state.checkdatsize(sym.STLSBSS)
  1773  
  1774  		if sect != nil {
  1775  			sect.Length = uint64(state.datsize)
  1776  		}
  1777  	}
  1778  
  1779  	/*
  1780  	 * We finished data, begin read-only data.
  1781  	 * Not all systems support a separate read-only non-executable data section.
  1782  	 * ELF and Windows PE systems do.
  1783  	 * OS X and Plan 9 do not.
  1784  	 * And if we're using external linking mode, the point is moot,
  1785  	 * since it's not our decision; that code expects the sections in
  1786  	 * segtext.
  1787  	 */
  1788  	var segro *sym.Segment
  1789  	if ctxt.IsELF && ctxt.LinkMode == LinkInternal {
  1790  		segro = &Segrodata
  1791  	} else if ctxt.HeadType == objabi.Hwindows {
  1792  		segro = &Segrodata
  1793  	} else {
  1794  		segro = &Segtext
  1795  	}
  1796  
  1797  	state.datsize = 0
  1798  
  1799  	/* read-only executable ELF, Mach-O sections */
  1800  	if len(state.data[sym.STEXT]) != 0 {
  1801  		culprit := ldr.SymName(state.data[sym.STEXT][0])
  1802  		Errorf(nil, "dodata found an sym.STEXT symbol: %s", culprit)
  1803  	}
  1804  	state.allocateSingleSymSections(&Segtext, sym.SELFRXSECT, sym.SRODATA, 05)
  1805  	state.allocateSingleSymSections(&Segtext, sym.SMACHOPLT, sym.SRODATA, 05)
  1806  
  1807  	/* read-only data */
  1808  	sect = state.allocateNamedDataSection(segro, ".rodata", sym.ReadOnly, 04)
  1809  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.rodata", 0), sect)
  1810  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.erodata", 0), sect)
  1811  	if !ctxt.UseRelro() {
  1812  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect)
  1813  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect)
  1814  	}
  1815  	for _, symn := range sym.ReadOnly {
  1816  		symnStartValue := state.datsize
  1817  		state.assignToSection(sect, symn, sym.SRODATA)
  1818  		setCarrierSize(symn, state.datsize-symnStartValue)
  1819  		if ctxt.HeadType == objabi.Haix {
  1820  			// Read-only symbols might be wrapped inside their outer
  1821  			// symbol.
  1822  			// XCOFF symbol table needs to know the size of
  1823  			// these outer symbols.
  1824  			xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn)
  1825  		}
  1826  	}
  1827  
  1828  	/* read-only ELF, Mach-O sections */
  1829  	state.allocateSingleSymSections(segro, sym.SELFROSECT, sym.SRODATA, 04)
  1830  
  1831  	// There is some data that are conceptually read-only but are written to by
  1832  	// relocations. On GNU systems, we can arrange for the dynamic linker to
  1833  	// mprotect sections after relocations are applied by giving them write
  1834  	// permissions in the object file and calling them ".data.rel.ro.FOO". We
  1835  	// divide the .rodata section between actual .rodata and .data.rel.ro.rodata,
  1836  	// but for the other sections that this applies to, we just write a read-only
  1837  	// .FOO section or a read-write .data.rel.ro.FOO section depending on the
  1838  	// situation.
  1839  	// TODO(mwhudson): It would make sense to do this more widely, but it makes
  1840  	// the system linker segfault on darwin.
  1841  	const relroPerm = 06
  1842  	const fallbackPerm = 04
  1843  	relroSecPerm := fallbackPerm
  1844  	genrelrosecname := func(suffix string) string {
  1845  		if suffix == "" {
  1846  			return ".rodata"
  1847  		}
  1848  		return suffix
  1849  	}
  1850  	seg := segro
  1851  
  1852  	if ctxt.UseRelro() {
  1853  		segrelro := &Segrelrodata
  1854  		if ctxt.LinkMode == LinkExternal && !ctxt.IsAIX() && !ctxt.IsDarwin() {
  1855  			// Using a separate segment with an external
  1856  			// linker results in some programs moving
  1857  			// their data sections unexpectedly, which
  1858  			// corrupts the moduledata. So we use the
  1859  			// rodata segment and let the external linker
  1860  			// sort out a rel.ro segment.
  1861  			segrelro = segro
  1862  		} else {
  1863  			// Reset datsize for new segment.
  1864  			state.datsize = 0
  1865  		}
  1866  
  1867  		if !ctxt.IsDarwin() { // We don't need the special names on darwin.
  1868  			genrelrosecname = func(suffix string) string {
  1869  				return ".data.rel.ro" + suffix
  1870  			}
  1871  		}
  1872  
  1873  		relroReadOnly := []sym.SymKind{}
  1874  		for _, symnro := range sym.ReadOnly {
  1875  			symn := sym.RelROMap[symnro]
  1876  			relroReadOnly = append(relroReadOnly, symn)
  1877  		}
  1878  		seg = segrelro
  1879  		relroSecPerm = relroPerm
  1880  
  1881  		/* data only written by relocations */
  1882  		sect = state.allocateNamedDataSection(segrelro, genrelrosecname(""), relroReadOnly, relroSecPerm)
  1883  
  1884  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect)
  1885  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect)
  1886  
  1887  		for i, symnro := range sym.ReadOnly {
  1888  			if i == 0 && symnro == sym.STYPE && ctxt.HeadType != objabi.Haix {
  1889  				// Skip forward so that no type
  1890  				// reference uses a zero offset.
  1891  				// This is unlikely but possible in small
  1892  				// programs with no other read-only data.
  1893  				state.datsize++
  1894  			}
  1895  
  1896  			symn := sym.RelROMap[symnro]
  1897  			symnStartValue := state.datsize
  1898  
  1899  			for _, s := range state.data[symn] {
  1900  				outer := ldr.OuterSym(s)
  1901  				if s != 0 && ldr.SymSect(outer) != nil && ldr.SymSect(outer) != sect {
  1902  					ctxt.Errorf(s, "s.Outer (%s) in different section from s, %s != %s", ldr.SymName(outer), ldr.SymSect(outer).Name, sect.Name)
  1903  				}
  1904  			}
  1905  			state.assignToSection(sect, symn, sym.SRODATA)
  1906  			setCarrierSize(symn, state.datsize-symnStartValue)
  1907  			if ctxt.HeadType == objabi.Haix {
  1908  				// Read-only symbols might be wrapped inside their outer
  1909  				// symbol.
  1910  				// XCOFF symbol table needs to know the size of
  1911  				// these outer symbols.
  1912  				xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn)
  1913  			}
  1914  		}
  1915  
  1916  		sect.Length = uint64(state.datsize) - sect.Vaddr
  1917  	}
  1918  
  1919  	/* typelink */
  1920  	sect = state.allocateNamedDataSection(seg, genrelrosecname(".typelink"), []sym.SymKind{sym.STYPELINK}, relroSecPerm)
  1921  
  1922  	typelink := ldr.CreateSymForUpdate("runtime.typelink", 0)
  1923  	ldr.SetSymSect(typelink.Sym(), sect)
  1924  	typelink.SetType(sym.SRODATA)
  1925  	state.datsize += typelink.Size()
  1926  	state.checkdatsize(sym.STYPELINK)
  1927  	sect.Length = uint64(state.datsize) - sect.Vaddr
  1928  
  1929  	/* itablink */
  1930  	sect = state.allocateNamedDataSection(seg, genrelrosecname(".itablink"), []sym.SymKind{sym.SITABLINK}, relroSecPerm)
  1931  
  1932  	itablink := ldr.CreateSymForUpdate("runtime.itablink", 0)
  1933  	ldr.SetSymSect(itablink.Sym(), sect)
  1934  	itablink.SetType(sym.SRODATA)
  1935  	state.datsize += itablink.Size()
  1936  	state.checkdatsize(sym.SITABLINK)
  1937  	sect.Length = uint64(state.datsize) - sect.Vaddr
  1938  
  1939  	/* gosymtab */
  1940  	sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gosymtab"), sym.SSYMTAB, sym.SRODATA, relroSecPerm)
  1941  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.symtab", 0), sect)
  1942  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.esymtab", 0), sect)
  1943  
  1944  	/* gopclntab */
  1945  	sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gopclntab"), sym.SPCLNTAB, sym.SRODATA, relroSecPerm)
  1946  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0), sect)
  1947  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pcheader", 0), sect)
  1948  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.funcnametab", 0), sect)
  1949  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.cutab", 0), sect)
  1950  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.filetab", 0), sect)
  1951  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pctab", 0), sect)
  1952  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.functab", 0), sect)
  1953  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.epclntab", 0), sect)
  1954  	setCarrierSize(sym.SPCLNTAB, int64(sect.Length))
  1955  	if ctxt.HeadType == objabi.Haix {
  1956  		xcoffUpdateOuterSize(ctxt, int64(sect.Length), sym.SPCLNTAB)
  1957  	}
  1958  
  1959  	// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
  1960  	if state.datsize != int64(uint32(state.datsize)) {
  1961  		Errorf(nil, "read-only data segment too large: %d", state.datsize)
  1962  	}
  1963  
  1964  	siz := 0
  1965  	for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ {
  1966  		siz += len(state.data[symn])
  1967  	}
  1968  	ctxt.datap = make([]loader.Sym, 0, siz)
  1969  	for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ {
  1970  		ctxt.datap = append(ctxt.datap, state.data[symn]...)
  1971  	}
  1972  }
  1973  
  1974  // allocateDwarfSections allocates sym.Section objects for DWARF
  1975  // symbols, and assigns symbols to sections.
  1976  func (state *dodataState) allocateDwarfSections(ctxt *Link) {
  1977  
  1978  	alignOne := func(state *dodataState, datsize int64, s loader.Sym) int64 { return datsize }
  1979  
  1980  	ldr := ctxt.loader
  1981  	for i := 0; i < len(dwarfp); i++ {
  1982  		// First the section symbol.
  1983  		s := dwarfp[i].secSym()
  1984  		sect := state.allocateNamedDataSection(&Segdwarf, ldr.SymName(s), []sym.SymKind{}, 04)
  1985  		ldr.SetSymSect(s, sect)
  1986  		sect.Sym = sym.LoaderSym(s)
  1987  		curType := ldr.SymType(s)
  1988  		state.setSymType(s, sym.SRODATA)
  1989  		ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr))
  1990  		state.datsize += ldr.SymSize(s)
  1991  
  1992  		// Then any sub-symbols for the section symbol.
  1993  		subSyms := dwarfp[i].subSyms()
  1994  		state.assignDsymsToSection(sect, subSyms, sym.SRODATA, alignOne)
  1995  
  1996  		for j := 0; j < len(subSyms); j++ {
  1997  			s := subSyms[j]
  1998  			if ctxt.HeadType == objabi.Haix && curType == sym.SDWARFLOC {
  1999  				// Update the size of .debug_loc for this symbol's
  2000  				// package.
  2001  				addDwsectCUSize(".debug_loc", ldr.SymPkg(s), uint64(ldr.SymSize(s)))
  2002  			}
  2003  		}
  2004  		sect.Length = uint64(state.datsize) - sect.Vaddr
  2005  		state.checkdatsize(curType)
  2006  	}
  2007  }
  2008  
  2009  type symNameSize struct {
  2010  	name string
  2011  	sz   int64
  2012  	val  int64
  2013  	sym  loader.Sym
  2014  }
  2015  
  2016  func (state *dodataState) dodataSect(ctxt *Link, symn sym.SymKind, syms []loader.Sym) (result []loader.Sym, maxAlign int32) {
  2017  	var head, tail loader.Sym
  2018  	ldr := ctxt.loader
  2019  	sl := make([]symNameSize, len(syms))
  2020  	for k, s := range syms {
  2021  		ss := ldr.SymSize(s)
  2022  		sl[k] = symNameSize{name: ldr.SymName(s), sz: ss, sym: s}
  2023  		ds := int64(len(ldr.Data(s)))
  2024  		switch {
  2025  		case ss < ds:
  2026  			ctxt.Errorf(s, "initialize bounds (%d < %d)", ss, ds)
  2027  		case ss < 0:
  2028  			ctxt.Errorf(s, "negative size (%d bytes)", ss)
  2029  		case ss > cutoff:
  2030  			ctxt.Errorf(s, "symbol too large (%d bytes)", ss)
  2031  		}
  2032  
  2033  		// If the usually-special section-marker symbols are being laid
  2034  		// out as regular symbols, put them either at the beginning or
  2035  		// end of their section.
  2036  		if (ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) || (ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) {
  2037  			switch ldr.SymName(s) {
  2038  			case "runtime.text", "runtime.bss", "runtime.data", "runtime.types", "runtime.rodata":
  2039  				head = s
  2040  				continue
  2041  			case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes", "runtime.erodata":
  2042  				tail = s
  2043  				continue
  2044  			}
  2045  		}
  2046  	}
  2047  
  2048  	// For ppc64, we want to interleave the .got and .toc sections
  2049  	// from input files. Both are type sym.SELFGOT, so in that case
  2050  	// we skip size comparison and fall through to the name
  2051  	// comparison (conveniently, .got sorts before .toc).
  2052  	checkSize := symn != sym.SELFGOT
  2053  
  2054  	// Perform the sort.
  2055  	if symn != sym.SPCLNTAB {
  2056  		sort.Slice(sl, func(i, j int) bool {
  2057  			si, sj := sl[i].sym, sl[j].sym
  2058  			switch {
  2059  			case si == head, sj == tail:
  2060  				return true
  2061  			case sj == head, si == tail:
  2062  				return false
  2063  			}
  2064  			if checkSize {
  2065  				isz := sl[i].sz
  2066  				jsz := sl[j].sz
  2067  				if isz != jsz {
  2068  					return isz < jsz
  2069  				}
  2070  			}
  2071  			iname := sl[i].name
  2072  			jname := sl[j].name
  2073  			if iname != jname {
  2074  				return iname < jname
  2075  			}
  2076  			return si < sj
  2077  		})
  2078  	} else {
  2079  		// PCLNTAB was built internally, and has the proper order based on value.
  2080  		// Sort the symbols as such.
  2081  		for k, s := range syms {
  2082  			sl[k].val = ldr.SymValue(s)
  2083  		}
  2084  		sort.Slice(sl, func(i, j int) bool { return sl[i].val < sl[j].val })
  2085  	}
  2086  
  2087  	// Set alignment, construct result
  2088  	syms = syms[:0]
  2089  	for k := range sl {
  2090  		s := sl[k].sym
  2091  		if s != head && s != tail {
  2092  			align := symalign(ldr, s)
  2093  			if maxAlign < align {
  2094  				maxAlign = align
  2095  			}
  2096  		}
  2097  		syms = append(syms, s)
  2098  	}
  2099  
  2100  	return syms, maxAlign
  2101  }
  2102  
  2103  // Add buildid to beginning of text segment, on non-ELF systems.
  2104  // Non-ELF binary formats are not always flexible enough to
  2105  // give us a place to put the Go build ID. On those systems, we put it
  2106  // at the very beginning of the text segment.
  2107  // This ``header'' is read by cmd/go.
  2108  func (ctxt *Link) textbuildid() {
  2109  	if ctxt.IsELF || ctxt.BuildMode == BuildModePlugin || *flagBuildid == "" {
  2110  		return
  2111  	}
  2112  
  2113  	ldr := ctxt.loader
  2114  	s := ldr.CreateSymForUpdate("go.buildid", 0)
  2115  	// The \xff is invalid UTF-8, meant to make it less likely
  2116  	// to find one of these accidentally.
  2117  	data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff"
  2118  	s.SetType(sym.STEXT)
  2119  	s.SetData([]byte(data))
  2120  	s.SetSize(int64(len(data)))
  2121  
  2122  	ctxt.Textp = append(ctxt.Textp, 0)
  2123  	copy(ctxt.Textp[1:], ctxt.Textp)
  2124  	ctxt.Textp[0] = s.Sym()
  2125  }
  2126  
  2127  func (ctxt *Link) buildinfo() {
  2128  	if ctxt.linkShared || ctxt.BuildMode == BuildModePlugin {
  2129  		// -linkshared and -buildmode=plugin get confused
  2130  		// about the relocations in go.buildinfo
  2131  		// pointing at the other data sections.
  2132  		// The version information is only available in executables.
  2133  		return
  2134  	}
  2135  
  2136  	ldr := ctxt.loader
  2137  	s := ldr.CreateSymForUpdate(".go.buildinfo", 0)
  2138  	// On AIX, .go.buildinfo must be in the symbol table as
  2139  	// it has relocations.
  2140  	s.SetNotInSymbolTable(!ctxt.IsAIX())
  2141  	s.SetType(sym.SBUILDINFO)
  2142  	s.SetAlign(16)
  2143  	// The \xff is invalid UTF-8, meant to make it less likely
  2144  	// to find one of these accidentally.
  2145  	const prefix = "\xff Go buildinf:" // 14 bytes, plus 2 data bytes filled in below
  2146  	data := make([]byte, 32)
  2147  	copy(data, prefix)
  2148  	data[len(prefix)] = byte(ctxt.Arch.PtrSize)
  2149  	data[len(prefix)+1] = 0
  2150  	if ctxt.Arch.ByteOrder == binary.BigEndian {
  2151  		data[len(prefix)+1] = 1
  2152  	}
  2153  	s.SetData(data)
  2154  	s.SetSize(int64(len(data)))
  2155  	r, _ := s.AddRel(objabi.R_ADDR)
  2156  	r.SetOff(16)
  2157  	r.SetSiz(uint8(ctxt.Arch.PtrSize))
  2158  	r.SetSym(ldr.LookupOrCreateSym("runtime.buildVersion", 0))
  2159  	r, _ = s.AddRel(objabi.R_ADDR)
  2160  	r.SetOff(16 + int32(ctxt.Arch.PtrSize))
  2161  	r.SetSiz(uint8(ctxt.Arch.PtrSize))
  2162  	r.SetSym(ldr.LookupOrCreateSym("runtime.modinfo", 0))
  2163  }
  2164  
  2165  // assign addresses to text
  2166  func (ctxt *Link) textaddress() {
  2167  	addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05)
  2168  
  2169  	// Assign PCs in text segment.
  2170  	// Could parallelize, by assigning to text
  2171  	// and then letting threads copy down, but probably not worth it.
  2172  	sect := Segtext.Sections[0]
  2173  
  2174  	sect.Align = int32(Funcalign)
  2175  
  2176  	ldr := ctxt.loader
  2177  
  2178  	text := ctxt.xdefine("runtime.text", sym.STEXT, 0)
  2179  	etext := ctxt.xdefine("runtime.etext", sym.STEXT, 0)
  2180  	ldr.SetSymSect(text, sect)
  2181  	if ctxt.IsAIX() && ctxt.IsExternal() {
  2182  		// Setting runtime.text has a real symbol prevents ld to
  2183  		// change its base address resulting in wrong offsets for
  2184  		// reflect methods.
  2185  		u := ldr.MakeSymbolUpdater(text)
  2186  		u.SetAlign(sect.Align)
  2187  		u.SetSize(8)
  2188  	}
  2189  
  2190  	if (ctxt.DynlinkingGo() && ctxt.IsDarwin()) || (ctxt.IsAIX() && ctxt.IsExternal()) {
  2191  		ldr.SetSymSect(etext, sect)
  2192  		ctxt.Textp = append(ctxt.Textp, etext, 0)
  2193  		copy(ctxt.Textp[1:], ctxt.Textp)
  2194  		ctxt.Textp[0] = text
  2195  	}
  2196  
  2197  	va := uint64(Rnd(*FlagTextAddr, int64(Funcalign)))
  2198  	n := 1
  2199  	sect.Vaddr = va
  2200  	ntramps := 0
  2201  	for _, s := range ctxt.Textp {
  2202  		sect, n, va = assignAddress(ctxt, sect, n, s, va, false)
  2203  
  2204  		trampoline(ctxt, s) // resolve jumps, may add trampolines if jump too far
  2205  
  2206  		// lay down trampolines after each function
  2207  		for ; ntramps < len(ctxt.tramps); ntramps++ {
  2208  			tramp := ctxt.tramps[ntramps]
  2209  			if ctxt.IsAIX() && strings.HasPrefix(ldr.SymName(tramp), "runtime.text.") {
  2210  				// Already set in assignAddress
  2211  				continue
  2212  			}
  2213  			sect, n, va = assignAddress(ctxt, sect, n, tramp, va, true)
  2214  		}
  2215  	}
  2216  
  2217  	sect.Length = va - sect.Vaddr
  2218  	ldr.SetSymSect(etext, sect)
  2219  	if ldr.SymValue(etext) == 0 {
  2220  		// Set the address of the start/end symbols, if not already
  2221  		// (i.e. not darwin+dynlink or AIX+external, see above).
  2222  		ldr.SetSymValue(etext, int64(va))
  2223  		ldr.SetSymValue(text, int64(Segtext.Sections[0].Vaddr))
  2224  	}
  2225  
  2226  	// merge tramps into Textp, keeping Textp in address order
  2227  	if ntramps != 0 {
  2228  		newtextp := make([]loader.Sym, 0, len(ctxt.Textp)+ntramps)
  2229  		i := 0
  2230  		for _, s := range ctxt.Textp {
  2231  			for ; i < ntramps && ldr.SymValue(ctxt.tramps[i]) < ldr.SymValue(s); i++ {
  2232  				newtextp = append(newtextp, ctxt.tramps[i])
  2233  			}
  2234  			newtextp = append(newtextp, s)
  2235  		}
  2236  		newtextp = append(newtextp, ctxt.tramps[i:ntramps]...)
  2237  
  2238  		ctxt.Textp = newtextp
  2239  	}
  2240  }
  2241  
  2242  // assigns address for a text symbol, returns (possibly new) section, its number, and the address
  2243  func assignAddress(ctxt *Link, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp bool) (*sym.Section, int, uint64) {
  2244  	ldr := ctxt.loader
  2245  	if thearch.AssignAddress != nil {
  2246  		return thearch.AssignAddress(ldr, sect, n, s, va, isTramp)
  2247  	}
  2248  
  2249  	ldr.SetSymSect(s, sect)
  2250  	if ldr.AttrSubSymbol(s) {
  2251  		return sect, n, va
  2252  	}
  2253  
  2254  	align := ldr.SymAlign(s)
  2255  	if align == 0 {
  2256  		align = int32(Funcalign)
  2257  	}
  2258  	va = uint64(Rnd(int64(va), int64(align)))
  2259  	if sect.Align < align {
  2260  		sect.Align = align
  2261  	}
  2262  
  2263  	funcsize := uint64(MINFUNC) // spacing required for findfunctab
  2264  	if ldr.SymSize(s) > MINFUNC {
  2265  		funcsize = uint64(ldr.SymSize(s))
  2266  	}
  2267  
  2268  	// On ppc64x a text section should not be larger than 2^26 bytes due to the size of
  2269  	// call target offset field in the bl instruction.  Splitting into smaller text
  2270  	// sections smaller than this limit allows the GNU linker to modify the long calls
  2271  	// appropriately.  The limit allows for the space needed for tables inserted by the linker.
  2272  
  2273  	// If this function doesn't fit in the current text section, then create a new one.
  2274  
  2275  	// Only break at outermost syms.
  2276  
  2277  	// For debugging purposes, allow text size limit to be cranked down,
  2278  	// so as to stress test the code that handles multiple text sections.
  2279  	var textSizelimit uint64 = 0x1c00000
  2280  	if *FlagDebugTextSize != 0 {
  2281  		textSizelimit = uint64(*FlagDebugTextSize)
  2282  	}
  2283  
  2284  	if ctxt.Arch.InFamily(sys.PPC64) && ldr.OuterSym(s) == 0 && ctxt.IsExternal() {
  2285  		// Sanity check: make sure the limit is larger than any
  2286  		// individual text symbol.
  2287  		if funcsize > textSizelimit {
  2288  			panic(fmt.Sprintf("error: ppc64 text size limit %d less than text symbol %s size of %d", textSizelimit, ldr.SymName(s), funcsize))
  2289  		}
  2290  
  2291  		if va-sect.Vaddr+funcsize+maxSizeTrampolinesPPC64(ldr, s, isTramp) > textSizelimit {
  2292  			// Set the length for the previous text section
  2293  			sect.Length = va - sect.Vaddr
  2294  
  2295  			// Create new section, set the starting Vaddr
  2296  			sect = addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05)
  2297  			sect.Vaddr = va
  2298  			ldr.SetSymSect(s, sect)
  2299  
  2300  			// Create a symbol for the start of the secondary text sections
  2301  			ntext := ldr.CreateSymForUpdate(fmt.Sprintf("runtime.text.%d", n), 0)
  2302  			ntext.SetSect(sect)
  2303  			if ctxt.IsAIX() {
  2304  				// runtime.text.X must be a real symbol on AIX.
  2305  				// Assign its address directly in order to be the
  2306  				// first symbol of this new section.
  2307  				ntext.SetType(sym.STEXT)
  2308  				ntext.SetSize(int64(MINFUNC))
  2309  				ntext.SetOnList(true)
  2310  				ctxt.tramps = append(ctxt.tramps, ntext.Sym())
  2311  
  2312  				ntext.SetValue(int64(va))
  2313  				va += uint64(ntext.Size())
  2314  
  2315  				if align := ldr.SymAlign(s); align != 0 {
  2316  					va = uint64(Rnd(int64(va), int64(align)))
  2317  				} else {
  2318  					va = uint64(Rnd(int64(va), int64(Funcalign)))
  2319  				}
  2320  			}
  2321  			n++
  2322  		}
  2323  	}
  2324  
  2325  	ldr.SetSymValue(s, 0)
  2326  	for sub := s; sub != 0; sub = ldr.SubSym(sub) {
  2327  		ldr.SetSymValue(sub, ldr.SymValue(sub)+int64(va))
  2328  	}
  2329  
  2330  	va += funcsize
  2331  
  2332  	return sect, n, va
  2333  }
  2334  
  2335  // address assigns virtual addresses to all segments and sections and
  2336  // returns all segments in file order.
  2337  func (ctxt *Link) address() []*sym.Segment {
  2338  	var order []*sym.Segment // Layout order
  2339  
  2340  	va := uint64(*FlagTextAddr)
  2341  	order = append(order, &Segtext)
  2342  	Segtext.Rwx = 05
  2343  	Segtext.Vaddr = va
  2344  	for _, s := range Segtext.Sections {
  2345  		va = uint64(Rnd(int64(va), int64(s.Align)))
  2346  		s.Vaddr = va
  2347  		va += s.Length
  2348  	}
  2349  
  2350  	Segtext.Length = va - uint64(*FlagTextAddr)
  2351  
  2352  	if len(Segrodata.Sections) > 0 {
  2353  		// align to page boundary so as not to mix
  2354  		// rodata and executable text.
  2355  		//
  2356  		// Note: gold or GNU ld will reduce the size of the executable
  2357  		// file by arranging for the relro segment to end at a page
  2358  		// boundary, and overlap the end of the text segment with the
  2359  		// start of the relro segment in the file.  The PT_LOAD segments
  2360  		// will be such that the last page of the text segment will be
  2361  		// mapped twice, once r-x and once starting out rw- and, after
  2362  		// relocation processing, changed to r--.
  2363  		//
  2364  		// Ideally the last page of the text segment would not be
  2365  		// writable even for this short period.
  2366  		va = uint64(Rnd(int64(va), int64(*FlagRound)))
  2367  
  2368  		order = append(order, &Segrodata)
  2369  		Segrodata.Rwx = 04
  2370  		Segrodata.Vaddr = va
  2371  		for _, s := range Segrodata.Sections {
  2372  			va = uint64(Rnd(int64(va), int64(s.Align)))
  2373  			s.Vaddr = va
  2374  			va += s.Length
  2375  		}
  2376  
  2377  		Segrodata.Length = va - Segrodata.Vaddr
  2378  	}
  2379  	if len(Segrelrodata.Sections) > 0 {
  2380  		// align to page boundary so as not to mix
  2381  		// rodata, rel-ro data, and executable text.
  2382  		va = uint64(Rnd(int64(va), int64(*FlagRound)))
  2383  		if ctxt.HeadType == objabi.Haix {
  2384  			// Relro data are inside data segment on AIX.
  2385  			va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE)
  2386  		}
  2387  
  2388  		order = append(order, &Segrelrodata)
  2389  		Segrelrodata.Rwx = 06
  2390  		Segrelrodata.Vaddr = va
  2391  		for _, s := range Segrelrodata.Sections {
  2392  			va = uint64(Rnd(int64(va), int64(s.Align)))
  2393  			s.Vaddr = va
  2394  			va += s.Length
  2395  		}
  2396  
  2397  		Segrelrodata.Length = va - Segrelrodata.Vaddr
  2398  	}
  2399  
  2400  	va = uint64(Rnd(int64(va), int64(*FlagRound)))
  2401  	if ctxt.HeadType == objabi.Haix && len(Segrelrodata.Sections) == 0 {
  2402  		// Data sections are moved to an unreachable segment
  2403  		// to ensure that they are position-independent.
  2404  		// Already done if relro sections exist.
  2405  		va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE)
  2406  	}
  2407  	order = append(order, &Segdata)
  2408  	Segdata.Rwx = 06
  2409  	Segdata.Vaddr = va
  2410  	var data *sym.Section
  2411  	var noptr *sym.Section
  2412  	var bss *sym.Section
  2413  	var noptrbss *sym.Section
  2414  	for i, s := range Segdata.Sections {
  2415  		if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && s.Name == ".tbss" {
  2416  			continue
  2417  		}
  2418  		vlen := int64(s.Length)
  2419  		if i+1 < len(Segdata.Sections) && !((ctxt.IsELF || ctxt.HeadType == objabi.Haix) && Segdata.Sections[i+1].Name == ".tbss") {
  2420  			vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr)
  2421  		}
  2422  		s.Vaddr = va
  2423  		va += uint64(vlen)
  2424  		Segdata.Length = va - Segdata.Vaddr
  2425  		if s.Name == ".data" {
  2426  			data = s
  2427  		}
  2428  		if s.Name == ".noptrdata" {
  2429  			noptr = s
  2430  		}
  2431  		if s.Name == ".bss" {
  2432  			bss = s
  2433  		}
  2434  		if s.Name == ".noptrbss" {
  2435  			noptrbss = s
  2436  		}
  2437  	}
  2438  
  2439  	// Assign Segdata's Filelen omitting the BSS. We do this here
  2440  	// simply because right now we know where the BSS starts.
  2441  	Segdata.Filelen = bss.Vaddr - Segdata.Vaddr
  2442  
  2443  	va = uint64(Rnd(int64(va), int64(*FlagRound)))
  2444  	order = append(order, &Segdwarf)
  2445  	Segdwarf.Rwx = 06
  2446  	Segdwarf.Vaddr = va
  2447  	for i, s := range Segdwarf.Sections {
  2448  		vlen := int64(s.Length)
  2449  		if i+1 < len(Segdwarf.Sections) {
  2450  			vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr)
  2451  		}
  2452  		s.Vaddr = va
  2453  		va += uint64(vlen)
  2454  		if ctxt.HeadType == objabi.Hwindows {
  2455  			va = uint64(Rnd(int64(va), PEFILEALIGN))
  2456  		}
  2457  		Segdwarf.Length = va - Segdwarf.Vaddr
  2458  	}
  2459  
  2460  	ldr := ctxt.loader
  2461  	var (
  2462  		rodata  = ldr.SymSect(ldr.LookupOrCreateSym("runtime.rodata", 0))
  2463  		symtab  = ldr.SymSect(ldr.LookupOrCreateSym("runtime.symtab", 0))
  2464  		pclntab = ldr.SymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0))
  2465  		types   = ldr.SymSect(ldr.LookupOrCreateSym("runtime.types", 0))
  2466  	)
  2467  
  2468  	for _, s := range ctxt.datap {
  2469  		if sect := ldr.SymSect(s); sect != nil {
  2470  			ldr.AddToSymValue(s, int64(sect.Vaddr))
  2471  		}
  2472  		v := ldr.SymValue(s)
  2473  		for sub := ldr.SubSym(s); sub != 0; sub = ldr.SubSym(sub) {
  2474  			ldr.AddToSymValue(sub, v)
  2475  		}
  2476  	}
  2477  
  2478  	for _, si := range dwarfp {
  2479  		for _, s := range si.syms {
  2480  			if sect := ldr.SymSect(s); sect != nil {
  2481  				ldr.AddToSymValue(s, int64(sect.Vaddr))
  2482  			}
  2483  			sub := ldr.SubSym(s)
  2484  			if sub != 0 {
  2485  				panic(fmt.Sprintf("unexpected sub-sym for %s %s", ldr.SymName(s), ldr.SymType(s).String()))
  2486  			}
  2487  			v := ldr.SymValue(s)
  2488  			for ; sub != 0; sub = ldr.SubSym(sub) {
  2489  				ldr.AddToSymValue(s, v)
  2490  			}
  2491  		}
  2492  	}
  2493  
  2494  	if ctxt.BuildMode == BuildModeShared {
  2495  		s := ldr.LookupOrCreateSym("go.link.abihashbytes", 0)
  2496  		sect := ldr.SymSect(ldr.LookupOrCreateSym(".note.go.abihash", 0))
  2497  		ldr.SetSymSect(s, sect)
  2498  		ldr.SetSymValue(s, int64(sect.Vaddr+16))
  2499  	}
  2500  
  2501  	// If there are multiple text sections, create runtime.text.n for
  2502  	// their section Vaddr, using n for index
  2503  	n := 1
  2504  	for _, sect := range Segtext.Sections[1:] {
  2505  		if sect.Name != ".text" {
  2506  			break
  2507  		}
  2508  		symname := fmt.Sprintf("runtime.text.%d", n)
  2509  		if ctxt.HeadType != objabi.Haix || ctxt.LinkMode != LinkExternal {
  2510  			// Addresses are already set on AIX with external linker
  2511  			// because these symbols are part of their sections.
  2512  			ctxt.xdefine(symname, sym.STEXT, int64(sect.Vaddr))
  2513  		}
  2514  		n++
  2515  	}
  2516  
  2517  	ctxt.xdefine("runtime.rodata", sym.SRODATA, int64(rodata.Vaddr))
  2518  	ctxt.xdefine("runtime.erodata", sym.SRODATA, int64(rodata.Vaddr+rodata.Length))
  2519  	ctxt.xdefine("runtime.types", sym.SRODATA, int64(types.Vaddr))
  2520  	ctxt.xdefine("runtime.etypes", sym.SRODATA, int64(types.Vaddr+types.Length))
  2521  
  2522  	s := ldr.Lookup("runtime.gcdata", 0)
  2523  	ldr.SetAttrLocal(s, true)
  2524  	ctxt.xdefine("runtime.egcdata", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s))
  2525  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcdata", 0), ldr.SymSect(s))
  2526  
  2527  	s = ldr.LookupOrCreateSym("runtime.gcbss", 0)
  2528  	ldr.SetAttrLocal(s, true)
  2529  	ctxt.xdefine("runtime.egcbss", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s))
  2530  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcbss", 0), ldr.SymSect(s))
  2531  
  2532  	ctxt.xdefine("runtime.symtab", sym.SRODATA, int64(symtab.Vaddr))
  2533  	ctxt.xdefine("runtime.esymtab", sym.SRODATA, int64(symtab.Vaddr+symtab.Length))
  2534  	ctxt.xdefine("runtime.pclntab", sym.SRODATA, int64(pclntab.Vaddr))
  2535  	ctxt.defineInternal("runtime.pcheader", sym.SRODATA)
  2536  	ctxt.defineInternal("runtime.funcnametab", sym.SRODATA)
  2537  	ctxt.defineInternal("runtime.cutab", sym.SRODATA)
  2538  	ctxt.defineInternal("runtime.filetab", sym.SRODATA)
  2539  	ctxt.defineInternal("runtime.pctab", sym.SRODATA)
  2540  	ctxt.defineInternal("runtime.functab", sym.SRODATA)
  2541  	ctxt.xdefine("runtime.epclntab", sym.SRODATA, int64(pclntab.Vaddr+pclntab.Length))
  2542  	ctxt.xdefine("runtime.noptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr))
  2543  	ctxt.xdefine("runtime.enoptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length))
  2544  	ctxt.xdefine("runtime.bss", sym.SBSS, int64(bss.Vaddr))
  2545  	ctxt.xdefine("runtime.ebss", sym.SBSS, int64(bss.Vaddr+bss.Length))
  2546  	ctxt.xdefine("runtime.data", sym.SDATA, int64(data.Vaddr))
  2547  	ctxt.xdefine("runtime.edata", sym.SDATA, int64(data.Vaddr+data.Length))
  2548  	ctxt.xdefine("runtime.noptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr))
  2549  	ctxt.xdefine("runtime.enoptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length))
  2550  	ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length))
  2551  
  2552  	if ctxt.IsSolaris() {
  2553  		// On Solaris, in the runtime it sets the external names of the
  2554  		// end symbols. Unset them and define separate symbols, so we
  2555  		// keep both.
  2556  		etext := ldr.Lookup("runtime.etext", 0)
  2557  		edata := ldr.Lookup("runtime.edata", 0)
  2558  		end := ldr.Lookup("runtime.end", 0)
  2559  		ldr.SetSymExtname(etext, "runtime.etext")
  2560  		ldr.SetSymExtname(edata, "runtime.edata")
  2561  		ldr.SetSymExtname(end, "runtime.end")
  2562  		ctxt.xdefine("_etext", ldr.SymType(etext), ldr.SymValue(etext))
  2563  		ctxt.xdefine("_edata", ldr.SymType(edata), ldr.SymValue(edata))
  2564  		ctxt.xdefine("_end", ldr.SymType(end), ldr.SymValue(end))
  2565  		ldr.SetSymSect(ldr.Lookup("_etext", 0), ldr.SymSect(etext))
  2566  		ldr.SetSymSect(ldr.Lookup("_edata", 0), ldr.SymSect(edata))
  2567  		ldr.SetSymSect(ldr.Lookup("_end", 0), ldr.SymSect(end))
  2568  	}
  2569  
  2570  	return order
  2571  }
  2572  
  2573  // layout assigns file offsets and lengths to the segments in order.
  2574  // Returns the file size containing all the segments.
  2575  func (ctxt *Link) layout(order []*sym.Segment) uint64 {
  2576  	var prev *sym.Segment
  2577  	for _, seg := range order {
  2578  		if prev == nil {
  2579  			seg.Fileoff = uint64(HEADR)
  2580  		} else {
  2581  			switch ctxt.HeadType {
  2582  			default:
  2583  				// Assuming the previous segment was
  2584  				// aligned, the following rounding
  2585  				// should ensure that this segment's
  2586  				// VA ≡ Fileoff mod FlagRound.
  2587  				seg.Fileoff = uint64(Rnd(int64(prev.Fileoff+prev.Filelen), int64(*FlagRound)))
  2588  				if seg.Vaddr%uint64(*FlagRound) != seg.Fileoff%uint64(*FlagRound) {
  2589  					Exitf("bad segment rounding (Vaddr=%#x Fileoff=%#x FlagRound=%#x)", seg.Vaddr, seg.Fileoff, *FlagRound)
  2590  				}
  2591  			case objabi.Hwindows:
  2592  				seg.Fileoff = prev.Fileoff + uint64(Rnd(int64(prev.Filelen), PEFILEALIGN))
  2593  			case objabi.Hplan9:
  2594  				seg.Fileoff = prev.Fileoff + prev.Filelen
  2595  			}
  2596  		}
  2597  		if seg != &Segdata {
  2598  			// Link.address already set Segdata.Filelen to
  2599  			// account for BSS.
  2600  			seg.Filelen = seg.Length
  2601  		}
  2602  		prev = seg
  2603  	}
  2604  	return prev.Fileoff + prev.Filelen
  2605  }
  2606  
  2607  // add a trampoline with symbol s (to be laid down after the current function)
  2608  func (ctxt *Link) AddTramp(s *loader.SymbolBuilder) {
  2609  	s.SetType(sym.STEXT)
  2610  	s.SetReachable(true)
  2611  	s.SetOnList(true)
  2612  	ctxt.tramps = append(ctxt.tramps, s.Sym())
  2613  	if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 {
  2614  		ctxt.Logf("trampoline %s inserted\n", s.Name())
  2615  	}
  2616  }
  2617  
  2618  // compressSyms compresses syms and returns the contents of the
  2619  // compressed section. If the section would get larger, it returns nil.
  2620  func compressSyms(ctxt *Link, syms []loader.Sym) []byte {
  2621  	ldr := ctxt.loader
  2622  	var total int64
  2623  	for _, sym := range syms {
  2624  		total += ldr.SymSize(sym)
  2625  	}
  2626  
  2627  	var buf bytes.Buffer
  2628  	buf.Write([]byte("ZLIB"))
  2629  	var sizeBytes [8]byte
  2630  	binary.BigEndian.PutUint64(sizeBytes[:], uint64(total))
  2631  	buf.Write(sizeBytes[:])
  2632  
  2633  	var relocbuf []byte // temporary buffer for applying relocations
  2634  
  2635  	// Using zlib.BestSpeed achieves very nearly the same
  2636  	// compression levels of zlib.DefaultCompression, but takes
  2637  	// substantially less time. This is important because DWARF
  2638  	// compression can be a significant fraction of link time.
  2639  	z, err := zlib.NewWriterLevel(&buf, zlib.BestSpeed)
  2640  	if err != nil {
  2641  		log.Fatalf("NewWriterLevel failed: %s", err)
  2642  	}
  2643  	st := ctxt.makeRelocSymState()
  2644  	for _, s := range syms {
  2645  		// Symbol data may be read-only. Apply relocations in a
  2646  		// temporary buffer, and immediately write it out.
  2647  		P := ldr.Data(s)
  2648  		relocs := ldr.Relocs(s)
  2649  		if relocs.Count() != 0 {
  2650  			relocbuf = append(relocbuf[:0], P...)
  2651  			P = relocbuf
  2652  			st.relocsym(s, P)
  2653  		}
  2654  		if _, err := z.Write(P); err != nil {
  2655  			log.Fatalf("compression failed: %s", err)
  2656  		}
  2657  		for i := ldr.SymSize(s) - int64(len(P)); i > 0; {
  2658  			b := zeros[:]
  2659  			if i < int64(len(b)) {
  2660  				b = b[:i]
  2661  			}
  2662  			n, err := z.Write(b)
  2663  			if err != nil {
  2664  				log.Fatalf("compression failed: %s", err)
  2665  			}
  2666  			i -= int64(n)
  2667  		}
  2668  	}
  2669  	if err := z.Close(); err != nil {
  2670  		log.Fatalf("compression failed: %s", err)
  2671  	}
  2672  	if int64(buf.Len()) >= total {
  2673  		// Compression didn't save any space.
  2674  		return nil
  2675  	}
  2676  	return buf.Bytes()
  2677  }
  2678  

View as plain text