LLVM  14.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 
15 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Format.h"
23 #include <cstring>
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "jitlink"
28 
29 namespace llvm {
30 namespace jitlink {
31 
33  const char *Data = Buffer.data();
34 
37  if (auto File = llvm::object::ELF64LEFile::create(Buffer)) {
38  return File->getHeader().e_machine;
39  } else {
40  return File.takeError();
41  }
42  } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
43  if (auto File = llvm::object::ELF32LEFile::create(Buffer)) {
44  return File->getHeader().e_machine;
45  } else {
46  return File.takeError();
47  }
48  }
49  }
50 
51  return ELF::EM_NONE;
52 }
53 
56  StringRef Buffer = ObjectBuffer.getBuffer();
57  if (Buffer.size() < ELF::EI_MAG3 + 1)
58  return make_error<JITLinkError>("Truncated ELF buffer");
59 
60  if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
61  return make_error<JITLinkError>("ELF magic not valid");
62 
63  Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
64  if (!TargetMachineArch)
65  return TargetMachineArch.takeError();
66 
67  switch (*TargetMachineArch) {
68  case ELF::EM_AARCH64:
69  return createLinkGraphFromELFObject_aarch64(ObjectBuffer);
70  case ELF::EM_RISCV:
71  return createLinkGraphFromELFObject_riscv(ObjectBuffer);
72  case ELF::EM_X86_64:
73  return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
74  default:
75  return make_error<JITLinkError>(
76  "Unsupported target machine architecture in ELF object " +
77  ObjectBuffer.getBufferIdentifier());
78  }
79 }
80 
81 void link_ELF(std::unique_ptr<LinkGraph> G,
82  std::unique_ptr<JITLinkContext> Ctx) {
83  switch (G->getTargetTriple().getArch()) {
84  case Triple::aarch64:
86  return;
87  case Triple::riscv32:
88  case Triple::riscv64:
90  return;
91  case Triple::x86_64:
93  return;
94  default:
95  Ctx->notifyFailed(make_error<JITLinkError>(
96  "Unsupported target machine architecture in ELF link graph " +
97  G->getName()));
98  return;
99  }
100 }
101 
102 } // end namespace jitlink
103 } // end namespace llvm
MemoryBuffer.h
llvm::Triple::riscv64
@ riscv64
Definition: Triple.h:74
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::ELF::ELFCLASS32
@ ELFCLASS32
Definition: ELF.h:328
llvm::ELF::EM_AARCH64
@ EM_AARCH64
Definition: ELF.h:281
llvm::ELF::EM_RISCV
@ EM_RISCV
Definition: ELF.h:318
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:906
llvm::Triple::x86_64
@ x86_64
Definition: Triple.h:84
llvm::ELF::ELFDATA2LSB
@ ELFDATA2LSB
Definition: ELF.h:335
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Format.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::ELF::EM_NONE
@ EM_NONE
Definition: ELF.h:134
ELF.h
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::ELF::ELFCLASS64
@ ELFCLASS64
Definition: ELF.h:329
ELF.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::ELF::EI_CLASS
@ EI_CLASS
Definition: ELF.h:53
llvm::MemoryBufferRef::getBuffer
StringRef getBuffer() const
Definition: MemoryBufferRef.h:32
ELF_aarch64.h
ELF_x86_64.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
ELF_riscv.h
llvm::Triple::riscv32
@ riscv32
Definition: Triple.h:73
llvm::object::ELFFile::create
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:757
llvm::ELF::ElfMagic
static const char ElfMagic[]
Definition: ELF.h:45
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
llvm::ELF::EI_MAG3
@ EI_MAG3
Definition: ELF.h:52
llvm::ELF::EM_X86_64
@ EM_X86_64
Definition: ELF.h:179
llvm::MemoryBufferRef::getBufferIdentifier
StringRef getBufferIdentifier() const
Definition: MemoryBufferRef.h:33
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
Endian.h
llvm::ELF::EI_DATA
@ EI_DATA
Definition: ELF.h:54
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:205
llvm::Triple::aarch64
@ aarch64
Definition: Triple.h:52