github.com/bir3/gocompiler@v0.3.205/src/cmd/link/internal/arm64/asm.go (about) 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 (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 arm64 32 33 import ( 34 "github.com/bir3/gocompiler/src/cmd/internal/objabi" 35 "github.com/bir3/gocompiler/src/cmd/internal/sys" 36 "github.com/bir3/gocompiler/src/cmd/link/internal/ld" 37 "github.com/bir3/gocompiler/src/cmd/link/internal/loader" 38 "github.com/bir3/gocompiler/src/cmd/link/internal/sym" 39 "debug/elf" 40 "fmt" 41 "log" 42 ) 43 44 func gentext(ctxt *ld.Link, ldr *loader.Loader) { 45 initfunc, addmoduledata := ld.PrepareAddmoduledata(ctxt) 46 if initfunc == nil { 47 return 48 } 49 50 o := func(op uint32) { 51 initfunc.AddUint32(ctxt.Arch, op) 52 } 53 // 0000000000000000 <local.dso_init>: 54 // 0: 90000000 adrp x0, 0 <runtime.firstmoduledata> 55 // 0: R_AARCH64_ADR_PREL_PG_HI21 local.moduledata 56 // 4: 91000000 add x0, x0, #0x0 57 // 4: R_AARCH64_ADD_ABS_LO12_NC local.moduledata 58 o(0x90000000) 59 o(0x91000000) 60 rel, _ := initfunc.AddRel(objabi.R_ADDRARM64) 61 rel.SetOff(0) 62 rel.SetSiz(8) 63 rel.SetSym(ctxt.Moduledata) 64 65 // 8: 14000000 b 0 <runtime.addmoduledata> 66 // 8: R_AARCH64_CALL26 runtime.addmoduledata 67 o(0x14000000) 68 rel2, _ := initfunc.AddRel(objabi.R_CALLARM64) 69 rel2.SetOff(8) 70 rel2.SetSiz(4) 71 rel2.SetSym(addmoduledata) 72 } 73 74 func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool { 75 targ := r.Sym() 76 var targType sym.SymKind 77 if targ != 0 { 78 targType = ldr.SymType(targ) 79 } 80 81 const pcrel = 1 82 switch r.Type() { 83 default: 84 if r.Type() >= objabi.ElfRelocOffset { 85 ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type())) 86 return false 87 } 88 89 // Handle relocations found in ELF object files. 90 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_PREL32): 91 if targType == sym.SDYNIMPORT { 92 ldr.Errorf(s, "unexpected R_AARCH64_PREL32 relocation for dynamic symbol %s", ldr.SymName(targ)) 93 } 94 if targType == 0 || targType == sym.SXREF { 95 ldr.Errorf(s, "unknown symbol %s in pcrel", ldr.SymName(targ)) 96 } 97 su := ldr.MakeSymbolUpdater(s) 98 su.SetRelocType(rIdx, objabi.R_PCREL) 99 su.SetRelocAdd(rIdx, r.Add()+4) 100 return true 101 102 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_PREL64): 103 if targType == sym.SDYNIMPORT { 104 ldr.Errorf(s, "unexpected R_AARCH64_PREL64 relocation for dynamic symbol %s", ldr.SymName(targ)) 105 } 106 if targType == 0 || targType == sym.SXREF { 107 ldr.Errorf(s, "unknown symbol %s in pcrel", ldr.SymName(targ)) 108 } 109 su := ldr.MakeSymbolUpdater(s) 110 su.SetRelocType(rIdx, objabi.R_PCREL) 111 su.SetRelocAdd(rIdx, r.Add()+8) 112 return true 113 114 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_CALL26), 115 objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_JUMP26): 116 if targType == sym.SDYNIMPORT { 117 addpltsym(target, ldr, syms, targ) 118 su := ldr.MakeSymbolUpdater(s) 119 su.SetRelocSym(rIdx, syms.PLT) 120 su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymPlt(targ))) 121 } 122 if targType == 0 || targType == sym.SXREF { 123 ldr.Errorf(s, "unknown symbol %s in callarm64", ldr.SymName(targ)) 124 } 125 su := ldr.MakeSymbolUpdater(s) 126 su.SetRelocType(rIdx, objabi.R_CALLARM64) 127 return true 128 129 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_ADR_GOT_PAGE), 130 objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_LD64_GOT_LO12_NC): 131 if targType != sym.SDYNIMPORT { 132 // have symbol 133 // TODO: turn LDR of GOT entry into ADR of symbol itself 134 } 135 136 // fall back to using GOT 137 // TODO: just needs relocation, no need to put in .dynsym 138 ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_AARCH64_GLOB_DAT)) 139 su := ldr.MakeSymbolUpdater(s) 140 su.SetRelocType(rIdx, objabi.R_ARM64_GOT) 141 su.SetRelocSym(rIdx, syms.GOT) 142 su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ))) 143 return true 144 145 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_ADR_PREL_PG_HI21), 146 objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_ADD_ABS_LO12_NC): 147 if targType == sym.SDYNIMPORT { 148 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 149 } 150 if targType == 0 || targType == sym.SXREF { 151 ldr.Errorf(s, "unknown symbol %s", ldr.SymName(targ)) 152 } 153 su := ldr.MakeSymbolUpdater(s) 154 su.SetRelocType(rIdx, objabi.R_ARM64_PCREL) 155 return true 156 157 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_ABS64): 158 if targType == sym.SDYNIMPORT { 159 ldr.Errorf(s, "unexpected R_AARCH64_ABS64 relocation for dynamic symbol %s", ldr.SymName(targ)) 160 } 161 su := ldr.MakeSymbolUpdater(s) 162 su.SetRelocType(rIdx, objabi.R_ADDR) 163 if target.IsPIE() && target.IsInternal() { 164 // For internal linking PIE, this R_ADDR relocation cannot 165 // be resolved statically. We need to generate a dynamic 166 // relocation. Let the code below handle it. 167 break 168 } 169 return true 170 171 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_LDST8_ABS_LO12_NC): 172 if targType == sym.SDYNIMPORT { 173 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 174 } 175 su := ldr.MakeSymbolUpdater(s) 176 su.SetRelocType(rIdx, objabi.R_ARM64_LDST8) 177 return true 178 179 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_LDST16_ABS_LO12_NC): 180 if targType == sym.SDYNIMPORT { 181 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 182 } 183 su := ldr.MakeSymbolUpdater(s) 184 su.SetRelocType(rIdx, objabi.R_ARM64_LDST16) 185 return true 186 187 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_LDST32_ABS_LO12_NC): 188 if targType == sym.SDYNIMPORT { 189 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 190 } 191 su := ldr.MakeSymbolUpdater(s) 192 su.SetRelocType(rIdx, objabi.R_ARM64_LDST32) 193 return true 194 195 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_LDST64_ABS_LO12_NC): 196 if targType == sym.SDYNIMPORT { 197 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 198 } 199 su := ldr.MakeSymbolUpdater(s) 200 su.SetRelocType(rIdx, objabi.R_ARM64_LDST64) 201 202 return true 203 204 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_LDST128_ABS_LO12_NC): 205 if targType == sym.SDYNIMPORT { 206 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 207 } 208 su := ldr.MakeSymbolUpdater(s) 209 su.SetRelocType(rIdx, objabi.R_ARM64_LDST128) 210 return true 211 212 // Handle relocations found in Mach-O object files. 213 case objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_UNSIGNED*2: 214 if targType == sym.SDYNIMPORT { 215 ldr.Errorf(s, "unexpected reloc for dynamic symbol %s", ldr.SymName(targ)) 216 } 217 su := ldr.MakeSymbolUpdater(s) 218 su.SetRelocType(rIdx, objabi.R_ADDR) 219 if target.IsPIE() && target.IsInternal() { 220 // For internal linking PIE, this R_ADDR relocation cannot 221 // be resolved statically. We need to generate a dynamic 222 // relocation. Let the code below handle it. 223 break 224 } 225 return true 226 227 case objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_BRANCH26*2 + pcrel: 228 su := ldr.MakeSymbolUpdater(s) 229 su.SetRelocType(rIdx, objabi.R_CALLARM64) 230 if targType == sym.SDYNIMPORT { 231 addpltsym(target, ldr, syms, targ) 232 su.SetRelocSym(rIdx, syms.PLT) 233 su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ))) 234 } 235 return true 236 237 case objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_PAGE21*2 + pcrel, 238 objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_PAGEOFF12*2: 239 if targType == sym.SDYNIMPORT { 240 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 241 } 242 su := ldr.MakeSymbolUpdater(s) 243 su.SetRelocType(rIdx, objabi.R_ARM64_PCREL) 244 return true 245 246 case objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_GOT_LOAD_PAGE21*2 + pcrel, 247 objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_GOT_LOAD_PAGEOFF12*2: 248 if targType != sym.SDYNIMPORT { 249 // have symbol 250 // turn MOVD sym@GOT (adrp+ldr) into MOVD $sym (adrp+add) 251 data := ldr.Data(s) 252 off := r.Off() 253 if int(off+3) >= len(data) { 254 ldr.Errorf(s, "unexpected GOT_LOAD reloc for non-dynamic symbol %s", ldr.SymName(targ)) 255 return false 256 } 257 o := target.Arch.ByteOrder.Uint32(data[off:]) 258 su := ldr.MakeSymbolUpdater(s) 259 switch { 260 case (o>>24)&0x9f == 0x90: // adrp 261 // keep instruction unchanged, change relocation type below 262 case o>>24 == 0xf9: // ldr 263 // rewrite to add 264 o = (0x91 << 24) | (o & (1<<22 - 1)) 265 su.MakeWritable() 266 su.SetUint32(target.Arch, int64(off), o) 267 default: 268 ldr.Errorf(s, "unexpected GOT_LOAD reloc for non-dynamic symbol %s", ldr.SymName(targ)) 269 return false 270 } 271 su.SetRelocType(rIdx, objabi.R_ARM64_PCREL) 272 return true 273 } 274 ld.AddGotSym(target, ldr, syms, targ, 0) 275 su := ldr.MakeSymbolUpdater(s) 276 su.SetRelocType(rIdx, objabi.R_ARM64_GOT) 277 su.SetRelocSym(rIdx, syms.GOT) 278 su.SetRelocAdd(rIdx, int64(ldr.SymGot(targ))) 279 return true 280 } 281 282 // Reread the reloc to incorporate any changes in type above. 283 relocs := ldr.Relocs(s) 284 r = relocs.At(rIdx) 285 286 switch r.Type() { 287 case objabi.R_CALL, 288 objabi.R_PCREL, 289 objabi.R_CALLARM64: 290 if targType != sym.SDYNIMPORT { 291 // nothing to do, the relocation will be laid out in reloc 292 return true 293 } 294 if target.IsExternal() { 295 // External linker will do this relocation. 296 return true 297 } 298 // Internal linking. 299 if r.Add() != 0 { 300 ldr.Errorf(s, "PLT call with non-zero addend (%v)", r.Add()) 301 } 302 // Build a PLT entry and change the relocation target to that entry. 303 addpltsym(target, ldr, syms, targ) 304 su := ldr.MakeSymbolUpdater(s) 305 su.SetRelocSym(rIdx, syms.PLT) 306 su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ))) 307 return true 308 309 case objabi.R_ADDR: 310 if ldr.SymType(s) == sym.STEXT && target.IsElf() { 311 // The code is asking for the address of an external 312 // function. We provide it with the address of the 313 // correspondent GOT symbol. 314 ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_AARCH64_GLOB_DAT)) 315 su := ldr.MakeSymbolUpdater(s) 316 su.SetRelocSym(rIdx, syms.GOT) 317 su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ))) 318 return true 319 } 320 321 // Process dynamic relocations for the data sections. 322 if target.IsPIE() && target.IsInternal() { 323 // When internally linking, generate dynamic relocations 324 // for all typical R_ADDR relocations. The exception 325 // are those R_ADDR that are created as part of generating 326 // the dynamic relocations and must be resolved statically. 327 // 328 // There are three phases relevant to understanding this: 329 // 330 // dodata() // we are here 331 // address() // symbol address assignment 332 // reloc() // resolution of static R_ADDR relocs 333 // 334 // At this point symbol addresses have not been 335 // assigned yet (as the final size of the .rela section 336 // will affect the addresses), and so we cannot write 337 // the Elf64_Rela.r_offset now. Instead we delay it 338 // until after the 'address' phase of the linker is 339 // complete. We do this via Addaddrplus, which creates 340 // a new R_ADDR relocation which will be resolved in 341 // the 'reloc' phase. 342 // 343 // These synthetic static R_ADDR relocs must be skipped 344 // now, or else we will be caught in an infinite loop 345 // of generating synthetic relocs for our synthetic 346 // relocs. 347 // 348 // Furthermore, the rela sections contain dynamic 349 // relocations with R_ADDR relocations on 350 // Elf64_Rela.r_offset. This field should contain the 351 // symbol offset as determined by reloc(), not the 352 // final dynamically linked address as a dynamic 353 // relocation would provide. 354 switch ldr.SymName(s) { 355 case ".dynsym", ".rela", ".rela.plt", ".got.plt", ".dynamic": 356 return false 357 } 358 } else { 359 // Either internally linking a static executable, 360 // in which case we can resolve these relocations 361 // statically in the 'reloc' phase, or externally 362 // linking, in which case the relocation will be 363 // prepared in the 'reloc' phase and passed to the 364 // external linker in the 'asmb' phase. 365 if ldr.SymType(s) != sym.SDATA && ldr.SymType(s) != sym.SRODATA { 366 break 367 } 368 } 369 370 if target.IsElf() { 371 // Generate R_AARCH64_RELATIVE relocations for best 372 // efficiency in the dynamic linker. 373 // 374 // As noted above, symbol addresses have not been 375 // assigned yet, so we can't generate the final reloc 376 // entry yet. We ultimately want: 377 // 378 // r_offset = s + r.Off 379 // r_info = R_AARCH64_RELATIVE 380 // r_addend = targ + r.Add 381 // 382 // The dynamic linker will set *offset = base address + 383 // addend. 384 // 385 // AddAddrPlus is used for r_offset and r_addend to 386 // generate new R_ADDR relocations that will update 387 // these fields in the 'reloc' phase. 388 rela := ldr.MakeSymbolUpdater(syms.Rela) 389 rela.AddAddrPlus(target.Arch, s, int64(r.Off())) 390 if r.Siz() == 8 { 391 rela.AddUint64(target.Arch, elf.R_INFO(0, uint32(elf.R_AARCH64_RELATIVE))) 392 } else { 393 ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ)) 394 } 395 rela.AddAddrPlus(target.Arch, targ, int64(r.Add())) 396 // Not mark r done here. So we still apply it statically, 397 // so in the file content we'll also have the right offset 398 // to the relocation target. So it can be examined statically 399 // (e.g. go version). 400 return true 401 } 402 403 if target.IsDarwin() { 404 // Mach-O relocations are a royal pain to lay out. 405 // They use a compact stateful bytecode representation. 406 // Here we record what are needed and encode them later. 407 ld.MachoAddRebase(s, int64(r.Off())) 408 // Not mark r done here. So we still apply it statically, 409 // so in the file content we'll also have the right offset 410 // to the relocation target. So it can be examined statically 411 // (e.g. go version). 412 return true 413 } 414 415 case objabi.R_ARM64_GOTPCREL: 416 if target.IsExternal() { 417 // External linker will do this relocation. 418 return true 419 } 420 if targType != sym.SDYNIMPORT { 421 ldr.Errorf(s, "R_ARM64_GOTPCREL target is not SDYNIMPORT symbol: %v", ldr.SymName(targ)) 422 } 423 if r.Add() != 0 { 424 ldr.Errorf(s, "R_ARM64_GOTPCREL with non-zero addend (%v)", r.Add()) 425 } 426 if target.IsElf() { 427 ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_AARCH64_GLOB_DAT)) 428 } else { 429 ld.AddGotSym(target, ldr, syms, targ, 0) 430 } 431 // turn into two relocations, one for each instruction. 432 su := ldr.MakeSymbolUpdater(s) 433 r.SetType(objabi.R_ARM64_GOT) 434 r.SetSiz(4) 435 r.SetSym(syms.GOT) 436 r.SetAdd(int64(ldr.SymGot(targ))) 437 r2, _ := su.AddRel(objabi.R_ARM64_GOT) 438 r2.SetSiz(4) 439 r2.SetOff(r.Off() + 4) 440 r2.SetSym(syms.GOT) 441 r2.SetAdd(int64(ldr.SymGot(targ))) 442 return true 443 } 444 return false 445 } 446 447 func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool { 448 out.Write64(uint64(sectoff)) 449 450 elfsym := ld.ElfSymForReloc(ctxt, r.Xsym) 451 siz := r.Size 452 switch r.Type { 453 default: 454 return false 455 case objabi.R_ADDR, objabi.R_DWARFSECREF: 456 switch siz { 457 case 4: 458 out.Write64(uint64(elf.R_AARCH64_ABS32) | uint64(elfsym)<<32) 459 case 8: 460 out.Write64(uint64(elf.R_AARCH64_ABS64) | uint64(elfsym)<<32) 461 default: 462 return false 463 } 464 case objabi.R_ADDRARM64: 465 // two relocations: R_AARCH64_ADR_PREL_PG_HI21 and R_AARCH64_ADD_ABS_LO12_NC 466 out.Write64(uint64(elf.R_AARCH64_ADR_PREL_PG_HI21) | uint64(elfsym)<<32) 467 out.Write64(uint64(r.Xadd)) 468 out.Write64(uint64(sectoff + 4)) 469 out.Write64(uint64(elf.R_AARCH64_ADD_ABS_LO12_NC) | uint64(elfsym)<<32) 470 471 case objabi.R_ARM64_PCREL_LDST8, 472 objabi.R_ARM64_PCREL_LDST16, 473 objabi.R_ARM64_PCREL_LDST32, 474 objabi.R_ARM64_PCREL_LDST64: 475 // two relocations: R_AARCH64_ADR_PREL_PG_HI21 and R_AARCH64_LDST{64/32/16/8}_ABS_LO12_NC 476 out.Write64(uint64(elf.R_AARCH64_ADR_PREL_PG_HI21) | uint64(elfsym)<<32) 477 out.Write64(uint64(r.Xadd)) 478 out.Write64(uint64(sectoff + 4)) 479 var ldstType elf.R_AARCH64 480 switch r.Type { 481 case objabi.R_ARM64_PCREL_LDST8: 482 ldstType = elf.R_AARCH64_LDST8_ABS_LO12_NC 483 case objabi.R_ARM64_PCREL_LDST16: 484 ldstType = elf.R_AARCH64_LDST16_ABS_LO12_NC 485 case objabi.R_ARM64_PCREL_LDST32: 486 ldstType = elf.R_AARCH64_LDST32_ABS_LO12_NC 487 case objabi.R_ARM64_PCREL_LDST64: 488 ldstType = elf.R_AARCH64_LDST64_ABS_LO12_NC 489 } 490 out.Write64(uint64(ldstType) | uint64(elfsym)<<32) 491 492 case objabi.R_ARM64_TLS_LE: 493 out.Write64(uint64(elf.R_AARCH64_TLSLE_MOVW_TPREL_G0) | uint64(elfsym)<<32) 494 case objabi.R_ARM64_TLS_IE: 495 out.Write64(uint64(elf.R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) | uint64(elfsym)<<32) 496 out.Write64(uint64(r.Xadd)) 497 out.Write64(uint64(sectoff + 4)) 498 out.Write64(uint64(elf.R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) | uint64(elfsym)<<32) 499 case objabi.R_ARM64_GOTPCREL: 500 out.Write64(uint64(elf.R_AARCH64_ADR_GOT_PAGE) | uint64(elfsym)<<32) 501 out.Write64(uint64(r.Xadd)) 502 out.Write64(uint64(sectoff + 4)) 503 out.Write64(uint64(elf.R_AARCH64_LD64_GOT_LO12_NC) | uint64(elfsym)<<32) 504 case objabi.R_CALLARM64: 505 if siz != 4 { 506 return false 507 } 508 out.Write64(uint64(elf.R_AARCH64_CALL26) | uint64(elfsym)<<32) 509 510 } 511 out.Write64(uint64(r.Xadd)) 512 513 return true 514 } 515 516 // sign-extends from 21, 24-bit. 517 func signext21(x int64) int64 { return x << (64 - 21) >> (64 - 21) } 518 func signext24(x int64) int64 { return x << (64 - 24) >> (64 - 24) } 519 520 func machoreloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool { 521 var v uint32 522 523 rs := r.Xsym 524 rt := r.Type 525 siz := r.Size 526 xadd := r.Xadd 527 528 if xadd != signext24(xadd) { 529 // If the relocation target would overflow the addend, then target 530 // a linker-manufactured label symbol with a smaller addend instead. 531 label := ldr.Lookup(offsetLabelName(ldr, rs, xadd/machoRelocLimit*machoRelocLimit), ldr.SymVersion(rs)) 532 if label != 0 { 533 xadd = ldr.SymValue(rs) + xadd - ldr.SymValue(label) 534 rs = label 535 } 536 if xadd != signext24(xadd) { 537 ldr.Errorf(s, "internal error: relocation addend overflow: %s+0x%x", ldr.SymName(rs), xadd) 538 } 539 } 540 if rt == objabi.R_CALLARM64 && xadd != 0 { 541 label := ldr.Lookup(offsetLabelName(ldr, rs, xadd), ldr.SymVersion(rs)) 542 if label != 0 { 543 xadd = ldr.SymValue(rs) + xadd - ldr.SymValue(label) // should always be 0 (checked below) 544 rs = label 545 } 546 } 547 548 if ldr.SymType(rs) == sym.SHOSTOBJ || rt == objabi.R_CALLARM64 || 549 rt == objabi.R_ARM64_PCREL_LDST8 || rt == objabi.R_ARM64_PCREL_LDST16 || 550 rt == objabi.R_ARM64_PCREL_LDST32 || rt == objabi.R_ARM64_PCREL_LDST64 || 551 rt == objabi.R_ADDRARM64 || rt == objabi.R_ARM64_GOTPCREL { 552 if ldr.SymDynid(rs) < 0 { 553 ldr.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymType(rs), ldr.SymType(rs)) 554 return false 555 } 556 557 v = uint32(ldr.SymDynid(rs)) 558 v |= 1 << 27 // external relocation 559 } else { 560 v = uint32(ldr.SymSect(rs).Extnum) 561 if v == 0 { 562 ldr.Errorf(s, "reloc %d (%s) to symbol %s in non-macho section %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymSect(rs).Name, ldr.SymType(rs), ldr.SymType(rs)) 563 return false 564 } 565 } 566 567 switch rt { 568 default: 569 return false 570 case objabi.R_ADDR: 571 v |= ld.MACHO_ARM64_RELOC_UNSIGNED << 28 572 case objabi.R_CALLARM64: 573 if xadd != 0 { 574 // Addend should be handled above via label symbols. 575 ldr.Errorf(s, "unexpected non-zero addend: %s+%d", ldr.SymName(rs), xadd) 576 } 577 v |= 1 << 24 // pc-relative bit 578 v |= ld.MACHO_ARM64_RELOC_BRANCH26 << 28 579 case objabi.R_ADDRARM64, 580 objabi.R_ARM64_PCREL_LDST8, 581 objabi.R_ARM64_PCREL_LDST16, 582 objabi.R_ARM64_PCREL_LDST32, 583 objabi.R_ARM64_PCREL_LDST64: 584 siz = 4 585 // Two relocation entries: MACHO_ARM64_RELOC_PAGEOFF12 MACHO_ARM64_RELOC_PAGE21 586 // if r.Xadd is non-zero, add two MACHO_ARM64_RELOC_ADDEND. 587 if r.Xadd != 0 { 588 out.Write32(uint32(sectoff + 4)) 589 out.Write32((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(xadd&0xffffff)) 590 } 591 out.Write32(uint32(sectoff + 4)) 592 out.Write32(v | (ld.MACHO_ARM64_RELOC_PAGEOFF12 << 28) | (2 << 25)) 593 if r.Xadd != 0 { 594 out.Write32(uint32(sectoff)) 595 out.Write32((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(xadd&0xffffff)) 596 } 597 v |= 1 << 24 // pc-relative bit 598 v |= ld.MACHO_ARM64_RELOC_PAGE21 << 28 599 case objabi.R_ARM64_GOTPCREL: 600 siz = 4 601 // Two relocation entries: MACHO_ARM64_RELOC_GOT_LOAD_PAGEOFF12 MACHO_ARM64_RELOC_GOT_LOAD_PAGE21 602 // if r.Xadd is non-zero, add two MACHO_ARM64_RELOC_ADDEND. 603 if r.Xadd != 0 { 604 out.Write32(uint32(sectoff + 4)) 605 out.Write32((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(xadd&0xffffff)) 606 } 607 out.Write32(uint32(sectoff + 4)) 608 out.Write32(v | (ld.MACHO_ARM64_RELOC_GOT_LOAD_PAGEOFF12 << 28) | (2 << 25)) 609 if r.Xadd != 0 { 610 out.Write32(uint32(sectoff)) 611 out.Write32((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(xadd&0xffffff)) 612 } 613 v |= 1 << 24 // pc-relative bit 614 v |= ld.MACHO_ARM64_RELOC_GOT_LOAD_PAGE21 << 28 615 } 616 617 switch siz { 618 default: 619 return false 620 case 1: 621 v |= 0 << 25 622 case 2: 623 v |= 1 << 25 624 case 4: 625 v |= 2 << 25 626 case 8: 627 v |= 3 << 25 628 } 629 630 out.Write32(uint32(sectoff)) 631 out.Write32(v) 632 return true 633 } 634 635 func pereloc1(arch *sys.Arch, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, sectoff int64) bool { 636 rs := r.Xsym 637 rt := r.Type 638 639 if (rt == objabi.R_ADDRARM64 || rt == objabi.R_ARM64_PCREL_LDST8 || rt == objabi.R_ARM64_PCREL_LDST16 || 640 rt == objabi.R_ARM64_PCREL_LDST32 || rt == objabi.R_ARM64_PCREL_LDST64) && r.Xadd != signext21(r.Xadd) { 641 // If the relocation target would overflow the addend, then target 642 // a linker-manufactured label symbol with a smaller addend instead. 643 label := ldr.Lookup(offsetLabelName(ldr, rs, r.Xadd/peRelocLimit*peRelocLimit), ldr.SymVersion(rs)) 644 if label == 0 { 645 ldr.Errorf(s, "invalid relocation: %v %s+0x%x", rt, ldr.SymName(rs), r.Xadd) 646 return false 647 } 648 rs = label 649 } 650 if rt == objabi.R_CALLARM64 && r.Xadd != 0 { 651 label := ldr.Lookup(offsetLabelName(ldr, rs, r.Xadd), ldr.SymVersion(rs)) 652 if label == 0 { 653 ldr.Errorf(s, "invalid relocation: %v %s+0x%x", rt, ldr.SymName(rs), r.Xadd) 654 return false 655 } 656 rs = label 657 } 658 symdynid := ldr.SymDynid(rs) 659 if symdynid < 0 { 660 ldr.Errorf(s, "reloc %d (%s) to non-coff symbol %s type=%d (%s)", rt, sym.RelocName(arch, rt), ldr.SymName(rs), ldr.SymType(rs), ldr.SymType(rs)) 661 return false 662 } 663 664 switch rt { 665 default: 666 return false 667 668 case objabi.R_DWARFSECREF: 669 out.Write32(uint32(sectoff)) 670 out.Write32(uint32(symdynid)) 671 out.Write16(ld.IMAGE_REL_ARM64_SECREL) 672 673 case objabi.R_ADDR: 674 out.Write32(uint32(sectoff)) 675 out.Write32(uint32(symdynid)) 676 if r.Size == 8 { 677 out.Write16(ld.IMAGE_REL_ARM64_ADDR64) 678 } else { 679 out.Write16(ld.IMAGE_REL_ARM64_ADDR32) 680 } 681 682 case objabi.R_ADDRARM64: 683 // Note: r.Xadd has been taken care of below, in archreloc. 684 out.Write32(uint32(sectoff)) 685 out.Write32(uint32(symdynid)) 686 out.Write16(ld.IMAGE_REL_ARM64_PAGEBASE_REL21) 687 688 out.Write32(uint32(sectoff + 4)) 689 out.Write32(uint32(symdynid)) 690 out.Write16(ld.IMAGE_REL_ARM64_PAGEOFFSET_12A) 691 692 case objabi.R_ARM64_PCREL_LDST8, 693 objabi.R_ARM64_PCREL_LDST16, 694 objabi.R_ARM64_PCREL_LDST32, 695 objabi.R_ARM64_PCREL_LDST64: 696 // Note: r.Xadd has been taken care of below, in archreloc. 697 out.Write32(uint32(sectoff)) 698 out.Write32(uint32(symdynid)) 699 out.Write16(ld.IMAGE_REL_ARM64_PAGEBASE_REL21) 700 701 out.Write32(uint32(sectoff + 4)) 702 out.Write32(uint32(symdynid)) 703 out.Write16(ld.IMAGE_REL_ARM64_PAGEOFFSET_12L) 704 705 case objabi.R_CALLARM64: 706 // Note: r.Xadd has been taken care of above, by using a label pointing into the middle of the function. 707 out.Write32(uint32(sectoff)) 708 out.Write32(uint32(symdynid)) 709 out.Write16(ld.IMAGE_REL_ARM64_BRANCH26) 710 } 711 712 return true 713 } 714 715 func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) (int64, int, bool) { 716 const noExtReloc = 0 717 const isOk = true 718 719 rs := r.Sym() 720 721 if target.IsExternal() { 722 nExtReloc := 0 723 switch rt := r.Type(); rt { 724 default: 725 case objabi.R_ARM64_GOTPCREL, 726 objabi.R_ARM64_PCREL_LDST8, 727 objabi.R_ARM64_PCREL_LDST16, 728 objabi.R_ARM64_PCREL_LDST32, 729 objabi.R_ARM64_PCREL_LDST64, 730 objabi.R_ADDRARM64: 731 732 // set up addend for eventual relocation via outer symbol. 733 rs, off := ld.FoldSubSymbolOffset(ldr, rs) 734 xadd := r.Add() + off 735 rst := ldr.SymType(rs) 736 if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && ldr.SymSect(rs) == nil { 737 ldr.Errorf(s, "missing section for %s", ldr.SymName(rs)) 738 } 739 740 nExtReloc = 2 // need two ELF/Mach-O relocations. see elfreloc1/machoreloc1 741 if target.IsDarwin() && xadd != 0 { 742 nExtReloc = 4 // need another two relocations for non-zero addend 743 } 744 745 if target.IsWindows() { 746 var o0, o1 uint32 747 if target.IsBigEndian() { 748 o0 = uint32(val >> 32) 749 o1 = uint32(val) 750 } else { 751 o0 = uint32(val) 752 o1 = uint32(val >> 32) 753 } 754 755 // The first instruction (ADRP) has a 21-bit immediate field, 756 // and the second (ADD or LD/ST) has a 12-bit immediate field. 757 // The first instruction is only for high bits, but to get the carry bits right we have 758 // to put the full addend, including the bottom 12 bits again. 759 // That limits the distance of any addend to only 21 bits. 760 // But we assume that ADRP's top bit will be interpreted as a sign bit, 761 // so we only use 20 bits. 762 // pereloc takes care of introducing new symbol labels 763 // every megabyte for longer relocations. 764 xadd := uint32(xadd) 765 o0 |= (xadd&3)<<29 | (xadd&0xffffc)<<3 766 switch rt { 767 case objabi.R_ARM64_PCREL_LDST8, objabi.R_ADDRARM64: 768 o1 |= (xadd & 0xfff) << 10 769 case objabi.R_ARM64_PCREL_LDST16: 770 if xadd&0x1 != 0 { 771 ldr.Errorf(s, "offset for 16-bit load/store has unaligned value %d", xadd&0xfff) 772 } 773 o1 |= ((xadd & 0xfff) >> 1) << 10 774 case objabi.R_ARM64_PCREL_LDST32: 775 if xadd&0x3 != 0 { 776 ldr.Errorf(s, "offset for 32-bit load/store has unaligned value %d", xadd&0xfff) 777 } 778 o1 |= ((xadd & 0xfff) >> 2) << 10 779 case objabi.R_ARM64_PCREL_LDST64: 780 if xadd&0x7 != 0 { 781 ldr.Errorf(s, "offset for 64-bit load/store has unaligned value %d", xadd&0xfff) 782 } 783 o1 |= ((xadd & 0xfff) >> 3) << 10 784 } 785 786 if target.IsBigEndian() { 787 val = int64(o0)<<32 | int64(o1) 788 } else { 789 val = int64(o1)<<32 | int64(o0) 790 } 791 } 792 793 return val, nExtReloc, isOk 794 795 case objabi.R_CALLARM64: 796 nExtReloc = 1 797 return val, nExtReloc, isOk 798 799 case objabi.R_ARM64_TLS_LE: 800 nExtReloc = 1 801 return val, nExtReloc, isOk 802 803 case objabi.R_ARM64_TLS_IE: 804 nExtReloc = 2 // need two ELF relocations. see elfreloc1 805 return val, nExtReloc, isOk 806 807 case objabi.R_ADDR: 808 if target.IsWindows() && r.Add() != 0 { 809 if r.Siz() == 8 { 810 val = r.Add() 811 } else if target.IsBigEndian() { 812 val = int64(uint32(val)) | int64(r.Add())<<32 813 } else { 814 val = val>>32<<32 | int64(uint32(r.Add())) 815 } 816 return val, 1, true 817 } 818 } 819 } 820 821 switch rt := r.Type(); rt { 822 case objabi.R_ADDRARM64, 823 objabi.R_ARM64_PCREL_LDST8, 824 objabi.R_ARM64_PCREL_LDST16, 825 objabi.R_ARM64_PCREL_LDST32, 826 objabi.R_ARM64_PCREL_LDST64: 827 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 828 if t >= 1<<32 || t < -1<<32 { 829 ldr.Errorf(s, "program too large, address relocation distance = %d", t) 830 } 831 832 var o0, o1 uint32 833 834 if target.IsBigEndian() { 835 o0 = uint32(val >> 32) 836 o1 = uint32(val) 837 } else { 838 o0 = uint32(val) 839 o1 = uint32(val >> 32) 840 } 841 842 o0 |= (uint32((t>>12)&3) << 29) | (uint32((t>>12>>2)&0x7ffff) << 5) 843 switch rt { 844 case objabi.R_ARM64_PCREL_LDST8, objabi.R_ADDRARM64: 845 o1 |= uint32(t&0xfff) << 10 846 case objabi.R_ARM64_PCREL_LDST16: 847 if t&0x1 != 0 { 848 ldr.Errorf(s, "offset for 16-bit load/store has unaligned value %d", t&0xfff) 849 } 850 o1 |= (uint32(t&0xfff) >> 1) << 10 851 case objabi.R_ARM64_PCREL_LDST32: 852 if t&0x3 != 0 { 853 ldr.Errorf(s, "offset for 32-bit load/store has unaligned value %d", t&0xfff) 854 } 855 o1 |= (uint32(t&0xfff) >> 2) << 10 856 case objabi.R_ARM64_PCREL_LDST64: 857 if t&0x7 != 0 { 858 ldr.Errorf(s, "offset for 64-bit load/store has unaligned value %d", t&0xfff) 859 } 860 o1 |= (uint32(t&0xfff) >> 3) << 10 861 } 862 863 // when laid out, the instruction order must always be o1, o2. 864 if target.IsBigEndian() { 865 return int64(o0)<<32 | int64(o1), noExtReloc, true 866 } 867 return int64(o1)<<32 | int64(o0), noExtReloc, true 868 869 case objabi.R_ARM64_TLS_LE: 870 if target.IsDarwin() { 871 ldr.Errorf(s, "TLS reloc on unsupported OS %v", target.HeadType) 872 } 873 // The TCB is two pointers. This is not documented anywhere, but is 874 // de facto part of the ABI. 875 v := ldr.SymValue(rs) + int64(2*target.Arch.PtrSize) 876 if v < 0 || v >= 32678 { 877 ldr.Errorf(s, "TLS offset out of range %d", v) 878 } 879 return val | (v << 5), noExtReloc, true 880 881 case objabi.R_ARM64_TLS_IE: 882 if target.IsPIE() && target.IsElf() { 883 // We are linking the final executable, so we 884 // can optimize any TLS IE relocation to LE. 885 886 if !target.IsLinux() { 887 ldr.Errorf(s, "TLS reloc on unsupported OS %v", target.HeadType) 888 } 889 890 // The TCB is two pointers. This is not documented anywhere, but is 891 // de facto part of the ABI. 892 v := ldr.SymAddr(rs) + int64(2*target.Arch.PtrSize) + r.Add() 893 if v < 0 || v >= 32678 { 894 ldr.Errorf(s, "TLS offset out of range %d", v) 895 } 896 897 var o0, o1 uint32 898 if target.IsBigEndian() { 899 o0 = uint32(val >> 32) 900 o1 = uint32(val) 901 } else { 902 o0 = uint32(val) 903 o1 = uint32(val >> 32) 904 } 905 906 // R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 907 // turn ADRP to MOVZ 908 o0 = 0xd2a00000 | uint32(o0&0x1f) | (uint32((v>>16)&0xffff) << 5) 909 // R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC 910 // turn LD64 to MOVK 911 if v&3 != 0 { 912 ldr.Errorf(s, "invalid address: %x for relocation type: R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC", v) 913 } 914 o1 = 0xf2800000 | uint32(o1&0x1f) | (uint32(v&0xffff) << 5) 915 916 // when laid out, the instruction order must always be o0, o1. 917 if target.IsBigEndian() { 918 return int64(o0)<<32 | int64(o1), noExtReloc, isOk 919 } 920 return int64(o1)<<32 | int64(o0), noExtReloc, isOk 921 } else { 922 log.Fatalf("cannot handle R_ARM64_TLS_IE (sym %s) when linking internally", ldr.SymName(s)) 923 } 924 925 case objabi.R_CALLARM64: 926 var t int64 927 if ldr.SymType(rs) == sym.SDYNIMPORT { 928 t = (ldr.SymAddr(syms.PLT) + r.Add()) - (ldr.SymValue(s) + int64(r.Off())) 929 } else { 930 t = (ldr.SymAddr(rs) + r.Add()) - (ldr.SymValue(s) + int64(r.Off())) 931 } 932 if t >= 1<<27 || t < -1<<27 { 933 ldr.Errorf(s, "program too large, call relocation distance = %d", t) 934 } 935 return val | ((t >> 2) & 0x03ffffff), noExtReloc, true 936 937 case objabi.R_ARM64_GOT: 938 if (val>>24)&0x9f == 0x90 { 939 // R_AARCH64_ADR_GOT_PAGE 940 // patch instruction: adrp 941 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 942 if t >= 1<<32 || t < -1<<32 { 943 ldr.Errorf(s, "program too large, address relocation distance = %d", t) 944 } 945 var o0 uint32 946 o0 |= (uint32((t>>12)&3) << 29) | (uint32((t>>12>>2)&0x7ffff) << 5) 947 return val | int64(o0), noExtReloc, isOk 948 } else if val>>24 == 0xf9 { 949 // R_AARCH64_LD64_GOT_LO12_NC 950 // patch instruction: ldr 951 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 952 if t&7 != 0 { 953 ldr.Errorf(s, "invalid address: %x for relocation type: R_AARCH64_LD64_GOT_LO12_NC", t) 954 } 955 var o1 uint32 956 o1 |= uint32(t&0xfff) << (10 - 3) 957 return val | int64(uint64(o1)), noExtReloc, isOk 958 } else { 959 ldr.Errorf(s, "unsupported instruction for %x R_GOTARM64", val) 960 } 961 962 case objabi.R_ARM64_PCREL: 963 if (val>>24)&0x9f == 0x90 { 964 // R_AARCH64_ADR_PREL_PG_HI21 965 // patch instruction: adrp 966 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 967 if t >= 1<<32 || t < -1<<32 { 968 ldr.Errorf(s, "program too large, address relocation distance = %d", t) 969 } 970 o0 := (uint32((t>>12)&3) << 29) | (uint32((t>>12>>2)&0x7ffff) << 5) 971 return val | int64(o0), noExtReloc, isOk 972 } else if (val>>24)&0x9f == 0x91 { 973 // ELF R_AARCH64_ADD_ABS_LO12_NC or Mach-O ARM64_RELOC_PAGEOFF12 974 // patch instruction: add 975 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 976 o1 := uint32(t&0xfff) << 10 977 return val | int64(o1), noExtReloc, isOk 978 } else if (val>>24)&0x3b == 0x39 { 979 // Mach-O ARM64_RELOC_PAGEOFF12 980 // patch ldr/str(b/h/w/d/q) (integer or vector) instructions, which have different scaling factors. 981 // Mach-O uses same relocation type for them. 982 shift := uint32(val) >> 30 983 if shift == 0 && (val>>20)&0x048 == 0x048 { // 128-bit vector load 984 shift = 4 985 } 986 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 987 if t&(1<<shift-1) != 0 { 988 ldr.Errorf(s, "invalid address: %x for relocation type: ARM64_RELOC_PAGEOFF12", t) 989 } 990 o1 := (uint32(t&0xfff) >> shift) << 10 991 return val | int64(o1), noExtReloc, isOk 992 } else { 993 ldr.Errorf(s, "unsupported instruction for %x R_ARM64_PCREL", val) 994 } 995 996 case objabi.R_ARM64_LDST8: 997 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 998 o0 := uint32(t&0xfff) << 10 999 return val | int64(o0), noExtReloc, true 1000 1001 case objabi.R_ARM64_LDST16: 1002 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 1003 if t&1 != 0 { 1004 ldr.Errorf(s, "invalid address: %x for relocation type: R_AARCH64_LDST16_ABS_LO12_NC", t) 1005 } 1006 o0 := (uint32(t&0xfff) >> 1) << 10 1007 return val | int64(o0), noExtReloc, true 1008 1009 case objabi.R_ARM64_LDST32: 1010 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 1011 if t&3 != 0 { 1012 ldr.Errorf(s, "invalid address: %x for relocation type: R_AARCH64_LDST32_ABS_LO12_NC", t) 1013 } 1014 o0 := (uint32(t&0xfff) >> 2) << 10 1015 return val | int64(o0), noExtReloc, true 1016 1017 case objabi.R_ARM64_LDST64: 1018 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 1019 if t&7 != 0 { 1020 ldr.Errorf(s, "invalid address: %x for relocation type: R_AARCH64_LDST64_ABS_LO12_NC", t) 1021 } 1022 o0 := (uint32(t&0xfff) >> 3) << 10 1023 return val | int64(o0), noExtReloc, true 1024 1025 case objabi.R_ARM64_LDST128: 1026 t := ldr.SymAddr(rs) + r.Add() - ((ldr.SymValue(s) + int64(r.Off())) &^ 0xfff) 1027 if t&15 != 0 { 1028 ldr.Errorf(s, "invalid address: %x for relocation type: R_AARCH64_LDST128_ABS_LO12_NC", t) 1029 } 1030 o0 := (uint32(t&0xfff) >> 4) << 10 1031 return val | int64(o0), noExtReloc, true 1032 } 1033 1034 return val, 0, false 1035 } 1036 1037 func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64, []byte) int64 { 1038 log.Fatalf("unexpected relocation variant") 1039 return -1 1040 } 1041 1042 func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) { 1043 switch rt := r.Type(); rt { 1044 case objabi.R_ARM64_GOTPCREL, 1045 objabi.R_ARM64_PCREL_LDST8, 1046 objabi.R_ARM64_PCREL_LDST16, 1047 objabi.R_ARM64_PCREL_LDST32, 1048 objabi.R_ARM64_PCREL_LDST64, 1049 objabi.R_ADDRARM64: 1050 rr := ld.ExtrelocViaOuterSym(ldr, r, s) 1051 return rr, true 1052 case objabi.R_CALLARM64, 1053 objabi.R_ARM64_TLS_LE, 1054 objabi.R_ARM64_TLS_IE: 1055 return ld.ExtrelocSimple(ldr, r), true 1056 } 1057 return loader.ExtReloc{}, false 1058 } 1059 1060 func elfsetupplt(ctxt *ld.Link, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym) { 1061 if plt.Size() == 0 { 1062 // stp x16, x30, [sp, #-16]! 1063 // identifying information 1064 plt.AddUint32(ctxt.Arch, 0xa9bf7bf0) 1065 1066 // the following two instructions (adrp + ldr) load *got[2] into x17 1067 // adrp x16, &got[0] 1068 plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 16, objabi.R_ARM64_GOT, 4) 1069 plt.SetUint32(ctxt.Arch, plt.Size()-4, 0x90000010) 1070 1071 // <imm> is the offset value of &got[2] to &got[0], the same below 1072 // ldr x17, [x16, <imm>] 1073 plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 16, objabi.R_ARM64_GOT, 4) 1074 plt.SetUint32(ctxt.Arch, plt.Size()-4, 0xf9400211) 1075 1076 // add x16, x16, <imm> 1077 plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 16, objabi.R_ARM64_PCREL, 4) 1078 plt.SetUint32(ctxt.Arch, plt.Size()-4, 0x91000210) 1079 1080 // br x17 1081 plt.AddUint32(ctxt.Arch, 0xd61f0220) 1082 1083 // 3 nop for place holder 1084 plt.AddUint32(ctxt.Arch, 0xd503201f) 1085 plt.AddUint32(ctxt.Arch, 0xd503201f) 1086 plt.AddUint32(ctxt.Arch, 0xd503201f) 1087 1088 // check gotplt.size == 0 1089 if gotplt.Size() != 0 { 1090 ctxt.Errorf(gotplt.Sym(), "got.plt is not empty at the very beginning") 1091 } 1092 gotplt.AddAddrPlus(ctxt.Arch, dynamic, 0) 1093 1094 gotplt.AddUint64(ctxt.Arch, 0) 1095 gotplt.AddUint64(ctxt.Arch, 0) 1096 } 1097 } 1098 1099 func addpltsym(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) { 1100 if ldr.SymPlt(s) >= 0 { 1101 return 1102 } 1103 1104 ld.Adddynsym(ldr, target, syms, s) 1105 1106 if target.IsElf() { 1107 plt := ldr.MakeSymbolUpdater(syms.PLT) 1108 gotplt := ldr.MakeSymbolUpdater(syms.GOTPLT) 1109 rela := ldr.MakeSymbolUpdater(syms.RelaPLT) 1110 if plt.Size() == 0 { 1111 panic("plt is not set up") 1112 } 1113 1114 // adrp x16, &got.plt[0] 1115 plt.AddAddrPlus4(target.Arch, gotplt.Sym(), gotplt.Size()) 1116 plt.SetUint32(target.Arch, plt.Size()-4, 0x90000010) 1117 relocs := plt.Relocs() 1118 plt.SetRelocType(relocs.Count()-1, objabi.R_ARM64_GOT) 1119 1120 // <offset> is the offset value of &got.plt[n] to &got.plt[0] 1121 // ldr x17, [x16, <offset>] 1122 plt.AddAddrPlus4(target.Arch, gotplt.Sym(), gotplt.Size()) 1123 plt.SetUint32(target.Arch, plt.Size()-4, 0xf9400211) 1124 relocs = plt.Relocs() 1125 plt.SetRelocType(relocs.Count()-1, objabi.R_ARM64_GOT) 1126 1127 // add x16, x16, <offset> 1128 plt.AddAddrPlus4(target.Arch, gotplt.Sym(), gotplt.Size()) 1129 plt.SetUint32(target.Arch, plt.Size()-4, 0x91000210) 1130 relocs = plt.Relocs() 1131 plt.SetRelocType(relocs.Count()-1, objabi.R_ARM64_PCREL) 1132 1133 // br x17 1134 plt.AddUint32(target.Arch, 0xd61f0220) 1135 1136 // add to got.plt: pointer to plt[0] 1137 gotplt.AddAddrPlus(target.Arch, plt.Sym(), 0) 1138 1139 // rela 1140 rela.AddAddrPlus(target.Arch, gotplt.Sym(), gotplt.Size()-8) 1141 sDynid := ldr.SymDynid(s) 1142 1143 rela.AddUint64(target.Arch, elf.R_INFO(uint32(sDynid), uint32(elf.R_AARCH64_JUMP_SLOT))) 1144 rela.AddUint64(target.Arch, 0) 1145 1146 ldr.SetPlt(s, int32(plt.Size()-16)) 1147 } else if target.IsDarwin() { 1148 ld.AddGotSym(target, ldr, syms, s, 0) 1149 1150 sDynid := ldr.SymDynid(s) 1151 lep := ldr.MakeSymbolUpdater(syms.LinkEditPLT) 1152 lep.AddUint32(target.Arch, uint32(sDynid)) 1153 1154 plt := ldr.MakeSymbolUpdater(syms.PLT) 1155 ldr.SetPlt(s, int32(plt.Size())) 1156 1157 // adrp x16, GOT 1158 plt.AddUint32(target.Arch, 0x90000010) 1159 r, _ := plt.AddRel(objabi.R_ARM64_GOT) 1160 r.SetOff(int32(plt.Size() - 4)) 1161 r.SetSiz(4) 1162 r.SetSym(syms.GOT) 1163 r.SetAdd(int64(ldr.SymGot(s))) 1164 1165 // ldr x17, [x16, <offset>] 1166 plt.AddUint32(target.Arch, 0xf9400211) 1167 r, _ = plt.AddRel(objabi.R_ARM64_GOT) 1168 r.SetOff(int32(plt.Size() - 4)) 1169 r.SetSiz(4) 1170 r.SetSym(syms.GOT) 1171 r.SetAdd(int64(ldr.SymGot(s))) 1172 1173 // br x17 1174 plt.AddUint32(target.Arch, 0xd61f0220) 1175 } else { 1176 ldr.Errorf(s, "addpltsym: unsupported binary format") 1177 } 1178 } 1179 1180 const ( 1181 machoRelocLimit = 1 << 23 1182 peRelocLimit = 1 << 20 1183 ) 1184 1185 func gensymlate(ctxt *ld.Link, ldr *loader.Loader) { 1186 // When external linking on darwin, Mach-O relocation has only signed 24-bit 1187 // addend. For large symbols, we generate "label" symbols in the middle, so 1188 // that relocations can target them with smaller addends. 1189 // On Windows, we only get 21 bits, again (presumably) signed. 1190 // Also, on Windows (always) and Darwin (for very large binaries), the external 1191 // linker does't support CALL relocations with addend, so we generate "label" 1192 // symbols for functions of which we can target the middle (Duff's devices). 1193 if !ctxt.IsDarwin() && !ctxt.IsWindows() || !ctxt.IsExternal() { 1194 return 1195 } 1196 1197 limit := int64(machoRelocLimit) 1198 if ctxt.IsWindows() { 1199 limit = peRelocLimit 1200 } 1201 1202 // addLabelSyms adds "label" symbols at s+limit, s+2*limit, etc. 1203 addLabelSyms := func(s loader.Sym, limit, sz int64) { 1204 v := ldr.SymValue(s) 1205 for off := limit; off < sz; off += limit { 1206 p := ldr.LookupOrCreateSym(offsetLabelName(ldr, s, off), ldr.SymVersion(s)) 1207 ldr.SetAttrReachable(p, true) 1208 ldr.SetSymValue(p, v+off) 1209 ldr.SetSymSect(p, ldr.SymSect(s)) 1210 if ctxt.IsDarwin() { 1211 ld.AddMachoSym(ldr, p) 1212 } else if ctxt.IsWindows() { 1213 ld.AddPELabelSym(ldr, p) 1214 } else { 1215 panic("missing case in gensymlate") 1216 } 1217 // fmt.Printf("gensymlate %s %x\n", ldr.SymName(p), ldr.SymValue(p)) 1218 } 1219 } 1220 1221 // Generate symbol names for every offset we need in duffcopy/duffzero (only 64 each). 1222 if s := ldr.Lookup("runtime.duffcopy", sym.SymVerABIInternal); s != 0 && ldr.AttrReachable(s) { 1223 addLabelSyms(s, 8, 8*64) 1224 } 1225 if s := ldr.Lookup("runtime.duffzero", sym.SymVerABIInternal); s != 0 && ldr.AttrReachable(s) { 1226 addLabelSyms(s, 4, 4*64) 1227 } 1228 1229 if ctxt.IsDarwin() { 1230 big := false 1231 for _, seg := range ld.Segments { 1232 if seg.Length >= machoRelocLimit { 1233 big = true 1234 break 1235 } 1236 } 1237 if !big { 1238 return // skip work if nothing big 1239 } 1240 } 1241 1242 for s, n := loader.Sym(1), loader.Sym(ldr.NSym()); s < n; s++ { 1243 if !ldr.AttrReachable(s) { 1244 continue 1245 } 1246 t := ldr.SymType(s) 1247 if t == sym.STEXT { 1248 // Except for Duff's devices (handled above), we don't 1249 // target the middle of a function. 1250 continue 1251 } 1252 if t >= sym.SDWARFSECT { 1253 continue // no need to add label for DWARF symbols 1254 } 1255 sz := ldr.SymSize(s) 1256 if sz <= limit { 1257 continue 1258 } 1259 addLabelSyms(s, limit, sz) 1260 } 1261 1262 // Also for carrier symbols (for which SymSize is 0) 1263 for _, ss := range ld.CarrierSymByType { 1264 if ss.Sym != 0 && ss.Size > limit { 1265 addLabelSyms(ss.Sym, limit, ss.Size) 1266 } 1267 } 1268 } 1269 1270 // offsetLabelName returns the name of the "label" symbol used for a 1271 // relocation targeting s+off. The label symbols is used on Darwin/Windows 1272 // when external linking, so that the addend fits in a Mach-O/PE relocation. 1273 func offsetLabelName(ldr *loader.Loader, s loader.Sym, off int64) string { 1274 if off>>20<<20 == off { 1275 return fmt.Sprintf("%s+%dMB", ldr.SymExtname(s), off>>20) 1276 } 1277 return fmt.Sprintf("%s+%d", ldr.SymExtname(s), off) 1278 } 1279 1280 // Convert the direct jump relocation r to refer to a trampoline if the target is too far. 1281 func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) { 1282 relocs := ldr.Relocs(s) 1283 r := relocs.At(ri) 1284 const pcrel = 1 1285 switch r.Type() { 1286 case objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_CALL26), 1287 objabi.ElfRelocOffset + objabi.RelocType(elf.R_AARCH64_JUMP26), 1288 objabi.MachoRelocOffset + ld.MACHO_ARM64_RELOC_BRANCH26*2 + pcrel: 1289 // Host object relocations that will be turned into a PLT call. 1290 // The PLT may be too far. Insert a trampoline for them. 1291 fallthrough 1292 case objabi.R_CALLARM64: 1293 var t int64 1294 // ldr.SymValue(rs) == 0 indicates a cross-package jump to a function that is not yet 1295 // laid out. Conservatively use a trampoline. This should be rare, as we lay out packages 1296 // in dependency order. 1297 if ldr.SymValue(rs) != 0 { 1298 t = ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off())) 1299 } 1300 if t >= 1<<27 || t < -1<<27 || ldr.SymValue(rs) == 0 || (*ld.FlagDebugTramp > 1 && (ldr.SymPkg(s) == "" || ldr.SymPkg(s) != ldr.SymPkg(rs))) { 1301 // direct call too far, need to insert trampoline. 1302 // look up existing trampolines first. if we found one within the range 1303 // of direct call, we can reuse it. otherwise create a new one. 1304 var tramp loader.Sym 1305 for i := 0; ; i++ { 1306 oName := ldr.SymName(rs) 1307 name := oName + fmt.Sprintf("%+x-tramp%d", r.Add(), i) 1308 tramp = ldr.LookupOrCreateSym(name, int(ldr.SymVersion(rs))) 1309 ldr.SetAttrReachable(tramp, true) 1310 if ldr.SymType(tramp) == sym.SDYNIMPORT { 1311 // don't reuse trampoline defined in other module 1312 continue 1313 } 1314 if oName == "runtime.deferreturn" { 1315 ldr.SetIsDeferReturnTramp(tramp, true) 1316 } 1317 if ldr.SymValue(tramp) == 0 { 1318 // either the trampoline does not exist -- we need to create one, 1319 // or found one the address which is not assigned -- this will be 1320 // laid down immediately after the current function. use this one. 1321 break 1322 } 1323 1324 t = ldr.SymValue(tramp) - (ldr.SymValue(s) + int64(r.Off())) 1325 if t >= -1<<27 && t < 1<<27 { 1326 // found an existing trampoline that is not too far 1327 // we can just use it 1328 break 1329 } 1330 } 1331 if ldr.SymType(tramp) == 0 { 1332 // trampoline does not exist, create one 1333 trampb := ldr.MakeSymbolUpdater(tramp) 1334 ctxt.AddTramp(trampb) 1335 if ldr.SymType(rs) == sym.SDYNIMPORT { 1336 if r.Add() != 0 { 1337 ctxt.Errorf(s, "nonzero addend for DYNIMPORT call: %v+%d", ldr.SymName(rs), r.Add()) 1338 } 1339 gentrampgot(ctxt, ldr, trampb, rs) 1340 } else { 1341 gentramp(ctxt, ldr, trampb, rs, r.Add()) 1342 } 1343 } 1344 // modify reloc to point to tramp, which will be resolved later 1345 sb := ldr.MakeSymbolUpdater(s) 1346 relocs := sb.Relocs() 1347 r := relocs.At(ri) 1348 r.SetSym(tramp) 1349 r.SetAdd(0) // clear the offset embedded in the instruction 1350 } 1351 default: 1352 ctxt.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type())) 1353 } 1354 } 1355 1356 // generate a trampoline to target+offset. 1357 func gentramp(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) { 1358 tramp.SetSize(12) // 3 instructions 1359 P := make([]byte, tramp.Size()) 1360 o1 := uint32(0x90000010) // adrp x16, target 1361 o2 := uint32(0x91000210) // add x16, pc-relative-offset 1362 o3 := uint32(0xd61f0200) // br x16 1363 ctxt.Arch.ByteOrder.PutUint32(P, o1) 1364 ctxt.Arch.ByteOrder.PutUint32(P[4:], o2) 1365 ctxt.Arch.ByteOrder.PutUint32(P[8:], o3) 1366 tramp.SetData(P) 1367 1368 r, _ := tramp.AddRel(objabi.R_ADDRARM64) 1369 r.SetSiz(8) 1370 r.SetSym(target) 1371 r.SetAdd(offset) 1372 } 1373 1374 // generate a trampoline to target+offset for a DYNIMPORT symbol via GOT. 1375 func gentrampgot(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym) { 1376 tramp.SetSize(12) // 3 instructions 1377 P := make([]byte, tramp.Size()) 1378 o1 := uint32(0x90000010) // adrp x16, target@GOT 1379 o2 := uint32(0xf9400210) // ldr x16, [x16, offset] 1380 o3 := uint32(0xd61f0200) // br x16 1381 ctxt.Arch.ByteOrder.PutUint32(P, o1) 1382 ctxt.Arch.ByteOrder.PutUint32(P[4:], o2) 1383 ctxt.Arch.ByteOrder.PutUint32(P[8:], o3) 1384 tramp.SetData(P) 1385 1386 r, _ := tramp.AddRel(objabi.R_ARM64_GOTPCREL) 1387 r.SetSiz(8) 1388 r.SetSym(target) 1389 }