github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/link/internal/x86/asm.go (about)

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