1 //===---- ELF_x86_64.cpp -JIT linker implementation for ELF/x86-64 ----===// 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 // ELF/x86-64 jit-link implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h" 14 #include "BasicGOTAndStubsBuilder.h" 15 #include "JITLinkGeneric.h" 16 #include "llvm/ExecutionEngine/JITLink/JITLink.h" 17 #include "llvm/Object/ELFObjectFile.h" 18 #include "llvm/Support/Endian.h" 19 20 #define DEBUG_TYPE "jitlink" 21 22 using namespace llvm; 23 using namespace llvm::jitlink; 24 using namespace llvm::jitlink::ELF_x86_64_Edges; 25 26 namespace { 27 class ELF_x86_64_GOTAndStubsBuilder 28 : public BasicGOTAndStubsBuilder<ELF_x86_64_GOTAndStubsBuilder> { 29 public: 30 static const uint8_t NullGOTEntryContent[8]; 31 static const uint8_t StubContent[6]; 32 33 ELF_x86_64_GOTAndStubsBuilder(LinkGraph &G) 34 : BasicGOTAndStubsBuilder<ELF_x86_64_GOTAndStubsBuilder>(G) {} 35 36 bool isGOTEdge(Edge &E) const { 37 return E.getKind() == PCRel32GOT || E.getKind() == PCRel32GOTLoad; 38 } 39 40 Symbol &createGOTEntry(Symbol &Target) { 41 auto &GOTEntryBlock = G.createContentBlock( 42 getGOTSection(), getGOTEntryBlockContent(), 0, 8, 0); 43 GOTEntryBlock.addEdge(Pointer64, 0, Target, 0); 44 return G.addAnonymousSymbol(GOTEntryBlock, 0, 8, false, false); 45 } 46 47 void fixGOTEdge(Edge &E, Symbol &GOTEntry) { 48 assert((E.getKind() == PCRel32GOT || E.getKind() == PCRel32GOTLoad) && 49 "Not a GOT edge?"); 50 // If this is a PCRel32GOT then change it to an ordinary PCRel32. If it is 51 // a PCRel32GOTLoad then leave it as-is for now. We will use the kind to 52 // check for GOT optimization opportunities in the 53 // optimizeMachO_x86_64_GOTAndStubs pass below. 54 if (E.getKind() == PCRel32GOT) 55 E.setKind(PCRel32); 56 57 E.setTarget(GOTEntry); 58 // Leave the edge addend as-is. 59 } 60 61 bool isExternalBranchEdge(Edge &E) { 62 return E.getKind() == Branch32 && !E.getTarget().isDefined(); 63 } 64 65 Symbol &createStub(Symbol &Target) { 66 auto &StubContentBlock = 67 G.createContentBlock(getStubsSection(), getStubBlockContent(), 0, 1, 0); 68 // Re-use GOT entries for stub targets. 69 auto &GOTEntrySymbol = getGOTEntrySymbol(Target); 70 StubContentBlock.addEdge(PCRel32, 2, GOTEntrySymbol, 0); 71 return G.addAnonymousSymbol(StubContentBlock, 0, 6, true, false); 72 } 73 74 void fixExternalBranchEdge(Edge &E, Symbol &Stub) { 75 assert(E.getKind() == Branch32 && "Not a Branch32 edge?"); 76 assert(E.getAddend() == 0 && "Branch32 edge has non-zero addend?"); 77 78 // Set the edge kind to Branch32ToStub. We will use this to check for stub 79 // optimization opportunities in the optimize ELF_x86_64_GOTAndStubs pass 80 // below. 81 E.setKind(Branch32ToStub); 82 E.setTarget(Stub); 83 } 84 85 private: 86 Section &getGOTSection() { 87 if (!GOTSection) 88 GOTSection = &G.createSection("$__GOT", sys::Memory::MF_READ); 89 return *GOTSection; 90 } 91 92 Section &getStubsSection() { 93 if (!StubsSection) { 94 auto StubsProt = static_cast<sys::Memory::ProtectionFlags>( 95 sys::Memory::MF_READ | sys::Memory::MF_EXEC); 96 StubsSection = &G.createSection("$__STUBS", StubsProt); 97 } 98 return *StubsSection; 99 } 100 101 StringRef getGOTEntryBlockContent() { 102 return StringRef(reinterpret_cast<const char *>(NullGOTEntryContent), 103 sizeof(NullGOTEntryContent)); 104 } 105 106 StringRef getStubBlockContent() { 107 return StringRef(reinterpret_cast<const char *>(StubContent), 108 sizeof(StubContent)); 109 } 110 111 Section *GOTSection = nullptr; 112 Section *StubsSection = nullptr; 113 }; 114 } // namespace 115 116 const uint8_t ELF_x86_64_GOTAndStubsBuilder::NullGOTEntryContent[8] = { 117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 118 const uint8_t ELF_x86_64_GOTAndStubsBuilder::StubContent[6] = { 119 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00}; 120 121 static const char *CommonSectionName = "__common"; 122 static Error optimizeELF_x86_64_GOTAndStubs(LinkGraph &G) { 123 LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n"); 124 125 for (auto *B : G.blocks()) 126 for (auto &E : B->edges()) 127 if (E.getKind() == PCRel32GOTLoad) { 128 assert(E.getOffset() >= 3 && "GOT edge occurs too early in block"); 129 130 // Switch the edge kind to PCRel32: Whether we change the edge target 131 // or not this will be the desired kind. 132 E.setKind(PCRel32); 133 134 // Optimize GOT references. 135 auto &GOTBlock = E.getTarget().getBlock(); 136 assert(GOTBlock.getSize() == G.getPointerSize() && 137 "GOT entry block should be pointer sized"); 138 assert(GOTBlock.edges_size() == 1 && 139 "GOT entry should only have one outgoing edge"); 140 141 auto &GOTTarget = GOTBlock.edges().begin()->getTarget(); 142 JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset(); 143 JITTargetAddress TargetAddr = GOTTarget.getAddress(); 144 145 // Check that this is a recognized MOV instruction. 146 // FIXME: Can we assume this? 147 constexpr uint8_t MOVQRIPRel[] = {0x48, 0x8b}; 148 if (strncmp(B->getContent().data() + E.getOffset() - 3, 149 reinterpret_cast<const char *>(MOVQRIPRel), 2) != 0) 150 continue; 151 152 int64_t Displacement = TargetAddr - EdgeAddr + 4; 153 if (Displacement >= std::numeric_limits<int32_t>::min() && 154 Displacement <= std::numeric_limits<int32_t>::max()) { 155 E.setTarget(GOTTarget); 156 auto *BlockData = reinterpret_cast<uint8_t *>( 157 const_cast<char *>(B->getContent().data())); 158 BlockData[E.getOffset() - 2] = 0x8d; 159 LLVM_DEBUG({ 160 dbgs() << " Replaced GOT load wih LEA:\n "; 161 printEdge(dbgs(), *B, E, getELFX86RelocationKindName(E.getKind())); 162 dbgs() << "\n"; 163 }); 164 } 165 } else if (E.getKind() == Branch32ToStub) { 166 167 // Switch the edge kind to PCRel32: Whether we change the edge target 168 // or not this will be the desired kind. 169 E.setKind(Branch32); 170 171 auto &StubBlock = E.getTarget().getBlock(); 172 assert(StubBlock.getSize() == 173 sizeof(ELF_x86_64_GOTAndStubsBuilder::StubContent) && 174 "Stub block should be stub sized"); 175 assert(StubBlock.edges_size() == 1 && 176 "Stub block should only have one outgoing edge"); 177 178 auto &GOTBlock = StubBlock.edges().begin()->getTarget().getBlock(); 179 assert(GOTBlock.getSize() == G.getPointerSize() && 180 "GOT block should be pointer sized"); 181 assert(GOTBlock.edges_size() == 1 && 182 "GOT block should only have one outgoing edge"); 183 184 auto &GOTTarget = GOTBlock.edges().begin()->getTarget(); 185 JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset(); 186 JITTargetAddress TargetAddr = GOTTarget.getAddress(); 187 188 int64_t Displacement = TargetAddr - EdgeAddr + 4; 189 if (Displacement >= std::numeric_limits<int32_t>::min() && 190 Displacement <= std::numeric_limits<int32_t>::max()) { 191 E.setTarget(GOTTarget); 192 LLVM_DEBUG({ 193 dbgs() << " Replaced stub branch with direct branch:\n "; 194 printEdge(dbgs(), *B, E, getELFX86RelocationKindName(E.getKind())); 195 dbgs() << "\n"; 196 }); 197 } 198 } 199 200 return Error::success(); 201 } 202 namespace llvm { 203 namespace jitlink { 204 205 // This should become a template as the ELFFile is so a lot of this could become 206 // generic 207 class ELFLinkGraphBuilder_x86_64 { 208 209 private: 210 Section *CommonSection = nullptr; 211 // TODO hack to get this working 212 // Find a better way 213 using SymbolTable = object::ELFFile<object::ELF64LE>::Elf_Shdr; 214 // For now we just assume 215 using SymbolMap = std::map<int32_t, Symbol *>; 216 SymbolMap JITSymbolTable; 217 218 Section &getCommonSection() { 219 if (!CommonSection) { 220 auto Prot = static_cast<sys::Memory::ProtectionFlags>( 221 sys::Memory::MF_READ | sys::Memory::MF_WRITE); 222 CommonSection = &G->createSection(CommonSectionName, Prot); 223 } 224 return *CommonSection; 225 } 226 227 static Expected<ELF_x86_64_Edges::ELFX86RelocationKind> 228 getRelocationKind(const uint32_t Type) { 229 switch (Type) { 230 case ELF::R_X86_64_PC32: 231 return ELF_x86_64_Edges::ELFX86RelocationKind::PCRel32; 232 case ELF::R_X86_64_64: 233 return ELF_x86_64_Edges::ELFX86RelocationKind::Pointer64; 234 case ELF::R_X86_64_GOTPCREL: 235 return ELF_x86_64_Edges::ELFX86RelocationKind::PCRel32GOTLoad; 236 } 237 return make_error<JITLinkError>("Unsupported x86-64 relocation:" + 238 formatv("{0:d}", Type)); 239 } 240 241 std::unique_ptr<LinkGraph> G; 242 // This could be a template 243 const object::ELFFile<object::ELF64LE> &Obj; 244 object::ELFFile<object::ELF64LE>::Elf_Shdr_Range sections; 245 SymbolTable SymTab; 246 247 bool isRelocatable() { return Obj.getHeader()->e_type == llvm::ELF::ET_REL; } 248 249 support::endianness 250 getEndianness(const object::ELFFile<object::ELF64LE> &Obj) { 251 return Obj.isLE() ? support::little : support::big; 252 } 253 254 // This could also just become part of a template 255 unsigned getPointerSize(const object::ELFFile<object::ELF64LE> &Obj) { 256 return Obj.getHeader()->getFileClass() == ELF::ELFCLASS64 ? 8 : 4; 257 } 258 259 // We don't technically need this right now 260 // But for now going to keep it as it helps me to debug things 261 262 Error createNormalizedSymbols() { 263 LLVM_DEBUG(dbgs() << "Creating normalized symbols...\n"); 264 265 for (auto SecRef : sections) { 266 if (SecRef.sh_type != ELF::SHT_SYMTAB && 267 SecRef.sh_type != ELF::SHT_DYNSYM) 268 continue; 269 270 auto Symbols = Obj.symbols(&SecRef); 271 // TODO: Currently I use this function to test things 272 // I also want to leave it to see if its common between MACH and elf 273 // so for now I just want to continue even if there is an error 274 if (errorToBool(Symbols.takeError())) 275 continue; 276 277 auto StrTabSec = Obj.getSection(SecRef.sh_link); 278 if (!StrTabSec) 279 return StrTabSec.takeError(); 280 auto StringTable = Obj.getStringTable(*StrTabSec); 281 if (!StringTable) 282 return StringTable.takeError(); 283 284 for (auto SymRef : *Symbols) { 285 Optional<StringRef> Name; 286 287 if (auto NameOrErr = SymRef.getName(*StringTable)) 288 Name = *NameOrErr; 289 else 290 return NameOrErr.takeError(); 291 292 LLVM_DEBUG({ 293 dbgs() << " "; 294 if (!Name) 295 dbgs() << "<anonymous symbol>"; 296 else 297 dbgs() << *Name; 298 dbgs() << ": value = " << formatv("{0:x16}", SymRef.getValue()) 299 << ", type = " << formatv("{0:x2}", SymRef.getType()) 300 << ", binding = " << SymRef.getBinding() 301 << ", size =" << SymRef.st_size 302 << ", info =" << SymRef.st_info; 303 dbgs() << "\n"; 304 }); 305 } 306 } 307 return Error::success(); 308 } 309 310 Error createNormalizedSections() { 311 LLVM_DEBUG(dbgs() << "Creating normalized sections...\n"); 312 for (auto &SecRef : sections) { 313 auto Name = Obj.getSectionName(&SecRef); 314 if (!Name) 315 return Name.takeError(); 316 sys::Memory::ProtectionFlags Prot; 317 if (SecRef.sh_flags & ELF::SHF_EXECINSTR) { 318 Prot = static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ | 319 sys::Memory::MF_EXEC); 320 } else { 321 Prot = static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ | 322 sys::Memory::MF_WRITE); 323 } 324 uint64_t Address = SecRef.sh_addr; 325 uint64_t Size = SecRef.sh_size; 326 uint64_t Flags = SecRef.sh_flags; 327 uint64_t Alignment = SecRef.sh_addralign; 328 const char *Data = nullptr; 329 // for now we just use this to skip the "undefined" section, probably need 330 // to revist 331 if (Size == 0) 332 continue; 333 334 // FIXME: Use flags. 335 (void)Flags; 336 337 LLVM_DEBUG({ 338 dbgs() << " " << *Name << ": " << formatv("{0:x16}", Address) << " -- " 339 << formatv("{0:x16}", Address + Size) << ", align: " << Alignment 340 << " Flags:" << Flags << "\n"; 341 }); 342 343 if (SecRef.sh_type != ELF::SHT_NOBITS) { 344 // .sections() already checks that the data is not beyond the end of 345 // file 346 auto contents = Obj.getSectionContentsAsArray<char>(&SecRef); 347 if (!contents) 348 return contents.takeError(); 349 350 Data = contents->data(); 351 // TODO protection flags. 352 // for now everything is 353 auto §ion = G->createSection(*Name, Prot); 354 // Do this here because we have it, but move it into graphify later 355 G->createContentBlock(section, StringRef(Data, Size), Address, 356 Alignment, 0); 357 if (SecRef.sh_type == ELF::SHT_SYMTAB) 358 // TODO: Dynamic? 359 SymTab = SecRef; 360 } 361 } 362 363 return Error::success(); 364 } 365 366 Error addRelocations() { 367 LLVM_DEBUG(dbgs() << "Adding relocations\n"); 368 // TODO a partern is forming of iterate some sections but only give me 369 // ones I am interested, i should abstract that concept some where 370 for (auto &SecRef : sections) { 371 if (SecRef.sh_type != ELF::SHT_RELA && SecRef.sh_type != ELF::SHT_REL) 372 continue; 373 // TODO can the elf obj file do this for me? 374 if (SecRef.sh_type == ELF::SHT_REL) 375 return make_error<llvm::StringError>("Shouldn't have REL in x64", 376 llvm::inconvertibleErrorCode()); 377 378 auto RelSectName = Obj.getSectionName(&SecRef); 379 if (!RelSectName) 380 return RelSectName.takeError(); 381 // Deal with .eh_frame later 382 if (*RelSectName == StringRef(".rela.eh_frame")) 383 continue; 384 385 auto UpdateSection = Obj.getSection(SecRef.sh_info); 386 if (!UpdateSection) 387 return UpdateSection.takeError(); 388 389 auto UpdateSectionName = Obj.getSectionName(*UpdateSection); 390 if (!UpdateSectionName) 391 return UpdateSectionName.takeError(); 392 393 auto JITSection = G->findSectionByName(*UpdateSectionName); 394 if (!JITSection) 395 return make_error<llvm::StringError>( 396 "Refencing a a section that wasn't added to graph" + 397 *UpdateSectionName, 398 llvm::inconvertibleErrorCode()); 399 400 auto Relocations = Obj.relas(&SecRef); 401 if (!Relocations) 402 return Relocations.takeError(); 403 404 for (const auto &Rela : *Relocations) { 405 auto Type = Rela.getType(false); 406 407 LLVM_DEBUG({ 408 dbgs() << "Relocation Type: " << Type << "\n" 409 << "Name: " << Obj.getRelocationTypeName(Type) << "\n"; 410 }); 411 auto SymbolIndex = Rela.getSymbol(false); 412 auto Symbol = Obj.getRelocationSymbol(&Rela, &SymTab); 413 if (!Symbol) 414 return Symbol.takeError(); 415 416 auto BlockToFix = *(JITSection->blocks().begin()); 417 auto *TargetSymbol = JITSymbolTable[SymbolIndex]; 418 419 if (!TargetSymbol) { 420 return make_error<llvm::StringError>( 421 "Could not find symbol at given index, did you add it to " 422 "JITSymbolTable? index: " + 423 std::to_string((*Symbol)->st_shndx) + 424 " Size of table: " + std::to_string(JITSymbolTable.size()), 425 llvm::inconvertibleErrorCode()); 426 } 427 uint64_t Addend = Rela.r_addend; 428 JITTargetAddress FixupAddress = 429 (*UpdateSection)->sh_addr + Rela.r_offset; 430 431 LLVM_DEBUG({ 432 dbgs() << "Processing relocation at " 433 << format("0x%016" PRIx64, FixupAddress) << "\n"; 434 }); 435 auto Kind = getRelocationKind(Type); 436 if (!Kind) 437 return Kind.takeError(); 438 439 LLVM_DEBUG({ 440 Edge GE(*Kind, FixupAddress - BlockToFix->getAddress(), *TargetSymbol, 441 Addend); 442 printEdge(dbgs(), *BlockToFix, GE, 443 getELFX86RelocationKindName(*Kind)); 444 dbgs() << "\n"; 445 }); 446 BlockToFix->addEdge(*Kind, FixupAddress - BlockToFix->getAddress(), 447 *TargetSymbol, Addend); 448 } 449 } 450 return Error::success(); 451 } 452 453 Error graphifyRegularSymbols() { 454 455 // TODO: ELF supports beyond SHN_LORESERVE, 456 // need to perf test how a vector vs map handles those cases 457 458 std::vector<std::vector<object::ELFFile<object::ELF64LE>::Elf_Shdr_Range *>> 459 SecIndexToSymbols; 460 461 LLVM_DEBUG(dbgs() << "Creating graph symbols...\n"); 462 463 for (auto SecRef : sections) { 464 465 if (SecRef.sh_type != ELF::SHT_SYMTAB && 466 SecRef.sh_type != ELF::SHT_DYNSYM) 467 continue; 468 auto Symbols = Obj.symbols(&SecRef); 469 if (!Symbols) 470 return Symbols.takeError(); 471 472 auto StrTabSec = Obj.getSection(SecRef.sh_link); 473 if (!StrTabSec) 474 return StrTabSec.takeError(); 475 auto StringTable = Obj.getStringTable(*StrTabSec); 476 if (!StringTable) 477 return StringTable.takeError(); 478 auto Name = Obj.getSectionName(&SecRef); 479 if (!Name) 480 return Name.takeError(); 481 auto Section = G->findSectionByName(*Name); 482 if (!Section) 483 return make_error<llvm::StringError>("Could not find a section", 484 llvm::inconvertibleErrorCode()); 485 // we only have one for now 486 auto blocks = Section->blocks(); 487 if (blocks.empty()) 488 return make_error<llvm::StringError>("Section has no block", 489 llvm::inconvertibleErrorCode()); 490 int SymbolIndex = -1; 491 for (auto SymRef : *Symbols) { 492 ++SymbolIndex; 493 auto Type = SymRef.getType(); 494 495 if (Type == ELF::STT_FILE || SymbolIndex == 0) 496 continue; 497 // these should do it for now 498 // if(Type != ELF::STT_NOTYPE && 499 // Type != ELF::STT_OBJECT && 500 // Type != ELF::STT_FUNC && 501 // Type != ELF::STT_SECTION && 502 // Type != ELF::STT_COMMON) { 503 // continue; 504 // } 505 std::pair<Linkage, Scope> bindings; 506 auto Name = SymRef.getName(*StringTable); 507 // I am not sure on If this is going to hold as an invariant. Revisit. 508 if (!Name) 509 return Name.takeError(); 510 // TODO: weak and hidden 511 if (SymRef.isExternal()) 512 bindings = {Linkage::Strong, Scope::Default}; 513 else 514 bindings = {Linkage::Strong, Scope::Local}; 515 516 if (SymRef.isDefined() && 517 (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT || 518 Type == ELF::STT_SECTION)) { 519 520 auto DefinedSection = Obj.getSection(SymRef.st_shndx); 521 if (!DefinedSection) 522 return DefinedSection.takeError(); 523 auto sectName = Obj.getSectionName(*DefinedSection); 524 if (!sectName) 525 return Name.takeError(); 526 527 auto JitSection = G->findSectionByName(*sectName); 528 if (!JitSection) 529 return make_error<llvm::StringError>( 530 "Could not find a section", llvm::inconvertibleErrorCode()); 531 auto bs = JitSection->blocks(); 532 if (bs.empty()) 533 return make_error<llvm::StringError>( 534 "Section has no block", llvm::inconvertibleErrorCode()); 535 536 auto B = *bs.begin(); 537 LLVM_DEBUG({ dbgs() << " " << *Name << ": "; }); 538 if (SymRef.getType() == ELF::STT_SECTION) 539 *Name = *sectName; 540 auto &S = G->addDefinedSymbol( 541 *B, SymRef.getValue(), *Name, SymRef.st_size, bindings.first, 542 bindings.second, SymRef.getType() == ELF::STT_FUNC, false); 543 JITSymbolTable[SymbolIndex] = &S; 544 } else if (SymRef.isUndefined() && SymRef.isExternal()) { 545 auto &S = G->addExternalSymbol(*Name, SymRef.st_size, bindings.first); 546 JITSymbolTable[SymbolIndex] = &S; 547 } 548 549 // } 550 // TODO: The following has to be implmented. 551 // leaving commented out to save time for future patchs 552 /* 553 G->addAbsoluteSymbol(*Name, SymRef.getValue(), SymRef.st_size, 554 Linkage::Strong, Scope::Default, false); 555 556 if(SymRef.isCommon()) { 557 G->addCommonSymbol(*Name, Scope::Default, getCommonSection(), 0, 0, 558 SymRef.getValue(), false); 559 } 560 */ 561 } 562 } 563 return Error::success(); 564 } 565 566 public: 567 ELFLinkGraphBuilder_x86_64(std::string filename, 568 const object::ELFFile<object::ELF64LE> &Obj) 569 : G(std::make_unique<LinkGraph>(filename, getPointerSize(Obj), 570 getEndianness(Obj))), 571 Obj(Obj) {} 572 573 Expected<std::unique_ptr<LinkGraph>> buildGraph() { 574 // Sanity check: we only operate on relocatable objects. 575 if (!isRelocatable()) 576 return make_error<JITLinkError>("Object is not a relocatable ELF"); 577 578 auto Secs = Obj.sections(); 579 580 if (!Secs) { 581 return Secs.takeError(); 582 } 583 sections = *Secs; 584 585 if (auto Err = createNormalizedSections()) 586 return std::move(Err); 587 588 if (auto Err = createNormalizedSymbols()) 589 return std::move(Err); 590 591 if (auto Err = graphifyRegularSymbols()) 592 return std::move(Err); 593 594 if (auto Err = addRelocations()) 595 return std::move(Err); 596 597 return std::move(G); 598 } 599 }; 600 601 class ELFJITLinker_x86_64 : public JITLinker<ELFJITLinker_x86_64> { 602 friend class JITLinker<ELFJITLinker_x86_64>; 603 604 public: 605 ELFJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx, 606 PassConfiguration PassConfig) 607 : JITLinker(std::move(Ctx), std::move(PassConfig)) {} 608 609 private: 610 StringRef getEdgeKindName(Edge::Kind R) const override { 611 return getELFX86RelocationKindName(R); 612 } 613 614 Expected<std::unique_ptr<LinkGraph>> 615 buildGraph(MemoryBufferRef ObjBuffer) override { 616 auto ELFObj = object::ObjectFile::createELFObjectFile(ObjBuffer); 617 if (!ELFObj) 618 return ELFObj.takeError(); 619 620 auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64LE>>(**ELFObj); 621 std::string fileName(ELFObj->get()->getFileName()); 622 return ELFLinkGraphBuilder_x86_64(std::move(fileName), 623 *ELFObjFile.getELFFile()) 624 .buildGraph(); 625 } 626 627 Error applyFixup(Block &B, const Edge &E, char *BlockWorkingMem) const { 628 using namespace ELF_x86_64_Edges; 629 using namespace llvm::support; 630 char *FixupPtr = BlockWorkingMem + E.getOffset(); 631 JITTargetAddress FixupAddress = B.getAddress() + E.getOffset(); 632 switch (E.getKind()) { 633 case ELFX86RelocationKind::PCRel32: { 634 int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; 635 endian::write32le(FixupPtr, Value); 636 break; 637 } 638 case ELFX86RelocationKind::Pointer64: { 639 int64_t Value = E.getTarget().getAddress() + E.getAddend(); 640 endian::write64le(FixupPtr, Value); 641 break; 642 } 643 } 644 return Error::success(); 645 } 646 }; 647 648 void jitLink_ELF_x86_64(std::unique_ptr<JITLinkContext> Ctx) { 649 PassConfiguration Config; 650 Triple TT("x86_64-linux"); 651 // Construct a JITLinker and run the link function. 652 // Add a mark-live pass. 653 if (auto MarkLive = Ctx->getMarkLivePass(TT)) 654 Config.PrePrunePasses.push_back(std::move(MarkLive)); 655 else 656 Config.PrePrunePasses.push_back(markAllSymbolsLive); 657 658 // Add an in-place GOT/Stubs pass. 659 Config.PostPrunePasses.push_back([](LinkGraph &G) -> Error { 660 ELF_x86_64_GOTAndStubsBuilder(G).run(); 661 return Error::success(); 662 }); 663 664 // Add GOT/Stubs optimizer pass. 665 Config.PostAllocationPasses.push_back(optimizeELF_x86_64_GOTAndStubs); 666 667 if (auto Err = Ctx->modifyPassConfig(TT, Config)) 668 return Ctx->notifyFailed(std::move(Err)); 669 670 ELFJITLinker_x86_64::link(std::move(Ctx), std::move(Config)); 671 } 672 StringRef getELFX86RelocationKindName(Edge::Kind R) { 673 switch (R) { 674 case PCRel32: 675 return "PCRel32"; 676 case Pointer64: 677 return "Pointer64"; 678 case PCRel32GOTLoad: 679 return "PCRel32GOTLoad"; 680 } 681 return getGenericEdgeKindName(static_cast<Edge::Kind>(R)); 682 } 683 } // end namespace jitlink 684 } // end namespace llvm 685