LLVM 23.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
25#include "llvm/Object/ELF.h"
26#include <cstring>
27
28using namespace llvm;
29
30#define DEBUG_TYPE "jitlink"
31
32namespace llvm {
33namespace jitlink {
34
36 const char *Data = Buffer.data();
37
40 if (auto File = llvm::object::ELF64LEFile::create(Buffer)) {
41 return File->getHeader().e_machine;
42 } else {
43 return File.takeError();
44 }
45 } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
46 if (auto File = llvm::object::ELF32LEFile::create(Buffer)) {
47 return File->getHeader().e_machine;
48 } else {
49 return File.takeError();
50 }
51 }
52 }
53
56 if (auto File = llvm::object::ELF64BEFile::create(Buffer)) {
57 return File->getHeader().e_machine;
58 } else {
59 return File.takeError();
60 }
61 } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
62 if (auto File = llvm::object::ELF32BEFile::create(Buffer)) {
63 return File->getHeader().e_machine;
64 } else {
65 return File.takeError();
66 }
67 }
68 }
69
70 return ELF::EM_NONE;
71}
72
75 std::shared_ptr<orc::SymbolStringPool> SSP) {
76 StringRef Buffer = ObjectBuffer.getBuffer();
77 if (Buffer.size() < ELF::EI_NIDENT)
78 return make_error<JITLinkError>("Truncated ELF buffer");
79
80 if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
81 return make_error<JITLinkError>("ELF magic not valid");
82
83 uint8_t DataEncoding = Buffer.data()[ELF::EI_DATA];
84 Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
85 if (!TargetMachineArch)
86 return TargetMachineArch.takeError();
87
88 switch (*TargetMachineArch) {
89 case ELF::EM_AARCH64:
90 return createLinkGraphFromELFObject_aarch64(ObjectBuffer, std::move(SSP));
91 case ELF::EM_ARM:
92 return createLinkGraphFromELFObject_aarch32(ObjectBuffer, std::move(SSP));
93 case ELF::EM_HEXAGON:
94 return createLinkGraphFromELFObject_hexagon(ObjectBuffer, std::move(SSP));
95 case ELF::EM_PPC64: {
96 if (DataEncoding == ELF::ELFDATA2LSB)
97 return createLinkGraphFromELFObject_ppc64le(ObjectBuffer, std::move(SSP));
98 else
99 return createLinkGraphFromELFObject_ppc64(ObjectBuffer, std::move(SSP));
100 }
102 return createLinkGraphFromELFObject_loongarch(ObjectBuffer, std::move(SSP));
103 case ELF::EM_RISCV:
104 return createLinkGraphFromELFObject_riscv(ObjectBuffer, std::move(SSP));
105 case ELF::EM_S390:
106 return createLinkGraphFromELFObject_systemz(ObjectBuffer, std::move(SSP));
107 case ELF::EM_X86_64:
108 return createLinkGraphFromELFObject_x86_64(ObjectBuffer, std::move(SSP));
109 case ELF::EM_386:
110 return createLinkGraphFromELFObject_x86(ObjectBuffer, std::move(SSP));
111 default:
113 "Unsupported target machine architecture in ELF object " +
114 ObjectBuffer.getBufferIdentifier());
115 }
116}
117
118void link_ELF(std::unique_ptr<LinkGraph> G,
119 std::unique_ptr<JITLinkContext> Ctx) {
120 switch (G->getTargetTriple().getArch()) {
121 case Triple::aarch64:
122 link_ELF_aarch64(std::move(G), std::move(Ctx));
123 return;
124 case Triple::arm:
125 case Triple::armeb:
126 case Triple::thumb:
127 case Triple::thumbeb:
128 link_ELF_aarch32(std::move(G), std::move(Ctx));
129 return;
130 case Triple::hexagon:
131 link_ELF_hexagon(std::move(G), std::move(Ctx));
132 return;
135 link_ELF_loongarch(std::move(G), std::move(Ctx));
136 return;
137 case Triple::ppc64:
138 link_ELF_ppc64(std::move(G), std::move(Ctx));
139 return;
140 case Triple::ppc64le:
141 link_ELF_ppc64le(std::move(G), std::move(Ctx));
142 return;
143 case Triple::riscv32:
144 case Triple::riscv64:
145 link_ELF_riscv(std::move(G), std::move(Ctx));
146 return;
147 case Triple::systemz:
148 link_ELF_systemz(std::move(G), std::move(Ctx));
149 return;
150 case Triple::x86_64:
151 link_ELF_x86_64(std::move(G), std::move(Ctx));
152 return;
153 case Triple::x86:
154 link_ELF_x86(std::move(G), std::move(Ctx));
155 return;
156 default:
157 Ctx->notifyFailed(make_error<JITLinkError>(
158 "Unsupported target machine architecture in ELF link graph " +
159 G->getName()));
160 return;
161 }
162}
163
164} // end namespace jitlink
165} // end namespace llvm
#define G(x, y, z)
Definition MD5.cpp:55
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
StringRef getBufferIdentifier() const
StringRef getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
@ loongarch32
Definition Triple.h:64
@ loongarch64
Definition Triple.h:65
static Expected< ELFFile > create(StringRef Object)
@ EI_DATA
Definition ELF.h:56
@ EI_NIDENT
Definition ELF.h:61
@ EI_CLASS
Definition ELF.h:55
@ EM_S390
Definition ELF.h:155
@ EM_PPC64
Definition ELF.h:154
@ EM_NONE
Definition ELF.h:138
@ EM_386
Definition ELF.h:141
@ EM_LOONGARCH
Definition ELF.h:327
@ EM_X86_64
Definition ELF.h:183
@ EM_HEXAGON
Definition ELF.h:262
@ EM_AARCH64
Definition ELF.h:285
@ EM_RISCV
Definition ELF.h:322
@ EM_ARM
Definition ELF.h:161
static const char ElfMagic[]
Definition ELF.h:47
@ ELFDATA2MSB
Definition ELF.h:341
@ ELFDATA2LSB
Definition ELF.h:340
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASS32
Definition ELF.h:333
This is an optimization pass for GlobalISel generic memory operations.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221