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