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
22#include "llvm/Object/ELF.h"
23#include "llvm/Support/Endian.h"
24#include "llvm/Support/Format.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
54 return ELF::EM_NONE;
55}
56
59 StringRef Buffer = ObjectBuffer.getBuffer();
60 if (Buffer.size() < ELF::EI_MAG3 + 1)
61 return make_error<JITLinkError>("Truncated ELF buffer");
62
63 if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
64 return make_error<JITLinkError>("ELF magic not valid");
65
66 Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
67 if (!TargetMachineArch)
68 return TargetMachineArch.takeError();
69
70 switch (*TargetMachineArch) {
71 case ELF::EM_AARCH64:
72 return createLinkGraphFromELFObject_aarch64(ObjectBuffer);
73 case ELF::EM_ARM:
74 return createLinkGraphFromELFObject_aarch32(ObjectBuffer);
76 return createLinkGraphFromELFObject_loongarch(ObjectBuffer);
77 case ELF::EM_RISCV:
78 return createLinkGraphFromELFObject_riscv(ObjectBuffer);
79 case ELF::EM_X86_64:
80 return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
81 case ELF::EM_386:
82 return createLinkGraphFromELFObject_i386(ObjectBuffer);
83 default:
84 return make_error<JITLinkError>(
85 "Unsupported target machine architecture in ELF object " +
86 ObjectBuffer.getBufferIdentifier());
87 }
88}
89
90void link_ELF(std::unique_ptr<LinkGraph> G,
91 std::unique_ptr<JITLinkContext> Ctx) {
92 switch (G->getTargetTriple().getArch()) {
93 case Triple::aarch64:
94 link_ELF_aarch64(std::move(G), std::move(Ctx));
95 return;
96 case Triple::arm:
97 case Triple::armeb:
98 case Triple::thumb:
99 case Triple::thumbeb:
100 link_ELF_aarch32(std::move(G), std::move(Ctx));
101 return;
104 link_ELF_loongarch(std::move(G), std::move(Ctx));
105 return;
106 case Triple::riscv32:
107 case Triple::riscv64:
108 link_ELF_riscv(std::move(G), std::move(Ctx));
109 return;
110 case Triple::x86_64:
111 link_ELF_x86_64(std::move(G), std::move(Ctx));
112 return;
113 case Triple::x86:
114 link_ELF_i386(std::move(G), std::move(Ctx));
115 return;
116 default:
117 Ctx->notifyFailed(make_error<JITLinkError>(
118 "Unsupported target machine architecture in ELF link graph " +
119 G->getName()));
120 return;
121 }
122}
123
124} // end namespace jitlink
125} // 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:775
@ 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
@ EM_ARM
Definition: ELF.h:156
@ 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