github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/cmd/link/internal/ld/data.go (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
     3  // http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
     4  //
     5  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     6  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     7  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     8  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     9  //	Portions Copyright © 2004,2006 Bruce Ellis
    10  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    11  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    12  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    13  //
    14  // Permission is hereby granted, free of charge, to any person obtaining a copy
    15  // of this software and associated documentation files (the "Software"), to deal
    16  // in the Software without restriction, including without limitation the rights
    17  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    18  // copies of the Software, and to permit persons to whom the Software is
    19  // furnished to do so, subject to the following conditions:
    20  //
    21  // The above copyright notice and this permission notice shall be included in
    22  // all copies or substantial portions of the Software.
    23  //
    24  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    25  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    26  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    27  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    28  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    29  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    30  // THE SOFTWARE.
    31  
    32  package ld
    33  
    34  import (
    35  	"cmd/internal/gcprog"
    36  	"cmd/internal/obj"
    37  	"fmt"
    38  	"log"
    39  	"os"
    40  	"strconv"
    41  	"strings"
    42  )
    43  
    44  func Symgrow(ctxt *Link, s *LSym, siz int64) {
    45  	if int64(int(siz)) != siz {
    46  		log.Fatalf("symgrow size %d too long", siz)
    47  	}
    48  	if int64(len(s.P)) >= siz {
    49  		return
    50  	}
    51  	for cap(s.P) < int(siz) {
    52  		s.P = append(s.P[:len(s.P)], 0)
    53  	}
    54  	s.P = s.P[:siz]
    55  }
    56  
    57  func Addrel(s *LSym) *Reloc {
    58  	s.R = append(s.R, Reloc{})
    59  	return &s.R[len(s.R)-1]
    60  }
    61  
    62  func setuintxx(ctxt *Link, s *LSym, off int64, v uint64, wid int64) int64 {
    63  	if s.Type == 0 {
    64  		s.Type = obj.SDATA
    65  	}
    66  	s.Reachable = true
    67  	if s.Size < off+wid {
    68  		s.Size = off + wid
    69  		Symgrow(ctxt, s, s.Size)
    70  	}
    71  
    72  	switch wid {
    73  	case 1:
    74  		s.P[off] = uint8(v)
    75  	case 2:
    76  		ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(v))
    77  	case 4:
    78  		ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(v))
    79  	case 8:
    80  		ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(v))
    81  	}
    82  
    83  	return off + wid
    84  }
    85  
    86  func adduintxx(ctxt *Link, s *LSym, v uint64, wid int) int64 {
    87  	off := s.Size
    88  	setuintxx(ctxt, s, off, v, int64(wid))
    89  	return off
    90  }
    91  
    92  func Adduint8(ctxt *Link, s *LSym, v uint8) int64 {
    93  	return adduintxx(ctxt, s, uint64(v), 1)
    94  }
    95  
    96  func Adduint16(ctxt *Link, s *LSym, v uint16) int64 {
    97  	return adduintxx(ctxt, s, uint64(v), 2)
    98  }
    99  
   100  func Adduint32(ctxt *Link, s *LSym, v uint32) int64 {
   101  	return adduintxx(ctxt, s, uint64(v), 4)
   102  }
   103  
   104  func Adduint64(ctxt *Link, s *LSym, v uint64) int64 {
   105  	return adduintxx(ctxt, s, v, 8)
   106  }
   107  
   108  func adduint(ctxt *Link, s *LSym, v uint64) int64 {
   109  	return adduintxx(ctxt, s, v, Thearch.Intsize)
   110  }
   111  
   112  func setuint8(ctxt *Link, s *LSym, r int64, v uint8) int64 {
   113  	return setuintxx(ctxt, s, r, uint64(v), 1)
   114  }
   115  
   116  func setuint32(ctxt *Link, s *LSym, r int64, v uint32) int64 {
   117  	return setuintxx(ctxt, s, r, uint64(v), 4)
   118  }
   119  
   120  func Addaddrplus(ctxt *Link, s *LSym, t *LSym, add int64) int64 {
   121  	if s.Type == 0 {
   122  		s.Type = obj.SDATA
   123  	}
   124  	s.Reachable = true
   125  	i := s.Size
   126  	s.Size += int64(ctxt.Arch.Ptrsize)
   127  	Symgrow(ctxt, s, s.Size)
   128  	r := Addrel(s)
   129  	r.Sym = t
   130  	r.Off = int32(i)
   131  	r.Siz = uint8(ctxt.Arch.Ptrsize)
   132  	r.Type = obj.R_ADDR
   133  	r.Add = add
   134  	return i + int64(r.Siz)
   135  }
   136  
   137  func Addpcrelplus(ctxt *Link, s *LSym, t *LSym, add int64) int64 {
   138  	if s.Type == 0 {
   139  		s.Type = obj.SDATA
   140  	}
   141  	s.Reachable = true
   142  	i := s.Size
   143  	s.Size += 4
   144  	Symgrow(ctxt, s, s.Size)
   145  	r := Addrel(s)
   146  	r.Sym = t
   147  	r.Off = int32(i)
   148  	r.Add = add
   149  	r.Type = obj.R_PCREL
   150  	r.Siz = 4
   151  	return i + int64(r.Siz)
   152  }
   153  
   154  func Addaddr(ctxt *Link, s *LSym, t *LSym) int64 {
   155  	return Addaddrplus(ctxt, s, t, 0)
   156  }
   157  
   158  func setaddrplus(ctxt *Link, s *LSym, off int64, t *LSym, add int64) int64 {
   159  	if s.Type == 0 {
   160  		s.Type = obj.SDATA
   161  	}
   162  	s.Reachable = true
   163  	if off+int64(ctxt.Arch.Ptrsize) > s.Size {
   164  		s.Size = off + int64(ctxt.Arch.Ptrsize)
   165  		Symgrow(ctxt, s, s.Size)
   166  	}
   167  
   168  	r := Addrel(s)
   169  	r.Sym = t
   170  	r.Off = int32(off)
   171  	r.Siz = uint8(ctxt.Arch.Ptrsize)
   172  	r.Type = obj.R_ADDR
   173  	r.Add = add
   174  	return off + int64(r.Siz)
   175  }
   176  
   177  func setaddr(ctxt *Link, s *LSym, off int64, t *LSym) int64 {
   178  	return setaddrplus(ctxt, s, off, t, 0)
   179  }
   180  
   181  func addsize(ctxt *Link, s *LSym, t *LSym) int64 {
   182  	if s.Type == 0 {
   183  		s.Type = obj.SDATA
   184  	}
   185  	s.Reachable = true
   186  	i := s.Size
   187  	s.Size += int64(ctxt.Arch.Ptrsize)
   188  	Symgrow(ctxt, s, s.Size)
   189  	r := Addrel(s)
   190  	r.Sym = t
   191  	r.Off = int32(i)
   192  	r.Siz = uint8(ctxt.Arch.Ptrsize)
   193  	r.Type = obj.R_SIZE
   194  	return i + int64(r.Siz)
   195  }
   196  
   197  func addaddrplus4(ctxt *Link, s *LSym, t *LSym, add int64) int64 {
   198  	if s.Type == 0 {
   199  		s.Type = obj.SDATA
   200  	}
   201  	s.Reachable = true
   202  	i := s.Size
   203  	s.Size += 4
   204  	Symgrow(ctxt, s, s.Size)
   205  	r := Addrel(s)
   206  	r.Sym = t
   207  	r.Off = int32(i)
   208  	r.Siz = 4
   209  	r.Type = obj.R_ADDR
   210  	r.Add = add
   211  	return i + int64(r.Siz)
   212  }
   213  
   214  /*
   215   * divide-and-conquer list-link
   216   * sort of LSym* structures.
   217   * Used for the data block.
   218   */
   219  func datcmp(s1 *LSym, s2 *LSym) int {
   220  	if s1.Type != s2.Type {
   221  		return int(s1.Type) - int(s2.Type)
   222  	}
   223  
   224  	// For ppc64, we want to interleave the .got and .toc sections
   225  	// from input files.  Both are type SELFGOT, so in that case
   226  	// fall through to the name comparison (conveniently, .got
   227  	// sorts before .toc).
   228  	if s1.Type != obj.SELFGOT && s1.Size != s2.Size {
   229  		if s1.Size < s2.Size {
   230  			return -1
   231  		}
   232  		return +1
   233  	}
   234  
   235  	return stringsCompare(s1.Name, s2.Name)
   236  }
   237  
   238  func listnextp(s *LSym) **LSym {
   239  	return &s.Next
   240  }
   241  
   242  func listsubp(s *LSym) **LSym {
   243  	return &s.Sub
   244  }
   245  
   246  func listsort(l *LSym, cmp func(*LSym, *LSym) int, nextp func(*LSym) **LSym) *LSym {
   247  	if l == nil || *nextp(l) == nil {
   248  		return l
   249  	}
   250  
   251  	l1 := l
   252  	l2 := l
   253  	for {
   254  		l2 = *nextp(l2)
   255  		if l2 == nil {
   256  			break
   257  		}
   258  		l2 = *nextp(l2)
   259  		if l2 == nil {
   260  			break
   261  		}
   262  		l1 = *nextp(l1)
   263  	}
   264  
   265  	l2 = *nextp(l1)
   266  	*nextp(l1) = nil
   267  	l1 = listsort(l, cmp, nextp)
   268  	l2 = listsort(l2, cmp, nextp)
   269  
   270  	/* set up lead element */
   271  	if cmp(l1, l2) < 0 {
   272  		l = l1
   273  		l1 = *nextp(l1)
   274  	} else {
   275  		l = l2
   276  		l2 = *nextp(l2)
   277  	}
   278  
   279  	le := l
   280  
   281  	for {
   282  		if l1 == nil {
   283  			for l2 != nil {
   284  				*nextp(le) = l2
   285  				le = l2
   286  				l2 = *nextp(l2)
   287  			}
   288  
   289  			*nextp(le) = nil
   290  			break
   291  		}
   292  
   293  		if l2 == nil {
   294  			for l1 != nil {
   295  				*nextp(le) = l1
   296  				le = l1
   297  				l1 = *nextp(l1)
   298  			}
   299  
   300  			break
   301  		}
   302  
   303  		if cmp(l1, l2) < 0 {
   304  			*nextp(le) = l1
   305  			le = l1
   306  			l1 = *nextp(l1)
   307  		} else {
   308  			*nextp(le) = l2
   309  			le = l2
   310  			l2 = *nextp(l2)
   311  		}
   312  	}
   313  
   314  	*nextp(le) = nil
   315  	return l
   316  }
   317  
   318  func relocsym(s *LSym) {
   319  	var r *Reloc
   320  	var rs *LSym
   321  	var i16 int16
   322  	var off int32
   323  	var siz int32
   324  	var fl int32
   325  	var o int64
   326  
   327  	Ctxt.Cursym = s
   328  	for ri := int32(0); ri < int32(len(s.R)); ri++ {
   329  		r = &s.R[ri]
   330  		r.Done = 1
   331  		off = r.Off
   332  		siz = int32(r.Siz)
   333  		if off < 0 || off+siz > int32(len(s.P)) {
   334  			Diag("%s: invalid relocation %d+%d not in [%d,%d)", s.Name, off, siz, 0, len(s.P))
   335  			continue
   336  		}
   337  
   338  		if r.Sym != nil && (r.Sym.Type&(obj.SMASK|obj.SHIDDEN) == 0 || r.Sym.Type&obj.SMASK == obj.SXREF) {
   339  			// When putting the runtime but not main into a shared library
   340  			// these symbols are undefined and that's OK.
   341  			if Buildmode == BuildmodeShared && (r.Sym.Name == "main.main" || r.Sym.Name == "main.init") {
   342  				r.Sym.Type = obj.SDYNIMPORT
   343  			} else {
   344  				Diag("%s: not defined", r.Sym.Name)
   345  				continue
   346  			}
   347  		}
   348  
   349  		if r.Type >= 256 {
   350  			continue
   351  		}
   352  		if r.Siz == 0 { // informational relocation - no work to do
   353  			continue
   354  		}
   355  
   356  		// We need to be able to reference dynimport symbols when linking against
   357  		// shared libraries, and Solaris needs it always
   358  		if HEADTYPE != obj.Hsolaris && r.Sym != nil && r.Sym.Type == obj.SDYNIMPORT && !DynlinkingGo() {
   359  			if !(Thearch.Thechar == '9' && Linkmode == LinkExternal && r.Sym.Name == ".TOC.") {
   360  				Diag("unhandled relocation for %s (type %d rtype %d)", r.Sym.Name, r.Sym.Type, r.Type)
   361  			}
   362  		}
   363  		if r.Sym != nil && r.Sym.Type != obj.STLSBSS && !r.Sym.Reachable {
   364  			Diag("unreachable sym in relocation: %s %s", s.Name, r.Sym.Name)
   365  		}
   366  
   367  		switch r.Type {
   368  		default:
   369  			switch siz {
   370  			default:
   371  				Diag("bad reloc size %#x for %s", uint32(siz), r.Sym.Name)
   372  			case 1:
   373  				o = int64(s.P[off])
   374  			case 2:
   375  				o = int64(Ctxt.Arch.ByteOrder.Uint16(s.P[off:]))
   376  			case 4:
   377  				o = int64(Ctxt.Arch.ByteOrder.Uint32(s.P[off:]))
   378  			case 8:
   379  				o = int64(Ctxt.Arch.ByteOrder.Uint64(s.P[off:]))
   380  			}
   381  			if Thearch.Archreloc(r, s, &o) < 0 {
   382  				Diag("unknown reloc %d", r.Type)
   383  			}
   384  
   385  		case obj.R_TLS_LE:
   386  			isAndroidX86 := goos == "android" && (Thearch.Thechar == '6' || Thearch.Thechar == '8')
   387  
   388  			if Linkmode == LinkExternal && Iself && HEADTYPE != obj.Hopenbsd && !isAndroidX86 {
   389  				r.Done = 0
   390  				if r.Sym == nil {
   391  					r.Sym = Ctxt.Tlsg
   392  				}
   393  				r.Xsym = r.Sym
   394  				r.Xadd = r.Add
   395  				o = 0
   396  				if Thearch.Thechar != '6' {
   397  					o = r.Add
   398  				}
   399  				break
   400  			}
   401  
   402  			if Iself && Thearch.Thechar == '5' {
   403  				// On ELF ARM, the thread pointer is 8 bytes before
   404  				// the start of the thread-local data block, so add 8
   405  				// to the actual TLS offset (r->sym->value).
   406  				// This 8 seems to be a fundamental constant of
   407  				// ELF on ARM (or maybe Glibc on ARM); it is not
   408  				// related to the fact that our own TLS storage happens
   409  				// to take up 8 bytes.
   410  				o = 8 + r.Sym.Value
   411  			} else if Iself || Ctxt.Headtype == obj.Hplan9 || Ctxt.Headtype == obj.Hdarwin || isAndroidX86 {
   412  				o = int64(Ctxt.Tlsoffset) + r.Add
   413  			} else if Ctxt.Headtype == obj.Hwindows {
   414  				o = r.Add
   415  			} else {
   416  				log.Fatalf("unexpected R_TLS_LE relocation for %s", Headstr(Ctxt.Headtype))
   417  			}
   418  
   419  		case obj.R_TLS_IE:
   420  			isAndroidX86 := goos == "android" && (Thearch.Thechar == '6' || Thearch.Thechar == '8')
   421  
   422  			if Linkmode == LinkExternal && Iself && HEADTYPE != obj.Hopenbsd && !isAndroidX86 {
   423  				r.Done = 0
   424  				if r.Sym == nil {
   425  					r.Sym = Ctxt.Tlsg
   426  				}
   427  				r.Xsym = r.Sym
   428  				r.Xadd = r.Add
   429  				o = 0
   430  				if Thearch.Thechar != '6' {
   431  					o = r.Add
   432  				}
   433  				break
   434  			}
   435  			log.Fatalf("cannot handle R_TLS_IE when linking internally")
   436  
   437  		case obj.R_ADDR:
   438  			if Linkmode == LinkExternal && r.Sym.Type != obj.SCONST {
   439  				r.Done = 0
   440  
   441  				// set up addend for eventual relocation via outer symbol.
   442  				rs = r.Sym
   443  
   444  				r.Xadd = r.Add
   445  				for rs.Outer != nil {
   446  					r.Xadd += Symaddr(rs) - Symaddr(rs.Outer)
   447  					rs = rs.Outer
   448  				}
   449  
   450  				if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil {
   451  					Diag("missing section for %s", rs.Name)
   452  				}
   453  				r.Xsym = rs
   454  
   455  				o = r.Xadd
   456  				if Iself {
   457  					if Thearch.Thechar == '6' {
   458  						o = 0
   459  					}
   460  				} else if HEADTYPE == obj.Hdarwin {
   461  					// ld64 for arm64 has a bug where if the address pointed to by o exists in the
   462  					// symbol table (dynid >= 0), or is inside a symbol that exists in the symbol
   463  					// table, then it will add o twice into the relocated value.
   464  					// The workaround is that on arm64 don't ever add symaddr to o and always use
   465  					// extern relocation by requiring rs->dynid >= 0.
   466  					if rs.Type != obj.SHOSTOBJ {
   467  						if Thearch.Thechar == '7' && rs.Dynid < 0 {
   468  							Diag("R_ADDR reloc to %s+%d is not supported on darwin/arm64", rs.Name, o)
   469  						}
   470  						if Thearch.Thechar != '7' {
   471  							o += Symaddr(rs)
   472  						}
   473  					}
   474  				} else if HEADTYPE == obj.Hwindows {
   475  					// nothing to do
   476  				} else {
   477  					Diag("unhandled pcrel relocation for %s", headstring)
   478  				}
   479  
   480  				break
   481  			}
   482  
   483  			o = Symaddr(r.Sym) + r.Add
   484  
   485  			// On amd64, 4-byte offsets will be sign-extended, so it is impossible to
   486  			// access more than 2GB of static data; fail at link time is better than
   487  			// fail at runtime. See https://golang.org/issue/7980.
   488  			// Instead of special casing only amd64, we treat this as an error on all
   489  			// 64-bit architectures so as to be future-proof.
   490  			if int32(o) < 0 && Thearch.Ptrsize > 4 && siz == 4 {
   491  				Diag("non-pc-relative relocation address is too big: %#x (%#x + %#x)", uint64(o), Symaddr(r.Sym), r.Add)
   492  				errorexit()
   493  			}
   494  
   495  			// r->sym can be null when CALL $(constant) is transformed from absolute PC to relative PC call.
   496  		case obj.R_CALL, obj.R_GOTPCREL, obj.R_PCREL:
   497  			if Linkmode == LinkExternal && r.Sym != nil && r.Sym.Type != obj.SCONST && (r.Sym.Sect != Ctxt.Cursym.Sect || r.Type == obj.R_GOTPCREL) {
   498  				r.Done = 0
   499  
   500  				// set up addend for eventual relocation via outer symbol.
   501  				rs = r.Sym
   502  
   503  				r.Xadd = r.Add
   504  				for rs.Outer != nil {
   505  					r.Xadd += Symaddr(rs) - Symaddr(rs.Outer)
   506  					rs = rs.Outer
   507  				}
   508  
   509  				r.Xadd -= int64(r.Siz) // relative to address after the relocated chunk
   510  				if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil {
   511  					Diag("missing section for %s", rs.Name)
   512  				}
   513  				r.Xsym = rs
   514  
   515  				o = r.Xadd
   516  				if Iself {
   517  					if Thearch.Thechar == '6' {
   518  						o = 0
   519  					}
   520  				} else if HEADTYPE == obj.Hdarwin {
   521  					if r.Type == obj.R_CALL {
   522  						if rs.Type != obj.SHOSTOBJ {
   523  							o += int64(uint64(Symaddr(rs)) - rs.Sect.Vaddr)
   524  						}
   525  						o -= int64(r.Off) // relative to section offset, not symbol
   526  					} else {
   527  						o += int64(r.Siz)
   528  					}
   529  				} else if HEADTYPE == obj.Hwindows && Thearch.Thechar == '6' { // only amd64 needs PCREL
   530  					// PE/COFF's PC32 relocation uses the address after the relocated
   531  					// bytes as the base. Compensate by skewing the addend.
   532  					o += int64(r.Siz)
   533  					// GNU ld always add VirtualAddress of the .text section to the
   534  					// relocated address, compensate that.
   535  					o -= int64(s.Sect.Vaddr - PEBASE)
   536  				} else {
   537  					Diag("unhandled pcrel relocation for %s", headstring)
   538  				}
   539  
   540  				break
   541  			}
   542  
   543  			o = 0
   544  			if r.Sym != nil {
   545  				o += Symaddr(r.Sym)
   546  			}
   547  
   548  			// NOTE: The (int32) cast on the next line works around a bug in Plan 9's 8c
   549  			// compiler. The expression s->value + r->off + r->siz is int32 + int32 +
   550  			// uchar, and Plan 9 8c incorrectly treats the expression as type uint32
   551  			// instead of int32, causing incorrect values when sign extended for adding
   552  			// to o. The bug only occurs on Plan 9, because this C program is compiled by
   553  			// the standard host compiler (gcc on most other systems).
   554  			o += r.Add - (s.Value + int64(r.Off) + int64(int32(r.Siz)))
   555  
   556  		case obj.R_SIZE:
   557  			o = r.Sym.Size + r.Add
   558  		}
   559  
   560  		if r.Variant != RV_NONE {
   561  			o = Thearch.Archrelocvariant(r, s, o)
   562  		}
   563  
   564  		if false {
   565  			nam := "<nil>"
   566  			if r.Sym != nil {
   567  				nam = r.Sym.Name
   568  			}
   569  			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)
   570  		}
   571  		switch siz {
   572  		default:
   573  			Ctxt.Cursym = s
   574  			Diag("bad reloc size %#x for %s", uint32(siz), r.Sym.Name)
   575  			fallthrough
   576  
   577  			// TODO(rsc): Remove.
   578  		case 1:
   579  			s.P[off] = byte(int8(o))
   580  
   581  		case 2:
   582  			if o != int64(int16(o)) {
   583  				Diag("relocation address is too big: %#x", o)
   584  			}
   585  			i16 = int16(o)
   586  			Ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16))
   587  
   588  		case 4:
   589  			if r.Type == obj.R_PCREL || r.Type == obj.R_CALL {
   590  				if o != int64(int32(o)) {
   591  					Diag("pc-relative relocation address is too big: %#x", o)
   592  				}
   593  			} else {
   594  				if o != int64(int32(o)) && o != int64(uint32(o)) {
   595  					Diag("non-pc-relative relocation address is too big: %#x", uint64(o))
   596  				}
   597  			}
   598  
   599  			fl = int32(o)
   600  			Ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl))
   601  
   602  		case 8:
   603  			Ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o))
   604  		}
   605  	}
   606  }
   607  
   608  func reloc() {
   609  	if Debug['v'] != 0 {
   610  		fmt.Fprintf(&Bso, "%5.2f reloc\n", obj.Cputime())
   611  	}
   612  	Bso.Flush()
   613  
   614  	for s := Ctxt.Textp; s != nil; s = s.Next {
   615  		relocsym(s)
   616  	}
   617  	for s := datap; s != nil; s = s.Next {
   618  		relocsym(s)
   619  	}
   620  }
   621  
   622  func dynrelocsym(s *LSym) {
   623  	if HEADTYPE == obj.Hwindows && Linkmode != LinkExternal {
   624  		rel := Linklookup(Ctxt, ".rel", 0)
   625  		if s == rel {
   626  			return
   627  		}
   628  		var r *Reloc
   629  		var targ *LSym
   630  		for ri := 0; ri < len(s.R); ri++ {
   631  			r = &s.R[ri]
   632  			targ = r.Sym
   633  			if targ == nil {
   634  				continue
   635  			}
   636  			if !targ.Reachable {
   637  				Diag("internal inconsistency: dynamic symbol %s is not reachable.", targ.Name)
   638  			}
   639  			if r.Sym.Plt == -2 && r.Sym.Got != -2 { // make dynimport JMP table for PE object files.
   640  				targ.Plt = int32(rel.Size)
   641  				r.Sym = rel
   642  				r.Add = int64(targ.Plt)
   643  
   644  				// jmp *addr
   645  				if Thearch.Thechar == '8' {
   646  					Adduint8(Ctxt, rel, 0xff)
   647  					Adduint8(Ctxt, rel, 0x25)
   648  					Addaddr(Ctxt, rel, targ)
   649  					Adduint8(Ctxt, rel, 0x90)
   650  					Adduint8(Ctxt, rel, 0x90)
   651  				} else {
   652  					Adduint8(Ctxt, rel, 0xff)
   653  					Adduint8(Ctxt, rel, 0x24)
   654  					Adduint8(Ctxt, rel, 0x25)
   655  					addaddrplus4(Ctxt, rel, targ, 0)
   656  					Adduint8(Ctxt, rel, 0x90)
   657  				}
   658  			} else if r.Sym.Plt >= 0 {
   659  				r.Sym = rel
   660  				r.Add = int64(targ.Plt)
   661  			}
   662  		}
   663  
   664  		return
   665  	}
   666  
   667  	var r *Reloc
   668  	for ri := 0; ri < len(s.R); ri++ {
   669  		r = &s.R[ri]
   670  		if r.Sym != nil && r.Sym.Type == obj.SDYNIMPORT || r.Type >= 256 {
   671  			if r.Sym != nil && !r.Sym.Reachable {
   672  				Diag("internal inconsistency: dynamic symbol %s is not reachable.", r.Sym.Name)
   673  			}
   674  			Thearch.Adddynrel(s, r)
   675  		}
   676  	}
   677  }
   678  
   679  func dynreloc() {
   680  	// -d suppresses dynamic loader format, so we may as well not
   681  	// compute these sections or mark their symbols as reachable.
   682  	if Debug['d'] != 0 && HEADTYPE != obj.Hwindows {
   683  		return
   684  	}
   685  	if Debug['v'] != 0 {
   686  		fmt.Fprintf(&Bso, "%5.2f reloc\n", obj.Cputime())
   687  	}
   688  	Bso.Flush()
   689  
   690  	for s := Ctxt.Textp; s != nil; s = s.Next {
   691  		dynrelocsym(s)
   692  	}
   693  	for s := datap; s != nil; s = s.Next {
   694  		dynrelocsym(s)
   695  	}
   696  	if Iself {
   697  		elfdynhash()
   698  	}
   699  }
   700  
   701  func blk(start *LSym, addr int64, size int64) {
   702  	var sym *LSym
   703  
   704  	for sym = start; sym != nil; sym = sym.Next {
   705  		if sym.Type&obj.SSUB == 0 && sym.Value >= addr {
   706  			break
   707  		}
   708  	}
   709  
   710  	eaddr := addr + size
   711  	var ep []byte
   712  	var p []byte
   713  	for ; sym != nil; sym = sym.Next {
   714  		if sym.Type&obj.SSUB != 0 {
   715  			continue
   716  		}
   717  		if sym.Value >= eaddr {
   718  			break
   719  		}
   720  		Ctxt.Cursym = sym
   721  		if sym.Value < addr {
   722  			Diag("phase error: addr=%#x but sym=%#x type=%d", int64(addr), int64(sym.Value), sym.Type)
   723  			errorexit()
   724  		}
   725  
   726  		for ; addr < sym.Value; addr++ {
   727  			Cput(0)
   728  		}
   729  		p = sym.P
   730  		ep = p[len(sym.P):]
   731  		for -cap(p) < -cap(ep) {
   732  			Cput(uint8(p[0]))
   733  			p = p[1:]
   734  		}
   735  		addr += int64(len(sym.P))
   736  		for ; addr < sym.Value+sym.Size; addr++ {
   737  			Cput(0)
   738  		}
   739  		if addr != sym.Value+sym.Size {
   740  			Diag("phase error: addr=%#x value+size=%#x", int64(addr), int64(sym.Value)+sym.Size)
   741  			errorexit()
   742  		}
   743  
   744  		if sym.Value+sym.Size >= eaddr {
   745  			break
   746  		}
   747  	}
   748  
   749  	for ; addr < eaddr; addr++ {
   750  		Cput(0)
   751  	}
   752  	Cflush()
   753  }
   754  
   755  func Codeblk(addr int64, size int64) {
   756  	if Debug['a'] != 0 {
   757  		fmt.Fprintf(&Bso, "codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos())
   758  	}
   759  
   760  	blk(Ctxt.Textp, addr, size)
   761  
   762  	/* again for printing */
   763  	if Debug['a'] == 0 {
   764  		return
   765  	}
   766  
   767  	var sym *LSym
   768  	for sym = Ctxt.Textp; sym != nil; sym = sym.Next {
   769  		if !sym.Reachable {
   770  			continue
   771  		}
   772  		if sym.Value >= addr {
   773  			break
   774  		}
   775  	}
   776  
   777  	eaddr := addr + size
   778  	var q []byte
   779  	for ; sym != nil; sym = sym.Next {
   780  		if !sym.Reachable {
   781  			continue
   782  		}
   783  		if sym.Value >= eaddr {
   784  			break
   785  		}
   786  
   787  		if addr < sym.Value {
   788  			fmt.Fprintf(&Bso, "%-20s %.8x|", "_", uint64(int64(addr)))
   789  			for ; addr < sym.Value; addr++ {
   790  				fmt.Fprintf(&Bso, " %.2x", 0)
   791  			}
   792  			fmt.Fprintf(&Bso, "\n")
   793  		}
   794  
   795  		fmt.Fprintf(&Bso, "%.6x\t%-20s\n", uint64(int64(addr)), sym.Name)
   796  		q = sym.P
   797  
   798  		for len(q) >= 16 {
   799  			fmt.Fprintf(&Bso, "%.6x\t% x\n", uint64(addr), q[:16])
   800  			addr += 16
   801  			q = q[16:]
   802  		}
   803  
   804  		if len(q) > 0 {
   805  			fmt.Fprintf(&Bso, "%.6x\t% x\n", uint64(addr), q)
   806  			addr += int64(len(q))
   807  		}
   808  	}
   809  
   810  	if addr < eaddr {
   811  		fmt.Fprintf(&Bso, "%-20s %.8x|", "_", uint64(int64(addr)))
   812  		for ; addr < eaddr; addr++ {
   813  			fmt.Fprintf(&Bso, " %.2x", 0)
   814  		}
   815  	}
   816  
   817  	Bso.Flush()
   818  }
   819  
   820  func Datblk(addr int64, size int64) {
   821  	if Debug['a'] != 0 {
   822  		fmt.Fprintf(&Bso, "datblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos())
   823  	}
   824  
   825  	blk(datap, addr, size)
   826  
   827  	/* again for printing */
   828  	if Debug['a'] == 0 {
   829  		return
   830  	}
   831  
   832  	var sym *LSym
   833  	for sym = datap; sym != nil; sym = sym.Next {
   834  		if sym.Value >= addr {
   835  			break
   836  		}
   837  	}
   838  
   839  	eaddr := addr + size
   840  	var ep []byte
   841  	var i int64
   842  	var p []byte
   843  	var r *Reloc
   844  	var rsname string
   845  	var typ string
   846  	for ; sym != nil; sym = sym.Next {
   847  		if sym.Value >= eaddr {
   848  			break
   849  		}
   850  		if addr < sym.Value {
   851  			fmt.Fprintf(&Bso, "\t%.8x| 00 ...\n", uint64(addr))
   852  			addr = sym.Value
   853  		}
   854  
   855  		fmt.Fprintf(&Bso, "%s\n\t%.8x|", sym.Name, uint(addr))
   856  		p = sym.P
   857  		ep = p[len(sym.P):]
   858  		for -cap(p) < -cap(ep) {
   859  			if -cap(p) > -cap(sym.P) && int(-cap(p)+cap(sym.P))%16 == 0 {
   860  				fmt.Fprintf(&Bso, "\n\t%.8x|", uint(addr+int64(-cap(p)+cap(sym.P))))
   861  			}
   862  			fmt.Fprintf(&Bso, " %.2x", p[0])
   863  			p = p[1:]
   864  		}
   865  
   866  		addr += int64(len(sym.P))
   867  		for ; addr < sym.Value+sym.Size; addr++ {
   868  			fmt.Fprintf(&Bso, " %.2x", 0)
   869  		}
   870  		fmt.Fprintf(&Bso, "\n")
   871  
   872  		if Linkmode == LinkExternal {
   873  			for i = 0; i < int64(len(sym.R)); i++ {
   874  				r = &sym.R[i]
   875  				rsname = ""
   876  				if r.Sym != nil {
   877  					rsname = r.Sym.Name
   878  				}
   879  				typ = "?"
   880  				switch r.Type {
   881  				case obj.R_ADDR:
   882  					typ = "addr"
   883  
   884  				case obj.R_PCREL:
   885  					typ = "pcrel"
   886  
   887  				case obj.R_CALL:
   888  					typ = "call"
   889  				}
   890  
   891  				fmt.Fprintf(&Bso, "\treloc %.8x/%d %s %s+%#x [%#x]\n", uint(sym.Value+int64(r.Off)), r.Siz, typ, rsname, int64(r.Add), int64(r.Sym.Value+r.Add))
   892  			}
   893  		}
   894  	}
   895  
   896  	if addr < eaddr {
   897  		fmt.Fprintf(&Bso, "\t%.8x| 00 ...\n", uint(addr))
   898  	}
   899  	fmt.Fprintf(&Bso, "\t%.8x|\n", uint(eaddr))
   900  }
   901  
   902  func strnput(s string, n int) {
   903  	for ; n > 0 && s != ""; s = s[1:] {
   904  		Cput(uint8(s[0]))
   905  		n--
   906  	}
   907  
   908  	for n > 0 {
   909  		Cput(0)
   910  		n--
   911  	}
   912  }
   913  
   914  var strdata []*LSym
   915  
   916  func addstrdata1(arg string) {
   917  	i := strings.Index(arg, "=")
   918  	if i < 0 {
   919  		Exitf("-X flag requires argument of the form importpath.name=value")
   920  	}
   921  	addstrdata(arg[:i], arg[i+1:])
   922  }
   923  
   924  func addstrdata(name string, value string) {
   925  	p := fmt.Sprintf("%s.str", name)
   926  	sp := Linklookup(Ctxt, p, 0)
   927  
   928  	Addstring(sp, value)
   929  	sp.Type = obj.SRODATA
   930  
   931  	s := Linklookup(Ctxt, name, 0)
   932  	s.Size = 0
   933  	s.Dupok = 1
   934  	reachable := s.Reachable
   935  	Addaddr(Ctxt, s, sp)
   936  	adduintxx(Ctxt, s, uint64(len(value)), Thearch.Ptrsize)
   937  
   938  	// addstring, addaddr, etc., mark the symbols as reachable.
   939  	// In this case that is not necessarily true, so stick to what
   940  	// we know before entering this function.
   941  	s.Reachable = reachable
   942  
   943  	strdata = append(strdata, s)
   944  
   945  	sp.Reachable = reachable
   946  }
   947  
   948  func checkstrdata() {
   949  	for _, s := range strdata {
   950  		if s.Type == obj.STEXT {
   951  			Diag("cannot use -X with text symbol %s", s.Name)
   952  		} else if s.Gotype != nil && s.Gotype.Name != "type.string" {
   953  			Diag("cannot use -X with non-string symbol %s", s.Name)
   954  		}
   955  	}
   956  }
   957  
   958  func Addstring(s *LSym, str string) int64 {
   959  	if s.Type == 0 {
   960  		s.Type = obj.SNOPTRDATA
   961  	}
   962  	s.Reachable = true
   963  	r := int32(s.Size)
   964  	n := len(str) + 1
   965  	if s.Name == ".shstrtab" {
   966  		elfsetstring(str, int(r))
   967  	}
   968  	Symgrow(Ctxt, s, int64(r)+int64(n))
   969  	copy(s.P[r:], str)
   970  	s.P[int(r)+len(str)] = 0
   971  	s.Size += int64(n)
   972  	return int64(r)
   973  }
   974  
   975  // addgostring adds str, as a Go string value, to s. symname is the name of the
   976  // symbol used to define the string data and must be unique per linked object.
   977  func addgostring(s *LSym, symname, str string) {
   978  	sym := Linklookup(Ctxt, symname, 0)
   979  	if sym.Type != obj.Sxxx {
   980  		Diag("duplicate symname in addgostring: %s", symname)
   981  	}
   982  	sym.Reachable = true
   983  	sym.Local = true
   984  	sym.Type = obj.SRODATA
   985  	sym.Size = int64(len(str))
   986  	sym.P = []byte(str)
   987  	Addaddr(Ctxt, s, sym)
   988  	adduint(Ctxt, s, uint64(len(str)))
   989  }
   990  
   991  func addinitarrdata(s *LSym) {
   992  	p := s.Name + ".ptr"
   993  	sp := Linklookup(Ctxt, p, 0)
   994  	sp.Type = obj.SINITARR
   995  	sp.Size = 0
   996  	sp.Dupok = 1
   997  	Addaddr(Ctxt, sp, s)
   998  }
   999  
  1000  func dosymtype() {
  1001  	for s := Ctxt.Allsym; s != nil; s = s.Allsym {
  1002  		if len(s.P) > 0 {
  1003  			if s.Type == obj.SBSS {
  1004  				s.Type = obj.SDATA
  1005  			}
  1006  			if s.Type == obj.SNOPTRBSS {
  1007  				s.Type = obj.SNOPTRDATA
  1008  			}
  1009  		}
  1010  		// Create a new entry in the .init_array section that points to the
  1011  		// library initializer function.
  1012  		switch Buildmode {
  1013  		case BuildmodeCArchive, BuildmodeCShared:
  1014  			if s.Name == INITENTRY {
  1015  				addinitarrdata(s)
  1016  			}
  1017  		}
  1018  	}
  1019  }
  1020  
  1021  func symalign(s *LSym) int32 {
  1022  	if s.Align != 0 {
  1023  		return s.Align
  1024  	}
  1025  
  1026  	align := int32(Thearch.Maxalign)
  1027  	for int64(align) > s.Size && align > 1 {
  1028  		align >>= 1
  1029  	}
  1030  	if align < s.Align {
  1031  		align = s.Align
  1032  	}
  1033  	return align
  1034  }
  1035  
  1036  func aligndatsize(datsize int64, s *LSym) int64 {
  1037  	return Rnd(datsize, int64(symalign(s)))
  1038  }
  1039  
  1040  // maxalign returns the maximum required alignment for
  1041  // the list of symbols s; the list stops when s->type exceeds type.
  1042  func maxalign(s *LSym, type_ int) int32 {
  1043  	var align int32
  1044  
  1045  	max := int32(0)
  1046  	for ; s != nil && int(s.Type) <= type_; s = s.Next {
  1047  		align = symalign(s)
  1048  		if max < align {
  1049  			max = align
  1050  		}
  1051  	}
  1052  
  1053  	return max
  1054  }
  1055  
  1056  const debugGCProg = false
  1057  
  1058  type GCProg struct {
  1059  	sym *LSym
  1060  	w   gcprog.Writer
  1061  }
  1062  
  1063  func (p *GCProg) Init(name string) {
  1064  	p.sym = Linklookup(Ctxt, name, 0)
  1065  	p.w.Init(p.writeByte)
  1066  	if debugGCProg {
  1067  		fmt.Fprintf(os.Stderr, "ld: start GCProg %s\n", name)
  1068  		p.w.Debug(os.Stderr)
  1069  	}
  1070  }
  1071  
  1072  func (p *GCProg) writeByte(x byte) {
  1073  	Adduint8(Ctxt, p.sym, x)
  1074  }
  1075  
  1076  func (p *GCProg) End(size int64) {
  1077  	p.w.ZeroUntil(size / int64(Thearch.Ptrsize))
  1078  	p.w.End()
  1079  	if debugGCProg {
  1080  		fmt.Fprintf(os.Stderr, "ld: end GCProg\n")
  1081  	}
  1082  }
  1083  
  1084  func (p *GCProg) AddSym(s *LSym) {
  1085  	typ := s.Gotype
  1086  	// Things without pointers should be in SNOPTRDATA or SNOPTRBSS;
  1087  	// everything we see should have pointers and should therefore have a type.
  1088  	if typ == nil {
  1089  		Diag("missing Go type information for global symbol: %s size %d", s.Name, int(s.Size))
  1090  		return
  1091  	}
  1092  
  1093  	ptrsize := int64(Thearch.Ptrsize)
  1094  	nptr := decodetype_ptrdata(typ) / ptrsize
  1095  
  1096  	if debugGCProg {
  1097  		fmt.Fprintf(os.Stderr, "gcprog sym: %s at %d (ptr=%d+%d)\n", s.Name, s.Value, s.Value/ptrsize, nptr)
  1098  	}
  1099  
  1100  	if decodetype_usegcprog(typ) == 0 {
  1101  		// Copy pointers from mask into program.
  1102  		mask := decodetype_gcmask(typ)
  1103  		for i := int64(0); i < nptr; i++ {
  1104  			if (mask[i/8]>>uint(i%8))&1 != 0 {
  1105  				p.w.Ptr(s.Value/ptrsize + i)
  1106  			}
  1107  		}
  1108  		return
  1109  	}
  1110  
  1111  	// Copy program.
  1112  	prog := decodetype_gcprog(typ)
  1113  	p.w.ZeroUntil(s.Value / ptrsize)
  1114  	p.w.Append(prog[4:], nptr)
  1115  }
  1116  
  1117  func growdatsize(datsizep *int64, s *LSym) {
  1118  	datsize := *datsizep
  1119  	const cutoff int64 = 2e9 // 2 GB (or so; looks better in errors than 2^31)
  1120  	switch {
  1121  	case s.Size < 0:
  1122  		Diag("%s: negative size (%d bytes)", s.Name, s.Size)
  1123  	case s.Size > cutoff:
  1124  		Diag("%s: symbol too large (%d bytes)", s.Name, s.Size)
  1125  	case datsize <= cutoff && datsize+s.Size > cutoff:
  1126  		Diag("%s: too much data (over %d bytes)", s.Name, cutoff)
  1127  	}
  1128  	*datsizep = datsize + s.Size
  1129  }
  1130  
  1131  func dodata() {
  1132  	if Debug['v'] != 0 {
  1133  		fmt.Fprintf(&Bso, "%5.2f dodata\n", obj.Cputime())
  1134  	}
  1135  	Bso.Flush()
  1136  
  1137  	var last *LSym
  1138  	datap = nil
  1139  
  1140  	for s := Ctxt.Allsym; s != nil; s = s.Allsym {
  1141  		if !s.Reachable || s.Special != 0 {
  1142  			continue
  1143  		}
  1144  		if obj.STEXT < s.Type && s.Type < obj.SXREF {
  1145  			if s.Onlist != 0 {
  1146  				log.Fatalf("symbol %s listed multiple times", s.Name)
  1147  			}
  1148  			s.Onlist = 1
  1149  			if last == nil {
  1150  				datap = s
  1151  			} else {
  1152  				last.Next = s
  1153  			}
  1154  			s.Next = nil
  1155  			last = s
  1156  		}
  1157  	}
  1158  
  1159  	for s := datap; s != nil; s = s.Next {
  1160  		if int64(len(s.P)) > s.Size {
  1161  			Diag("%s: initialize bounds (%d < %d)", s.Name, int64(s.Size), len(s.P))
  1162  		}
  1163  	}
  1164  
  1165  	/*
  1166  	 * now that we have the datap list, but before we start
  1167  	 * to assign addresses, record all the necessary
  1168  	 * dynamic relocations.  these will grow the relocation
  1169  	 * symbol, which is itself data.
  1170  	 *
  1171  	 * on darwin, we need the symbol table numbers for dynreloc.
  1172  	 */
  1173  	if HEADTYPE == obj.Hdarwin {
  1174  		machosymorder()
  1175  	}
  1176  	dynreloc()
  1177  
  1178  	/* some symbols may no longer belong in datap (Mach-O) */
  1179  	var l **LSym
  1180  	var s *LSym
  1181  	for l = &datap; ; {
  1182  		s = *l
  1183  		if s == nil {
  1184  			break
  1185  		}
  1186  
  1187  		if s.Type <= obj.STEXT || obj.SXREF <= s.Type {
  1188  			*l = s.Next
  1189  		} else {
  1190  			l = &s.Next
  1191  		}
  1192  	}
  1193  
  1194  	*l = nil
  1195  
  1196  	if UseRelro() {
  1197  		// "read only" data with relocations needs to go in its own section
  1198  		// when building a shared library. We do this by boosting objects of
  1199  		// type SXXX with relocations to type SXXXRELRO.
  1200  		for s := datap; s != nil; s = s.Next {
  1201  			if (s.Type >= obj.STYPE && s.Type <= obj.SFUNCTAB && len(s.R) > 0) || s.Type == obj.SGOSTRING {
  1202  				s.Type += (obj.STYPERELRO - obj.STYPE)
  1203  				if s.Outer != nil {
  1204  					s.Outer.Type = s.Type
  1205  				}
  1206  			}
  1207  		}
  1208  		// Check that we haven't made two symbols with the same .Outer into
  1209  		// different types (because references two symbols with non-nil Outer
  1210  		// become references to the outer symbol + offset it's vital that the
  1211  		// symbol and the outer end up in the same section).
  1212  		for s := datap; s != nil; s = s.Next {
  1213  			if s.Outer != nil && s.Outer.Type != s.Type {
  1214  				Diag("inconsistent types for %s and its Outer %s (%d != %d)",
  1215  					s.Name, s.Outer.Name, s.Type, s.Outer.Type)
  1216  			}
  1217  		}
  1218  
  1219  	}
  1220  
  1221  	datap = listsort(datap, datcmp, listnextp)
  1222  
  1223  	if Iself {
  1224  		// Make .rela and .rela.plt contiguous, the ELF ABI requires this
  1225  		// and Solaris actually cares.
  1226  		var relplt *LSym
  1227  		for l = &datap; *l != nil; l = &(*l).Next {
  1228  			if (*l).Name == ".rel.plt" || (*l).Name == ".rela.plt" {
  1229  				relplt = (*l)
  1230  				*l = (*l).Next
  1231  				break
  1232  			}
  1233  		}
  1234  		if relplt != nil {
  1235  			for s = datap; s != nil; s = s.Next {
  1236  				if s.Name == ".rel" || s.Name == ".rela" {
  1237  					relplt.Next = s.Next
  1238  					s.Next = relplt
  1239  				}
  1240  			}
  1241  		}
  1242  	}
  1243  
  1244  	/*
  1245  	 * allocate sections.  list is sorted by type,
  1246  	 * so we can just walk it for each piece we want to emit.
  1247  	 * segdata is processed before segtext, because we need
  1248  	 * to see all symbols in the .data and .bss sections in order
  1249  	 * to generate garbage collection information.
  1250  	 */
  1251  
  1252  	/* begin segdata */
  1253  
  1254  	/* skip symbols belonging to segtext */
  1255  	s = datap
  1256  
  1257  	for ; s != nil && s.Type < obj.SELFSECT; s = s.Next {
  1258  	}
  1259  
  1260  	/* writable ELF sections */
  1261  	datsize := int64(0)
  1262  
  1263  	var sect *Section
  1264  	for ; s != nil && s.Type < obj.SELFGOT; s = s.Next {
  1265  		sect = addsection(&Segdata, s.Name, 06)
  1266  		sect.Align = symalign(s)
  1267  		datsize = Rnd(datsize, int64(sect.Align))
  1268  		sect.Vaddr = uint64(datsize)
  1269  		s.Sect = sect
  1270  		s.Type = obj.SDATA
  1271  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1272  		growdatsize(&datsize, s)
  1273  		sect.Length = uint64(datsize) - sect.Vaddr
  1274  	}
  1275  
  1276  	/* .got (and .toc on ppc64) */
  1277  	if s.Type == obj.SELFGOT {
  1278  		sect := addsection(&Segdata, ".got", 06)
  1279  		sect.Align = maxalign(s, obj.SELFGOT)
  1280  		datsize = Rnd(datsize, int64(sect.Align))
  1281  		sect.Vaddr = uint64(datsize)
  1282  		var toc *LSym
  1283  		for ; s != nil && s.Type == obj.SELFGOT; s = s.Next {
  1284  			datsize = aligndatsize(datsize, s)
  1285  			s.Sect = sect
  1286  			s.Type = obj.SDATA
  1287  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1288  
  1289  			// Resolve .TOC. symbol for this object file (ppc64)
  1290  			toc = Linkrlookup(Ctxt, ".TOC.", int(s.Version))
  1291  
  1292  			if toc != nil {
  1293  				toc.Sect = sect
  1294  				toc.Outer = s
  1295  				toc.Sub = s.Sub
  1296  				s.Sub = toc
  1297  
  1298  				toc.Value = 0x8000
  1299  			}
  1300  
  1301  			growdatsize(&datsize, s)
  1302  		}
  1303  
  1304  		sect.Length = uint64(datsize) - sect.Vaddr
  1305  	}
  1306  
  1307  	/* pointer-free data */
  1308  	sect = addsection(&Segdata, ".noptrdata", 06)
  1309  
  1310  	sect.Align = maxalign(s, obj.SINITARR-1)
  1311  	datsize = Rnd(datsize, int64(sect.Align))
  1312  	sect.Vaddr = uint64(datsize)
  1313  	Linklookup(Ctxt, "runtime.noptrdata", 0).Sect = sect
  1314  	Linklookup(Ctxt, "runtime.enoptrdata", 0).Sect = sect
  1315  	for ; s != nil && s.Type < obj.SINITARR; s = s.Next {
  1316  		datsize = aligndatsize(datsize, s)
  1317  		s.Sect = sect
  1318  		s.Type = obj.SDATA
  1319  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1320  		growdatsize(&datsize, s)
  1321  	}
  1322  
  1323  	sect.Length = uint64(datsize) - sect.Vaddr
  1324  
  1325  	hasinitarr := Linkshared
  1326  
  1327  	/* shared library initializer */
  1328  	switch Buildmode {
  1329  	case BuildmodeCArchive, BuildmodeCShared, BuildmodeShared:
  1330  		hasinitarr = true
  1331  	}
  1332  
  1333  	if hasinitarr {
  1334  		sect := addsection(&Segdata, ".init_array", 06)
  1335  		sect.Align = maxalign(s, obj.SINITARR)
  1336  		datsize = Rnd(datsize, int64(sect.Align))
  1337  		sect.Vaddr = uint64(datsize)
  1338  		for ; s != nil && s.Type == obj.SINITARR; s = s.Next {
  1339  			datsize = aligndatsize(datsize, s)
  1340  			s.Sect = sect
  1341  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1342  			growdatsize(&datsize, s)
  1343  		}
  1344  
  1345  		sect.Length = uint64(datsize) - sect.Vaddr
  1346  	}
  1347  
  1348  	/* data */
  1349  	sect = addsection(&Segdata, ".data", 06)
  1350  	sect.Align = maxalign(s, obj.SBSS-1)
  1351  	datsize = Rnd(datsize, int64(sect.Align))
  1352  	sect.Vaddr = uint64(datsize)
  1353  	Linklookup(Ctxt, "runtime.data", 0).Sect = sect
  1354  	Linklookup(Ctxt, "runtime.edata", 0).Sect = sect
  1355  	var gc GCProg
  1356  	gc.Init("runtime.gcdata")
  1357  	for ; s != nil && s.Type < obj.SBSS; s = s.Next {
  1358  		if s.Type == obj.SINITARR {
  1359  			Ctxt.Cursym = s
  1360  			Diag("unexpected symbol type %d", s.Type)
  1361  		}
  1362  
  1363  		s.Sect = sect
  1364  		s.Type = obj.SDATA
  1365  		datsize = aligndatsize(datsize, s)
  1366  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1367  		gc.AddSym(s)
  1368  		growdatsize(&datsize, s)
  1369  	}
  1370  	sect.Length = uint64(datsize) - sect.Vaddr
  1371  	gc.End(int64(sect.Length))
  1372  
  1373  	/* bss */
  1374  	sect = addsection(&Segdata, ".bss", 06)
  1375  	sect.Align = maxalign(s, obj.SNOPTRBSS-1)
  1376  	datsize = Rnd(datsize, int64(sect.Align))
  1377  	sect.Vaddr = uint64(datsize)
  1378  	Linklookup(Ctxt, "runtime.bss", 0).Sect = sect
  1379  	Linklookup(Ctxt, "runtime.ebss", 0).Sect = sect
  1380  	gc = GCProg{}
  1381  	gc.Init("runtime.gcbss")
  1382  	for ; s != nil && s.Type < obj.SNOPTRBSS; s = s.Next {
  1383  		s.Sect = sect
  1384  		datsize = aligndatsize(datsize, s)
  1385  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1386  		gc.AddSym(s)
  1387  		growdatsize(&datsize, s)
  1388  	}
  1389  	sect.Length = uint64(datsize) - sect.Vaddr
  1390  	gc.End(int64(sect.Length))
  1391  
  1392  	/* pointer-free bss */
  1393  	sect = addsection(&Segdata, ".noptrbss", 06)
  1394  
  1395  	sect.Align = maxalign(s, obj.SNOPTRBSS)
  1396  	datsize = Rnd(datsize, int64(sect.Align))
  1397  	sect.Vaddr = uint64(datsize)
  1398  	Linklookup(Ctxt, "runtime.noptrbss", 0).Sect = sect
  1399  	Linklookup(Ctxt, "runtime.enoptrbss", 0).Sect = sect
  1400  	for ; s != nil && s.Type == obj.SNOPTRBSS; s = s.Next {
  1401  		datsize = aligndatsize(datsize, s)
  1402  		s.Sect = sect
  1403  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1404  		growdatsize(&datsize, s)
  1405  	}
  1406  
  1407  	sect.Length = uint64(datsize) - sect.Vaddr
  1408  	Linklookup(Ctxt, "runtime.end", 0).Sect = sect
  1409  
  1410  	// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
  1411  	if datsize != int64(uint32(datsize)) {
  1412  		Diag("data or bss segment too large")
  1413  	}
  1414  
  1415  	if s != nil && s.Type == obj.STLSBSS {
  1416  		if Iself && (Linkmode == LinkExternal || Debug['d'] == 0) && HEADTYPE != obj.Hopenbsd {
  1417  			sect = addsection(&Segdata, ".tbss", 06)
  1418  			sect.Align = int32(Thearch.Ptrsize)
  1419  			sect.Vaddr = 0
  1420  		} else {
  1421  			sect = nil
  1422  		}
  1423  		datsize = 0
  1424  
  1425  		for ; s != nil && s.Type == obj.STLSBSS; s = s.Next {
  1426  			datsize = aligndatsize(datsize, s)
  1427  			s.Sect = sect
  1428  			s.Value = datsize
  1429  			growdatsize(&datsize, s)
  1430  		}
  1431  
  1432  		if sect != nil {
  1433  			sect.Length = uint64(datsize)
  1434  		}
  1435  	}
  1436  
  1437  	if s != nil {
  1438  		Ctxt.Cursym = nil
  1439  		Diag("unexpected symbol type %d for %s", s.Type, s.Name)
  1440  	}
  1441  
  1442  	/*
  1443  	 * We finished data, begin read-only data.
  1444  	 * Not all systems support a separate read-only non-executable data section.
  1445  	 * ELF systems do.
  1446  	 * OS X and Plan 9 do not.
  1447  	 * Windows PE may, but if so we have not implemented it.
  1448  	 * And if we're using external linking mode, the point is moot,
  1449  	 * since it's not our decision; that code expects the sections in
  1450  	 * segtext.
  1451  	 */
  1452  	var segro *Segment
  1453  	if Iself && Linkmode == LinkInternal {
  1454  		segro = &Segrodata
  1455  	} else {
  1456  		segro = &Segtext
  1457  	}
  1458  
  1459  	s = datap
  1460  
  1461  	datsize = 0
  1462  
  1463  	/* read-only executable ELF, Mach-O sections */
  1464  	for ; s != nil && s.Type < obj.STYPE; s = s.Next {
  1465  		sect = addsection(&Segtext, s.Name, 04)
  1466  		sect.Align = symalign(s)
  1467  		datsize = Rnd(datsize, int64(sect.Align))
  1468  		sect.Vaddr = uint64(datsize)
  1469  		s.Sect = sect
  1470  		s.Type = obj.SRODATA
  1471  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1472  		growdatsize(&datsize, s)
  1473  		sect.Length = uint64(datsize) - sect.Vaddr
  1474  	}
  1475  
  1476  	/* read-only data */
  1477  	sect = addsection(segro, ".rodata", 04)
  1478  
  1479  	sect.Align = maxalign(s, obj.STYPERELRO-1)
  1480  	datsize = Rnd(datsize, int64(sect.Align))
  1481  	sect.Vaddr = 0
  1482  	Linklookup(Ctxt, "runtime.rodata", 0).Sect = sect
  1483  	Linklookup(Ctxt, "runtime.erodata", 0).Sect = sect
  1484  	for ; s != nil && s.Type < obj.STYPERELRO; s = s.Next {
  1485  		datsize = aligndatsize(datsize, s)
  1486  		s.Sect = sect
  1487  		s.Type = obj.SRODATA
  1488  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1489  		growdatsize(&datsize, s)
  1490  	}
  1491  
  1492  	sect.Length = uint64(datsize) - sect.Vaddr
  1493  
  1494  	// There is some data that are conceptually read-only but are written to by
  1495  	// relocations. On GNU systems, we can arrange for the dynamic linker to
  1496  	// mprotect sections after relocations are applied by giving them write
  1497  	// permissions in the object file and calling them ".data.rel.ro.FOO". We
  1498  	// divide the .rodata section between actual .rodata and .data.rel.ro.rodata,
  1499  	// but for the other sections that this applies to, we just write a read-only
  1500  	// .FOO section or a read-write .data.rel.ro.FOO section depending on the
  1501  	// situation.
  1502  	// TODO(mwhudson): It would make sense to do this more widely, but it makes
  1503  	// the system linker segfault on darwin.
  1504  	relro_perms := 04
  1505  	relro_prefix := ""
  1506  
  1507  	if UseRelro() {
  1508  		relro_perms = 06
  1509  		relro_prefix = ".data.rel.ro"
  1510  		/* data only written by relocations */
  1511  		sect = addsection(segro, ".data.rel.ro", 06)
  1512  
  1513  		sect.Align = maxalign(s, obj.STYPELINK-1)
  1514  		datsize = Rnd(datsize, int64(sect.Align))
  1515  		sect.Vaddr = 0
  1516  		for ; s != nil && s.Type < obj.STYPELINK; s = s.Next {
  1517  			datsize = aligndatsize(datsize, s)
  1518  			if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect {
  1519  				Diag("s.Outer (%s) in different section from s (%s)", s.Outer.Name, s.Name)
  1520  			}
  1521  			s.Sect = sect
  1522  			s.Type = obj.SRODATA
  1523  			s.Value = int64(uint64(datsize) - sect.Vaddr)
  1524  			growdatsize(&datsize, s)
  1525  		}
  1526  
  1527  		sect.Length = uint64(datsize) - sect.Vaddr
  1528  
  1529  	}
  1530  
  1531  	/* typelink */
  1532  	sect = addsection(segro, relro_prefix+".typelink", relro_perms)
  1533  
  1534  	sect.Align = maxalign(s, obj.STYPELINK)
  1535  	datsize = Rnd(datsize, int64(sect.Align))
  1536  	sect.Vaddr = uint64(datsize)
  1537  	Linklookup(Ctxt, "runtime.typelink", 0).Sect = sect
  1538  	Linklookup(Ctxt, "runtime.etypelink", 0).Sect = sect
  1539  	for ; s != nil && s.Type == obj.STYPELINK; s = s.Next {
  1540  		datsize = aligndatsize(datsize, s)
  1541  		s.Sect = sect
  1542  		s.Type = obj.SRODATA
  1543  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1544  		growdatsize(&datsize, s)
  1545  	}
  1546  
  1547  	sect.Length = uint64(datsize) - sect.Vaddr
  1548  
  1549  	/* gosymtab */
  1550  	sect = addsection(segro, relro_prefix+".gosymtab", relro_perms)
  1551  
  1552  	sect.Align = maxalign(s, obj.SPCLNTAB-1)
  1553  	datsize = Rnd(datsize, int64(sect.Align))
  1554  	sect.Vaddr = uint64(datsize)
  1555  	Linklookup(Ctxt, "runtime.symtab", 0).Sect = sect
  1556  	Linklookup(Ctxt, "runtime.esymtab", 0).Sect = sect
  1557  	for ; s != nil && s.Type < obj.SPCLNTAB; s = s.Next {
  1558  		datsize = aligndatsize(datsize, s)
  1559  		s.Sect = sect
  1560  		s.Type = obj.SRODATA
  1561  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1562  		growdatsize(&datsize, s)
  1563  	}
  1564  
  1565  	sect.Length = uint64(datsize) - sect.Vaddr
  1566  
  1567  	/* gopclntab */
  1568  	sect = addsection(segro, relro_prefix+".gopclntab", relro_perms)
  1569  
  1570  	sect.Align = maxalign(s, obj.SELFROSECT-1)
  1571  	datsize = Rnd(datsize, int64(sect.Align))
  1572  	sect.Vaddr = uint64(datsize)
  1573  	Linklookup(Ctxt, "runtime.pclntab", 0).Sect = sect
  1574  	Linklookup(Ctxt, "runtime.epclntab", 0).Sect = sect
  1575  	for ; s != nil && s.Type < obj.SELFROSECT; s = s.Next {
  1576  		datsize = aligndatsize(datsize, s)
  1577  		s.Sect = sect
  1578  		s.Type = obj.SRODATA
  1579  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1580  		growdatsize(&datsize, s)
  1581  	}
  1582  
  1583  	sect.Length = uint64(datsize) - sect.Vaddr
  1584  
  1585  	/* read-only ELF, Mach-O sections */
  1586  	for ; s != nil && s.Type < obj.SELFSECT; s = s.Next {
  1587  		sect = addsection(segro, s.Name, 04)
  1588  		sect.Align = symalign(s)
  1589  		datsize = Rnd(datsize, int64(sect.Align))
  1590  		sect.Vaddr = uint64(datsize)
  1591  		s.Sect = sect
  1592  		s.Type = obj.SRODATA
  1593  		s.Value = int64(uint64(datsize) - sect.Vaddr)
  1594  		growdatsize(&datsize, s)
  1595  		sect.Length = uint64(datsize) - sect.Vaddr
  1596  	}
  1597  
  1598  	// 6g uses 4-byte relocation offsets, so the entire segment must fit in 32 bits.
  1599  	if datsize != int64(uint32(datsize)) {
  1600  		Diag("read-only data segment too large")
  1601  	}
  1602  
  1603  	/* number the sections */
  1604  	n := int32(1)
  1605  
  1606  	for sect := Segtext.Sect; sect != nil; sect = sect.Next {
  1607  		sect.Extnum = int16(n)
  1608  		n++
  1609  	}
  1610  	for sect := Segrodata.Sect; sect != nil; sect = sect.Next {
  1611  		sect.Extnum = int16(n)
  1612  		n++
  1613  	}
  1614  	for sect := Segdata.Sect; sect != nil; sect = sect.Next {
  1615  		sect.Extnum = int16(n)
  1616  		n++
  1617  	}
  1618  }
  1619  
  1620  // Add buildid to beginning of text segment, on non-ELF systems.
  1621  // Non-ELF binary formats are not always flexible enough to
  1622  // give us a place to put the Go build ID. On those systems, we put it
  1623  // at the very beginning of the text segment.
  1624  // This ``header'' is read by cmd/go.
  1625  func textbuildid() {
  1626  	if Iself || buildid == "" {
  1627  		return
  1628  	}
  1629  
  1630  	sym := Linklookup(Ctxt, "go.buildid", 0)
  1631  	sym.Reachable = true
  1632  	// The \xff is invalid UTF-8, meant to make it less likely
  1633  	// to find one of these accidentally.
  1634  	data := "\xff Go build ID: " + strconv.Quote(buildid) + "\n \xff"
  1635  	sym.Type = obj.STEXT
  1636  	sym.P = []byte(data)
  1637  	sym.Size = int64(len(sym.P))
  1638  
  1639  	sym.Next = Ctxt.Textp
  1640  	Ctxt.Textp = sym
  1641  }
  1642  
  1643  // assign addresses to text
  1644  func textaddress() {
  1645  	var sub *LSym
  1646  
  1647  	addsection(&Segtext, ".text", 05)
  1648  
  1649  	// Assign PCs in text segment.
  1650  	// Could parallelize, by assigning to text
  1651  	// and then letting threads copy down, but probably not worth it.
  1652  	sect := Segtext.Sect
  1653  
  1654  	sect.Align = int32(Funcalign)
  1655  	Linklookup(Ctxt, "runtime.text", 0).Sect = sect
  1656  	Linklookup(Ctxt, "runtime.etext", 0).Sect = sect
  1657  	va := uint64(INITTEXT)
  1658  	sect.Vaddr = va
  1659  	for sym := Ctxt.Textp; sym != nil; sym = sym.Next {
  1660  		sym.Sect = sect
  1661  		if sym.Type&obj.SSUB != 0 {
  1662  			continue
  1663  		}
  1664  		if sym.Align != 0 {
  1665  			va = uint64(Rnd(int64(va), int64(sym.Align)))
  1666  		} else {
  1667  			va = uint64(Rnd(int64(va), int64(Funcalign)))
  1668  		}
  1669  		sym.Value = 0
  1670  		for sub = sym; sub != nil; sub = sub.Sub {
  1671  			sub.Value += int64(va)
  1672  		}
  1673  		if sym.Size == 0 && sym.Sub != nil {
  1674  			Ctxt.Cursym = sym
  1675  		}
  1676  		if sym.Size < MINFUNC {
  1677  			va += MINFUNC // spacing required for findfunctab
  1678  		} else {
  1679  			va += uint64(sym.Size)
  1680  		}
  1681  	}
  1682  
  1683  	sect.Length = va - sect.Vaddr
  1684  }
  1685  
  1686  // assign addresses
  1687  func address() {
  1688  	va := uint64(INITTEXT)
  1689  	Segtext.Rwx = 05
  1690  	Segtext.Vaddr = va
  1691  	Segtext.Fileoff = uint64(HEADR)
  1692  	for s := Segtext.Sect; s != nil; s = s.Next {
  1693  		va = uint64(Rnd(int64(va), int64(s.Align)))
  1694  		s.Vaddr = va
  1695  		va += s.Length
  1696  	}
  1697  
  1698  	Segtext.Length = va - uint64(INITTEXT)
  1699  	Segtext.Filelen = Segtext.Length
  1700  	if HEADTYPE == obj.Hnacl {
  1701  		va += 32 // room for the "halt sled"
  1702  	}
  1703  
  1704  	if Segrodata.Sect != nil {
  1705  		// align to page boundary so as not to mix
  1706  		// rodata and executable text.
  1707  		va = uint64(Rnd(int64(va), int64(INITRND)))
  1708  
  1709  		Segrodata.Rwx = 04
  1710  		Segrodata.Vaddr = va
  1711  		Segrodata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff
  1712  		Segrodata.Filelen = 0
  1713  		for s := Segrodata.Sect; s != nil; s = s.Next {
  1714  			va = uint64(Rnd(int64(va), int64(s.Align)))
  1715  			s.Vaddr = va
  1716  			va += s.Length
  1717  		}
  1718  
  1719  		Segrodata.Length = va - Segrodata.Vaddr
  1720  		Segrodata.Filelen = Segrodata.Length
  1721  	}
  1722  
  1723  	va = uint64(Rnd(int64(va), int64(INITRND)))
  1724  	Segdata.Rwx = 06
  1725  	Segdata.Vaddr = va
  1726  	Segdata.Fileoff = va - Segtext.Vaddr + Segtext.Fileoff
  1727  	Segdata.Filelen = 0
  1728  	if HEADTYPE == obj.Hwindows {
  1729  		Segdata.Fileoff = Segtext.Fileoff + uint64(Rnd(int64(Segtext.Length), PEFILEALIGN))
  1730  	}
  1731  	if HEADTYPE == obj.Hplan9 {
  1732  		Segdata.Fileoff = Segtext.Fileoff + Segtext.Filelen
  1733  	}
  1734  	var data *Section
  1735  	var noptr *Section
  1736  	var bss *Section
  1737  	var noptrbss *Section
  1738  	var vlen int64
  1739  	for s := Segdata.Sect; s != nil; s = s.Next {
  1740  		if Iself && s.Name == ".tbss" {
  1741  			continue
  1742  		}
  1743  		vlen = int64(s.Length)
  1744  		if s.Next != nil && !(Iself && s.Next.Name == ".tbss") {
  1745  			vlen = int64(s.Next.Vaddr - s.Vaddr)
  1746  		}
  1747  		s.Vaddr = va
  1748  		va += uint64(vlen)
  1749  		Segdata.Length = va - Segdata.Vaddr
  1750  		if s.Name == ".data" {
  1751  			data = s
  1752  		}
  1753  		if s.Name == ".noptrdata" {
  1754  			noptr = s
  1755  		}
  1756  		if s.Name == ".bss" {
  1757  			bss = s
  1758  		}
  1759  		if s.Name == ".noptrbss" {
  1760  			noptrbss = s
  1761  		}
  1762  	}
  1763  
  1764  	Segdata.Filelen = bss.Vaddr - Segdata.Vaddr
  1765  
  1766  	text := Segtext.Sect
  1767  	var rodata *Section
  1768  	if Segrodata.Sect != nil {
  1769  		rodata = Segrodata.Sect
  1770  	} else {
  1771  		rodata = text.Next
  1772  	}
  1773  	typelink := rodata.Next
  1774  	if UseRelro() {
  1775  		// There is another section (.data.rel.ro) when building a shared
  1776  		// object on elf systems.
  1777  		typelink = typelink.Next
  1778  	}
  1779  	symtab := typelink.Next
  1780  	pclntab := symtab.Next
  1781  
  1782  	var sub *LSym
  1783  	for sym := datap; sym != nil; sym = sym.Next {
  1784  		Ctxt.Cursym = sym
  1785  		if sym.Sect != nil {
  1786  			sym.Value += int64(sym.Sect.Vaddr)
  1787  		}
  1788  		for sub = sym.Sub; sub != nil; sub = sub.Sub {
  1789  			sub.Value += sym.Value
  1790  		}
  1791  	}
  1792  
  1793  	if Buildmode == BuildmodeShared {
  1794  		s := Linklookup(Ctxt, "go.link.abihashbytes", 0)
  1795  		sectSym := Linklookup(Ctxt, ".note.go.abihash", 0)
  1796  		s.Sect = sectSym.Sect
  1797  		s.Value = int64(sectSym.Sect.Vaddr + 16)
  1798  	}
  1799  
  1800  	xdefine("runtime.text", obj.STEXT, int64(text.Vaddr))
  1801  	xdefine("runtime.etext", obj.STEXT, int64(text.Vaddr+text.Length))
  1802  	xdefine("runtime.rodata", obj.SRODATA, int64(rodata.Vaddr))
  1803  	xdefine("runtime.erodata", obj.SRODATA, int64(rodata.Vaddr+rodata.Length))
  1804  	xdefine("runtime.typelink", obj.SRODATA, int64(typelink.Vaddr))
  1805  	xdefine("runtime.etypelink", obj.SRODATA, int64(typelink.Vaddr+typelink.Length))
  1806  
  1807  	sym := Linklookup(Ctxt, "runtime.gcdata", 0)
  1808  	sym.Local = true
  1809  	xdefine("runtime.egcdata", obj.SRODATA, Symaddr(sym)+sym.Size)
  1810  	Linklookup(Ctxt, "runtime.egcdata", 0).Sect = sym.Sect
  1811  
  1812  	sym = Linklookup(Ctxt, "runtime.gcbss", 0)
  1813  	sym.Local = true
  1814  	xdefine("runtime.egcbss", obj.SRODATA, Symaddr(sym)+sym.Size)
  1815  	Linklookup(Ctxt, "runtime.egcbss", 0).Sect = sym.Sect
  1816  
  1817  	xdefine("runtime.symtab", obj.SRODATA, int64(symtab.Vaddr))
  1818  	xdefine("runtime.esymtab", obj.SRODATA, int64(symtab.Vaddr+symtab.Length))
  1819  	xdefine("runtime.pclntab", obj.SRODATA, int64(pclntab.Vaddr))
  1820  	xdefine("runtime.epclntab", obj.SRODATA, int64(pclntab.Vaddr+pclntab.Length))
  1821  	xdefine("runtime.noptrdata", obj.SNOPTRDATA, int64(noptr.Vaddr))
  1822  	xdefine("runtime.enoptrdata", obj.SNOPTRDATA, int64(noptr.Vaddr+noptr.Length))
  1823  	xdefine("runtime.bss", obj.SBSS, int64(bss.Vaddr))
  1824  	xdefine("runtime.ebss", obj.SBSS, int64(bss.Vaddr+bss.Length))
  1825  	xdefine("runtime.data", obj.SDATA, int64(data.Vaddr))
  1826  	xdefine("runtime.edata", obj.SDATA, int64(data.Vaddr+data.Length))
  1827  	xdefine("runtime.noptrbss", obj.SNOPTRBSS, int64(noptrbss.Vaddr))
  1828  	xdefine("runtime.enoptrbss", obj.SNOPTRBSS, int64(noptrbss.Vaddr+noptrbss.Length))
  1829  	xdefine("runtime.end", obj.SBSS, int64(Segdata.Vaddr+Segdata.Length))
  1830  }