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