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