| 1 |
// Inferno utils/5l/asm.c |
| 2 |
// https://bitbucket.org/inferno-os/inferno-os/src/master/utils/5l/asm.c |
| 3 |
// |
| 4 |
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. |
| 5 |
// Portions Copyright © 1995-1997 C H Forsyth ([email protected]) |
| 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 ([email protected]) |
| 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/loader" |
| 38 |
"cmd/link/internal/sym" |
| 39 |
"debug/elf" |
| 40 |
"fmt" |
| 41 |
"log" |
| 42 |
) |
| 43 |
|
| 44 |
// This assembler: |
| 45 |
// |
| 46 |
// .align 2 |
| 47 |
// local.dso_init: |
| 48 |
// ldr r0, .Lmoduledata |
| 49 |
// .Lloadfrom: |
| 50 |
// ldr r0, [r0] |
| 51 |
// b runtime.addmoduledata@plt |
| 52 |
// .align 2 |
| 53 |
// .Lmoduledata: |
| 54 |
// .word local.moduledata(GOT_PREL) + (. - (.Lloadfrom + 4)) |
| 55 |
// assembles to: |
| 56 |
// |
| 57 |
// 00000000 <local.dso_init>: |
| 58 |
// 0: e59f0004 ldr r0, [pc, #4] ; c <local.dso_init+0xc> |
| 59 |
// 4: e5900000 ldr r0, [r0] |
| 60 |
// 8: eafffffe b 0 <runtime.addmoduledata> |
| 61 |
// 8: R_ARM_JUMP24 runtime.addmoduledata |
| 62 |
// c: 00000004 .word 0x00000004 |
| 63 |
// c: R_ARM_GOT_PREL local.moduledata |
| 64 |
|
| 65 |
func gentext(ctxt *ld.Link, ldr *loader.Loader) { |
| 66 |
initfunc, addmoduledata := ld.PrepareAddmoduledata(ctxt) |
| 67 |
if initfunc == nil { |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
o := func(op uint32) { |
| 72 |
initfunc.AddUint32(ctxt.Arch, op) |
| 73 |
} |
| 74 |
o(0xe59f0004) |
| 75 |
o(0xe08f0000) |
| 76 |
|
| 77 |
o(0xeafffffe) |
| 78 |
rel, _ := initfunc.AddRel(objabi.R_CALLARM) |
| 79 |
rel.SetOff(8) |
| 80 |
rel.SetSiz(4) |
| 81 |
rel.SetSym(addmoduledata) |
| 82 |
rel.SetAdd(0xeafffffe) // vomit |
| 83 |
|
| 84 |
o(0x00000000) |
| 85 |
|
| 86 |
rel2, _ := initfunc.AddRel(objabi.R_PCREL) |
| 87 |
rel2.SetOff(12) |
| 88 |
rel2.SetSiz(4) |
| 89 |
rel2.SetSym(ctxt.Moduledata) |
| 90 |
rel2.SetAdd(4) |
| 91 |
} |
| 92 |
|
| 93 |
// Preserve highest 8 bits of a, and do addition to lower 24-bit |
| 94 |
// of a and b; used to adjust ARM branch instruction's target. |
| 95 |
func braddoff(a int32, b int32) int32 { |
| 96 |
return int32((uint32(a))&0xff000000 | 0x00ffffff&uint32(a+b)) |
| 97 |
} |
| 98 |
|
| 99 |
func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool { |
| 100 |
|
| 101 |
targ := r.Sym() |
| 102 |
var targType sym.SymKind |
| 103 |
if targ != 0 { |
| 104 |
targType = ldr.SymType(targ) |
| 105 |
} |
| 106 |
|
| 107 |
switch r.Type() { |
| 108 |
default: |
| 109 |
if r.Type() >= objabi.ElfRelocOffset { |
| 110 |
ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type())) |
| 111 |
return false |
| 112 |
} |
| 113 |
|
| 114 |
// Handle relocations found in ELF object files. |
| 115 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_PLT32): |
| 116 |
su := ldr.MakeSymbolUpdater(s) |
| 117 |
su.SetRelocType(rIdx, objabi.R_CALLARM) |
| 118 |
|
| 119 |
if targType == sym.SDYNIMPORT { |
| 120 |
addpltsym(target, ldr, syms, targ) |
| 121 |
su.SetRelocSym(rIdx, syms.PLT) |
| 122 |
su.SetRelocAdd(rIdx, int64(braddoff(int32(r.Add()), ldr.SymPlt(targ)/4))) |
| 123 |
} |
| 124 |
|
| 125 |
return true |
| 126 |
|
| 127 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_THM_PC22): // R_ARM_THM_CALL |
| 128 |
ld.Exitf("R_ARM_THM_CALL, are you using -marm?") |
| 129 |
return false |
| 130 |
|
| 131 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOT32): // R_ARM_GOT_BREL |
| 132 |
if targType != sym.SDYNIMPORT { |
| 133 |
addgotsyminternal(target, ldr, syms, targ) |
| 134 |
} else { |
| 135 |
ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_ARM_GLOB_DAT)) |
| 136 |
} |
| 137 |
|
| 138 |
su := ldr.MakeSymbolUpdater(s) |
| 139 |
su.SetRelocType(rIdx, objabi.R_CONST) // write r->add during relocsym |
| 140 |
su.SetRelocSym(rIdx, 0) |
| 141 |
su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ))) |
| 142 |
return true |
| 143 |
|
| 144 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOT_PREL): // GOT(nil) + A - nil |
| 145 |
if targType != sym.SDYNIMPORT { |
| 146 |
addgotsyminternal(target, ldr, syms, targ) |
| 147 |
} else { |
| 148 |
ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_ARM_GLOB_DAT)) |
| 149 |
} |
| 150 |
su := ldr.MakeSymbolUpdater(s) |
| 151 |
su.SetRelocType(rIdx, objabi.R_PCREL) |
| 152 |
su.SetRelocSym(rIdx, syms.GOT) |
| 153 |
su.SetRelocAdd(rIdx, r.Add()+4+int64(ldr.SymGot(targ))) |
| 154 |
return true |
| 155 |
|
| 156 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOTOFF): // R_ARM_GOTOFF32 |
| 157 |
su := ldr.MakeSymbolUpdater(s) |
| 158 |
su.SetRelocType(rIdx, objabi.R_GOTOFF) |
| 159 |
return true |
| 160 |
|
| 161 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOTPC): // R_ARM_BASE_PREL |
| 162 |
su := ldr.MakeSymbolUpdater(s) |
| 163 |
su.SetRelocType(rIdx, objabi.R_PCREL) |
| 164 |
su.SetRelocSym(rIdx, syms.GOT) |
| 165 |
su.SetRelocAdd(rIdx, r.Add()+4) |
| 166 |
return true |
| 167 |
|
| 168 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_CALL): |
| 169 |
su := ldr.MakeSymbolUpdater(s) |
| 170 |
su.SetRelocType(rIdx, objabi.R_CALLARM) |
| 171 |
if targType == sym.SDYNIMPORT { |
| 172 |
addpltsym(target, ldr, syms, targ) |
| 173 |
su.SetRelocSym(rIdx, syms.PLT) |
| 174 |
su.SetRelocAdd(rIdx, int64(braddoff(int32(r.Add()), ldr.SymPlt(targ)/4))) |
| 175 |
} |
| 176 |
return true |
| 177 |
|
| 178 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_REL32): // R_ARM_REL32 |
| 179 |
su := ldr.MakeSymbolUpdater(s) |
| 180 |
su.SetRelocType(rIdx, objabi.R_PCREL) |
| 181 |
su.SetRelocAdd(rIdx, r.Add()+4) |
| 182 |
return true |
| 183 |
|
| 184 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_ABS32): |
| 185 |
if targType == sym.SDYNIMPORT { |
| 186 |
ldr.Errorf(s, "unexpected R_ARM_ABS32 relocation for dynamic symbol %s", ldr.SymName(targ)) |
| 187 |
} |
| 188 |
su := ldr.MakeSymbolUpdater(s) |
| 189 |
su.SetRelocType(rIdx, objabi.R_ADDR) |
| 190 |
return true |
| 191 |
|
| 192 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_PC24), |
| 193 |
objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_JUMP24): |
| 194 |
su := ldr.MakeSymbolUpdater(s) |
| 195 |
su.SetRelocType(rIdx, objabi.R_CALLARM) |
| 196 |
if targType == sym.SDYNIMPORT { |
| 197 |
addpltsym(target, ldr, syms, targ) |
| 198 |
su.SetRelocSym(rIdx, syms.PLT) |
| 199 |
su.SetRelocAdd(rIdx, int64(braddoff(int32(r.Add()), ldr.SymPlt(targ)/4))) |
| 200 |
} |
| 201 |
|
| 202 |
return true |
| 203 |
} |
| 204 |
|
| 205 |
// Handle references to ELF symbols from our own object files. |
| 206 |
if targType != sym.SDYNIMPORT { |
| 207 |
return true |
| 208 |
} |
| 209 |
|
| 210 |
// Reread the reloc to incorporate any changes in type above. |
| 211 |
relocs := ldr.Relocs(s) |
| 212 |
r = relocs.At(rIdx) |
| 213 |
|
| 214 |
switch r.Type() { |
| 215 |
case objabi.R_CALLARM: |
| 216 |
if target.IsExternal() { |
| 217 |
// External linker will do this relocation. |
| 218 |
return true |
| 219 |
} |
| 220 |
addpltsym(target, ldr, syms, targ) |
| 221 |
su := ldr.MakeSymbolUpdater(s) |
| 222 |
su.SetRelocSym(rIdx, syms.PLT) |
| 223 |
su.SetRelocAdd(rIdx, int64(braddoff(int32(r.Add()), ldr.SymPlt(targ)/4))) // TODO: don't use r.Add for instruction bytes (issue 19811) |
| 224 |
return true |
| 225 |
|
| 226 |
case objabi.R_ADDR: |
| 227 |
if !ldr.SymType(s).IsDATA() { |
| 228 |
break |
| 229 |
} |
| 230 |
if target.IsElf() { |
| 231 |
ld.Adddynsym(ldr, target, syms, targ) |
| 232 |
rel := ldr.MakeSymbolUpdater(syms.Rel) |
| 233 |
rel.AddAddrPlus(target.Arch, s, int64(r.Off())) |
| 234 |
rel.AddUint32(target.Arch, elf.R_INFO32(uint32(ldr.SymDynid(targ)), uint32(elf.R_ARM_GLOB_DAT))) // we need a nil + A dynamic reloc |
| 235 |
su := ldr.MakeSymbolUpdater(s) |
| 236 |
su.SetRelocType(rIdx, objabi.R_CONST) // write r->add during relocsym |
| 237 |
su.SetRelocSym(rIdx, 0) |
| 238 |
return true |
| 239 |
} |
| 240 |
|
| 241 |
case objabi.R_GOTPCREL: |
| 242 |
if target.IsExternal() { |
| 243 |
// External linker will do this relocation. |
| 244 |
return true |
| 245 |
} |
| 246 |
if targType != sym.SDYNIMPORT { |
| 247 |
ldr.Errorf(s, "R_GOTPCREL target is not SDYNIMPORT symbol: %v", ldr.SymName(targ)) |
| 248 |
} |
| 249 |
ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_ARM_GLOB_DAT)) |
| 250 |
su := ldr.MakeSymbolUpdater(s) |
| 251 |
su.SetRelocType(rIdx, objabi.R_PCREL) |
| 252 |
su.SetRelocSym(rIdx, syms.GOT) |
| 253 |
su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ))) |
| 254 |
return true |
| 255 |
} |
| 256 |
|
| 257 |
return false |
| 258 |
} |
| 259 |
|
| 260 |
func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool { |
| 261 |
out.Write32(uint32(sectoff)) |
| 262 |
|
| 263 |
elfsym := ld.ElfSymForReloc(ctxt, r.Xsym) |
| 264 |
siz := r.Size |
| 265 |
switch r.Type { |
| 266 |
default: |
| 267 |
return false |
| 268 |
case objabi.R_ADDR, objabi.R_DWARFSECREF: |
| 269 |
if siz == 4 { |
| 270 |
out.Write32(uint32(elf.R_ARM_ABS32) | uint32(elfsym)<<8) |
| 271 |
} else { |
| 272 |
return false |
| 273 |
} |
| 274 |
case objabi.R_PCREL: |
| 275 |
if siz == 4 { |
| 276 |
out.Write32(uint32(elf.R_ARM_REL32) | uint32(elfsym)<<8) |
| 277 |
} else { |
| 278 |
return false |
| 279 |
} |
| 280 |
case objabi.R_CALLARM: |
| 281 |
if siz == 4 { |
| 282 |
relocs := ldr.Relocs(s) |
| 283 |
r := relocs.At(ri) |
| 284 |
if r.Add()&0xff000000 == 0xeb000000 { // BL // TODO: using r.Add here is bad (issue 19811) |
| 285 |
out.Write32(uint32(elf.R_ARM_CALL) | uint32(elfsym)<<8) |
| 286 |
} else { |
| 287 |
out.Write32(uint32(elf.R_ARM_JUMP24) | uint32(elfsym)<<8) |
| 288 |
} |
| 289 |
} else { |
| 290 |
return false |
| 291 |
} |
| 292 |
case objabi.R_TLS_LE: |
| 293 |
out.Write32(uint32(elf.R_ARM_TLS_LE32) | uint32(elfsym)<<8) |
| 294 |
case objabi.R_TLS_IE: |
| 295 |
out.Write32(uint32(elf.R_ARM_TLS_IE32) | uint32(elfsym)<<8) |
| 296 |
case objabi.R_GOTPCREL: |
| 297 |
if siz == 4 { |
| 298 |
out.Write32(uint32(elf.R_ARM_GOT_PREL) | uint32(elfsym)<<8) |
| 299 |
} else { |
| 300 |
return false |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return true |
| 305 |
} |
| 306 |
|
| 307 |
func elfsetupplt(ctxt *ld.Link, ldr *loader.Loader, plt, got *loader.SymbolBuilder, dynamic loader.Sym) { |
| 308 |
if plt.Size() == 0 { |
| 309 |
// str lr, [sp, #-4]! |
| 310 |
plt.AddUint32(ctxt.Arch, 0xe52de004) |
| 311 |
|
| 312 |
// ldr lr, [pc, #4] |
| 313 |
plt.AddUint32(ctxt.Arch, 0xe59fe004) |
| 314 |
|
| 315 |
// add lr, pc, lr |
| 316 |
plt.AddUint32(ctxt.Arch, 0xe08fe00e) |
| 317 |
|
| 318 |
// ldr pc, [lr, #8]! |
| 319 |
plt.AddUint32(ctxt.Arch, 0xe5bef008) |
| 320 |
|
| 321 |
// .word &GLOBAL_OFFSET_TABLE[0] - . |
| 322 |
plt.AddPCRelPlus(ctxt.Arch, got.Sym(), 4) |
| 323 |
|
| 324 |
// the first .plt entry requires 3 .plt.got entries |
| 325 |
got.AddUint32(ctxt.Arch, 0) |
| 326 |
|
| 327 |
got.AddUint32(ctxt.Arch, 0) |
| 328 |
got.AddUint32(ctxt.Arch, 0) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool { |
| 333 |
return false |
| 334 |
} |
| 335 |
|
| 336 |
func pereloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool { |
| 337 |
return false |
| 338 |
} |
| 339 |
|
| 340 |
// sign extend a 24-bit integer. |
| 341 |
func signext24(x int64) int32 { |
| 342 |
return (int32(x) << 8) >> 8 |
| 343 |
} |
| 344 |
|
| 345 |
// encode an immediate in ARM's imm12 format. copied from ../../../internal/obj/arm/asm5.go |
| 346 |
func immrot(v uint32) uint32 { |
| 347 |
for i := 0; i < 16; i++ { |
| 348 |
if v&^0xff == 0 { |
| 349 |
return uint32(i<<8) | v | 1<<25 |
| 350 |
} |
| 351 |
v = v<<2 | v>>30 |
| 352 |
} |
| 353 |
return 0 |
| 354 |
} |
| 355 |
|
| 356 |
// Convert the direct jump relocation r to refer to a trampoline if the target is too far. |
| 357 |
func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) { |
| 358 |
relocs := ldr.Relocs(s) |
| 359 |
r := relocs.At(ri) |
| 360 |
switch r.Type() { |
| 361 |
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_CALL), |
| 362 |
objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_PC24), |
| 363 |
objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_JUMP24): |
| 364 |
// Host object relocations that will be turned into a PLT call. |
| 365 |
// The PLT may be too far. Insert a trampoline for them. |
| 366 |
fallthrough |
| 367 |
case objabi.R_CALLARM: |
| 368 |
var t int64 |
| 369 |
// ldr.SymValue(rs) == 0 indicates a cross-package jump to a function that is not yet |
| 370 |
// laid out. Conservatively use a trampoline. This should be rare, as we lay out packages |
| 371 |
// in dependency order. |
| 372 |
if ldr.SymValue(rs) != 0 { |
| 373 |
// Workaround for issue #58425: it appears that the |
| 374 |
// external linker doesn't always take into account the |
| 375 |
// relocation addend when doing reachability checks. This |
| 376 |
// means that if you have a call from function XYZ at |
| 377 |
// offset 8 to runtime.duffzero with addend 800 (for |
| 378 |
// example), where the distance between the start of XYZ |
| 379 |
// and the start of runtime.duffzero is just over the |
| 380 |
// limit (by 100 bytes, say), you can get "relocation |
| 381 |
// doesn't fit" errors from the external linker. To deal |
| 382 |
// with this, ignore the addend when performing the |
| 383 |
// distance calculation (this assumes that we're only |
| 384 |
// handling backward jumps; ideally we might want to check |
| 385 |
// both with and without the addend). |
| 386 |
if ctxt.IsExternal() { |
| 387 |
t = (ldr.SymValue(rs) - (ldr.SymValue(s) + int64(r.Off()))) / 4 |
| 388 |
} else { |
| 389 |
// r.Add is the instruction |
| 390 |
// low 24-bit encodes the target address |
| 391 |
t = (ldr.SymValue(rs) + int64(signext24(r.Add()&0xffffff)*4) - (ldr.SymValue(s) + int64(r.Off()))) / 4 |
| 392 |
} |
| 393 |
} |
| 394 |
if t > 0x7fffff || t <= -0x800000 || ldr.SymValue(rs) == 0 || (*ld.FlagDebugTramp > 1 && ldr.SymPkg(s) != ldr.SymPkg(rs)) { |
| 395 |
// direct call too far, need to insert trampoline. |
| 396 |
// look up existing trampolines first. if we found one within the range |
| 397 |
// of direct call, we can reuse it. otherwise create a new one. |
| 398 |
offset := (signext24(r.Add()&0xffffff) + 2) * 4 |
| 399 |
var tramp loader.Sym |
| 400 |
for i := 0; ; i++ { |
| 401 |
oName := ldr.SymName(rs) |
| 402 |
name := oName + fmt.Sprintf("%+d-tramp%d", offset, i) |
| 403 |
tramp = ldr.LookupOrCreateSym(name, ldr.SymVersion(rs)) |
| 404 |
ldr.SetAttrReachable(tramp, true) |
| 405 |
if ldr.SymType(tramp) == sym.SDYNIMPORT { |
| 406 |
// don't reuse trampoline defined in other module |
| 407 |
continue |
| 408 |
} |
| 409 |
if oName == "runtime.deferreturn" { |
| 410 |
ldr.SetIsDeferReturnTramp(tramp, true) |
| 411 |
} |
| 412 |
if ldr.SymValue(tramp) == 0 { |
| 413 |
// either the trampoline does not exist -- we need to create one, |
| 414 |
// or found one the address which is not assigned -- this will be |
| 415 |
// laid down immediately after the current function. use this one. |
| 416 |
break |
| 417 |
} |
| 418 |
|
| 419 |
t = (ldr.SymValue(tramp) - 8 - (ldr.SymValue(s) + int64(r.Off()))) / 4 |
| 420 |
if t >= -0x800000 && t < 0x7fffff { |
| 421 |
// found an existing trampoline that is not too far |
| 422 |
// we can just use it |
| 423 |
break |
| 424 |
} |
| 425 |
} |
| 426 |
if ldr.SymType(tramp) == 0 { |
| 427 |
// trampoline does not exist, create one |
| 428 |
trampb := ldr.MakeSymbolUpdater(tramp) |
| 429 |
ctxt.AddTramp(trampb, ldr.SymType(s)) |
| 430 |
if ctxt.DynlinkingGo() || ldr.SymType(rs) == sym.SDYNIMPORT { |
| 431 |
if immrot(uint32(offset)) == 0 { |
| 432 |
ctxt.Errorf(s, "odd offset in dynlink direct call: %v+%d", ldr.SymName(rs), offset) |
| 433 |
} |
| 434 |
gentrampdyn(ctxt.Arch, trampb, rs, int64(offset)) |
| 435 |
} else if ctxt.BuildMode == ld.BuildModeCArchive || ctxt.BuildMode == ld.BuildModeCShared || ctxt.BuildMode == ld.BuildModePIE { |
| 436 |
gentramppic(ctxt.Arch, trampb, rs, int64(offset)) |
| 437 |
} else { |
| 438 |
gentramp(ctxt.Arch, ctxt.LinkMode, ldr, trampb, rs, int64(offset)) |
| 439 |
} |
| 440 |
} |
| 441 |
// modify reloc to point to tramp, which will be resolved later |
| 442 |
sb := ldr.MakeSymbolUpdater(s) |
| 443 |
relocs := sb.Relocs() |
| 444 |
r := relocs.At(ri) |
| 445 |
r.SetSym(tramp) |
| 446 |
r.SetAdd(r.Add()&0xff000000 | 0xfffffe) // clear the offset embedded in the instruction |
| 447 |
} |
| 448 |
default: |
| 449 |
ctxt.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type())) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
// generate a trampoline to target+offset. |
| 454 |
func gentramp(arch *sys.Arch, linkmode ld.LinkMode, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) { |
| 455 |
tramp.SetSize(12) // 3 instructions |
| 456 |
P := make([]byte, tramp.Size()) |
| 457 |
t := ldr.SymValue(target) + offset |
| 458 |
o1 := uint32(0xe5900000 | 12<<12 | 15<<16) // MOVW (R15), R12 // R15 is actual pc + 8 |
| 459 |
o2 := uint32(0xe12fff10 | 12) // JMP (R12) |
| 460 |
o3 := uint32(t) // WORD $target |
| 461 |
arch.ByteOrder.PutUint32(P, o1) |
| 462 |
arch.ByteOrder.PutUint32(P[4:], o2) |
| 463 |
arch.ByteOrder.PutUint32(P[8:], o3) |
| 464 |
tramp.SetData(P) |
| 465 |
|
| 466 |
if linkmode == ld.LinkExternal || ldr.SymValue(target) == 0 { |
| 467 |
r, _ := tramp.AddRel(objabi.R_ADDR) |
| 468 |
r.SetOff(8) |
| 469 |
r.SetSiz(4) |
| 470 |
r.SetSym(target) |
| 471 |
r.SetAdd(offset) |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
// generate a trampoline to target+offset in position independent code. |
| 476 |
func gentramppic(arch *sys.Arch, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) { |
| 477 |
tramp.SetSize(16) // 4 instructions |
| 478 |
P := make([]byte, tramp.Size()) |
| 479 |
o1 := uint32(0xe5900000 | 12<<12 | 15<<16 | 4) // MOVW 4(R15), R12 // R15 is actual pc + 8 |
| 480 |
o2 := uint32(0xe0800000 | 12<<12 | 15<<16 | 12) // ADD R15, R12, R12 |
| 481 |
o3 := uint32(0xe12fff10 | 12) // JMP (R12) |
| 482 |
o4 := uint32(0) // WORD $(target-pc) // filled in with relocation |
| 483 |
arch.ByteOrder.PutUint32(P, o1) |
| 484 |
arch.ByteOrder.PutUint32(P[4:], o2) |
| 485 |
arch.ByteOrder.PutUint32(P[8:], o3) |
| 486 |
arch.ByteOrder.PutUint32(P[12:], o4) |
| 487 |
tramp.SetData(P) |
| 488 |
|
| 489 |
r, _ := tramp.AddRel(objabi.R_PCREL) |
| 490 |
r.SetOff(12) |
| 491 |
r.SetSiz(4) |
| 492 |
r.SetSym(target) |
| 493 |
r.SetAdd(offset + 4) |
| 494 |
} |
| 495 |
|
| 496 |
// generate a trampoline to target+offset in dynlink mode (using GOT). |
| 497 |
func gentrampdyn(arch *sys.Arch, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) { |
| 498 |
tramp.SetSize(20) // 5 instructions |
| 499 |
o1 := uint32(0xe5900000 | 12<<12 | 15<<16 | 8) // MOVW 8(R15), R12 // R15 is actual pc + 8 |
| 500 |
o2 := uint32(0xe0800000 | 12<<12 | 15<<16 | 12) // ADD R15, R12, R12 |
| 501 |
o3 := uint32(0xe5900000 | 12<<12 | 12<<16) // MOVW (R12), R12 |
| 502 |
o4 := uint32(0xe12fff10 | 12) // JMP (R12) |
| 503 |
o5 := uint32(0) // WORD $target@GOT // filled in with relocation |
| 504 |
o6 := uint32(0) |
| 505 |
if offset != 0 { |
| 506 |
// insert an instruction to add offset |
| 507 |
tramp.SetSize(24) // 6 instructions |
| 508 |
o6 = o5 |
| 509 |
o5 = o4 |
| 510 |
o4 = 0xe2800000 | 12<<12 | 12<<16 | immrot(uint32(offset)) // ADD $offset, R12, R12 |
| 511 |
o1 = uint32(0xe5900000 | 12<<12 | 15<<16 | 12) // MOVW 12(R15), R12 |
| 512 |
} |
| 513 |
P := make([]byte, tramp.Size()) |
| 514 |
arch.ByteOrder.PutUint32(P, o1) |
| 515 |
arch.ByteOrder.PutUint32(P[4:], o2) |
| 516 |
arch.ByteOrder.PutUint32(P[8:], o3) |
| 517 |
arch.ByteOrder.PutUint32(P[12:], o4) |
| 518 |
arch.ByteOrder.PutUint32(P[16:], o5) |
| 519 |
if offset != 0 { |
| 520 |
arch.ByteOrder.PutUint32(P[20:], o6) |
| 521 |
} |
| 522 |
tramp.SetData(P) |
| 523 |
|
| 524 |
r, _ := tramp.AddRel(objabi.R_GOTPCREL) |
| 525 |
r.SetOff(16) |
| 526 |
r.SetSiz(4) |
| 527 |
r.SetSym(target) |
| 528 |
r.SetAdd(8) |
| 529 |
if offset != 0 { |
| 530 |
// increase reloc offset by 4 as we inserted an ADD instruction |
| 531 |
r.SetOff(20) |
| 532 |
r.SetAdd(12) |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) (o int64, nExtReloc int, ok bool) { |
| 537 |
rs := r.Sym() |
| 538 |
if target.IsExternal() { |
| 539 |
switch r.Type() { |
| 540 |
case objabi.R_CALLARM: |
| 541 |
// set up addend for eventual relocation via outer symbol. |
| 542 |
_, off := ld.FoldSubSymbolOffset(ldr, rs) |
| 543 |
xadd := int64(signext24(r.Add()&0xffffff))*4 + off |
| 544 |
if xadd/4 > 0x7fffff || xadd/4 < -0x800000 { |
| 545 |
ldr.Errorf(s, "direct call too far %d", xadd/4) |
| 546 |
} |
| 547 |
return int64(braddoff(int32(0xff000000&uint32(r.Add())), int32(0xffffff&uint32(xadd/4)))), 1, true |
| 548 |
} |
| 549 |
return -1, 0, false |
| 550 |
} |
| 551 |
|
| 552 |
const isOk = true |
| 553 |
const noExtReloc = 0 |
| 554 |
switch r.Type() { |
| 555 |
// The following three arch specific relocations are only for generation of |
| 556 |
// Linux/ARM ELF's PLT entry (3 assembler instruction) |
| 557 |
case objabi.R_PLT0: // add ip, pc, #0xXX00000 |
| 558 |
if ldr.SymValue(syms.GOTPLT) < ldr.SymValue(syms.PLT) { |
| 559 |
ldr.Errorf(s, ".got.plt should be placed after .plt section.") |
| 560 |
} |
| 561 |
return 0xe28fc600 + (0xff & (int64(uint32(ldr.SymValue(rs)-(ldr.SymValue(syms.PLT)+int64(r.Off()))+r.Add())) >> 20)), noExtReloc, isOk |
| 562 |
case objabi.R_PLT1: // add ip, ip, #0xYY000 |
| 563 |
return 0xe28cca00 + (0xff & (int64(uint32(ldr.SymValue(rs)-(ldr.SymValue(syms.PLT)+int64(r.Off()))+r.Add()+4)) >> 12)), noExtReloc, isOk |
| 564 |
case objabi.R_PLT2: // ldr pc, [ip, #0xZZZ]! |
| 565 |
return 0xe5bcf000 + (0xfff & int64(uint32(ldr.SymValue(rs)-(ldr.SymValue(syms.PLT)+int64(r.Off()))+r.Add()+8))), noExtReloc, isOk |
| 566 |
case objabi.R_CALLARM: // bl XXXXXX or b YYYYYY |
| 567 |
// r.Add is the instruction |
| 568 |
// low 24-bit encodes the target address |
| 569 |
t := (ldr.SymValue(rs) + int64(signext24(r.Add()&0xffffff)*4) - (ldr.SymValue(s) + int64(r.Off()))) / 4 |
| 570 |
if t > 0x7fffff || t < -0x800000 { |
| 571 |
ldr.Errorf(s, "direct call too far: %s %x", ldr.SymName(rs), t) |
| 572 |
} |
| 573 |
return int64(braddoff(int32(0xff000000&uint32(r.Add())), int32(0xffffff&t))), noExtReloc, isOk |
| 574 |
} |
| 575 |
|
| 576 |
return val, 0, false |
| 577 |
} |
| 578 |
|
| 579 |
func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64, []byte) int64 { |
| 580 |
log.Fatalf("unexpected relocation variant") |
| 581 |
return -1 |
| 582 |
} |
| 583 |
|
| 584 |
func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) { |
| 585 |
rs := r.Sym() |
| 586 |
var rr loader.ExtReloc |
| 587 |
switch r.Type() { |
| 588 |
case objabi.R_CALLARM: |
| 589 |
// set up addend for eventual relocation via outer symbol. |
| 590 |
rs, off := ld.FoldSubSymbolOffset(ldr, rs) |
| 591 |
rr.Xadd = int64(signext24(r.Add()&0xffffff))*4 + off |
| 592 |
rst := ldr.SymType(rs) |
| 593 |
if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && rst != sym.SUNDEFEXT && ldr.SymSect(rs) == nil { |
| 594 |
ldr.Errorf(s, "missing section for %s", ldr.SymName(rs)) |
| 595 |
} |
| 596 |
rr.Xsym = rs |
| 597 |
rr.Type = r.Type() |
| 598 |
rr.Size = r.Siz() |
| 599 |
return rr, true |
| 600 |
} |
| 601 |
return rr, false |
| 602 |
} |
| 603 |
|
| 604 |
func addpltreloc(ldr *loader.Loader, plt *loader.SymbolBuilder, got *loader.SymbolBuilder, s loader.Sym, typ objabi.RelocType) { |
| 605 |
r, _ := plt.AddRel(typ) |
| 606 |
r.SetSym(got.Sym()) |
| 607 |
r.SetOff(int32(plt.Size())) |
| 608 |
r.SetSiz(4) |
| 609 |
r.SetAdd(int64(ldr.SymGot(s)) - 8) |
| 610 |
|
| 611 |
plt.SetReachable(true) |
| 612 |
plt.SetSize(plt.Size() + 4) |
| 613 |
plt.Grow(plt.Size()) |
| 614 |
} |
| 615 |
|
| 616 |
func addpltsym(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) { |
| 617 |
if ldr.SymPlt(s) >= 0 { |
| 618 |
return |
| 619 |
} |
| 620 |
|
| 621 |
ld.Adddynsym(ldr, target, syms, s) |
| 622 |
|
| 623 |
if target.IsElf() { |
| 624 |
plt := ldr.MakeSymbolUpdater(syms.PLT) |
| 625 |
got := ldr.MakeSymbolUpdater(syms.GOTPLT) |
| 626 |
rel := ldr.MakeSymbolUpdater(syms.RelPLT) |
| 627 |
if plt.Size() == 0 { |
| 628 |
panic("plt is not set up") |
| 629 |
} |
| 630 |
|
| 631 |
// .got entry |
| 632 |
ldr.SetGot(s, int32(got.Size())) |
| 633 |
|
| 634 |
// In theory, all GOT should point to the first PLT entry, |
| 635 |
// Linux/ARM's dynamic linker will do that for us, but FreeBSD/ARM's |
| 636 |
// dynamic linker won't, so we'd better do it ourselves. |
| 637 |
got.AddAddrPlus(target.Arch, plt.Sym(), 0) |
| 638 |
|
| 639 |
// .plt entry, this depends on the .got entry |
| 640 |
ldr.SetPlt(s, int32(plt.Size())) |
| 641 |
|
| 642 |
addpltreloc(ldr, plt, got, s, objabi.R_PLT0) // add lr, pc, #0xXX00000 |
| 643 |
addpltreloc(ldr, plt, got, s, objabi.R_PLT1) // add lr, lr, #0xYY000 |
| 644 |
addpltreloc(ldr, plt, got, s, objabi.R_PLT2) // ldr pc, [lr, #0xZZZ]! |
| 645 |
|
| 646 |
// rel |
| 647 |
rel.AddAddrPlus(target.Arch, got.Sym(), int64(ldr.SymGot(s))) |
| 648 |
|
| 649 |
rel.AddUint32(target.Arch, elf.R_INFO32(uint32(ldr.SymDynid(s)), uint32(elf.R_ARM_JUMP_SLOT))) |
| 650 |
} else { |
| 651 |
ldr.Errorf(s, "addpltsym: unsupported binary format") |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
func addgotsyminternal(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) { |
| 656 |
if ldr.SymGot(s) >= 0 { |
| 657 |
return |
| 658 |
} |
| 659 |
|
| 660 |
got := ldr.MakeSymbolUpdater(syms.GOT) |
| 661 |
ldr.SetGot(s, int32(got.Size())) |
| 662 |
got.AddAddrPlus(target.Arch, s, 0) |
| 663 |
|
| 664 |
if target.IsElf() { |
| 665 |
} else { |
| 666 |
ldr.Errorf(s, "addgotsyminternal: unsupported binary format") |
| 667 |
} |
| 668 |
} |