LLVM  16.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"
20 #include "llvm/Object/ELF.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Format.h"
24 #include <cstring>
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "jitlink"
29 
30 namespace llvm {
31 namespace 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 
52  return ELF::EM_NONE;
53 }
54 
57  StringRef Buffer = ObjectBuffer.getBuffer();
58  if (Buffer.size() < ELF::EI_MAG3 + 1)
59  return make_error<JITLinkError>("Truncated ELF buffer");
60 
61  if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
62  return make_error<JITLinkError>("ELF magic not valid");
63 
64  Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
65  if (!TargetMachineArch)
66  return TargetMachineArch.takeError();
67 
68  switch (*TargetMachineArch) {
69  case ELF::EM_AARCH64:
70  return createLinkGraphFromELFObject_aarch64(ObjectBuffer);
71  case ELF::EM_RISCV:
72  return createLinkGraphFromELFObject_riscv(ObjectBuffer);
73  case ELF::EM_X86_64:
74  return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
75  case ELF::EM_386:
76  return createLinkGraphFromELFObject_i386(ObjectBuffer);
77  default:
78  return make_error<JITLinkError>(
79  "Unsupported target machine architecture in ELF object " +
80  ObjectBuffer.getBufferIdentifier());
81  }
82 }
83 
84 void link_ELF(std::unique_ptr<LinkGraph> G,
85  std::unique_ptr<JITLinkContext> Ctx) {
86  switch (G->getTargetTriple().getArch()) {
87  case Triple::aarch64:
89  return;
90  case Triple::riscv32:
91  case Triple::riscv64:
93  return;
94  case Triple::x86_64:
96  return;
97  case Triple::x86:
99  return;
100  default:
101  Ctx->notifyFailed(make_error<JITLinkError>(
102  "Unsupported target machine architecture in ELF link graph " +
103  G->getName()));
104  return;
105  }
106 }
107 
108 } // end namespace jitlink
109 } // end namespace llvm
MemoryBuffer.h
llvm::Triple::riscv64
@ riscv64
Definition: Triple.h:76
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::ELF::EI_DATA
@ EI_DATA
Definition: ELF.h:53
llvm::Triple::x86
@ x86
Definition: Triple.h:85
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:899
llvm::ELF::EM_NONE
@ EM_NONE
Definition: ELF.h:133
llvm::Triple::x86_64
@ x86_64
Definition: Triple.h:86
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
Format.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
ELF.h
llvm::StringRef::data
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
llvm::ELF::EM_RISCV
@ EM_RISCV
Definition: ELF.h:317
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::ELF::EI_CLASS
@ EI_CLASS
Definition: ELF.h:52
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::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:50
llvm::ELF::ELFDATA2LSB
@ ELFDATA2LSB
Definition: ELF.h:335
ELF_riscv.h
ELF_i386.h
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
llvm::Triple::riscv32
@ riscv32
Definition: Triple.h:75
llvm::object::ELFFile::create
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:760
llvm::ELF::ElfMagic
static const char ElfMagic[]
Definition: ELF.h:44
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::ELF::EM_X86_64
@ EM_X86_64
Definition: ELF.h:178
llvm::ELF::ELFCLASS64
@ ELFCLASS64
Definition: ELF.h:329
llvm::MemoryBufferRef::getBufferIdentifier
StringRef getBufferIdentifier() const
Definition: MemoryBufferRef.h:33
llvm::ELF::ELFCLASS32
@ ELFCLASS32
Definition: ELF.h:328
llvm::ELF::EM_AARCH64
@ EM_AARCH64
Definition: ELF.h:280
Endian.h
llvm::ELF::EM_386
@ EM_386
Definition: ELF.h:136
llvm::ELF::EI_MAG3
@ EI_MAG3
Definition: ELF.h:51
llvm::Triple::aarch64
@ aarch64
Definition: Triple.h:51