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