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