LLVM  13.0.0git
ELF.cpp
Go to the documentation of this file.
1 //===-------------- ELF.cpp - JIT linker function for ELF -------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // ELF jit-link function.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
16 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/Object/ELF.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/Format.h"
22 #include <cstring>
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "jitlink"
27 
28 namespace llvm {
29 namespace jitlink {
30 
32  const char *Data = Buffer.data();
33 
36  if (auto File = llvm::object::ELF64LEFile::create(Buffer)) {
37  return File->getHeader().e_machine;
38  } else {
39  return File.takeError();
40  }
41  } else if (Data[ELF::EI_CLASS] == ELF::ELFCLASS32) {
42  if (auto File = llvm::object::ELF32LEFile::create(Buffer)) {
43  return File->getHeader().e_machine;
44  } else {
45  return File.takeError();
46  }
47  }
48  }
49 
50  return ELF::EM_NONE;
51 }
52 
55  StringRef Buffer = ObjectBuffer.getBuffer();
56  if (Buffer.size() < ELF::EI_MAG3 + 1)
57  return make_error<JITLinkError>("Truncated ELF buffer");
58 
59  if (memcmp(Buffer.data(), ELF::ElfMagic, strlen(ELF::ElfMagic)) != 0)
60  return make_error<JITLinkError>("ELF magic not valid");
61 
62  Expected<uint16_t> TargetMachineArch = readTargetMachineArch(Buffer);
63  if (!TargetMachineArch)
64  return TargetMachineArch.takeError();
65 
66  switch (*TargetMachineArch) {
67  case ELF::EM_X86_64:
68  return createLinkGraphFromELFObject_x86_64(ObjectBuffer);
69  default:
70  return make_error<JITLinkError>(
71  "Unsupported target machine architecture in ELF object " +
72  ObjectBuffer.getBufferIdentifier());
73  }
74 }
75 
76 void link_ELF(std::unique_ptr<LinkGraph> G,
77  std::unique_ptr<JITLinkContext> Ctx) {
78  switch (G->getTargetTriple().getArch()) {
79  case Triple::x86_64:
81  return;
82  default:
83  Ctx->notifyFailed(make_error<JITLinkError>(
84  "Unsupported target machine architecture in ELF link graph " +
85  G->getName()));
86  return;
87  }
88 }
89 
90 } // end namespace jitlink
91 } // end namespace llvm
MemoryBuffer.h
llvm
Definition: AllocatorList.h:23
llvm::ELF::ELFCLASS32
@ ELFCLASS32
Definition: ELF.h:326
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:927
llvm::Triple::x86_64
@ x86_64
Definition: Triple.h:84
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::ELF::ELFDATA2LSB
@ ELFDATA2LSB
Definition: ELF.h:333
Format.h
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::ELF::ELFCLASS64
@ ELFCLASS64
Definition: ELF.h:327
ELF.h
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
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::EM_X86_64
@ EM_X86_64
Definition: ELF.h:177
llvm::MemoryBufferRef::getBuffer
StringRef getBuffer() const
Definition: MemoryBufferRef.h:32
llvm::ELF::EI_MAG3
@ EI_MAG3
Definition: ELF.h:50
ELF_x86_64.h
llvm::ELF::EI_CLASS
@ EI_CLASS
Definition: ELF.h:51
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ELFFile::create
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:757
llvm::ELF::ElfMagic
static const char ElfMagic[]
Definition: ELF.h:43
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
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::ELF::EI_DATA
@ EI_DATA
Definition: ELF.h:52
llvm::ELF::EM_NONE
@ EM_NONE
Definition: ELF.h:132
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
Endian.h
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:206