LLVM 24.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 if (DataEncoding == ELF::ELFDATA2LSB)
91 return createLinkGraphFromELFObject_aarch64(ObjectBuffer, std::move(SSP));
92 else
94 std::move(SSP));
95 }
96 case ELF::EM_ARM:
97 return createLinkGraphFromELFObject_aarch32(ObjectBuffer, std::move(SSP));
98 case ELF::EM_HEXAGON:
99 return createLinkGraphFromELFObject_hexagon(ObjectBuffer, std::move(SSP));
100 case ELF::EM_PPC64: {
101 if (DataEncoding == ELF::ELFDATA2LSB)
102 return createLinkGraphFromELFObject_ppc64le(ObjectBuffer, std::move(SSP));
103 else
104 return createLinkGraphFromELFObject_ppc64(ObjectBuffer, std::move(SSP));
105 }
107 return createLinkGraphFromELFObject_loongarch(ObjectBuffer, std::move(SSP));
108 case ELF::EM_RISCV:
109 return createLinkGraphFromELFObject_riscv(ObjectBuffer, std::move(SSP));
110 case ELF::EM_S390:
111 return createLinkGraphFromELFObject_systemz(ObjectBuffer, std::move(SSP));
112 case ELF::EM_X86_64:
113 return createLinkGraphFromELFObject_x86_64(ObjectBuffer, std::move(SSP));
114 case ELF::EM_386:
115 return createLinkGraphFromELFObject_x86(ObjectBuffer, std::move(SSP));
116 default:
118 "Unsupported target machine architecture in ELF object " +
119 ObjectBuffer.getBufferIdentifier());
120 }
121}
122
123void link_ELF(std::unique_ptr<LinkGraph> G,
124 std::unique_ptr<JITLinkContext> Ctx) {
125 switch (G->getTargetTriple().getArch()) {
126 case Triple::aarch64:
127 link_ELF_aarch64(std::move(G), std::move(Ctx));
128 return;
130 link_ELF_aarch64_be(std::move(G), std::move(Ctx));
131 return;
132 case Triple::arm:
133 case Triple::armeb:
134 case Triple::thumb:
135 case Triple::thumbeb:
136 link_ELF_aarch32(std::move(G), std::move(Ctx));
137 return;
138 case Triple::hexagon:
139 link_ELF_hexagon(std::move(G), std::move(Ctx));
140 return;
143 link_ELF_loongarch(std::move(G), std::move(Ctx));
144 return;
145 case Triple::ppc64:
146 link_ELF_ppc64(std::move(G), std::move(Ctx));
147 return;
148 case Triple::ppc64le:
149 link_ELF_ppc64le(std::move(G), std::move(Ctx));
150 return;
151 case Triple::riscv32:
152 case Triple::riscv64:
153 link_ELF_riscv(std::move(G), std::move(Ctx));
154 return;
155 case Triple::systemz:
156 link_ELF_systemz(std::move(G), std::move(Ctx));
157 return;
158 case Triple::x86_64:
159 link_ELF_x86_64(std::move(G), std::move(Ctx));
160 return;
161 case Triple::x86:
162 link_ELF_x86(std::move(G), std::move(Ctx));
163 return;
164 default:
165 Ctx->notifyFailed(make_error<JITLinkError>(
166 "Unsupported target machine architecture in ELF link graph " +
167 G->getName()));
168 return;
169 }
170}
171
172} // end namespace jitlink
173} // 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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
@ loongarch32
Definition Triple.h:65
@ loongarch64
Definition Triple.h:66
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