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