xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/JITLink/ELF.cpp (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 //===-------------- ELF.cpp - JIT linker function for ELF -------------===//
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 jit-link function.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ExecutionEngine/JITLink/ELF.h"
14 
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/ExecutionEngine/JITLink/ELF_aarch64.h"
17 #include "llvm/ExecutionEngine/JITLink/ELF_riscv.h"
18 #include "llvm/ExecutionEngine/JITLink/ELF_x86_64.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include <cstring>
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "jitlink"
28 
29 namespace llvm {
30 namespace jitlink {
31 
32 Expected<uint16_t> readTargetMachineArch(StringRef Buffer) {
33   const char *Data = Buffer.data();
34 
35   if (Data[ELF::EI_DATA] == ELF::ELFDATA2LSB) {
36     if (Data[ELF::EI_CLASS] == ELF::ELFCLASS64) {
37       if (auto File = llvm::object::ELF64LEFile::create(Buffer)) {
38         return File->getHeader().e_machine;
39       } else {
40         return File.takeError();
41       }
42     } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
43       if (auto File = llvm::object::ELF32LEFile::create(Buffer)) {
44         return File->getHeader().e_machine;
45       } else {
46         return File.takeError();
47       }
48     }
49   }
50 
51   return ELF::EM_NONE;
52 }
53 
54 Expected<std::unique_ptr<LinkGraph>>
55 createLinkGraphFromELFObject(MemoryBufferRef ObjectBuffer) {
56   StringRef Buffer = ObjectBuffer.getBuffer();
57   if (Buffer.size() < ELF::EI_MAG3 + 1)
58     return make_error<JITLinkError>("Truncated ELF buffer");
59 
60   if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
61     return make_error<JITLinkError>("ELF magic not valid");
62 
63   Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
64   if (!TargetMachineArch)
65     return TargetMachineArch.takeError();
66 
67   switch (*TargetMachineArch) {
68   case ELF::EM_AARCH64:
69     return createLinkGraphFromELFObject_aarch64(ObjectBuffer);
70   case ELF::EM_RISCV:
71     return createLinkGraphFromELFObject_riscv(ObjectBuffer);
72   case ELF::EM_X86_64:
73     return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
74   default:
75     return make_error<JITLinkError>(
76         "Unsupported target machine architecture in ELF object " +
77         ObjectBuffer.getBufferIdentifier());
78   }
79 }
80 
81 void link_ELF(std::unique_ptr<LinkGraph> G,
82               std::unique_ptr<JITLinkContext> Ctx) {
83   switch (G->getTargetTriple().getArch()) {
84   case Triple::aarch64:
85     link_ELF_aarch64(std::move(G), std::move(Ctx));
86     return;
87   case Triple::riscv32:
88   case Triple::riscv64:
89     link_ELF_riscv(std::move(G), std::move(Ctx));
90     return;
91   case Triple::x86_64:
92     link_ELF_x86_64(std::move(G), std::move(Ctx));
93     return;
94   default:
95     Ctx->notifyFailed(make_error<JITLinkError>(
96         "Unsupported target machine architecture in ELF link graph " +
97         G->getName()));
98     return;
99   }
100 }
101 
102 } // end namespace jitlink
103 } // end namespace llvm
104