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