blob: 412c299c1f7dc25c8d52ae635439c7bc5afdbf57 [file] [log] [blame]
Armando Montanez18952b82019-01-03 18:32:36 +00001//===- ELFObjHandler.cpp --------------------------------------------------===//
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#include "ELFObjHandler.h"
11#include "llvm/Object/Binary.h"
12#include "llvm/Object/ELFObjectFile.h"
13#include "llvm/Object/ELFTypes.h"
14#include "llvm/Support/Errc.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/TextAPI/ELF/ELFStub.h"
18
19using llvm::MemoryBufferRef;
20using llvm::object::ELFObjectFile;
21
22using namespace llvm;
23using namespace llvm::object;
24using namespace llvm::elfabi;
25using namespace llvm::ELF;
26
27namespace llvm {
28namespace elfabi {
29
30/// Returns a new ELFStub with all members populated from an ELFObjectFile.
31/// @param ElfObj Source ELFObjectFile.
32template <class ELFT>
33Expected<std::unique_ptr<ELFStub>>
34buildStub(const ELFObjectFile<ELFT> &ElfObj) {
35 std::unique_ptr<ELFStub> DestStub = make_unique<ELFStub>();
36 const ELFFile<ELFT> *ElfFile = ElfObj.getELFFile();
37
38 DestStub->Arch = ElfFile->getHeader()->e_machine;
39
40 // TODO: Populate SoName from .dynamic entries and linked string table.
41 // TODO: Populate NeededLibs from .dynamic entries and linked string table.
42 // TODO: Populate Symbols from .dynsym table and linked string table.
43
44 return std::move(DestStub);
45}
46
47Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf) {
48 Expected<std::unique_ptr<Binary>> BinOrErr = createBinary(Buf);
49 if (!BinOrErr) {
50 return BinOrErr.takeError();
51 }
52
53 Binary *Bin = BinOrErr->get();
54 if (auto Obj = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
55 return buildStub(*Obj);
56 } else if (auto Obj = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
57 return buildStub(*Obj);
58 } else if (auto Obj = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
59 return buildStub(*Obj);
60 } else if (auto Obj = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
61 return buildStub(*Obj);
62 }
63
64 return createStringError(errc::not_supported, "Unsupported binary format");
65}
66
67} // end namespace elfabi
68} // end namespace llvm