LLVM 20.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
23#include "llvm/Object/ELF.h"
24#include <cstring>
25
26using namespace llvm;
27
28#define DEBUG_TYPE "jitlink"
29
30namespace llvm {
31namespace jitlink {
32
34 const char *Data = Buffer.data();
35
38 if (auto File = llvm::object::ELF64LEFile::create(Buffer)) {
39 return File->getHeader().e_machine;
40 } else {
41 return File.takeError();
42 }
43 } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
44 if (auto File = llvm::object::ELF32LEFile::create(Buffer)) {
45 return File->getHeader().e_machine;
46 } else {
47 return File.takeError();
48 }
49 }
50 }
51
54 if (auto File = llvm::object::ELF64BEFile::create(Buffer)) {
55 return File->getHeader().e_machine;
56 } else {
57 return File.takeError();
58 }
59 } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
60 if (auto File = llvm::object::ELF32BEFile::create(Buffer)) {
61 return File->getHeader().e_machine;
62 } else {
63 return File.takeError();
64 }
65 }
66 }
67
68 return ELF::EM_NONE;
69}
70
73 std::shared_ptr<orc::SymbolStringPool> SSP) {
74 StringRef Buffer = ObjectBuffer.getBuffer();
75 if (Buffer.size() < ELF::EI_NIDENT)
76 return make_error<JITLinkError>("Truncated ELF buffer");
77
78 if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
79 return make_error<JITLinkError>("ELF magic not valid");
80
81 uint8_t DataEncoding = Buffer.data()[ELF::EI_DATA];
82 Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
83 if (!TargetMachineArch)
84 return TargetMachineArch.takeError();
85
86 switch (*TargetMachineArch) {
87 case ELF::EM_AARCH64:
88 return createLinkGraphFromELFObject_aarch64(ObjectBuffer, std::move(SSP));
89 case ELF::EM_ARM:
90 return createLinkGraphFromELFObject_aarch32(ObjectBuffer, std::move(SSP));
91 case ELF::EM_PPC64: {
92 if (DataEncoding == ELF::ELFDATA2LSB)
93 return createLinkGraphFromELFObject_ppc64le(ObjectBuffer, std::move(SSP));
94 else
95 return createLinkGraphFromELFObject_ppc64(ObjectBuffer, std::move(SSP));
96 }
98 return createLinkGraphFromELFObject_loongarch(ObjectBuffer, std::move(SSP));
99 case ELF::EM_RISCV:
100 return createLinkGraphFromELFObject_riscv(ObjectBuffer, std::move(SSP));
101 case ELF::EM_X86_64:
102 return createLinkGraphFromELFObject_x86_64(ObjectBuffer, std::move(SSP));
103 case ELF::EM_386:
104 return createLinkGraphFromELFObject_i386(ObjectBuffer, std::move(SSP));
105 default:
106 return make_error<JITLinkError>(
107 "Unsupported target machine architecture in ELF object " +
108 ObjectBuffer.getBufferIdentifier());
109 }
110}
111
112void link_ELF(std::unique_ptr<LinkGraph> G,
113 std::unique_ptr<JITLinkContext> Ctx) {
114 switch (G->getTargetTriple().getArch()) {
115 case Triple::aarch64:
116 link_ELF_aarch64(std::move(G), std::move(Ctx));
117 return;
118 case Triple::arm:
119 case Triple::armeb:
120 case Triple::thumb:
121 case Triple::thumbeb:
122 link_ELF_aarch32(std::move(G), std::move(Ctx));
123 return;
126 link_ELF_loongarch(std::move(G), std::move(Ctx));
127 return;
128 case Triple::ppc64:
129 link_ELF_ppc64(std::move(G), std::move(Ctx));
130 return;
131 case Triple::ppc64le:
132 link_ELF_ppc64le(std::move(G), std::move(Ctx));
133 return;
134 case Triple::riscv32:
135 case Triple::riscv64:
136 link_ELF_riscv(std::move(G), std::move(Ctx));
137 return;
138 case Triple::x86_64:
139 link_ELF_x86_64(std::move(G), std::move(Ctx));
140 return;
141 case Triple::x86:
142 link_ELF_i386(std::move(G), std::move(Ctx));
143 return;
144 default:
145 Ctx->notifyFailed(make_error<JITLinkError>(
146 "Unsupported target machine architecture in ELF link graph " +
147 G->getName()));
148 return;
149 }
150}
151
152} // end namespace jitlink
153} // end namespace llvm
#define G(x, y, z)
Definition: MD5.cpp:56
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:911
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
StringRef getBufferIdentifier() const
StringRef getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
@ loongarch32
Definition: Triple.h:61
@ loongarch64
Definition: Triple.h:62
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:888
@ ELFCLASS64
Definition: ELF.h:330
@ ELFCLASS32
Definition: ELF.h:329
@ EI_DATA
Definition: ELF.h:54
@ EI_NIDENT
Definition: ELF.h:59
@ EI_CLASS
Definition: ELF.h:53
@ EM_PPC64
Definition: ELF.h:150
@ EM_NONE
Definition: ELF.h:134
@ EM_386
Definition: ELF.h:137
@ EM_LOONGARCH
Definition: ELF.h:323
@ EM_X86_64
Definition: ELF.h:179
@ EM_AARCH64
Definition: ELF.h:281
@ EM_RISCV
Definition: ELF.h:318
@ EM_ARM
Definition: ELF.h:157
static const char ElfMagic[]
Definition: ELF.h:45
@ ELFDATA2MSB
Definition: ELF.h:337
@ ELFDATA2LSB
Definition: ELF.h:336
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18