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

     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/loadpe"
    41  	"cmd/link/internal/sym"
    42  	"compress/zlib"
    43  	"debug/elf"
    44  	"encoding/binary"
    45  	"fmt"
    46  	"internal/abi"
    47  	"log"
    48  	"math/rand"
    49  	"os"
    50  	"sort"
    51  	"strconv"
    52  	"strings"
    53  	"sync"
    54  	"sync/atomic"
    55  )
    56  
    57  // isRuntimeDepPkg reports whether pkg is the runtime package or its dependency.
    58  func isRuntimeDepPkg(pkg string) bool {
    59  	return objabi.LookupPkgSpecial(pkg).Runtime
    60  }
    61  
    62  // Estimate the max size needed to hold any new trampolines created for this function. This
    63  // is used to determine when the section can be split if it becomes too large, to ensure that
    64  // the trampolines are in the same section as the function that uses them.
    65  func maxSizeTrampolines(ctxt *Link, ldr *loader.Loader, s loader.Sym, isTramp bool) uint64 {
    66  	// If thearch.Trampoline is nil, then trampoline support is not available on this arch.
    67  	// A trampoline does not need any dependent trampolines.
    68  	if thearch.Trampoline == nil || isTramp {
    69  		return 0
    70  	}
    71  
    72  	n := uint64(0)
    73  	relocs := ldr.Relocs(s)
    74  	for ri := 0; ri < relocs.Count(); ri++ {
    75  		r := relocs.At(ri)
    76  		if r.Type().IsDirectCallOrJump() {
    77  			n++
    78  		}
    79  	}
    80  
    81  	switch {
    82  	case ctxt.IsARM():
    83  		return n * 20 // Trampolines in ARM range from 3 to 5 instructions.
    84  	case ctxt.IsARM64():
    85  		return n * 12 // Trampolines in ARM64 are 3 instructions.
    86  	case ctxt.IsLOONG64():
    87  		return n * 12 // Trampolines in LOONG64 are 3 instructions.
    88  	case ctxt.IsPPC64():
    89  		return n * 16 // Trampolines in PPC64 are 4 instructions.
    90  	case ctxt.IsRISCV64():
    91  		return n * 8 // Trampolines in RISCV64 are 2 instructions.
    92  	}
    93  	panic("unreachable")
    94  }
    95  
    96  // Detect too-far jumps in function s, and add trampolines if necessary.
    97  // ARM, LOONG64, PPC64, PPC64LE and RISCV64 support trampoline insertion for internal
    98  // and external linking. On PPC64 and PPC64LE the text sections might be split
    99  // but will still insert trampolines where necessary.
   100  func trampoline(ctxt *Link, s loader.Sym) {
   101  	if thearch.Trampoline == nil {
   102  		return // no need or no support of trampolines on this arch
   103  	}
   104  
   105  	ldr := ctxt.loader
   106  	relocs := ldr.Relocs(s)
   107  	for ri := 0; ri < relocs.Count(); ri++ {
   108  		r := relocs.At(ri)
   109  		rt := r.Type()
   110  		if !rt.IsDirectCallOrJump() && !isPLTCall(ctxt.Arch, rt) {
   111  			continue
   112  		}
   113  		rs := r.Sym()
   114  		if !ldr.AttrReachable(rs) || ldr.SymType(rs) == sym.Sxxx {
   115  			continue // something is wrong. skip it here and we'll emit a better error later
   116  		}
   117  
   118  		if ldr.SymValue(rs) == 0 && ldr.SymType(rs) != sym.SDYNIMPORT && ldr.SymType(rs) != sym.SUNDEFEXT {
   119  			// Symbols in the same package are laid out together (if we
   120  			// don't randomize the function order).
   121  			// Except that if SymPkg(s) == "", it is a host object symbol
   122  			// which may call an external symbol via PLT.
   123  			if ldr.SymPkg(s) != "" && ldr.SymPkg(rs) == ldr.SymPkg(s) && ldr.SymType(rs) == ldr.SymType(s) && *flagRandLayout == 0 {
   124  				// RISC-V is only able to reach +/-1MiB via a JAL instruction.
   125  				// We need to generate a trampoline when an address is
   126  				// currently unknown.
   127  				if !ctxt.Target.IsRISCV64() {
   128  					continue
   129  				}
   130  			}
   131  			// Runtime packages are laid out together.
   132  			if isRuntimeDepPkg(ldr.SymPkg(s)) && isRuntimeDepPkg(ldr.SymPkg(rs)) && *flagRandLayout == 0 {
   133  				continue
   134  			}
   135  		}
   136  		thearch.Trampoline(ctxt, ldr, ri, rs, s)
   137  	}
   138  }
   139  
   140  // whether rt is a (host object) relocation that will be turned into
   141  // a call to PLT.
   142  func isPLTCall(arch *sys.Arch, rt objabi.RelocType) bool {
   143  	const pcrel = 1
   144  	switch uint32(arch.Family) | uint32(rt)<<8 {
   145  	// ARM64
   146  	case uint32(sys.ARM64) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_AARCH64_CALL26))<<8,
   147  		uint32(sys.ARM64) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_AARCH64_JUMP26))<<8,
   148  		uint32(sys.ARM64) | uint32(objabi.MachoRelocOffset+MACHO_ARM64_RELOC_BRANCH26*2+pcrel)<<8:
   149  		return true
   150  
   151  	// ARM
   152  	case uint32(sys.ARM) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_ARM_CALL))<<8,
   153  		uint32(sys.ARM) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_ARM_PC24))<<8,
   154  		uint32(sys.ARM) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_ARM_JUMP24))<<8:
   155  		return true
   156  
   157  	// Loong64
   158  	case uint32(sys.Loong64) | uint32(objabi.ElfRelocOffset+objabi.RelocType(elf.R_LARCH_B26))<<8:
   159  		return true
   160  	}
   161  	// TODO: other architectures.
   162  	return false
   163  }
   164  
   165  // FoldSubSymbolOffset computes the offset of symbol s to its top-level outer
   166  // symbol. Returns the top-level symbol and the offset.
   167  // This is used in generating external relocations.
   168  func FoldSubSymbolOffset(ldr *loader.Loader, s loader.Sym) (loader.Sym, int64) {
   169  	outer := ldr.OuterSym(s)
   170  	off := int64(0)
   171  	if outer != 0 {
   172  		off += ldr.SymValue(s) - ldr.SymValue(outer)
   173  		s = outer
   174  	}
   175  	return s, off
   176  }
   177  
   178  // relocsym resolve relocations in "s", updating the symbol's content
   179  // in "P".
   180  // The main loop walks through the list of relocations attached to "s"
   181  // and resolves them where applicable. Relocations are often
   182  // architecture-specific, requiring calls into the 'archreloc' and/or
   183  // 'archrelocvariant' functions for the architecture. When external
   184  // linking is in effect, it may not be  possible to completely resolve
   185  // the address/offset for a symbol, in which case the goal is to lay
   186  // the groundwork for turning a given relocation into an external reloc
   187  // (to be applied by the external linker). For more on how relocations
   188  // work in general, see
   189  //
   190  //	"Linkers and Loaders", by John R. Levine (Morgan Kaufmann, 1999), ch. 7
   191  //
   192  // This is a performance-critical function for the linker; be careful
   193  // to avoid introducing unnecessary allocations in the main loop.
   194  func (st *relocSymState) relocsym(s loader.Sym, P []byte) {
   195  	ldr := st.ldr
   196  	relocs := ldr.Relocs(s)
   197  	if relocs.Count() == 0 {
   198  		return
   199  	}
   200  	target := st.target
   201  	syms := st.syms
   202  	nExtReloc := 0 // number of external relocations
   203  	for ri := 0; ri < relocs.Count(); ri++ {
   204  		r := relocs.At(ri)
   205  		off := r.Off()
   206  		siz := int32(r.Siz())
   207  		rs := r.Sym()
   208  		rt := r.Type()
   209  		weak := r.Weak()
   210  		if off < 0 || off+siz > int32(len(P)) {
   211  			rname := ""
   212  			if rs != 0 {
   213  				rname = ldr.SymName(rs)
   214  			}
   215  			st.err.Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(P))
   216  			continue
   217  		}
   218  		if siz == 0 { // informational relocation - no work to do
   219  			continue
   220  		}
   221  
   222  		var rst sym.SymKind
   223  		if rs != 0 {
   224  			rst = ldr.SymType(rs)
   225  		}
   226  
   227  		if rs != 0 && (rst == sym.Sxxx || rst == sym.SXREF) {
   228  			// When putting the runtime but not main into a shared library
   229  			// these symbols are undefined and that's OK.
   230  			if target.IsShared() || target.IsPlugin() {
   231  				if ldr.SymName(rs) == "main.main" || (!target.IsPlugin() && ldr.SymName(rs) == "main..inittask") {
   232  					sb := ldr.MakeSymbolUpdater(rs)
   233  					sb.SetType(sym.SDYNIMPORT)
   234  				} else if strings.HasPrefix(ldr.SymName(rs), "go:info.") {
   235  					// Skip go.info symbols. They are only needed to communicate
   236  					// DWARF info between the compiler and linker.
   237  					continue
   238  				}
   239  			} else if target.IsPPC64() && ldr.SymName(rs) == ".TOC." {
   240  				// TOC symbol doesn't have a type but we do assign a value
   241  				// (see the address pass) and we can resolve it.
   242  				// TODO: give it a type.
   243  			} else {
   244  				st.err.errorUnresolved(ldr, s, rs)
   245  				continue
   246  			}
   247  		}
   248  
   249  		if rt >= objabi.ElfRelocOffset {
   250  			continue
   251  		}
   252  
   253  		// We need to be able to reference dynimport symbols when linking against
   254  		// shared libraries, and AIX, Darwin, OpenBSD and Solaris always need it.
   255  		if !target.IsAIX() && !target.IsDarwin() && !target.IsSolaris() && !target.IsOpenbsd() && rs != 0 && rst == sym.SDYNIMPORT && !target.IsDynlinkingGo() && !ldr.AttrSubSymbol(rs) {
   256  			if !(target.IsPPC64() && target.IsExternal() && ldr.SymName(rs) == ".TOC.") {
   257  				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))
   258  			}
   259  		}
   260  		if rs != 0 && rst != sym.STLSBSS && !weak && rt != objabi.R_METHODOFF && !ldr.AttrReachable(rs) {
   261  			st.err.Errorf(s, "unreachable sym in relocation: %s", ldr.SymName(rs))
   262  		}
   263  
   264  		var rv sym.RelocVariant
   265  		if target.IsPPC64() || target.IsS390X() {
   266  			rv = ldr.RelocVariant(s, ri)
   267  		}
   268  
   269  		// TODO(mundaym): remove this special case - see issue 14218.
   270  		if target.IsS390X() {
   271  			switch rt {
   272  			case objabi.R_PCRELDBL:
   273  				rt = objabi.R_PCREL
   274  				rv = sym.RV_390_DBL
   275  			case objabi.R_CALL:
   276  				rv = sym.RV_390_DBL
   277  			}
   278  		}
   279  
   280  		var o int64
   281  		switch rt {
   282  		default:
   283  			switch siz {
   284  			default:
   285  				st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs))
   286  			case 1:
   287  				o = int64(P[off])
   288  			case 2:
   289  				o = int64(target.Arch.ByteOrder.Uint16(P[off:]))
   290  			case 4:
   291  				o = int64(target.Arch.ByteOrder.Uint32(P[off:]))
   292  			case 8:
   293  				o = int64(target.Arch.ByteOrder.Uint64(P[off:]))
   294  			}
   295  			out, n, ok := thearch.Archreloc(target, ldr, syms, r, s, o)
   296  			if target.IsExternal() {
   297  				nExtReloc += n
   298  			}
   299  			if ok {
   300  				o = out
   301  			} else {
   302  				st.err.Errorf(s, "unknown reloc to %v: %d (%s)", ldr.SymName(rs), rt, sym.RelocName(target.Arch, rt))
   303  			}
   304  		case objabi.R_TLS_LE:
   305  			if target.IsExternal() && target.IsElf() {
   306  				nExtReloc++
   307  				o = 0
   308  				if !target.IsAMD64() {
   309  					o = r.Add()
   310  				}
   311  				break
   312  			}
   313  
   314  			if target.IsElf() && target.IsARM() {
   315  				// On ELF ARM, the thread pointer is 8 bytes before
   316  				// the start of the thread-local data block, so add 8
   317  				// to the actual TLS offset (r->sym->value).
   318  				// This 8 seems to be a fundamental constant of
   319  				// ELF on ARM (or maybe Glibc on ARM); it is not
   320  				// related to the fact that our own TLS storage happens
   321  				// to take up 8 bytes.
   322  				o = 8 + ldr.SymValue(rs)
   323  			} else if target.IsElf() || target.IsPlan9() || target.IsDarwin() {
   324  				o = int64(syms.Tlsoffset) + r.Add()
   325  			} else if target.IsWindows() {
   326  				o = r.Add()
   327  			} else {
   328  				log.Fatalf("unexpected R_TLS_LE relocation for %v", target.HeadType)
   329  			}
   330  		case objabi.R_TLS_IE:
   331  			if target.IsExternal() && target.IsElf() {
   332  				nExtReloc++
   333  				o = 0
   334  				if !target.IsAMD64() {
   335  					o = r.Add()
   336  				}
   337  				if target.Is386() {
   338  					nExtReloc++ // need two ELF relocations on 386, see ../x86/asm.go:elfreloc1
   339  				}
   340  				break
   341  			}
   342  			if target.IsPIE() && target.IsElf() {
   343  				// We are linking the final executable, so we
   344  				// can optimize any TLS IE relocation to LE.
   345  				if thearch.TLSIEtoLE == nil {
   346  					log.Fatalf("internal linking of TLS IE not supported on %v", target.Arch.Family)
   347  				}
   348  				thearch.TLSIEtoLE(P, int(off), int(siz))
   349  				o = int64(syms.Tlsoffset)
   350  			} else {
   351  				log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", ldr.SymName(s))
   352  			}
   353  		case objabi.R_ADDR, objabi.R_PEIMAGEOFF:
   354  			if weak && !ldr.AttrReachable(rs) {
   355  				// Redirect it to runtime.unreachableMethod, which will throw if called.
   356  				rs = syms.unreachableMethod
   357  			}
   358  			if target.IsExternal() {
   359  				nExtReloc++
   360  
   361  				// set up addend for eventual relocation via outer symbol.
   362  				rs := rs
   363  				rs, off := FoldSubSymbolOffset(ldr, rs)
   364  				xadd := r.Add() + off
   365  				rst := ldr.SymType(rs)
   366  				if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil {
   367  					st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs))
   368  				}
   369  
   370  				o = xadd
   371  				if target.IsElf() {
   372  					if target.IsAMD64() {
   373  						o = 0
   374  					}
   375  				} else if target.IsDarwin() {
   376  					if ldr.SymType(s).IsDWARF() {
   377  						// We generally use symbol-targeted relocations.
   378  						// DWARF tools seem to only handle section-targeted relocations,
   379  						// so generate section-targeted relocations in DWARF sections.
   380  						// See also machoreloc1.
   381  						o += ldr.SymValue(rs)
   382  					}
   383  				} else if target.IsWindows() {
   384  					// nothing to do
   385  				} else if target.IsAIX() {
   386  					o = ldr.SymValue(rs) + xadd
   387  				} else {
   388  					st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType)
   389  				}
   390  
   391  				break
   392  			}
   393  
   394  			// On AIX, a second relocation must be done by the loader,
   395  			// as section addresses can change once loaded.
   396  			// The "default" symbol address is still needed by the loader so
   397  			// the current relocation can't be skipped.
   398  			if target.IsAIX() && rst != sym.SDYNIMPORT {
   399  				// It's not possible to make a loader relocation in a
   400  				// symbol which is not inside .data section.
   401  				// FIXME: It should be forbidden to have R_ADDR from a
   402  				// symbol which isn't in .data. However, as .text has the
   403  				// same address once loaded, this is possible.
   404  				// TODO: .text (including rodata) to .data relocation
   405  				// doesn't work correctly, so we should really disallow it.
   406  				// See also aixStaticDataBase in symtab.go and in runtime.
   407  				if ldr.SymSect(s).Seg == &Segdata {
   408  					Xcoffadddynrel(target, ldr, syms, s, r, ri)
   409  				}
   410  			}
   411  
   412  			o = ldr.SymValue(rs) + r.Add()
   413  			if rt == objabi.R_PEIMAGEOFF {
   414  				// The R_PEIMAGEOFF offset is a RVA, so subtract
   415  				// the base address for the executable.
   416  				o -= PEBASE
   417  			}
   418  
   419  			// On amd64, 4-byte offsets will be sign-extended, so it is impossible to
   420  			// access more than 2GB of static data; fail at link time is better than
   421  			// fail at runtime. See https://golang.org/issue/7980.
   422  			// Instead of special casing only amd64, we treat this as an error on all
   423  			// 64-bit architectures so as to be future-proof.
   424  			if int32(o) < 0 && target.Arch.PtrSize > 4 && siz == 4 {
   425  				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())
   426  				errorexit()
   427  			}
   428  		case objabi.R_DWTXTADDR_U1, objabi.R_DWTXTADDR_U2, objabi.R_DWTXTADDR_U3, objabi.R_DWTXTADDR_U4:
   429  			unit := ldr.SymUnit(rs)
   430  			if idx, ok := unit.Addrs[sym.LoaderSym(rs)]; ok {
   431  				o = int64(idx)
   432  			} else {
   433  				st.err.Errorf(s, "missing .debug_addr index relocation target %s", ldr.SymName(rs))
   434  			}
   435  
   436  			// For these relocations we write a ULEB128, but using a
   437  			// cooked/hacked recipe that ensures the result has a
   438  			// fixed length. That is, if we're writing a value of 1
   439  			// with length requirement 3, we'll actually emit three
   440  			// bytes, 0x81 0x80 0x0.
   441  			_, leb128len := rt.DwTxtAddrRelocParams()
   442  			if err := writeUleb128FixedLength(P[off:], uint64(o), leb128len); err != nil {
   443  				st.err.Errorf(s, "internal error: %v applying %s to DWARF sym with reloc target %s", err, rt.String(), ldr.SymName(rs))
   444  			}
   445  			continue
   446  
   447  		case objabi.R_DWARFSECREF:
   448  			if ldr.SymSect(rs) == nil {
   449  				st.err.Errorf(s, "missing DWARF section for relocation target %s", ldr.SymName(rs))
   450  			}
   451  
   452  			if target.IsExternal() {
   453  				// On most platforms, the external linker needs to adjust DWARF references
   454  				// as it combines DWARF sections. However, on Darwin, dsymutil does the
   455  				// DWARF linking, and it understands how to follow section offsets.
   456  				// Leaving in the relocation records confuses it (see
   457  				// https://golang.org/issue/22068) so drop them for Darwin.
   458  				if !target.IsDarwin() {
   459  					nExtReloc++
   460  				}
   461  
   462  				xadd := r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr)
   463  
   464  				o = xadd
   465  				if target.IsElf() && target.IsAMD64() {
   466  					o = 0
   467  				}
   468  				break
   469  			}
   470  			o = ldr.SymValue(rs) + r.Add() - int64(ldr.SymSect(rs).Vaddr)
   471  		case objabi.R_METHODOFF:
   472  			if !ldr.AttrReachable(rs) {
   473  				// Set it to a sentinel value. The runtime knows this is not pointing to
   474  				// anything valid.
   475  				o = -1
   476  				break
   477  			}
   478  			fallthrough
   479  		case objabi.R_ADDROFF:
   480  			if weak && !ldr.AttrReachable(rs) {
   481  				continue
   482  			}
   483  			sect := ldr.SymSect(rs)
   484  			if sect == nil {
   485  				if rst == sym.SDYNIMPORT {
   486  					st.err.Errorf(s, "cannot target DYNIMPORT sym in section-relative reloc: %s", ldr.SymName(rs))
   487  				} else if rst == sym.SUNDEFEXT {
   488  					st.err.Errorf(s, "undefined symbol in relocation: %s", ldr.SymName(rs))
   489  				} else {
   490  					st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs))
   491  				}
   492  				continue
   493  			}
   494  
   495  			// The method offset tables using this relocation expect the offset to be relative
   496  			// to the start of the first text section, even if there are multiple.
   497  			if sect.Name == ".text" {
   498  				o = ldr.SymValue(rs) - int64(Segtext.Sections[0].Vaddr) + r.Add()
   499  			} else {
   500  				o = ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) + r.Add()
   501  			}
   502  
   503  		case objabi.R_ADDRCUOFF:
   504  			// debug_range and debug_loc elements use this relocation type to get an
   505  			// offset from the start of the compile unit.
   506  			o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(loader.Sym(ldr.SymUnit(rs).Textp[0]))
   507  
   508  		// r.Sym() can be 0 when CALL $(constant) is transformed from absolute PC to relative PC call.
   509  		case objabi.R_GOTPCREL:
   510  			if target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 {
   511  				nExtReloc++
   512  				o = r.Add()
   513  				break
   514  			}
   515  			if target.Is386() && target.IsExternal() && target.IsELF {
   516  				nExtReloc++ // need two ELF relocations on 386, see ../x86/asm.go:elfreloc1
   517  			}
   518  			fallthrough
   519  		case objabi.R_CALL, objabi.R_PCREL:
   520  			if target.IsExternal() && rs != 0 && rst == sym.SUNDEFEXT {
   521  				// pass through to the external linker.
   522  				nExtReloc++
   523  				o = 0
   524  				break
   525  			}
   526  			if target.IsExternal() && rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) {
   527  				nExtReloc++
   528  
   529  				// set up addend for eventual relocation via outer symbol.
   530  				rs := rs
   531  				rs, off := FoldSubSymbolOffset(ldr, rs)
   532  				xadd := r.Add() + off - int64(siz) // relative to address after the relocated chunk
   533  				rst := ldr.SymType(rs)
   534  				if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && ldr.SymSect(rs) == nil {
   535  					st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs))
   536  				}
   537  
   538  				o = xadd
   539  				if target.IsElf() {
   540  					if target.IsAMD64() {
   541  						o = 0
   542  					}
   543  				} else if target.IsDarwin() {
   544  					if rt == objabi.R_CALL {
   545  						if target.IsExternal() && rst == sym.SDYNIMPORT {
   546  							if target.IsAMD64() {
   547  								// AMD64 dynamic relocations are relative to the end of the relocation.
   548  								o += int64(siz)
   549  							}
   550  						} else {
   551  							if rst != sym.SHOSTOBJ {
   552  								o += int64(uint64(ldr.SymValue(rs)) - ldr.SymSect(rs).Vaddr)
   553  							}
   554  							o -= int64(off) // relative to section offset, not symbol
   555  						}
   556  					} else {
   557  						o += int64(siz)
   558  					}
   559  				} else if target.IsWindows() && target.IsAMD64() { // only amd64 needs PCREL
   560  					// PE/COFF's PC32 relocation uses the address after the relocated
   561  					// bytes as the base. Compensate by skewing the addend.
   562  					o += int64(siz)
   563  				} else {
   564  					st.err.Errorf(s, "unhandled pcrel relocation to %s on %v", ldr.SymName(rs), target.HeadType)
   565  				}
   566  
   567  				break
   568  			}
   569  
   570  			o = 0
   571  			if rs != 0 {
   572  				o = ldr.SymValue(rs)
   573  			}
   574  
   575  			o += r.Add() - (ldr.SymValue(s) + int64(off) + int64(siz))
   576  		case objabi.R_SIZE:
   577  			o = ldr.SymSize(rs) + r.Add()
   578  
   579  		case objabi.R_XCOFFREF:
   580  			if !target.IsAIX() {
   581  				st.err.Errorf(s, "find XCOFF R_REF on non-XCOFF files")
   582  			}
   583  			if !target.IsExternal() {
   584  				st.err.Errorf(s, "find XCOFF R_REF with internal linking")
   585  			}
   586  			nExtReloc++
   587  			continue
   588  
   589  		case objabi.R_CONST:
   590  			o = r.Add()
   591  
   592  		case objabi.R_GOTOFF:
   593  			o = ldr.SymValue(rs) + r.Add() - ldr.SymValue(syms.GOT)
   594  		}
   595  
   596  		if target.IsPPC64() || target.IsS390X() {
   597  			if rv != sym.RV_NONE {
   598  				o = thearch.Archrelocvariant(target, ldr, r, rv, s, o, P)
   599  			}
   600  		}
   601  
   602  		switch siz {
   603  		default:
   604  			st.err.Errorf(s, "bad reloc size %#x for %s", uint32(siz), ldr.SymName(rs))
   605  		case 1:
   606  			P[off] = byte(int8(o))
   607  		case 2:
   608  			if (rt == objabi.R_PCREL || rt == objabi.R_CALL) && o != int64(int16(o)) {
   609  				st.err.Errorf(s, "pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), o)
   610  			} else if o != int64(int16(o)) && o != int64(uint16(o)) {
   611  				st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), uint64(o))
   612  			}
   613  			target.Arch.ByteOrder.PutUint16(P[off:], uint16(o))
   614  		case 4:
   615  			if (rt == objabi.R_PCREL || rt == objabi.R_CALL) && o != int64(int32(o)) {
   616  				st.err.Errorf(s, "pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), o)
   617  			} else if o != int64(int32(o)) && o != int64(uint32(o)) {
   618  				st.err.Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", ldr.SymName(rs), uint64(o))
   619  			}
   620  			target.Arch.ByteOrder.PutUint32(P[off:], uint32(o))
   621  		case 8:
   622  			target.Arch.ByteOrder.PutUint64(P[off:], uint64(o))
   623  		}
   624  	}
   625  	if target.IsExternal() {
   626  		// We'll stream out the external relocations in asmb2 (e.g. elfrelocsect)
   627  		// and we only need the count here.
   628  		atomic.AddUint32(&ldr.SymSect(s).Relcount, uint32(nExtReloc))
   629  	}
   630  }
   631  
   632  // Convert a Go relocation to an external relocation.
   633  func extreloc(ctxt *Link, ldr *loader.Loader, s loader.Sym, r loader.Reloc) (loader.ExtReloc, bool) {
   634  	var rr loader.ExtReloc
   635  	target := &ctxt.Target
   636  	siz := int32(r.Siz())
   637  	if siz == 0 { // informational relocation - no work to do
   638  		return rr, false
   639  	}
   640  
   641  	rt := r.Type()
   642  	if rt >= objabi.ElfRelocOffset {
   643  		return rr, false
   644  	}
   645  	rr.Type = rt
   646  	rr.Size = uint8(siz)
   647  
   648  	// TODO(mundaym): remove this special case - see issue 14218.
   649  	if target.IsS390X() {
   650  		switch rt {
   651  		case objabi.R_PCRELDBL:
   652  			rt = objabi.R_PCREL
   653  		}
   654  	}
   655  
   656  	switch rt {
   657  	default:
   658  		return thearch.Extreloc(target, ldr, r, s)
   659  
   660  	case objabi.R_TLS_LE, objabi.R_TLS_IE:
   661  		if target.IsElf() {
   662  			rs := r.Sym()
   663  			rr.Xsym = rs
   664  			if rr.Xsym == 0 {
   665  				rr.Xsym = ctxt.Tlsg
   666  			}
   667  			rr.Xadd = r.Add()
   668  			break
   669  		}
   670  		return rr, false
   671  
   672  	case objabi.R_ADDR, objabi.R_PEIMAGEOFF:
   673  		// set up addend for eventual relocation via outer symbol.
   674  		rs := r.Sym()
   675  		if r.Weak() && !ldr.AttrReachable(rs) {
   676  			rs = ctxt.ArchSyms.unreachableMethod
   677  		}
   678  		rs, off := FoldSubSymbolOffset(ldr, rs)
   679  		rr.Xadd = r.Add() + off
   680  		rr.Xsym = rs
   681  
   682  	case objabi.R_DWARFSECREF:
   683  		// On most platforms, the external linker needs to adjust DWARF references
   684  		// as it combines DWARF sections. However, on Darwin, dsymutil does the
   685  		// DWARF linking, and it understands how to follow section offsets.
   686  		// Leaving in the relocation records confuses it (see
   687  		// https://golang.org/issue/22068) so drop them for Darwin.
   688  		if target.IsDarwin() {
   689  			return rr, false
   690  		}
   691  		rs := r.Sym()
   692  		rr.Xsym = loader.Sym(ldr.SymSect(rs).Sym)
   693  		rr.Xadd = r.Add() + ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr)
   694  
   695  	// r.Sym() can be 0 when CALL $(constant) is transformed from absolute PC to relative PC call.
   696  	case objabi.R_GOTPCREL, objabi.R_CALL, objabi.R_PCREL:
   697  		rs := r.Sym()
   698  		if rt == objabi.R_GOTPCREL && target.IsDynlinkingGo() && target.IsDarwin() && rs != 0 {
   699  			rr.Xadd = r.Add()
   700  			rr.Xadd -= int64(siz) // relative to address after the relocated chunk
   701  			rr.Xsym = rs
   702  			break
   703  		}
   704  		if rs != 0 && ldr.SymType(rs) == sym.SUNDEFEXT {
   705  			// pass through to the external linker.
   706  			rr.Xadd = 0
   707  			if target.IsElf() {
   708  				rr.Xadd -= int64(siz)
   709  			}
   710  			rr.Xsym = rs
   711  			break
   712  		}
   713  		if rs != 0 && (ldr.SymSect(rs) != ldr.SymSect(s) || rt == objabi.R_GOTPCREL) {
   714  			// set up addend for eventual relocation via outer symbol.
   715  			rs := rs
   716  			rs, off := FoldSubSymbolOffset(ldr, rs)
   717  			rr.Xadd = r.Add() + off
   718  			rr.Xadd -= int64(siz) // relative to address after the relocated chunk
   719  			rr.Xsym = rs
   720  			break
   721  		}
   722  		return rr, false
   723  
   724  	case objabi.R_XCOFFREF:
   725  		return ExtrelocSimple(ldr, r), true
   726  
   727  	// These reloc types don't need external relocations.
   728  	case objabi.R_ADDROFF, objabi.R_METHODOFF, objabi.R_ADDRCUOFF,
   729  		objabi.R_SIZE, objabi.R_CONST, objabi.R_GOTOFF,
   730  		objabi.R_DWTXTADDR_U1, objabi.R_DWTXTADDR_U2,
   731  		objabi.R_DWTXTADDR_U3, objabi.R_DWTXTADDR_U4:
   732  		return rr, false
   733  	}
   734  	return rr, true
   735  }
   736  
   737  // ExtrelocSimple creates a simple external relocation from r, with the same
   738  // symbol and addend.
   739  func ExtrelocSimple(ldr *loader.Loader, r loader.Reloc) loader.ExtReloc {
   740  	var rr loader.ExtReloc
   741  	rs := r.Sym()
   742  	rr.Xsym = rs
   743  	rr.Xadd = r.Add()
   744  	rr.Type = r.Type()
   745  	rr.Size = r.Siz()
   746  	return rr
   747  }
   748  
   749  // ExtrelocViaOuterSym creates an external relocation from r targeting the
   750  // outer symbol and folding the subsymbol's offset into the addend.
   751  func ExtrelocViaOuterSym(ldr *loader.Loader, r loader.Reloc, s loader.Sym) loader.ExtReloc {
   752  	// set up addend for eventual relocation via outer symbol.
   753  	var rr loader.ExtReloc
   754  	rs := r.Sym()
   755  	rs, off := FoldSubSymbolOffset(ldr, rs)
   756  	rr.Xadd = r.Add() + off
   757  	rst := ldr.SymType(rs)
   758  	if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil {
   759  		ldr.Errorf(s, "missing section for %s", ldr.SymName(rs))
   760  	}
   761  	rr.Xsym = rs
   762  	rr.Type = r.Type()
   763  	rr.Size = r.Siz()
   764  	return rr
   765  }
   766  
   767  // relocSymState hold state information needed when making a series of
   768  // successive calls to relocsym(). The items here are invariant
   769  // (meaning that they are set up once initially and then don't change
   770  // during the execution of relocsym), with the exception of a slice
   771  // used to facilitate batch allocation of external relocations. Calls
   772  // to relocsym happen in parallel; the assumption is that each
   773  // parallel thread will have its own state object.
   774  type relocSymState struct {
   775  	target *Target
   776  	ldr    *loader.Loader
   777  	err    *ErrorReporter
   778  	syms   *ArchSyms
   779  }
   780  
   781  // makeRelocSymState creates a relocSymState container object to
   782  // pass to relocsym(). If relocsym() calls happen in parallel,
   783  // each parallel thread should have its own state object.
   784  func (ctxt *Link) makeRelocSymState() *relocSymState {
   785  	return &relocSymState{
   786  		target: &ctxt.Target,
   787  		ldr:    ctxt.loader,
   788  		err:    &ctxt.ErrorReporter,
   789  		syms:   &ctxt.ArchSyms,
   790  	}
   791  }
   792  
   793  // windynrelocsym examines a text symbol 's' and looks for relocations
   794  // from it that correspond to references to symbols defined in DLLs,
   795  // then fixes up those relocations as needed. A reference to a symbol
   796  // XYZ from some DLL will fall into one of two categories: an indirect
   797  // ref via "__imp_XYZ", or a direct ref to "XYZ". Here's an example of
   798  // an indirect ref (this is an excerpt from objdump -ldr):
   799  //
   800  //	     1c1: 48 89 c6                     	movq	%rax, %rsi
   801  //	     1c4: ff 15 00 00 00 00            	callq	*(%rip)
   802  //			00000000000001c6:  IMAGE_REL_AMD64_REL32	__imp__errno
   803  //
   804  // In the assembly above, the code loads up the value of __imp_errno
   805  // and then does an indirect call to that value.
   806  //
   807  // Here is what a direct reference might look like:
   808  //
   809  //	     137: e9 20 06 00 00               	jmp	0x75c <pow+0x75c>
   810  //	     13c: e8 00 00 00 00               	callq	0x141 <pow+0x141>
   811  //			000000000000013d:  IMAGE_REL_AMD64_REL32	_errno
   812  //
   813  // The assembly below dispenses with the import symbol and just makes
   814  // a direct call to _errno.
   815  //
   816  // The code below handles indirect refs by redirecting the target of
   817  // the relocation from "__imp_XYZ" to "XYZ" (since the latter symbol
   818  // is what the Windows loader is expected to resolve). For direct refs
   819  // the call is redirected to a stub, where the stub first loads the
   820  // symbol and then direct an indirect call to that value.
   821  //
   822  // Note that for a given symbol (as above) it is perfectly legal to
   823  // have both direct and indirect references.
   824  func windynrelocsym(ctxt *Link, rel *loader.SymbolBuilder, s loader.Sym) error {
   825  	var su *loader.SymbolBuilder
   826  	relocs := ctxt.loader.Relocs(s)
   827  	for ri := 0; ri < relocs.Count(); ri++ {
   828  		r := relocs.At(ri)
   829  		if r.IsMarker() {
   830  			continue // skip marker relocations
   831  		}
   832  		targ := r.Sym()
   833  		if targ == 0 {
   834  			continue
   835  		}
   836  		if !ctxt.loader.AttrReachable(targ) {
   837  			if r.Weak() {
   838  				continue
   839  			}
   840  			return fmt.Errorf("dynamic relocation to unreachable symbol %s",
   841  				ctxt.loader.SymName(targ))
   842  		}
   843  		tgot := ctxt.loader.SymGot(targ)
   844  		if tgot == loadpe.RedirectToDynImportGotToken {
   845  
   846  			// Consistency check: name should be __imp_X
   847  			sname := ctxt.loader.SymName(targ)
   848  			if !strings.HasPrefix(sname, "__imp_") {
   849  				return fmt.Errorf("internal error in windynrelocsym: redirect GOT token applied to non-import symbol %s", sname)
   850  			}
   851  
   852  			// Locate underlying symbol (which originally had type
   853  			// SDYNIMPORT but has since been retyped to SWINDOWS).
   854  			ds, err := loadpe.LookupBaseFromImport(targ, ctxt.loader, ctxt.Arch)
   855  			if err != nil {
   856  				return err
   857  			}
   858  			dstyp := ctxt.loader.SymType(ds)
   859  			if dstyp != sym.SWINDOWS {
   860  				return fmt.Errorf("internal error in windynrelocsym: underlying sym for %q has wrong type %s", sname, dstyp.String())
   861  			}
   862  
   863  			// Redirect relocation to the dynimport.
   864  			r.SetSym(ds)
   865  			continue
   866  		}
   867  
   868  		tplt := ctxt.loader.SymPlt(targ)
   869  		if tplt == loadpe.CreateImportStubPltToken {
   870  
   871  			// Consistency check: don't want to see both PLT and GOT tokens.
   872  			if tgot != -1 {
   873  				return fmt.Errorf("internal error in windynrelocsym: invalid GOT setting %d for reloc to %s", tgot, ctxt.loader.SymName(targ))
   874  			}
   875  
   876  			// make dynimport JMP table for PE object files.
   877  			tplt := int32(rel.Size())
   878  			ctxt.loader.SetPlt(targ, tplt)
   879  
   880  			if su == nil {
   881  				su = ctxt.loader.MakeSymbolUpdater(s)
   882  			}
   883  			r.SetSym(rel.Sym())
   884  			r.SetAdd(int64(tplt))
   885  
   886  			// jmp *addr
   887  			switch ctxt.Arch.Family {
   888  			default:
   889  				return fmt.Errorf("internal error in windynrelocsym: unsupported arch %v", ctxt.Arch.Family)
   890  			case sys.I386:
   891  				rel.AddUint8(0xff)
   892  				rel.AddUint8(0x25)
   893  				rel.AddAddrPlus(ctxt.Arch, targ, 0)
   894  				rel.AddUint8(0x90)
   895  				rel.AddUint8(0x90)
   896  			case sys.AMD64:
   897  				// The relocation symbol might be at an absolute offset
   898  				// higher than 32 bits, but the jump instruction can't
   899  				// encode more than 32 bit offsets. We use a jump
   900  				// relative to the instruction pointer to get around this
   901  				// limitation.
   902  				rel.AddUint8(0xff)
   903  				rel.AddUint8(0x25)
   904  				rel.AddPCRelPlus(ctxt.Arch, targ, 0)
   905  				rel.AddUint8(0x90)
   906  				rel.AddUint8(0x90)
   907  			}
   908  		} else if tplt >= 0 {
   909  			if su == nil {
   910  				su = ctxt.loader.MakeSymbolUpdater(s)
   911  			}
   912  			r.SetSym(rel.Sym())
   913  			r.SetAdd(int64(tplt))
   914  		}
   915  	}
   916  	return nil
   917  }
   918  
   919  // windynrelocsyms generates jump table to C library functions that will be
   920  // added later. windynrelocsyms writes the table into .rel symbol.
   921  func (ctxt *Link) windynrelocsyms() {
   922  	if !(ctxt.IsWindows() && iscgo && ctxt.IsInternal()) {
   923  		return
   924  	}
   925  
   926  	rel := ctxt.loader.CreateSymForUpdate(".rel", 0)
   927  	rel.SetType(sym.STEXT)
   928  
   929  	for _, s := range ctxt.Textp {
   930  		if err := windynrelocsym(ctxt, rel, s); err != nil {
   931  			ctxt.Errorf(s, "%v", err)
   932  		}
   933  	}
   934  
   935  	ctxt.Textp = append(ctxt.Textp, rel.Sym())
   936  }
   937  
   938  func dynrelocsym(ctxt *Link, s loader.Sym) {
   939  	target := &ctxt.Target
   940  	ldr := ctxt.loader
   941  	syms := &ctxt.ArchSyms
   942  	relocs := ldr.Relocs(s)
   943  	for ri := 0; ri < relocs.Count(); ri++ {
   944  		r := relocs.At(ri)
   945  		if r.IsMarker() {
   946  			continue // skip marker relocations
   947  		}
   948  		rSym := r.Sym()
   949  		if r.Weak() && !ldr.AttrReachable(rSym) {
   950  			continue
   951  		}
   952  		if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal {
   953  			// It's expected that some relocations will be done
   954  			// later by relocsym (R_TLS_LE, R_ADDROFF), so
   955  			// don't worry if Adddynrel returns false.
   956  			thearch.Adddynrel(target, ldr, syms, s, r, ri)
   957  			continue
   958  		}
   959  
   960  		if rSym != 0 && ldr.SymType(rSym) == sym.SDYNIMPORT || r.Type() >= objabi.ElfRelocOffset {
   961  			if rSym != 0 && !ldr.AttrReachable(rSym) {
   962  				ctxt.Errorf(s, "dynamic relocation to unreachable symbol %s", ldr.SymName(rSym))
   963  			}
   964  			if !thearch.Adddynrel(target, ldr, syms, s, r, ri) {
   965  				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))
   966  			}
   967  		}
   968  	}
   969  }
   970  
   971  func (state *dodataState) dynreloc(ctxt *Link) {
   972  	if ctxt.HeadType == objabi.Hwindows {
   973  		return
   974  	}
   975  	// -d suppresses dynamic loader format, so we may as well not
   976  	// compute these sections or mark their symbols as reachable.
   977  	if *FlagD {
   978  		return
   979  	}
   980  
   981  	for _, s := range ctxt.Textp {
   982  		dynrelocsym(ctxt, s)
   983  	}
   984  	for _, syms := range state.data {
   985  		for _, s := range syms {
   986  			dynrelocsym(ctxt, s)
   987  		}
   988  	}
   989  	if ctxt.IsELF {
   990  		elfdynhash(ctxt)
   991  	}
   992  }
   993  
   994  func CodeblkPad(ctxt *Link, out *OutBuf, addr int64, size int64, pad []byte) {
   995  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.Textp, addr, size, pad)
   996  }
   997  
   998  const blockSize = 1 << 20 // 1MB chunks written at a time.
   999  
  1000  // writeBlocks writes a specified chunk of symbols to the output buffer. It
  1001  // breaks the write up into ≥blockSize chunks to write them out, and schedules
  1002  // as many goroutines as necessary to accomplish this task. This call then
  1003  // blocks, waiting on the writes to complete. Note that we use the sem parameter
  1004  // to limit the number of concurrent writes taking place.
  1005  func writeBlocks(ctxt *Link, out *OutBuf, sem chan int, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) {
  1006  	for i, s := range syms {
  1007  		if ldr.SymValue(s) >= addr && !ldr.AttrSubSymbol(s) {
  1008  			syms = syms[i:]
  1009  			break
  1010  		}
  1011  	}
  1012  
  1013  	var wg sync.WaitGroup
  1014  	max, lastAddr, written := int64(blockSize), addr+size, int64(0)
  1015  	for addr < lastAddr {
  1016  		// Find the last symbol we'd write.
  1017  		idx := -1
  1018  		for i, s := range syms {
  1019  			if ldr.AttrSubSymbol(s) {
  1020  				continue
  1021  			}
  1022  
  1023  			// If the next symbol's size would put us out of bounds on the total length,
  1024  			// stop looking.
  1025  			end := ldr.SymValue(s) + ldr.SymSize(s)
  1026  			if end > lastAddr {
  1027  				break
  1028  			}
  1029  
  1030  			// We're gonna write this symbol.
  1031  			idx = i
  1032  
  1033  			// If we cross over the max size, we've got enough symbols.
  1034  			if end > addr+max {
  1035  				break
  1036  			}
  1037  		}
  1038  
  1039  		// If we didn't find any symbols to write, we're done here.
  1040  		if idx < 0 {
  1041  			break
  1042  		}
  1043  
  1044  		// Compute the length to write, including padding.
  1045  		// We need to write to the end address (lastAddr), or the next symbol's
  1046  		// start address, whichever comes first. If there is no more symbols,
  1047  		// just write to lastAddr. This ensures we don't leave holes between the
  1048  		// blocks or at the end.
  1049  		length := int64(0)
  1050  		if idx+1 < len(syms) {
  1051  			// Find the next top-level symbol.
  1052  			// Skip over sub symbols so we won't split a container symbol
  1053  			// into two blocks.
  1054  			next := syms[idx+1]
  1055  			for ldr.AttrSubSymbol(next) {
  1056  				idx++
  1057  				next = syms[idx+1]
  1058  			}
  1059  			length = ldr.SymValue(next) - addr
  1060  		}
  1061  		if length == 0 || length > lastAddr-addr {
  1062  			length = lastAddr - addr
  1063  		}
  1064  
  1065  		// Start the block output operator.
  1066  		if ctxt.Out.isMmapped() {
  1067  			o := out.View(uint64(out.Offset() + written))
  1068  			sem <- 1
  1069  			wg.Add(1)
  1070  			go func(o *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) {
  1071  				writeBlock(ctxt, o, ldr, syms, addr, size, pad)
  1072  				wg.Done()
  1073  				<-sem
  1074  			}(o, ldr, syms, addr, length, pad)
  1075  		} else { // output not mmaped, don't parallelize.
  1076  			writeBlock(ctxt, out, ldr, syms, addr, length, pad)
  1077  		}
  1078  
  1079  		// Prepare for the next loop.
  1080  		if idx != -1 {
  1081  			syms = syms[idx+1:]
  1082  		}
  1083  		written += length
  1084  		addr += length
  1085  	}
  1086  	wg.Wait()
  1087  }
  1088  
  1089  func writeBlock(ctxt *Link, out *OutBuf, ldr *loader.Loader, syms []loader.Sym, addr, size int64, pad []byte) {
  1090  
  1091  	st := ctxt.makeRelocSymState()
  1092  
  1093  	// This doesn't distinguish the memory size from the file
  1094  	// size, and it lays out the file based on Symbol.Value, which
  1095  	// is the virtual address. DWARF compression changes file sizes,
  1096  	// so dwarfcompress will fix this up later if necessary.
  1097  	eaddr := addr + size
  1098  	var prev loader.Sym
  1099  	for _, s := range syms {
  1100  		if ldr.AttrSubSymbol(s) {
  1101  			continue
  1102  		}
  1103  		val := ldr.SymValue(s)
  1104  		if val >= eaddr {
  1105  			break
  1106  		}
  1107  		if val < addr {
  1108  			ldr.Errorf(s, "phase error: addr=%#x but val=%#x sym=%s type=%v sect=%v sect.addr=%#x prev=%s", addr, val, ldr.SymName(s), ldr.SymType(s), ldr.SymSect(s).Name, ldr.SymSect(s).Vaddr, ldr.SymName(prev))
  1109  			errorexit()
  1110  		}
  1111  		prev = s
  1112  		if addr < val {
  1113  			out.WriteStringPad("", int(val-addr), pad)
  1114  			addr = val
  1115  		}
  1116  		P := out.WriteSym(ldr, s)
  1117  		st.relocsym(s, P)
  1118  		if ldr.IsGeneratedSym(s) {
  1119  			f := ctxt.generatorSyms[s]
  1120  			f(ctxt, s)
  1121  		}
  1122  		addr += int64(len(P))
  1123  		siz := ldr.SymSize(s)
  1124  		if addr < val+siz {
  1125  			out.WriteStringPad("", int(val+siz-addr), pad)
  1126  			addr = val + siz
  1127  		}
  1128  		if addr != val+siz {
  1129  			ldr.Errorf(s, "phase error: addr=%#x value+size=%#x", addr, val+siz)
  1130  			errorexit()
  1131  		}
  1132  		if val+siz >= eaddr {
  1133  			break
  1134  		}
  1135  	}
  1136  
  1137  	if addr < eaddr {
  1138  		out.WriteStringPad("", int(eaddr-addr), pad)
  1139  	}
  1140  }
  1141  
  1142  type writeFn func(*Link, *OutBuf, int64, int64)
  1143  
  1144  // writeParallel handles scheduling parallel execution of data write functions.
  1145  func writeParallel(wg *sync.WaitGroup, fn writeFn, ctxt *Link, seek, vaddr, length uint64) {
  1146  	if ctxt.Out.isMmapped() {
  1147  		out := ctxt.Out.View(seek)
  1148  		wg.Add(1)
  1149  		go func() {
  1150  			defer wg.Done()
  1151  			fn(ctxt, out, int64(vaddr), int64(length))
  1152  		}()
  1153  	} else {
  1154  		ctxt.Out.SeekSet(int64(seek))
  1155  		fn(ctxt, ctxt.Out, int64(vaddr), int64(length))
  1156  	}
  1157  }
  1158  
  1159  func datblk(ctxt *Link, out *OutBuf, addr, size int64) {
  1160  	writeDatblkToOutBuf(ctxt, out, addr, size)
  1161  }
  1162  
  1163  // Used only on Wasm for now.
  1164  func DatblkBytes(ctxt *Link, addr int64, size int64) []byte {
  1165  	buf := make([]byte, size)
  1166  	out := &OutBuf{heap: buf}
  1167  	writeDatblkToOutBuf(ctxt, out, addr, size)
  1168  	return buf
  1169  }
  1170  
  1171  func writeDatblkToOutBuf(ctxt *Link, out *OutBuf, addr int64, size int64) {
  1172  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, ctxt.datap, addr, size, zeros[:])
  1173  }
  1174  
  1175  func dwarfblk(ctxt *Link, out *OutBuf, addr int64, size int64) {
  1176  	// Concatenate the section symbol lists into a single list to pass
  1177  	// to writeBlocks.
  1178  	//
  1179  	// NB: ideally we would do a separate writeBlocks call for each
  1180  	// section, but this would run the risk of undoing any file offset
  1181  	// adjustments made during layout.
  1182  	n := 0
  1183  	for i := range dwarfp {
  1184  		n += len(dwarfp[i].syms)
  1185  	}
  1186  	syms := make([]loader.Sym, 0, n)
  1187  	for i := range dwarfp {
  1188  		syms = append(syms, dwarfp[i].syms...)
  1189  	}
  1190  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, syms, addr, size, zeros[:])
  1191  }
  1192  
  1193  func pdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) {
  1194  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, sehp.pdata, addr, size, zeros[:])
  1195  }
  1196  
  1197  func xdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) {
  1198  	writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, sehp.xdata, addr, size, zeros[:])
  1199  }
  1200  
  1201  var covCounterDataStartOff, covCounterDataLen uint64
  1202  
  1203  var zeros [512]byte
  1204  
  1205  var (
  1206  	strdata  = make(map[string]string)
  1207  	strnames []string
  1208  )
  1209  
  1210  func addstrdata1(ctxt *Link, arg string) {
  1211  	eq := strings.Index(arg, "=")
  1212  	dot := strings.LastIndex(arg[:eq+1], ".")
  1213  	if eq < 0 || dot < 0 {
  1214  		Exitf("-X flag requires argument of the form importpath.name=value")
  1215  	}
  1216  	pkg := arg[:dot]
  1217  	if ctxt.BuildMode == BuildModePlugin && pkg == "main" {
  1218  		pkg = *flagPluginPath
  1219  	}
  1220  	pkg = objabi.PathToPrefix(pkg)
  1221  	name := pkg + arg[dot:eq]
  1222  	value := arg[eq+1:]
  1223  	if _, ok := strdata[name]; !ok {
  1224  		strnames = append(strnames, name)
  1225  	}
  1226  	strdata[name] = value
  1227  }
  1228  
  1229  // addstrdata sets the initial value of the string variable name to value.
  1230  func addstrdata(arch *sys.Arch, l *loader.Loader, name, value string) {
  1231  	s := l.Lookup(name, 0)
  1232  	if s == 0 {
  1233  		return
  1234  	}
  1235  	if goType := l.SymGoType(s); goType == 0 {
  1236  		return
  1237  	} else if typeName := l.SymName(goType); typeName != "type:string" {
  1238  		Errorf("%s: cannot set with -X: not a var of type string (%s)", name, typeName)
  1239  		return
  1240  	}
  1241  	if !l.AttrReachable(s) {
  1242  		return // don't bother setting unreachable variable
  1243  	}
  1244  	bld := l.MakeSymbolUpdater(s)
  1245  	if bld.Type() == sym.SBSS {
  1246  		bld.SetType(sym.SDATA)
  1247  	}
  1248  
  1249  	p := fmt.Sprintf("%s.str", name)
  1250  	sbld := l.CreateSymForUpdate(p, 0)
  1251  	sbld.Addstring(value)
  1252  	sbld.SetType(sym.SRODATA)
  1253  
  1254  	// Don't reset the variable's size. String variable usually has size of
  1255  	// 2*PtrSize, but in ASAN build it can be larger due to red zone.
  1256  	// (See issue 56175.)
  1257  	bld.SetData(make([]byte, arch.PtrSize*2))
  1258  	bld.SetReadOnly(false)
  1259  	bld.ResetRelocs()
  1260  	bld.SetAddrPlus(arch, 0, sbld.Sym(), 0)
  1261  	bld.SetUint(arch, int64(arch.PtrSize), uint64(len(value)))
  1262  }
  1263  
  1264  func (ctxt *Link) dostrdata() {
  1265  	for _, name := range strnames {
  1266  		addstrdata(ctxt.Arch, ctxt.loader, name, strdata[name])
  1267  	}
  1268  }
  1269  
  1270  // addgostring adds str, as a Go string value, to s. symname is the name of the
  1271  // symbol used to define the string data and must be unique per linked object.
  1272  func addgostring(ctxt *Link, ldr *loader.Loader, s *loader.SymbolBuilder, symname, str string) {
  1273  	sdata := ldr.CreateSymForUpdate(symname, 0)
  1274  	if sdata.Type() != sym.Sxxx {
  1275  		ctxt.Errorf(s.Sym(), "duplicate symname in addgostring: %s", symname)
  1276  	}
  1277  	sdata.SetLocal(true)
  1278  	sdata.SetType(sym.SRODATA)
  1279  	sdata.SetSize(int64(len(str)))
  1280  	sdata.SetData([]byte(str))
  1281  	s.AddAddr(ctxt.Arch, sdata.Sym())
  1282  	s.AddUint(ctxt.Arch, uint64(len(str)))
  1283  }
  1284  
  1285  func addinitarrdata(ctxt *Link, ldr *loader.Loader, s loader.Sym) {
  1286  	p := ldr.SymName(s) + ".ptr"
  1287  	sp := ldr.CreateSymForUpdate(p, 0)
  1288  	sp.SetType(sym.SINITARR)
  1289  	sp.SetSize(0)
  1290  	sp.SetDuplicateOK(true)
  1291  	sp.AddAddr(ctxt.Arch, s)
  1292  }
  1293  
  1294  // symalign returns the required alignment for the given symbol s.
  1295  func symalign(ldr *loader.Loader, s loader.Sym) int32 {
  1296  	min := int32(thearch.Minalign)
  1297  	align := ldr.SymAlign(s)
  1298  	if align >= min {
  1299  		return align
  1300  	} else if align != 0 {
  1301  		return min
  1302  	}
  1303  	align = int32(thearch.Maxalign)
  1304  	ssz := ldr.SymSize(s)
  1305  	for int64(align) > ssz && align > min {
  1306  		align >>= 1
  1307  	}
  1308  	ldr.SetSymAlign(s, align)
  1309  	return align
  1310  }
  1311  
  1312  func aligndatsize(state *dodataState, datsize int64, s loader.Sym) int64 {
  1313  	return Rnd(datsize, int64(symalign(state.ctxt.loader, s)))
  1314  }
  1315  
  1316  const debugGCProg = false
  1317  
  1318  type GCProg struct {
  1319  	ctxt *Link
  1320  	sym  *loader.SymbolBuilder
  1321  	w    gcprog.Writer
  1322  }
  1323  
  1324  func (p *GCProg) Init(ctxt *Link, name string) {
  1325  	p.ctxt = ctxt
  1326  	p.sym = ctxt.loader.CreateSymForUpdate(name, 0)
  1327  	p.w.Init(p.writeByte())
  1328  	if debugGCProg {
  1329  		fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name)
  1330  		p.w.Debug(os.Stderr)
  1331  	}
  1332  }
  1333  
  1334  func (p *GCProg) writeByte() func(x byte) {
  1335  	return func(x byte) {
  1336  		p.sym.AddUint8(x)
  1337  	}
  1338  }
  1339  
  1340  func (p *GCProg) End(size int64) {
  1341  	p.w.ZeroUntil(size / int64(p.ctxt.Arch.PtrSize))
  1342  	p.w.End()
  1343  	if debugGCProg {
  1344  		fmt.Fprintf(os.Stderr, "ld: end GCProg\n")
  1345  	}
  1346  }
  1347  
  1348  func (p *GCProg) AddSym(s loader.Sym) {
  1349  	ldr := p.ctxt.loader
  1350  	typ := ldr.SymGoType(s)
  1351  
  1352  	// Things without pointers should be in sym.SNOPTRDATA or sym.SNOPTRBSS;
  1353  	// everything we see should have pointers and should therefore have a type.
  1354  	if typ == 0 {
  1355  		switch ldr.SymName(s) {
  1356  		case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss", "runtime.gcdata", "runtime.gcbss":
  1357  			// Ignore special symbols that are sometimes laid out
  1358  			// as real symbols. See comment about dyld on darwin in
  1359  			// the address function.
  1360  			return
  1361  		}
  1362  		p.ctxt.Errorf(p.sym.Sym(), "missing Go type information for global symbol %s: size %d", ldr.SymName(s), ldr.SymSize(s))
  1363  		return
  1364  	}
  1365  
  1366  	if debugGCProg {
  1367  		fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d)\n", ldr.SymName(s), ldr.SymValue(s), ldr.SymValue(s)/int64(p.ctxt.Arch.PtrSize))
  1368  	}
  1369  
  1370  	sval := ldr.SymValue(s)
  1371  	p.AddType(sval, typ)
  1372  }
  1373  
  1374  // Add to the gc program the ptr bits for the type typ at
  1375  // byte offset off in the region being described.
  1376  // The type must have a pointer in it.
  1377  func (p *GCProg) AddType(off int64, typ loader.Sym) {
  1378  	ldr := p.ctxt.loader
  1379  	typData := ldr.Data(typ)
  1380  	ptrdata := decodetypePtrdata(p.ctxt.Arch, typData)
  1381  	if ptrdata == 0 {
  1382  		p.ctxt.Errorf(p.sym.Sym(), "has no pointers but in data section")
  1383  		// TODO: just skip these? They might occur in assembly
  1384  		// that doesn't know to use NOPTR? But there must have been
  1385  		// a Go declaration somewhere.
  1386  	}
  1387  	switch decodetypeKind(p.ctxt.Arch, typData) {
  1388  	default:
  1389  		if decodetypeGCMaskOnDemand(p.ctxt.Arch, typData) {
  1390  			p.ctxt.Errorf(p.sym.Sym(), "GC mask not available")
  1391  		}
  1392  		// Copy pointers from mask into program.
  1393  		ptrsize := int64(p.ctxt.Arch.PtrSize)
  1394  		mask := decodetypeGcmask(p.ctxt, typ)
  1395  		for i := int64(0); i < ptrdata/ptrsize; i++ {
  1396  			if (mask[i/8]>>uint(i%8))&1 != 0 {
  1397  				p.w.Ptr(off/ptrsize + i)
  1398  			}
  1399  		}
  1400  	case abi.Array:
  1401  		elem := decodetypeArrayElem(p.ctxt, p.ctxt.Arch, typ)
  1402  		n := decodetypeArrayLen(ldr, p.ctxt.Arch, typ)
  1403  		p.AddType(off, elem)
  1404  		if n > 1 {
  1405  			// Issue repeat for subsequent n-1 instances.
  1406  			elemSize := decodetypeSize(p.ctxt.Arch, ldr.Data(elem))
  1407  			ptrsize := int64(p.ctxt.Arch.PtrSize)
  1408  			p.w.ZeroUntil((off + elemSize) / ptrsize)
  1409  			p.w.Repeat(elemSize/ptrsize, n-1)
  1410  		}
  1411  	case abi.Struct:
  1412  		nField := decodetypeStructFieldCount(ldr, p.ctxt.Arch, typ)
  1413  		for i := 0; i < nField; i++ {
  1414  			fTyp := decodetypeStructFieldType(p.ctxt, p.ctxt.Arch, typ, i)
  1415  			if decodetypePtrdata(p.ctxt.Arch, ldr.Data(fTyp)) == 0 {
  1416  				continue
  1417  			}
  1418  			fOff := decodetypeStructFieldOffset(ldr, p.ctxt.Arch, typ, i)
  1419  			p.AddType(off+fOff, fTyp)
  1420  		}
  1421  	}
  1422  }
  1423  
  1424  // cutoff is the maximum data section size permitted by the linker
  1425  // (see issue #9862).
  1426  const cutoff = 2e9 // 2 GB (or so; looks better in errors than 2^31)
  1427  
  1428  // check accumulated size of data sections
  1429  func (state *dodataState) checkdatsize(symn sym.SymKind) {
  1430  	if state.datsize > cutoff {
  1431  		Errorf("too much data, last section %v (%d, over %v bytes)", symn, state.datsize, cutoff)
  1432  	}
  1433  }
  1434  
  1435  func checkSectSize(sect *sym.Section) {
  1436  	// TODO: consider using 4 GB size limit for DWARF sections, and
  1437  	// make sure we generate unsigned offset in relocations and check
  1438  	// for overflow.
  1439  	if sect.Length > cutoff {
  1440  		Errorf("too much data in section %s (%d, over %v bytes)", sect.Name, sect.Length, cutoff)
  1441  	}
  1442  }
  1443  
  1444  // fixZeroSizedSymbols gives a few special symbols with zero size some space.
  1445  func fixZeroSizedSymbols(ctxt *Link) {
  1446  	// The values in moduledata are filled out by relocations
  1447  	// pointing to the addresses of these special symbols.
  1448  	// Typically these symbols have no size and are not laid
  1449  	// out with their matching section.
  1450  	//
  1451  	// However on darwin, dyld will find the special symbol
  1452  	// in the first loaded module, even though it is local.
  1453  	//
  1454  	// (An hypothesis, formed without looking in the dyld sources:
  1455  	// these special symbols have no size, so their address
  1456  	// matches a real symbol. The dynamic linker assumes we
  1457  	// want the normal symbol with the same address and finds
  1458  	// it in the other module.)
  1459  	//
  1460  	// To work around this we lay out the symbls whose
  1461  	// addresses are vital for multi-module programs to work
  1462  	// as normal symbols, and give them a little size.
  1463  	//
  1464  	// On AIX, as all DATA sections are merged together, ld might not put
  1465  	// these symbols at the beginning of their respective section if there
  1466  	// aren't real symbols, their alignment might not match the
  1467  	// first symbol alignment. Therefore, there are explicitly put at the
  1468  	// beginning of their section with the same alignment.
  1469  	if !(ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) && !(ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) {
  1470  		return
  1471  	}
  1472  
  1473  	ldr := ctxt.loader
  1474  	bss := ldr.CreateSymForUpdate("runtime.bss", 0)
  1475  	bss.SetSize(8)
  1476  	ldr.SetAttrSpecial(bss.Sym(), false)
  1477  
  1478  	ebss := ldr.CreateSymForUpdate("runtime.ebss", 0)
  1479  	ldr.SetAttrSpecial(ebss.Sym(), false)
  1480  
  1481  	data := ldr.CreateSymForUpdate("runtime.data", 0)
  1482  	data.SetSize(8)
  1483  	ldr.SetAttrSpecial(data.Sym(), false)
  1484  
  1485  	edata := ldr.CreateSymForUpdate("runtime.edata", 0)
  1486  	ldr.SetAttrSpecial(edata.Sym(), false)
  1487  
  1488  	if ctxt.HeadType == objabi.Haix {
  1489  		// XCOFFTOC symbols are part of .data section.
  1490  		edata.SetType(sym.SXCOFFTOC)
  1491  	}
  1492  
  1493  	noptrbss := ldr.CreateSymForUpdate("runtime.noptrbss", 0)
  1494  	noptrbss.SetSize(8)
  1495  	ldr.SetAttrSpecial(noptrbss.Sym(), false)
  1496  
  1497  	enoptrbss := ldr.CreateSymForUpdate("runtime.enoptrbss", 0)
  1498  	ldr.SetAttrSpecial(enoptrbss.Sym(), false)
  1499  
  1500  	noptrdata := ldr.CreateSymForUpdate("runtime.noptrdata", 0)
  1501  	noptrdata.SetSize(8)
  1502  	ldr.SetAttrSpecial(noptrdata.Sym(), false)
  1503  
  1504  	enoptrdata := ldr.CreateSymForUpdate("runtime.enoptrdata", 0)
  1505  	ldr.SetAttrSpecial(enoptrdata.Sym(), false)
  1506  
  1507  	types := ldr.CreateSymForUpdate("runtime.types", 0)
  1508  	types.SetType(sym.STYPE)
  1509  	types.SetSize(8)
  1510  	ldr.SetAttrSpecial(types.Sym(), false)
  1511  
  1512  	etypes := ldr.CreateSymForUpdate("runtime.etypes", 0)
  1513  	etypes.SetType(sym.SFUNCTAB)
  1514  	ldr.SetAttrSpecial(etypes.Sym(), false)
  1515  
  1516  	if ctxt.HeadType == objabi.Haix {
  1517  		rodata := ldr.CreateSymForUpdate("runtime.rodata", 0)
  1518  		rodata.SetType(sym.SSTRING)
  1519  		rodata.SetSize(8)
  1520  		ldr.SetAttrSpecial(rodata.Sym(), false)
  1521  
  1522  		erodata := ldr.CreateSymForUpdate("runtime.erodata", 0)
  1523  		ldr.SetAttrSpecial(erodata.Sym(), false)
  1524  	}
  1525  }
  1526  
  1527  // makeRelroForSharedLib creates a section of readonly data if necessary.
  1528  func (state *dodataState) makeRelroForSharedLib(target *Link) {
  1529  	if !target.UseRelro() {
  1530  		return
  1531  	}
  1532  
  1533  	// "read only" data with relocations needs to go in its own section
  1534  	// when building a shared library. We do this by boosting objects of
  1535  	// type SXXX with relocations to type SXXXRELRO.
  1536  	ldr := target.loader
  1537  	for _, symnro := range sym.ReadOnly {
  1538  		symnrelro := sym.RelROMap[symnro]
  1539  
  1540  		ro := []loader.Sym{}
  1541  		relro := state.data[symnrelro]
  1542  
  1543  		for _, s := range state.data[symnro] {
  1544  			relocs := ldr.Relocs(s)
  1545  			isRelro := relocs.Count() > 0
  1546  			switch state.symType(s) {
  1547  			case sym.STYPE, sym.STYPERELRO, sym.SGOFUNCRELRO:
  1548  				// Symbols are not sorted yet, so it is possible
  1549  				// that an Outer symbol has been changed to a
  1550  				// relro Type before it reaches here.
  1551  				isRelro = true
  1552  			case sym.SFUNCTAB:
  1553  				if ldr.SymName(s) == "runtime.etypes" {
  1554  					// runtime.etypes must be at the end of
  1555  					// the relro data.
  1556  					isRelro = true
  1557  				}
  1558  			case sym.SGOFUNC:
  1559  				// The only SGOFUNC symbols that contain relocations are .stkobj,
  1560  				// and their relocations are of type objabi.R_ADDROFF,
  1561  				// which always get resolved during linking.
  1562  				isRelro = false
  1563  			}
  1564  			if isRelro {
  1565  				if symnrelro == sym.Sxxx {
  1566  					state.ctxt.Errorf(s, "cannot contain relocations (type %v)", symnro)
  1567  				}
  1568  				state.setSymType(s, symnrelro)
  1569  				if outer := ldr.OuterSym(s); outer != 0 {
  1570  					state.setSymType(outer, symnrelro)
  1571  				}
  1572  				relro = append(relro, s)
  1573  			} else {
  1574  				ro = append(ro, s)
  1575  			}
  1576  		}
  1577  
  1578  		// Check that we haven't made two symbols with the same .Outer into
  1579  		// different types (because references two symbols with non-nil Outer
  1580  		// become references to the outer symbol + offset it's vital that the
  1581  		// symbol and the outer end up in the same section).
  1582  		for _, s := range relro {
  1583  			if outer := ldr.OuterSym(s); outer != 0 {
  1584  				st := state.symType(s)
  1585  				ost := state.symType(outer)
  1586  				if st != ost {
  1587  					state.ctxt.Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)",
  1588  						ldr.SymName(outer), st, ost)
  1589  				}
  1590  			}
  1591  		}
  1592  
  1593  		state.data[symnro] = ro
  1594  		state.data[symnrelro] = relro
  1595  	}
  1596  }
  1597  
  1598  // dodataState holds bits of state information needed by dodata() and the
  1599  // various helpers it calls. The lifetime of these items should not extend
  1600  // past the end of dodata().
  1601  type dodataState struct {
  1602  	// Link context
  1603  	ctxt *Link
  1604  	// Data symbols bucketed by type.
  1605  	data [sym.SXREF][]loader.Sym
  1606  	// Max alignment for each flavor of data symbol.
  1607  	dataMaxAlign [sym.SXREF]int32
  1608  	// Overridden sym type
  1609  	symGroupType []sym.SymKind
  1610  	// Current data size so far.
  1611  	datsize int64
  1612  }
  1613  
  1614  // A note on symType/setSymType below:
  1615  //
  1616  // In the legacy linker, the types of symbols (notably data symbols) are
  1617  // changed during the symtab() phase so as to insure that similar symbols
  1618  // are bucketed together, then their types are changed back again during
  1619  // dodata. Symbol to section assignment also plays tricks along these lines
  1620  // in the case where a relro segment is needed.
  1621  //
  1622  // The value returned from setType() below reflects the effects of
  1623  // any overrides made by symtab and/or dodata.
  1624  
  1625  // symType returns the (possibly overridden) type of 's'.
  1626  func (state *dodataState) symType(s loader.Sym) sym.SymKind {
  1627  	if int(s) < len(state.symGroupType) {
  1628  		if override := state.symGroupType[s]; override != 0 {
  1629  			return override
  1630  		}
  1631  	}
  1632  	return state.ctxt.loader.SymType(s)
  1633  }
  1634  
  1635  // setSymType sets a new override type for 's'.
  1636  func (state *dodataState) setSymType(s loader.Sym, kind sym.SymKind) {
  1637  	if s == 0 {
  1638  		panic("bad")
  1639  	}
  1640  	if int(s) < len(state.symGroupType) {
  1641  		state.symGroupType[s] = kind
  1642  	} else {
  1643  		su := state.ctxt.loader.MakeSymbolUpdater(s)
  1644  		su.SetType(kind)
  1645  	}
  1646  }
  1647  
  1648  func (ctxt *Link) dodata(symGroupType []sym.SymKind) {
  1649  
  1650  	// Give zeros sized symbols space if necessary.
  1651  	fixZeroSizedSymbols(ctxt)
  1652  
  1653  	// Collect data symbols by type into data.
  1654  	state := dodataState{ctxt: ctxt, symGroupType: symGroupType}
  1655  	ldr := ctxt.loader
  1656  	for s := loader.Sym(1); s < loader.Sym(ldr.NSym()); s++ {
  1657  		if !ldr.AttrReachable(s) || ldr.AttrSpecial(s) || ldr.AttrSubSymbol(s) ||
  1658  			!ldr.TopLevelSym(s) {
  1659  			continue
  1660  		}
  1661  
  1662  		st := state.symType(s)
  1663  
  1664  		if st <= sym.STEXTFIPSEND || st >= sym.SXREF {
  1665  			continue
  1666  		}
  1667  		state.data[st] = append(state.data[st], s)
  1668  
  1669  		// Similarly with checking the onlist attr.
  1670  		if ldr.AttrOnList(s) {
  1671  			log.Fatalf("symbol %s listed multiple times", ldr.SymName(s))
  1672  		}
  1673  		ldr.SetAttrOnList(s, true)
  1674  	}
  1675  
  1676  	// Now that we have the data symbols, but before we start
  1677  	// to assign addresses, record all the necessary
  1678  	// dynamic relocations. These will grow the relocation
  1679  	// symbol, which is itself data.
  1680  	//
  1681  	// On darwin, we need the symbol table numbers for dynreloc.
  1682  	if ctxt.HeadType == objabi.Hdarwin {
  1683  		machosymorder(ctxt)
  1684  	}
  1685  	state.dynreloc(ctxt)
  1686  
  1687  	// Move any RO data with relocations to a separate section.
  1688  	state.makeRelroForSharedLib(ctxt)
  1689  
  1690  	// Set alignment for the symbol with the largest known index,
  1691  	// so as to trigger allocation of the loader's internal
  1692  	// alignment array. This will avoid data races in the parallel
  1693  	// section below.
  1694  	lastSym := loader.Sym(ldr.NSym() - 1)
  1695  	ldr.SetSymAlign(lastSym, ldr.SymAlign(lastSym))
  1696  
  1697  	// Sort symbols.
  1698  	var wg sync.WaitGroup
  1699  	for symn := range state.data {
  1700  		symn := sym.SymKind(symn)
  1701  		wg.Add(1)
  1702  		go func() {
  1703  			state.data[symn], state.dataMaxAlign[symn] = state.dodataSect(ctxt, symn, state.data[symn])
  1704  			wg.Done()
  1705  		}()
  1706  	}
  1707  	wg.Wait()
  1708  
  1709  	if ctxt.IsELF {
  1710  		// Make .rela and .rela.plt contiguous, the ELF ABI requires this
  1711  		// and Solaris actually cares.
  1712  		syms := state.data[sym.SELFROSECT]
  1713  		reli, plti := -1, -1
  1714  		for i, s := range syms {
  1715  			switch ldr.SymName(s) {
  1716  			case ".rel.plt", ".rela.plt":
  1717  				plti = i
  1718  			case ".rel", ".rela":
  1719  				reli = i
  1720  			}
  1721  		}
  1722  		if reli >= 0 && plti >= 0 && plti != reli+1 {
  1723  			var first, second int
  1724  			if plti > reli {
  1725  				first, second = reli, plti
  1726  			} else {
  1727  				first, second = plti, reli
  1728  			}
  1729  			rel, plt := syms[reli], syms[plti]
  1730  			copy(syms[first+2:], syms[first+1:second])
  1731  			syms[first+0] = rel
  1732  			syms[first+1] = plt
  1733  
  1734  			// Make sure alignment doesn't introduce a gap.
  1735  			// Setting the alignment explicitly prevents
  1736  			// symalign from basing it on the size and
  1737  			// getting it wrong.
  1738  			ldr.SetSymAlign(rel, int32(ctxt.Arch.RegSize))
  1739  			ldr.SetSymAlign(plt, int32(ctxt.Arch.RegSize))
  1740  		}
  1741  		state.data[sym.SELFROSECT] = syms
  1742  	}
  1743  
  1744  	if ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal {
  1745  		// These symbols must have the same alignment as their section.
  1746  		// Otherwise, ld might change the layout of Go sections.
  1747  		ldr.SetSymAlign(ldr.Lookup("runtime.data", 0), state.dataMaxAlign[sym.SDATA])
  1748  		ldr.SetSymAlign(ldr.Lookup("runtime.bss", 0), state.dataMaxAlign[sym.SBSS])
  1749  	}
  1750  
  1751  	// Create *sym.Section objects and assign symbols to sections for
  1752  	// data/rodata (and related) symbols.
  1753  	state.allocateDataSections(ctxt)
  1754  
  1755  	state.allocateSEHSections(ctxt)
  1756  
  1757  	// Create *sym.Section objects and assign symbols to sections for
  1758  	// DWARF symbols.
  1759  	state.allocateDwarfSections(ctxt)
  1760  
  1761  	/* number the sections */
  1762  	n := int16(1)
  1763  
  1764  	for _, sect := range Segtext.Sections {
  1765  		sect.Extnum = n
  1766  		n++
  1767  	}
  1768  	for _, sect := range Segrodata.Sections {
  1769  		sect.Extnum = n
  1770  		n++
  1771  	}
  1772  	for _, sect := range Segrelrodata.Sections {
  1773  		sect.Extnum = n
  1774  		n++
  1775  	}
  1776  	for _, sect := range Segdata.Sections {
  1777  		sect.Extnum = n
  1778  		n++
  1779  	}
  1780  	for _, sect := range Segdwarf.Sections {
  1781  		sect.Extnum = n
  1782  		n++
  1783  	}
  1784  	for _, sect := range Segpdata.Sections {
  1785  		sect.Extnum = n
  1786  		n++
  1787  	}
  1788  	for _, sect := range Segxdata.Sections {
  1789  		sect.Extnum = n
  1790  		n++
  1791  	}
  1792  }
  1793  
  1794  // allocateDataSectionForSym creates a new sym.Section into which a
  1795  // single symbol will be placed. Here "seg" is the segment into which
  1796  // the section will go, "s" is the symbol to be placed into the new
  1797  // section, and "rwx" contains permissions for the section.
  1798  func (state *dodataState) allocateDataSectionForSym(seg *sym.Segment, s loader.Sym, rwx int) *sym.Section {
  1799  	ldr := state.ctxt.loader
  1800  	sname := ldr.SymName(s)
  1801  	if strings.HasPrefix(sname, "go:") {
  1802  		sname = ".go." + sname[len("go:"):]
  1803  	}
  1804  	sect := addsection(ldr, state.ctxt.Arch, seg, sname, rwx)
  1805  	sect.Align = symalign(ldr, s)
  1806  	state.datsize = Rnd(state.datsize, int64(sect.Align))
  1807  	sect.Vaddr = uint64(state.datsize)
  1808  	return sect
  1809  }
  1810  
  1811  // allocateNamedDataSection creates a new sym.Section for a category
  1812  // of data symbols. Here "seg" is the segment into which the section
  1813  // will go, "sName" is the name to give to the section, "types" is a
  1814  // range of symbol types to be put into the section, and "rwx"
  1815  // contains permissions for the section.
  1816  func (state *dodataState) allocateNamedDataSection(seg *sym.Segment, sName string, types []sym.SymKind, rwx int) *sym.Section {
  1817  	sect := addsection(state.ctxt.loader, state.ctxt.Arch, seg, sName, rwx)
  1818  	if len(types) == 0 {
  1819  		sect.Align = 1
  1820  	} else if len(types) == 1 {
  1821  		sect.Align = state.dataMaxAlign[types[0]]
  1822  	} else {
  1823  		for _, symn := range types {
  1824  			align := state.dataMaxAlign[symn]
  1825  			if sect.Align < align {
  1826  				sect.Align = align
  1827  			}
  1828  		}
  1829  	}
  1830  	state.datsize = Rnd(state.datsize, int64(sect.Align))
  1831  	sect.Vaddr = uint64(state.datsize)
  1832  	return sect
  1833  }
  1834  
  1835  // assignDsymsToSection assigns a collection of data symbols to a
  1836  // newly created section. "sect" is the section into which to place
  1837  // the symbols, "syms" holds the list of symbols to assign,
  1838  // "forceType" (if non-zero) contains a new sym type to apply to each
  1839  // sym during the assignment, and "aligner" is a hook to call to
  1840  // handle alignment during the assignment process.
  1841  func (state *dodataState) assignDsymsToSection(sect *sym.Section, syms []loader.Sym, forceType sym.SymKind, aligner func(state *dodataState, datsize int64, s loader.Sym) int64) {
  1842  	ldr := state.ctxt.loader
  1843  	for _, s := range syms {
  1844  		state.datsize = aligner(state, state.datsize, s)
  1845  		ldr.SetSymSect(s, sect)
  1846  		if forceType != sym.Sxxx {
  1847  			state.setSymType(s, forceType)
  1848  		}
  1849  		ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr))
  1850  		state.datsize += ldr.SymSize(s)
  1851  	}
  1852  	sect.Length = uint64(state.datsize) - sect.Vaddr
  1853  }
  1854  
  1855  func (state *dodataState) assignToSection(sect *sym.Section, symn sym.SymKind, forceType sym.SymKind) {
  1856  	state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize)
  1857  	state.checkdatsize(symn)
  1858  }
  1859  
  1860  // allocateSingleSymSections walks through the bucketed data symbols
  1861  // with type 'symn', creates a new section for each sym, and assigns
  1862  // the sym to a newly created section. Section name is set from the
  1863  // symbol name. "Seg" is the segment into which to place the new
  1864  // section, "forceType" is the new sym.SymKind to assign to the symbol
  1865  // within the section, and "rwx" holds section permissions.
  1866  func (state *dodataState) allocateSingleSymSections(seg *sym.Segment, symn sym.SymKind, forceType sym.SymKind, rwx int) {
  1867  	ldr := state.ctxt.loader
  1868  	for _, s := range state.data[symn] {
  1869  		sect := state.allocateDataSectionForSym(seg, s, rwx)
  1870  		ldr.SetSymSect(s, sect)
  1871  		state.setSymType(s, forceType)
  1872  		ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr))
  1873  		state.datsize += ldr.SymSize(s)
  1874  		sect.Length = uint64(state.datsize) - sect.Vaddr
  1875  	}
  1876  	state.checkdatsize(symn)
  1877  }
  1878  
  1879  // allocateNamedSectionAndAssignSyms creates a new section with the
  1880  // specified name, then walks through the bucketed data symbols with
  1881  // type 'symn' and assigns each of them to this new section. "Seg" is
  1882  // the segment into which to place the new section, "secName" is the
  1883  // name to give to the new section, "forceType" (if non-zero) contains
  1884  // a new sym type to apply to each sym during the assignment, and
  1885  // "rwx" holds section permissions.
  1886  func (state *dodataState) allocateNamedSectionAndAssignSyms(seg *sym.Segment, secName string, symn sym.SymKind, forceType sym.SymKind, rwx int) *sym.Section {
  1887  
  1888  	sect := state.allocateNamedDataSection(seg, secName, []sym.SymKind{symn}, rwx)
  1889  	state.assignDsymsToSection(sect, state.data[symn], forceType, aligndatsize)
  1890  	return sect
  1891  }
  1892  
  1893  // allocateDataSections allocates sym.Section objects for data/rodata
  1894  // (and related) symbols, and then assigns symbols to those sections.
  1895  func (state *dodataState) allocateDataSections(ctxt *Link) {
  1896  	// Allocate sections.
  1897  	// Data is processed before segtext, because we need
  1898  	// to see all symbols in the .data and .bss sections in order
  1899  	// to generate garbage collection information.
  1900  
  1901  	// Writable data sections that do not need any specialized handling.
  1902  	writable := []sym.SymKind{
  1903  		sym.SBUILDINFO,
  1904  		sym.SFIPSINFO,
  1905  		sym.SELFSECT,
  1906  		sym.SMACHO,
  1907  		sym.SWINDOWS,
  1908  	}
  1909  	for _, symn := range writable {
  1910  		state.allocateSingleSymSections(&Segdata, symn, sym.SDATA, 06)
  1911  	}
  1912  	ldr := ctxt.loader
  1913  
  1914  	// writable .got (note that for PIE binaries .got goes in relro)
  1915  	if len(state.data[sym.SELFGOT]) > 0 {
  1916  		state.allocateNamedSectionAndAssignSyms(&Segdata, ".got", sym.SELFGOT, sym.SDATA, 06)
  1917  	}
  1918  	if len(state.data[sym.SMACHOGOT]) > 0 {
  1919  		state.allocateNamedSectionAndAssignSyms(&Segdata, ".got", sym.SMACHOGOT, sym.SDATA, 06)
  1920  	}
  1921  
  1922  	/* pointer-free data */
  1923  	sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrdata", sym.SNOPTRDATA, sym.SDATA, 06)
  1924  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrdata", 0), sect)
  1925  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrdata", 0), sect)
  1926  
  1927  	state.assignToSection(sect, sym.SNOPTRDATAFIPSSTART, sym.SDATA)
  1928  	state.assignToSection(sect, sym.SNOPTRDATAFIPS, sym.SDATA)
  1929  	state.assignToSection(sect, sym.SNOPTRDATAFIPSEND, sym.SDATA)
  1930  	state.assignToSection(sect, sym.SNOPTRDATAEND, sym.SDATA)
  1931  
  1932  	hasinitarr := ctxt.linkShared
  1933  
  1934  	/* shared library initializer */
  1935  	switch ctxt.BuildMode {
  1936  	case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin:
  1937  		hasinitarr = true
  1938  	}
  1939  
  1940  	if ctxt.HeadType == objabi.Haix {
  1941  		if len(state.data[sym.SINITARR]) > 0 {
  1942  			Errorf("XCOFF format doesn't allow .init_array section")
  1943  		}
  1944  	}
  1945  
  1946  	if hasinitarr && len(state.data[sym.SINITARR]) > 0 {
  1947  		state.allocateNamedSectionAndAssignSyms(&Segdata, ".init_array", sym.SINITARR, sym.Sxxx, 06)
  1948  	}
  1949  
  1950  	/* data */
  1951  	sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".data", sym.SDATA, sym.SDATA, 06)
  1952  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.data", 0), sect)
  1953  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.edata", 0), sect)
  1954  
  1955  	state.assignToSection(sect, sym.SDATAFIPSSTART, sym.SDATA)
  1956  	state.assignToSection(sect, sym.SDATAFIPS, sym.SDATA)
  1957  	state.assignToSection(sect, sym.SDATAFIPSEND, sym.SDATA)
  1958  	state.assignToSection(sect, sym.SDATAEND, sym.SDATA)
  1959  
  1960  	dataGcEnd := state.datsize - int64(sect.Vaddr)
  1961  
  1962  	// On AIX, TOC entries must be the last of .data
  1963  	// These aren't part of gc as they won't change during the runtime.
  1964  	state.assignToSection(sect, sym.SXCOFFTOC, sym.SDATA)
  1965  	state.checkdatsize(sym.SDATA)
  1966  	sect.Length = uint64(state.datsize) - sect.Vaddr
  1967  
  1968  	/* bss */
  1969  	sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".bss", sym.SBSS, sym.Sxxx, 06)
  1970  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.bss", 0), sect)
  1971  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ebss", 0), sect)
  1972  	bssGcEnd := state.datsize - int64(sect.Vaddr)
  1973  
  1974  	// Emit gcdata for bss symbols now that symbol values have been assigned.
  1975  	gcsToEmit := []struct {
  1976  		symName string
  1977  		symKind sym.SymKind
  1978  		gcEnd   int64
  1979  	}{
  1980  		{"runtime.gcdata", sym.SDATA, dataGcEnd},
  1981  		{"runtime.gcbss", sym.SBSS, bssGcEnd},
  1982  	}
  1983  	for _, g := range gcsToEmit {
  1984  		var gc GCProg
  1985  		gc.Init(ctxt, g.symName)
  1986  		for _, s := range state.data[g.symKind] {
  1987  			gc.AddSym(s)
  1988  		}
  1989  		gc.End(g.gcEnd)
  1990  	}
  1991  
  1992  	/* pointer-free bss */
  1993  	sect = state.allocateNamedSectionAndAssignSyms(&Segdata, ".noptrbss", sym.SNOPTRBSS, sym.Sxxx, 06)
  1994  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.noptrbss", 0), sect)
  1995  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.enoptrbss", 0), sect)
  1996  
  1997  	// Code coverage counters are assigned to the .noptrbss section.
  1998  	// We assign them in a separate pass so that they stay aggregated
  1999  	// together in a single blob (coverage runtime depends on this).
  2000  	covCounterDataStartOff = sect.Length
  2001  	state.assignToSection(sect, sym.SCOVERAGE_COUNTER, sym.SNOPTRBSS)
  2002  	covCounterDataLen = sect.Length - covCounterDataStartOff
  2003  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.covctrs", 0), sect)
  2004  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ecovctrs", 0), sect)
  2005  
  2006  	// Coverage instrumentation counters for libfuzzer.
  2007  	if len(state.data[sym.SLIBFUZZER_8BIT_COUNTER]) > 0 {
  2008  		sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".go.fuzzcntrs", sym.SLIBFUZZER_8BIT_COUNTER, sym.Sxxx, 06)
  2009  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__start___sancov_cntrs", 0), sect)
  2010  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.__stop___sancov_cntrs", 0), sect)
  2011  		ldr.SetSymSect(ldr.LookupOrCreateSym("internal/fuzz._counters", 0), sect)
  2012  		ldr.SetSymSect(ldr.LookupOrCreateSym("internal/fuzz._ecounters", 0), sect)
  2013  	}
  2014  
  2015  	// Assign runtime.end to the last section of data segment.
  2016  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.end", 0), Segdata.Sections[len(Segdata.Sections)-1])
  2017  
  2018  	if len(state.data[sym.STLSBSS]) > 0 {
  2019  		var sect *sym.Section
  2020  		// FIXME: not clear why it is sometimes necessary to suppress .tbss section creation.
  2021  		if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && (ctxt.LinkMode == LinkExternal || !*FlagD) {
  2022  			sect = addsection(ldr, ctxt.Arch, &Segdata, ".tbss", 06)
  2023  			sect.Align = int32(ctxt.Arch.PtrSize)
  2024  			// FIXME: why does this need to be set to zero?
  2025  			sect.Vaddr = 0
  2026  		}
  2027  		state.datsize = 0
  2028  
  2029  		for _, s := range state.data[sym.STLSBSS] {
  2030  			state.datsize = aligndatsize(state, state.datsize, s)
  2031  			if sect != nil {
  2032  				ldr.SetSymSect(s, sect)
  2033  			}
  2034  			ldr.SetSymValue(s, state.datsize)
  2035  			state.datsize += ldr.SymSize(s)
  2036  		}
  2037  		state.checkdatsize(sym.STLSBSS)
  2038  
  2039  		if sect != nil {
  2040  			sect.Length = uint64(state.datsize)
  2041  		}
  2042  	}
  2043  
  2044  	/*
  2045  	 * We finished data, begin read-only data.
  2046  	 * Not all systems support a separate read-only non-executable data section.
  2047  	 * ELF and Windows PE systems do.
  2048  	 * OS X and Plan 9 do not.
  2049  	 * And if we're using external linking mode, the point is moot,
  2050  	 * since it's not our decision; that code expects the sections in
  2051  	 * segtext.
  2052  	 */
  2053  	var segro *sym.Segment
  2054  	if ctxt.IsELF && ctxt.LinkMode == LinkInternal {
  2055  		segro = &Segrodata
  2056  	} else if ctxt.HeadType == objabi.Hwindows {
  2057  		segro = &Segrodata
  2058  	} else {
  2059  		segro = &Segtext
  2060  	}
  2061  
  2062  	state.datsize = 0
  2063  
  2064  	/* read-only executable ELF, Mach-O sections */
  2065  	if len(state.data[sym.STEXT]) != 0 {
  2066  		culprit := ldr.SymName(state.data[sym.STEXT][0])
  2067  		Errorf("dodata found an sym.STEXT symbol: %s", culprit)
  2068  	}
  2069  	state.allocateSingleSymSections(&Segtext, sym.SELFRXSECT, sym.SRODATA, 05)
  2070  	state.allocateSingleSymSections(&Segtext, sym.SMACHOPLT, sym.SRODATA, 05)
  2071  
  2072  	/* read-only data */
  2073  	sect = state.allocateNamedDataSection(segro, ".rodata", sym.ReadOnly, 04)
  2074  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.rodata", 0), sect)
  2075  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.erodata", 0), sect)
  2076  	if !ctxt.UseRelro() {
  2077  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect)
  2078  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect)
  2079  	}
  2080  	for _, symn := range sym.ReadOnly {
  2081  		symnStartValue := state.datsize
  2082  		if len(state.data[symn]) != 0 {
  2083  			symnStartValue = aligndatsize(state, symnStartValue, state.data[symn][0])
  2084  		}
  2085  		state.assignToSection(sect, symn, sym.SRODATA)
  2086  		setCarrierSize(symn, state.datsize-symnStartValue)
  2087  		if ctxt.HeadType == objabi.Haix {
  2088  			// Read-only symbols might be wrapped inside their outer
  2089  			// symbol.
  2090  			// XCOFF symbol table needs to know the size of
  2091  			// these outer symbols.
  2092  			xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn)
  2093  		}
  2094  	}
  2095  
  2096  	/* read-only ELF, Mach-O sections */
  2097  	state.allocateSingleSymSections(segro, sym.SELFROSECT, sym.SRODATA, 04)
  2098  
  2099  	// There is some data that are conceptually read-only but are written to by
  2100  	// relocations. On GNU systems, we can arrange for the dynamic linker to
  2101  	// mprotect sections after relocations are applied by giving them write
  2102  	// permissions in the object file and calling them ".data.rel.ro.FOO". We
  2103  	// divide the .rodata section between actual .rodata and .data.rel.ro.rodata,
  2104  	// but for the other sections that this applies to, we just write a read-only
  2105  	// .FOO section or a read-write .data.rel.ro.FOO section depending on the
  2106  	// situation.
  2107  	// TODO(mwhudson): It would make sense to do this more widely, but it makes
  2108  	// the system linker segfault on darwin.
  2109  	const relroPerm = 06
  2110  	const fallbackPerm = 04
  2111  	relroSecPerm := fallbackPerm
  2112  	genrelrosecname := func(suffix string) string {
  2113  		if suffix == "" {
  2114  			return ".rodata"
  2115  		}
  2116  		return suffix
  2117  	}
  2118  	seg := segro
  2119  
  2120  	if ctxt.UseRelro() {
  2121  		segrelro := &Segrelrodata
  2122  		if ctxt.LinkMode == LinkExternal && !ctxt.IsAIX() && !ctxt.IsDarwin() {
  2123  			// Using a separate segment with an external
  2124  			// linker results in some programs moving
  2125  			// their data sections unexpectedly, which
  2126  			// corrupts the moduledata. So we use the
  2127  			// rodata segment and let the external linker
  2128  			// sort out a rel.ro segment.
  2129  			segrelro = segro
  2130  		} else {
  2131  			// Reset datsize for new segment.
  2132  			state.datsize = 0
  2133  		}
  2134  
  2135  		if !ctxt.IsDarwin() { // We don't need the special names on darwin.
  2136  			genrelrosecname = func(suffix string) string {
  2137  				return ".data.rel.ro" + suffix
  2138  			}
  2139  		}
  2140  
  2141  		relroReadOnly := []sym.SymKind{}
  2142  		for _, symnro := range sym.ReadOnly {
  2143  			symn := sym.RelROMap[symnro]
  2144  			relroReadOnly = append(relroReadOnly, symn)
  2145  		}
  2146  		seg = segrelro
  2147  		relroSecPerm = relroPerm
  2148  
  2149  		/* data only written by relocations */
  2150  		sect = state.allocateNamedDataSection(segrelro, genrelrosecname(""), relroReadOnly, relroSecPerm)
  2151  
  2152  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.types", 0), sect)
  2153  		ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.etypes", 0), sect)
  2154  
  2155  		for i, symnro := range sym.ReadOnly {
  2156  			if i == 0 && symnro == sym.STYPE && ctxt.HeadType != objabi.Haix {
  2157  				// Skip forward so that no type
  2158  				// reference uses a zero offset.
  2159  				// This is unlikely but possible in small
  2160  				// programs with no other read-only data.
  2161  				state.datsize++
  2162  			}
  2163  
  2164  			symn := sym.RelROMap[symnro]
  2165  			if symn == sym.Sxxx {
  2166  				continue
  2167  			}
  2168  			symnStartValue := state.datsize
  2169  			if len(state.data[symn]) != 0 {
  2170  				symnStartValue = aligndatsize(state, symnStartValue, state.data[symn][0])
  2171  			}
  2172  
  2173  			for _, s := range state.data[symn] {
  2174  				outer := ldr.OuterSym(s)
  2175  				if s != 0 && ldr.SymSect(outer) != nil && ldr.SymSect(outer) != sect {
  2176  					ctxt.Errorf(s, "s.Outer (%s) in different section from s, %s != %s", ldr.SymName(outer), ldr.SymSect(outer).Name, sect.Name)
  2177  				}
  2178  			}
  2179  			state.assignToSection(sect, symn, sym.SRODATA)
  2180  			setCarrierSize(symn, state.datsize-symnStartValue)
  2181  			if ctxt.HeadType == objabi.Haix {
  2182  				// Read-only symbols might be wrapped inside their outer
  2183  				// symbol.
  2184  				// XCOFF symbol table needs to know the size of
  2185  				// these outer symbols.
  2186  				xcoffUpdateOuterSize(ctxt, state.datsize-symnStartValue, symn)
  2187  			}
  2188  		}
  2189  		sect.Length = uint64(state.datsize) - sect.Vaddr
  2190  
  2191  		state.allocateSingleSymSections(segrelro, sym.SELFRELROSECT, sym.SRODATA, relroSecPerm)
  2192  		state.allocateSingleSymSections(segrelro, sym.SMACHORELROSECT, sym.SRODATA, relroSecPerm)
  2193  	}
  2194  
  2195  	/* typelink */
  2196  	sect = state.allocateNamedDataSection(seg, genrelrosecname(".typelink"), []sym.SymKind{sym.STYPELINK}, relroSecPerm)
  2197  
  2198  	typelink := ldr.CreateSymForUpdate("runtime.typelink", 0)
  2199  	ldr.SetSymSect(typelink.Sym(), sect)
  2200  	typelink.SetType(sym.SRODATA)
  2201  	state.datsize += typelink.Size()
  2202  	state.checkdatsize(sym.STYPELINK)
  2203  	sect.Length = uint64(state.datsize) - sect.Vaddr
  2204  
  2205  	/* itablink */
  2206  	sect = state.allocateNamedDataSection(seg, genrelrosecname(".itablink"), []sym.SymKind{sym.SITABLINK}, relroSecPerm)
  2207  
  2208  	itablink := ldr.CreateSymForUpdate("runtime.itablink", 0)
  2209  	ldr.SetSymSect(itablink.Sym(), sect)
  2210  	itablink.SetType(sym.SRODATA)
  2211  	state.datsize += itablink.Size()
  2212  	state.checkdatsize(sym.SITABLINK)
  2213  	sect.Length = uint64(state.datsize) - sect.Vaddr
  2214  
  2215  	/* gosymtab */
  2216  	sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gosymtab"), sym.SSYMTAB, sym.SRODATA, relroSecPerm)
  2217  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.symtab", 0), sect)
  2218  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.esymtab", 0), sect)
  2219  
  2220  	/* gopclntab */
  2221  	sect = state.allocateNamedSectionAndAssignSyms(seg, genrelrosecname(".gopclntab"), sym.SPCLNTAB, sym.SRODATA, relroSecPerm)
  2222  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0), sect)
  2223  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pcheader", 0), sect)
  2224  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.funcnametab", 0), sect)
  2225  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.cutab", 0), sect)
  2226  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.filetab", 0), sect)
  2227  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.pctab", 0), sect)
  2228  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.functab", 0), sect)
  2229  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.epclntab", 0), sect)
  2230  	setCarrierSize(sym.SPCLNTAB, int64(sect.Length))
  2231  	if ctxt.HeadType == objabi.Haix {
  2232  		xcoffUpdateOuterSize(ctxt, int64(sect.Length), sym.SPCLNTAB)
  2233  	}
  2234  
  2235  	// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
  2236  	if state.datsize != int64(uint32(state.datsize)) {
  2237  		Errorf("read-only data segment too large: %d", state.datsize)
  2238  	}
  2239  
  2240  	siz := 0
  2241  	for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ {
  2242  		siz += len(state.data[symn])
  2243  	}
  2244  	ctxt.datap = make([]loader.Sym, 0, siz)
  2245  	for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ {
  2246  		ctxt.datap = append(ctxt.datap, state.data[symn]...)
  2247  	}
  2248  }
  2249  
  2250  // allocateDwarfSections allocates sym.Section objects for DWARF
  2251  // symbols, and assigns symbols to sections.
  2252  func (state *dodataState) allocateDwarfSections(ctxt *Link) {
  2253  
  2254  	alignOne := func(state *dodataState, datsize int64, s loader.Sym) int64 { return datsize }
  2255  
  2256  	ldr := ctxt.loader
  2257  	for i := 0; i < len(dwarfp); i++ {
  2258  		// First the section symbol.
  2259  		s := dwarfp[i].secSym()
  2260  		sect := state.allocateNamedDataSection(&Segdwarf, ldr.SymName(s), []sym.SymKind{}, 04)
  2261  		ldr.SetSymSect(s, sect)
  2262  		sect.Sym = sym.LoaderSym(s)
  2263  		curType := ldr.SymType(s)
  2264  		state.setSymType(s, sym.SRODATA)
  2265  		ldr.SetSymValue(s, int64(uint64(state.datsize)-sect.Vaddr))
  2266  		state.datsize += ldr.SymSize(s)
  2267  
  2268  		// Then any sub-symbols for the section symbol.
  2269  		subSyms := dwarfp[i].subSyms()
  2270  		state.assignDsymsToSection(sect, subSyms, sym.SRODATA, alignOne)
  2271  
  2272  		for j := 0; j < len(subSyms); j++ {
  2273  			s := subSyms[j]
  2274  			if ctxt.HeadType == objabi.Haix && curType == sym.SDWARFLOC {
  2275  				// Update the size of .debug_loc for this symbol's
  2276  				// package.
  2277  				addDwsectCUSize(".debug_loc", ldr.SymPkg(s), uint64(ldr.SymSize(s)))
  2278  			}
  2279  		}
  2280  		sect.Length = uint64(state.datsize) - sect.Vaddr
  2281  		checkSectSize(sect)
  2282  	}
  2283  }
  2284  
  2285  // allocateSEHSections allocate a sym.Section object for SEH
  2286  // symbols, and assigns symbols to sections.
  2287  func (state *dodataState) allocateSEHSections(ctxt *Link) {
  2288  	if len(sehp.pdata) > 0 {
  2289  		sect := state.allocateNamedDataSection(&Segpdata, ".pdata", []sym.SymKind{}, 04)
  2290  		state.assignDsymsToSection(sect, sehp.pdata, sym.SRODATA, aligndatsize)
  2291  		state.checkdatsize(sym.SSEHSECT)
  2292  	}
  2293  	if len(sehp.xdata) > 0 {
  2294  		sect := state.allocateNamedDataSection(&Segxdata, ".xdata", []sym.SymKind{}, 04)
  2295  		state.assignDsymsToSection(sect, sehp.xdata, sym.SRODATA, aligndatsize)
  2296  		state.checkdatsize(sym.SSEHSECT)
  2297  	}
  2298  }
  2299  
  2300  type symNameSize struct {
  2301  	name string
  2302  	sz   int64
  2303  	val  int64
  2304  	sym  loader.Sym
  2305  }
  2306  
  2307  func (state *dodataState) dodataSect(ctxt *Link, symn sym.SymKind, syms []loader.Sym) (result []loader.Sym, maxAlign int32) {
  2308  	var head, tail, zerobase loader.Sym
  2309  	ldr := ctxt.loader
  2310  	sl := make([]symNameSize, len(syms))
  2311  
  2312  	// For ppc64, we want to interleave the .got and .toc sections
  2313  	// from input files. Both are type sym.SELFGOT, so in that case
  2314  	// we skip size comparison and do the name comparison instead
  2315  	// (conveniently, .got sorts before .toc).
  2316  	sortBySize := symn != sym.SELFGOT
  2317  
  2318  	for k, s := range syms {
  2319  		ss := ldr.SymSize(s)
  2320  		sl[k] = symNameSize{sz: ss, sym: s}
  2321  		if !sortBySize {
  2322  			sl[k].name = ldr.SymName(s)
  2323  		}
  2324  		ds := int64(len(ldr.Data(s)))
  2325  		switch {
  2326  		case ss < ds:
  2327  			ctxt.Errorf(s, "initialize bounds (%d < %d)", ss, ds)
  2328  		case ss < 0:
  2329  			ctxt.Errorf(s, "negative size (%d bytes)", ss)
  2330  		case ss > cutoff:
  2331  			ctxt.Errorf(s, "symbol too large (%d bytes)", ss)
  2332  		}
  2333  
  2334  		// If the usually-special section-marker symbols are being laid
  2335  		// out as regular symbols, put them either at the beginning or
  2336  		// end of their section.
  2337  		if (ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin) || (ctxt.HeadType == objabi.Haix && ctxt.LinkMode == LinkExternal) {
  2338  			switch ldr.SymName(s) {
  2339  			case "runtime.text", "runtime.bss", "runtime.data", "runtime.types", "runtime.rodata",
  2340  				"runtime.noptrdata", "runtime.noptrbss":
  2341  				head = s
  2342  				continue
  2343  			case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes", "runtime.erodata",
  2344  				"runtime.enoptrdata", "runtime.enoptrbss":
  2345  				tail = s
  2346  				continue
  2347  			}
  2348  		}
  2349  	}
  2350  	zerobase = ldr.Lookup("runtime.zerobase", 0)
  2351  
  2352  	// Perform the sort.
  2353  	if symn != sym.SPCLNTAB {
  2354  		sort.Slice(sl, func(i, j int) bool {
  2355  			si, sj := sl[i].sym, sl[j].sym
  2356  			isz, jsz := sl[i].sz, sl[j].sz
  2357  			switch {
  2358  			case si == head, sj == tail:
  2359  				return true
  2360  			case sj == head, si == tail:
  2361  				return false
  2362  			}
  2363  			if sortBySize {
  2364  				switch {
  2365  				// put zerobase right after all the zero-sized symbols,
  2366  				// so zero-sized symbols have the same address as zerobase.
  2367  				case si == zerobase:
  2368  					return jsz != 0 // zerobase < nonzero-sized, zerobase > zero-sized
  2369  				case sj == zerobase:
  2370  					return isz == 0 // 0-sized < zerobase, nonzero-sized > zerobase
  2371  				case isz != jsz:
  2372  					return isz < jsz
  2373  				}
  2374  			} else {
  2375  				iname := sl[i].name
  2376  				jname := sl[j].name
  2377  				if iname != jname {
  2378  					return iname < jname
  2379  				}
  2380  			}
  2381  			return si < sj // break ties by symbol number
  2382  		})
  2383  	} else {
  2384  		// PCLNTAB was built internally, and already has the proper order.
  2385  	}
  2386  
  2387  	// Set alignment, construct result
  2388  	syms = syms[:0]
  2389  	for k := range sl {
  2390  		s := sl[k].sym
  2391  		if s != head && s != tail {
  2392  			align := symalign(ldr, s)
  2393  			if maxAlign < align {
  2394  				maxAlign = align
  2395  			}
  2396  		}
  2397  		syms = append(syms, s)
  2398  	}
  2399  
  2400  	return syms, maxAlign
  2401  }
  2402  
  2403  // Add buildid to beginning of text segment, on non-ELF systems.
  2404  // Non-ELF binary formats are not always flexible enough to
  2405  // give us a place to put the Go build ID. On those systems, we put it
  2406  // at the very beginning of the text segment.
  2407  // This “header” is read by cmd/go.
  2408  func (ctxt *Link) textbuildid() {
  2409  	if ctxt.IsELF || *flagBuildid == "" {
  2410  		return
  2411  	}
  2412  
  2413  	ldr := ctxt.loader
  2414  	s := ldr.CreateSymForUpdate("go:buildid", 0)
  2415  	// The \xff is invalid UTF-8, meant to make it less likely
  2416  	// to find one of these accidentally.
  2417  	data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff"
  2418  	s.SetType(sym.STEXT)
  2419  	s.SetData([]byte(data))
  2420  	s.SetSize(int64(len(data)))
  2421  
  2422  	ctxt.Textp = append(ctxt.Textp, 0)
  2423  	copy(ctxt.Textp[1:], ctxt.Textp)
  2424  	ctxt.Textp[0] = s.Sym()
  2425  }
  2426  
  2427  func (ctxt *Link) buildinfo() {
  2428  	// Write the buildinfo symbol, which go version looks for.
  2429  	// The code reading this data is in package debug/buildinfo.
  2430  	ldr := ctxt.loader
  2431  	s := ldr.CreateSymForUpdate("go:buildinfo", 0)
  2432  	s.SetType(sym.SBUILDINFO)
  2433  	s.SetAlign(16)
  2434  
  2435  	// The \xff is invalid UTF-8, meant to make it less likely
  2436  	// to find one of these accidentally.
  2437  	const prefix = "\xff Go buildinf:" // 14 bytes, plus 1 data byte filled in below
  2438  
  2439  	// Header is always 32-bytes, a hold-over from before
  2440  	// https://go.dev/cl/369977.
  2441  	data := make([]byte, 32)
  2442  	copy(data, prefix)
  2443  	data[len(prefix)] = byte(ctxt.Arch.PtrSize)
  2444  	data[len(prefix)+1] = 0
  2445  	if ctxt.Arch.ByteOrder == binary.BigEndian {
  2446  		data[len(prefix)+1] = 1
  2447  	}
  2448  	data[len(prefix)+1] |= 2 // signals new pointer-free format
  2449  	data = appendString(data, strdata["runtime.buildVersion"])
  2450  	data = appendString(data, strdata["runtime.modinfo"])
  2451  	// MacOS linker gets very upset if the size is not a multiple of alignment.
  2452  	for len(data)%16 != 0 {
  2453  		data = append(data, 0)
  2454  	}
  2455  	s.SetData(data)
  2456  	s.SetSize(int64(len(data)))
  2457  
  2458  	// Add reference to go:buildinfo from the rodata section,
  2459  	// so that external linking with -Wl,--gc-sections does not
  2460  	// delete the build info.
  2461  	sr := ldr.CreateSymForUpdate("go:buildinfo.ref", 0)
  2462  	sr.SetType(sym.SRODATA)
  2463  	sr.SetAlign(int32(ctxt.Arch.PtrSize))
  2464  	sr.AddAddr(ctxt.Arch, s.Sym())
  2465  }
  2466  
  2467  // appendString appends s to data, prefixed by its varint-encoded length.
  2468  func appendString(data []byte, s string) []byte {
  2469  	var v [binary.MaxVarintLen64]byte
  2470  	n := binary.PutUvarint(v[:], uint64(len(s)))
  2471  	data = append(data, v[:n]...)
  2472  	data = append(data, s...)
  2473  	return data
  2474  }
  2475  
  2476  // assign addresses to text
  2477  func (ctxt *Link) textaddress() {
  2478  	addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05)
  2479  
  2480  	// Assign PCs in text segment.
  2481  	// Could parallelize, by assigning to text
  2482  	// and then letting threads copy down, but probably not worth it.
  2483  	sect := Segtext.Sections[0]
  2484  
  2485  	sect.Align = int32(Funcalign)
  2486  
  2487  	ldr := ctxt.loader
  2488  
  2489  	if *flagRandLayout != 0 {
  2490  		r := rand.New(rand.NewSource(*flagRandLayout))
  2491  		textp := ctxt.Textp
  2492  		i := 0
  2493  		// don't move the buildid symbol
  2494  		if len(textp) > 0 && ldr.SymName(textp[0]) == "go:buildid" {
  2495  			i++
  2496  		}
  2497  		// Skip over C symbols, as functions in a (C object) section must stay together.
  2498  		// TODO: maybe we can move a section as a whole.
  2499  		// Note: we load C symbols before Go symbols, so we can scan from the start.
  2500  		for i < len(textp) && (ldr.SubSym(textp[i]) != 0 || ldr.AttrSubSymbol(textp[i])) {
  2501  			i++
  2502  		}
  2503  		textp = textp[i:]
  2504  		r.Shuffle(len(textp), func(i, j int) {
  2505  			textp[i], textp[j] = textp[j], textp[i]
  2506  		})
  2507  	}
  2508  
  2509  	// Sort the text symbols by type, so that FIPS symbols are
  2510  	// gathered together, with the FIPS start and end symbols
  2511  	// bracketing them , even if we've randomized the overall order.
  2512  	sort.SliceStable(ctxt.Textp, func(i, j int) bool {
  2513  		return ldr.SymType(ctxt.Textp[i]) < ldr.SymType(ctxt.Textp[j])
  2514  	})
  2515  
  2516  	text := ctxt.xdefine("runtime.text", sym.STEXT, 0)
  2517  	etext := ctxt.xdefine("runtime.etext", sym.STEXTEND, 0)
  2518  	ldr.SetSymSect(text, sect)
  2519  	if ctxt.IsAIX() && ctxt.IsExternal() {
  2520  		// Setting runtime.text has a real symbol prevents ld to
  2521  		// change its base address resulting in wrong offsets for
  2522  		// reflect methods.
  2523  		u := ldr.MakeSymbolUpdater(text)
  2524  		u.SetAlign(sect.Align)
  2525  		u.SetSize(8)
  2526  	}
  2527  
  2528  	if (ctxt.DynlinkingGo() && ctxt.IsDarwin()) || (ctxt.IsAIX() && ctxt.IsExternal()) {
  2529  		ldr.SetSymSect(etext, sect)
  2530  		ctxt.Textp = append(ctxt.Textp, etext, 0)
  2531  		copy(ctxt.Textp[1:], ctxt.Textp)
  2532  		ctxt.Textp[0] = text
  2533  	}
  2534  
  2535  	start := uint64(Rnd(*FlagTextAddr, int64(Funcalign)))
  2536  	va := start
  2537  	n := 1
  2538  	sect.Vaddr = va
  2539  
  2540  	limit := thearch.TrampLimit
  2541  	if limit == 0 {
  2542  		limit = 1 << 63 // unlimited
  2543  	}
  2544  	if *FlagDebugTextSize != 0 {
  2545  		limit = uint64(*FlagDebugTextSize)
  2546  	}
  2547  	if *FlagDebugTramp > 1 {
  2548  		limit = 1 // debug mode, force generating trampolines for everything
  2549  	}
  2550  
  2551  	if ctxt.IsAIX() && ctxt.IsExternal() {
  2552  		// On AIX, normally we won't generate direct calls to external symbols,
  2553  		// except in one test, cmd/go/testdata/script/link_syso_issue33139.txt.
  2554  		// That test doesn't make much sense, and I'm not sure it ever works.
  2555  		// Just generate trampoline for now (which will turn a direct call to
  2556  		// an indirect call, which at least builds).
  2557  		limit = 1
  2558  	}
  2559  
  2560  	// First pass: assign addresses assuming the program is small and will
  2561  	// not require trampoline generation.
  2562  	big := false
  2563  	for _, s := range ctxt.Textp {
  2564  		sect, n, va = assignAddress(ctxt, sect, n, s, va, false, big)
  2565  		if va-start >= limit {
  2566  			big = true
  2567  			break
  2568  		}
  2569  	}
  2570  
  2571  	// Second pass: only if it is too big, insert trampolines for too-far
  2572  	// jumps and targets with unknown addresses.
  2573  	if big {
  2574  		// reset addresses
  2575  		for _, s := range ctxt.Textp {
  2576  			if s != text {
  2577  				resetAddress(ctxt, s)
  2578  			}
  2579  		}
  2580  		va = start
  2581  
  2582  		ntramps := 0
  2583  		var curPkg string
  2584  		for i, s := range ctxt.Textp {
  2585  			// When we find the first symbol in a package, perform a
  2586  			// single iteration that assigns temporary addresses to all
  2587  			// of the text in the same package, using the maximum possible
  2588  			// number of trampolines. This allows for better decisions to
  2589  			// be made regarding reachability and the need for trampolines.
  2590  			if symPkg := ldr.SymPkg(s); symPkg != "" && curPkg != symPkg {
  2591  				curPkg = symPkg
  2592  				vaTmp := va
  2593  				for j := i; j < len(ctxt.Textp); j++ {
  2594  					curSym := ctxt.Textp[j]
  2595  					if symPkg := ldr.SymPkg(curSym); symPkg == "" || curPkg != symPkg {
  2596  						break
  2597  					}
  2598  					// We do not pass big to assignAddress here, as this
  2599  					// can result in side effects such as section splitting.
  2600  					sect, n, vaTmp = assignAddress(ctxt, sect, n, curSym, vaTmp, false, false)
  2601  					vaTmp += maxSizeTrampolines(ctxt, ldr, curSym, false)
  2602  				}
  2603  			}
  2604  
  2605  			// Reset address for current symbol.
  2606  			if s != text {
  2607  				resetAddress(ctxt, s)
  2608  			}
  2609  
  2610  			// Assign actual address for current symbol.
  2611  			sect, n, va = assignAddress(ctxt, sect, n, s, va, false, big)
  2612  
  2613  			// Resolve jumps, adding trampolines if they are needed.
  2614  			trampoline(ctxt, s)
  2615  
  2616  			// lay down trampolines after each function
  2617  			for ; ntramps < len(ctxt.tramps); ntramps++ {
  2618  				tramp := ctxt.tramps[ntramps]
  2619  				if ctxt.IsAIX() && strings.HasPrefix(ldr.SymName(tramp), "runtime.text.") {
  2620  					// Already set in assignAddress
  2621  					continue
  2622  				}
  2623  				sect, n, va = assignAddress(ctxt, sect, n, tramp, va, true, big)
  2624  			}
  2625  		}
  2626  
  2627  		// merge tramps into Textp, keeping Textp in address order
  2628  		if ntramps != 0 {
  2629  			newtextp := make([]loader.Sym, 0, len(ctxt.Textp)+ntramps)
  2630  			i := 0
  2631  			for _, s := range ctxt.Textp {
  2632  				for ; i < ntramps && ldr.SymValue(ctxt.tramps[i]) < ldr.SymValue(s); i++ {
  2633  					newtextp = append(newtextp, ctxt.tramps[i])
  2634  				}
  2635  				newtextp = append(newtextp, s)
  2636  			}
  2637  			newtextp = append(newtextp, ctxt.tramps[i:ntramps]...)
  2638  
  2639  			ctxt.Textp = newtextp
  2640  		}
  2641  	}
  2642  
  2643  	// Add MinLC size after etext, so it won't collide with the next symbol
  2644  	// (which may confuse some symbolizer).
  2645  	sect.Length = va - sect.Vaddr + uint64(ctxt.Arch.MinLC)
  2646  	ldr.SetSymSect(etext, sect)
  2647  	if ldr.SymValue(etext) == 0 {
  2648  		// Set the address of the start/end symbols, if not already
  2649  		// (i.e. not darwin+dynlink or AIX+external, see above).
  2650  		ldr.SetSymValue(etext, int64(va))
  2651  		ldr.SetSymValue(text, int64(Segtext.Sections[0].Vaddr))
  2652  	}
  2653  }
  2654  
  2655  // assigns address for a text symbol, returns (possibly new) section, its number, and the address.
  2656  func assignAddress(ctxt *Link, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp, big bool) (*sym.Section, int, uint64) {
  2657  	ldr := ctxt.loader
  2658  	if thearch.AssignAddress != nil {
  2659  		return thearch.AssignAddress(ldr, sect, n, s, va, isTramp)
  2660  	}
  2661  
  2662  	ldr.SetSymSect(s, sect)
  2663  	if ldr.AttrSubSymbol(s) {
  2664  		return sect, n, va
  2665  	}
  2666  
  2667  	align := ldr.SymAlign(s)
  2668  	align = max(align, int32(Funcalign))
  2669  	va = uint64(Rnd(int64(va), int64(align)))
  2670  	if sect.Align < align {
  2671  		sect.Align = align
  2672  	}
  2673  
  2674  	funcsize := uint64(abi.MINFUNC) // spacing required for findfunctab
  2675  	if ldr.SymSize(s) > abi.MINFUNC {
  2676  		funcsize = uint64(ldr.SymSize(s))
  2677  	}
  2678  
  2679  	// If we need to split text sections, and this function doesn't fit in the current
  2680  	// section, then create a new one.
  2681  	//
  2682  	// Only break at outermost syms.
  2683  	if big && splitTextSections(ctxt) && ldr.OuterSym(s) == 0 {
  2684  		// For debugging purposes, allow text size limit to be cranked down,
  2685  		// so as to stress test the code that handles multiple text sections.
  2686  		var textSizelimit uint64 = thearch.TrampLimit
  2687  		if *FlagDebugTextSize != 0 {
  2688  			textSizelimit = uint64(*FlagDebugTextSize)
  2689  		}
  2690  
  2691  		// Sanity check: make sure the limit is larger than any
  2692  		// individual text symbol.
  2693  		if funcsize > textSizelimit {
  2694  			panic(fmt.Sprintf("error: text size limit %d less than text symbol %s size of %d", textSizelimit, ldr.SymName(s), funcsize))
  2695  		}
  2696  
  2697  		if va-sect.Vaddr+funcsize+maxSizeTrampolines(ctxt, ldr, s, isTramp) > textSizelimit {
  2698  			sectAlign := int32(thearch.Funcalign)
  2699  			if ctxt.IsPPC64() {
  2700  				// Align the next text section to the worst case function alignment likely
  2701  				// to be encountered when processing function symbols. The start address
  2702  				// is rounded against the final alignment of the text section later on in
  2703  				// (*Link).address. This may happen due to usage of PCALIGN directives
  2704  				// larger than Funcalign, or usage of ISA 3.1 prefixed instructions
  2705  				// (see ISA 3.1 Book I 1.9).
  2706  				const ppc64maxFuncalign = 64
  2707  				sectAlign = ppc64maxFuncalign
  2708  				va = uint64(Rnd(int64(va), ppc64maxFuncalign))
  2709  			}
  2710  
  2711  			// Set the length for the previous text section
  2712  			sect.Length = va - sect.Vaddr
  2713  
  2714  			// Create new section, set the starting Vaddr
  2715  			sect = addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05)
  2716  
  2717  			sect.Vaddr = va
  2718  			sect.Align = sectAlign
  2719  			ldr.SetSymSect(s, sect)
  2720  
  2721  			// Create a symbol for the start of the secondary text sections
  2722  			ntext := ldr.CreateSymForUpdate(fmt.Sprintf("runtime.text.%d", n), 0)
  2723  			ntext.SetSect(sect)
  2724  			if ctxt.IsAIX() {
  2725  				// runtime.text.X must be a real symbol on AIX.
  2726  				// Assign its address directly in order to be the
  2727  				// first symbol of this new section.
  2728  				ntext.SetType(sym.STEXT)
  2729  				ntext.SetSize(int64(abi.MINFUNC))
  2730  				ntext.SetOnList(true)
  2731  				ntext.SetAlign(sectAlign)
  2732  				ctxt.tramps = append(ctxt.tramps, ntext.Sym())
  2733  
  2734  				ntext.SetValue(int64(va))
  2735  				va += uint64(ntext.Size())
  2736  
  2737  				if align := ldr.SymAlign(s); align != 0 {
  2738  					va = uint64(Rnd(int64(va), int64(align)))
  2739  				} else {
  2740  					va = uint64(Rnd(int64(va), int64(Funcalign)))
  2741  				}
  2742  			}
  2743  			n++
  2744  		}
  2745  	}
  2746  
  2747  	ldr.SetSymValue(s, 0)
  2748  	for sub := s; sub != 0; sub = ldr.SubSym(sub) {
  2749  		ldr.SetSymValue(sub, ldr.SymValue(sub)+int64(va))
  2750  		if ctxt.Debugvlog > 2 {
  2751  			fmt.Println("assign text address:", ldr.SymName(sub), ldr.SymValue(sub))
  2752  		}
  2753  	}
  2754  
  2755  	va += funcsize
  2756  
  2757  	return sect, n, va
  2758  }
  2759  
  2760  func resetAddress(ctxt *Link, s loader.Sym) {
  2761  	ldr := ctxt.loader
  2762  	if ldr.OuterSym(s) != 0 {
  2763  		return
  2764  	}
  2765  	oldv := ldr.SymValue(s)
  2766  	for sub := s; sub != 0; sub = ldr.SubSym(sub) {
  2767  		ldr.SetSymValue(sub, ldr.SymValue(sub)-oldv)
  2768  	}
  2769  }
  2770  
  2771  // Return whether we may need to split text sections.
  2772  //
  2773  // On PPC64x, when external linking, a text section should not be
  2774  // larger than 2^25 bytes due to the size of call target offset field
  2775  // in the 'bl' instruction. Splitting into smaller text sections
  2776  // smaller than this limit allows the system linker to modify the long
  2777  // calls appropriately. The limit allows for the space needed for
  2778  // tables inserted by the linker.
  2779  //
  2780  // The same applies to Darwin/ARM64, with 2^27 byte threshold.
  2781  //
  2782  // Similarly for ARM, we split sections (at 2^25 bytes) to avoid
  2783  // inconsistencies between the Go linker's reachability calculations
  2784  // (e.g. will direct call from X to Y need a trampoline) and similar
  2785  // machinery in the external linker; see #58425 for more on the
  2786  // history here.
  2787  func splitTextSections(ctxt *Link) bool {
  2788  	return (ctxt.IsARM() || ctxt.IsPPC64() || (ctxt.IsARM64() && ctxt.IsDarwin())) && ctxt.IsExternal()
  2789  }
  2790  
  2791  // On Wasm, we reserve 4096 bytes for zero page, then 8192 bytes for wasm_exec.js
  2792  // to store command line args and environment variables.
  2793  // Data sections starts from at least address 12288.
  2794  // Keep in sync with wasm_exec.js.
  2795  const wasmMinDataAddr = 4096 + 8192
  2796  
  2797  // address assigns virtual addresses to all segments and sections and
  2798  // returns all segments in file order.
  2799  func (ctxt *Link) address() []*sym.Segment {
  2800  	var order []*sym.Segment // Layout order
  2801  
  2802  	va := uint64(*FlagTextAddr)
  2803  	order = append(order, &Segtext)
  2804  	Segtext.Rwx = 05
  2805  	Segtext.Vaddr = va
  2806  	for i, s := range Segtext.Sections {
  2807  		va = uint64(Rnd(int64(va), int64(s.Align)))
  2808  		s.Vaddr = va
  2809  		va += s.Length
  2810  
  2811  		if ctxt.IsWasm() && i == 0 && va < wasmMinDataAddr {
  2812  			va = wasmMinDataAddr
  2813  		}
  2814  	}
  2815  
  2816  	Segtext.Length = va - uint64(*FlagTextAddr)
  2817  
  2818  	if len(Segrodata.Sections) > 0 {
  2819  		// align to page boundary so as not to mix
  2820  		// rodata and executable text.
  2821  		//
  2822  		// Note: gold or GNU ld will reduce the size of the executable
  2823  		// file by arranging for the relro segment to end at a page
  2824  		// boundary, and overlap the end of the text segment with the
  2825  		// start of the relro segment in the file.  The PT_LOAD segments
  2826  		// will be such that the last page of the text segment will be
  2827  		// mapped twice, once r-x and once starting out rw- and, after
  2828  		// relocation processing, changed to r--.
  2829  		//
  2830  		// Ideally the last page of the text segment would not be
  2831  		// writable even for this short period.
  2832  		va = uint64(Rnd(int64(va), *FlagRound))
  2833  
  2834  		order = append(order, &Segrodata)
  2835  		Segrodata.Rwx = 04
  2836  		Segrodata.Vaddr = va
  2837  		for _, s := range Segrodata.Sections {
  2838  			va = uint64(Rnd(int64(va), int64(s.Align)))
  2839  			s.Vaddr = va
  2840  			va += s.Length
  2841  		}
  2842  
  2843  		Segrodata.Length = va - Segrodata.Vaddr
  2844  	}
  2845  	if len(Segrelrodata.Sections) > 0 {
  2846  		// align to page boundary so as not to mix
  2847  		// rodata, rel-ro data, and executable text.
  2848  		va = uint64(Rnd(int64(va), *FlagRound))
  2849  		if ctxt.HeadType == objabi.Haix {
  2850  			// Relro data are inside data segment on AIX.
  2851  			va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE)
  2852  		}
  2853  
  2854  		order = append(order, &Segrelrodata)
  2855  		Segrelrodata.Rwx = 06
  2856  		Segrelrodata.Vaddr = va
  2857  		for _, s := range Segrelrodata.Sections {
  2858  			va = uint64(Rnd(int64(va), int64(s.Align)))
  2859  			s.Vaddr = va
  2860  			va += s.Length
  2861  		}
  2862  
  2863  		Segrelrodata.Length = va - Segrelrodata.Vaddr
  2864  	}
  2865  
  2866  	va = uint64(Rnd(int64(va), *FlagRound))
  2867  	if ctxt.HeadType == objabi.Haix && len(Segrelrodata.Sections) == 0 {
  2868  		// Data sections are moved to an unreachable segment
  2869  		// to ensure that they are position-independent.
  2870  		// Already done if relro sections exist.
  2871  		va += uint64(XCOFFDATABASE) - uint64(XCOFFTEXTBASE)
  2872  	}
  2873  	order = append(order, &Segdata)
  2874  	Segdata.Rwx = 06
  2875  	Segdata.Vaddr = va
  2876  	var data *sym.Section
  2877  	var noptr *sym.Section
  2878  	var bss *sym.Section
  2879  	var noptrbss *sym.Section
  2880  	var fuzzCounters *sym.Section
  2881  	for i, s := range Segdata.Sections {
  2882  		if (ctxt.IsELF || ctxt.HeadType == objabi.Haix) && s.Name == ".tbss" {
  2883  			continue
  2884  		}
  2885  		vlen := int64(s.Length)
  2886  		if i+1 < len(Segdata.Sections) && !((ctxt.IsELF || ctxt.HeadType == objabi.Haix) && Segdata.Sections[i+1].Name == ".tbss") {
  2887  			vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr)
  2888  		}
  2889  		s.Vaddr = va
  2890  		va += uint64(vlen)
  2891  		Segdata.Length = va - Segdata.Vaddr
  2892  		switch s.Name {
  2893  		case ".data":
  2894  			data = s
  2895  		case ".noptrdata":
  2896  			noptr = s
  2897  		case ".bss":
  2898  			bss = s
  2899  		case ".noptrbss":
  2900  			noptrbss = s
  2901  		case ".go.fuzzcntrs":
  2902  			fuzzCounters = s
  2903  		}
  2904  	}
  2905  
  2906  	// Assign Segdata's Filelen omitting the BSS. We do this here
  2907  	// simply because right now we know where the BSS starts.
  2908  	Segdata.Filelen = bss.Vaddr - Segdata.Vaddr
  2909  
  2910  	if len(Segpdata.Sections) > 0 {
  2911  		va = uint64(Rnd(int64(va), *FlagRound))
  2912  		order = append(order, &Segpdata)
  2913  		Segpdata.Rwx = 04
  2914  		Segpdata.Vaddr = va
  2915  		// Segpdata.Sections is intended to contain just one section.
  2916  		// Loop through the slice anyway for consistency.
  2917  		for _, s := range Segpdata.Sections {
  2918  			va = uint64(Rnd(int64(va), int64(s.Align)))
  2919  			s.Vaddr = va
  2920  			va += s.Length
  2921  		}
  2922  		Segpdata.Length = va - Segpdata.Vaddr
  2923  	}
  2924  
  2925  	if len(Segxdata.Sections) > 0 {
  2926  		va = uint64(Rnd(int64(va), *FlagRound))
  2927  		order = append(order, &Segxdata)
  2928  		Segxdata.Rwx = 04
  2929  		Segxdata.Vaddr = va
  2930  		// Segxdata.Sections is intended to contain just one section.
  2931  		// Loop through the slice anyway for consistency.
  2932  		for _, s := range Segxdata.Sections {
  2933  			va = uint64(Rnd(int64(va), int64(s.Align)))
  2934  			s.Vaddr = va
  2935  			va += s.Length
  2936  		}
  2937  		Segxdata.Length = va - Segxdata.Vaddr
  2938  	}
  2939  
  2940  	va = uint64(Rnd(int64(va), *FlagRound))
  2941  	order = append(order, &Segdwarf)
  2942  	Segdwarf.Rwx = 06
  2943  	Segdwarf.Vaddr = va
  2944  	for i, s := range Segdwarf.Sections {
  2945  		vlen := int64(s.Length)
  2946  		if i+1 < len(Segdwarf.Sections) {
  2947  			vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr)
  2948  		}
  2949  		s.Vaddr = va
  2950  		va += uint64(vlen)
  2951  		if ctxt.HeadType == objabi.Hwindows {
  2952  			va = uint64(Rnd(int64(va), PEFILEALIGN))
  2953  		}
  2954  		Segdwarf.Length = va - Segdwarf.Vaddr
  2955  	}
  2956  
  2957  	ldr := ctxt.loader
  2958  	var (
  2959  		rodata  = ldr.SymSect(ldr.LookupOrCreateSym("runtime.rodata", 0))
  2960  		symtab  = ldr.SymSect(ldr.LookupOrCreateSym("runtime.symtab", 0))
  2961  		pclntab = ldr.SymSect(ldr.LookupOrCreateSym("runtime.pclntab", 0))
  2962  		types   = ldr.SymSect(ldr.LookupOrCreateSym("runtime.types", 0))
  2963  	)
  2964  
  2965  	for _, s := range ctxt.datap {
  2966  		if sect := ldr.SymSect(s); sect != nil {
  2967  			ldr.AddToSymValue(s, int64(sect.Vaddr))
  2968  		}
  2969  		v := ldr.SymValue(s)
  2970  		for sub := ldr.SubSym(s); sub != 0; sub = ldr.SubSym(sub) {
  2971  			ldr.AddToSymValue(sub, v)
  2972  		}
  2973  	}
  2974  
  2975  	for _, si := range dwarfp {
  2976  		for _, s := range si.syms {
  2977  			if sect := ldr.SymSect(s); sect != nil {
  2978  				ldr.AddToSymValue(s, int64(sect.Vaddr))
  2979  			}
  2980  			sub := ldr.SubSym(s)
  2981  			if sub != 0 {
  2982  				panic(fmt.Sprintf("unexpected sub-sym for %s %s", ldr.SymName(s), ldr.SymType(s).String()))
  2983  			}
  2984  			v := ldr.SymValue(s)
  2985  			for ; sub != 0; sub = ldr.SubSym(sub) {
  2986  				ldr.AddToSymValue(s, v)
  2987  			}
  2988  		}
  2989  	}
  2990  
  2991  	for _, s := range sehp.pdata {
  2992  		if sect := ldr.SymSect(s); sect != nil {
  2993  			ldr.AddToSymValue(s, int64(sect.Vaddr))
  2994  		}
  2995  	}
  2996  	for _, s := range sehp.xdata {
  2997  		if sect := ldr.SymSect(s); sect != nil {
  2998  			ldr.AddToSymValue(s, int64(sect.Vaddr))
  2999  		}
  3000  	}
  3001  
  3002  	if ctxt.BuildMode == BuildModeShared {
  3003  		s := ldr.LookupOrCreateSym("go:link.abihashbytes", 0)
  3004  		sect := ldr.SymSect(ldr.LookupOrCreateSym(".note.go.abihash", 0))
  3005  		ldr.SetSymSect(s, sect)
  3006  		ldr.SetSymValue(s, int64(sect.Vaddr+16))
  3007  	}
  3008  
  3009  	// If there are multiple text sections, create runtime.text.n for
  3010  	// their section Vaddr, using n for index
  3011  	n := 1
  3012  	for _, sect := range Segtext.Sections[1:] {
  3013  		if sect.Name != ".text" {
  3014  			break
  3015  		}
  3016  		symname := fmt.Sprintf("runtime.text.%d", n)
  3017  		if ctxt.HeadType != objabi.Haix || ctxt.LinkMode != LinkExternal {
  3018  			// Addresses are already set on AIX with external linker
  3019  			// because these symbols are part of their sections.
  3020  			ctxt.xdefine(symname, sym.STEXT, int64(sect.Vaddr))
  3021  		}
  3022  		n++
  3023  	}
  3024  
  3025  	ctxt.xdefine("runtime.rodata", sym.SRODATA, int64(rodata.Vaddr))
  3026  	ctxt.xdefine("runtime.erodata", sym.SRODATA, int64(rodata.Vaddr+rodata.Length))
  3027  	ctxt.xdefine("runtime.types", sym.SRODATA, int64(types.Vaddr))
  3028  	ctxt.xdefine("runtime.etypes", sym.SRODATA, int64(types.Vaddr+types.Length))
  3029  
  3030  	s := ldr.Lookup("runtime.gcdata", 0)
  3031  	ldr.SetAttrLocal(s, true)
  3032  	ctxt.xdefine("runtime.egcdata", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s))
  3033  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcdata", 0), ldr.SymSect(s))
  3034  
  3035  	s = ldr.LookupOrCreateSym("runtime.gcbss", 0)
  3036  	ldr.SetAttrLocal(s, true)
  3037  	ctxt.xdefine("runtime.egcbss", sym.SRODATA, ldr.SymAddr(s)+ldr.SymSize(s))
  3038  	ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.egcbss", 0), ldr.SymSect(s))
  3039  
  3040  	ctxt.xdefine("runtime.symtab", sym.SRODATA, int64(symtab.Vaddr))
  3041  	ctxt.xdefine("runtime.esymtab", sym.SRODATA, int64(symtab.Vaddr+symtab.Length))
  3042  	ctxt.xdefine("runtime.pclntab", sym.SRODATA, int64(pclntab.Vaddr))
  3043  	ctxt.defineInternal("runtime.pcheader", sym.SRODATA)
  3044  	ctxt.defineInternal("runtime.funcnametab", sym.SRODATA)
  3045  	ctxt.defineInternal("runtime.cutab", sym.SRODATA)
  3046  	ctxt.defineInternal("runtime.filetab", sym.SRODATA)
  3047  	ctxt.defineInternal("runtime.pctab", sym.SRODATA)
  3048  	ctxt.defineInternal("runtime.functab", sym.SRODATA)
  3049  	ctxt.xdefine("runtime.epclntab", sym.SRODATA, int64(pclntab.Vaddr+pclntab.Length))
  3050  	ctxt.xdefine("runtime.noptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr))
  3051  	ctxt.xdefine("runtime.enoptrdata", sym.SNOPTRDATAEND, int64(noptr.Vaddr+noptr.Length))
  3052  	ctxt.xdefine("runtime.bss", sym.SBSS, int64(bss.Vaddr))
  3053  	ctxt.xdefine("runtime.ebss", sym.SBSS, int64(bss.Vaddr+bss.Length))
  3054  	ctxt.xdefine("runtime.data", sym.SDATA, int64(data.Vaddr))
  3055  	ctxt.xdefine("runtime.edata", sym.SDATAEND, int64(data.Vaddr+data.Length))
  3056  	ctxt.xdefine("runtime.noptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr))
  3057  	ctxt.xdefine("runtime.enoptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length))
  3058  	ctxt.xdefine("runtime.covctrs", sym.SCOVERAGE_COUNTER, int64(noptrbss.Vaddr+covCounterDataStartOff))
  3059  	ctxt.xdefine("runtime.ecovctrs", sym.SCOVERAGE_COUNTER, int64(noptrbss.Vaddr+covCounterDataStartOff+covCounterDataLen))
  3060  	ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length))
  3061  
  3062  	if fuzzCounters != nil {
  3063  		if *flagAsan {
  3064  			// ASAN requires that the symbol marking the end
  3065  			// of the section be aligned on an 8 byte boundary.
  3066  			// See issue #66966.
  3067  			fuzzCounters.Length = uint64(Rnd(int64(fuzzCounters.Length), 8))
  3068  		}
  3069  		ctxt.xdefine("runtime.__start___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr))
  3070  		ctxt.xdefine("runtime.__stop___sancov_cntrs", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length))
  3071  		ctxt.xdefine("internal/fuzz._counters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr))
  3072  		ctxt.xdefine("internal/fuzz._ecounters", sym.SLIBFUZZER_8BIT_COUNTER, int64(fuzzCounters.Vaddr+fuzzCounters.Length))
  3073  	}
  3074  
  3075  	if ctxt.IsSolaris() {
  3076  		// On Solaris, in the runtime it sets the external names of the
  3077  		// end symbols. Unset them and define separate symbols, so we
  3078  		// keep both.
  3079  		etext := ldr.Lookup("runtime.etext", 0)
  3080  		edata := ldr.Lookup("runtime.edata", 0)
  3081  		end := ldr.Lookup("runtime.end", 0)
  3082  		ldr.SetSymExtname(etext, "runtime.etext")
  3083  		ldr.SetSymExtname(edata, "runtime.edata")
  3084  		ldr.SetSymExtname(end, "runtime.end")
  3085  		ctxt.xdefine("_etext", ldr.SymType(etext), ldr.SymValue(etext))
  3086  		ctxt.xdefine("_edata", ldr.SymType(edata), ldr.SymValue(edata))
  3087  		ctxt.xdefine("_end", ldr.SymType(end), ldr.SymValue(end))
  3088  		ldr.SetSymSect(ldr.Lookup("_etext", 0), ldr.SymSect(etext))
  3089  		ldr.SetSymSect(ldr.Lookup("_edata", 0), ldr.SymSect(edata))
  3090  		ldr.SetSymSect(ldr.Lookup("_end", 0), ldr.SymSect(end))
  3091  	}
  3092  
  3093  	if ctxt.IsPPC64() && ctxt.IsElf() {
  3094  		// Resolve .TOC. symbols for all objects. Only one TOC region is supported. If a
  3095  		// GOT section is present, compute it as suggested by the ELFv2 ABI. Otherwise,
  3096  		// choose a similar offset from the start of the data segment.
  3097  		tocAddr := int64(Segdata.Vaddr) + 0x8000
  3098  		if gotAddr := ldr.SymValue(ctxt.GOT); gotAddr != 0 {
  3099  			tocAddr = gotAddr + 0x8000
  3100  		}
  3101  		for i := range ctxt.DotTOC {
  3102  			if i >= sym.SymVerABICount && i < sym.SymVerStatic { // these versions are not used currently
  3103  				continue
  3104  			}
  3105  			if toc := ldr.Lookup(".TOC.", i); toc != 0 {
  3106  				ldr.SetSymValue(toc, tocAddr)
  3107  			}
  3108  		}
  3109  	}
  3110  
  3111  	return order
  3112  }
  3113  
  3114  // layout assigns file offsets and lengths to the segments in order.
  3115  // Returns the file size containing all the segments.
  3116  func (ctxt *Link) layout(order []*sym.Segment) uint64 {
  3117  	var prev *sym.Segment
  3118  	for _, seg := range order {
  3119  		if prev == nil {
  3120  			seg.Fileoff = uint64(HEADR)
  3121  		} else {
  3122  			switch ctxt.HeadType {
  3123  			default:
  3124  				// Assuming the previous segment was
  3125  				// aligned, the following rounding
  3126  				// should ensure that this segment's
  3127  				// VA ≡ Fileoff mod FlagRound.
  3128  				seg.Fileoff = uint64(Rnd(int64(prev.Fileoff+prev.Filelen), *FlagRound))
  3129  				if seg.Vaddr%uint64(*FlagRound) != seg.Fileoff%uint64(*FlagRound) {
  3130  					Exitf("bad segment rounding (Vaddr=%#x Fileoff=%#x FlagRound=%#x)", seg.Vaddr, seg.Fileoff, *FlagRound)
  3131  				}
  3132  			case objabi.Hwindows:
  3133  				seg.Fileoff = prev.Fileoff + uint64(Rnd(int64(prev.Filelen), PEFILEALIGN))
  3134  			case objabi.Hplan9:
  3135  				seg.Fileoff = prev.Fileoff + prev.Filelen
  3136  			}
  3137  		}
  3138  		if seg != &Segdata {
  3139  			// Link.address already set Segdata.Filelen to
  3140  			// account for BSS.
  3141  			seg.Filelen = seg.Length
  3142  		}
  3143  		prev = seg
  3144  	}
  3145  	return prev.Fileoff + prev.Filelen
  3146  }
  3147  
  3148  // add a trampoline with symbol s (to be laid down after the current function)
  3149  func (ctxt *Link) AddTramp(s *loader.SymbolBuilder, typ sym.SymKind) {
  3150  	s.SetType(typ)
  3151  	s.SetReachable(true)
  3152  	s.SetOnList(true)
  3153  	ctxt.tramps = append(ctxt.tramps, s.Sym())
  3154  	if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 {
  3155  		ctxt.Logf("trampoline %s inserted\n", s.Name())
  3156  	}
  3157  }
  3158  
  3159  // compressSyms compresses syms and returns the contents of the
  3160  // compressed section. If the section would get larger, it returns nil.
  3161  func compressSyms(ctxt *Link, syms []loader.Sym) []byte {
  3162  	ldr := ctxt.loader
  3163  	var total int64
  3164  	for _, sym := range syms {
  3165  		total += ldr.SymSize(sym)
  3166  	}
  3167  
  3168  	var buf bytes.Buffer
  3169  	if ctxt.IsELF {
  3170  		switch ctxt.Arch.PtrSize {
  3171  		case 8:
  3172  			binary.Write(&buf, ctxt.Arch.ByteOrder, elf.Chdr64{
  3173  				Type:      uint32(elf.COMPRESS_ZLIB),
  3174  				Size:      uint64(total),
  3175  				Addralign: uint64(ctxt.Arch.Alignment),
  3176  			})
  3177  		case 4:
  3178  			binary.Write(&buf, ctxt.Arch.ByteOrder, elf.Chdr32{
  3179  				Type:      uint32(elf.COMPRESS_ZLIB),
  3180  				Size:      uint32(total),
  3181  				Addralign: uint32(ctxt.Arch.Alignment),
  3182  			})
  3183  		default:
  3184  			log.Fatalf("can't compress header size:%d", ctxt.Arch.PtrSize)
  3185  		}
  3186  	} else {
  3187  		buf.Write([]byte("ZLIB"))
  3188  		var sizeBytes [8]byte
  3189  		binary.BigEndian.PutUint64(sizeBytes[:], uint64(total))
  3190  		buf.Write(sizeBytes[:])
  3191  	}
  3192  
  3193  	var relocbuf []byte // temporary buffer for applying relocations
  3194  
  3195  	// Using zlib.BestSpeed achieves very nearly the same
  3196  	// compression levels of zlib.DefaultCompression, but takes
  3197  	// substantially less time. This is important because DWARF
  3198  	// compression can be a significant fraction of link time.
  3199  	z, err := zlib.NewWriterLevel(&buf, zlib.BestSpeed)
  3200  	if err != nil {
  3201  		log.Fatalf("NewWriterLevel failed: %s", err)
  3202  	}
  3203  	st := ctxt.makeRelocSymState()
  3204  	for _, s := range syms {
  3205  		// Symbol data may be read-only. Apply relocations in a
  3206  		// temporary buffer, and immediately write it out.
  3207  		P := ldr.Data(s)
  3208  		relocs := ldr.Relocs(s)
  3209  		if relocs.Count() != 0 {
  3210  			relocbuf = append(relocbuf[:0], P...)
  3211  			P = relocbuf
  3212  			st.relocsym(s, P)
  3213  		}
  3214  		if _, err := z.Write(P); err != nil {
  3215  			log.Fatalf("compression failed: %s", err)
  3216  		}
  3217  		for i := ldr.SymSize(s) - int64(len(P)); i > 0; {
  3218  			b := zeros[:]
  3219  			if i < int64(len(b)) {
  3220  				b = b[:i]
  3221  			}
  3222  			n, err := z.Write(b)
  3223  			if err != nil {
  3224  				log.Fatalf("compression failed: %s", err)
  3225  			}
  3226  			i -= int64(n)
  3227  		}
  3228  	}
  3229  	if err := z.Close(); err != nil {
  3230  		log.Fatalf("compression failed: %s", err)
  3231  	}
  3232  	if int64(buf.Len()) >= total {
  3233  		// Compression didn't save any space.
  3234  		return nil
  3235  	}
  3236  	return buf.Bytes()
  3237  }
  3238  
  3239  // writeUleb128FixedLength writes out value v in LEB128 encoded
  3240  // format, ensuring that the space written takes up length bytes. When
  3241  // extra space is needed, we write initial bytes with just the
  3242  // continuation bit set. For example, if val is 1 and length is 3,
  3243  // we'll write 0x80 0x80 0x1 (first two bytes with zero val but
  3244  // continuation bit set). NB: this function adapted from a similar
  3245  // function in cmd/link/internal/wasm, they could be commoned up if
  3246  // needed.
  3247  func writeUleb128FixedLength(b []byte, v uint64, length int) error {
  3248  	for i := 0; i < length; i++ {
  3249  		c := uint8(v & 0x7f)
  3250  		v >>= 7
  3251  		if i < length-1 {
  3252  			c |= 0x80
  3253  		}
  3254  		b[i] = c
  3255  	}
  3256  	if v != 0 {
  3257  		return fmt.Errorf("writeUleb128FixedLength: length too small")
  3258  	}
  3259  	return nil
  3260  }
  3261  

View as plain text