LLVM 22.0.0git
RuntimeDyldELF.cpp
Go to the documentation of this file.
1//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of ELF support for the MC-JIT runtime dynamic linker.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RuntimeDyldELF.h"
15#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Endian.h"
23
24using namespace llvm;
25using namespace llvm::object;
26using namespace llvm::support::endian;
27
28#define DEBUG_TYPE "dyld"
29
30static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); }
31
32static void or32AArch64Imm(void *L, uint64_t Imm) {
33 or32le(L, (Imm & 0xFFF) << 10);
34}
35
36template <class T> static void write(bool isBE, void *P, T V) {
37 isBE ? write<T, llvm::endianness::big>(P, V)
38 : write<T, llvm::endianness::little>(P, V);
39}
40
41static void write32AArch64Addr(void *L, uint64_t Imm) {
42 uint32_t ImmLo = (Imm & 0x3) << 29;
43 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
44 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
45 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
46}
47
48// Return the bits [Start, End] from Val shifted Start bits.
49// For instance, getBits(0xF0, 4, 8) returns 0xF.
50static uint64_t getBits(uint64_t Val, int Start, int End) {
51 uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1;
52 return (Val >> Start) & Mask;
53}
54
55namespace {
56
57template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> {
59
60 typedef typename ELFT::uint addr_type;
61
62 DyldELFObject(ELFObjectFile<ELFT> &&Obj);
63
64public:
67
68 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
69
70 void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr);
71
72 // Methods for type inquiry through isa, cast and dyn_cast
73 static bool classof(const Binary *v) {
74 return (isa<ELFObjectFile<ELFT>>(v) &&
76 }
77 static bool classof(const ELFObjectFile<ELFT> *v) {
78 return v->isDyldType();
79 }
80};
81
82
83
84// The MemoryBuffer passed into this constructor is just a wrapper around the
85// actual memory. Ultimately, the Binary parent class will take ownership of
86// this MemoryBuffer object but not the underlying memory.
87template <class ELFT>
88DyldELFObject<ELFT>::DyldELFObject(ELFObjectFile<ELFT> &&Obj)
89 : ELFObjectFile<ELFT>(std::move(Obj)) {
90 this->isDyldELFObject = true;
91}
92
93template <class ELFT>
95DyldELFObject<ELFT>::create(MemoryBufferRef Wrapper) {
97 if (auto E = Obj.takeError())
98 return std::move(E);
99 std::unique_ptr<DyldELFObject<ELFT>> Ret(
100 new DyldELFObject<ELFT>(std::move(*Obj)));
101 return std::move(Ret);
102}
103
104template <class ELFT>
105void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec,
106 uint64_t Addr) {
107 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
108 Elf_Shdr *shdr =
109 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
110
111 // This assumes the address passed in matches the target address bitness
112 // The template-based type cast handles everything else.
113 shdr->sh_addr = static_cast<addr_type>(Addr);
114}
115
116template <class ELFT>
117void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef,
118 uint64_t Addr) {
119
120 Elf_Sym *sym = const_cast<Elf_Sym *>(
122
123 // This assumes the address passed in matches the target address bitness
124 // The template-based type cast handles everything else.
125 sym->st_value = static_cast<addr_type>(Addr);
126}
127
128class LoadedELFObjectInfo final
129 : public LoadedObjectInfoHelper<LoadedELFObjectInfo,
130 RuntimeDyld::LoadedObjectInfo> {
131public:
132 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
133 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
134
136 getObjectForDebug(const ObjectFile &Obj) const override;
137};
138
139template <typename ELFT>
141createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject,
142 const LoadedELFObjectInfo &L) {
143 typedef typename ELFT::Shdr Elf_Shdr;
144 typedef typename ELFT::uint addr_type;
145
147 DyldELFObject<ELFT>::create(Buffer);
148 if (Error E = ObjOrErr.takeError())
149 return std::move(E);
150
151 std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr);
152
153 // Iterate over all sections in the object.
154 auto SI = SourceObject.section_begin();
155 for (const auto &Sec : Obj->sections()) {
156 Expected<StringRef> NameOrErr = Sec.getName();
157 if (!NameOrErr) {
158 consumeError(NameOrErr.takeError());
159 continue;
160 }
161
162 if (*NameOrErr != "") {
163 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
164 Elf_Shdr *shdr = const_cast<Elf_Shdr *>(
165 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
166
167 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) {
168 // This assumes that the address passed in matches the target address
169 // bitness. The template-based type cast handles everything else.
170 shdr->sh_addr = static_cast<addr_type>(SecLoadAddr);
171 }
172 }
173 ++SI;
174 }
175
176 return std::move(Obj);
177}
178
180createELFDebugObject(const ObjectFile &Obj, const LoadedELFObjectInfo &L) {
181 assert(Obj.isELF() && "Not an ELF object file.");
182
183 std::unique_ptr<MemoryBuffer> Buffer =
185
186 Expected<std::unique_ptr<ObjectFile>> DebugObj(nullptr);
187 handleAllErrors(DebugObj.takeError());
188 if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian())
189 DebugObj =
190 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L);
191 else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian())
192 DebugObj =
193 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L);
194 else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian())
195 DebugObj =
196 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L);
197 else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian())
198 DebugObj =
199 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L);
200 else
201 llvm_unreachable("Unexpected ELF format");
202
203 handleAllErrors(DebugObj.takeError());
204 return OwningBinary<ObjectFile>(std::move(*DebugObj), std::move(Buffer));
205}
206
208LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const {
209 return createELFDebugObject(Obj, *this);
210}
211
212} // anonymous namespace
213
214namespace llvm {
215
218 : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {}
220
222 for (SID EHFrameSID : UnregisteredEHFrameSections) {
223 uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
224 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
225 size_t EHFrameSize = Sections[EHFrameSID].getSize();
226 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
227 }
228 UnregisteredEHFrameSections.clear();
229}
230
231std::unique_ptr<RuntimeDyldELF>
235 switch (Arch) {
236 default:
237 return std::make_unique<RuntimeDyldELF>(MemMgr, Resolver);
238 case Triple::mips:
239 case Triple::mipsel:
240 case Triple::mips64:
241 case Triple::mips64el:
242 return std::make_unique<RuntimeDyldELFMips>(MemMgr, Resolver);
243 }
244}
245
246std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
248 if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
249 return std::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr);
250 else {
251 HasError = true;
252 raw_string_ostream ErrStream(ErrorStr);
253 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
254 return nullptr;
255 }
256}
257
258void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
260 uint32_t Type, int64_t Addend,
261 uint64_t SymOffset) {
262 switch (Type) {
263 default:
264 report_fatal_error("Relocation type not implemented yet!");
265 break;
266 case ELF::R_X86_64_NONE:
267 break;
268 case ELF::R_X86_64_8: {
269 Value += Addend;
270 assert((int64_t)Value <= INT8_MAX && (int64_t)Value >= INT8_MIN);
271 uint8_t TruncatedAddr = (Value & 0xFF);
272 *Section.getAddressWithOffset(Offset) = TruncatedAddr;
273 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
274 << format("%p\n", Section.getAddressWithOffset(Offset)));
275 break;
276 }
277 case ELF::R_X86_64_16: {
278 Value += Addend;
279 assert((int64_t)Value <= INT16_MAX && (int64_t)Value >= INT16_MIN);
280 uint16_t TruncatedAddr = (Value & 0xFFFF);
281 support::ulittle16_t::ref(Section.getAddressWithOffset(Offset)) =
282 TruncatedAddr;
283 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
284 << format("%p\n", Section.getAddressWithOffset(Offset)));
285 break;
286 }
287 case ELF::R_X86_64_64: {
288 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
289 Value + Addend;
290 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
291 << format("%p\n", Section.getAddressWithOffset(Offset)));
292 break;
293 }
294 case ELF::R_X86_64_32:
295 case ELF::R_X86_64_32S: {
296 Value += Addend;
297 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
298 (Type == ELF::R_X86_64_32S &&
299 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
300 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
301 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
302 TruncatedAddr;
303 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
304 << format("%p\n", Section.getAddressWithOffset(Offset)));
305 break;
306 }
307 case ELF::R_X86_64_PC8: {
308 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
309 int64_t RealOffset = Value + Addend - FinalAddress;
310 assert(isInt<8>(RealOffset));
311 int8_t TruncOffset = (RealOffset & 0xFF);
312 Section.getAddress()[Offset] = TruncOffset;
313 break;
314 }
315 case ELF::R_X86_64_PC32: {
316 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
317 int64_t RealOffset = Value + Addend - FinalAddress;
318 assert(isInt<32>(RealOffset));
319 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
320 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
321 TruncOffset;
322 break;
323 }
324 case ELF::R_X86_64_PC64: {
325 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
326 int64_t RealOffset = Value + Addend - FinalAddress;
327 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
328 RealOffset;
329 LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset) << " at "
330 << format("%p\n", FinalAddress));
331 break;
332 }
333 case ELF::R_X86_64_GOTOFF64: {
334 // Compute Value - GOTBase.
335 uint64_t GOTBase = 0;
336 for (const auto &Section : Sections) {
337 if (Section.getName() == ".got") {
338 GOTBase = Section.getLoadAddressWithOffset(0);
339 break;
340 }
341 }
342 assert(GOTBase != 0 && "missing GOT");
343 int64_t GOTOffset = Value - GOTBase + Addend;
344 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = GOTOffset;
345 break;
346 }
347 case ELF::R_X86_64_DTPMOD64: {
348 // We only have one DSO, so the module id is always 1.
349 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 1;
350 break;
351 }
352 case ELF::R_X86_64_DTPOFF64:
353 case ELF::R_X86_64_TPOFF64: {
354 // DTPOFF64 should resolve to the offset in the TLS block, TPOFF64 to the
355 // offset in the *initial* TLS block. Since we are statically linking, all
356 // TLS blocks already exist in the initial block, so resolve both
357 // relocations equally.
358 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
359 Value + Addend;
360 break;
361 }
362 case ELF::R_X86_64_DTPOFF32:
363 case ELF::R_X86_64_TPOFF32: {
364 // As for the (D)TPOFF64 relocations above, both DTPOFF32 and TPOFF32 can
365 // be resolved equally.
366 int64_t RealValue = Value + Addend;
367 assert(RealValue >= INT32_MIN && RealValue <= INT32_MAX);
368 int32_t TruncValue = RealValue;
369 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
370 TruncValue;
371 break;
372 }
373 }
374}
375
376void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
378 uint32_t Type, int32_t Addend) {
379 switch (Type) {
380 case ELF::R_386_32: {
381 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
382 Value + Addend;
383 break;
384 }
385 // Handle R_386_PLT32 like R_386_PC32 since it should be able to
386 // reach any 32 bit address.
387 case ELF::R_386_PLT32:
388 case ELF::R_386_PC32: {
389 uint32_t FinalAddress =
390 Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
391 uint32_t RealOffset = Value + Addend - FinalAddress;
392 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
393 RealOffset;
394 break;
395 }
396 default:
397 // There are other relocation types, but it appears these are the
398 // only ones currently used by the LLVM ELF object writer
399 report_fatal_error("Relocation type not implemented yet!");
400 break;
401 }
402}
403
404void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
406 uint32_t Type, int64_t Addend) {
407 uint32_t *TargetPtr =
408 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
409 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
410 // Data should use target endian. Code should always use little endian.
411 bool isBE = Arch == Triple::aarch64_be;
412
413 LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
414 << format("%llx", Section.getAddressWithOffset(Offset))
415 << " FinalAddress: 0x" << format("%llx", FinalAddress)
416 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
417 << format("%x", Type) << " Addend: 0x"
418 << format("%llx", Addend) << "\n");
419
420 switch (Type) {
421 default:
422 report_fatal_error("Relocation type not implemented yet!");
423 break;
424 case ELF::R_AARCH64_NONE:
425 break;
426 case ELF::R_AARCH64_ABS16: {
427 uint64_t Result = Value + Addend;
428 assert(Result == static_cast<uint64_t>(llvm::SignExtend64(Result, 16)) ||
429 (Result >> 16) == 0);
430 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
431 break;
432 }
433 case ELF::R_AARCH64_ABS32: {
434 uint64_t Result = Value + Addend;
435 assert(Result == static_cast<uint64_t>(llvm::SignExtend64(Result, 32)) ||
436 (Result >> 32) == 0);
437 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
438 break;
439 }
440 case ELF::R_AARCH64_ABS64:
441 write(isBE, TargetPtr, Value + Addend);
442 break;
443 case ELF::R_AARCH64_PLT32: {
444 uint64_t Result = Value + Addend - FinalAddress;
445 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
446 static_cast<int64_t>(Result) <= INT32_MAX);
447 write(isBE, TargetPtr, static_cast<uint32_t>(Result));
448 break;
449 }
450 case ELF::R_AARCH64_PREL16: {
451 uint64_t Result = Value + Addend - FinalAddress;
452 assert(static_cast<int64_t>(Result) >= INT16_MIN &&
453 static_cast<int64_t>(Result) <= UINT16_MAX);
454 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU));
455 break;
456 }
457 case ELF::R_AARCH64_PREL32: {
458 uint64_t Result = Value + Addend - FinalAddress;
459 assert(static_cast<int64_t>(Result) >= INT32_MIN &&
460 static_cast<int64_t>(Result) <= UINT32_MAX);
461 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU));
462 break;
463 }
464 case ELF::R_AARCH64_PREL64:
465 write(isBE, TargetPtr, Value + Addend - FinalAddress);
466 break;
467 case ELF::R_AARCH64_CONDBR19: {
468 uint64_t BranchImm = Value + Addend - FinalAddress;
469
470 assert(isInt<21>(BranchImm));
471 *TargetPtr &= 0xff00001fU;
472 // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ
473 or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3);
474 break;
475 }
476 case ELF::R_AARCH64_TSTBR14: {
477 uint64_t BranchImm = Value + Addend - FinalAddress;
478
479 assert(isInt<16>(BranchImm));
480
481 uint32_t RawInstr = *(support::little32_t *)TargetPtr;
482 *(support::little32_t *)TargetPtr = RawInstr & 0xfff8001fU;
483
484 // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ
485 or32le(TargetPtr, (BranchImm & 0x0000FFFC) << 3);
486 break;
487 }
488 case ELF::R_AARCH64_CALL26: // fallthrough
489 case ELF::R_AARCH64_JUMP26: {
490 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
491 // calculation.
492 uint64_t BranchImm = Value + Addend - FinalAddress;
493
494 // "Check that -2^27 <= result < 2^27".
495 assert(isInt<28>(BranchImm));
496 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2);
497 break;
498 }
499 case ELF::R_AARCH64_MOVW_UABS_G3:
500 or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43);
501 break;
502 case ELF::R_AARCH64_MOVW_UABS_G2_NC:
503 or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27);
504 break;
505 case ELF::R_AARCH64_MOVW_UABS_G1_NC:
506 or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11);
507 break;
508 case ELF::R_AARCH64_MOVW_UABS_G0_NC:
509 or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5);
510 break;
511 case ELF::R_AARCH64_ADR_PREL_PG_HI21: {
512 // Operation: Page(S+A) - Page(P)
514 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL);
515
516 // Check that -2^32 <= X < 2^32
517 assert(isInt<33>(Result) && "overflow check failed for relocation");
518
519 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
520 // from bits 32:12 of X.
521 write32AArch64Addr(TargetPtr, Result >> 12);
522 break;
523 }
524 case ELF::R_AARCH64_ADD_ABS_LO12_NC:
525 // Operation: S + A
526 // Immediate goes in bits 21:10 of LD/ST instruction, taken
527 // from bits 11:0 of X
528 or32AArch64Imm(TargetPtr, Value + Addend);
529 break;
530 case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
531 // Operation: S + A
532 // Immediate goes in bits 21:10 of LD/ST instruction, taken
533 // from bits 11:0 of X
534 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11));
535 break;
536 case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
537 // Operation: S + A
538 // Immediate goes in bits 21:10 of LD/ST instruction, taken
539 // from bits 11:1 of X
540 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11));
541 break;
542 case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
543 // Operation: S + A
544 // Immediate goes in bits 21:10 of LD/ST instruction, taken
545 // from bits 11:2 of X
546 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11));
547 break;
548 case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
549 // Operation: S + A
550 // Immediate goes in bits 21:10 of LD/ST instruction, taken
551 // from bits 11:3 of X
552 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11));
553 break;
554 case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
555 // Operation: S + A
556 // Immediate goes in bits 21:10 of LD/ST instruction, taken
557 // from bits 11:4 of X
558 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11));
559 break;
560 case ELF::R_AARCH64_LD_PREL_LO19: {
561 // Operation: S + A - P
562 uint64_t Result = Value + Addend - FinalAddress;
563
564 // "Check that -2^20 <= result < 2^20".
565 assert(isInt<21>(Result));
566
567 *TargetPtr &= 0xff00001fU;
568 // Immediate goes in bits 23:5 of LD imm instruction, taken
569 // from bits 20:2 of X
570 *TargetPtr |= ((Result & 0xffc) << (5 - 2));
571 break;
572 }
573 case ELF::R_AARCH64_ADR_PREL_LO21: {
574 // Operation: S + A - P
575 uint64_t Result = Value + Addend - FinalAddress;
576
577 // "Check that -2^20 <= result < 2^20".
578 assert(isInt<21>(Result));
579
580 *TargetPtr &= 0x9f00001fU;
581 // Immediate goes in bits 23:5, 30:29 of ADR imm instruction, taken
582 // from bits 20:0 of X
583 *TargetPtr |= ((Result & 0xffc) << (5 - 2));
584 *TargetPtr |= (Result & 0x3) << 29;
585 break;
586 }
587 }
588}
589
590void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
592 uint32_t Type, int32_t Addend) {
593 // TODO: Add Thumb relocations.
594 uint32_t *TargetPtr =
595 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
596 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
597 Value += Addend;
598
599 LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
600 << Section.getAddressWithOffset(Offset)
601 << " FinalAddress: " << format("%p", FinalAddress)
602 << " Value: " << format("%x", Value)
603 << " Type: " << format("%x", Type)
604 << " Addend: " << format("%x", Addend) << "\n");
605
606 switch (Type) {
607 default:
608 llvm_unreachable("Not implemented relocation type!");
609
610 case ELF::R_ARM_NONE:
611 break;
612 // Write a 31bit signed offset
613 case ELF::R_ARM_PREL31:
614 support::ulittle32_t::ref{TargetPtr} =
615 (support::ulittle32_t::ref{TargetPtr} & 0x80000000) |
616 ((Value - FinalAddress) & ~0x80000000);
617 break;
618 case ELF::R_ARM_TARGET1:
619 case ELF::R_ARM_ABS32:
620 support::ulittle32_t::ref{TargetPtr} = Value;
621 break;
622 // Write first 16 bit of 32 bit value to the mov instruction.
623 // Last 4 bit should be shifted.
624 case ELF::R_ARM_MOVW_ABS_NC:
625 case ELF::R_ARM_MOVT_ABS:
626 if (Type == ELF::R_ARM_MOVW_ABS_NC)
627 Value = Value & 0xFFFF;
628 else if (Type == ELF::R_ARM_MOVT_ABS)
629 Value = (Value >> 16) & 0xFFFF;
630 support::ulittle32_t::ref{TargetPtr} =
631 (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) |
632 (((Value >> 12) & 0xF) << 16);
633 break;
634 // Write 24 bit relative value to the branch instruction.
635 case ELF::R_ARM_PC24: // Fall through.
636 case ELF::R_ARM_CALL: // Fall through.
637 case ELF::R_ARM_JUMP24:
638 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
639 RelValue = (RelValue & 0x03FFFFFC) >> 2;
640 assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE);
641 support::ulittle32_t::ref{TargetPtr} =
642 (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue;
643 break;
644 }
645}
646
647bool RuntimeDyldELF::resolveLoongArch64ShortBranch(
648 unsigned SectionID, relocation_iterator RelI,
649 const RelocationValueRef &Value) {
651 if (Value.SymbolName) {
652 auto Loc = GlobalSymbolTable.find(Value.SymbolName);
653 // Don't create direct branch for external symbols.
654 if (Loc == GlobalSymbolTable.end())
655 return false;
656 const auto &SymInfo = Loc->second;
657 Address = Sections[SymInfo.getSectionID()].getLoadAddressWithOffset(
658 SymInfo.getOffset());
659 } else {
660 Address = Sections[Value.SectionID].getLoadAddress();
661 }
662 uint64_t Offset = RelI->getOffset();
663 uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset);
664 uint64_t Delta = Address + Value.Addend - SourceAddress;
665 // Normal call
666 if (RelI->getType() == ELF::R_LARCH_B26) {
667 if (!isInt<28>(Delta))
668 return false;
669 resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
670 Value.Addend);
671 return true;
672 }
673 // Medium call: R_LARCH_CALL36
674 // Range: [-128G - 0x20000, +128G - 0x20000)
675 if (((int64_t)Delta + 0x20000) != llvm::SignExtend64(Delta + 0x20000, 38))
676 return false;
677 resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(),
678 Value.Addend);
679 return true;
680}
681
682void RuntimeDyldELF::resolveLoongArch64Branch(unsigned SectionID,
685 StubMap &Stubs) {
686 LLVM_DEBUG(dbgs() << "\t\tThis is an LoongArch64 branch relocation.\n");
687
688 if (resolveLoongArch64ShortBranch(SectionID, RelI, Value))
689 return;
690
691 SectionEntry &Section = Sections[SectionID];
692 uint64_t Offset = RelI->getOffset();
693 unsigned RelType = RelI->getType();
694 // Look for an existing stub.
695 auto [It, Inserted] = Stubs.try_emplace(Value);
696 if (!Inserted) {
697 resolveRelocation(Section, Offset,
698 (uint64_t)Section.getAddressWithOffset(It->second),
699 RelType, 0);
700 LLVM_DEBUG(dbgs() << " Stub function found\n");
701 return;
702 }
703 // Create a new stub function.
704 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
705 It->second = Section.getStubOffset();
706 uint8_t *StubTargetAddr =
707 createStubFunction(Section.getAddressWithOffset(Section.getStubOffset()));
708 RelocationEntry LU12I_W(SectionID, StubTargetAddr - Section.getAddress(),
709 ELF::R_LARCH_ABS_HI20, Value.Addend);
710 RelocationEntry ORI(SectionID, StubTargetAddr - Section.getAddress() + 4,
711 ELF::R_LARCH_ABS_LO12, Value.Addend);
712 RelocationEntry LU32I_D(SectionID, StubTargetAddr - Section.getAddress() + 8,
713 ELF::R_LARCH_ABS64_LO20, Value.Addend);
714 RelocationEntry LU52I_D(SectionID, StubTargetAddr - Section.getAddress() + 12,
715 ELF::R_LARCH_ABS64_HI12, Value.Addend);
716 if (Value.SymbolName) {
717 addRelocationForSymbol(LU12I_W, Value.SymbolName);
718 addRelocationForSymbol(ORI, Value.SymbolName);
719 addRelocationForSymbol(LU32I_D, Value.SymbolName);
720 addRelocationForSymbol(LU52I_D, Value.SymbolName);
721 } else {
722 addRelocationForSection(LU12I_W, Value.SectionID);
723 addRelocationForSection(ORI, Value.SectionID);
724 addRelocationForSection(LU32I_D, Value.SectionID);
725
726 addRelocationForSection(LU52I_D, Value.SectionID);
727 }
728 resolveRelocation(Section, Offset,
729 reinterpret_cast<uint64_t>(
730 Section.getAddressWithOffset(Section.getStubOffset())),
731 RelType, 0);
732 Section.advanceStubOffset(getMaxStubSize());
733}
734
735// Returns extract bits Val[Hi:Lo].
737 return Hi == 63 ? Val >> Lo : (Val & (((1ULL << (Hi + 1)) - 1))) >> Lo;
738}
739
740// Calculate the adjusted page delta between dest and PC. The code is copied
741// from lld and see comments there for more details.
743 uint32_t type) {
744 uint64_t pcalau12i_pc;
745 switch (type) {
746 case ELF::R_LARCH_PCALA64_LO20:
747 case ELF::R_LARCH_GOT64_PC_LO20:
748 pcalau12i_pc = pc - 8;
749 break;
750 case ELF::R_LARCH_PCALA64_HI12:
751 case ELF::R_LARCH_GOT64_PC_HI12:
752 pcalau12i_pc = pc - 12;
753 break;
754 default:
755 pcalau12i_pc = pc;
756 break;
757 }
758 uint64_t result = (dest & ~0xfffULL) - (pcalau12i_pc & ~0xfffULL);
759 if (dest & 0x800)
760 result += 0x1000 - 0x1'0000'0000;
761 if (result & 0x8000'0000)
762 result += 0x1'0000'0000;
763 return result;
764}
765
766void RuntimeDyldELF::resolveLoongArch64Relocation(const SectionEntry &Section,
769 int64_t Addend) {
770 auto *TargetPtr = Section.getAddressWithOffset(Offset);
771 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
772
773 LLVM_DEBUG(dbgs() << "resolveLoongArch64Relocation, LocalAddress: 0x"
774 << format("%llx", Section.getAddressWithOffset(Offset))
775 << " FinalAddress: 0x" << format("%llx", FinalAddress)
776 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
777 << format("%x", Type) << " Addend: 0x"
778 << format("%llx", Addend) << "\n");
779
780 switch (Type) {
781 default:
782 report_fatal_error("Relocation type not implemented yet!");
783 break;
784 case ELF::R_LARCH_32:
785 support::ulittle32_t::ref{TargetPtr} =
786 static_cast<uint32_t>(Value + Addend);
787 break;
788 case ELF::R_LARCH_64:
789 support::ulittle64_t::ref{TargetPtr} = Value + Addend;
790 break;
791 case ELF::R_LARCH_32_PCREL:
792 support::ulittle32_t::ref{TargetPtr} =
793 static_cast<uint32_t>(Value + Addend - FinalAddress);
794 break;
795 case ELF::R_LARCH_B26: {
796 uint64_t B26 = (Value + Addend - FinalAddress) >> 2;
797 auto Instr = support::ulittle32_t::ref(TargetPtr);
798 uint32_t Imm15_0 = extractBits(B26, /*Hi=*/15, /*Lo=*/0) << 10;
799 uint32_t Imm25_16 = extractBits(B26, /*Hi=*/25, /*Lo=*/16);
800 Instr = (Instr & 0xfc000000) | Imm15_0 | Imm25_16;
801 break;
802 }
803 case ELF::R_LARCH_CALL36: {
804 uint64_t Call36 = (Value + Addend - FinalAddress) >> 2;
805 auto Pcaddu18i = support::ulittle32_t::ref(TargetPtr);
806 uint32_t Imm35_16 =
807 extractBits((Call36 + (1UL << 15)), /*Hi=*/35, /*Lo=*/16) << 5;
808 Pcaddu18i = (Pcaddu18i & 0xfe00001f) | Imm35_16;
809 auto Jirl = support::ulittle32_t::ref(TargetPtr + 4);
810 uint32_t Imm15_0 = extractBits(Call36, /*Hi=*/15, /*Lo=*/0) << 10;
811 Jirl = (Jirl & 0xfc0003ff) | Imm15_0;
812 break;
813 }
814 case ELF::R_LARCH_GOT_PC_HI20:
815 case ELF::R_LARCH_PCALA_HI20: {
816 uint64_t Target = Value + Addend;
817 int64_t PageDelta = getLoongArchPageDelta(Target, FinalAddress, Type);
818 auto Instr = support::ulittle32_t::ref(TargetPtr);
819 uint32_t Imm31_12 = extractBits(PageDelta, /*Hi=*/31, /*Lo=*/12) << 5;
820 Instr = (Instr & 0xfe00001f) | Imm31_12;
821 break;
822 }
823 case ELF::R_LARCH_GOT_PC_LO12:
824 case ELF::R_LARCH_PCALA_LO12: {
825 uint64_t TargetOffset = (Value + Addend) & 0xfff;
826 auto Instr = support::ulittle32_t::ref(TargetPtr);
827 uint32_t Imm11_0 = TargetOffset << 10;
828 Instr = (Instr & 0xffc003ff) | Imm11_0;
829 break;
830 }
831 case ELF::R_LARCH_GOT64_PC_LO20:
832 case ELF::R_LARCH_PCALA64_LO20: {
833 uint64_t Target = Value + Addend;
834 int64_t PageDelta = getLoongArchPageDelta(Target, FinalAddress, Type);
835 auto Instr = support::ulittle32_t::ref(TargetPtr);
836 uint32_t Imm51_32 = extractBits(PageDelta, /*Hi=*/51, /*Lo=*/32) << 5;
837 Instr = (Instr & 0xfe00001f) | Imm51_32;
838 break;
839 }
840 case ELF::R_LARCH_GOT64_PC_HI12:
841 case ELF::R_LARCH_PCALA64_HI12: {
842 uint64_t Target = Value + Addend;
843 int64_t PageDelta = getLoongArchPageDelta(Target, FinalAddress, Type);
844 auto Instr = support::ulittle32_t::ref(TargetPtr);
845 uint32_t Imm63_52 = extractBits(PageDelta, /*Hi=*/63, /*Lo=*/52) << 10;
846 Instr = (Instr & 0xffc003ff) | Imm63_52;
847 break;
848 }
849 case ELF::R_LARCH_ABS_HI20: {
850 uint64_t Target = Value + Addend;
851 auto Instr = support::ulittle32_t::ref(TargetPtr);
852 uint32_t Imm31_12 = extractBits(Target, /*Hi=*/31, /*Lo=*/12) << 5;
853 Instr = (Instr & 0xfe00001f) | Imm31_12;
854 break;
855 }
856 case ELF::R_LARCH_ABS_LO12: {
857 uint64_t Target = Value + Addend;
858 auto Instr = support::ulittle32_t::ref(TargetPtr);
859 uint32_t Imm11_0 = extractBits(Target, /*Hi=*/11, /*Lo=*/0) << 10;
860 Instr = (Instr & 0xffc003ff) | Imm11_0;
861 break;
862 }
863 case ELF::R_LARCH_ABS64_LO20: {
864 uint64_t Target = Value + Addend;
865 auto Instr = support::ulittle32_t::ref(TargetPtr);
866 uint32_t Imm51_32 = extractBits(Target, /*Hi=*/51, /*Lo=*/32) << 5;
867 Instr = (Instr & 0xfe00001f) | Imm51_32;
868 break;
869 }
870 case ELF::R_LARCH_ABS64_HI12: {
871 uint64_t Target = Value + Addend;
872 auto Instr = support::ulittle32_t::ref(TargetPtr);
873 uint32_t Imm63_52 = extractBits(Target, /*Hi=*/63, /*Lo=*/52) << 10;
874 Instr = (Instr & 0xffc003ff) | Imm63_52;
875 break;
876 }
877 case ELF::R_LARCH_ADD32:
878 support::ulittle32_t::ref{TargetPtr} =
879 (support::ulittle32_t::ref{TargetPtr} +
880 static_cast<uint32_t>(Value + Addend));
881 break;
882 case ELF::R_LARCH_SUB32:
883 support::ulittle32_t::ref{TargetPtr} =
884 (support::ulittle32_t::ref{TargetPtr} -
885 static_cast<uint32_t>(Value + Addend));
886 break;
887 case ELF::R_LARCH_ADD64:
888 support::ulittle64_t::ref{TargetPtr} =
889 (support::ulittle64_t::ref{TargetPtr} + Value + Addend);
890 break;
891 case ELF::R_LARCH_SUB64:
892 support::ulittle64_t::ref{TargetPtr} =
893 (support::ulittle64_t::ref{TargetPtr} - Value - Addend);
894 break;
895 }
896}
897
898void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) {
899 if (Arch == Triple::UnknownArch ||
900 Triple::getArchTypePrefix(Arch) != "mips") {
901 IsMipsO32ABI = false;
902 IsMipsN32ABI = false;
903 IsMipsN64ABI = false;
904 return;
905 }
906 if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) {
907 unsigned AbiVariant = E->getPlatformFlags();
908 IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32;
909 IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2;
910 }
911 IsMipsN64ABI = Obj.getFileFormatName() == "elf64-mips";
912}
913
914// Return the .TOC. section and offset.
915Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj,
916 ObjSectionToIDMap &LocalSections,
917 RelocationValueRef &Rel) {
918 // Set a default SectionID in case we do not find a TOC section below.
919 // This may happen for references to TOC base base (sym@toc, .odp
920 // relocation) without a .toc directive. In this case just use the
921 // first section (which is usually the .odp) since the code won't
922 // reference the .toc base directly.
923 Rel.SymbolName = nullptr;
924 Rel.SectionID = 0;
925
926 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
927 // order. The TOC starts where the first of these sections starts.
928 for (auto &Section : Obj.sections()) {
929 Expected<StringRef> NameOrErr = Section.getName();
930 if (!NameOrErr)
931 return NameOrErr.takeError();
932 StringRef SectionName = *NameOrErr;
933
934 if (SectionName == ".got"
935 || SectionName == ".toc"
936 || SectionName == ".tocbss"
937 || SectionName == ".plt") {
938 if (auto SectionIDOrErr =
939 findOrEmitSection(Obj, Section, false, LocalSections))
940 Rel.SectionID = *SectionIDOrErr;
941 else
942 return SectionIDOrErr.takeError();
943 break;
944 }
945 }
946
947 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
948 // thus permitting a full 64 Kbytes segment.
949 Rel.Addend = 0x8000;
950
951 return Error::success();
952}
953
954// Returns the sections and offset associated with the ODP entry referenced
955// by Symbol.
956Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj,
957 ObjSectionToIDMap &LocalSections,
958 RelocationValueRef &Rel) {
959 // Get the ELF symbol value (st_value) to compare with Relocation offset in
960 // .opd entries
961 for (section_iterator si = Obj.section_begin(), se = Obj.section_end();
962 si != se; ++si) {
963
964 Expected<section_iterator> RelSecOrErr = si->getRelocatedSection();
965 if (!RelSecOrErr)
967
968 section_iterator RelSecI = *RelSecOrErr;
969 if (RelSecI == Obj.section_end())
970 continue;
971
972 Expected<StringRef> NameOrErr = RelSecI->getName();
973 if (!NameOrErr)
974 return NameOrErr.takeError();
975 StringRef RelSectionName = *NameOrErr;
976
977 if (RelSectionName != ".opd")
978 continue;
979
980 for (elf_relocation_iterator i = si->relocation_begin(),
981 e = si->relocation_end();
982 i != e;) {
983 // The R_PPC64_ADDR64 relocation indicates the first field
984 // of a .opd entry
985 uint64_t TypeFunc = i->getType();
986 if (TypeFunc != ELF::R_PPC64_ADDR64) {
987 ++i;
988 continue;
989 }
990
991 uint64_t TargetSymbolOffset = i->getOffset();
992 symbol_iterator TargetSymbol = i->getSymbol();
993 int64_t Addend;
994 if (auto AddendOrErr = i->getAddend())
995 Addend = *AddendOrErr;
996 else
997 return AddendOrErr.takeError();
998
999 ++i;
1000 if (i == e)
1001 break;
1002
1003 // Just check if following relocation is a R_PPC64_TOC
1004 uint64_t TypeTOC = i->getType();
1005 if (TypeTOC != ELF::R_PPC64_TOC)
1006 continue;
1007
1008 // Finally compares the Symbol value and the target symbol offset
1009 // to check if this .opd entry refers to the symbol the relocation
1010 // points to.
1011 if (Rel.Addend != (int64_t)TargetSymbolOffset)
1012 continue;
1013
1014 section_iterator TSI = Obj.section_end();
1015 if (auto TSIOrErr = TargetSymbol->getSection())
1016 TSI = *TSIOrErr;
1017 else
1018 return TSIOrErr.takeError();
1019 assert(TSI != Obj.section_end() && "TSI should refer to a valid section");
1020
1021 bool IsCode = TSI->isText();
1022 if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode,
1023 LocalSections))
1024 Rel.SectionID = *SectionIDOrErr;
1025 else
1026 return SectionIDOrErr.takeError();
1027 Rel.Addend = (intptr_t)Addend;
1028 return Error::success();
1029 }
1030 }
1031 llvm_unreachable("Attempting to get address of ODP entry!");
1032}
1033
1034// Relocation masks following the #lo(value), #hi(value), #ha(value),
1035// #higher(value), #highera(value), #highest(value), and #highesta(value)
1036// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
1037// document.
1038
1039static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; }
1040
1042 return (value >> 16) & 0xffff;
1043}
1044
1046 return ((value + 0x8000) >> 16) & 0xffff;
1047}
1048
1050 return (value >> 32) & 0xffff;
1051}
1052
1054 return ((value + 0x8000) >> 32) & 0xffff;
1055}
1056
1058 return (value >> 48) & 0xffff;
1059}
1060
1062 return ((value + 0x8000) >> 48) & 0xffff;
1063}
1064
1065void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
1067 uint32_t Type, int64_t Addend) {
1068 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
1069 switch (Type) {
1070 default:
1071 report_fatal_error("Relocation type not implemented yet!");
1072 break;
1073 case ELF::R_PPC_ADDR16_LO:
1074 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
1075 break;
1076 case ELF::R_PPC_ADDR16_HI:
1077 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
1078 break;
1079 case ELF::R_PPC_ADDR16_HA:
1080 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
1081 break;
1082 }
1083}
1084
1085void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
1087 uint32_t Type, int64_t Addend) {
1088 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
1089 switch (Type) {
1090 default:
1091 report_fatal_error("Relocation type not implemented yet!");
1092 break;
1093 case ELF::R_PPC64_ADDR16:
1094 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
1095 break;
1096 case ELF::R_PPC64_ADDR16_DS:
1097 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
1098 break;
1099 case ELF::R_PPC64_ADDR16_LO:
1100 writeInt16BE(LocalAddress, applyPPClo(Value + Addend));
1101 break;
1102 case ELF::R_PPC64_ADDR16_LO_DS:
1103 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3);
1104 break;
1105 case ELF::R_PPC64_ADDR16_HI:
1106 case ELF::R_PPC64_ADDR16_HIGH:
1107 writeInt16BE(LocalAddress, applyPPChi(Value + Addend));
1108 break;
1109 case ELF::R_PPC64_ADDR16_HA:
1110 case ELF::R_PPC64_ADDR16_HIGHA:
1111 writeInt16BE(LocalAddress, applyPPCha(Value + Addend));
1112 break;
1113 case ELF::R_PPC64_ADDR16_HIGHER:
1114 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend));
1115 break;
1116 case ELF::R_PPC64_ADDR16_HIGHERA:
1117 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend));
1118 break;
1119 case ELF::R_PPC64_ADDR16_HIGHEST:
1120 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend));
1121 break;
1122 case ELF::R_PPC64_ADDR16_HIGHESTA:
1123 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend));
1124 break;
1125 case ELF::R_PPC64_ADDR14: {
1126 assert(((Value + Addend) & 3) == 0);
1127 // Preserve the AA/LK bits in the branch instruction
1128 uint8_t aalk = *(LocalAddress + 3);
1129 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
1130 } break;
1131 case ELF::R_PPC64_REL16_LO: {
1132 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1133 uint64_t Delta = Value - FinalAddress + Addend;
1134 writeInt16BE(LocalAddress, applyPPClo(Delta));
1135 } break;
1136 case ELF::R_PPC64_REL16_HI: {
1137 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1138 uint64_t Delta = Value - FinalAddress + Addend;
1139 writeInt16BE(LocalAddress, applyPPChi(Delta));
1140 } break;
1141 case ELF::R_PPC64_REL16_HA: {
1142 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1143 uint64_t Delta = Value - FinalAddress + Addend;
1144 writeInt16BE(LocalAddress, applyPPCha(Delta));
1145 } break;
1146 case ELF::R_PPC64_ADDR32: {
1147 int64_t Result = static_cast<int64_t>(Value + Addend);
1148 if (SignExtend64<32>(Result) != Result)
1149 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
1150 writeInt32BE(LocalAddress, Result);
1151 } break;
1152 case ELF::R_PPC64_REL24: {
1153 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1154 int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
1155 if (SignExtend64<26>(delta) != delta)
1156 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
1157 // We preserve bits other than LI field, i.e. PO and AA/LK fields.
1158 uint32_t Inst = readBytesUnaligned(LocalAddress, 4);
1159 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC));
1160 } break;
1161 case ELF::R_PPC64_REL32: {
1162 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1163 int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend);
1164 if (SignExtend64<32>(delta) != delta)
1165 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
1166 writeInt32BE(LocalAddress, delta);
1167 } break;
1168 case ELF::R_PPC64_REL64: {
1169 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1170 uint64_t Delta = Value - FinalAddress + Addend;
1171 writeInt64BE(LocalAddress, Delta);
1172 } break;
1173 case ELF::R_PPC64_ADDR64:
1174 writeInt64BE(LocalAddress, Value + Addend);
1175 break;
1176 }
1177}
1178
1179void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
1181 uint32_t Type, int64_t Addend) {
1182 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
1183 switch (Type) {
1184 default:
1185 report_fatal_error("Relocation type not implemented yet!");
1186 break;
1187 case ELF::R_390_PC16DBL:
1188 case ELF::R_390_PLT16DBL: {
1189 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
1190 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
1191 writeInt16BE(LocalAddress, Delta / 2);
1192 break;
1193 }
1194 case ELF::R_390_PC32DBL:
1195 case ELF::R_390_PLT32DBL: {
1196 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
1197 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
1198 writeInt32BE(LocalAddress, Delta / 2);
1199 break;
1200 }
1201 case ELF::R_390_PC16: {
1202 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
1203 assert(int16_t(Delta) == Delta && "R_390_PC16 overflow");
1204 writeInt16BE(LocalAddress, Delta);
1205 break;
1206 }
1207 case ELF::R_390_PC32: {
1208 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
1209 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
1210 writeInt32BE(LocalAddress, Delta);
1211 break;
1212 }
1213 case ELF::R_390_PC64: {
1214 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
1215 writeInt64BE(LocalAddress, Delta);
1216 break;
1217 }
1218 case ELF::R_390_8:
1219 *LocalAddress = (uint8_t)(Value + Addend);
1220 break;
1221 case ELF::R_390_16:
1222 writeInt16BE(LocalAddress, Value + Addend);
1223 break;
1224 case ELF::R_390_32:
1225 writeInt32BE(LocalAddress, Value + Addend);
1226 break;
1227 case ELF::R_390_64:
1228 writeInt64BE(LocalAddress, Value + Addend);
1229 break;
1230 }
1231}
1232
1233void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section,
1235 uint32_t Type, int64_t Addend) {
1236 bool isBE = Arch == Triple::bpfeb;
1237
1238 switch (Type) {
1239 default:
1240 report_fatal_error("Relocation type not implemented yet!");
1241 break;
1242 case ELF::R_BPF_NONE:
1243 case ELF::R_BPF_64_64:
1244 case ELF::R_BPF_64_32:
1245 case ELF::R_BPF_64_NODYLD32:
1246 break;
1247 case ELF::R_BPF_64_ABS64: {
1248 write(isBE, Section.getAddressWithOffset(Offset), Value + Addend);
1249 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
1250 << format("%p\n", Section.getAddressWithOffset(Offset)));
1251 break;
1252 }
1253 case ELF::R_BPF_64_ABS32: {
1254 Value += Addend;
1255 assert(Value <= UINT32_MAX);
1256 write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value));
1257 LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at "
1258 << format("%p\n", Section.getAddressWithOffset(Offset)));
1259 break;
1260 }
1261 }
1262}
1263
1264static void applyUTypeImmRISCV(uint8_t *InstrAddr, uint32_t Imm) {
1265 uint32_t UpperImm = (Imm + 0x800) & 0xfffff000;
1266 auto Instr = support::ulittle32_t::ref(InstrAddr);
1267 Instr = (Instr & 0xfff) | UpperImm;
1268}
1269
1270static void applyITypeImmRISCV(uint8_t *InstrAddr, uint32_t Imm) {
1271 uint32_t LowerImm = Imm & 0xfff;
1272 auto Instr = support::ulittle32_t::ref(InstrAddr);
1273 Instr = (Instr & 0xfffff) | (LowerImm << 20);
1274}
1275
1276void RuntimeDyldELF::resolveRISCVRelocation(const SectionEntry &Section,
1278 uint32_t Type, int64_t Addend,
1279 SID SectionID) {
1280 switch (Type) {
1281 default: {
1282 std::string Err = "Unimplemented reloc type: " + std::to_string(Type);
1283 llvm::report_fatal_error(Err.c_str());
1284 }
1285 // 32-bit PC-relative function call, macros call, tail (PIC)
1286 // Write first 20 bits of 32 bit value to the auipc instruction
1287 // Last 12 bits to the jalr instruction
1288 case ELF::R_RISCV_CALL:
1289 case ELF::R_RISCV_CALL_PLT: {
1290 uint64_t P = Section.getLoadAddressWithOffset(Offset);
1291 uint64_t PCOffset = Value + Addend - P;
1292 applyUTypeImmRISCV(Section.getAddressWithOffset(Offset), PCOffset);
1293 applyITypeImmRISCV(Section.getAddressWithOffset(Offset + 4), PCOffset);
1294 break;
1295 }
1296 // High 20 bits of 32-bit absolute address, %hi(symbol)
1297 case ELF::R_RISCV_HI20: {
1298 uint64_t PCOffset = Value + Addend;
1299 applyUTypeImmRISCV(Section.getAddressWithOffset(Offset), PCOffset);
1300 break;
1301 }
1302 // Low 12 bits of 32-bit absolute address, %lo(symbol)
1303 case ELF::R_RISCV_LO12_I: {
1304 uint64_t PCOffset = Value + Addend;
1305 applyITypeImmRISCV(Section.getAddressWithOffset(Offset), PCOffset);
1306 break;
1307 }
1308 // High 20 bits of 32-bit PC-relative reference, %pcrel_hi(symbol)
1309 case ELF::R_RISCV_GOT_HI20:
1310 case ELF::R_RISCV_PCREL_HI20: {
1311 uint64_t P = Section.getLoadAddressWithOffset(Offset);
1312 uint64_t PCOffset = Value + Addend - P;
1313 applyUTypeImmRISCV(Section.getAddressWithOffset(Offset), PCOffset);
1314 break;
1315 }
1316
1317 // label:
1318 // auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
1319 // addi a0, a0, %pcrel_lo(label) // R_RISCV_PCREL_LO12_I
1320 //
1321 // The low 12 bits of relative address between pc and symbol.
1322 // The symbol is related to the high part instruction which is marked by
1323 // label.
1324 case ELF::R_RISCV_PCREL_LO12_I: {
1325 for (auto &&PendingReloc : PendingRelocs) {
1326 const RelocationValueRef &MatchingValue = PendingReloc.first;
1327 RelocationEntry &Reloc = PendingReloc.second;
1328 uint64_t HIRelocPC =
1329 getSectionLoadAddress(Reloc.SectionID) + Reloc.Offset;
1330 if (Value + Addend == HIRelocPC) {
1332 MatchingValue.Addend;
1333 auto PCOffset = Symbol - HIRelocPC;
1334 applyITypeImmRISCV(Section.getAddressWithOffset(Offset), PCOffset);
1335 return;
1336 }
1337 }
1338
1340 "R_RISCV_PCREL_LO12_I without matching R_RISCV_PCREL_HI20");
1341 }
1342 case ELF::R_RISCV_32_PCREL: {
1343 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
1344 int64_t RealOffset = Value + Addend - FinalAddress;
1345 int32_t TruncOffset = Lo_32(RealOffset);
1346 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
1347 TruncOffset;
1348 break;
1349 }
1350 case ELF::R_RISCV_32: {
1351 auto Ref = support::ulittle32_t::ref(Section.getAddressWithOffset(Offset));
1352 Ref = Value + Addend;
1353 break;
1354 }
1355 case ELF::R_RISCV_64: {
1356 auto Ref = support::ulittle64_t::ref(Section.getAddressWithOffset(Offset));
1357 Ref = Value + Addend;
1358 break;
1359 }
1360 case ELF::R_RISCV_ADD8: {
1361 auto Ref = support::ulittle8_t::ref(Section.getAddressWithOffset(Offset));
1362 Ref = Ref + Value + Addend;
1363 break;
1364 }
1365 case ELF::R_RISCV_ADD16: {
1366 auto Ref = support::ulittle16_t::ref(Section.getAddressWithOffset(Offset));
1367 Ref = Ref + Value + Addend;
1368 break;
1369 }
1370 case ELF::R_RISCV_ADD32: {
1371 auto Ref = support::ulittle32_t::ref(Section.getAddressWithOffset(Offset));
1372 Ref = Ref + Value + Addend;
1373 break;
1374 }
1375 case ELF::R_RISCV_ADD64: {
1376 auto Ref = support::ulittle64_t::ref(Section.getAddressWithOffset(Offset));
1377 Ref = Ref + Value + Addend;
1378 break;
1379 }
1380 case ELF::R_RISCV_SUB8: {
1381 auto Ref = support::ulittle8_t::ref(Section.getAddressWithOffset(Offset));
1382 Ref = Ref - Value - Addend;
1383 break;
1384 }
1385 case ELF::R_RISCV_SUB16: {
1386 auto Ref = support::ulittle16_t::ref(Section.getAddressWithOffset(Offset));
1387 Ref = Ref - Value - Addend;
1388 break;
1389 }
1390 case ELF::R_RISCV_SUB32: {
1391 auto Ref = support::ulittle32_t::ref(Section.getAddressWithOffset(Offset));
1392 Ref = Ref - Value - Addend;
1393 break;
1394 }
1395 case ELF::R_RISCV_SUB64: {
1396 auto Ref = support::ulittle64_t::ref(Section.getAddressWithOffset(Offset));
1397 Ref = Ref - Value - Addend;
1398 break;
1399 }
1400 case ELF::R_RISCV_SET8: {
1401 auto Ref = support::ulittle8_t::ref(Section.getAddressWithOffset(Offset));
1402 Ref = Value + Addend;
1403 break;
1404 }
1405 case ELF::R_RISCV_SET16: {
1406 auto Ref = support::ulittle16_t::ref(Section.getAddressWithOffset(Offset));
1407 Ref = Value + Addend;
1408 break;
1409 }
1410 case ELF::R_RISCV_SET32: {
1411 auto Ref = support::ulittle32_t::ref(Section.getAddressWithOffset(Offset));
1412 Ref = Value + Addend;
1413 break;
1414 }
1415 }
1416}
1417
1418// The target location for the relocation is described by RE.SectionID and
1419// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
1420// SectionEntry has three members describing its location.
1421// SectionEntry::Address is the address at which the section has been loaded
1422// into memory in the current (host) process. SectionEntry::LoadAddress is the
1423// address that the section will have in the target process.
1424// SectionEntry::ObjAddress is the address of the bits for this section in the
1425// original emitted object image (also in the current address space).
1426//
1427// Relocations will be applied as if the section were loaded at
1428// SectionEntry::LoadAddress, but they will be applied at an address based
1429// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
1430// Target memory contents if they are required for value calculations.
1431//
1432// The Value parameter here is the load address of the symbol for the
1433// relocation to be applied. For relocations which refer to symbols in the
1434// current object Value will be the LoadAddress of the section in which
1435// the symbol resides (RE.Addend provides additional information about the
1436// symbol location). For external symbols, Value will be the address of the
1437// symbol in the target address space.
1438void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE,
1439 uint64_t Value) {
1440 const SectionEntry &Section = Sections[RE.SectionID];
1441 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
1442 RE.SymOffset, RE.SectionID);
1443}
1444
1445void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
1447 uint32_t Type, int64_t Addend,
1448 uint64_t SymOffset, SID SectionID) {
1449 switch (Arch) {
1450 case Triple::x86_64:
1451 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset);
1452 break;
1453 case Triple::x86:
1454 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1455 (uint32_t)(Addend & 0xffffffffL));
1456 break;
1457 case Triple::aarch64:
1458 case Triple::aarch64_be:
1459 resolveAArch64Relocation(Section, Offset, Value, Type, Addend);
1460 break;
1461 case Triple::arm: // Fall through.
1462 case Triple::armeb:
1463 case Triple::thumb:
1464 case Triple::thumbeb:
1465 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type,
1466 (uint32_t)(Addend & 0xffffffffL));
1467 break;
1469 resolveLoongArch64Relocation(Section, Offset, Value, Type, Addend);
1470 break;
1471 case Triple::ppc: // Fall through.
1472 case Triple::ppcle:
1473 resolvePPC32Relocation(Section, Offset, Value, Type, Addend);
1474 break;
1475 case Triple::ppc64: // Fall through.
1476 case Triple::ppc64le:
1477 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
1478 break;
1479 case Triple::systemz:
1480 resolveSystemZRelocation(Section, Offset, Value, Type, Addend);
1481 break;
1482 case Triple::bpfel:
1483 case Triple::bpfeb:
1484 resolveBPFRelocation(Section, Offset, Value, Type, Addend);
1485 break;
1486 case Triple::riscv32: // Fall through.
1487 case Triple::riscv64:
1488 resolveRISCVRelocation(Section, Offset, Value, Type, Addend, SectionID);
1489 break;
1490 default:
1491 llvm_unreachable("Unsupported CPU type!");
1492 }
1493}
1494
1495void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID,
1496 uint64_t Offset) const {
1497 return (void *)(Sections[SectionID].getObjAddress() + Offset);
1498}
1499
1500void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
1501 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset);
1502 if (Value.SymbolName)
1503 addRelocationForSymbol(RE, Value.SymbolName);
1504 else
1505 addRelocationForSection(RE, Value.SectionID);
1506}
1507
1508uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType,
1509 bool IsLocal) const {
1510 switch (RelType) {
1511 case ELF::R_MICROMIPS_GOT16:
1512 if (IsLocal)
1513 return ELF::R_MICROMIPS_LO16;
1514 break;
1515 case ELF::R_MICROMIPS_HI16:
1516 return ELF::R_MICROMIPS_LO16;
1517 case ELF::R_MIPS_GOT16:
1518 if (IsLocal)
1519 return ELF::R_MIPS_LO16;
1520 break;
1521 case ELF::R_MIPS_HI16:
1522 return ELF::R_MIPS_LO16;
1523 case ELF::R_MIPS_PCHI16:
1524 return ELF::R_MIPS_PCLO16;
1525 default:
1526 break;
1527 }
1528 return ELF::R_MIPS_NONE;
1529}
1530
1531// Sometimes we don't need to create thunk for a branch.
1532// This typically happens when branch target is located
1533// in the same object file. In such case target is either
1534// a weak symbol or symbol in a different executable section.
1535// This function checks if branch target is located in the
1536// same object file and if distance between source and target
1537// fits R_AARCH64_CALL26 relocation. If both conditions are
1538// met, it emits direct jump to the target and returns true.
1539// Otherwise false is returned and thunk is created.
1540bool RuntimeDyldELF::resolveAArch64ShortBranch(
1541 unsigned SectionID, relocation_iterator RelI,
1542 const RelocationValueRef &Value) {
1543 uint64_t TargetOffset;
1544 unsigned TargetSectionID;
1545 if (Value.SymbolName) {
1546 auto Loc = GlobalSymbolTable.find(Value.SymbolName);
1547
1548 // Don't create direct branch for external symbols.
1549 if (Loc == GlobalSymbolTable.end())
1550 return false;
1551
1552 const auto &SymInfo = Loc->second;
1553
1554 TargetSectionID = SymInfo.getSectionID();
1555 TargetOffset = SymInfo.getOffset();
1556 } else {
1557 TargetSectionID = Value.SectionID;
1558 TargetOffset = 0;
1559 }
1560
1561 // We don't actually know the load addresses at this point, so if the
1562 // branch is cross-section, we don't know exactly how far away it is.
1563 if (TargetSectionID != SectionID)
1564 return false;
1565
1566 uint64_t SourceOffset = RelI->getOffset();
1567
1568 // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
1569 // If distance between source and target is out of range then we should
1570 // create thunk.
1571 if (!isInt<28>(TargetOffset + Value.Addend - SourceOffset))
1572 return false;
1573
1574 RelocationEntry RE(SectionID, SourceOffset, RelI->getType(), Value.Addend);
1575 if (Value.SymbolName)
1576 addRelocationForSymbol(RE, Value.SymbolName);
1577 else
1578 addRelocationForSection(RE, Value.SectionID);
1579
1580 return true;
1581}
1582
1583void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID,
1586 StubMap &Stubs) {
1587
1588 LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1589 SectionEntry &Section = Sections[SectionID];
1590
1591 uint64_t Offset = RelI->getOffset();
1592 unsigned RelType = RelI->getType();
1593 // Look for an existing stub.
1594 StubMap::const_iterator i = Stubs.find(Value);
1595 if (i != Stubs.end()) {
1596 resolveRelocation(Section, Offset,
1597 Section.getLoadAddressWithOffset(i->second), RelType, 0);
1598 LLVM_DEBUG(dbgs() << " Stub function found\n");
1599 } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) {
1600 // Create a new stub function.
1601 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1602 Stubs[Value] = Section.getStubOffset();
1603 uint8_t *StubTargetAddr = createStubFunction(
1604 Section.getAddressWithOffset(Section.getStubOffset()));
1605
1606 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(),
1607 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
1608 RelocationEntry REmovk_g2(SectionID,
1609 StubTargetAddr - Section.getAddress() + 4,
1610 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
1611 RelocationEntry REmovk_g1(SectionID,
1612 StubTargetAddr - Section.getAddress() + 8,
1613 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
1614 RelocationEntry REmovk_g0(SectionID,
1615 StubTargetAddr - Section.getAddress() + 12,
1616 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
1617
1618 if (Value.SymbolName) {
1619 addRelocationForSymbol(REmovz_g3, Value.SymbolName);
1620 addRelocationForSymbol(REmovk_g2, Value.SymbolName);
1621 addRelocationForSymbol(REmovk_g1, Value.SymbolName);
1622 addRelocationForSymbol(REmovk_g0, Value.SymbolName);
1623 } else {
1624 addRelocationForSection(REmovz_g3, Value.SectionID);
1625 addRelocationForSection(REmovk_g2, Value.SectionID);
1626 addRelocationForSection(REmovk_g1, Value.SectionID);
1627 addRelocationForSection(REmovk_g0, Value.SectionID);
1628 }
1629 resolveRelocation(Section, Offset,
1630 Section.getLoadAddressWithOffset(Section.getStubOffset()),
1631 RelType, 0);
1632 Section.advanceStubOffset(getMaxStubSize());
1633 }
1634}
1635
1638 unsigned SectionID, relocation_iterator RelI, const ObjectFile &O,
1639 ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) {
1640 const auto &Obj = cast<ELFObjectFileBase>(O);
1641 uint64_t RelType = RelI->getType();
1642 int64_t Addend = 0;
1643 if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend())
1644 Addend = *AddendOrErr;
1645 else
1646 consumeError(AddendOrErr.takeError());
1647 elf_symbol_iterator Symbol = RelI->getSymbol();
1648
1649 // Obtain the symbol name which is referenced in the relocation
1650 StringRef TargetName;
1651 if (Symbol != Obj.symbol_end()) {
1652 if (auto TargetNameOrErr = Symbol->getName())
1653 TargetName = *TargetNameOrErr;
1654 else
1655 return TargetNameOrErr.takeError();
1656 }
1657 LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
1658 << " TargetName: " << TargetName << "\n");
1660 // First search for the symbol in the local symbol table
1662
1663 // Search for the symbol in the global symbol table
1665 if (Symbol != Obj.symbol_end()) {
1666 gsi = GlobalSymbolTable.find(TargetName.data());
1667 Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType();
1668 if (!SymTypeOrErr) {
1669 std::string Buf;
1671 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
1673 }
1674 SymType = *SymTypeOrErr;
1675 }
1676 if (gsi != GlobalSymbolTable.end()) {
1677 const auto &SymInfo = gsi->second;
1678 Value.SectionID = SymInfo.getSectionID();
1679 Value.Offset = SymInfo.getOffset();
1680 Value.Addend = SymInfo.getOffset() + Addend;
1681 } else {
1682 switch (SymType) {
1683 case SymbolRef::ST_Debug: {
1684 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
1685 // and can be changed by another developers. Maybe best way is add
1686 // a new symbol type ST_Section to SymbolRef and use it.
1687 auto SectionOrErr = Symbol->getSection();
1688 if (!SectionOrErr) {
1689 std::string Buf;
1691 logAllUnhandledErrors(SectionOrErr.takeError(), OS);
1693 }
1694 section_iterator si = *SectionOrErr;
1695 if (si == Obj.section_end())
1696 llvm_unreachable("Symbol section not found, bad object file format!");
1697 LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n");
1698 bool isCode = si->isText();
1699 if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode,
1700 ObjSectionToID))
1701 Value.SectionID = *SectionIDOrErr;
1702 else
1703 return SectionIDOrErr.takeError();
1704 Value.Addend = Addend;
1705 break;
1706 }
1707 case SymbolRef::ST_Data:
1710 case SymbolRef::ST_Unknown: {
1711 Value.SymbolName = TargetName.data();
1712 Value.Addend = Addend;
1713
1714 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
1715 // will manifest here as a NULL symbol name.
1716 // We can set this as a valid (but empty) symbol name, and rely
1717 // on addRelocationForSymbol to handle this.
1718 if (!Value.SymbolName)
1719 Value.SymbolName = "";
1720 break;
1721 }
1722 default:
1723 llvm_unreachable("Unresolved symbol type!");
1724 break;
1725 }
1726 }
1727
1728 uint64_t Offset = RelI->getOffset();
1729
1730 LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset
1731 << "\n");
1733 if ((RelType == ELF::R_AARCH64_CALL26 ||
1734 RelType == ELF::R_AARCH64_JUMP26) &&
1736 resolveAArch64Branch(SectionID, Value, RelI, Stubs);
1737 } else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) {
1738 // Create new GOT entry or find existing one. If GOT entry is
1739 // to be created, then we also emit ABS64 relocation for it.
1740 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1741 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1742 ELF::R_AARCH64_ADR_PREL_PG_HI21);
1743
1744 } else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) {
1745 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64);
1746 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1747 ELF::R_AARCH64_LDST64_ABS_LO12_NC);
1748 } else {
1749 processSimpleRelocation(SectionID, Offset, RelType, Value);
1750 }
1751 } else if (Arch == Triple::arm) {
1752 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
1753 RelType == ELF::R_ARM_JUMP24) {
1754 // This is an ARM branch relocation, need to use a stub function.
1755 LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
1756 SectionEntry &Section = Sections[SectionID];
1757
1758 // Look for an existing stub.
1759 auto [It, Inserted] = Stubs.try_emplace(Value);
1760 if (!Inserted) {
1761 resolveRelocation(Section, Offset,
1762 Section.getLoadAddressWithOffset(It->second), RelType,
1763 0);
1764 LLVM_DEBUG(dbgs() << " Stub function found\n");
1765 } else {
1766 // Create a new stub function.
1767 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1768 It->second = Section.getStubOffset();
1769 uint8_t *StubTargetAddr = createStubFunction(
1770 Section.getAddressWithOffset(Section.getStubOffset()));
1771 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
1772 ELF::R_ARM_ABS32, Value.Addend);
1773 if (Value.SymbolName)
1774 addRelocationForSymbol(RE, Value.SymbolName);
1775 else
1776 addRelocationForSection(RE, Value.SectionID);
1777
1778 resolveRelocation(
1779 Section, Offset,
1780 Section.getLoadAddressWithOffset(Section.getStubOffset()), RelType,
1781 0);
1782 Section.advanceStubOffset(getMaxStubSize());
1783 }
1784 } else {
1785 uint32_t *Placeholder =
1786 reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset));
1787 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 ||
1788 RelType == ELF::R_ARM_ABS32) {
1789 Value.Addend += *Placeholder;
1790 } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) {
1791 // See ELF for ARM documentation
1792 Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12));
1793 }
1794 processSimpleRelocation(SectionID, Offset, RelType, Value);
1795 }
1796 } else if (Arch == Triple::loongarch64) {
1797 if ((RelType == ELF::R_LARCH_B26 || RelType == ELF::R_LARCH_CALL36) &&
1799 resolveLoongArch64Branch(SectionID, Value, RelI, Stubs);
1800 } else if (RelType == ELF::R_LARCH_GOT_PC_HI20 ||
1801 RelType == ELF::R_LARCH_GOT_PC_LO12 ||
1802 RelType == ELF::R_LARCH_GOT64_PC_HI12 ||
1803 RelType == ELF::R_LARCH_GOT64_PC_LO20) {
1804 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_LARCH_64);
1805 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
1806 RelType);
1807 } else {
1808 processSimpleRelocation(SectionID, Offset, RelType, Value);
1809 }
1810 } else if (IsMipsO32ABI) {
1811 uint8_t *Placeholder = reinterpret_cast<uint8_t *>(
1812 computePlaceholderAddress(SectionID, Offset));
1813 uint32_t Opcode = readBytesUnaligned(Placeholder, 4);
1814 if (RelType == ELF::R_MIPS_26) {
1815 // This is an Mips branch relocation, need to use a stub function.
1816 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1817 SectionEntry &Section = Sections[SectionID];
1818
1819 // Extract the addend from the instruction.
1820 // We shift up by two since the Value will be down shifted again
1821 // when applying the relocation.
1822 uint32_t Addend = (Opcode & 0x03ffffff) << 2;
1823
1824 Value.Addend += Addend;
1825
1826 // Look up for existing stub.
1827 auto [It, Inserted] = Stubs.try_emplace(Value);
1828 if (!Inserted) {
1829 RelocationEntry RE(SectionID, Offset, RelType, It->second);
1830 addRelocationForSection(RE, SectionID);
1831 LLVM_DEBUG(dbgs() << " Stub function found\n");
1832 } else {
1833 // Create a new stub function.
1834 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1835 It->second = Section.getStubOffset();
1836
1837 unsigned AbiVariant = Obj.getPlatformFlags();
1838
1839 uint8_t *StubTargetAddr = createStubFunction(
1840 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1841
1842 // Creating Hi and Lo relocations for the filled stub instructions.
1843 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1844 ELF::R_MIPS_HI16, Value.Addend);
1845 RelocationEntry RELo(SectionID,
1846 StubTargetAddr - Section.getAddress() + 4,
1847 ELF::R_MIPS_LO16, Value.Addend);
1848
1849 if (Value.SymbolName) {
1850 addRelocationForSymbol(REHi, Value.SymbolName);
1851 addRelocationForSymbol(RELo, Value.SymbolName);
1852 } else {
1853 addRelocationForSection(REHi, Value.SectionID);
1854 addRelocationForSection(RELo, Value.SectionID);
1855 }
1856
1857 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1858 addRelocationForSection(RE, SectionID);
1859 Section.advanceStubOffset(getMaxStubSize());
1860 }
1861 } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
1862 int64_t Addend = (Opcode & 0x0000ffff) << 16;
1863 RelocationEntry RE(SectionID, Offset, RelType, Addend);
1864 PendingRelocs.push_back(std::make_pair(Value, RE));
1865 } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) {
1866 int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff);
1867 for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) {
1868 const RelocationValueRef &MatchingValue = I->first;
1869 RelocationEntry &Reloc = I->second;
1870 if (MatchingValue == Value &&
1871 RelType == getMatchingLoRelocation(Reloc.RelType) &&
1872 SectionID == Reloc.SectionID) {
1873 Reloc.Addend += Addend;
1874 if (Value.SymbolName)
1875 addRelocationForSymbol(Reloc, Value.SymbolName);
1876 else
1877 addRelocationForSection(Reloc, Value.SectionID);
1878 I = PendingRelocs.erase(I);
1879 } else
1880 ++I;
1881 }
1882 RelocationEntry RE(SectionID, Offset, RelType, Addend);
1883 if (Value.SymbolName)
1884 addRelocationForSymbol(RE, Value.SymbolName);
1885 else
1886 addRelocationForSection(RE, Value.SectionID);
1887 } else {
1888 if (RelType == ELF::R_MIPS_32)
1889 Value.Addend += Opcode;
1890 else if (RelType == ELF::R_MIPS_PC16)
1891 Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2);
1892 else if (RelType == ELF::R_MIPS_PC19_S2)
1893 Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2);
1894 else if (RelType == ELF::R_MIPS_PC21_S2)
1895 Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2);
1896 else if (RelType == ELF::R_MIPS_PC26_S2)
1897 Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2);
1898 processSimpleRelocation(SectionID, Offset, RelType, Value);
1899 }
1900 } else if (IsMipsN32ABI || IsMipsN64ABI) {
1901 uint32_t r_type = RelType & 0xff;
1902 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
1903 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE
1904 || r_type == ELF::R_MIPS_GOT_DISP) {
1905 auto [I, Inserted] = GOTSymbolOffsets.try_emplace(TargetName);
1906 if (Inserted)
1907 I->second = allocateGOTEntries(1);
1908 RE.SymOffset = I->second;
1909 if (Value.SymbolName)
1910 addRelocationForSymbol(RE, Value.SymbolName);
1911 else
1912 addRelocationForSection(RE, Value.SectionID);
1913 } else if (RelType == ELF::R_MIPS_26) {
1914 // This is an Mips branch relocation, need to use a stub function.
1915 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1916 SectionEntry &Section = Sections[SectionID];
1917
1918 // Look up for existing stub.
1919 StubMap::const_iterator i = Stubs.find(Value);
1920 if (i != Stubs.end()) {
1921 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1922 addRelocationForSection(RE, SectionID);
1923 LLVM_DEBUG(dbgs() << " Stub function found\n");
1924 } else {
1925 // Create a new stub function.
1926 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1927 Stubs[Value] = Section.getStubOffset();
1928
1929 unsigned AbiVariant = Obj.getPlatformFlags();
1930
1931 uint8_t *StubTargetAddr = createStubFunction(
1932 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1933
1934 if (IsMipsN32ABI) {
1935 // Creating Hi and Lo relocations for the filled stub instructions.
1936 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1937 ELF::R_MIPS_HI16, Value.Addend);
1938 RelocationEntry RELo(SectionID,
1939 StubTargetAddr - Section.getAddress() + 4,
1940 ELF::R_MIPS_LO16, Value.Addend);
1941 if (Value.SymbolName) {
1942 addRelocationForSymbol(REHi, Value.SymbolName);
1943 addRelocationForSymbol(RELo, Value.SymbolName);
1944 } else {
1945 addRelocationForSection(REHi, Value.SectionID);
1946 addRelocationForSection(RELo, Value.SectionID);
1947 }
1948 } else {
1949 // Creating Highest, Higher, Hi and Lo relocations for the filled stub
1950 // instructions.
1951 RelocationEntry REHighest(SectionID,
1952 StubTargetAddr - Section.getAddress(),
1953 ELF::R_MIPS_HIGHEST, Value.Addend);
1954 RelocationEntry REHigher(SectionID,
1955 StubTargetAddr - Section.getAddress() + 4,
1956 ELF::R_MIPS_HIGHER, Value.Addend);
1957 RelocationEntry REHi(SectionID,
1958 StubTargetAddr - Section.getAddress() + 12,
1959 ELF::R_MIPS_HI16, Value.Addend);
1960 RelocationEntry RELo(SectionID,
1961 StubTargetAddr - Section.getAddress() + 20,
1962 ELF::R_MIPS_LO16, Value.Addend);
1963 if (Value.SymbolName) {
1964 addRelocationForSymbol(REHighest, Value.SymbolName);
1965 addRelocationForSymbol(REHigher, Value.SymbolName);
1966 addRelocationForSymbol(REHi, Value.SymbolName);
1967 addRelocationForSymbol(RELo, Value.SymbolName);
1968 } else {
1969 addRelocationForSection(REHighest, Value.SectionID);
1970 addRelocationForSection(REHigher, Value.SectionID);
1971 addRelocationForSection(REHi, Value.SectionID);
1972 addRelocationForSection(RELo, Value.SectionID);
1973 }
1974 }
1975 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1976 addRelocationForSection(RE, SectionID);
1977 Section.advanceStubOffset(getMaxStubSize());
1978 }
1979 } else {
1980 processSimpleRelocation(SectionID, Offset, RelType, Value);
1981 }
1982
1983 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
1984 if (RelType == ELF::R_PPC64_REL24) {
1985 // Determine ABI variant in use for this object.
1986 unsigned AbiVariant = Obj.getPlatformFlags();
1987 AbiVariant &= ELF::EF_PPC64_ABI;
1988 // A PPC branch relocation will need a stub function if the target is
1989 // an external symbol (either Value.SymbolName is set, or SymType is
1990 // Symbol::ST_Unknown) or if the target address is not within the
1991 // signed 24-bits branch address.
1992 SectionEntry &Section = Sections[SectionID];
1993 uint8_t *Target = Section.getAddressWithOffset(Offset);
1994 bool RangeOverflow = false;
1995 bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown;
1996 if (!IsExtern) {
1997 if (AbiVariant != 2) {
1998 // In the ELFv1 ABI, a function call may point to the .opd entry,
1999 // so the final symbol value is calculated based on the relocation
2000 // values in the .opd section.
2001 if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value))
2002 return std::move(Err);
2003 } else {
2004 // In the ELFv2 ABI, a function symbol may provide a local entry
2005 // point, which must be used for direct calls.
2006 if (Value.SectionID == SectionID){
2007 uint8_t SymOther = Symbol->getOther();
2008 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
2009 }
2010 }
2011 uint8_t *RelocTarget =
2012 Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
2013 int64_t delta = static_cast<int64_t>(Target - RelocTarget);
2014 // If it is within 26-bits branch range, just set the branch target
2015 if (SignExtend64<26>(delta) != delta) {
2016 RangeOverflow = true;
2017 } else if ((AbiVariant != 2) ||
2018 (AbiVariant == 2 && Value.SectionID == SectionID)) {
2019 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
2020 addRelocationForSection(RE, Value.SectionID);
2021 }
2022 }
2023 if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID) ||
2024 RangeOverflow) {
2025 // It is an external symbol (either Value.SymbolName is set, or
2026 // SymType is SymbolRef::ST_Unknown) or out of range.
2027 auto [It, Inserted] = Stubs.try_emplace(Value);
2028 if (!Inserted) {
2029 // Symbol function stub already created, just relocate to it
2030 resolveRelocation(Section, Offset,
2031 Section.getLoadAddressWithOffset(It->second),
2032 RelType, 0);
2033 LLVM_DEBUG(dbgs() << " Stub function found\n");
2034 } else {
2035 // Create a new stub function.
2036 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
2037 It->second = Section.getStubOffset();
2038 uint8_t *StubTargetAddr = createStubFunction(
2039 Section.getAddressWithOffset(Section.getStubOffset()),
2040 AbiVariant);
2041 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
2042 ELF::R_PPC64_ADDR64, Value.Addend);
2043
2044 // Generates the 64-bits address loads as exemplified in section
2045 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
2046 // apply to the low part of the instructions, so we have to update
2047 // the offset according to the target endianness.
2048 uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
2050 StubRelocOffset += 2;
2051
2052 RelocationEntry REhst(SectionID, StubRelocOffset + 0,
2053 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
2054 RelocationEntry REhr(SectionID, StubRelocOffset + 4,
2055 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
2056 RelocationEntry REh(SectionID, StubRelocOffset + 12,
2057 ELF::R_PPC64_ADDR16_HI, Value.Addend);
2058 RelocationEntry REl(SectionID, StubRelocOffset + 16,
2059 ELF::R_PPC64_ADDR16_LO, Value.Addend);
2060
2061 if (Value.SymbolName) {
2062 addRelocationForSymbol(REhst, Value.SymbolName);
2063 addRelocationForSymbol(REhr, Value.SymbolName);
2064 addRelocationForSymbol(REh, Value.SymbolName);
2065 addRelocationForSymbol(REl, Value.SymbolName);
2066 } else {
2067 addRelocationForSection(REhst, Value.SectionID);
2068 addRelocationForSection(REhr, Value.SectionID);
2069 addRelocationForSection(REh, Value.SectionID);
2070 addRelocationForSection(REl, Value.SectionID);
2071 }
2072
2073 resolveRelocation(
2074 Section, Offset,
2075 Section.getLoadAddressWithOffset(Section.getStubOffset()),
2076 RelType, 0);
2077 Section.advanceStubOffset(getMaxStubSize());
2078 }
2079 if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) {
2080 // Restore the TOC for external calls
2081 if (AbiVariant == 2)
2082 writeInt32BE(Target + 4, 0xE8410018); // ld r2,24(r1)
2083 else
2084 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1)
2085 }
2086 }
2087 } else if (RelType == ELF::R_PPC64_TOC16 ||
2088 RelType == ELF::R_PPC64_TOC16_DS ||
2089 RelType == ELF::R_PPC64_TOC16_LO ||
2090 RelType == ELF::R_PPC64_TOC16_LO_DS ||
2091 RelType == ELF::R_PPC64_TOC16_HI ||
2092 RelType == ELF::R_PPC64_TOC16_HA) {
2093 // These relocations are supposed to subtract the TOC address from
2094 // the final value. This does not fit cleanly into the RuntimeDyld
2095 // scheme, since there may be *two* sections involved in determining
2096 // the relocation value (the section of the symbol referred to by the
2097 // relocation, and the TOC section associated with the current module).
2098 //
2099 // Fortunately, these relocations are currently only ever generated
2100 // referring to symbols that themselves reside in the TOC, which means
2101 // that the two sections are actually the same. Thus they cancel out
2102 // and we can immediately resolve the relocation right now.
2103 switch (RelType) {
2104 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break;
2105 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break;
2106 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break;
2107 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break;
2108 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break;
2109 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break;
2110 default: llvm_unreachable("Wrong relocation type.");
2111 }
2112
2113 RelocationValueRef TOCValue;
2114 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue))
2115 return std::move(Err);
2116 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID)
2117 llvm_unreachable("Unsupported TOC relocation.");
2118 Value.Addend -= TOCValue.Addend;
2119 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0);
2120 } else {
2121 // There are two ways to refer to the TOC address directly: either
2122 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
2123 // ignored), or via any relocation that refers to the magic ".TOC."
2124 // symbols (in which case the addend is respected).
2125 if (RelType == ELF::R_PPC64_TOC) {
2126 RelType = ELF::R_PPC64_ADDR64;
2127 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
2128 return std::move(Err);
2129 } else if (TargetName == ".TOC.") {
2130 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value))
2131 return std::move(Err);
2132 Value.Addend += Addend;
2133 }
2134
2135 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
2136
2137 if (Value.SymbolName)
2138 addRelocationForSymbol(RE, Value.SymbolName);
2139 else
2140 addRelocationForSection(RE, Value.SectionID);
2141 }
2142 } else if (Arch == Triple::systemz &&
2143 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) {
2144 // Create function stubs for both PLT and GOT references, regardless of
2145 // whether the GOT reference is to data or code. The stub contains the
2146 // full address of the symbol, as needed by GOT references, and the
2147 // executable part only adds an overhead of 8 bytes.
2148 //
2149 // We could try to conserve space by allocating the code and data
2150 // parts of the stub separately. However, as things stand, we allocate
2151 // a stub for every relocation, so using a GOT in JIT code should be
2152 // no less space efficient than using an explicit constant pool.
2153 LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
2154 SectionEntry &Section = Sections[SectionID];
2155
2156 // Look for an existing stub.
2157 StubMap::const_iterator i = Stubs.find(Value);
2158 uintptr_t StubAddress;
2159 if (i != Stubs.end()) {
2160 StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
2161 LLVM_DEBUG(dbgs() << " Stub function found\n");
2162 } else {
2163 // Create a new stub function.
2164 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
2165
2166 uintptr_t BaseAddress = uintptr_t(Section.getAddress());
2167 StubAddress =
2168 alignTo(BaseAddress + Section.getStubOffset(), getStubAlignment());
2169 unsigned StubOffset = StubAddress - BaseAddress;
2170
2171 Stubs[Value] = StubOffset;
2172 createStubFunction((uint8_t *)StubAddress);
2173 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64,
2174 Value.Offset);
2175 if (Value.SymbolName)
2176 addRelocationForSymbol(RE, Value.SymbolName);
2177 else
2178 addRelocationForSection(RE, Value.SectionID);
2179 Section.advanceStubOffset(getMaxStubSize());
2180 }
2181
2182 if (RelType == ELF::R_390_GOTENT)
2183 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL,
2184 Addend);
2185 else
2186 resolveRelocation(Section, Offset, StubAddress, RelType, Addend);
2187 } else if (Arch == Triple::x86_64) {
2188 if (RelType == ELF::R_X86_64_PLT32) {
2189 // The way the PLT relocations normally work is that the linker allocates
2190 // the
2191 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
2192 // entry will then jump to an address provided by the GOT. On first call,
2193 // the
2194 // GOT address will point back into PLT code that resolves the symbol. After
2195 // the first call, the GOT entry points to the actual function.
2196 //
2197 // For local functions we're ignoring all of that here and just replacing
2198 // the PLT32 relocation type with PC32, which will translate the relocation
2199 // into a PC-relative call directly to the function. For external symbols we
2200 // can't be sure the function will be within 2^32 bytes of the call site, so
2201 // we need to create a stub, which calls into the GOT. This case is
2202 // equivalent to the usual PLT implementation except that we use the stub
2203 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
2204 // rather than allocating a PLT section.
2205 if (Value.SymbolName && MemMgr.allowStubAllocation()) {
2206 // This is a call to an external function.
2207 // Look for an existing stub.
2208 SectionEntry *Section = &Sections[SectionID];
2209 auto [It, Inserted] = Stubs.try_emplace(Value);
2210 uintptr_t StubAddress;
2211 if (!Inserted) {
2212 StubAddress = uintptr_t(Section->getAddress()) + It->second;
2213 LLVM_DEBUG(dbgs() << " Stub function found\n");
2214 } else {
2215 // Create a new stub function (equivalent to a PLT entry).
2216 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
2217
2218 uintptr_t BaseAddress = uintptr_t(Section->getAddress());
2219 StubAddress = alignTo(BaseAddress + Section->getStubOffset(),
2220 getStubAlignment());
2221 unsigned StubOffset = StubAddress - BaseAddress;
2222 It->second = StubOffset;
2223 createStubFunction((uint8_t *)StubAddress);
2224
2225 // Bump our stub offset counter
2226 Section->advanceStubOffset(getMaxStubSize());
2227
2228 // Allocate a GOT Entry
2229 uint64_t GOTOffset = allocateGOTEntries(1);
2230 // This potentially creates a new Section which potentially
2231 // invalidates the Section pointer, so reload it.
2232 Section = &Sections[SectionID];
2233
2234 // The load of the GOT address has an addend of -4
2235 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4,
2236 ELF::R_X86_64_PC32);
2237
2238 // Fill in the value of the symbol we're targeting into the GOT
2240 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64),
2241 Value.SymbolName);
2242 }
2243
2244 // Make the target call a call into the stub table.
2245 resolveRelocation(*Section, Offset, StubAddress, ELF::R_X86_64_PC32,
2246 Addend);
2247 } else {
2249 computePlaceholderAddress(SectionID, Offset));
2250 processSimpleRelocation(SectionID, Offset, ELF::R_X86_64_PC32, Value);
2251 }
2252 } else if (RelType == ELF::R_X86_64_GOTPCREL ||
2253 RelType == ELF::R_X86_64_GOTPCRELX ||
2254 RelType == ELF::R_X86_64_REX_GOTPCRELX) {
2255 uint64_t GOTOffset = allocateGOTEntries(1);
2256 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
2257 ELF::R_X86_64_PC32);
2258
2259 // Fill in the value of the symbol we're targeting into the GOT
2260 RelocationEntry RE =
2261 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
2262 if (Value.SymbolName)
2263 addRelocationForSymbol(RE, Value.SymbolName);
2264 else
2265 addRelocationForSection(RE, Value.SectionID);
2266 } else if (RelType == ELF::R_X86_64_GOT64) {
2267 // Fill in a 64-bit GOT offset.
2268 uint64_t GOTOffset = allocateGOTEntries(1);
2269 resolveRelocation(Sections[SectionID], Offset, GOTOffset,
2270 ELF::R_X86_64_64, 0);
2271
2272 // Fill in the value of the symbol we're targeting into the GOT
2273 RelocationEntry RE =
2274 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64);
2275 if (Value.SymbolName)
2276 addRelocationForSymbol(RE, Value.SymbolName);
2277 else
2278 addRelocationForSection(RE, Value.SectionID);
2279 } else if (RelType == ELF::R_X86_64_GOTPC32) {
2280 // Materialize the address of the base of the GOT relative to the PC.
2281 // This doesn't create a GOT entry, but it does mean we need a GOT
2282 // section.
2283 (void)allocateGOTEntries(0);
2284 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC32);
2285 } else if (RelType == ELF::R_X86_64_GOTPC64) {
2286 (void)allocateGOTEntries(0);
2287 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64);
2288 } else if (RelType == ELF::R_X86_64_GOTOFF64) {
2289 // GOTOFF relocations ultimately require a section difference relocation.
2290 (void)allocateGOTEntries(0);
2291 processSimpleRelocation(SectionID, Offset, RelType, Value);
2292 } else if (RelType == ELF::R_X86_64_PC32) {
2293 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset));
2294 processSimpleRelocation(SectionID, Offset, RelType, Value);
2295 } else if (RelType == ELF::R_X86_64_PC64) {
2297 computePlaceholderAddress(SectionID, Offset));
2298 processSimpleRelocation(SectionID, Offset, RelType, Value);
2299 } else if (RelType == ELF::R_X86_64_GOTTPOFF) {
2300 processX86_64GOTTPOFFRelocation(SectionID, Offset, Value, Addend);
2301 } else if (RelType == ELF::R_X86_64_TLSGD ||
2302 RelType == ELF::R_X86_64_TLSLD) {
2303 // The next relocation must be the relocation for __tls_get_addr.
2304 ++RelI;
2305 auto &GetAddrRelocation = *RelI;
2306 processX86_64TLSRelocation(SectionID, Offset, RelType, Value, Addend,
2307 GetAddrRelocation);
2308 } else {
2309 processSimpleRelocation(SectionID, Offset, RelType, Value);
2310 }
2311 } else if (Arch == Triple::riscv32 || Arch == Triple::riscv64) {
2312 // *_LO12 relocation receive information about a symbol from the
2313 // corresponding *_HI20 relocation, so we have to collect this information
2314 // before resolving
2315 if (RelType == ELF::R_RISCV_GOT_HI20 ||
2316 RelType == ELF::R_RISCV_PCREL_HI20 ||
2317 RelType == ELF::R_RISCV_TPREL_HI20 ||
2318 RelType == ELF::R_RISCV_TLS_GD_HI20 ||
2319 RelType == ELF::R_RISCV_TLS_GOT_HI20) {
2320 RelocationEntry RE(SectionID, Offset, RelType, Addend);
2321 PendingRelocs.push_back({Value, RE});
2322 }
2323 processSimpleRelocation(SectionID, Offset, RelType, Value);
2324 } else {
2325 if (Arch == Triple::x86) {
2327 computePlaceholderAddress(SectionID, Offset));
2328 }
2329 processSimpleRelocation(SectionID, Offset, RelType, Value);
2330 }
2331 return ++RelI;
2332}
2333
2334void RuntimeDyldELF::processX86_64GOTTPOFFRelocation(unsigned SectionID,
2337 int64_t Addend) {
2338 // Use the approach from "x86-64 Linker Optimizations" from the TLS spec
2339 // to replace the GOTTPOFF relocation with a TPOFF relocation. The spec
2340 // only mentions one optimization even though there are two different
2341 // code sequences for the Initial Exec TLS Model. We match the code to
2342 // find out which one was used.
2343
2344 // A possible TLS code sequence and its replacement
2345 struct CodeSequence {
2346 // The expected code sequence
2347 ArrayRef<uint8_t> ExpectedCodeSequence;
2348 // The negative offset of the GOTTPOFF relocation to the beginning of
2349 // the sequence
2350 uint64_t TLSSequenceOffset;
2351 // The new code sequence
2352 ArrayRef<uint8_t> NewCodeSequence;
2353 // The offset of the new TPOFF relocation
2354 uint64_t TpoffRelocationOffset;
2355 };
2356
2357 std::array<CodeSequence, 2> CodeSequences;
2358
2359 // Initial Exec Code Model Sequence
2360 {
2361 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
2362 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2363 0x00, // mov %fs:0, %rax
2364 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // add x@gotpoff(%rip),
2365 // %rax
2366 };
2367 CodeSequences[0].ExpectedCodeSequence =
2368 ArrayRef<uint8_t>(ExpectedCodeSequenceList);
2369 CodeSequences[0].TLSSequenceOffset = 12;
2370
2371 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
2372 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0, %rax
2373 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax), %rax
2374 };
2375 CodeSequences[0].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
2376 CodeSequences[0].TpoffRelocationOffset = 12;
2377 }
2378
2379 // Initial Exec Code Model Sequence, II
2380 {
2381 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = {
2382 0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00, // mov x@gotpoff(%rip), %rax
2383 0x64, 0x48, 0x8b, 0x00, 0x00, 0x00, 0x00 // mov %fs:(%rax), %rax
2384 };
2385 CodeSequences[1].ExpectedCodeSequence =
2386 ArrayRef<uint8_t>(ExpectedCodeSequenceList);
2387 CodeSequences[1].TLSSequenceOffset = 3;
2388
2389 static const std::initializer_list<uint8_t> NewCodeSequenceList = {
2390 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, // 6 byte nop
2391 0x64, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:x@tpoff, %rax
2392 };
2393 CodeSequences[1].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList);
2394 CodeSequences[1].TpoffRelocationOffset = 10;
2395 }
2396
2397 bool Resolved = false;
2398 auto &Section = Sections[SectionID];
2399 for (const auto &C : CodeSequences) {
2400 assert(C.ExpectedCodeSequence.size() == C.NewCodeSequence.size() &&
2401 "Old and new code sequences must have the same size");
2402
2403 if (Offset < C.TLSSequenceOffset ||
2404 (Offset - C.TLSSequenceOffset + C.NewCodeSequence.size()) >
2405 Section.getSize()) {
2406 // This can't be a matching sequence as it doesn't fit in the current
2407 // section
2408 continue;
2409 }
2410
2411 auto TLSSequenceStartOffset = Offset - C.TLSSequenceOffset;
2412 auto *TLSSequence = Section.getAddressWithOffset(TLSSequenceStartOffset);
2413 if (ArrayRef<uint8_t>(TLSSequence, C.ExpectedCodeSequence.size()) !=
2414 C.ExpectedCodeSequence) {
2415 continue;
2416 }
2417
2418 memcpy(TLSSequence, C.NewCodeSequence.data(), C.NewCodeSequence.size());
2419
2420 // The original GOTTPOFF relocation has an addend as it is PC relative,
2421 // so it needs to be corrected. The TPOFF32 relocation is used as an
2422 // absolute value (which is an offset from %fs:0), so remove the addend
2423 // again.
2424 RelocationEntry RE(SectionID,
2425 TLSSequenceStartOffset + C.TpoffRelocationOffset,
2426 ELF::R_X86_64_TPOFF32, Value.Addend - Addend);
2427
2428 if (Value.SymbolName)
2429 addRelocationForSymbol(RE, Value.SymbolName);
2430 else
2431 addRelocationForSection(RE, Value.SectionID);
2432
2433 Resolved = true;
2434 break;
2435 }
2436
2437 if (!Resolved) {
2438 // The GOTTPOFF relocation was not used in one of the sequences
2439 // described in the spec, so we can't optimize it to a TPOFF
2440 // relocation.
2441 uint64_t GOTOffset = allocateGOTEntries(1);
2442 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend,
2443 ELF::R_X86_64_PC32);
2444 RelocationEntry RE =
2445 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_TPOFF64);
2446 if (Value.SymbolName)
2447 addRelocationForSymbol(RE, Value.SymbolName);
2448 else
2449 addRelocationForSection(RE, Value.SectionID);
2450 }
2451}
2452
2453void RuntimeDyldELF::processX86_64TLSRelocation(
2454 unsigned SectionID, uint64_t Offset, uint64_t RelType,
2455 RelocationValueRef Value, int64_t Addend,
2456 const RelocationRef &GetAddrRelocation) {
2457 // Since we are statically linking and have no additional DSOs, we can resolve
2458 // the relocation directly without using __tls_get_addr.
2459 // Use the approach from "x86-64 Linker Optimizations" from the TLS spec
2460 // to replace it with the Local Exec relocation variant.
2461
2462 // Find out whether the code was compiled with the large or small memory
2463 // model. For this we look at the next relocation which is the relocation
2464 // for the __tls_get_addr function. If it's a 32 bit relocation, it's the
2465 // small code model, with a 64 bit relocation it's the large code model.
2466 bool IsSmallCodeModel;
2467 // Is the relocation for the __tls_get_addr a PC-relative GOT relocation?
2468 bool IsGOTPCRel = false;
2469
2470 switch (GetAddrRelocation.getType()) {
2471 case ELF::R_X86_64_GOTPCREL:
2472 case ELF::R_X86_64_REX_GOTPCRELX:
2473 case ELF::R_X86_64_GOTPCRELX:
2474 IsGOTPCRel = true;
2475 [[fallthrough]];
2476 case ELF::R_X86_64_PLT32:
2477 IsSmallCodeModel = true;
2478 break;
2479 case ELF::R_X86_64_PLTOFF64:
2480 IsSmallCodeModel = false;
2481 break;
2482 default:
2484 "invalid TLS relocations for General/Local Dynamic TLS Model: "
2485 "expected PLT or GOT relocation for __tls_get_addr function");
2486 }
2487
2488 // The negative offset to the start of the TLS code sequence relative to
2489 // the offset of the TLSGD/TLSLD relocation
2490 uint64_t TLSSequenceOffset;
2491 // The expected start of the code sequence
2492 ArrayRef<uint8_t> ExpectedCodeSequence;
2493 // The new TLS code sequence that will replace the existing code
2494 ArrayRef<uint8_t> NewCodeSequence;
2495
2496 if (RelType == ELF::R_X86_64_TLSGD) {
2497 // The offset of the new TPOFF32 relocation (offset starting from the
2498 // beginning of the whole TLS sequence)
2499 uint64_t TpoffRelocOffset;
2500
2501 if (IsSmallCodeModel) {
2502 if (!IsGOTPCRel) {
2503 static const std::initializer_list<uint8_t> CodeSequence = {
2504 0x66, // data16 (no-op prefix)
2505 0x48, 0x8d, 0x3d, 0x00, 0x00,
2506 0x00, 0x00, // lea <disp32>(%rip), %rdi
2507 0x66, 0x66, // two data16 prefixes
2508 0x48, // rex64 (no-op prefix)
2509 0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt
2510 };
2511 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2512 TLSSequenceOffset = 4;
2513 } else {
2514 // This code sequence is not described in the TLS spec but gcc
2515 // generates it sometimes.
2516 static const std::initializer_list<uint8_t> CodeSequence = {
2517 0x66, // data16 (no-op prefix)
2518 0x48, 0x8d, 0x3d, 0x00, 0x00,
2519 0x00, 0x00, // lea <disp32>(%rip), %rdi
2520 0x66, // data16 prefix (no-op prefix)
2521 0x48, // rex64 (no-op prefix)
2522 0xff, 0x15, 0x00, 0x00, 0x00,
2523 0x00 // call *__tls_get_addr@gotpcrel(%rip)
2524 };
2525 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2526 TLSSequenceOffset = 4;
2527 }
2528
2529 // The replacement code for the small code model. It's the same for
2530 // both sequences.
2531 static const std::initializer_list<uint8_t> SmallSequence = {
2532 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2533 0x00, // mov %fs:0, %rax
2534 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax),
2535 // %rax
2536 };
2537 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2538 TpoffRelocOffset = 12;
2539 } else {
2540 static const std::initializer_list<uint8_t> CodeSequence = {
2541 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip),
2542 // %rdi
2543 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2544 0x00, // movabs $__tls_get_addr@pltoff, %rax
2545 0x48, 0x01, 0xd8, // add %rbx, %rax
2546 0xff, 0xd0 // call *%rax
2547 };
2548 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2549 TLSSequenceOffset = 3;
2550
2551 // The replacement code for the large code model
2552 static const std::initializer_list<uint8_t> LargeSequence = {
2553 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00,
2554 0x00, // mov %fs:0, %rax
2555 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00, // lea x@tpoff(%rax),
2556 // %rax
2557 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 // nopw 0x0(%rax,%rax,1)
2558 };
2559 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2560 TpoffRelocOffset = 12;
2561 }
2562
2563 // The TLSGD/TLSLD relocations are PC-relative, so they have an addend.
2564 // The new TPOFF32 relocations is used as an absolute offset from
2565 // %fs:0, so remove the TLSGD/TLSLD addend again.
2566 RelocationEntry RE(SectionID, Offset - TLSSequenceOffset + TpoffRelocOffset,
2567 ELF::R_X86_64_TPOFF32, Value.Addend - Addend);
2568 if (Value.SymbolName)
2569 addRelocationForSymbol(RE, Value.SymbolName);
2570 else
2571 addRelocationForSection(RE, Value.SectionID);
2572 } else if (RelType == ELF::R_X86_64_TLSLD) {
2573 if (IsSmallCodeModel) {
2574 if (!IsGOTPCRel) {
2575 static const std::initializer_list<uint8_t> CodeSequence = {
2576 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi
2577 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt
2578 };
2579 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2580 TLSSequenceOffset = 3;
2581
2582 // The replacement code for the small code model
2583 static const std::initializer_list<uint8_t> SmallSequence = {
2584 0x66, 0x66, 0x66, // three data16 prefixes (no-op)
2585 0x64, 0x48, 0x8b, 0x04, 0x25,
2586 0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax
2587 };
2588 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2589 } else {
2590 // This code sequence is not described in the TLS spec but gcc
2591 // generates it sometimes.
2592 static const std::initializer_list<uint8_t> CodeSequence = {
2593 0x48, 0x8d, 0x3d, 0x00,
2594 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi
2595 0xff, 0x15, 0x00, 0x00,
2596 0x00, 0x00 // call
2597 // *__tls_get_addr@gotpcrel(%rip)
2598 };
2599 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2600 TLSSequenceOffset = 3;
2601
2602 // The replacement is code is just like above but it needs to be
2603 // one byte longer.
2604 static const std::initializer_list<uint8_t> SmallSequence = {
2605 0x0f, 0x1f, 0x40, 0x00, // 4 byte nop
2606 0x64, 0x48, 0x8b, 0x04, 0x25,
2607 0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax
2608 };
2609 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence);
2610 }
2611 } else {
2612 // This is the same sequence as for the TLSGD sequence with the large
2613 // memory model above
2614 static const std::initializer_list<uint8_t> CodeSequence = {
2615 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip),
2616 // %rdi
2617 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2618 0x48, // movabs $__tls_get_addr@pltoff, %rax
2619 0x01, 0xd8, // add %rbx, %rax
2620 0xff, 0xd0 // call *%rax
2621 };
2622 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence);
2623 TLSSequenceOffset = 3;
2624
2625 // The replacement code for the large code model
2626 static const std::initializer_list<uint8_t> LargeSequence = {
2627 0x66, 0x66, 0x66, // three data16 prefixes (no-op)
2628 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00,
2629 0x00, // 10 byte nop
2630 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
2631 };
2632 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence);
2633 }
2634 } else {
2635 llvm_unreachable("both TLS relocations handled above");
2636 }
2637
2638 assert(ExpectedCodeSequence.size() == NewCodeSequence.size() &&
2639 "Old and new code sequences must have the same size");
2640
2641 auto &Section = Sections[SectionID];
2642 if (Offset < TLSSequenceOffset ||
2643 (Offset - TLSSequenceOffset + NewCodeSequence.size()) >
2644 Section.getSize()) {
2645 report_fatal_error("unexpected end of section in TLS sequence");
2646 }
2647
2648 auto *TLSSequence = Section.getAddressWithOffset(Offset - TLSSequenceOffset);
2649 if (ArrayRef<uint8_t>(TLSSequence, ExpectedCodeSequence.size()) !=
2650 ExpectedCodeSequence) {
2652 "invalid TLS sequence for Global/Local Dynamic TLS Model");
2653 }
2654
2655 memcpy(TLSSequence, NewCodeSequence.data(), NewCodeSequence.size());
2656}
2657
2659 // We don't use the GOT in all of these cases, but it's essentially free
2660 // to put them all here.
2661 size_t Result = 0;
2662 switch (Arch) {
2663 case Triple::x86_64:
2664 case Triple::aarch64:
2665 case Triple::aarch64_be:
2667 case Triple::ppc64:
2668 case Triple::ppc64le:
2669 case Triple::systemz:
2670 Result = sizeof(uint64_t);
2671 break;
2672 case Triple::x86:
2673 case Triple::arm:
2674 case Triple::thumb:
2675 Result = sizeof(uint32_t);
2676 break;
2677 case Triple::mips:
2678 case Triple::mipsel:
2679 case Triple::mips64:
2680 case Triple::mips64el:
2682 Result = sizeof(uint32_t);
2683 else if (IsMipsN64ABI)
2684 Result = sizeof(uint64_t);
2685 else
2686 llvm_unreachable("Mips ABI not handled");
2687 break;
2688 default:
2689 llvm_unreachable("Unsupported CPU type!");
2690 }
2691 return Result;
2692}
2693
2694uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no) {
2695 if (GOTSectionID == 0) {
2696 GOTSectionID = Sections.size();
2697 // Reserve a section id. We'll allocate the section later
2698 // once we know the total size
2699 Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0));
2700 }
2701 uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize();
2702 CurrentGOTIndex += no;
2703 return StartOffset;
2704}
2705
2706uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef &Value,
2707 unsigned GOTRelType) {
2708 auto E = GOTOffsetMap.insert({Value, 0});
2709 if (E.second) {
2710 uint64_t GOTOffset = allocateGOTEntries(1);
2711
2712 // Create relocation for newly created GOT entry
2713 RelocationEntry RE =
2714 computeGOTOffsetRE(GOTOffset, Value.Offset, GOTRelType);
2715 if (Value.SymbolName)
2716 addRelocationForSymbol(RE, Value.SymbolName);
2717 else
2718 addRelocationForSection(RE, Value.SectionID);
2719
2720 E.first->second = GOTOffset;
2721 }
2722
2723 return E.first->second;
2724}
2725
2726void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID,
2728 uint64_t GOTOffset,
2729 uint32_t Type) {
2730 // Fill in the relative address of the GOT Entry into the stub
2731 RelocationEntry GOTRE(SectionID, Offset, Type, GOTOffset);
2732 addRelocationForSection(GOTRE, GOTSectionID);
2733}
2734
2735RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset,
2736 uint64_t SymbolOffset,
2737 uint32_t Type) {
2738 return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset);
2739}
2740
2741void RuntimeDyldELF::processNewSymbol(const SymbolRef &ObjSymbol, SymbolTableEntry& Symbol) {
2742 // This should never return an error as `processNewSymbol` wouldn't have been
2743 // called if getFlags() returned an error before.
2744 auto ObjSymbolFlags = cantFail(ObjSymbol.getFlags());
2745
2746 if (ObjSymbolFlags & SymbolRef::SF_Indirect) {
2747 if (IFuncStubSectionID == 0) {
2748 // Create a dummy section for the ifunc stubs. It will be actually
2749 // allocated in finalizeLoad() below.
2750 IFuncStubSectionID = Sections.size();
2751 Sections.push_back(
2752 SectionEntry(".text.__llvm_IFuncStubs", nullptr, 0, 0, 0));
2753 // First 64B are reserverd for the IFunc resolver
2754 IFuncStubOffset = 64;
2755 }
2756
2757 IFuncStubs.push_back(IFuncStub{IFuncStubOffset, Symbol});
2758 // Modify the symbol so that it points to the ifunc stub instead of to the
2759 // resolver function.
2760 Symbol = SymbolTableEntry(IFuncStubSectionID, IFuncStubOffset,
2761 Symbol.getFlags());
2762 IFuncStubOffset += getMaxIFuncStubSize();
2763 }
2764}
2765
2767 ObjSectionToIDMap &SectionMap) {
2768 if (IsMipsO32ABI)
2769 if (!PendingRelocs.empty())
2770 return make_error<RuntimeDyldError>("Can't find matching LO16 reloc");
2771
2772 // Create the IFunc stubs if necessary. This must be done before processing
2773 // the GOT entries, as the IFunc stubs may create some.
2774 if (IFuncStubSectionID != 0) {
2775 uint8_t *IFuncStubsAddr = MemMgr.allocateCodeSection(
2776 IFuncStubOffset, 1, IFuncStubSectionID, ".text.__llvm_IFuncStubs");
2777 if (!IFuncStubsAddr)
2778 return make_error<RuntimeDyldError>(
2779 "Unable to allocate memory for IFunc stubs!");
2780 Sections[IFuncStubSectionID] =
2781 SectionEntry(".text.__llvm_IFuncStubs", IFuncStubsAddr, IFuncStubOffset,
2782 IFuncStubOffset, 0);
2783
2784 createIFuncResolver(IFuncStubsAddr);
2785
2786 LLVM_DEBUG(dbgs() << "Creating IFunc stubs SectionID: "
2787 << IFuncStubSectionID << " Addr: "
2788 << Sections[IFuncStubSectionID].getAddress() << '\n');
2789 for (auto &IFuncStub : IFuncStubs) {
2790 auto &Symbol = IFuncStub.OriginalSymbol;
2791 LLVM_DEBUG(dbgs() << "\tSectionID: " << Symbol.getSectionID()
2792 << " Offset: " << format("%p", Symbol.getOffset())
2793 << " IFuncStubOffset: "
2794 << format("%p\n", IFuncStub.StubOffset));
2795 createIFuncStub(IFuncStubSectionID, 0, IFuncStub.StubOffset,
2796 Symbol.getSectionID(), Symbol.getOffset());
2797 }
2798
2799 IFuncStubSectionID = 0;
2800 IFuncStubOffset = 0;
2801 IFuncStubs.clear();
2802 }
2803
2804 // If necessary, allocate the global offset table
2805 if (GOTSectionID != 0) {
2806 // Allocate memory for the section
2807 size_t TotalSize = CurrentGOTIndex * getGOTEntrySize();
2809 GOTSectionID, ".got", false);
2810 if (!Addr)
2811 return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!");
2812
2813 Sections[GOTSectionID] =
2814 SectionEntry(".got", Addr, TotalSize, TotalSize, 0);
2815
2816 // For now, initialize all GOT entries to zero. We'll fill them in as
2817 // needed when GOT-based relocations are applied.
2818 memset(Addr, 0, TotalSize);
2819 if (IsMipsN32ABI || IsMipsN64ABI) {
2820 // To correctly resolve Mips GOT relocations, we need a mapping from
2821 // object's sections to GOTs.
2822 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
2823 SI != SE; ++SI) {
2824 if (!SI->relocations().empty()) {
2825 Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
2826 if (!RelSecOrErr)
2827 return make_error<RuntimeDyldError>(
2828 toString(RelSecOrErr.takeError()));
2829
2830 section_iterator RelocatedSection = *RelSecOrErr;
2831 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection);
2832 assert(i != SectionMap.end());
2833 SectionToGOTMap[i->second] = GOTSectionID;
2834 }
2835 }
2836 GOTSymbolOffsets.clear();
2837 }
2838 }
2839
2840 // Look for and record the EH frame section.
2841 ObjSectionToIDMap::iterator i, e;
2842 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
2843 const SectionRef &Section = i->first;
2844
2846 Expected<StringRef> NameOrErr = Section.getName();
2847 if (NameOrErr)
2848 Name = *NameOrErr;
2849 else
2850 consumeError(NameOrErr.takeError());
2851
2852 if (Name == ".eh_frame") {
2853 UnregisteredEHFrameSections.push_back(i->second);
2854 break;
2855 }
2856 }
2857
2858 GOTOffsetMap.clear();
2859 GOTSectionID = 0;
2860 CurrentGOTIndex = 0;
2861
2862 return Error::success();
2863}
2864
2866 return Obj.isELF();
2867}
2868
2869void RuntimeDyldELF::createIFuncResolver(uint8_t *Addr) const {
2870 if (Arch == Triple::x86_64) {
2871 // The adddres of the GOT1 entry is in %r11, the GOT2 entry is in %r11+8
2872 // (see createIFuncStub() for details)
2873 // The following code first saves all registers that contain the original
2874 // function arguments as those registers are not saved by the resolver
2875 // function. %r11 is saved as well so that the GOT2 entry can be updated
2876 // afterwards. Then it calls the actual IFunc resolver function whose
2877 // address is stored in GOT2. After the resolver function returns, all
2878 // saved registers are restored and the return value is written to GOT1.
2879 // Finally, jump to the now resolved function.
2880 // clang-format off
2881 const uint8_t StubCode[] = {
2882 0x57, // push %rdi
2883 0x56, // push %rsi
2884 0x52, // push %rdx
2885 0x51, // push %rcx
2886 0x41, 0x50, // push %r8
2887 0x41, 0x51, // push %r9
2888 0x41, 0x53, // push %r11
2889 0x41, 0xff, 0x53, 0x08, // call *0x8(%r11)
2890 0x41, 0x5b, // pop %r11
2891 0x41, 0x59, // pop %r9
2892 0x41, 0x58, // pop %r8
2893 0x59, // pop %rcx
2894 0x5a, // pop %rdx
2895 0x5e, // pop %rsi
2896 0x5f, // pop %rdi
2897 0x49, 0x89, 0x03, // mov %rax,(%r11)
2898 0xff, 0xe0 // jmp *%rax
2899 };
2900 // clang-format on
2901 static_assert(sizeof(StubCode) <= 64,
2902 "maximum size of the IFunc resolver is 64B");
2903 memcpy(Addr, StubCode, sizeof(StubCode));
2904 } else {
2906 "IFunc resolver is not supported for target architecture");
2907 }
2908}
2909
2910void RuntimeDyldELF::createIFuncStub(unsigned IFuncStubSectionID,
2911 uint64_t IFuncResolverOffset,
2912 uint64_t IFuncStubOffset,
2913 unsigned IFuncSectionID,
2914 uint64_t IFuncOffset) {
2915 auto &IFuncStubSection = Sections[IFuncStubSectionID];
2916 auto *Addr = IFuncStubSection.getAddressWithOffset(IFuncStubOffset);
2917
2918 if (Arch == Triple::x86_64) {
2919 // The first instruction loads a PC-relative address into %r11 which is a
2920 // GOT entry for this stub. This initially contains the address to the
2921 // IFunc resolver. We can use %r11 here as it's caller saved but not used
2922 // to pass any arguments. In fact, x86_64 ABI even suggests using %r11 for
2923 // code in the PLT. The IFunc resolver will use %r11 to update the GOT
2924 // entry.
2925 //
2926 // The next instruction just jumps to the address contained in the GOT
2927 // entry. As mentioned above, we do this two-step jump by first setting
2928 // %r11 so that the IFunc resolver has access to it.
2929 //
2930 // The IFunc resolver of course also needs to know the actual address of
2931 // the actual IFunc resolver function. This will be stored in a GOT entry
2932 // right next to the first one for this stub. So, the IFunc resolver will
2933 // be able to call it with %r11+8.
2934 //
2935 // In total, two adjacent GOT entries (+relocation) and one additional
2936 // relocation are required:
2937 // GOT1: Address of the IFunc resolver.
2938 // GOT2: Address of the IFunc resolver function.
2939 // IFuncStubOffset+3: 32-bit PC-relative address of GOT1.
2940 uint64_t GOT1 = allocateGOTEntries(2);
2941 uint64_t GOT2 = GOT1 + getGOTEntrySize();
2942
2943 RelocationEntry RE1(GOTSectionID, GOT1, ELF::R_X86_64_64,
2944 IFuncResolverOffset, {});
2945 addRelocationForSection(RE1, IFuncStubSectionID);
2946 RelocationEntry RE2(GOTSectionID, GOT2, ELF::R_X86_64_64, IFuncOffset, {});
2947 addRelocationForSection(RE2, IFuncSectionID);
2948
2949 const uint8_t StubCode[] = {
2950 0x4c, 0x8d, 0x1d, 0x00, 0x00, 0x00, 0x00, // leaq 0x0(%rip),%r11
2951 0x41, 0xff, 0x23 // jmpq *(%r11)
2952 };
2953 assert(sizeof(StubCode) <= getMaxIFuncStubSize() &&
2954 "IFunc stub size must not exceed getMaxIFuncStubSize()");
2955 memcpy(Addr, StubCode, sizeof(StubCode));
2956
2957 // The PC-relative value starts 4 bytes from the end of the leaq
2958 // instruction, so the addend is -4.
2959 resolveGOTOffsetRelocation(IFuncStubSectionID, IFuncStubOffset + 3,
2960 GOT1 - 4, ELF::R_X86_64_PC32);
2961 } else {
2962 report_fatal_error("IFunc stub is not supported for target architecture");
2963 }
2964}
2965
2966unsigned RuntimeDyldELF::getMaxIFuncStubSize() const {
2967 if (Arch == Triple::x86_64) {
2968 return 10;
2969 }
2970 return 0;
2971}
2972
2973bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef &R) const {
2974 unsigned RelTy = R.getType();
2976 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE ||
2977 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC;
2978
2980 return RelTy == ELF::R_LARCH_GOT_PC_HI20 ||
2981 RelTy == ELF::R_LARCH_GOT_PC_LO12 ||
2982 RelTy == ELF::R_LARCH_GOT64_PC_HI12 ||
2983 RelTy == ELF::R_LARCH_GOT64_PC_LO20;
2984
2985 if (Arch == Triple::x86_64)
2986 return RelTy == ELF::R_X86_64_GOTPCREL ||
2987 RelTy == ELF::R_X86_64_GOTPCRELX ||
2988 RelTy == ELF::R_X86_64_GOT64 ||
2989 RelTy == ELF::R_X86_64_REX_GOTPCRELX;
2990 return false;
2991}
2992
2993bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const {
2994 if (Arch != Triple::x86_64)
2995 return true; // Conservative answer
2996
2997 switch (R.getType()) {
2998 default:
2999 return true; // Conservative answer
3000
3001
3002 case ELF::R_X86_64_GOTPCREL:
3003 case ELF::R_X86_64_GOTPCRELX:
3004 case ELF::R_X86_64_REX_GOTPCRELX:
3005 case ELF::R_X86_64_GOTPC64:
3006 case ELF::R_X86_64_GOT64:
3007 case ELF::R_X86_64_GOTOFF64:
3008 case ELF::R_X86_64_PC32:
3009 case ELF::R_X86_64_PC64:
3010 case ELF::R_X86_64_64:
3011 // We know that these reloation types won't need a stub function. This list
3012 // can be extended as needed.
3013 return false;
3014 }
3015}
3016
3017} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
Given that RA is a live value
uint64_t Addr
std::string Name
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:107
bool End
Definition: ELF_riscv.cpp:480
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
static void or32le(void *P, int32_t V)
static void or32AArch64Imm(void *L, uint64_t Imm)
static void write(bool isBE, void *P, T V)
static uint64_t getBits(uint64_t Val, int Start, int End)
static void write32AArch64Addr(void *L, uint64_t Imm)
raw_pwrite_stream & OS
#define LLVM_DEBUG(...)
Definition: Debug.h:119
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
const T * data() const
Definition: ArrayRef.h:144
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
Error takeError()
Take ownership of the stored error.
Definition: Error.h:612
Symbol resolution interface.
Definition: JITSymbol.h:373
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
RelocationEntry - used to represent relocations internally in the dynamic linker.
uint32_t RelType
RelType - relocation type.
uint64_t Offset
Offset - offset into the section.
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
unsigned SectionID
SectionID - the section this relocation points to.
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2196
void registerEHFrames() override
size_t getGOTEntrySize() override
~RuntimeDyldELF() override
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Error finalizeLoad(const ObjectFile &Obj, ObjSectionToIDMap &SectionMap) override
DenseMap< SID, SID > SectionToGOTMap
bool isCompatibleFile(const object::ObjectFile &Obj) const override
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &O) override
RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
std::map< SectionRef, unsigned > ObjSectionToIDMap
void writeInt32BE(uint8_t *Addr, uint32_t Value)
void writeInt64BE(uint8_t *Addr, uint64_t Value)
std::map< RelocationValueRef, uintptr_t > StubMap
void writeInt16BE(uint8_t *Addr, uint16_t Value)
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
RuntimeDyld::MemoryManager & MemMgr
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
Triple::ArchType Arch
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
uint64_t getSectionLoadAddress(unsigned SectionID) const
RTDyldSymbolTable GlobalSymbolTable
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
virtual uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName, bool IsReadOnly)=0
Allocate a memory block of (at least) the given size suitable for data.
virtual uint8_t * allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName)=0
Allocate a memory block of (at least) the given size suitable for executable code.
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size)=0
Register the EH frames with the runtime so that c++ exceptions work.
virtual bool allowStubAllocation() const
Override to return false to tell LLVM no stub space will be needed.
Definition: RuntimeDyld.h:149
SectionEntry - represents a section emitted into memory by the dynamic linker.
void push_back(const T &Elt)
Definition: SmallVector.h:414
iterator end()
Definition: StringMap.h:224
iterator find(StringRef Key)
Definition: StringMap.h:237
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition: StringMap.h:372
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
Symbol info for RuntimeDyld.
Target - Wrapper for Target specific information.
@ UnknownArch
Definition: Triple.h:50
@ aarch64_be
Definition: Triple.h:55
@ loongarch64
Definition: Triple.h:65
@ mips64el
Definition: Triple.h:70
static LLVM_ABI StringRef getArchTypePrefix(ArchType Kind)
Get the "prefix" canonical name for the Kind architecture.
Definition: Triple.cpp:171
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
Definition: SymbolicFile.h:207
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:211
StringRef getData() const
Definition: Binary.cpp:39
bool isLittleEndian() const
Definition: Binary.h:157
StringRef getFileName() const
Definition: Binary.cpp:41
bool isELF() const
Definition: Binary.h:125
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
static bool classof(const Binary *v)
Expected< const Elf_Sym * > getSymbol(DataRefImpl Sym) const
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
Expected< int64_t > getAddend() const
This class is the base class for all object file types.
Definition: ObjectFile.h:231
virtual section_iterator section_end() const =0
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
section_iterator_range sections() const
Definition: ObjectFile.h:331
virtual StringRef getFileFormatName() const =0
virtual section_iterator section_begin() const =0
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:54
uint64_t getType() const
Definition: ObjectFile.h:633
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:83
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:603
bool isText() const
Whether this section contains instructions.
Definition: ObjectFile.h:555
Expected< StringRef > getName() const
Definition: ObjectFile.h:522
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:170
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:485
virtual basic_symbol_iterator symbol_end() const =0
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ EF_MIPS_ABI_O32
Definition: ELF.h:534
@ EF_MIPS_ABI2
Definition: ELF.h:526
static int64_t decodePPC64LocalEntryOffset(unsigned Other)
Definition: ELF.h:426
@ EF_PPC64_ABI
Definition: ELF.h:418
@ Resolved
Queried, materialization begun.
NodeAddr< InstrNode * > Instr
Definition: RDFGraph.h:389
void write32le(void *P, uint32_t V)
Definition: Endian.h:472
uint32_t read32le(const void *P)
Definition: Endian.h:429
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
static uint16_t applyPPChighera(uint64_t value)
static uint16_t applyPPChi(uint64_t value)
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:990
static void applyITypeImmRISCV(uint8_t *InstrAddr, uint32_t Imm)
static uint16_t applyPPChighesta(uint64_t value)
static uint16_t applyPPChighest(uint64_t value)
LLVM_ABI Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
Definition: DWP.cpp:622
static uint16_t applyPPCha(uint64_t value)
static void applyUTypeImmRISCV(uint8_t *InstrAddr, uint32_t Imm)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
Definition: MathExtras.h:164
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition: Casting.h:548
static uint16_t applyPPClo(uint64_t value)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:126
@ Ref
The access may reference the value stored in memory.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:769
static uint16_t applyPPChigher(uint64_t value)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
static void or32le(void *P, int32_t V)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:565
static uint32_t extractBits(uint64_t Val, uint32_t Hi, uint32_t Lo)
const char * toString(DWARFSectionKind Kind)
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition: MathExtras.h:577
static uint64_t getLoongArchPageDelta(uint64_t dest, uint64_t pc, uint32_t type)
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083
static void write32AArch64Addr(void *T, uint64_t s, uint64_t p, int shift)
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...