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