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