github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/cmd/link/internal/amd64/asm.go (about)

     1  // Inferno utils/6l/asm.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/asm.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  package amd64
    32  
    33  import (
    34  	"cmd/internal/obj"
    35  	"cmd/link/internal/ld"
    36  	"debug/elf"
    37  	"fmt"
    38  	"log"
    39  )
    40  
    41  func PADDR(x uint32) uint32 {
    42  	return x &^ 0x80000000
    43  }
    44  
    45  func Addcall(ctxt *ld.Link, s *ld.LSym, t *ld.LSym) int64 {
    46  	s.Attr |= ld.AttrReachable
    47  	i := s.Size
    48  	s.Size += 4
    49  	ld.Symgrow(ctxt, s, s.Size)
    50  	r := ld.Addrel(s)
    51  	r.Sym = t
    52  	r.Off = int32(i)
    53  	r.Type = obj.R_CALL
    54  	r.Siz = 4
    55  	return i + int64(r.Siz)
    56  }
    57  
    58  func gentext() {
    59  	if !ld.DynlinkingGo() {
    60  		return
    61  	}
    62  	addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0)
    63  	if addmoduledata.Type == obj.STEXT {
    64  		// we're linking a module containing the runtime -> no need for
    65  		// an init function
    66  		return
    67  	}
    68  	addmoduledata.Attr |= ld.AttrReachable
    69  	initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0)
    70  	initfunc.Type = obj.STEXT
    71  	initfunc.Attr |= ld.AttrLocal
    72  	initfunc.Attr |= ld.AttrReachable
    73  	o := func(op ...uint8) {
    74  		for _, op1 := range op {
    75  			ld.Adduint8(ld.Ctxt, initfunc, op1)
    76  		}
    77  	}
    78  	// 0000000000000000 <local.dso_init>:
    79  	//    0:	48 8d 3d 00 00 00 00 	lea    0x0(%rip),%rdi        # 7 <local.dso_init+0x7>
    80  	// 			3: R_X86_64_PC32	runtime.firstmoduledata-0x4
    81  	o(0x48, 0x8d, 0x3d)
    82  	ld.Addpcrelplus(ld.Ctxt, initfunc, ld.Ctxt.Moduledata, 0)
    83  	//    7:	e8 00 00 00 00       	callq  c <local.dso_init+0xc>
    84  	// 			8: R_X86_64_PLT32	runtime.addmoduledata-0x4
    85  	o(0xe8)
    86  	Addcall(ld.Ctxt, initfunc, addmoduledata)
    87  	//    c:	c3                   	retq
    88  	o(0xc3)
    89  	ld.Ctxt.Textp = append(ld.Ctxt.Textp, initfunc)
    90  	initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0)
    91  	initarray_entry.Attr |= ld.AttrReachable
    92  	initarray_entry.Attr |= ld.AttrLocal
    93  	initarray_entry.Type = obj.SINITARR
    94  	ld.Addaddr(ld.Ctxt, initarray_entry, initfunc)
    95  }
    96  
    97  func adddynrel(s *ld.LSym, r *ld.Reloc) {
    98  	targ := r.Sym
    99  	ld.Ctxt.Cursym = s
   100  
   101  	switch r.Type {
   102  	default:
   103  		if r.Type >= 256 {
   104  			ld.Diag("unexpected relocation type %d", r.Type)
   105  			return
   106  		}
   107  
   108  		// Handle relocations found in ELF object files.
   109  	case 256 + ld.R_X86_64_PC32:
   110  		if targ.Type == obj.SDYNIMPORT {
   111  			ld.Diag("unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name)
   112  		}
   113  		if targ.Type == 0 || targ.Type == obj.SXREF {
   114  			ld.Diag("unknown symbol %s in pcrel", targ.Name)
   115  		}
   116  		r.Type = obj.R_PCREL
   117  		r.Add += 4
   118  		return
   119  
   120  	case 256 + ld.R_X86_64_PLT32:
   121  		r.Type = obj.R_PCREL
   122  		r.Add += 4
   123  		if targ.Type == obj.SDYNIMPORT {
   124  			addpltsym(targ)
   125  			r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
   126  			r.Add += int64(targ.Plt)
   127  		}
   128  
   129  		return
   130  
   131  	case 256 + ld.R_X86_64_GOTPCREL, 256 + ld.R_X86_64_GOTPCRELX, 256 + ld.R_X86_64_REX_GOTPCRELX:
   132  		if targ.Type != obj.SDYNIMPORT {
   133  			// have symbol
   134  			if r.Off >= 2 && s.P[r.Off-2] == 0x8b {
   135  				// turn MOVQ of GOT entry into LEAQ of symbol itself
   136  				s.P[r.Off-2] = 0x8d
   137  
   138  				r.Type = obj.R_PCREL
   139  				r.Add += 4
   140  				return
   141  			}
   142  		}
   143  
   144  		// fall back to using GOT and hope for the best (CMOV*)
   145  		// TODO: just needs relocation, no need to put in .dynsym
   146  		addgotsym(targ)
   147  
   148  		r.Type = obj.R_PCREL
   149  		r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
   150  		r.Add += 4
   151  		r.Add += int64(targ.Got)
   152  		return
   153  
   154  	case 256 + ld.R_X86_64_64:
   155  		if targ.Type == obj.SDYNIMPORT {
   156  			ld.Diag("unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name)
   157  		}
   158  		r.Type = obj.R_ADDR
   159  		return
   160  
   161  	// Handle relocations found in Mach-O object files.
   162  	case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0,
   163  		512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0,
   164  		512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0:
   165  		// TODO: What is the difference between all these?
   166  		r.Type = obj.R_ADDR
   167  
   168  		if targ.Type == obj.SDYNIMPORT {
   169  			ld.Diag("unexpected reloc for dynamic symbol %s", targ.Name)
   170  		}
   171  		return
   172  
   173  	case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1:
   174  		if targ.Type == obj.SDYNIMPORT {
   175  			addpltsym(targ)
   176  			r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
   177  			r.Add = int64(targ.Plt)
   178  			r.Type = obj.R_PCREL
   179  			return
   180  		}
   181  		fallthrough
   182  
   183  		// fall through
   184  	case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1,
   185  		512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1,
   186  		512 + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1,
   187  		512 + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1,
   188  		512 + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1:
   189  		r.Type = obj.R_PCREL
   190  
   191  		if targ.Type == obj.SDYNIMPORT {
   192  			ld.Diag("unexpected pc-relative reloc for dynamic symbol %s", targ.Name)
   193  		}
   194  		return
   195  
   196  	case 512 + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1:
   197  		if targ.Type != obj.SDYNIMPORT {
   198  			// have symbol
   199  			// turn MOVQ of GOT entry into LEAQ of symbol itself
   200  			if r.Off < 2 || s.P[r.Off-2] != 0x8b {
   201  				ld.Diag("unexpected GOT_LOAD reloc for non-dynamic symbol %s", targ.Name)
   202  				return
   203  			}
   204  
   205  			s.P[r.Off-2] = 0x8d
   206  			r.Type = obj.R_PCREL
   207  			return
   208  		}
   209  		fallthrough
   210  
   211  		// fall through
   212  	case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1:
   213  		if targ.Type != obj.SDYNIMPORT {
   214  			ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name)
   215  		}
   216  		addgotsym(targ)
   217  		r.Type = obj.R_PCREL
   218  		r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
   219  		r.Add += int64(targ.Got)
   220  		return
   221  	}
   222  
   223  	// Handle references to ELF symbols from our own object files.
   224  	if targ.Type != obj.SDYNIMPORT {
   225  		return
   226  	}
   227  
   228  	switch r.Type {
   229  	case obj.R_CALL,
   230  		obj.R_PCREL:
   231  		if ld.HEADTYPE == obj.Hwindows {
   232  			// nothing to do, the relocation will be laid out in pereloc1
   233  			return
   234  		} else {
   235  			// for both ELF and Mach-O
   236  			addpltsym(targ)
   237  			r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
   238  			r.Add = int64(targ.Plt)
   239  			return
   240  		}
   241  
   242  	case obj.R_ADDR:
   243  		if s.Type == obj.STEXT && ld.Iself {
   244  			if ld.HEADTYPE == obj.Hsolaris {
   245  				addpltsym(targ)
   246  				r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
   247  				r.Add += int64(targ.Plt)
   248  				return
   249  			}
   250  			// The code is asking for the address of an external
   251  			// function. We provide it with the address of the
   252  			// correspondent GOT symbol.
   253  			addgotsym(targ)
   254  
   255  			r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
   256  			r.Add += int64(targ.Got)
   257  			return
   258  		}
   259  
   260  		if s.Type != obj.SDATA {
   261  			break
   262  		}
   263  		if ld.Iself {
   264  			ld.Adddynsym(ld.Ctxt, targ)
   265  			rela := ld.Linklookup(ld.Ctxt, ".rela", 0)
   266  			ld.Addaddrplus(ld.Ctxt, rela, s, int64(r.Off))
   267  			if r.Siz == 8 {
   268  				ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_64))
   269  			} else {
   270  				ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_32))
   271  			}
   272  			ld.Adduint64(ld.Ctxt, rela, uint64(r.Add))
   273  			r.Type = 256 // ignore during relocsym
   274  			return
   275  		}
   276  
   277  		if ld.HEADTYPE == obj.Hdarwin && s.Size == int64(ld.SysArch.PtrSize) && r.Off == 0 {
   278  			// Mach-O relocations are a royal pain to lay out.
   279  			// They use a compact stateful bytecode representation
   280  			// that is too much bother to deal with.
   281  			// Instead, interpret the C declaration
   282  			//	void *_Cvar_stderr = &stderr;
   283  			// as making _Cvar_stderr the name of a GOT entry
   284  			// for stderr. This is separate from the usual GOT entry,
   285  			// just in case the C code assigns to the variable,
   286  			// and of course it only works for single pointers,
   287  			// but we only need to support cgo and that's all it needs.
   288  			ld.Adddynsym(ld.Ctxt, targ)
   289  
   290  			got := ld.Linklookup(ld.Ctxt, ".got", 0)
   291  			s.Type = got.Type | obj.SSUB
   292  			s.Outer = got
   293  			s.Sub = got.Sub
   294  			got.Sub = s
   295  			s.Value = got.Size
   296  			ld.Adduint64(ld.Ctxt, got, 0)
   297  			ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(targ.Dynid))
   298  			r.Type = 256 // ignore during relocsym
   299  			return
   300  		}
   301  
   302  		if ld.HEADTYPE == obj.Hwindows {
   303  			// nothing to do, the relocation will be laid out in pereloc1
   304  			return
   305  		}
   306  	}
   307  
   308  	ld.Ctxt.Cursym = s
   309  	ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type)
   310  }
   311  
   312  func elfreloc1(r *ld.Reloc, sectoff int64) int {
   313  	ld.Thearch.Vput(uint64(sectoff))
   314  
   315  	elfsym := r.Xsym.ElfsymForReloc()
   316  	switch r.Type {
   317  	default:
   318  		return -1
   319  
   320  	case obj.R_ADDR:
   321  		if r.Siz == 4 {
   322  			ld.Thearch.Vput(ld.R_X86_64_32 | uint64(elfsym)<<32)
   323  		} else if r.Siz == 8 {
   324  			ld.Thearch.Vput(ld.R_X86_64_64 | uint64(elfsym)<<32)
   325  		} else {
   326  			return -1
   327  		}
   328  
   329  	case obj.R_TLS_LE:
   330  		if r.Siz == 4 {
   331  			ld.Thearch.Vput(ld.R_X86_64_TPOFF32 | uint64(elfsym)<<32)
   332  		} else {
   333  			return -1
   334  		}
   335  
   336  	case obj.R_TLS_IE:
   337  		if r.Siz == 4 {
   338  			ld.Thearch.Vput(ld.R_X86_64_GOTTPOFF | uint64(elfsym)<<32)
   339  		} else {
   340  			return -1
   341  		}
   342  
   343  	case obj.R_CALL:
   344  		if r.Siz == 4 {
   345  			if r.Xsym.Type == obj.SDYNIMPORT {
   346  				if ld.DynlinkingGo() {
   347  					ld.Thearch.Vput(ld.R_X86_64_PLT32 | uint64(elfsym)<<32)
   348  				} else {
   349  					ld.Thearch.Vput(ld.R_X86_64_GOTPCREL | uint64(elfsym)<<32)
   350  				}
   351  			} else {
   352  				ld.Thearch.Vput(ld.R_X86_64_PC32 | uint64(elfsym)<<32)
   353  			}
   354  		} else {
   355  			return -1
   356  		}
   357  
   358  	case obj.R_PCREL:
   359  		if r.Siz == 4 {
   360  			if r.Xsym.Type == obj.SDYNIMPORT && r.Xsym.ElfType == elf.STT_FUNC {
   361  				ld.Thearch.Vput(ld.R_X86_64_PLT32 | uint64(elfsym)<<32)
   362  			} else {
   363  				ld.Thearch.Vput(ld.R_X86_64_PC32 | uint64(elfsym)<<32)
   364  			}
   365  		} else {
   366  			return -1
   367  		}
   368  
   369  	case obj.R_GOTPCREL:
   370  		if r.Siz == 4 {
   371  			ld.Thearch.Vput(ld.R_X86_64_GOTPCREL | uint64(elfsym)<<32)
   372  		} else {
   373  			return -1
   374  		}
   375  	}
   376  
   377  	ld.Thearch.Vput(uint64(r.Xadd))
   378  	return 0
   379  }
   380  
   381  func machoreloc1(r *ld.Reloc, sectoff int64) int {
   382  	var v uint32
   383  
   384  	rs := r.Xsym
   385  
   386  	if rs.Type == obj.SHOSTOBJ || r.Type == obj.R_PCREL {
   387  		if rs.Dynid < 0 {
   388  			ld.Diag("reloc %d to non-macho symbol %s type=%d", r.Type, rs.Name, rs.Type)
   389  			return -1
   390  		}
   391  
   392  		v = uint32(rs.Dynid)
   393  		v |= 1 << 27 // external relocation
   394  	} else {
   395  		v = uint32(rs.Sect.Extnum)
   396  		if v == 0 {
   397  			ld.Diag("reloc %d to symbol %s in non-macho section %s type=%d", r.Type, rs.Name, rs.Sect.Name, rs.Type)
   398  			return -1
   399  		}
   400  	}
   401  
   402  	switch r.Type {
   403  	default:
   404  		return -1
   405  
   406  	case obj.R_ADDR:
   407  		v |= ld.MACHO_X86_64_RELOC_UNSIGNED << 28
   408  
   409  	case obj.R_CALL:
   410  		v |= 1 << 24 // pc-relative bit
   411  		v |= ld.MACHO_X86_64_RELOC_BRANCH << 28
   412  
   413  		// NOTE: Only works with 'external' relocation. Forced above.
   414  	case obj.R_PCREL:
   415  		v |= 1 << 24 // pc-relative bit
   416  		v |= ld.MACHO_X86_64_RELOC_SIGNED << 28
   417  	}
   418  
   419  	switch r.Siz {
   420  	default:
   421  		return -1
   422  
   423  	case 1:
   424  		v |= 0 << 25
   425  
   426  	case 2:
   427  		v |= 1 << 25
   428  
   429  	case 4:
   430  		v |= 2 << 25
   431  
   432  	case 8:
   433  		v |= 3 << 25
   434  	}
   435  
   436  	ld.Thearch.Lput(uint32(sectoff))
   437  	ld.Thearch.Lput(v)
   438  	return 0
   439  }
   440  
   441  func pereloc1(r *ld.Reloc, sectoff int64) bool {
   442  	var v uint32
   443  
   444  	rs := r.Xsym
   445  
   446  	if rs.Dynid < 0 {
   447  		ld.Diag("reloc %d to non-coff symbol %s type=%d", r.Type, rs.Name, rs.Type)
   448  		return false
   449  	}
   450  
   451  	ld.Thearch.Lput(uint32(sectoff))
   452  	ld.Thearch.Lput(uint32(rs.Dynid))
   453  
   454  	switch r.Type {
   455  	default:
   456  		return false
   457  
   458  	case obj.R_ADDR:
   459  		if r.Siz == 8 {
   460  			v = ld.IMAGE_REL_AMD64_ADDR64
   461  		} else {
   462  			v = ld.IMAGE_REL_AMD64_ADDR32
   463  		}
   464  
   465  	case obj.R_CALL,
   466  		obj.R_PCREL:
   467  		v = ld.IMAGE_REL_AMD64_REL32
   468  	}
   469  
   470  	ld.Thearch.Wput(uint16(v))
   471  
   472  	return true
   473  }
   474  
   475  func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
   476  	return -1
   477  }
   478  
   479  func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 {
   480  	log.Fatalf("unexpected relocation variant")
   481  	return t
   482  }
   483  
   484  func elfsetupplt() {
   485  	plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
   486  	got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
   487  	if plt.Size == 0 {
   488  		// pushq got+8(IP)
   489  		ld.Adduint8(ld.Ctxt, plt, 0xff)
   490  
   491  		ld.Adduint8(ld.Ctxt, plt, 0x35)
   492  		ld.Addpcrelplus(ld.Ctxt, plt, got, 8)
   493  
   494  		// jmpq got+16(IP)
   495  		ld.Adduint8(ld.Ctxt, plt, 0xff)
   496  
   497  		ld.Adduint8(ld.Ctxt, plt, 0x25)
   498  		ld.Addpcrelplus(ld.Ctxt, plt, got, 16)
   499  
   500  		// nopl 0(AX)
   501  		ld.Adduint32(ld.Ctxt, plt, 0x00401f0f)
   502  
   503  		// assume got->size == 0 too
   504  		ld.Addaddrplus(ld.Ctxt, got, ld.Linklookup(ld.Ctxt, ".dynamic", 0), 0)
   505  
   506  		ld.Adduint64(ld.Ctxt, got, 0)
   507  		ld.Adduint64(ld.Ctxt, got, 0)
   508  	}
   509  }
   510  
   511  func addpltsym(s *ld.LSym) {
   512  	if s.Plt >= 0 {
   513  		return
   514  	}
   515  
   516  	ld.Adddynsym(ld.Ctxt, s)
   517  
   518  	if ld.Iself {
   519  		plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
   520  		got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
   521  		rela := ld.Linklookup(ld.Ctxt, ".rela.plt", 0)
   522  		if plt.Size == 0 {
   523  			elfsetupplt()
   524  		}
   525  
   526  		// jmpq *got+size(IP)
   527  		ld.Adduint8(ld.Ctxt, plt, 0xff)
   528  
   529  		ld.Adduint8(ld.Ctxt, plt, 0x25)
   530  		ld.Addpcrelplus(ld.Ctxt, plt, got, got.Size)
   531  
   532  		// add to got: pointer to current pos in plt
   533  		ld.Addaddrplus(ld.Ctxt, got, plt, plt.Size)
   534  
   535  		// pushq $x
   536  		ld.Adduint8(ld.Ctxt, plt, 0x68)
   537  
   538  		ld.Adduint32(ld.Ctxt, plt, uint32((got.Size-24-8)/8))
   539  
   540  		// jmpq .plt
   541  		ld.Adduint8(ld.Ctxt, plt, 0xe9)
   542  
   543  		ld.Adduint32(ld.Ctxt, plt, uint32(-(plt.Size + 4)))
   544  
   545  		// rela
   546  		ld.Addaddrplus(ld.Ctxt, rela, got, got.Size-8)
   547  
   548  		ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_JMP_SLOT))
   549  		ld.Adduint64(ld.Ctxt, rela, 0)
   550  
   551  		s.Plt = int32(plt.Size - 16)
   552  	} else if ld.HEADTYPE == obj.Hdarwin {
   553  		// To do lazy symbol lookup right, we're supposed
   554  		// to tell the dynamic loader which library each
   555  		// symbol comes from and format the link info
   556  		// section just so. I'm too lazy (ha!) to do that
   557  		// so for now we'll just use non-lazy pointers,
   558  		// which don't need to be told which library to use.
   559  		//
   560  		// http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html
   561  		// has details about what we're avoiding.
   562  
   563  		addgotsym(s)
   564  		plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
   565  
   566  		ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.plt", 0), uint32(s.Dynid))
   567  
   568  		// jmpq *got+size(IP)
   569  		s.Plt = int32(plt.Size)
   570  
   571  		ld.Adduint8(ld.Ctxt, plt, 0xff)
   572  		ld.Adduint8(ld.Ctxt, plt, 0x25)
   573  		ld.Addpcrelplus(ld.Ctxt, plt, ld.Linklookup(ld.Ctxt, ".got", 0), int64(s.Got))
   574  	} else {
   575  		ld.Diag("addpltsym: unsupported binary format")
   576  	}
   577  }
   578  
   579  func addgotsym(s *ld.LSym) {
   580  	if s.Got >= 0 {
   581  		return
   582  	}
   583  
   584  	ld.Adddynsym(ld.Ctxt, s)
   585  	got := ld.Linklookup(ld.Ctxt, ".got", 0)
   586  	s.Got = int32(got.Size)
   587  	ld.Adduint64(ld.Ctxt, got, 0)
   588  
   589  	if ld.Iself {
   590  		rela := ld.Linklookup(ld.Ctxt, ".rela", 0)
   591  		ld.Addaddrplus(ld.Ctxt, rela, got, int64(s.Got))
   592  		ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_GLOB_DAT))
   593  		ld.Adduint64(ld.Ctxt, rela, 0)
   594  	} else if ld.HEADTYPE == obj.Hdarwin {
   595  		ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(s.Dynid))
   596  	} else {
   597  		ld.Diag("addgotsym: unsupported binary format")
   598  	}
   599  }
   600  
   601  func asmb() {
   602  	if ld.Debug['v'] != 0 {
   603  		fmt.Fprintf(ld.Bso, "%5.2f asmb\n", obj.Cputime())
   604  	}
   605  	ld.Bso.Flush()
   606  
   607  	if ld.Debug['v'] != 0 {
   608  		fmt.Fprintf(ld.Bso, "%5.2f codeblk\n", obj.Cputime())
   609  	}
   610  	ld.Bso.Flush()
   611  
   612  	if ld.Iself {
   613  		ld.Asmbelfsetup()
   614  	}
   615  
   616  	sect := ld.Segtext.Sect
   617  	ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   618  	// 0xCC is INT $3 - breakpoint instruction
   619  	ld.CodeblkPad(int64(sect.Vaddr), int64(sect.Length), []byte{0xCC})
   620  	for sect = sect.Next; sect != nil; sect = sect.Next {
   621  		ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   622  		ld.Datblk(int64(sect.Vaddr), int64(sect.Length))
   623  	}
   624  
   625  	if ld.Segrodata.Filelen > 0 {
   626  		if ld.Debug['v'] != 0 {
   627  			fmt.Fprintf(ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
   628  		}
   629  		ld.Bso.Flush()
   630  
   631  		ld.Cseek(int64(ld.Segrodata.Fileoff))
   632  		ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   633  	}
   634  
   635  	if ld.Debug['v'] != 0 {
   636  		fmt.Fprintf(ld.Bso, "%5.2f datblk\n", obj.Cputime())
   637  	}
   638  	ld.Bso.Flush()
   639  
   640  	ld.Cseek(int64(ld.Segdata.Fileoff))
   641  	ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   642  
   643  	ld.Cseek(int64(ld.Segdwarf.Fileoff))
   644  	ld.Dwarfblk(int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   645  
   646  	machlink := int64(0)
   647  	if ld.HEADTYPE == obj.Hdarwin {
   648  		machlink = ld.Domacholink()
   649  	}
   650  
   651  	switch ld.HEADTYPE {
   652  	default:
   653  		ld.Diag("unknown header type %d", ld.HEADTYPE)
   654  		fallthrough
   655  
   656  	case obj.Hplan9:
   657  		break
   658  
   659  	case obj.Hdarwin:
   660  		ld.Debug['8'] = 1 /* 64-bit addresses */
   661  
   662  	case obj.Hlinux,
   663  		obj.Hfreebsd,
   664  		obj.Hnetbsd,
   665  		obj.Hopenbsd,
   666  		obj.Hdragonfly,
   667  		obj.Hsolaris:
   668  		ld.Debug['8'] = 1 /* 64-bit addresses */
   669  
   670  	case obj.Hnacl,
   671  		obj.Hwindows:
   672  		break
   673  	}
   674  
   675  	ld.Symsize = 0
   676  	ld.Spsize = 0
   677  	ld.Lcsize = 0
   678  	symo := int64(0)
   679  	if ld.Debug['s'] == 0 {
   680  		if ld.Debug['v'] != 0 {
   681  			fmt.Fprintf(ld.Bso, "%5.2f sym\n", obj.Cputime())
   682  		}
   683  		ld.Bso.Flush()
   684  		switch ld.HEADTYPE {
   685  		default:
   686  		case obj.Hplan9:
   687  			ld.Debug['s'] = 1
   688  			symo = int64(ld.Segdata.Fileoff + ld.Segdata.Filelen)
   689  
   690  		case obj.Hdarwin:
   691  			symo = int64(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(ld.INITRND))) + uint64(machlink))
   692  
   693  		case obj.Hlinux,
   694  			obj.Hfreebsd,
   695  			obj.Hnetbsd,
   696  			obj.Hopenbsd,
   697  			obj.Hdragonfly,
   698  			obj.Hsolaris,
   699  			obj.Hnacl:
   700  			symo = int64(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   701  			symo = ld.Rnd(symo, int64(ld.INITRND))
   702  
   703  		case obj.Hwindows:
   704  			symo = int64(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   705  			symo = ld.Rnd(symo, ld.PEFILEALIGN)
   706  		}
   707  
   708  		ld.Cseek(symo)
   709  		switch ld.HEADTYPE {
   710  		default:
   711  			if ld.Iself {
   712  				ld.Cseek(symo)
   713  				ld.Asmelfsym()
   714  				ld.Cflush()
   715  				ld.Cwrite(ld.Elfstrdat)
   716  
   717  				if ld.Debug['v'] != 0 {
   718  					fmt.Fprintf(ld.Bso, "%5.2f dwarf\n", obj.Cputime())
   719  				}
   720  
   721  				if ld.Linkmode == ld.LinkExternal {
   722  					ld.Elfemitreloc()
   723  				}
   724  			}
   725  
   726  		case obj.Hplan9:
   727  			ld.Asmplan9sym()
   728  			ld.Cflush()
   729  
   730  			sym := ld.Linklookup(ld.Ctxt, "pclntab", 0)
   731  			if sym != nil {
   732  				ld.Lcsize = int32(len(sym.P))
   733  				for i := 0; int32(i) < ld.Lcsize; i++ {
   734  					ld.Cput(sym.P[i])
   735  				}
   736  
   737  				ld.Cflush()
   738  			}
   739  
   740  		case obj.Hwindows:
   741  			if ld.Debug['v'] != 0 {
   742  				fmt.Fprintf(ld.Bso, "%5.2f dwarf\n", obj.Cputime())
   743  			}
   744  
   745  		case obj.Hdarwin:
   746  			if ld.Linkmode == ld.LinkExternal {
   747  				ld.Machoemitreloc()
   748  			}
   749  		}
   750  	}
   751  
   752  	if ld.Debug['v'] != 0 {
   753  		fmt.Fprintf(ld.Bso, "%5.2f headr\n", obj.Cputime())
   754  	}
   755  	ld.Bso.Flush()
   756  	ld.Cseek(0)
   757  	switch ld.HEADTYPE {
   758  	default:
   759  	case obj.Hplan9: /* plan9 */
   760  		magic := int32(4*26*26 + 7)
   761  
   762  		magic |= 0x00008000                  /* fat header */
   763  		ld.Lputb(uint32(magic))              /* magic */
   764  		ld.Lputb(uint32(ld.Segtext.Filelen)) /* sizes */
   765  		ld.Lputb(uint32(ld.Segdata.Filelen))
   766  		ld.Lputb(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
   767  		ld.Lputb(uint32(ld.Symsize)) /* nsyms */
   768  		vl := ld.Entryvalue()
   769  		ld.Lputb(PADDR(uint32(vl))) /* va of entry */
   770  		ld.Lputb(uint32(ld.Spsize)) /* sp offsets */
   771  		ld.Lputb(uint32(ld.Lcsize)) /* line offsets */
   772  		ld.Vputb(uint64(vl))        /* va of entry */
   773  
   774  	case obj.Hdarwin:
   775  		ld.Asmbmacho()
   776  
   777  	case obj.Hlinux,
   778  		obj.Hfreebsd,
   779  		obj.Hnetbsd,
   780  		obj.Hopenbsd,
   781  		obj.Hdragonfly,
   782  		obj.Hsolaris,
   783  		obj.Hnacl:
   784  		ld.Asmbelf(symo)
   785  
   786  	case obj.Hwindows:
   787  		ld.Asmbpe()
   788  	}
   789  
   790  	ld.Cflush()
   791  }