LLVM 17.0.0git
ELF.cpp
Go to the documentation of this file.
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
14
21#include "llvm/Object/ELF.h"
22#include "llvm/Support/Endian.h"
23#include "llvm/Support/Format.h"
25#include <cstring>
26
27using namespace llvm;
28
29#define DEBUG_TYPE "jitlink"
30
31namespace llvm {
32namespace jitlink {
33
35 const char *Data = Buffer.data();
36
39 if (auto File = llvm::object::ELF64LEFile::create(Buffer)) {
40 return File->getHeader().e_machine;
41 } else {
42 return File.takeError();
43 }
44 } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
45 if (auto File = llvm::object::ELF32LEFile::create(Buffer)) {
46 return File->getHeader().e_machine;
47 } else {
48 return File.takeError();
49 }
50 }
51 }
52
53 return ELF::EM_NONE;
54}
55
58 StringRef Buffer = ObjectBuffer.getBuffer();
59 if (Buffer.size() < ELF::EI_MAG3 + 1)
60 return make_error<JITLinkError>("Truncated ELF buffer");
61
62 if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
63 return make_error<JITLinkError>("ELF magic not valid");
64
65 Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
66 if (!TargetMachineArch)
67 return TargetMachineArch.takeError();
68
69 switch (*TargetMachineArch) {
70 case ELF::EM_AARCH64:
71 return createLinkGraphFromELFObject_aarch64(ObjectBuffer);
73 return createLinkGraphFromELFObject_loongarch(ObjectBuffer);
74 case ELF::EM_RISCV:
75 return createLinkGraphFromELFObject_riscv(ObjectBuffer);
76 case ELF::EM_X86_64:
77 return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
78 case ELF::EM_386:
79 return createLinkGraphFromELFObject_i386(ObjectBuffer);
80 default:
81 return make_error<JITLinkError>(
82 "Unsupported target machine architecture in ELF object " +
83 ObjectBuffer.getBufferIdentifier());
84 }
85}
86
87void link_ELF(std::unique_ptr<LinkGraph> G,
88 std::unique_ptr<JITLinkContext> Ctx) {
89 switch (G->getTargetTriple().getArch()) {
90 case Triple::aarch64:
91 link_ELF_aarch64(std::move(G), std::move(Ctx));
92 return;
95 link_ELF_loongarch(std::move(G), std::move(Ctx));
96 return;
97 case Triple::riscv32:
98 case Triple::riscv64:
99 link_ELF_riscv(std::move(G), std::move(Ctx));
100 return;
101 case Triple::x86_64:
102 link_ELF_x86_64(std::move(G), std::move(Ctx));
103 return;
104 case Triple::x86:
105 link_ELF_i386(std::move(G), std::move(Ctx));
106 return;
107 default:
108 Ctx->notifyFailed(make_error<JITLinkError>(
109 "Unsupported target machine architecture in ELF link graph " +
110 G->getName()));
111 return;
112 }
113}
114
115} // end namespace jitlink
116} // end namespace llvm
#define G(x, y, z)
Definition: MD5.cpp:56
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:908
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
StringRef getBufferIdentifier() const
StringRef getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
@ loongarch32
Definition: Triple.h:61
@ loongarch64
Definition: Triple.h:62
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:760
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASS32
Definition: ELF.h:328
@ EM_NONE
Definition: ELF.h:133
@ EM_386
Definition: ELF.h:136
@ EM_LOONGARCH
Definition: ELF.h:322
@ EM_X86_64
Definition: ELF.h:178
@ EM_AARCH64
Definition: ELF.h:280
@ EM_RISCV
Definition: ELF.h:317
@ ELFDATA2LSB
Definition: ELF.h:335
static const char ElfMagic[]
Definition: ELF.h:44
@ EI_DATA
Definition: ELF.h:53
@ EI_MAG3
Definition: ELF.h:51
@ EI_CLASS
Definition: ELF.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18