github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/cmd/link/internal/arm/asm.go (about)

     1  // Inferno utils/5l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/5l/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 arm
    32  
    33  import (
    34  	"cmd/internal/objabi"
    35  	"cmd/link/internal/ld"
    36  	"fmt"
    37  	"log"
    38  )
    39  
    40  // This assembler:
    41  //
    42  //         .align 2
    43  // local.dso_init:
    44  //         ldr r0, .Lmoduledata
    45  // .Lloadfrom:
    46  //         ldr r0, [r0]
    47  //         b runtime.addmoduledata@plt
    48  // .align 2
    49  // .Lmoduledata:
    50  //         .word local.moduledata(GOT_PREL) + (. - (.Lloadfrom + 4))
    51  // assembles to:
    52  //
    53  // 00000000 <local.dso_init>:
    54  //    0:        e59f0004        ldr     r0, [pc, #4]    ; c <local.dso_init+0xc>
    55  //    4:        e5900000        ldr     r0, [r0]
    56  //    8:        eafffffe        b       0 <runtime.addmoduledata>
    57  //                      8: R_ARM_JUMP24 runtime.addmoduledata
    58  //    c:        00000004        .word   0x00000004
    59  //                      c: R_ARM_GOT_PREL       local.moduledata
    60  
    61  func gentext(ctxt *ld.Link) {
    62  	if !ctxt.DynlinkingGo() {
    63  		return
    64  	}
    65  	addmoduledata := ctxt.Syms.Lookup("runtime.addmoduledata", 0)
    66  	if addmoduledata.Type == ld.STEXT && ld.Buildmode != ld.BuildmodePlugin {
    67  		// we're linking a module containing the runtime -> no need for
    68  		// an init function
    69  		return
    70  	}
    71  	addmoduledata.Attr |= ld.AttrReachable
    72  	initfunc := ctxt.Syms.Lookup("go.link.addmoduledata", 0)
    73  	initfunc.Type = ld.STEXT
    74  	initfunc.Attr |= ld.AttrLocal
    75  	initfunc.Attr |= ld.AttrReachable
    76  	o := func(op uint32) {
    77  		ld.Adduint32(ctxt, initfunc, op)
    78  	}
    79  	o(0xe59f0004)
    80  	o(0xe08f0000)
    81  
    82  	o(0xeafffffe)
    83  	rel := ld.Addrel(initfunc)
    84  	rel.Off = 8
    85  	rel.Siz = 4
    86  	rel.Sym = ctxt.Syms.Lookup("runtime.addmoduledata", 0)
    87  	rel.Type = objabi.R_CALLARM
    88  	rel.Add = 0xeafffffe // vomit
    89  
    90  	o(0x00000000)
    91  	rel = ld.Addrel(initfunc)
    92  	rel.Off = 12
    93  	rel.Siz = 4
    94  	rel.Sym = ctxt.Moduledata
    95  	rel.Type = objabi.R_PCREL
    96  	rel.Add = 4
    97  
    98  	if ld.Buildmode == ld.BuildmodePlugin {
    99  		ctxt.Textp = append(ctxt.Textp, addmoduledata)
   100  	}
   101  	ctxt.Textp = append(ctxt.Textp, initfunc)
   102  	initarray_entry := ctxt.Syms.Lookup("go.link.addmoduledatainit", 0)
   103  	initarray_entry.Attr |= ld.AttrReachable
   104  	initarray_entry.Attr |= ld.AttrLocal
   105  	initarray_entry.Type = ld.SINITARR
   106  	ld.Addaddr(ctxt, initarray_entry, initfunc)
   107  }
   108  
   109  // Preserve highest 8 bits of a, and do addition to lower 24-bit
   110  // of a and b; used to adjust ARM branch instruction's target
   111  func braddoff(a int32, b int32) int32 {
   112  	return int32((uint32(a))&0xff000000 | 0x00ffffff&uint32(a+b))
   113  }
   114  
   115  func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) bool {
   116  	targ := r.Sym
   117  
   118  	switch r.Type {
   119  	default:
   120  		if r.Type >= 256 {
   121  			ld.Errorf(s, "unexpected relocation type %d (%s)", r.Type, ld.RelocName(r.Type))
   122  			return false
   123  		}
   124  
   125  		// Handle relocations found in ELF object files.
   126  	case 256 + ld.R_ARM_PLT32:
   127  		r.Type = objabi.R_CALLARM
   128  
   129  		if targ.Type == ld.SDYNIMPORT {
   130  			addpltsym(ctxt, targ)
   131  			r.Sym = ctxt.Syms.Lookup(".plt", 0)
   132  			r.Add = int64(braddoff(int32(r.Add), targ.Plt/4))
   133  		}
   134  
   135  		return true
   136  
   137  	case 256 + ld.R_ARM_THM_PC22: // R_ARM_THM_CALL
   138  		ld.Exitf("R_ARM_THM_CALL, are you using -marm?")
   139  		return false
   140  
   141  	case 256 + ld.R_ARM_GOT32: // R_ARM_GOT_BREL
   142  		if targ.Type != ld.SDYNIMPORT {
   143  			addgotsyminternal(ctxt, targ)
   144  		} else {
   145  			addgotsym(ctxt, targ)
   146  		}
   147  
   148  		r.Type = objabi.R_CONST // write r->add during relocsym
   149  		r.Sym = nil
   150  		r.Add += int64(targ.Got)
   151  		return true
   152  
   153  	case 256 + ld.R_ARM_GOT_PREL: // GOT(nil) + A - nil
   154  		if targ.Type != ld.SDYNIMPORT {
   155  			addgotsyminternal(ctxt, targ)
   156  		} else {
   157  			addgotsym(ctxt, targ)
   158  		}
   159  
   160  		r.Type = objabi.R_PCREL
   161  		r.Sym = ctxt.Syms.Lookup(".got", 0)
   162  		r.Add += int64(targ.Got) + 4
   163  		return true
   164  
   165  	case 256 + ld.R_ARM_GOTOFF: // R_ARM_GOTOFF32
   166  		r.Type = objabi.R_GOTOFF
   167  
   168  		return true
   169  
   170  	case 256 + ld.R_ARM_GOTPC: // R_ARM_BASE_PREL
   171  		r.Type = objabi.R_PCREL
   172  
   173  		r.Sym = ctxt.Syms.Lookup(".got", 0)
   174  		r.Add += 4
   175  		return true
   176  
   177  	case 256 + ld.R_ARM_CALL:
   178  		r.Type = objabi.R_CALLARM
   179  		if targ.Type == ld.SDYNIMPORT {
   180  			addpltsym(ctxt, targ)
   181  			r.Sym = ctxt.Syms.Lookup(".plt", 0)
   182  			r.Add = int64(braddoff(int32(r.Add), targ.Plt/4))
   183  		}
   184  
   185  		return true
   186  
   187  	case 256 + ld.R_ARM_REL32: // R_ARM_REL32
   188  		r.Type = objabi.R_PCREL
   189  
   190  		r.Add += 4
   191  		return true
   192  
   193  	case 256 + ld.R_ARM_ABS32:
   194  		if targ.Type == ld.SDYNIMPORT {
   195  			ld.Errorf(s, "unexpected R_ARM_ABS32 relocation for dynamic symbol %s", targ.Name)
   196  		}
   197  		r.Type = objabi.R_ADDR
   198  		return true
   199  
   200  		// we can just ignore this, because we are targeting ARM V5+ anyway
   201  	case 256 + ld.R_ARM_V4BX:
   202  		if r.Sym != nil {
   203  			// R_ARM_V4BX is ABS relocation, so this symbol is a dummy symbol, ignore it
   204  			r.Sym.Type = 0
   205  		}
   206  
   207  		r.Sym = nil
   208  		return true
   209  
   210  	case 256 + ld.R_ARM_PC24,
   211  		256 + ld.R_ARM_JUMP24:
   212  		r.Type = objabi.R_CALLARM
   213  		if targ.Type == ld.SDYNIMPORT {
   214  			addpltsym(ctxt, targ)
   215  			r.Sym = ctxt.Syms.Lookup(".plt", 0)
   216  			r.Add = int64(braddoff(int32(r.Add), targ.Plt/4))
   217  		}
   218  
   219  		return true
   220  	}
   221  
   222  	// Handle references to ELF symbols from our own object files.
   223  	if targ.Type != ld.SDYNIMPORT {
   224  		return true
   225  	}
   226  
   227  	switch r.Type {
   228  	case objabi.R_CALLARM:
   229  		addpltsym(ctxt, targ)
   230  		r.Sym = ctxt.Syms.Lookup(".plt", 0)
   231  		r.Add = int64(targ.Plt)
   232  		return true
   233  
   234  	case objabi.R_ADDR:
   235  		if s.Type != ld.SDATA {
   236  			break
   237  		}
   238  		if ld.Iself {
   239  			ld.Adddynsym(ctxt, targ)
   240  			rel := ctxt.Syms.Lookup(".rel", 0)
   241  			ld.Addaddrplus(ctxt, rel, s, int64(r.Off))
   242  			ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(targ.Dynid), ld.R_ARM_GLOB_DAT)) // we need a nil + A dynamic reloc
   243  			r.Type = objabi.R_CONST                                                         // write r->add during relocsym
   244  			r.Sym = nil
   245  			return true
   246  		}
   247  	}
   248  
   249  	return false
   250  }
   251  
   252  func elfreloc1(ctxt *ld.Link, r *ld.Reloc, sectoff int64) bool {
   253  	ld.Thearch.Lput(uint32(sectoff))
   254  
   255  	elfsym := r.Xsym.ElfsymForReloc()
   256  	switch r.Type {
   257  	default:
   258  		return false
   259  	case objabi.R_ADDR:
   260  		if r.Siz == 4 {
   261  			ld.Thearch.Lput(ld.R_ARM_ABS32 | uint32(elfsym)<<8)
   262  		} else {
   263  			return false
   264  		}
   265  	case objabi.R_PCREL:
   266  		if r.Siz == 4 {
   267  			ld.Thearch.Lput(ld.R_ARM_REL32 | uint32(elfsym)<<8)
   268  		} else {
   269  			return false
   270  		}
   271  	case objabi.R_CALLARM:
   272  		if r.Siz == 4 {
   273  			if r.Add&0xff000000 == 0xeb000000 { // BL
   274  				ld.Thearch.Lput(ld.R_ARM_CALL | uint32(elfsym)<<8)
   275  			} else {
   276  				ld.Thearch.Lput(ld.R_ARM_JUMP24 | uint32(elfsym)<<8)
   277  			}
   278  		} else {
   279  			return false
   280  		}
   281  	case objabi.R_TLS_LE:
   282  		ld.Thearch.Lput(ld.R_ARM_TLS_LE32 | uint32(elfsym)<<8)
   283  	case objabi.R_TLS_IE:
   284  		ld.Thearch.Lput(ld.R_ARM_TLS_IE32 | uint32(elfsym)<<8)
   285  	case objabi.R_GOTPCREL:
   286  		if r.Siz == 4 {
   287  			ld.Thearch.Lput(ld.R_ARM_GOT_PREL | uint32(elfsym)<<8)
   288  		} else {
   289  			return false
   290  		}
   291  	}
   292  
   293  	return true
   294  }
   295  
   296  func elfsetupplt(ctxt *ld.Link) {
   297  	plt := ctxt.Syms.Lookup(".plt", 0)
   298  	got := ctxt.Syms.Lookup(".got.plt", 0)
   299  	if plt.Size == 0 {
   300  		// str lr, [sp, #-4]!
   301  		ld.Adduint32(ctxt, plt, 0xe52de004)
   302  
   303  		// ldr lr, [pc, #4]
   304  		ld.Adduint32(ctxt, plt, 0xe59fe004)
   305  
   306  		// add lr, pc, lr
   307  		ld.Adduint32(ctxt, plt, 0xe08fe00e)
   308  
   309  		// ldr pc, [lr, #8]!
   310  		ld.Adduint32(ctxt, plt, 0xe5bef008)
   311  
   312  		// .word &GLOBAL_OFFSET_TABLE[0] - .
   313  		ld.Addpcrelplus(ctxt, plt, got, 4)
   314  
   315  		// the first .plt entry requires 3 .plt.got entries
   316  		ld.Adduint32(ctxt, got, 0)
   317  
   318  		ld.Adduint32(ctxt, got, 0)
   319  		ld.Adduint32(ctxt, got, 0)
   320  	}
   321  }
   322  
   323  func machoreloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool {
   324  	var v uint32
   325  
   326  	rs := r.Xsym
   327  
   328  	if r.Type == objabi.R_PCREL {
   329  		if rs.Type == ld.SHOSTOBJ {
   330  			ld.Errorf(s, "pc-relative relocation of external symbol is not supported")
   331  			return false
   332  		}
   333  		if r.Siz != 4 {
   334  			return false
   335  		}
   336  
   337  		// emit a pair of "scattered" relocations that
   338  		// resolve to the difference of section addresses of
   339  		// the symbol and the instruction
   340  		// this value is added to the field being relocated
   341  		o1 := uint32(sectoff)
   342  		o1 |= 1 << 31 // scattered bit
   343  		o1 |= ld.MACHO_ARM_RELOC_SECTDIFF << 24
   344  		o1 |= 2 << 28 // size = 4
   345  
   346  		o2 := uint32(0)
   347  		o2 |= 1 << 31 // scattered bit
   348  		o2 |= ld.MACHO_ARM_RELOC_PAIR << 24
   349  		o2 |= 2 << 28 // size = 4
   350  
   351  		ld.Thearch.Lput(o1)
   352  		ld.Thearch.Lput(uint32(ld.Symaddr(rs)))
   353  		ld.Thearch.Lput(o2)
   354  		ld.Thearch.Lput(uint32(s.Value + int64(r.Off)))
   355  		return true
   356  	}
   357  
   358  	if rs.Type == ld.SHOSTOBJ || r.Type == objabi.R_CALLARM {
   359  		if rs.Dynid < 0 {
   360  			ld.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", r.Type, ld.RelocName(r.Type), rs.Name, rs.Type, rs.Type)
   361  			return false
   362  		}
   363  
   364  		v = uint32(rs.Dynid)
   365  		v |= 1 << 27 // external relocation
   366  	} else {
   367  		v = uint32(rs.Sect.Extnum)
   368  		if v == 0 {
   369  			ld.Errorf(s, "reloc %d (%s) to symbol %s in non-macho section %s type=%d (%s)", r.Type, ld.RelocName(r.Type), rs.Name, rs.Sect.Name, rs.Type, rs.Type)
   370  			return false
   371  		}
   372  	}
   373  
   374  	switch r.Type {
   375  	default:
   376  		return false
   377  
   378  	case objabi.R_ADDR:
   379  		v |= ld.MACHO_GENERIC_RELOC_VANILLA << 28
   380  
   381  	case objabi.R_CALLARM:
   382  		v |= 1 << 24 // pc-relative bit
   383  		v |= ld.MACHO_ARM_RELOC_BR24 << 28
   384  	}
   385  
   386  	switch r.Siz {
   387  	default:
   388  		return false
   389  	case 1:
   390  		v |= 0 << 25
   391  
   392  	case 2:
   393  		v |= 1 << 25
   394  
   395  	case 4:
   396  		v |= 2 << 25
   397  
   398  	case 8:
   399  		v |= 3 << 25
   400  	}
   401  
   402  	ld.Thearch.Lput(uint32(sectoff))
   403  	ld.Thearch.Lput(v)
   404  	return true
   405  }
   406  
   407  // sign extend a 24-bit integer
   408  func signext24(x int64) int32 {
   409  	return (int32(x) << 8) >> 8
   410  }
   411  
   412  // encode an immediate in ARM's imm12 format. copied from ../../../internal/obj/arm/asm5.go
   413  func immrot(v uint32) uint32 {
   414  	for i := 0; i < 16; i++ {
   415  		if v&^0xff == 0 {
   416  			return uint32(i<<8) | v | 1<<25
   417  		}
   418  		v = v<<2 | v>>30
   419  	}
   420  	return 0
   421  }
   422  
   423  // Convert the direct jump relocation r to refer to a trampoline if the target is too far
   424  func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) {
   425  	switch r.Type {
   426  	case objabi.R_CALLARM:
   427  		// r.Add is the instruction
   428  		// low 24-bit encodes the target address
   429  		t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4
   430  		if t > 0x7fffff || t < -0x800000 || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) {
   431  			// direct call too far, need to insert trampoline.
   432  			// look up existing trampolines first. if we found one within the range
   433  			// of direct call, we can reuse it. otherwise create a new one.
   434  			offset := (signext24(r.Add&0xffffff) + 2) * 4
   435  			var tramp *ld.Symbol
   436  			for i := 0; ; i++ {
   437  				name := r.Sym.Name + fmt.Sprintf("%+d-tramp%d", offset, i)
   438  				tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version))
   439  				if tramp.Type == ld.SDYNIMPORT {
   440  					// don't reuse trampoline defined in other module
   441  					continue
   442  				}
   443  				if tramp.Value == 0 {
   444  					// either the trampoline does not exist -- we need to create one,
   445  					// or found one the address which is not assigned -- this will be
   446  					// laid down immediately after the current function. use this one.
   447  					break
   448  				}
   449  
   450  				t = (ld.Symaddr(tramp) - 8 - (s.Value + int64(r.Off))) / 4
   451  				if t >= -0x800000 && t < 0x7fffff {
   452  					// found an existing trampoline that is not too far
   453  					// we can just use it
   454  					break
   455  				}
   456  			}
   457  			if tramp.Type == 0 {
   458  				// trampoline does not exist, create one
   459  				ctxt.AddTramp(tramp)
   460  				if ctxt.DynlinkingGo() {
   461  					if immrot(uint32(offset)) == 0 {
   462  						ld.Errorf(s, "odd offset in dynlink direct call: %v+%d", r.Sym, offset)
   463  					}
   464  					gentrampdyn(tramp, r.Sym, int64(offset))
   465  				} else if ld.Buildmode == ld.BuildmodeCArchive || ld.Buildmode == ld.BuildmodeCShared || ld.Buildmode == ld.BuildmodePIE {
   466  					gentramppic(tramp, r.Sym, int64(offset))
   467  				} else {
   468  					gentramp(tramp, r.Sym, int64(offset))
   469  				}
   470  			}
   471  			// modify reloc to point to tramp, which will be resolved later
   472  			r.Sym = tramp
   473  			r.Add = r.Add&0xff000000 | 0xfffffe // clear the offset embedded in the instruction
   474  			r.Done = false
   475  		}
   476  	default:
   477  		ld.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type, ld.RelocName(r.Type))
   478  	}
   479  }
   480  
   481  // generate a trampoline to target+offset
   482  func gentramp(tramp, target *ld.Symbol, offset int64) {
   483  	tramp.Size = 12 // 3 instructions
   484  	tramp.P = make([]byte, tramp.Size)
   485  	t := ld.Symaddr(target) + int64(offset)
   486  	o1 := uint32(0xe5900000 | 11<<12 | 15<<16) // MOVW (R15), R11 // R15 is actual pc + 8
   487  	o2 := uint32(0xe12fff10 | 11)              // JMP  (R11)
   488  	o3 := uint32(t)                            // WORD $target
   489  	ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
   490  	ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
   491  	ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
   492  
   493  	if ld.Linkmode == ld.LinkExternal {
   494  		r := ld.Addrel(tramp)
   495  		r.Off = 8
   496  		r.Type = objabi.R_ADDR
   497  		r.Siz = 4
   498  		r.Sym = target
   499  		r.Add = offset
   500  	}
   501  }
   502  
   503  // generate a trampoline to target+offset in position independent code
   504  func gentramppic(tramp, target *ld.Symbol, offset int64) {
   505  	tramp.Size = 16 // 4 instructions
   506  	tramp.P = make([]byte, tramp.Size)
   507  	o1 := uint32(0xe5900000 | 11<<12 | 15<<16 | 4)  // MOVW 4(R15), R11 // R15 is actual pc + 8
   508  	o2 := uint32(0xe0800000 | 11<<12 | 15<<16 | 11) // ADD R15, R11, R11
   509  	o3 := uint32(0xe12fff10 | 11)                   // JMP  (R11)
   510  	o4 := uint32(0)                                 // WORD $(target-pc) // filled in with relocation
   511  	ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
   512  	ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
   513  	ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
   514  	ld.SysArch.ByteOrder.PutUint32(tramp.P[12:], o4)
   515  
   516  	r := ld.Addrel(tramp)
   517  	r.Off = 12
   518  	r.Type = objabi.R_PCREL
   519  	r.Siz = 4
   520  	r.Sym = target
   521  	r.Add = offset + 4
   522  }
   523  
   524  // generate a trampoline to target+offset in dynlink mode (using GOT)
   525  func gentrampdyn(tramp, target *ld.Symbol, offset int64) {
   526  	tramp.Size = 20                                 // 5 instructions
   527  	o1 := uint32(0xe5900000 | 11<<12 | 15<<16 | 8)  // MOVW 8(R15), R11 // R15 is actual pc + 8
   528  	o2 := uint32(0xe0800000 | 11<<12 | 15<<16 | 11) // ADD R15, R11, R11
   529  	o3 := uint32(0xe5900000 | 11<<12 | 11<<16)      // MOVW (R11), R11
   530  	o4 := uint32(0xe12fff10 | 11)                   // JMP  (R11)
   531  	o5 := uint32(0)                                 // WORD $target@GOT // filled in with relocation
   532  	o6 := uint32(0)
   533  	if offset != 0 {
   534  		// insert an instruction to add offset
   535  		tramp.Size = 24 // 6 instructions
   536  		o6 = o5
   537  		o5 = o4
   538  		o4 = uint32(0xe2800000 | 11<<12 | 11<<16 | immrot(uint32(offset))) // ADD $offset, R11, R11
   539  		o1 = uint32(0xe5900000 | 11<<12 | 15<<16 | 12)                     // MOVW 12(R15), R11
   540  	}
   541  	tramp.P = make([]byte, tramp.Size)
   542  	ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
   543  	ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
   544  	ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
   545  	ld.SysArch.ByteOrder.PutUint32(tramp.P[12:], o4)
   546  	ld.SysArch.ByteOrder.PutUint32(tramp.P[16:], o5)
   547  	if offset != 0 {
   548  		ld.SysArch.ByteOrder.PutUint32(tramp.P[20:], o6)
   549  	}
   550  
   551  	r := ld.Addrel(tramp)
   552  	r.Off = 16
   553  	r.Type = objabi.R_GOTPCREL
   554  	r.Siz = 4
   555  	r.Sym = target
   556  	r.Add = 8
   557  	if offset != 0 {
   558  		// increase reloc offset by 4 as we inserted an ADD instruction
   559  		r.Off = 20
   560  		r.Add = 12
   561  	}
   562  }
   563  
   564  func archreloc(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) bool {
   565  	if ld.Linkmode == ld.LinkExternal {
   566  		switch r.Type {
   567  		case objabi.R_CALLARM:
   568  			r.Done = false
   569  
   570  			// set up addend for eventual relocation via outer symbol.
   571  			rs := r.Sym
   572  
   573  			r.Xadd = int64(signext24(r.Add & 0xffffff))
   574  			r.Xadd *= 4
   575  			for rs.Outer != nil {
   576  				r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer)
   577  				rs = rs.Outer
   578  			}
   579  
   580  			if rs.Type != ld.SHOSTOBJ && rs.Type != ld.SDYNIMPORT && rs.Sect == nil {
   581  				ld.Errorf(s, "missing section for %s", rs.Name)
   582  			}
   583  			r.Xsym = rs
   584  
   585  			// ld64 for arm seems to want the symbol table to contain offset
   586  			// into the section rather than pseudo virtual address that contains
   587  			// the section load address.
   588  			// we need to compensate that by removing the instruction's address
   589  			// from addend.
   590  			if ld.Headtype == objabi.Hdarwin {
   591  				r.Xadd -= ld.Symaddr(s) + int64(r.Off)
   592  			}
   593  
   594  			if r.Xadd/4 > 0x7fffff || r.Xadd/4 < -0x800000 {
   595  				ld.Errorf(s, "direct call too far %d", r.Xadd/4)
   596  			}
   597  
   598  			*val = int64(braddoff(int32(0xff000000&uint32(r.Add)), int32(0xffffff&uint32(r.Xadd/4))))
   599  			return true
   600  		}
   601  
   602  		return false
   603  	}
   604  
   605  	switch r.Type {
   606  	case objabi.R_CONST:
   607  		*val = r.Add
   608  		return true
   609  	case objabi.R_GOTOFF:
   610  		*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0))
   611  		return true
   612  
   613  	// The following three arch specific relocations are only for generation of
   614  	// Linux/ARM ELF's PLT entry (3 assembler instruction)
   615  	case objabi.R_PLT0: // add ip, pc, #0xXX00000
   616  		if ld.Symaddr(ctxt.Syms.Lookup(".got.plt", 0)) < ld.Symaddr(ctxt.Syms.Lookup(".plt", 0)) {
   617  			ld.Errorf(s, ".got.plt should be placed after .plt section.")
   618  		}
   619  		*val = 0xe28fc600 + (0xff & (int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ctxt.Syms.Lookup(".plt", 0))+int64(r.Off))+r.Add)) >> 20))
   620  		return true
   621  	case objabi.R_PLT1: // add ip, ip, #0xYY000
   622  		*val = 0xe28cca00 + (0xff & (int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ctxt.Syms.Lookup(".plt", 0))+int64(r.Off))+r.Add+4)) >> 12))
   623  
   624  		return true
   625  	case objabi.R_PLT2: // ldr pc, [ip, #0xZZZ]!
   626  		*val = 0xe5bcf000 + (0xfff & int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ctxt.Syms.Lookup(".plt", 0))+int64(r.Off))+r.Add+8)))
   627  
   628  		return true
   629  	case objabi.R_CALLARM: // bl XXXXXX or b YYYYYY
   630  		// r.Add is the instruction
   631  		// low 24-bit encodes the target address
   632  		t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4
   633  		if t > 0x7fffff || t < -0x800000 {
   634  			ld.Errorf(s, "direct call too far: %s %x", r.Sym.Name, t)
   635  		}
   636  		*val = int64(braddoff(int32(0xff000000&uint32(r.Add)), int32(0xffffff&t)))
   637  
   638  		return true
   639  	}
   640  
   641  	return false
   642  }
   643  
   644  func archrelocvariant(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, t int64) int64 {
   645  	log.Fatalf("unexpected relocation variant")
   646  	return t
   647  }
   648  
   649  func addpltreloc(ctxt *ld.Link, plt *ld.Symbol, got *ld.Symbol, sym *ld.Symbol, typ objabi.RelocType) *ld.Reloc {
   650  	r := ld.Addrel(plt)
   651  	r.Sym = got
   652  	r.Off = int32(plt.Size)
   653  	r.Siz = 4
   654  	r.Type = typ
   655  	r.Add = int64(sym.Got) - 8
   656  
   657  	plt.Attr |= ld.AttrReachable
   658  	plt.Size += 4
   659  	ld.Symgrow(plt, plt.Size)
   660  
   661  	return r
   662  }
   663  
   664  func addpltsym(ctxt *ld.Link, s *ld.Symbol) {
   665  	if s.Plt >= 0 {
   666  		return
   667  	}
   668  
   669  	ld.Adddynsym(ctxt, s)
   670  
   671  	if ld.Iself {
   672  		plt := ctxt.Syms.Lookup(".plt", 0)
   673  		got := ctxt.Syms.Lookup(".got.plt", 0)
   674  		rel := ctxt.Syms.Lookup(".rel.plt", 0)
   675  		if plt.Size == 0 {
   676  			elfsetupplt(ctxt)
   677  		}
   678  
   679  		// .got entry
   680  		s.Got = int32(got.Size)
   681  
   682  		// In theory, all GOT should point to the first PLT entry,
   683  		// Linux/ARM's dynamic linker will do that for us, but FreeBSD/ARM's
   684  		// dynamic linker won't, so we'd better do it ourselves.
   685  		ld.Addaddrplus(ctxt, got, plt, 0)
   686  
   687  		// .plt entry, this depends on the .got entry
   688  		s.Plt = int32(plt.Size)
   689  
   690  		addpltreloc(ctxt, plt, got, s, objabi.R_PLT0) // add lr, pc, #0xXX00000
   691  		addpltreloc(ctxt, plt, got, s, objabi.R_PLT1) // add lr, lr, #0xYY000
   692  		addpltreloc(ctxt, plt, got, s, objabi.R_PLT2) // ldr pc, [lr, #0xZZZ]!
   693  
   694  		// rel
   695  		ld.Addaddrplus(ctxt, rel, got, int64(s.Got))
   696  
   697  		ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_ARM_JUMP_SLOT))
   698  	} else {
   699  		ld.Errorf(s, "addpltsym: unsupported binary format")
   700  	}
   701  }
   702  
   703  func addgotsyminternal(ctxt *ld.Link, s *ld.Symbol) {
   704  	if s.Got >= 0 {
   705  		return
   706  	}
   707  
   708  	got := ctxt.Syms.Lookup(".got", 0)
   709  	s.Got = int32(got.Size)
   710  
   711  	ld.Addaddrplus(ctxt, got, s, 0)
   712  
   713  	if ld.Iself {
   714  	} else {
   715  		ld.Errorf(s, "addgotsyminternal: unsupported binary format")
   716  	}
   717  }
   718  
   719  func addgotsym(ctxt *ld.Link, s *ld.Symbol) {
   720  	if s.Got >= 0 {
   721  		return
   722  	}
   723  
   724  	ld.Adddynsym(ctxt, s)
   725  	got := ctxt.Syms.Lookup(".got", 0)
   726  	s.Got = int32(got.Size)
   727  	ld.Adduint32(ctxt, got, 0)
   728  
   729  	if ld.Iself {
   730  		rel := ctxt.Syms.Lookup(".rel", 0)
   731  		ld.Addaddrplus(ctxt, rel, got, int64(s.Got))
   732  		ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_ARM_GLOB_DAT))
   733  	} else {
   734  		ld.Errorf(s, "addgotsym: unsupported binary format")
   735  	}
   736  }
   737  
   738  func asmb(ctxt *ld.Link) {
   739  	if ctxt.Debugvlog != 0 {
   740  		ctxt.Logf("%5.2f asmb\n", ld.Cputime())
   741  	}
   742  
   743  	if ld.Iself {
   744  		ld.Asmbelfsetup()
   745  	}
   746  
   747  	sect := ld.Segtext.Sections[0]
   748  	ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   749  	ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   750  	for _, sect = range ld.Segtext.Sections[1:] {
   751  		ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   752  		ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   753  	}
   754  
   755  	if ld.Segrodata.Filelen > 0 {
   756  		if ctxt.Debugvlog != 0 {
   757  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   758  		}
   759  		ld.Cseek(int64(ld.Segrodata.Fileoff))
   760  		ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   761  	}
   762  	if ld.Segrelrodata.Filelen > 0 {
   763  		if ctxt.Debugvlog != 0 {
   764  			ctxt.Logf("%5.2f relrodatblk\n", ld.Cputime())
   765  		}
   766  		ld.Cseek(int64(ld.Segrelrodata.Fileoff))
   767  		ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen))
   768  	}
   769  
   770  	if ctxt.Debugvlog != 0 {
   771  		ctxt.Logf("%5.2f datblk\n", ld.Cputime())
   772  	}
   773  
   774  	ld.Cseek(int64(ld.Segdata.Fileoff))
   775  	ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   776  
   777  	ld.Cseek(int64(ld.Segdwarf.Fileoff))
   778  	ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   779  
   780  	machlink := uint32(0)
   781  	if ld.Headtype == objabi.Hdarwin {
   782  		machlink = uint32(ld.Domacholink(ctxt))
   783  	}
   784  
   785  	/* output symbol table */
   786  	ld.Symsize = 0
   787  
   788  	ld.Lcsize = 0
   789  	symo := uint32(0)
   790  	if !*ld.FlagS {
   791  		// TODO: rationalize
   792  		if ctxt.Debugvlog != 0 {
   793  			ctxt.Logf("%5.2f sym\n", ld.Cputime())
   794  		}
   795  		switch ld.Headtype {
   796  		default:
   797  			if ld.Iself {
   798  				symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   799  				symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound)))
   800  			}
   801  
   802  		case objabi.Hplan9:
   803  			symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
   804  
   805  		case objabi.Hdarwin:
   806  			symo = uint32(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(*ld.FlagRound))) + uint64(machlink))
   807  		}
   808  
   809  		ld.Cseek(int64(symo))
   810  		switch ld.Headtype {
   811  		default:
   812  			if ld.Iself {
   813  				if ctxt.Debugvlog != 0 {
   814  					ctxt.Logf("%5.2f elfsym\n", ld.Cputime())
   815  				}
   816  				ld.Asmelfsym(ctxt)
   817  				ld.Cflush()
   818  				ld.Cwrite(ld.Elfstrdat)
   819  
   820  				if ld.Linkmode == ld.LinkExternal {
   821  					ld.Elfemitreloc(ctxt)
   822  				}
   823  			}
   824  
   825  		case objabi.Hplan9:
   826  			ld.Asmplan9sym(ctxt)
   827  			ld.Cflush()
   828  
   829  			sym := ctxt.Syms.Lookup("pclntab", 0)
   830  			if sym != nil {
   831  				ld.Lcsize = int32(len(sym.P))
   832  				for i := 0; int32(i) < ld.Lcsize; i++ {
   833  					ld.Cput(sym.P[i])
   834  				}
   835  
   836  				ld.Cflush()
   837  			}
   838  
   839  		case objabi.Hdarwin:
   840  			if ld.Linkmode == ld.LinkExternal {
   841  				ld.Machoemitreloc(ctxt)
   842  			}
   843  		}
   844  	}
   845  
   846  	if ctxt.Debugvlog != 0 {
   847  		ctxt.Logf("%5.2f header\n", ld.Cputime())
   848  	}
   849  	ld.Cseek(0)
   850  	switch ld.Headtype {
   851  	default:
   852  	case objabi.Hplan9: /* plan 9 */
   853  		ld.Lputb(0x647)                      /* magic */
   854  		ld.Lputb(uint32(ld.Segtext.Filelen)) /* sizes */
   855  		ld.Lputb(uint32(ld.Segdata.Filelen))
   856  		ld.Lputb(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
   857  		ld.Lputb(uint32(ld.Symsize))          /* nsyms */
   858  		ld.Lputb(uint32(ld.Entryvalue(ctxt))) /* va of entry */
   859  		ld.Lputb(0)
   860  		ld.Lputb(uint32(ld.Lcsize))
   861  
   862  	case objabi.Hlinux,
   863  		objabi.Hfreebsd,
   864  		objabi.Hnetbsd,
   865  		objabi.Hopenbsd,
   866  		objabi.Hnacl:
   867  		ld.Asmbelf(ctxt, int64(symo))
   868  
   869  	case objabi.Hdarwin:
   870  		ld.Asmbmacho(ctxt)
   871  	}
   872  
   873  	ld.Cflush()
   874  	if *ld.FlagC {
   875  		fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
   876  		fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
   877  		fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
   878  		fmt.Printf("symsize=%d\n", ld.Symsize)
   879  		fmt.Printf("lcsize=%d\n", ld.Lcsize)
   880  		fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))
   881  	}
   882  }