...
Run Format

Source file src/cmd/link/internal/arm/asm.go

Documentation: cmd/link/internal/arm

     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  		if ctxt.LinkMode == ld.LinkExternal {
   233  			// External linker will do this relocation.
   234  			return true
   235  		}
   236  		addpltsym(ctxt, targ)
   237  		r.Sym = ctxt.Syms.Lookup(".plt", 0)
   238  		r.Add = int64(targ.Plt)
   239  		return true
   240  
   241  	case objabi.R_ADDR:
   242  		if s.Type != sym.SDATA {
   243  			break
   244  		}
   245  		if ctxt.IsELF {
   246  			ld.Adddynsym(ctxt, targ)
   247  			rel := ctxt.Syms.Lookup(".rel", 0)
   248  			rel.AddAddrPlus(ctxt.Arch, s, int64(r.Off))
   249  			rel.AddUint32(ctxt.Arch, ld.ELF32_R_INFO(uint32(targ.Dynid), uint32(elf.R_ARM_GLOB_DAT))) // we need a nil + A dynamic reloc
   250  			r.Type = objabi.R_CONST                                                                   // write r->add during relocsym
   251  			r.Sym = nil
   252  			return true
   253  		}
   254  	}
   255  
   256  	return false
   257  }
   258  
   259  func elfreloc1(ctxt *ld.Link, r *sym.Reloc, sectoff int64) bool {
   260  	ctxt.Out.Write32(uint32(sectoff))
   261  
   262  	elfsym := r.Xsym.ElfsymForReloc()
   263  	switch r.Type {
   264  	default:
   265  		return false
   266  	case objabi.R_ADDR:
   267  		if r.Siz == 4 {
   268  			ctxt.Out.Write32(uint32(elf.R_ARM_ABS32) | uint32(elfsym)<<8)
   269  		} else {
   270  			return false
   271  		}
   272  	case objabi.R_PCREL:
   273  		if r.Siz == 4 {
   274  			ctxt.Out.Write32(uint32(elf.R_ARM_REL32) | uint32(elfsym)<<8)
   275  		} else {
   276  			return false
   277  		}
   278  	case objabi.R_CALLARM:
   279  		if r.Siz == 4 {
   280  			if r.Add&0xff000000 == 0xeb000000 { // BL
   281  				ctxt.Out.Write32(uint32(elf.R_ARM_CALL) | uint32(elfsym)<<8)
   282  			} else {
   283  				ctxt.Out.Write32(uint32(elf.R_ARM_JUMP24) | uint32(elfsym)<<8)
   284  			}
   285  		} else {
   286  			return false
   287  		}
   288  	case objabi.R_TLS_LE:
   289  		ctxt.Out.Write32(uint32(elf.R_ARM_TLS_LE32) | uint32(elfsym)<<8)
   290  	case objabi.R_TLS_IE:
   291  		ctxt.Out.Write32(uint32(elf.R_ARM_TLS_IE32) | uint32(elfsym)<<8)
   292  	case objabi.R_GOTPCREL:
   293  		if r.Siz == 4 {
   294  			ctxt.Out.Write32(uint32(elf.R_ARM_GOT_PREL) | uint32(elfsym)<<8)
   295  		} else {
   296  			return false
   297  		}
   298  	}
   299  
   300  	return true
   301  }
   302  
   303  func elfsetupplt(ctxt *ld.Link) {
   304  	plt := ctxt.Syms.Lookup(".plt", 0)
   305  	got := ctxt.Syms.Lookup(".got.plt", 0)
   306  	if plt.Size == 0 {
   307  		// str lr, [sp, #-4]!
   308  		plt.AddUint32(ctxt.Arch, 0xe52de004)
   309  
   310  		// ldr lr, [pc, #4]
   311  		plt.AddUint32(ctxt.Arch, 0xe59fe004)
   312  
   313  		// add lr, pc, lr
   314  		plt.AddUint32(ctxt.Arch, 0xe08fe00e)
   315  
   316  		// ldr pc, [lr, #8]!
   317  		plt.AddUint32(ctxt.Arch, 0xe5bef008)
   318  
   319  		// .word &GLOBAL_OFFSET_TABLE[0] - .
   320  		plt.AddPCRelPlus(ctxt.Arch, got, 4)
   321  
   322  		// the first .plt entry requires 3 .plt.got entries
   323  		got.AddUint32(ctxt.Arch, 0)
   324  
   325  		got.AddUint32(ctxt.Arch, 0)
   326  		got.AddUint32(ctxt.Arch, 0)
   327  	}
   328  }
   329  
   330  func machoreloc1(arch *sys.Arch, out *ld.OutBuf, s *sym.Symbol, r *sym.Reloc, sectoff int64) bool {
   331  	var v uint32
   332  
   333  	rs := r.Xsym
   334  
   335  	if r.Type == objabi.R_PCREL {
   336  		if rs.Type == sym.SHOSTOBJ {
   337  			ld.Errorf(s, "pc-relative relocation of external symbol is not supported")
   338  			return false
   339  		}
   340  		if r.Siz != 4 {
   341  			return false
   342  		}
   343  
   344  		// emit a pair of "scattered" relocations that
   345  		// resolve to the difference of section addresses of
   346  		// the symbol and the instruction
   347  		// this value is added to the field being relocated
   348  		o1 := uint32(sectoff)
   349  		o1 |= 1 << 31 // scattered bit
   350  		o1 |= ld.MACHO_ARM_RELOC_SECTDIFF << 24
   351  		o1 |= 2 << 28 // size = 4
   352  
   353  		o2 := uint32(0)
   354  		o2 |= 1 << 31 // scattered bit
   355  		o2 |= ld.MACHO_ARM_RELOC_PAIR << 24
   356  		o2 |= 2 << 28 // size = 4
   357  
   358  		out.Write32(o1)
   359  		out.Write32(uint32(ld.Symaddr(rs)))
   360  		out.Write32(o2)
   361  		out.Write32(uint32(s.Value + int64(r.Off)))
   362  		return true
   363  	}
   364  
   365  	if rs.Type == sym.SHOSTOBJ || r.Type == objabi.R_CALLARM {
   366  		if rs.Dynid < 0 {
   367  			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)
   368  			return false
   369  		}
   370  
   371  		v = uint32(rs.Dynid)
   372  		v |= 1 << 27 // external relocation
   373  	} else {
   374  		v = uint32(rs.Sect.Extnum)
   375  		if v == 0 {
   376  			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)
   377  			return false
   378  		}
   379  	}
   380  
   381  	switch r.Type {
   382  	default:
   383  		return false
   384  
   385  	case objabi.R_ADDR:
   386  		v |= ld.MACHO_GENERIC_RELOC_VANILLA << 28
   387  
   388  	case objabi.R_CALLARM:
   389  		v |= 1 << 24 // pc-relative bit
   390  		v |= ld.MACHO_ARM_RELOC_BR24 << 28
   391  	}
   392  
   393  	switch r.Siz {
   394  	default:
   395  		return false
   396  	case 1:
   397  		v |= 0 << 25
   398  
   399  	case 2:
   400  		v |= 1 << 25
   401  
   402  	case 4:
   403  		v |= 2 << 25
   404  
   405  	case 8:
   406  		v |= 3 << 25
   407  	}
   408  
   409  	out.Write32(uint32(sectoff))
   410  	out.Write32(v)
   411  	return true
   412  }
   413  
   414  // sign extend a 24-bit integer
   415  func signext24(x int64) int32 {
   416  	return (int32(x) << 8) >> 8
   417  }
   418  
   419  // encode an immediate in ARM's imm12 format. copied from ../../../internal/obj/arm/asm5.go
   420  func immrot(v uint32) uint32 {
   421  	for i := 0; i < 16; i++ {
   422  		if v&^0xff == 0 {
   423  			return uint32(i<<8) | v | 1<<25
   424  		}
   425  		v = v<<2 | v>>30
   426  	}
   427  	return 0
   428  }
   429  
   430  // Convert the direct jump relocation r to refer to a trampoline if the target is too far
   431  func trampoline(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol) {
   432  	switch r.Type {
   433  	case objabi.R_CALLARM:
   434  		// r.Add is the instruction
   435  		// low 24-bit encodes the target address
   436  		t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4
   437  		if t > 0x7fffff || t < -0x800000 || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) {
   438  			// direct call too far, need to insert trampoline.
   439  			// look up existing trampolines first. if we found one within the range
   440  			// of direct call, we can reuse it. otherwise create a new one.
   441  			offset := (signext24(r.Add&0xffffff) + 2) * 4
   442  			var tramp *sym.Symbol
   443  			for i := 0; ; i++ {
   444  				name := r.Sym.Name + fmt.Sprintf("%+d-tramp%d", offset, i)
   445  				tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version))
   446  				if tramp.Type == sym.SDYNIMPORT {
   447  					// don't reuse trampoline defined in other module
   448  					continue
   449  				}
   450  				if tramp.Value == 0 {
   451  					// either the trampoline does not exist -- we need to create one,
   452  					// or found one the address which is not assigned -- this will be
   453  					// laid down immediately after the current function. use this one.
   454  					break
   455  				}
   456  
   457  				t = (ld.Symaddr(tramp) - 8 - (s.Value + int64(r.Off))) / 4
   458  				if t >= -0x800000 && t < 0x7fffff {
   459  					// found an existing trampoline that is not too far
   460  					// we can just use it
   461  					break
   462  				}
   463  			}
   464  			if tramp.Type == 0 {
   465  				// trampoline does not exist, create one
   466  				ctxt.AddTramp(tramp)
   467  				if ctxt.DynlinkingGo() {
   468  					if immrot(uint32(offset)) == 0 {
   469  						ld.Errorf(s, "odd offset in dynlink direct call: %v+%d", r.Sym, offset)
   470  					}
   471  					gentrampdyn(ctxt.Arch, tramp, r.Sym, int64(offset))
   472  				} else if ctxt.BuildMode == ld.BuildModeCArchive || ctxt.BuildMode == ld.BuildModeCShared || ctxt.BuildMode == ld.BuildModePIE {
   473  					gentramppic(ctxt.Arch, tramp, r.Sym, int64(offset))
   474  				} else {
   475  					gentramp(ctxt.Arch, ctxt.LinkMode, tramp, r.Sym, int64(offset))
   476  				}
   477  			}
   478  			// modify reloc to point to tramp, which will be resolved later
   479  			r.Sym = tramp
   480  			r.Add = r.Add&0xff000000 | 0xfffffe // clear the offset embedded in the instruction
   481  			r.Done = false
   482  		}
   483  	default:
   484  		ld.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type))
   485  	}
   486  }
   487  
   488  // generate a trampoline to target+offset
   489  func gentramp(arch *sys.Arch, linkmode ld.LinkMode, tramp, target *sym.Symbol, offset int64) {
   490  	tramp.Size = 12 // 3 instructions
   491  	tramp.P = make([]byte, tramp.Size)
   492  	t := ld.Symaddr(target) + offset
   493  	o1 := uint32(0xe5900000 | 11<<12 | 15<<16) // MOVW (R15), R11 // R15 is actual pc + 8
   494  	o2 := uint32(0xe12fff10 | 11)              // JMP  (R11)
   495  	o3 := uint32(t)                            // WORD $target
   496  	arch.ByteOrder.PutUint32(tramp.P, o1)
   497  	arch.ByteOrder.PutUint32(tramp.P[4:], o2)
   498  	arch.ByteOrder.PutUint32(tramp.P[8:], o3)
   499  
   500  	if linkmode == ld.LinkExternal {
   501  		r := tramp.AddRel()
   502  		r.Off = 8
   503  		r.Type = objabi.R_ADDR
   504  		r.Siz = 4
   505  		r.Sym = target
   506  		r.Add = offset
   507  	}
   508  }
   509  
   510  // generate a trampoline to target+offset in position independent code
   511  func gentramppic(arch *sys.Arch, tramp, target *sym.Symbol, offset int64) {
   512  	tramp.Size = 16 // 4 instructions
   513  	tramp.P = make([]byte, tramp.Size)
   514  	o1 := uint32(0xe5900000 | 11<<12 | 15<<16 | 4)  // MOVW 4(R15), R11 // R15 is actual pc + 8
   515  	o2 := uint32(0xe0800000 | 11<<12 | 15<<16 | 11) // ADD R15, R11, R11
   516  	o3 := uint32(0xe12fff10 | 11)                   // JMP  (R11)
   517  	o4 := uint32(0)                                 // WORD $(target-pc) // filled in with relocation
   518  	arch.ByteOrder.PutUint32(tramp.P, o1)
   519  	arch.ByteOrder.PutUint32(tramp.P[4:], o2)
   520  	arch.ByteOrder.PutUint32(tramp.P[8:], o3)
   521  	arch.ByteOrder.PutUint32(tramp.P[12:], o4)
   522  
   523  	r := tramp.AddRel()
   524  	r.Off = 12
   525  	r.Type = objabi.R_PCREL
   526  	r.Siz = 4
   527  	r.Sym = target
   528  	r.Add = offset + 4
   529  }
   530  
   531  // generate a trampoline to target+offset in dynlink mode (using GOT)
   532  func gentrampdyn(arch *sys.Arch, tramp, target *sym.Symbol, offset int64) {
   533  	tramp.Size = 20                                 // 5 instructions
   534  	o1 := uint32(0xe5900000 | 11<<12 | 15<<16 | 8)  // MOVW 8(R15), R11 // R15 is actual pc + 8
   535  	o2 := uint32(0xe0800000 | 11<<12 | 15<<16 | 11) // ADD R15, R11, R11
   536  	o3 := uint32(0xe5900000 | 11<<12 | 11<<16)      // MOVW (R11), R11
   537  	o4 := uint32(0xe12fff10 | 11)                   // JMP  (R11)
   538  	o5 := uint32(0)                                 // WORD $target@GOT // filled in with relocation
   539  	o6 := uint32(0)
   540  	if offset != 0 {
   541  		// insert an instruction to add offset
   542  		tramp.Size = 24 // 6 instructions
   543  		o6 = o5
   544  		o5 = o4
   545  		o4 = 0xe2800000 | 11<<12 | 11<<16 | immrot(uint32(offset)) // ADD $offset, R11, R11
   546  		o1 = uint32(0xe5900000 | 11<<12 | 15<<16 | 12)             // MOVW 12(R15), R11
   547  	}
   548  	tramp.P = make([]byte, tramp.Size)
   549  	arch.ByteOrder.PutUint32(tramp.P, o1)
   550  	arch.ByteOrder.PutUint32(tramp.P[4:], o2)
   551  	arch.ByteOrder.PutUint32(tramp.P[8:], o3)
   552  	arch.ByteOrder.PutUint32(tramp.P[12:], o4)
   553  	arch.ByteOrder.PutUint32(tramp.P[16:], o5)
   554  	if offset != 0 {
   555  		arch.ByteOrder.PutUint32(tramp.P[20:], o6)
   556  	}
   557  
   558  	r := tramp.AddRel()
   559  	r.Off = 16
   560  	r.Type = objabi.R_GOTPCREL
   561  	r.Siz = 4
   562  	r.Sym = target
   563  	r.Add = 8
   564  	if offset != 0 {
   565  		// increase reloc offset by 4 as we inserted an ADD instruction
   566  		r.Off = 20
   567  		r.Add = 12
   568  	}
   569  }
   570  
   571  func archreloc(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, val *int64) bool {
   572  	if ctxt.LinkMode == ld.LinkExternal {
   573  		switch r.Type {
   574  		case objabi.R_CALLARM:
   575  			r.Done = false
   576  
   577  			// set up addend for eventual relocation via outer symbol.
   578  			rs := r.Sym
   579  
   580  			r.Xadd = int64(signext24(r.Add & 0xffffff))
   581  			r.Xadd *= 4
   582  			for rs.Outer != nil {
   583  				r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer)
   584  				rs = rs.Outer
   585  			}
   586  
   587  			if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
   588  				ld.Errorf(s, "missing section for %s", rs.Name)
   589  			}
   590  			r.Xsym = rs
   591  
   592  			// ld64 for arm seems to want the symbol table to contain offset
   593  			// into the section rather than pseudo virtual address that contains
   594  			// the section load address.
   595  			// we need to compensate that by removing the instruction's address
   596  			// from addend.
   597  			if ctxt.HeadType == objabi.Hdarwin {
   598  				r.Xadd -= ld.Symaddr(s) + int64(r.Off)
   599  			}
   600  
   601  			if r.Xadd/4 > 0x7fffff || r.Xadd/4 < -0x800000 {
   602  				ld.Errorf(s, "direct call too far %d", r.Xadd/4)
   603  			}
   604  
   605  			*val = int64(braddoff(int32(0xff000000&uint32(r.Add)), int32(0xffffff&uint32(r.Xadd/4))))
   606  			return true
   607  		}
   608  
   609  		return false
   610  	}
   611  
   612  	switch r.Type {
   613  	case objabi.R_CONST:
   614  		*val = r.Add
   615  		return true
   616  	case objabi.R_GOTOFF:
   617  		*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0))
   618  		return true
   619  
   620  	// The following three arch specific relocations are only for generation of
   621  	// Linux/ARM ELF's PLT entry (3 assembler instruction)
   622  	case objabi.R_PLT0: // add ip, pc, #0xXX00000
   623  		if ld.Symaddr(ctxt.Syms.Lookup(".got.plt", 0)) < ld.Symaddr(ctxt.Syms.Lookup(".plt", 0)) {
   624  			ld.Errorf(s, ".got.plt should be placed after .plt section.")
   625  		}
   626  		*val = 0xe28fc600 + (0xff & (int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ctxt.Syms.Lookup(".plt", 0))+int64(r.Off))+r.Add)) >> 20))
   627  		return true
   628  	case objabi.R_PLT1: // add ip, ip, #0xYY000
   629  		*val = 0xe28cca00 + (0xff & (int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ctxt.Syms.Lookup(".plt", 0))+int64(r.Off))+r.Add+4)) >> 12))
   630  
   631  		return true
   632  	case objabi.R_PLT2: // ldr pc, [ip, #0xZZZ]!
   633  		*val = 0xe5bcf000 + (0xfff & int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ctxt.Syms.Lookup(".plt", 0))+int64(r.Off))+r.Add+8)))
   634  
   635  		return true
   636  	case objabi.R_CALLARM: // bl XXXXXX or b YYYYYY
   637  		// r.Add is the instruction
   638  		// low 24-bit encodes the target address
   639  		t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4
   640  		if t > 0x7fffff || t < -0x800000 {
   641  			ld.Errorf(s, "direct call too far: %s %x", r.Sym.Name, t)
   642  		}
   643  		*val = int64(braddoff(int32(0xff000000&uint32(r.Add)), int32(0xffffff&t)))
   644  
   645  		return true
   646  	}
   647  
   648  	return false
   649  }
   650  
   651  func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 {
   652  	log.Fatalf("unexpected relocation variant")
   653  	return t
   654  }
   655  
   656  func addpltreloc(ctxt *ld.Link, plt *sym.Symbol, got *sym.Symbol, s *sym.Symbol, typ objabi.RelocType) {
   657  	r := plt.AddRel()
   658  	r.Sym = got
   659  	r.Off = int32(plt.Size)
   660  	r.Siz = 4
   661  	r.Type = typ
   662  	r.Add = int64(s.Got) - 8
   663  
   664  	plt.Attr |= sym.AttrReachable
   665  	plt.Size += 4
   666  	plt.Grow(plt.Size)
   667  }
   668  
   669  func addpltsym(ctxt *ld.Link, s *sym.Symbol) {
   670  	if s.Plt >= 0 {
   671  		return
   672  	}
   673  
   674  	ld.Adddynsym(ctxt, s)
   675  
   676  	if ctxt.IsELF {
   677  		plt := ctxt.Syms.Lookup(".plt", 0)
   678  		got := ctxt.Syms.Lookup(".got.plt", 0)
   679  		rel := ctxt.Syms.Lookup(".rel.plt", 0)
   680  		if plt.Size == 0 {
   681  			elfsetupplt(ctxt)
   682  		}
   683  
   684  		// .got entry
   685  		s.Got = int32(got.Size)
   686  
   687  		// In theory, all GOT should point to the first PLT entry,
   688  		// Linux/ARM's dynamic linker will do that for us, but FreeBSD/ARM's
   689  		// dynamic linker won't, so we'd better do it ourselves.
   690  		got.AddAddrPlus(ctxt.Arch, plt, 0)
   691  
   692  		// .plt entry, this depends on the .got entry
   693  		s.Plt = int32(plt.Size)
   694  
   695  		addpltreloc(ctxt, plt, got, s, objabi.R_PLT0) // add lr, pc, #0xXX00000
   696  		addpltreloc(ctxt, plt, got, s, objabi.R_PLT1) // add lr, lr, #0xYY000
   697  		addpltreloc(ctxt, plt, got, s, objabi.R_PLT2) // ldr pc, [lr, #0xZZZ]!
   698  
   699  		// rel
   700  		rel.AddAddrPlus(ctxt.Arch, got, int64(s.Got))
   701  
   702  		rel.AddUint32(ctxt.Arch, ld.ELF32_R_INFO(uint32(s.Dynid), uint32(elf.R_ARM_JUMP_SLOT)))
   703  	} else {
   704  		ld.Errorf(s, "addpltsym: unsupported binary format")
   705  	}
   706  }
   707  
   708  func addgotsyminternal(ctxt *ld.Link, s *sym.Symbol) {
   709  	if s.Got >= 0 {
   710  		return
   711  	}
   712  
   713  	got := ctxt.Syms.Lookup(".got", 0)
   714  	s.Got = int32(got.Size)
   715  
   716  	got.AddAddrPlus(ctxt.Arch, s, 0)
   717  
   718  	if ctxt.IsELF {
   719  	} else {
   720  		ld.Errorf(s, "addgotsyminternal: unsupported binary format")
   721  	}
   722  }
   723  
   724  func addgotsym(ctxt *ld.Link, s *sym.Symbol) {
   725  	if s.Got >= 0 {
   726  		return
   727  	}
   728  
   729  	ld.Adddynsym(ctxt, s)
   730  	got := ctxt.Syms.Lookup(".got", 0)
   731  	s.Got = int32(got.Size)
   732  	got.AddUint32(ctxt.Arch, 0)
   733  
   734  	if ctxt.IsELF {
   735  		rel := ctxt.Syms.Lookup(".rel", 0)
   736  		rel.AddAddrPlus(ctxt.Arch, got, int64(s.Got))
   737  		rel.AddUint32(ctxt.Arch, ld.ELF32_R_INFO(uint32(s.Dynid), uint32(elf.R_ARM_GLOB_DAT)))
   738  	} else {
   739  		ld.Errorf(s, "addgotsym: unsupported binary format")
   740  	}
   741  }
   742  
   743  func asmb(ctxt *ld.Link) {
   744  	if ctxt.Debugvlog != 0 {
   745  		ctxt.Logf("%5.2f asmb\n", ld.Cputime())
   746  	}
   747  
   748  	if ctxt.IsELF {
   749  		ld.Asmbelfsetup()
   750  	}
   751  
   752  	sect := ld.Segtext.Sections[0]
   753  	ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   754  	ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   755  	for _, sect = range ld.Segtext.Sections[1:] {
   756  		ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   757  		ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   758  	}
   759  
   760  	if ld.Segrodata.Filelen > 0 {
   761  		if ctxt.Debugvlog != 0 {
   762  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   763  		}
   764  		ctxt.Out.SeekSet(int64(ld.Segrodata.Fileoff))
   765  		ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   766  	}
   767  	if ld.Segrelrodata.Filelen > 0 {
   768  		if ctxt.Debugvlog != 0 {
   769  			ctxt.Logf("%5.2f relrodatblk\n", ld.Cputime())
   770  		}
   771  		ctxt.Out.SeekSet(int64(ld.Segrelrodata.Fileoff))
   772  		ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen))
   773  	}
   774  
   775  	if ctxt.Debugvlog != 0 {
   776  		ctxt.Logf("%5.2f datblk\n", ld.Cputime())
   777  	}
   778  
   779  	ctxt.Out.SeekSet(int64(ld.Segdata.Fileoff))
   780  	ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   781  
   782  	ctxt.Out.SeekSet(int64(ld.Segdwarf.Fileoff))
   783  	ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   784  
   785  	machlink := uint32(0)
   786  	if ctxt.HeadType == objabi.Hdarwin {
   787  		machlink = uint32(ld.Domacholink(ctxt))
   788  	}
   789  
   790  	/* output symbol table */
   791  	ld.Symsize = 0
   792  
   793  	ld.Lcsize = 0
   794  	symo := uint32(0)
   795  	if !*ld.FlagS {
   796  		// TODO: rationalize
   797  		if ctxt.Debugvlog != 0 {
   798  			ctxt.Logf("%5.2f sym\n", ld.Cputime())
   799  		}
   800  		switch ctxt.HeadType {
   801  		default:
   802  			if ctxt.IsELF {
   803  				symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   804  				symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound)))
   805  			}
   806  
   807  		case objabi.Hplan9:
   808  			symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
   809  
   810  		case objabi.Hdarwin:
   811  			symo = uint32(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(*ld.FlagRound))) + uint64(machlink))
   812  		}
   813  
   814  		ctxt.Out.SeekSet(int64(symo))
   815  		switch ctxt.HeadType {
   816  		default:
   817  			if ctxt.IsELF {
   818  				if ctxt.Debugvlog != 0 {
   819  					ctxt.Logf("%5.2f elfsym\n", ld.Cputime())
   820  				}
   821  				ld.Asmelfsym(ctxt)
   822  				ctxt.Out.Flush()
   823  				ctxt.Out.Write(ld.Elfstrdat)
   824  
   825  				if ctxt.LinkMode == ld.LinkExternal {
   826  					ld.Elfemitreloc(ctxt)
   827  				}
   828  			}
   829  
   830  		case objabi.Hplan9:
   831  			ld.Asmplan9sym(ctxt)
   832  			ctxt.Out.Flush()
   833  
   834  			sym := ctxt.Syms.Lookup("pclntab", 0)
   835  			if sym != nil {
   836  				ld.Lcsize = int32(len(sym.P))
   837  				ctxt.Out.Write(sym.P)
   838  				ctxt.Out.Flush()
   839  			}
   840  
   841  		case objabi.Hdarwin:
   842  			if ctxt.LinkMode == ld.LinkExternal {
   843  				ld.Machoemitreloc(ctxt)
   844  			}
   845  		}
   846  	}
   847  
   848  	if ctxt.Debugvlog != 0 {
   849  		ctxt.Logf("%5.2f header\n", ld.Cputime())
   850  	}
   851  	ctxt.Out.SeekSet(0)
   852  	switch ctxt.HeadType {
   853  	default:
   854  	case objabi.Hplan9: /* plan 9 */
   855  		ctxt.Out.Write32b(0x647)                      /* magic */
   856  		ctxt.Out.Write32b(uint32(ld.Segtext.Filelen)) /* sizes */
   857  		ctxt.Out.Write32b(uint32(ld.Segdata.Filelen))
   858  		ctxt.Out.Write32b(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
   859  		ctxt.Out.Write32b(uint32(ld.Symsize))          /* nsyms */
   860  		ctxt.Out.Write32b(uint32(ld.Entryvalue(ctxt))) /* va of entry */
   861  		ctxt.Out.Write32b(0)
   862  		ctxt.Out.Write32b(uint32(ld.Lcsize))
   863  
   864  	case objabi.Hlinux,
   865  		objabi.Hfreebsd,
   866  		objabi.Hnetbsd,
   867  		objabi.Hopenbsd,
   868  		objabi.Hnacl:
   869  		ld.Asmbelf(ctxt, int64(symo))
   870  
   871  	case objabi.Hdarwin:
   872  		ld.Asmbmacho(ctxt)
   873  	}
   874  
   875  	ctxt.Out.Flush()
   876  	if *ld.FlagC {
   877  		fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
   878  		fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
   879  		fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
   880  		fmt.Printf("symsize=%d\n", ld.Symsize)
   881  		fmt.Printf("lcsize=%d\n", ld.Lcsize)
   882  		fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))
   883  	}
   884  }
   885  

View as plain text