github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/cmd/link/internal/ld/data.go (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c
     3  // https://bitbucket.org/inferno-os/inferno-os/src/default/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  	"cmd/internal/gcprog"
    36  	"cmd/internal/objabi"
    37  	"cmd/internal/sys"
    38  	"cmd/link/internal/sym"
    39  	"fmt"
    40  	"log"
    41  	"os"
    42  	"sort"
    43  	"strconv"
    44  	"strings"
    45  	"sync"
    46  )
    47  
    48  // isRuntimeDepPkg returns whether pkg is the runtime package or its dependency
    49  func isRuntimeDepPkg(pkg string) bool {
    50  	switch pkg {
    51  	case "runtime",
    52  		"sync/atomic": // runtime may call to sync/atomic, due to go:linkname
    53  		return true
    54  	}
    55  	return strings.HasPrefix(pkg, "runtime/internal/") && !strings.HasSuffix(pkg, "_test")
    56  }
    57  
    58  // Estimate the max size needed to hold any new trampolines created for this function. This
    59  // is used to determine when the section can be split if it becomes too large, to ensure that
    60  // the trampolines are in the same section as the function that uses them.
    61  func maxSizeTrampolinesPPC64(s *sym.Symbol, isTramp bool) uint64 {
    62  	// If Thearch.Trampoline is nil, then trampoline support is not available on this arch.
    63  	// A trampoline does not need any dependent trampolines.
    64  	if Thearch.Trampoline == nil || isTramp {
    65  		return 0
    66  	}
    67  
    68  	n := uint64(0)
    69  	for ri := range s.R {
    70  		r := &s.R[ri]
    71  		if r.Type.IsDirectJump() {
    72  			n++
    73  		}
    74  	}
    75  	// Trampolines in ppc64 are 4 instructions.
    76  	return n * 16
    77  }
    78  
    79  // detect too-far jumps in function s, and add trampolines if necessary
    80  // ARM, PPC64 & PPC64LE support trampoline insertion for internal and external linking
    81  // On PPC64 & PPC64LE the text sections might be split but will still insert trampolines
    82  // where necessary.
    83  func trampoline(ctxt *Link, s *sym.Symbol) {
    84  	if Thearch.Trampoline == nil {
    85  		return // no need or no support of trampolines on this arch
    86  	}
    87  
    88  	for ri := range s.R {
    89  		r := &s.R[ri]
    90  		if !r.Type.IsDirectJump() {
    91  			continue
    92  		}
    93  		if Symaddr(r.Sym) == 0 && r.Sym.Type != sym.SDYNIMPORT {
    94  			if r.Sym.File != s.File {
    95  				if !isRuntimeDepPkg(s.File) || !isRuntimeDepPkg(r.Sym.File) {
    96  					Errorf(s, "unresolved inter-package jump to %s(%s)", r.Sym, r.Sym.File)
    97  				}
    98  				// runtime and its dependent packages may call to each other.
    99  				// they are fine, as they will be laid down together.
   100  			}
   101  			continue
   102  		}
   103  
   104  		Thearch.Trampoline(ctxt, r, s)
   105  	}
   106  
   107  }
   108  
   109  // resolve relocations in s.
   110  func relocsym(ctxt *Link, s *sym.Symbol) {
   111  	for ri := int32(0); ri < int32(len(s.R)); ri++ {
   112  		r := &s.R[ri]
   113  		if r.Done {
   114  			// Relocation already processed by an earlier phase.
   115  			continue
   116  		}
   117  		r.Done = true
   118  		off := r.Off
   119  		siz := int32(r.Siz)
   120  		if off < 0 || off+siz > int32(len(s.P)) {
   121  			rname := ""
   122  			if r.Sym != nil {
   123  				rname = r.Sym.Name
   124  			}
   125  			Errorf(s, "invalid relocation %s: %d+%d not in [%d,%d)", rname, off, siz, 0, len(s.P))
   126  			continue
   127  		}
   128  
   129  		if r.Sym != nil && ((r.Sym.Type == 0 && !r.Sym.Attr.VisibilityHidden()) || r.Sym.Type == sym.SXREF) {
   130  			// When putting the runtime but not main into a shared library
   131  			// these symbols are undefined and that's OK.
   132  			if ctxt.BuildMode == BuildModeShared {
   133  				if r.Sym.Name == "main.main" || r.Sym.Name == "main.init" {
   134  					r.Sym.Type = sym.SDYNIMPORT
   135  				} else if strings.HasPrefix(r.Sym.Name, "go.info.") {
   136  					// Skip go.info symbols. They are only needed to communicate
   137  					// DWARF info between the compiler and linker.
   138  					continue
   139  				}
   140  			} else {
   141  				Errorf(s, "relocation target %s not defined", r.Sym.Name)
   142  				continue
   143  			}
   144  		}
   145  
   146  		if r.Type >= 256 {
   147  			continue
   148  		}
   149  		if r.Siz == 0 { // informational relocation - no work to do
   150  			continue
   151  		}
   152  		if r.Type == objabi.R_DWARFFILEREF {
   153  			// These should have been processed previously during
   154  			// line table writing.
   155  			Errorf(s, "orphan R_DWARFFILEREF reloc to %v", r.Sym.Name)
   156  			continue
   157  		}
   158  
   159  		// We need to be able to reference dynimport symbols when linking against
   160  		// shared libraries, and Solaris needs it always
   161  		if ctxt.HeadType != objabi.Hsolaris && r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT && !ctxt.DynlinkingGo() && !r.Sym.Attr.SubSymbol() {
   162  			if !(ctxt.Arch.Family == sys.PPC64 && ctxt.LinkMode == LinkExternal && r.Sym.Name == ".TOC.") {
   163  				Errorf(s, "unhandled relocation for %s (type %d (%s) rtype %d (%s))", r.Sym.Name, r.Sym.Type, r.Sym.Type, r.Type, sym.RelocName(ctxt.Arch, r.Type))
   164  			}
   165  		}
   166  		if r.Sym != nil && r.Sym.Type != sym.STLSBSS && r.Type != objabi.R_WEAKADDROFF && !r.Sym.Attr.Reachable() {
   167  			Errorf(s, "unreachable sym in relocation: %s", r.Sym.Name)
   168  		}
   169  
   170  		// TODO(mundaym): remove this special case - see issue 14218.
   171  		if ctxt.Arch.Family == sys.S390X {
   172  			switch r.Type {
   173  			case objabi.R_PCRELDBL:
   174  				r.Type = objabi.R_PCREL
   175  				r.Variant = sym.RV_390_DBL
   176  			case objabi.R_CALL:
   177  				r.Variant = sym.RV_390_DBL
   178  			}
   179  		}
   180  
   181  		var o int64
   182  		switch r.Type {
   183  		default:
   184  			switch siz {
   185  			default:
   186  				Errorf(s, "bad reloc size %#x for %s", uint32(siz), r.Sym.Name)
   187  			case 1:
   188  				o = int64(s.P[off])
   189  			case 2:
   190  				o = int64(ctxt.Arch.ByteOrder.Uint16(s.P[off:]))
   191  			case 4:
   192  				o = int64(ctxt.Arch.ByteOrder.Uint32(s.P[off:]))
   193  			case 8:
   194  				o = int64(ctxt.Arch.ByteOrder.Uint64(s.P[off:]))
   195  			}
   196  			if !Thearch.Archreloc(ctxt, r, s, &o) {
   197  				Errorf(s, "unknown reloc to %v: %d (%s)", r.Sym.Name, r.Type, sym.RelocName(ctxt.Arch, r.Type))
   198  			}
   199  		case objabi.R_TLS_LE:
   200  			isAndroidX86 := objabi.GOOS == "android" && (ctxt.Arch.InFamily(sys.AMD64, sys.I386))
   201  
   202  			if ctxt.LinkMode == LinkExternal && ctxt.IsELF && !isAndroidX86 {
   203  				r.Done = false
   204  				if r.Sym == nil {
   205  					r.Sym = ctxt.Tlsg
   206  				}
   207  				r.Xsym = r.Sym
   208  				r.Xadd = r.Add
   209  				o = 0
   210  				if ctxt.Arch.Family != sys.AMD64 {
   211  					o = r.Add
   212  				}
   213  				break
   214  			}
   215  
   216  			if ctxt.IsELF && ctxt.Arch.Family == sys.ARM {
   217  				// On ELF ARM, the thread pointer is 8 bytes before
   218  				// the start of the thread-local data block, so add 8
   219  				// to the actual TLS offset (r->sym->value).
   220  				// This 8 seems to be a fundamental constant of
   221  				// ELF on ARM (or maybe Glibc on ARM); it is not
   222  				// related to the fact that our own TLS storage happens
   223  				// to take up 8 bytes.
   224  				o = 8 + r.Sym.Value
   225  			} else if ctxt.IsELF || ctxt.HeadType == objabi.Hplan9 || ctxt.HeadType == objabi.Hdarwin || isAndroidX86 {
   226  				o = int64(ctxt.Tlsoffset) + r.Add
   227  			} else if ctxt.HeadType == objabi.Hwindows {
   228  				o = r.Add
   229  			} else {
   230  				log.Fatalf("unexpected R_TLS_LE relocation for %v", ctxt.HeadType)
   231  			}
   232  		case objabi.R_TLS_IE:
   233  			isAndroidX86 := objabi.GOOS == "android" && (ctxt.Arch.InFamily(sys.AMD64, sys.I386))
   234  
   235  			if ctxt.LinkMode == LinkExternal && ctxt.IsELF && !isAndroidX86 {
   236  				r.Done = false
   237  				if r.Sym == nil {
   238  					r.Sym = ctxt.Tlsg
   239  				}
   240  				r.Xsym = r.Sym
   241  				r.Xadd = r.Add
   242  				o = 0
   243  				if ctxt.Arch.Family != sys.AMD64 {
   244  					o = r.Add
   245  				}
   246  				break
   247  			}
   248  			if ctxt.BuildMode == BuildModePIE && ctxt.IsELF {
   249  				// We are linking the final executable, so we
   250  				// can optimize any TLS IE relocation to LE.
   251  				if Thearch.TLSIEtoLE == nil {
   252  					log.Fatalf("internal linking of TLS IE not supported on %v", ctxt.Arch.Family)
   253  				}
   254  				Thearch.TLSIEtoLE(s, int(off), int(r.Siz))
   255  				o = int64(ctxt.Tlsoffset)
   256  				// TODO: o += r.Add when ctxt.Arch.Family != sys.AMD64?
   257  				// Why do we treat r.Add differently on AMD64?
   258  				// Is the external linker using Xadd at all?
   259  			} else {
   260  				log.Fatalf("cannot handle R_TLS_IE (sym %s) when linking internally", s.Name)
   261  			}
   262  		case objabi.R_ADDR:
   263  			if ctxt.LinkMode == LinkExternal && r.Sym.Type != sym.SCONST {
   264  				r.Done = false
   265  
   266  				// set up addend for eventual relocation via outer symbol.
   267  				rs := r.Sym
   268  
   269  				r.Xadd = r.Add
   270  				for rs.Outer != nil {
   271  					r.Xadd += Symaddr(rs) - Symaddr(rs.Outer)
   272  					rs = rs.Outer
   273  				}
   274  
   275  				if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
   276  					Errorf(s, "missing section for relocation target %s", rs.Name)
   277  				}
   278  				r.Xsym = rs
   279  
   280  				o = r.Xadd
   281  				if ctxt.IsELF {
   282  					if ctxt.Arch.Family == sys.AMD64 {
   283  						o = 0
   284  					}
   285  				} else if ctxt.HeadType == objabi.Hdarwin {
   286  					// ld64 for arm64 has a bug where if the address pointed to by o exists in the
   287  					// symbol table (dynid >= 0), or is inside a symbol that exists in the symbol
   288  					// table, then it will add o twice into the relocated value.
   289  					// The workaround is that on arm64 don't ever add symaddr to o and always use
   290  					// extern relocation by requiring rs->dynid >= 0.
   291  					if rs.Type != sym.SHOSTOBJ {
   292  						if ctxt.Arch.Family == sys.ARM64 && rs.Dynid < 0 {
   293  							Errorf(s, "R_ADDR reloc to %s+%d is not supported on darwin/arm64", rs.Name, o)
   294  						}
   295  						if ctxt.Arch.Family != sys.ARM64 {
   296  							o += Symaddr(rs)
   297  						}
   298  					}
   299  				} else if ctxt.HeadType == objabi.Hwindows {
   300  					// nothing to do
   301  				} else {
   302  					Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, ctxt.HeadType)
   303  				}
   304  
   305  				break
   306  			}
   307  
   308  			o = Symaddr(r.Sym) + r.Add
   309  
   310  			// On amd64, 4-byte offsets will be sign-extended, so it is impossible to
   311  			// access more than 2GB of static data; fail at link time is better than
   312  			// fail at runtime. See https://golang.org/issue/7980.
   313  			// Instead of special casing only amd64, we treat this as an error on all
   314  			// 64-bit architectures so as to be future-proof.
   315  			if int32(o) < 0 && ctxt.Arch.PtrSize > 4 && siz == 4 {
   316  				Errorf(s, "non-pc-relative relocation address for %s is too big: %#x (%#x + %#x)", r.Sym.Name, uint64(o), Symaddr(r.Sym), r.Add)
   317  				errorexit()
   318  			}
   319  		case objabi.R_DWARFSECREF:
   320  			if r.Sym.Sect == nil {
   321  				Errorf(s, "missing DWARF section for relocation target %s", r.Sym.Name)
   322  			}
   323  
   324  			if ctxt.LinkMode == LinkExternal {
   325  				r.Done = false
   326  
   327  				// On most platforms, the external linker needs to adjust DWARF references
   328  				// as it combines DWARF sections. However, on Darwin, dsymutil does the
   329  				// DWARF linking, and it understands how to follow section offsets.
   330  				// Leaving in the relocation records confuses it (see
   331  				// https://golang.org/issue/22068) so drop them for Darwin.
   332  				if ctxt.HeadType == objabi.Hdarwin {
   333  					r.Done = true
   334  				}
   335  
   336  				// PE code emits IMAGE_REL_I386_SECREL and IMAGE_REL_AMD64_SECREL
   337  				// for R_DWARFSECREF relocations, while R_ADDR is replaced with
   338  				// IMAGE_REL_I386_DIR32, IMAGE_REL_AMD64_ADDR64 and IMAGE_REL_AMD64_ADDR32.
   339  				// Do not replace R_DWARFSECREF with R_ADDR for windows -
   340  				// let PE code emit correct relocations.
   341  				if ctxt.HeadType != objabi.Hwindows {
   342  					r.Type = objabi.R_ADDR
   343  				}
   344  
   345  				r.Xsym = ctxt.Syms.ROLookup(r.Sym.Sect.Name, 0)
   346  				r.Xadd = r.Add + Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr)
   347  
   348  				o = r.Xadd
   349  				if ctxt.IsELF && ctxt.Arch.Family == sys.AMD64 {
   350  					o = 0
   351  				}
   352  				break
   353  			}
   354  			o = Symaddr(r.Sym) + r.Add - int64(r.Sym.Sect.Vaddr)
   355  		case objabi.R_WEAKADDROFF:
   356  			if !r.Sym.Attr.Reachable() {
   357  				continue
   358  			}
   359  			fallthrough
   360  		case objabi.R_ADDROFF:
   361  			// The method offset tables using this relocation expect the offset to be relative
   362  			// to the start of the first text section, even if there are multiple.
   363  
   364  			if r.Sym.Sect.Name == ".text" {
   365  				o = Symaddr(r.Sym) - int64(Segtext.Sections[0].Vaddr) + r.Add
   366  			} else {
   367  				o = Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr) + r.Add
   368  			}
   369  
   370  		case objabi.R_ADDRCUOFF:
   371  			// debug_range and debug_loc elements use this relocation type to get an
   372  			// offset from the start of the compile unit.
   373  			o = Symaddr(r.Sym) + r.Add - Symaddr(r.Sym.Lib.Textp[0])
   374  
   375  			// r->sym can be null when CALL $(constant) is transformed from absolute PC to relative PC call.
   376  		case objabi.R_GOTPCREL:
   377  			if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin && r.Sym != nil && r.Sym.Type != sym.SCONST {
   378  				r.Done = false
   379  				r.Xadd = r.Add
   380  				r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk
   381  				r.Xsym = r.Sym
   382  
   383  				o = r.Xadd
   384  				o += int64(r.Siz)
   385  				break
   386  			}
   387  			fallthrough
   388  		case objabi.R_CALL, objabi.R_PCREL:
   389  			if ctxt.LinkMode == LinkExternal && r.Sym != nil && r.Sym.Type != sym.SCONST && (r.Sym.Sect != s.Sect || r.Type == objabi.R_GOTPCREL) {
   390  				r.Done = false
   391  
   392  				// set up addend for eventual relocation via outer symbol.
   393  				rs := r.Sym
   394  
   395  				r.Xadd = r.Add
   396  				for rs.Outer != nil {
   397  					r.Xadd += Symaddr(rs) - Symaddr(rs.Outer)
   398  					rs = rs.Outer
   399  				}
   400  
   401  				r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk
   402  				if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
   403  					Errorf(s, "missing section for relocation target %s", rs.Name)
   404  				}
   405  				r.Xsym = rs
   406  
   407  				o = r.Xadd
   408  				if ctxt.IsELF {
   409  					if ctxt.Arch.Family == sys.AMD64 {
   410  						o = 0
   411  					}
   412  				} else if ctxt.HeadType == objabi.Hdarwin {
   413  					if r.Type == objabi.R_CALL {
   414  						if rs.Type != sym.SHOSTOBJ {
   415  							o += int64(uint64(Symaddr(rs)) - rs.Sect.Vaddr)
   416  						}
   417  						o -= int64(r.Off) // relative to section offset, not symbol
   418  					} else if ctxt.Arch.Family == sys.ARM {
   419  						// see ../arm/asm.go:/machoreloc1
   420  						o += Symaddr(rs) - int64(s.Value) - int64(r.Off)
   421  					} else {
   422  						o += int64(r.Siz)
   423  					}
   424  				} else if ctxt.HeadType == objabi.Hwindows && ctxt.Arch.Family == sys.AMD64 { // only amd64 needs PCREL
   425  					// PE/COFF's PC32 relocation uses the address after the relocated
   426  					// bytes as the base. Compensate by skewing the addend.
   427  					o += int64(r.Siz)
   428  				} else {
   429  					Errorf(s, "unhandled pcrel relocation to %s on %v", rs.Name, ctxt.HeadType)
   430  				}
   431  
   432  				break
   433  			}
   434  
   435  			o = 0
   436  			if r.Sym != nil {
   437  				o += Symaddr(r.Sym)
   438  			}
   439  
   440  			o += r.Add - (s.Value + int64(r.Off) + int64(r.Siz))
   441  		case objabi.R_SIZE:
   442  			o = r.Sym.Size + r.Add
   443  		}
   444  
   445  		if r.Variant != sym.RV_NONE {
   446  			o = Thearch.Archrelocvariant(ctxt, r, s, o)
   447  		}
   448  
   449  		if false {
   450  			nam := "<nil>"
   451  			if r.Sym != nil {
   452  				nam = r.Sym.Name
   453  			}
   454  			fmt.Printf("relocate %s %#x (%#x+%#x, size %d) => %s %#x +%#x [type %d (%s)/%d, %x]\n", s.Name, s.Value+int64(off), s.Value, r.Off, r.Siz, nam, Symaddr(r.Sym), r.Add, r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Variant, o)
   455  		}
   456  		switch siz {
   457  		default:
   458  			Errorf(s, "bad reloc size %#x for %s", uint32(siz), r.Sym.Name)
   459  			fallthrough
   460  
   461  			// TODO(rsc): Remove.
   462  		case 1:
   463  			s.P[off] = byte(int8(o))
   464  		case 2:
   465  			if o != int64(int16(o)) {
   466  				Errorf(s, "relocation address for %s is too big: %#x", r.Sym.Name, o)
   467  			}
   468  			i16 := int16(o)
   469  			ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16))
   470  		case 4:
   471  			if r.Type == objabi.R_PCREL || r.Type == objabi.R_CALL {
   472  				if o != int64(int32(o)) {
   473  					Errorf(s, "pc-relative relocation address for %s is too big: %#x", r.Sym.Name, o)
   474  				}
   475  			} else {
   476  				if o != int64(int32(o)) && o != int64(uint32(o)) {
   477  					Errorf(s, "non-pc-relative relocation address for %s is too big: %#x", r.Sym.Name, uint64(o))
   478  				}
   479  			}
   480  
   481  			fl := int32(o)
   482  			ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl))
   483  		case 8:
   484  			ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o))
   485  		}
   486  	}
   487  }
   488  
   489  func (ctxt *Link) reloc() {
   490  	if ctxt.Debugvlog != 0 {
   491  		ctxt.Logf("%5.2f reloc\n", Cputime())
   492  	}
   493  
   494  	for _, s := range ctxt.Textp {
   495  		relocsym(ctxt, s)
   496  	}
   497  	for _, s := range datap {
   498  		relocsym(ctxt, s)
   499  	}
   500  	for _, s := range dwarfp {
   501  		relocsym(ctxt, s)
   502  	}
   503  }
   504  
   505  func windynrelocsym(ctxt *Link, s *sym.Symbol) {
   506  	rel := ctxt.Syms.Lookup(".rel", 0)
   507  	if s == rel {
   508  		return
   509  	}
   510  	for ri := 0; ri < len(s.R); ri++ {
   511  		r := &s.R[ri]
   512  		targ := r.Sym
   513  		if targ == nil {
   514  			continue
   515  		}
   516  		if !targ.Attr.Reachable() {
   517  			if r.Type == objabi.R_WEAKADDROFF {
   518  				continue
   519  			}
   520  			Errorf(s, "dynamic relocation to unreachable symbol %s", targ.Name)
   521  		}
   522  		if r.Sym.Plt == -2 && r.Sym.Got != -2 { // make dynimport JMP table for PE object files.
   523  			targ.Plt = int32(rel.Size)
   524  			r.Sym = rel
   525  			r.Add = int64(targ.Plt)
   526  
   527  			// jmp *addr
   528  			if ctxt.Arch.Family == sys.I386 {
   529  				rel.AddUint8(0xff)
   530  				rel.AddUint8(0x25)
   531  				rel.AddAddr(ctxt.Arch, targ)
   532  				rel.AddUint8(0x90)
   533  				rel.AddUint8(0x90)
   534  			} else {
   535  				rel.AddUint8(0xff)
   536  				rel.AddUint8(0x24)
   537  				rel.AddUint8(0x25)
   538  				rel.AddAddrPlus4(targ, 0)
   539  				rel.AddUint8(0x90)
   540  			}
   541  		} else if r.Sym.Plt >= 0 {
   542  			r.Sym = rel
   543  			r.Add = int64(targ.Plt)
   544  		}
   545  	}
   546  }
   547  
   548  func dynrelocsym(ctxt *Link, s *sym.Symbol) {
   549  	if ctxt.HeadType == objabi.Hwindows {
   550  		if ctxt.LinkMode == LinkInternal {
   551  			windynrelocsym(ctxt, s)
   552  		}
   553  		return
   554  	}
   555  
   556  	for ri := 0; ri < len(s.R); ri++ {
   557  		r := &s.R[ri]
   558  		if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal {
   559  			// It's expected that some relocations will be done
   560  			// later by relocsym (R_TLS_LE, R_ADDROFF), so
   561  			// don't worry if Adddynrel returns false.
   562  			Thearch.Adddynrel(ctxt, s, r)
   563  			continue
   564  		}
   565  		if r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT || r.Type >= 256 {
   566  			if r.Sym != nil && !r.Sym.Attr.Reachable() {
   567  				Errorf(s, "dynamic relocation to unreachable symbol %s", r.Sym.Name)
   568  			}
   569  			if !Thearch.Adddynrel(ctxt, s, r) {
   570  				Errorf(s, "unsupported dynamic relocation for symbol %s (type=%d (%s) stype=%d (%s))", r.Sym.Name, r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Sym.Type, r.Sym.Type)
   571  			}
   572  		}
   573  	}
   574  }
   575  
   576  func dynreloc(ctxt *Link, data *[sym.SXREF][]*sym.Symbol) {
   577  	// -d suppresses dynamic loader format, so we may as well not
   578  	// compute these sections or mark their symbols as reachable.
   579  	if *FlagD && ctxt.HeadType != objabi.Hwindows {
   580  		return
   581  	}
   582  	if ctxt.Debugvlog != 0 {
   583  		ctxt.Logf("%5.2f dynreloc\n", Cputime())
   584  	}
   585  
   586  	for _, s := range ctxt.Textp {
   587  		dynrelocsym(ctxt, s)
   588  	}
   589  	for _, syms := range data {
   590  		for _, s := range syms {
   591  			dynrelocsym(ctxt, s)
   592  		}
   593  	}
   594  	if ctxt.IsELF {
   595  		elfdynhash(ctxt)
   596  	}
   597  }
   598  
   599  func Codeblk(ctxt *Link, addr int64, size int64) {
   600  	CodeblkPad(ctxt, addr, size, zeros[:])
   601  }
   602  func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) {
   603  	if *flagA {
   604  		ctxt.Logf("codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, ctxt.Out.Offset())
   605  	}
   606  
   607  	blk(ctxt, ctxt.Textp, addr, size, pad)
   608  
   609  	/* again for printing */
   610  	if !*flagA {
   611  		return
   612  	}
   613  
   614  	syms := ctxt.Textp
   615  	for i, s := range syms {
   616  		if !s.Attr.Reachable() {
   617  			continue
   618  		}
   619  		if s.Value >= addr {
   620  			syms = syms[i:]
   621  			break
   622  		}
   623  	}
   624  
   625  	eaddr := addr + size
   626  	var q []byte
   627  	for _, s := range syms {
   628  		if !s.Attr.Reachable() {
   629  			continue
   630  		}
   631  		if s.Value >= eaddr {
   632  			break
   633  		}
   634  
   635  		if addr < s.Value {
   636  			ctxt.Logf("%-20s %.8x|", "_", uint64(addr))
   637  			for ; addr < s.Value; addr++ {
   638  				ctxt.Logf(" %.2x", 0)
   639  			}
   640  			ctxt.Logf("\n")
   641  		}
   642  
   643  		ctxt.Logf("%.6x\t%-20s\n", uint64(addr), s.Name)
   644  		q = s.P
   645  
   646  		for len(q) >= 16 {
   647  			ctxt.Logf("%.6x\t% x\n", uint64(addr), q[:16])
   648  			addr += 16
   649  			q = q[16:]
   650  		}
   651  
   652  		if len(q) > 0 {
   653  			ctxt.Logf("%.6x\t% x\n", uint64(addr), q)
   654  			addr += int64(len(q))
   655  		}
   656  	}
   657  
   658  	if addr < eaddr {
   659  		ctxt.Logf("%-20s %.8x|", "_", uint64(addr))
   660  		for ; addr < eaddr; addr++ {
   661  			ctxt.Logf(" %.2x", 0)
   662  		}
   663  	}
   664  }
   665  
   666  func blk(ctxt *Link, syms []*sym.Symbol, addr, size int64, pad []byte) {
   667  	for i, s := range syms {
   668  		if !s.Attr.SubSymbol() && s.Value >= addr {
   669  			syms = syms[i:]
   670  			break
   671  		}
   672  	}
   673  
   674  	eaddr := addr + size
   675  	for _, s := range syms {
   676  		if s.Attr.SubSymbol() {
   677  			continue
   678  		}
   679  		if s.Value >= eaddr {
   680  			break
   681  		}
   682  		if s.Value < addr {
   683  			Errorf(s, "phase error: addr=%#x but sym=%#x type=%d", addr, s.Value, s.Type)
   684  			errorexit()
   685  		}
   686  		if addr < s.Value {
   687  			ctxt.Out.WriteStringPad("", int(s.Value-addr), pad)
   688  			addr = s.Value
   689  		}
   690  		ctxt.Out.Write(s.P)
   691  		addr += int64(len(s.P))
   692  		if addr < s.Value+s.Size {
   693  			ctxt.Out.WriteStringPad("", int(s.Value+s.Size-addr), pad)
   694  			addr = s.Value + s.Size
   695  		}
   696  		if addr != s.Value+s.Size {
   697  			Errorf(s, "phase error: addr=%#x value+size=%#x", addr, s.Value+s.Size)
   698  			errorexit()
   699  		}
   700  		if s.Value+s.Size >= eaddr {
   701  			break
   702  		}
   703  	}
   704  
   705  	if addr < eaddr {
   706  		ctxt.Out.WriteStringPad("", int(eaddr-addr), pad)
   707  	}
   708  	ctxt.Out.Flush()
   709  }
   710  
   711  func Datblk(ctxt *Link, addr int64, size int64) {
   712  	if *flagA {
   713  		ctxt.Logf("datblk [%#x,%#x) at offset %#x\n", addr, addr+size, ctxt.Out.Offset())
   714  	}
   715  
   716  	blk(ctxt, datap, addr, size, zeros[:])
   717  
   718  	/* again for printing */
   719  	if !*flagA {
   720  		return
   721  	}
   722  
   723  	syms := datap
   724  	for i, sym := range syms {
   725  		if sym.Value >= addr {
   726  			syms = syms[i:]
   727  			break
   728  		}
   729  	}
   730  
   731  	eaddr := addr + size
   732  	for _, sym := range syms {
   733  		if sym.Value >= eaddr {
   734  			break
   735  		}
   736  		if addr < sym.Value {
   737  			ctxt.Logf("\t%.8x| 00 ...\n", uint64(addr))
   738  			addr = sym.Value
   739  		}
   740  
   741  		ctxt.Logf("%s\n\t%.8x|", sym.Name, uint64(addr))
   742  		for i, b := range sym.P {
   743  			if i > 0 && i%16 == 0 {
   744  				ctxt.Logf("\n\t%.8x|", uint64(addr)+uint64(i))
   745  			}
   746  			ctxt.Logf(" %.2x", b)
   747  		}
   748  
   749  		addr += int64(len(sym.P))
   750  		for ; addr < sym.Value+sym.Size; addr++ {
   751  			ctxt.Logf(" %.2x", 0)
   752  		}
   753  		ctxt.Logf("\n")
   754  
   755  		if ctxt.LinkMode != LinkExternal {
   756  			continue
   757  		}
   758  		for _, r := range sym.R {
   759  			rsname := ""
   760  			if r.Sym != nil {
   761  				rsname = r.Sym.Name
   762  			}
   763  			typ := "?"
   764  			switch r.Type {
   765  			case objabi.R_ADDR:
   766  				typ = "addr"
   767  			case objabi.R_PCREL:
   768  				typ = "pcrel"
   769  			case objabi.R_CALL:
   770  				typ = "call"
   771  			}
   772  			ctxt.Logf("\treloc %.8x/%d %s %s+%#x [%#x]\n", uint(sym.Value+int64(r.Off)), r.Siz, typ, rsname, r.Add, r.Sym.Value+r.Add)
   773  		}
   774  	}
   775  
   776  	if addr < eaddr {
   777  		ctxt.Logf("\t%.8x| 00 ...\n", uint(addr))
   778  	}
   779  	ctxt.Logf("\t%.8x|\n", uint(eaddr))
   780  }
   781  
   782  func Dwarfblk(ctxt *Link, addr int64, size int64) {
   783  	if *flagA {
   784  		ctxt.Logf("dwarfblk [%#x,%#x) at offset %#x\n", addr, addr+size, ctxt.Out.Offset())
   785  	}
   786  
   787  	blk(ctxt, dwarfp, addr, size, zeros[:])
   788  }
   789  
   790  var zeros [512]byte
   791  
   792  var strdata []*sym.Symbol
   793  
   794  func addstrdata1(ctxt *Link, arg string) {
   795  	eq := strings.Index(arg, "=")
   796  	dot := strings.LastIndex(arg[:eq+1], ".")
   797  	if eq < 0 || dot < 0 {
   798  		Exitf("-X flag requires argument of the form importpath.name=value")
   799  	}
   800  	pkg := arg[:dot]
   801  	if ctxt.BuildMode == BuildModePlugin && pkg == "main" {
   802  		pkg = *flagPluginPath
   803  	}
   804  	pkg = objabi.PathToPrefix(pkg)
   805  	addstrdata(ctxt, pkg+arg[dot:eq], arg[eq+1:])
   806  }
   807  
   808  func addstrdata(ctxt *Link, name string, value string) {
   809  	p := fmt.Sprintf("%s.str", name)
   810  	sp := ctxt.Syms.Lookup(p, 0)
   811  
   812  	Addstring(sp, value)
   813  	sp.Type = sym.SRODATA
   814  
   815  	s := ctxt.Syms.Lookup(name, 0)
   816  	s.Size = 0
   817  	s.Attr |= sym.AttrDuplicateOK
   818  	reachable := s.Attr.Reachable()
   819  	s.AddAddr(ctxt.Arch, sp)
   820  	s.AddUint(ctxt.Arch, uint64(len(value)))
   821  
   822  	// addstring, addaddr, etc., mark the symbols as reachable.
   823  	// In this case that is not necessarily true, so stick to what
   824  	// we know before entering this function.
   825  	s.Attr.Set(sym.AttrReachable, reachable)
   826  
   827  	strdata = append(strdata, s)
   828  
   829  	sp.Attr.Set(sym.AttrReachable, reachable)
   830  }
   831  
   832  func (ctxt *Link) checkstrdata() {
   833  	for _, s := range strdata {
   834  		if s.Type == sym.STEXT {
   835  			Errorf(s, "cannot use -X with text symbol")
   836  		} else if s.Gotype != nil && s.Gotype.Name != "type.string" {
   837  			Errorf(s, "cannot use -X with non-string symbol")
   838  		}
   839  	}
   840  }
   841  
   842  func Addstring(s *sym.Symbol, str string) int64 {
   843  	if s.Type == 0 {
   844  		s.Type = sym.SNOPTRDATA
   845  	}
   846  	s.Attr |= sym.AttrReachable
   847  	r := s.Size
   848  	if s.Name == ".shstrtab" {
   849  		elfsetstring(s, str, int(r))
   850  	}
   851  	s.P = append(s.P, str...)
   852  	s.P = append(s.P, 0)
   853  	s.Size = int64(len(s.P))
   854  	return r
   855  }
   856  
   857  // addgostring adds str, as a Go string value, to s. symname is the name of the
   858  // symbol used to define the string data and must be unique per linked object.
   859  func addgostring(ctxt *Link, s *sym.Symbol, symname, str string) {
   860  	sdata := ctxt.Syms.Lookup(symname, 0)
   861  	if sdata.Type != sym.Sxxx {
   862  		Errorf(s, "duplicate symname in addgostring: %s", symname)
   863  	}
   864  	sdata.Attr |= sym.AttrReachable
   865  	sdata.Attr |= sym.AttrLocal
   866  	sdata.Type = sym.SRODATA
   867  	sdata.Size = int64(len(str))
   868  	sdata.P = []byte(str)
   869  	s.AddAddr(ctxt.Arch, sdata)
   870  	s.AddUint(ctxt.Arch, uint64(len(str)))
   871  }
   872  
   873  func addinitarrdata(ctxt *Link, s *sym.Symbol) {
   874  	p := s.Name + ".ptr"
   875  	sp := ctxt.Syms.Lookup(p, 0)
   876  	sp.Type = sym.SINITARR
   877  	sp.Size = 0
   878  	sp.Attr |= sym.AttrDuplicateOK
   879  	sp.AddAddr(ctxt.Arch, s)
   880  }
   881  
   882  func dosymtype(ctxt *Link) {
   883  	switch ctxt.BuildMode {
   884  	case BuildModeCArchive, BuildModeCShared:
   885  		for _, s := range ctxt.Syms.Allsym {
   886  			// Create a new entry in the .init_array section that points to the
   887  			// library initializer function.
   888  			if s.Name == *flagEntrySymbol {
   889  				addinitarrdata(ctxt, s)
   890  			}
   891  		}
   892  	}
   893  }
   894  
   895  // symalign returns the required alignment for the given symbol s.
   896  func symalign(s *sym.Symbol) int32 {
   897  	min := int32(Thearch.Minalign)
   898  	if s.Align >= min {
   899  		return s.Align
   900  	} else if s.Align != 0 {
   901  		return min
   902  	}
   903  	if strings.HasPrefix(s.Name, "go.string.") || strings.HasPrefix(s.Name, "type..namedata.") {
   904  		// String data is just bytes.
   905  		// If we align it, we waste a lot of space to padding.
   906  		return min
   907  	}
   908  	align := int32(Thearch.Maxalign)
   909  	for int64(align) > s.Size && align > min {
   910  		align >>= 1
   911  	}
   912  	return align
   913  }
   914  
   915  func aligndatsize(datsize int64, s *sym.Symbol) int64 {
   916  	return Rnd(datsize, int64(symalign(s)))
   917  }
   918  
   919  const debugGCProg = false
   920  
   921  type GCProg struct {
   922  	ctxt *Link
   923  	sym  *sym.Symbol
   924  	w    gcprog.Writer
   925  }
   926  
   927  func (p *GCProg) Init(ctxt *Link, name string) {
   928  	p.ctxt = ctxt
   929  	p.sym = ctxt.Syms.Lookup(name, 0)
   930  	p.w.Init(p.writeByte(ctxt))
   931  	if debugGCProg {
   932  		fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name)
   933  		p.w.Debug(os.Stderr)
   934  	}
   935  }
   936  
   937  func (p *GCProg) writeByte(ctxt *Link) func(x byte) {
   938  	return func(x byte) {
   939  		p.sym.AddUint8(x)
   940  	}
   941  }
   942  
   943  func (p *GCProg) End(size int64) {
   944  	p.w.ZeroUntil(size / int64(p.ctxt.Arch.PtrSize))
   945  	p.w.End()
   946  	if debugGCProg {
   947  		fmt.Fprintf(os.Stderr, "ld: end GCProg\n")
   948  	}
   949  }
   950  
   951  func (p *GCProg) AddSym(s *sym.Symbol) {
   952  	typ := s.Gotype
   953  	// Things without pointers should be in sym.SNOPTRDATA or sym.SNOPTRBSS;
   954  	// everything we see should have pointers and should therefore have a type.
   955  	if typ == nil {
   956  		switch s.Name {
   957  		case "runtime.data", "runtime.edata", "runtime.bss", "runtime.ebss":
   958  			// Ignore special symbols that are sometimes laid out
   959  			// as real symbols. See comment about dyld on darwin in
   960  			// the address function.
   961  			return
   962  		}
   963  		Errorf(s, "missing Go type information for global symbol: size %d", s.Size)
   964  		return
   965  	}
   966  
   967  	ptrsize := int64(p.ctxt.Arch.PtrSize)
   968  	nptr := decodetypePtrdata(p.ctxt.Arch, typ) / ptrsize
   969  
   970  	if debugGCProg {
   971  		fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", s.Name, s.Value, s.Value/ptrsize, nptr)
   972  	}
   973  
   974  	if decodetypeUsegcprog(p.ctxt.Arch, typ) == 0 {
   975  		// Copy pointers from mask into program.
   976  		mask := decodetypeGcmask(p.ctxt, typ)
   977  		for i := int64(0); i < nptr; i++ {
   978  			if (mask[i/8]>>uint(i%8))&1 != 0 {
   979  				p.w.Ptr(s.Value/ptrsize + i)
   980  			}
   981  		}
   982  		return
   983  	}
   984  
   985  	// Copy program.
   986  	prog := decodetypeGcprog(p.ctxt, typ)
   987  	p.w.ZeroUntil(s.Value / ptrsize)
   988  	p.w.Append(prog[4:], nptr)
   989  }
   990  
   991  // dataSortKey is used to sort a slice of data symbol *sym.Symbol pointers.
   992  // The sort keys are kept inline to improve cache behavior while sorting.
   993  type dataSortKey struct {
   994  	size int64
   995  	name string
   996  	sym  *sym.Symbol
   997  }
   998  
   999  type bySizeAndName []dataSortKey
  1000  
  1001  func (d bySizeAndName) Len() int      { return len(d) }
  1002  func (d bySizeAndName) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
  1003  func (d bySizeAndName) Less(i, j int) bool {
  1004  	s1, s2 := d[i], d[j]
  1005  	if s1.size != s2.size {
  1006  		return s1.size < s2.size
  1007  	}
  1008  	return s1.name < s2.name
  1009  }
  1010  
  1011  // cutoff is the maximum data section size permitted by the linker
  1012  // (see issue #9862).
  1013  const cutoff = 2e9 // 2 GB (or so; looks better in errors than 2^31)
  1014  
  1015  func checkdatsize(ctxt *Link, datsize int64, symn sym.SymKind) {
  1016  	if datsize > cutoff {
  1017  		Errorf(nil, "too much data in section %v (over %v bytes)", symn, cutoff)
  1018  	}
  1019  }
  1020  
  1021  // datap is a collection of reachable data symbols in address order.
  1022  // Generated by dodata.
  1023  var datap []*sym.Symbol
  1024  
  1025  func (ctxt *Link) dodata() {
  1026  	if ctxt.Debugvlog != 0 {
  1027  		ctxt.Logf("%5.2f dodata\n", Cputime())
  1028  	}
  1029  
  1030  	if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin {
  1031  		// The values in moduledata are filled out by relocations
  1032  		// pointing to the addresses of these special symbols.
  1033  		// Typically these symbols have no size and are not laid
  1034  		// out with their matching section.
  1035  		//
  1036  		// However on darwin, dyld will find the special symbol
  1037  		// in the first loaded module, even though it is local.
  1038  		//
  1039  		// (An hypothesis, formed without looking in the dyld sources:
  1040  		// these special symbols have no size, so their address
  1041  		// matches a real symbol. The dynamic linker assumes we
  1042  		// want the normal symbol with the same address and finds
  1043  		// it in the other module.)
  1044  		//
  1045  		// To work around this we lay out the symbls whose
  1046  		// addresses are vital for multi-module programs to work
  1047  		// as normal symbols, and give them a little size.
  1048  		bss := ctxt.Syms.Lookup("runtime.bss", 0)
  1049  		bss.Size = 8
  1050  		bss.Attr.Set(sym.AttrSpecial, false)
  1051  
  1052  		ctxt.Syms.Lookup("runtime.ebss", 0).Attr.Set(sym.AttrSpecial, false)
  1053  
  1054  		data := ctxt.Syms.Lookup("runtime.data", 0)
  1055  		data.Size = 8
  1056  		data.Attr.Set(sym.AttrSpecial, false)
  1057  
  1058  		ctxt.Syms.Lookup("runtime.edata", 0).Attr.Set(sym.AttrSpecial, false)
  1059  
  1060  		types := ctxt.Syms.Lookup("runtime.types", 0)
  1061  		types.Type = sym.STYPE
  1062  		types.Size = 8
  1063  		types.Attr.Set(sym.AttrSpecial, false)
  1064  
  1065  		etypes := ctxt.Syms.Lookup("runtime.etypes", 0)
  1066  		etypes.Type = sym.SFUNCTAB
  1067  		etypes.Attr.Set(sym.AttrSpecial, false)
  1068  	}
  1069  
  1070  	// Collect data symbols by type into data.
  1071  	var data [sym.SXREF][]*sym.Symbol
  1072  	for _, s := range ctxt.Syms.Allsym {
  1073  		if !s.Attr.Reachable() || s.Attr.Special() || s.Attr.SubSymbol() {
  1074  			continue
  1075  		}
  1076  		if s.Type <= sym.STEXT || s.Type >= sym.SXREF {
  1077  			continue
  1078  		}
  1079  		data[s.Type] = append(data[s.Type], s)
  1080  	}
  1081  
  1082  	// Now that we have the data symbols, but before we start
  1083  	// to assign addresses, record all the necessary
  1084  	// dynamic relocations. These will grow the relocation
  1085  	// symbol, which is itself data.
  1086  	//
  1087  	// On darwin, we need the symbol table numbers for dynreloc.
  1088  	if ctxt.HeadType == objabi.Hdarwin {
  1089  		machosymorder(ctxt)
  1090  	}
  1091  	dynreloc(ctxt, &data)
  1092  
  1093  	if ctxt.UseRelro() {
  1094  		// "read only" data with relocations needs to go in its own section
  1095  		// when building a shared library. We do this by boosting objects of
  1096  		// type SXXX with relocations to type SXXXRELRO.
  1097  		for _, symnro := range sym.ReadOnly {
  1098  			symnrelro := sym.RelROMap[symnro]
  1099  
  1100  			ro := []*sym.Symbol{}
  1101  			relro := data[symnrelro]
  1102  
  1103  			for _, s := range data[symnro] {
  1104  				isRelro := len(s.R) > 0
  1105  				switch s.Type {
  1106  				case sym.STYPE, sym.STYPERELRO, sym.SGOFUNCRELRO:
  1107  					// Symbols are not sorted yet, so it is possible
  1108  					// that an Outer symbol has been changed to a
  1109  					// relro Type before it reaches here.
  1110  					isRelro = true
  1111  				}
  1112  				if isRelro {
  1113  					s.Type = symnrelro
  1114  					if s.Outer != nil {
  1115  						s.Outer.Type = s.Type
  1116  					}
  1117  					relro = append(relro, s)
  1118  				} else {
  1119  					ro = append(ro, s)
  1120  				}
  1121  			}
  1122  
  1123  			// Check that we haven't made two symbols with the same .Outer into
  1124  			// different types (because references two symbols with non-nil Outer
  1125  			// become references to the outer symbol + offset it's vital that the
  1126  			// symbol and the outer end up in the same section).
  1127  			for _, s := range relro {
  1128  				if s.Outer != nil && s.Outer.Type != s.Type {
  1129  					Errorf(s, "inconsistent types for symbol and its Outer %s (%v != %v)",
  1130  						s.Outer.Name, s.Type, s.Outer.Type)
  1131  				}
  1132  			}
  1133  
  1134  			data[symnro] = ro
  1135  			data[symnrelro] = relro
  1136  		}
  1137  	}
  1138  
  1139  	// Sort symbols.
  1140  	var dataMaxAlign [sym.SXREF]int32
  1141  	var wg sync.WaitGroup
  1142  	for symn := range data {
  1143  		symn := sym.SymKind(symn)
  1144  		wg.Add(1)
  1145  		go func() {
  1146  			data[symn], dataMaxAlign[symn] = dodataSect(ctxt, symn, data[symn])
  1147  			wg.Done()
  1148  		}()
  1149  	}
  1150  	wg.Wait()
  1151  
  1152  	// Allocate sections.
  1153  	// Data is processed before segtext, because we need
  1154  	// to see all symbols in the .data and .bss sections in order
  1155  	// to generate garbage collection information.
  1156  	datsize := int64(0)
  1157  
  1158  	// Writable data sections that do not need any specialized handling.
  1159  	writable := []sym.SymKind{
  1160  		sym.SELFSECT,
  1161  		sym.SMACHO,
  1162  		sym.SMACHOGOT,
  1163  		sym.SWINDOWS,
  1164  	}
  1165  	for _, symn := range writable {
  1166  		for _, s := range data[symn] {
  1167  			sect := addsection(ctxt.Arch, &Segdata, s.Name, 06)
  1168  			sect.Align = symalign(s)
  1169  			datsize = Rnd(datsize, int64(sect.Align))
  1170  			sect.Vaddr = uint64(datsize)
  1171  			s.Sect = sect
  1172  			s.Type = sym.SDATA
  1173  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1174  			datsize += s.Size
  1175  			sect.Length = uint64(datsize) - sect.Vaddr
  1176  		}
  1177  		checkdatsize(ctxt, datsize, symn)
  1178  	}
  1179  
  1180  	// .got (and .toc on ppc64)
  1181  	if len(data[sym.SELFGOT]) > 0 {
  1182  		sect := addsection(ctxt.Arch, &Segdata, ".got", 06)
  1183  		sect.Align = dataMaxAlign[sym.SELFGOT]
  1184  		datsize = Rnd(datsize, int64(sect.Align))
  1185  		sect.Vaddr = uint64(datsize)
  1186  		var toc *sym.Symbol
  1187  		for _, s := range data[sym.SELFGOT] {
  1188  			datsize = aligndatsize(datsize, s)
  1189  			s.Sect = sect
  1190  			s.Type = sym.SDATA
  1191  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1192  
  1193  			// Resolve .TOC. symbol for this object file (ppc64)
  1194  			toc = ctxt.Syms.ROLookup(".TOC.", int(s.Version))
  1195  			if toc != nil {
  1196  				toc.Sect = sect
  1197  				toc.Outer = s
  1198  				toc.Sub = s.Sub
  1199  				s.Sub = toc
  1200  
  1201  				toc.Value = 0x8000
  1202  			}
  1203  
  1204  			datsize += s.Size
  1205  		}
  1206  		checkdatsize(ctxt, datsize, sym.SELFGOT)
  1207  		sect.Length = uint64(datsize) - sect.Vaddr
  1208  	}
  1209  
  1210  	/* pointer-free data */
  1211  	sect := addsection(ctxt.Arch, &Segdata, ".noptrdata", 06)
  1212  	sect.Align = dataMaxAlign[sym.SNOPTRDATA]
  1213  	datsize = Rnd(datsize, int64(sect.Align))
  1214  	sect.Vaddr = uint64(datsize)
  1215  	ctxt.Syms.Lookup("runtime.noptrdata", 0).Sect = sect
  1216  	ctxt.Syms.Lookup("runtime.enoptrdata", 0).Sect = sect
  1217  	for _, s := range data[sym.SNOPTRDATA] {
  1218  		datsize = aligndatsize(datsize, s)
  1219  		s.Sect = sect
  1220  		s.Type = sym.SDATA
  1221  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1222  		datsize += s.Size
  1223  	}
  1224  	checkdatsize(ctxt, datsize, sym.SNOPTRDATA)
  1225  	sect.Length = uint64(datsize) - sect.Vaddr
  1226  
  1227  	hasinitarr := ctxt.linkShared
  1228  
  1229  	/* shared library initializer */
  1230  	switch ctxt.BuildMode {
  1231  	case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin:
  1232  		hasinitarr = true
  1233  	}
  1234  	if hasinitarr {
  1235  		sect := addsection(ctxt.Arch, &Segdata, ".init_array", 06)
  1236  		sect.Align = dataMaxAlign[sym.SINITARR]
  1237  		datsize = Rnd(datsize, int64(sect.Align))
  1238  		sect.Vaddr = uint64(datsize)
  1239  		for _, s := range data[sym.SINITARR] {
  1240  			datsize = aligndatsize(datsize, s)
  1241  			s.Sect = sect
  1242  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1243  			datsize += s.Size
  1244  		}
  1245  		sect.Length = uint64(datsize) - sect.Vaddr
  1246  		checkdatsize(ctxt, datsize, sym.SINITARR)
  1247  	}
  1248  
  1249  	/* data */
  1250  	sect = addsection(ctxt.Arch, &Segdata, ".data", 06)
  1251  	sect.Align = dataMaxAlign[sym.SDATA]
  1252  	datsize = Rnd(datsize, int64(sect.Align))
  1253  	sect.Vaddr = uint64(datsize)
  1254  	ctxt.Syms.Lookup("runtime.data", 0).Sect = sect
  1255  	ctxt.Syms.Lookup("runtime.edata", 0).Sect = sect
  1256  	var gc GCProg
  1257  	gc.Init(ctxt, "runtime.gcdata")
  1258  	for _, s := range data[sym.SDATA] {
  1259  		s.Sect = sect
  1260  		s.Type = sym.SDATA
  1261  		datsize = aligndatsize(datsize, s)
  1262  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1263  		gc.AddSym(s)
  1264  		datsize += s.Size
  1265  	}
  1266  	checkdatsize(ctxt, datsize, sym.SDATA)
  1267  	sect.Length = uint64(datsize) - sect.Vaddr
  1268  	gc.End(int64(sect.Length))
  1269  
  1270  	/* bss */
  1271  	sect = addsection(ctxt.Arch, &Segdata, ".bss", 06)
  1272  	sect.Align = dataMaxAlign[sym.SBSS]
  1273  	datsize = Rnd(datsize, int64(sect.Align))
  1274  	sect.Vaddr = uint64(datsize)
  1275  	ctxt.Syms.Lookup("runtime.bss", 0).Sect = sect
  1276  	ctxt.Syms.Lookup("runtime.ebss", 0).Sect = sect
  1277  	gc = GCProg{}
  1278  	gc.Init(ctxt, "runtime.gcbss")
  1279  	for _, s := range data[sym.SBSS] {
  1280  		s.Sect = sect
  1281  		datsize = aligndatsize(datsize, s)
  1282  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1283  		gc.AddSym(s)
  1284  		datsize += s.Size
  1285  	}
  1286  	checkdatsize(ctxt, datsize, sym.SBSS)
  1287  	sect.Length = uint64(datsize) - sect.Vaddr
  1288  	gc.End(int64(sect.Length))
  1289  
  1290  	/* pointer-free bss */
  1291  	sect = addsection(ctxt.Arch, &Segdata, ".noptrbss", 06)
  1292  	sect.Align = dataMaxAlign[sym.SNOPTRBSS]
  1293  	datsize = Rnd(datsize, int64(sect.Align))
  1294  	sect.Vaddr = uint64(datsize)
  1295  	ctxt.Syms.Lookup("runtime.noptrbss", 0).Sect = sect
  1296  	ctxt.Syms.Lookup("runtime.enoptrbss", 0).Sect = sect
  1297  	for _, s := range data[sym.SNOPTRBSS] {
  1298  		datsize = aligndatsize(datsize, s)
  1299  		s.Sect = sect
  1300  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1301  		datsize += s.Size
  1302  	}
  1303  
  1304  	sect.Length = uint64(datsize) - sect.Vaddr
  1305  	ctxt.Syms.Lookup("runtime.end", 0).Sect = sect
  1306  	checkdatsize(ctxt, datsize, sym.SNOPTRBSS)
  1307  
  1308  	if len(data[sym.STLSBSS]) > 0 {
  1309  		var sect *sym.Section
  1310  		if ctxt.IsELF && (ctxt.LinkMode == LinkExternal || !*FlagD) {
  1311  			sect = addsection(ctxt.Arch, &Segdata, ".tbss", 06)
  1312  			sect.Align = int32(ctxt.Arch.PtrSize)
  1313  			sect.Vaddr = 0
  1314  		}
  1315  		datsize = 0
  1316  
  1317  		for _, s := range data[sym.STLSBSS] {
  1318  			datsize = aligndatsize(datsize, s)
  1319  			s.Sect = sect
  1320  			s.Value = datsize
  1321  			datsize += s.Size
  1322  		}
  1323  		checkdatsize(ctxt, datsize, sym.STLSBSS)
  1324  
  1325  		if sect != nil {
  1326  			sect.Length = uint64(datsize)
  1327  		}
  1328  	}
  1329  
  1330  	/*
  1331  	 * We finished data, begin read-only data.
  1332  	 * Not all systems support a separate read-only non-executable data section.
  1333  	 * ELF systems do.
  1334  	 * OS X and Plan 9 do not.
  1335  	 * Windows PE may, but if so we have not implemented it.
  1336  	 * And if we're using external linking mode, the point is moot,
  1337  	 * since it's not our decision; that code expects the sections in
  1338  	 * segtext.
  1339  	 */
  1340  	var segro *sym.Segment
  1341  	if ctxt.IsELF && ctxt.LinkMode == LinkInternal {
  1342  		segro = &Segrodata
  1343  	} else {
  1344  		segro = &Segtext
  1345  	}
  1346  
  1347  	datsize = 0
  1348  
  1349  	/* read-only executable ELF, Mach-O sections */
  1350  	if len(data[sym.STEXT]) != 0 {
  1351  		Errorf(nil, "dodata found an sym.STEXT symbol: %s", data[sym.STEXT][0].Name)
  1352  	}
  1353  	for _, s := range data[sym.SELFRXSECT] {
  1354  		sect := addsection(ctxt.Arch, &Segtext, s.Name, 04)
  1355  		sect.Align = symalign(s)
  1356  		datsize = Rnd(datsize, int64(sect.Align))
  1357  		sect.Vaddr = uint64(datsize)
  1358  		s.Sect = sect
  1359  		s.Type = sym.SRODATA
  1360  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1361  		datsize += s.Size
  1362  		sect.Length = uint64(datsize) - sect.Vaddr
  1363  		checkdatsize(ctxt, datsize, sym.SELFRXSECT)
  1364  	}
  1365  
  1366  	/* read-only data */
  1367  	sect = addsection(ctxt.Arch, segro, ".rodata", 04)
  1368  
  1369  	sect.Vaddr = 0
  1370  	ctxt.Syms.Lookup("runtime.rodata", 0).Sect = sect
  1371  	ctxt.Syms.Lookup("runtime.erodata", 0).Sect = sect
  1372  	if !ctxt.UseRelro() {
  1373  		ctxt.Syms.Lookup("runtime.types", 0).Sect = sect
  1374  		ctxt.Syms.Lookup("runtime.etypes", 0).Sect = sect
  1375  	}
  1376  	for _, symn := range sym.ReadOnly {
  1377  		align := dataMaxAlign[symn]
  1378  		if sect.Align < align {
  1379  			sect.Align = align
  1380  		}
  1381  	}
  1382  	datsize = Rnd(datsize, int64(sect.Align))
  1383  	for _, symn := range sym.ReadOnly {
  1384  		for _, s := range data[symn] {
  1385  			datsize = aligndatsize(datsize, s)
  1386  			s.Sect = sect
  1387  			s.Type = sym.SRODATA
  1388  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1389  			datsize += s.Size
  1390  		}
  1391  		checkdatsize(ctxt, datsize, symn)
  1392  	}
  1393  	sect.Length = uint64(datsize) - sect.Vaddr
  1394  
  1395  	/* read-only ELF, Mach-O sections */
  1396  	for _, s := range data[sym.SELFROSECT] {
  1397  		sect = addsection(ctxt.Arch, segro, s.Name, 04)
  1398  		sect.Align = symalign(s)
  1399  		datsize = Rnd(datsize, int64(sect.Align))
  1400  		sect.Vaddr = uint64(datsize)
  1401  		s.Sect = sect
  1402  		s.Type = sym.SRODATA
  1403  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1404  		datsize += s.Size
  1405  		sect.Length = uint64(datsize) - sect.Vaddr
  1406  	}
  1407  	checkdatsize(ctxt, datsize, sym.SELFROSECT)
  1408  
  1409  	for _, s := range data[sym.SMACHOPLT] {
  1410  		sect = addsection(ctxt.Arch, segro, s.Name, 04)
  1411  		sect.Align = symalign(s)
  1412  		datsize = Rnd(datsize, int64(sect.Align))
  1413  		sect.Vaddr = uint64(datsize)
  1414  		s.Sect = sect
  1415  		s.Type = sym.SRODATA
  1416  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1417  		datsize += s.Size
  1418  		sect.Length = uint64(datsize) - sect.Vaddr
  1419  	}
  1420  	checkdatsize(ctxt, datsize, sym.SMACHOPLT)
  1421  
  1422  	// There is some data that are conceptually read-only but are written to by
  1423  	// relocations. On GNU systems, we can arrange for the dynamic linker to
  1424  	// mprotect sections after relocations are applied by giving them write
  1425  	// permissions in the object file and calling them ".data.rel.ro.FOO". We
  1426  	// divide the .rodata section between actual .rodata and .data.rel.ro.rodata,
  1427  	// but for the other sections that this applies to, we just write a read-only
  1428  	// .FOO section or a read-write .data.rel.ro.FOO section depending on the
  1429  	// situation.
  1430  	// TODO(mwhudson): It would make sense to do this more widely, but it makes
  1431  	// the system linker segfault on darwin.
  1432  	addrelrosection := func(suffix string) *sym.Section {
  1433  		return addsection(ctxt.Arch, segro, suffix, 04)
  1434  	}
  1435  
  1436  	if ctxt.UseRelro() {
  1437  		addrelrosection = func(suffix string) *sym.Section {
  1438  			seg := &Segrelrodata
  1439  			if ctxt.LinkMode == LinkExternal {
  1440  				// Using a separate segment with an external
  1441  				// linker results in some programs moving
  1442  				// their data sections unexpectedly, which
  1443  				// corrupts the moduledata. So we use the
  1444  				// rodata segment and let the external linker
  1445  				// sort out a rel.ro segment.
  1446  				seg = &Segrodata
  1447  			}
  1448  			return addsection(ctxt.Arch, seg, ".data.rel.ro"+suffix, 06)
  1449  		}
  1450  		/* data only written by relocations */
  1451  		sect = addrelrosection("")
  1452  
  1453  		sect.Vaddr = 0
  1454  		ctxt.Syms.Lookup("runtime.types", 0).Sect = sect
  1455  		ctxt.Syms.Lookup("runtime.etypes", 0).Sect = sect
  1456  		for _, symnro := range sym.ReadOnly {
  1457  			symn := sym.RelROMap[symnro]
  1458  			align := dataMaxAlign[symn]
  1459  			if sect.Align < align {
  1460  				sect.Align = align
  1461  			}
  1462  		}
  1463  		datsize = Rnd(datsize, int64(sect.Align))
  1464  		for _, symnro := range sym.ReadOnly {
  1465  			symn := sym.RelROMap[symnro]
  1466  			for _, s := range data[symn] {
  1467  				datsize = aligndatsize(datsize, s)
  1468  				if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect {
  1469  					Errorf(s, "s.Outer (%s) in different section from s, %s != %s", s.Outer.Name, s.Outer.Sect.Name, sect.Name)
  1470  				}
  1471  				s.Sect = sect
  1472  				s.Type = sym.SRODATA
  1473  				s.Value = int64(uint64(datsize) - sect.Vaddr)
  1474  				datsize += s.Size
  1475  			}
  1476  			checkdatsize(ctxt, datsize, symn)
  1477  		}
  1478  
  1479  		sect.Length = uint64(datsize) - sect.Vaddr
  1480  	}
  1481  
  1482  	/* typelink */
  1483  	sect = addrelrosection(".typelink")
  1484  	sect.Align = dataMaxAlign[sym.STYPELINK]
  1485  	datsize = Rnd(datsize, int64(sect.Align))
  1486  	sect.Vaddr = uint64(datsize)
  1487  	typelink := ctxt.Syms.Lookup("runtime.typelink", 0)
  1488  	typelink.Sect = sect
  1489  	typelink.Type = sym.SRODATA
  1490  	datsize += typelink.Size
  1491  	checkdatsize(ctxt, datsize, sym.STYPELINK)
  1492  	sect.Length = uint64(datsize) - sect.Vaddr
  1493  
  1494  	/* itablink */
  1495  	sect = addrelrosection(".itablink")
  1496  	sect.Align = dataMaxAlign[sym.SITABLINK]
  1497  	datsize = Rnd(datsize, int64(sect.Align))
  1498  	sect.Vaddr = uint64(datsize)
  1499  	ctxt.Syms.Lookup("runtime.itablink", 0).Sect = sect
  1500  	ctxt.Syms.Lookup("runtime.eitablink", 0).Sect = sect
  1501  	for _, s := range data[sym.SITABLINK] {
  1502  		datsize = aligndatsize(datsize, s)
  1503  		s.Sect = sect
  1504  		s.Type = sym.SRODATA
  1505  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1506  		datsize += s.Size
  1507  	}
  1508  	checkdatsize(ctxt, datsize, sym.SITABLINK)
  1509  	sect.Length = uint64(datsize) - sect.Vaddr
  1510  
  1511  	/* gosymtab */
  1512  	sect = addrelrosection(".gosymtab")
  1513  	sect.Align = dataMaxAlign[sym.SSYMTAB]
  1514  	datsize = Rnd(datsize, int64(sect.Align))
  1515  	sect.Vaddr = uint64(datsize)
  1516  	ctxt.Syms.Lookup("runtime.symtab", 0).Sect = sect
  1517  	ctxt.Syms.Lookup("runtime.esymtab", 0).Sect = sect
  1518  	for _, s := range data[sym.SSYMTAB] {
  1519  		datsize = aligndatsize(datsize, s)
  1520  		s.Sect = sect
  1521  		s.Type = sym.SRODATA
  1522  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1523  		datsize += s.Size
  1524  	}
  1525  	checkdatsize(ctxt, datsize, sym.SSYMTAB)
  1526  	sect.Length = uint64(datsize) - sect.Vaddr
  1527  
  1528  	/* gopclntab */
  1529  	sect = addrelrosection(".gopclntab")
  1530  	sect.Align = dataMaxAlign[sym.SPCLNTAB]
  1531  	datsize = Rnd(datsize, int64(sect.Align))
  1532  	sect.Vaddr = uint64(datsize)
  1533  	ctxt.Syms.Lookup("runtime.pclntab", 0).Sect = sect
  1534  	ctxt.Syms.Lookup("runtime.epclntab", 0).Sect = sect
  1535  	for _, s := range data[sym.SPCLNTAB] {
  1536  		datsize = aligndatsize(datsize, s)
  1537  		s.Sect = sect
  1538  		s.Type = sym.SRODATA
  1539  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1540  		datsize += s.Size
  1541  	}
  1542  	checkdatsize(ctxt, datsize, sym.SRODATA)
  1543  	sect.Length = uint64(datsize) - sect.Vaddr
  1544  
  1545  	// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
  1546  	if datsize != int64(uint32(datsize)) {
  1547  		Errorf(nil, "read-only data segment too large: %d", datsize)
  1548  	}
  1549  
  1550  	for symn := sym.SELFRXSECT; symn < sym.SXREF; symn++ {
  1551  		datap = append(datap, data[symn]...)
  1552  	}
  1553  
  1554  	dwarfgeneratedebugsyms(ctxt)
  1555  
  1556  	var i int
  1557  	for ; i < len(dwarfp); i++ {
  1558  		s := dwarfp[i]
  1559  		if s.Type != sym.SDWARFSECT {
  1560  			break
  1561  		}
  1562  
  1563  		sect = addsection(ctxt.Arch, &Segdwarf, s.Name, 04)
  1564  		sect.Align = 1
  1565  		datsize = Rnd(datsize, int64(sect.Align))
  1566  		sect.Vaddr = uint64(datsize)
  1567  		s.Sect = sect
  1568  		s.Type = sym.SRODATA
  1569  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1570  		datsize += s.Size
  1571  		sect.Length = uint64(datsize) - sect.Vaddr
  1572  	}
  1573  	checkdatsize(ctxt, datsize, sym.SDWARFSECT)
  1574  
  1575  	for i < len(dwarfp) {
  1576  		curType := dwarfp[i].Type
  1577  		var sect *sym.Section
  1578  		switch curType {
  1579  		case sym.SDWARFINFO:
  1580  			sect = addsection(ctxt.Arch, &Segdwarf, ".debug_info", 04)
  1581  		case sym.SDWARFRANGE:
  1582  			sect = addsection(ctxt.Arch, &Segdwarf, ".debug_ranges", 04)
  1583  		case sym.SDWARFLOC:
  1584  			sect = addsection(ctxt.Arch, &Segdwarf, ".debug_loc", 04)
  1585  		default:
  1586  			Errorf(dwarfp[i], "unknown DWARF section %v", curType)
  1587  		}
  1588  
  1589  		sect.Align = 1
  1590  		datsize = Rnd(datsize, int64(sect.Align))
  1591  		sect.Vaddr = uint64(datsize)
  1592  		for ; i < len(dwarfp); i++ {
  1593  			s := dwarfp[i]
  1594  			if s.Type != curType {
  1595  				break
  1596  			}
  1597  			s.Sect = sect
  1598  			s.Type = sym.SRODATA
  1599  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1600  			s.Attr |= sym.AttrLocal
  1601  			datsize += s.Size
  1602  		}
  1603  		sect.Length = uint64(datsize) - sect.Vaddr
  1604  		checkdatsize(ctxt, datsize, curType)
  1605  	}
  1606  
  1607  	/* number the sections */
  1608  	n := int32(1)
  1609  
  1610  	for _, sect := range Segtext.Sections {
  1611  		sect.Extnum = int16(n)
  1612  		n++
  1613  	}
  1614  	for _, sect := range Segrodata.Sections {
  1615  		sect.Extnum = int16(n)
  1616  		n++
  1617  	}
  1618  	for _, sect := range Segrelrodata.Sections {
  1619  		sect.Extnum = int16(n)
  1620  		n++
  1621  	}
  1622  	for _, sect := range Segdata.Sections {
  1623  		sect.Extnum = int16(n)
  1624  		n++
  1625  	}
  1626  	for _, sect := range Segdwarf.Sections {
  1627  		sect.Extnum = int16(n)
  1628  		n++
  1629  	}
  1630  }
  1631  
  1632  func dodataSect(ctxt *Link, symn sym.SymKind, syms []*sym.Symbol) (result []*sym.Symbol, maxAlign int32) {
  1633  	if ctxt.HeadType == objabi.Hdarwin {
  1634  		// Some symbols may no longer belong in syms
  1635  		// due to movement in machosymorder.
  1636  		newSyms := make([]*sym.Symbol, 0, len(syms))
  1637  		for _, s := range syms {
  1638  			if s.Type == symn {
  1639  				newSyms = append(newSyms, s)
  1640  			}
  1641  		}
  1642  		syms = newSyms
  1643  	}
  1644  
  1645  	var head, tail *sym.Symbol
  1646  	symsSort := make([]dataSortKey, 0, len(syms))
  1647  	for _, s := range syms {
  1648  		if s.Attr.OnList() {
  1649  			log.Fatalf("symbol %s listed multiple times", s.Name)
  1650  		}
  1651  		s.Attr |= sym.AttrOnList
  1652  		switch {
  1653  		case s.Size < int64(len(s.P)):
  1654  			Errorf(s, "initialize bounds (%d < %d)", s.Size, len(s.P))
  1655  		case s.Size < 0:
  1656  			Errorf(s, "negative size (%d bytes)", s.Size)
  1657  		case s.Size > cutoff:
  1658  			Errorf(s, "symbol too large (%d bytes)", s.Size)
  1659  		}
  1660  
  1661  		// If the usually-special section-marker symbols are being laid
  1662  		// out as regular symbols, put them either at the beginning or
  1663  		// end of their section.
  1664  		if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin {
  1665  			switch s.Name {
  1666  			case "runtime.text", "runtime.bss", "runtime.data", "runtime.types":
  1667  				head = s
  1668  				continue
  1669  			case "runtime.etext", "runtime.ebss", "runtime.edata", "runtime.etypes":
  1670  				tail = s
  1671  				continue
  1672  			}
  1673  		}
  1674  
  1675  		key := dataSortKey{
  1676  			size: s.Size,
  1677  			name: s.Name,
  1678  			sym:  s,
  1679  		}
  1680  
  1681  		switch s.Type {
  1682  		case sym.SELFGOT:
  1683  			// For ppc64, we want to interleave the .got and .toc sections
  1684  			// from input files. Both are type sym.SELFGOT, so in that case
  1685  			// we skip size comparison and fall through to the name
  1686  			// comparison (conveniently, .got sorts before .toc).
  1687  			key.size = 0
  1688  		}
  1689  
  1690  		symsSort = append(symsSort, key)
  1691  	}
  1692  
  1693  	sort.Sort(bySizeAndName(symsSort))
  1694  
  1695  	off := 0
  1696  	if head != nil {
  1697  		syms[0] = head
  1698  		off++
  1699  	}
  1700  	for i, symSort := range symsSort {
  1701  		syms[i+off] = symSort.sym
  1702  		align := symalign(symSort.sym)
  1703  		if maxAlign < align {
  1704  			maxAlign = align
  1705  		}
  1706  	}
  1707  	if tail != nil {
  1708  		syms[len(syms)-1] = tail
  1709  	}
  1710  
  1711  	if ctxt.IsELF && symn == sym.SELFROSECT {
  1712  		// Make .rela and .rela.plt contiguous, the ELF ABI requires this
  1713  		// and Solaris actually cares.
  1714  		reli, plti := -1, -1
  1715  		for i, s := range syms {
  1716  			switch s.Name {
  1717  			case ".rel.plt", ".rela.plt":
  1718  				plti = i
  1719  			case ".rel", ".rela":
  1720  				reli = i
  1721  			}
  1722  		}
  1723  		if reli >= 0 && plti >= 0 && plti != reli+1 {
  1724  			var first, second int
  1725  			if plti > reli {
  1726  				first, second = reli, plti
  1727  			} else {
  1728  				first, second = plti, reli
  1729  			}
  1730  			rel, plt := syms[reli], syms[plti]
  1731  			copy(syms[first+2:], syms[first+1:second])
  1732  			syms[first+0] = rel
  1733  			syms[first+1] = plt
  1734  
  1735  			// Make sure alignment doesn't introduce a gap.
  1736  			// Setting the alignment explicitly prevents
  1737  			// symalign from basing it on the size and
  1738  			// getting it wrong.
  1739  			rel.Align = int32(ctxt.Arch.RegSize)
  1740  			plt.Align = int32(ctxt.Arch.RegSize)
  1741  		}
  1742  	}
  1743  
  1744  	return syms, maxAlign
  1745  }
  1746  
  1747  // Add buildid to beginning of text segment, on non-ELF systems.
  1748  // Non-ELF binary formats are not always flexible enough to
  1749  // give us a place to put the Go build ID. On those systems, we put it
  1750  // at the very beginning of the text segment.
  1751  // This ``header'' is read by cmd/go.
  1752  func (ctxt *Link) textbuildid() {
  1753  	if ctxt.IsELF || ctxt.BuildMode == BuildModePlugin || *flagBuildid == "" {
  1754  		return
  1755  	}
  1756  
  1757  	s := ctxt.Syms.Lookup("go.buildid", 0)
  1758  	s.Attr |= sym.AttrReachable
  1759  	// The \xff is invalid UTF-8, meant to make it less likely
  1760  	// to find one of these accidentally.
  1761  	data := "\xff Go build ID: " + strconv.Quote(*flagBuildid) + "\n \xff"
  1762  	s.Type = sym.STEXT
  1763  	s.P = []byte(data)
  1764  	s.Size = int64(len(s.P))
  1765  
  1766  	ctxt.Textp = append(ctxt.Textp, nil)
  1767  	copy(ctxt.Textp[1:], ctxt.Textp)
  1768  	ctxt.Textp[0] = s
  1769  }
  1770  
  1771  // assign addresses to text
  1772  func (ctxt *Link) textaddress() {
  1773  	addsection(ctxt.Arch, &Segtext, ".text", 05)
  1774  
  1775  	// Assign PCs in text segment.
  1776  	// Could parallelize, by assigning to text
  1777  	// and then letting threads copy down, but probably not worth it.
  1778  	sect := Segtext.Sections[0]
  1779  
  1780  	sect.Align = int32(Funcalign)
  1781  
  1782  	text := ctxt.Syms.Lookup("runtime.text", 0)
  1783  	text.Sect = sect
  1784  
  1785  	if ctxt.DynlinkingGo() && ctxt.HeadType == objabi.Hdarwin {
  1786  		etext := ctxt.Syms.Lookup("runtime.etext", 0)
  1787  		etext.Sect = sect
  1788  
  1789  		ctxt.Textp = append(ctxt.Textp, etext, nil)
  1790  		copy(ctxt.Textp[1:], ctxt.Textp)
  1791  		ctxt.Textp[0] = text
  1792  	}
  1793  
  1794  	va := uint64(*FlagTextAddr)
  1795  	n := 1
  1796  	sect.Vaddr = va
  1797  	ntramps := 0
  1798  	for _, s := range ctxt.Textp {
  1799  		sect, n, va = assignAddress(ctxt, sect, n, s, va, false)
  1800  
  1801  		trampoline(ctxt, s) // resolve jumps, may add trampolines if jump too far
  1802  
  1803  		// lay down trampolines after each function
  1804  		for ; ntramps < len(ctxt.tramps); ntramps++ {
  1805  			tramp := ctxt.tramps[ntramps]
  1806  			sect, n, va = assignAddress(ctxt, sect, n, tramp, va, true)
  1807  		}
  1808  	}
  1809  
  1810  	sect.Length = va - sect.Vaddr
  1811  	ctxt.Syms.Lookup("runtime.etext", 0).Sect = sect
  1812  
  1813  	// merge tramps into Textp, keeping Textp in address order
  1814  	if ntramps != 0 {
  1815  		newtextp := make([]*sym.Symbol, 0, len(ctxt.Textp)+ntramps)
  1816  		i := 0
  1817  		for _, s := range ctxt.Textp {
  1818  			for ; i < ntramps && ctxt.tramps[i].Value < s.Value; i++ {
  1819  				newtextp = append(newtextp, ctxt.tramps[i])
  1820  			}
  1821  			newtextp = append(newtextp, s)
  1822  		}
  1823  		newtextp = append(newtextp, ctxt.tramps[i:ntramps]...)
  1824  
  1825  		ctxt.Textp = newtextp
  1826  	}
  1827  }
  1828  
  1829  // assigns address for a text symbol, returns (possibly new) section, its number, and the address
  1830  // Note: once we have trampoline insertion support for external linking, this function
  1831  // will not need to create new text sections, and so no need to return sect and n.
  1832  func assignAddress(ctxt *Link, sect *sym.Section, n int, s *sym.Symbol, va uint64, isTramp bool) (*sym.Section, int, uint64) {
  1833  	s.Sect = sect
  1834  	if s.Attr.SubSymbol() {
  1835  		return sect, n, va
  1836  	}
  1837  	if s.Align != 0 {
  1838  		va = uint64(Rnd(int64(va), int64(s.Align)))
  1839  	} else {
  1840  		va = uint64(Rnd(int64(va), int64(Funcalign)))
  1841  	}
  1842  	s.Value = 0
  1843  	for sub := s; sub != nil; sub = sub.Sub {
  1844  		sub.Value += int64(va)
  1845  	}
  1846  
  1847  	funcsize := uint64(MINFUNC) // spacing required for findfunctab
  1848  	if s.Size > MINFUNC {
  1849  		funcsize = uint64(s.Size)
  1850  	}
  1851  
  1852  	// On ppc64x a text section should not be larger than 2^26 bytes due to the size of
  1853  	// call target offset field in the bl instruction.  Splitting into smaller text
  1854  	// sections smaller than this limit allows the GNU linker to modify the long calls
  1855  	// appropriately.  The limit allows for the space needed for tables inserted by the linker.
  1856  
  1857  	// If this function doesn't fit in the current text section, then create a new one.
  1858  
  1859  	// Only break at outermost syms.
  1860  
  1861  	if ctxt.Arch.InFamily(sys.PPC64) && s.Outer == nil && ctxt.IsELF && ctxt.LinkMode == LinkExternal && va-sect.Vaddr+funcsize+maxSizeTrampolinesPPC64(s, isTramp) > 0x1c00000 {
  1862  		// Set the length for the previous text section
  1863  		sect.Length = va - sect.Vaddr
  1864  
  1865  		// Create new section, set the starting Vaddr
  1866  		sect = addsection(ctxt.Arch, &Segtext, ".text", 05)
  1867  		sect.Vaddr = va
  1868  		s.Sect = sect
  1869  
  1870  		// Create a symbol for the start of the secondary text sections
  1871  		ctxt.Syms.Lookup(fmt.Sprintf("runtime.text.%d", n), 0).Sect = sect
  1872  		n++
  1873  	}
  1874  	va += funcsize
  1875  
  1876  	return sect, n, va
  1877  }
  1878  
  1879  // assign addresses
  1880  func (ctxt *Link) address() {
  1881  	va := uint64(*FlagTextAddr)
  1882  	Segtext.Rwx = 05
  1883  	Segtext.Vaddr = va
  1884  	Segtext.Fileoff = uint64(HEADR)
  1885  	for _, s := range Segtext.Sections {
  1886  		va = uint64(Rnd(int64(va), int64(s.Align)))
  1887  		s.Vaddr = va
  1888  		va += s.Length
  1889  	}
  1890  
  1891  	Segtext.Length = va - uint64(*FlagTextAddr)
  1892  	Segtext.Filelen = Segtext.Length
  1893  	if ctxt.HeadType == objabi.Hnacl {
  1894  		va += 32 // room for the "halt sled"
  1895  	}
  1896  
  1897  	if len(Segrodata.Sections) > 0 {
  1898  		// align to page boundary so as not to mix
  1899  		// rodata and executable text.
  1900  		//
  1901  		// Note: gold or GNU ld will reduce the size of the executable
  1902  		// file by arranging for the relro segment to end at a page
  1903  		// boundary, and overlap the end of the text segment with the
  1904  		// start of the relro segment in the file.  The PT_LOAD segments
  1905  		// will be such that the last page of the text segment will be
  1906  		// mapped twice, once r-x and once starting out rw- and, after
  1907  		// relocation processing, changed to r--.
  1908  		//
  1909  		// Ideally the last page of the text segment would not be
  1910  		// writable even for this short period.
  1911  		va = uint64(Rnd(int64(va), int64(*FlagRound)))
  1912  
  1913  		Segrodata.Rwx = 04
  1914  		Segrodata.Vaddr = va
  1915  		Segrodata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff
  1916  		Segrodata.Filelen = 0
  1917  		for _, s := range Segrodata.Sections {
  1918  			va = uint64(Rnd(int64(va), int64(s.Align)))
  1919  			s.Vaddr = va
  1920  			va += s.Length
  1921  		}
  1922  
  1923  		Segrodata.Length = va - Segrodata.Vaddr
  1924  		Segrodata.Filelen = Segrodata.Length
  1925  	}
  1926  	if len(Segrelrodata.Sections) > 0 {
  1927  		// align to page boundary so as not to mix
  1928  		// rodata, rel-ro data, and executable text.
  1929  		va = uint64(Rnd(int64(va), int64(*FlagRound)))
  1930  
  1931  		Segrelrodata.Rwx = 06
  1932  		Segrelrodata.Vaddr = va
  1933  		Segrelrodata.Fileoff = va - Segrodata.Vaddr + Segrodata.Fileoff
  1934  		Segrelrodata.Filelen = 0
  1935  		for _, s := range Segrelrodata.Sections {
  1936  			va = uint64(Rnd(int64(va), int64(s.Align)))
  1937  			s.Vaddr = va
  1938  			va += s.Length
  1939  		}
  1940  
  1941  		Segrelrodata.Length = va - Segrelrodata.Vaddr
  1942  		Segrelrodata.Filelen = Segrelrodata.Length
  1943  	}
  1944  
  1945  	va = uint64(Rnd(int64(va), int64(*FlagRound)))
  1946  	Segdata.Rwx = 06
  1947  	Segdata.Vaddr = va
  1948  	Segdata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff
  1949  	Segdata.Filelen = 0
  1950  	if ctxt.HeadType == objabi.Hwindows {
  1951  		Segdata.Fileoff = Segtext.Fileoff + uint64(Rnd(int64(Segtext.Length), PEFILEALIGN))
  1952  	}
  1953  	if ctxt.HeadType == objabi.Hplan9 {
  1954  		Segdata.Fileoff = Segtext.Fileoff + Segtext.Filelen
  1955  	}
  1956  	var data *sym.Section
  1957  	var noptr *sym.Section
  1958  	var bss *sym.Section
  1959  	var noptrbss *sym.Section
  1960  	for i, s := range Segdata.Sections {
  1961  		if ctxt.IsELF && s.Name == ".tbss" {
  1962  			continue
  1963  		}
  1964  		vlen := int64(s.Length)
  1965  		if i+1 < len(Segdata.Sections) && !(ctxt.IsELF && Segdata.Sections[i+1].Name == ".tbss") {
  1966  			vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr)
  1967  		}
  1968  		s.Vaddr = va
  1969  		va += uint64(vlen)
  1970  		Segdata.Length = va - Segdata.Vaddr
  1971  		if s.Name == ".data" {
  1972  			data = s
  1973  		}
  1974  		if s.Name == ".noptrdata" {
  1975  			noptr = s
  1976  		}
  1977  		if s.Name == ".bss" {
  1978  			bss = s
  1979  		}
  1980  		if s.Name == ".noptrbss" {
  1981  			noptrbss = s
  1982  		}
  1983  	}
  1984  
  1985  	Segdata.Filelen = bss.Vaddr - Segdata.Vaddr
  1986  
  1987  	va = uint64(Rnd(int64(va), int64(*FlagRound)))
  1988  	Segdwarf.Rwx = 06
  1989  	Segdwarf.Vaddr = va
  1990  	Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(*FlagRound)))
  1991  	Segdwarf.Filelen = 0
  1992  	if ctxt.HeadType == objabi.Hwindows {
  1993  		Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(PEFILEALIGN)))
  1994  	}
  1995  	for i, s := range Segdwarf.Sections {
  1996  		vlen := int64(s.Length)
  1997  		if i+1 < len(Segdwarf.Sections) {
  1998  			vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr)
  1999  		}
  2000  		s.Vaddr = va
  2001  		va += uint64(vlen)
  2002  		if ctxt.HeadType == objabi.Hwindows {
  2003  			va = uint64(Rnd(int64(va), PEFILEALIGN))
  2004  		}
  2005  		Segdwarf.Length = va - Segdwarf.Vaddr
  2006  	}
  2007  
  2008  	Segdwarf.Filelen = va - Segdwarf.Vaddr
  2009  
  2010  	var (
  2011  		text     = Segtext.Sections[0]
  2012  		rodata   = ctxt.Syms.Lookup("runtime.rodata", 0).Sect
  2013  		itablink = ctxt.Syms.Lookup("runtime.itablink", 0).Sect
  2014  		symtab   = ctxt.Syms.Lookup("runtime.symtab", 0).Sect
  2015  		pclntab  = ctxt.Syms.Lookup("runtime.pclntab", 0).Sect
  2016  		types    = ctxt.Syms.Lookup("runtime.types", 0).Sect
  2017  	)
  2018  	lasttext := text
  2019  	// Could be multiple .text sections
  2020  	for _, sect := range Segtext.Sections {
  2021  		if sect.Name == ".text" {
  2022  			lasttext = sect
  2023  		}
  2024  	}
  2025  
  2026  	for _, s := range datap {
  2027  		if s.Sect != nil {
  2028  			s.Value += int64(s.Sect.Vaddr)
  2029  		}
  2030  		for sub := s.Sub; sub != nil; sub = sub.Sub {
  2031  			sub.Value += s.Value
  2032  		}
  2033  	}
  2034  
  2035  	for _, s := range dwarfp {
  2036  		if s.Sect != nil {
  2037  			s.Value += int64(s.Sect.Vaddr)
  2038  		}
  2039  		for sub := s.Sub; sub != nil; sub = sub.Sub {
  2040  			sub.Value += s.Value
  2041  		}
  2042  	}
  2043  
  2044  	if ctxt.BuildMode == BuildModeShared {
  2045  		s := ctxt.Syms.Lookup("go.link.abihashbytes", 0)
  2046  		sectSym := ctxt.Syms.Lookup(".note.go.abihash", 0)
  2047  		s.Sect = sectSym.Sect
  2048  		s.Value = int64(sectSym.Sect.Vaddr + 16)
  2049  	}
  2050  
  2051  	ctxt.xdefine("runtime.text", sym.STEXT, int64(text.Vaddr))
  2052  	ctxt.xdefine("runtime.etext", sym.STEXT, int64(lasttext.Vaddr+lasttext.Length))
  2053  
  2054  	// If there are multiple text sections, create runtime.text.n for
  2055  	// their section Vaddr, using n for index
  2056  	n := 1
  2057  	for _, sect := range Segtext.Sections[1:] {
  2058  		if sect.Name != ".text" {
  2059  			break
  2060  		}
  2061  		symname := fmt.Sprintf("runtime.text.%d", n)
  2062  		ctxt.xdefine(symname, sym.STEXT, int64(sect.Vaddr))
  2063  		n++
  2064  	}
  2065  
  2066  	ctxt.xdefine("runtime.rodata", sym.SRODATA, int64(rodata.Vaddr))
  2067  	ctxt.xdefine("runtime.erodata", sym.SRODATA, int64(rodata.Vaddr+rodata.Length))
  2068  	ctxt.xdefine("runtime.types", sym.SRODATA, int64(types.Vaddr))
  2069  	ctxt.xdefine("runtime.etypes", sym.SRODATA, int64(types.Vaddr+types.Length))
  2070  	ctxt.xdefine("runtime.itablink", sym.SRODATA, int64(itablink.Vaddr))
  2071  	ctxt.xdefine("runtime.eitablink", sym.SRODATA, int64(itablink.Vaddr+itablink.Length))
  2072  
  2073  	s := ctxt.Syms.Lookup("runtime.gcdata", 0)
  2074  	s.Attr |= sym.AttrLocal
  2075  	ctxt.xdefine("runtime.egcdata", sym.SRODATA, Symaddr(s)+s.Size)
  2076  	ctxt.Syms.Lookup("runtime.egcdata", 0).Sect = s.Sect
  2077  
  2078  	s = ctxt.Syms.Lookup("runtime.gcbss", 0)
  2079  	s.Attr |= sym.AttrLocal
  2080  	ctxt.xdefine("runtime.egcbss", sym.SRODATA, Symaddr(s)+s.Size)
  2081  	ctxt.Syms.Lookup("runtime.egcbss", 0).Sect = s.Sect
  2082  
  2083  	ctxt.xdefine("runtime.symtab", sym.SRODATA, int64(symtab.Vaddr))
  2084  	ctxt.xdefine("runtime.esymtab", sym.SRODATA, int64(symtab.Vaddr+symtab.Length))
  2085  	ctxt.xdefine("runtime.pclntab", sym.SRODATA, int64(pclntab.Vaddr))
  2086  	ctxt.xdefine("runtime.epclntab", sym.SRODATA, int64(pclntab.Vaddr+pclntab.Length))
  2087  	ctxt.xdefine("runtime.noptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr))
  2088  	ctxt.xdefine("runtime.enoptrdata", sym.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length))
  2089  	ctxt.xdefine("runtime.bss", sym.SBSS, int64(bss.Vaddr))
  2090  	ctxt.xdefine("runtime.ebss", sym.SBSS, int64(bss.Vaddr+bss.Length))
  2091  	ctxt.xdefine("runtime.data", sym.SDATA, int64(data.Vaddr))
  2092  	ctxt.xdefine("runtime.edata", sym.SDATA, int64(data.Vaddr+data.Length))
  2093  	ctxt.xdefine("runtime.noptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr))
  2094  	ctxt.xdefine("runtime.enoptrbss", sym.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length))
  2095  	ctxt.xdefine("runtime.end", sym.SBSS, int64(Segdata.Vaddr+Segdata.Length))
  2096  }
  2097  
  2098  // add a trampoline with symbol s (to be laid down after the current function)
  2099  func (ctxt *Link) AddTramp(s *sym.Symbol) {
  2100  	s.Type = sym.STEXT
  2101  	s.Attr |= sym.AttrReachable
  2102  	s.Attr |= sym.AttrOnList
  2103  	ctxt.tramps = append(ctxt.tramps, s)
  2104  	if *FlagDebugTramp > 0 && ctxt.Debugvlog > 0 {
  2105  		ctxt.Logf("trampoline %s inserted\n", s)
  2106  	}
  2107  }