Armando Montanez | 18952b8 | 2019-01-03 18:32:36 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 19 | using llvm::MemoryBufferRef; |
| 20 | using llvm::object::ELFObjectFile; |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace llvm::object; |
| 24 | using namespace llvm::elfabi; |
| 25 | using namespace llvm::ELF; |
| 26 | |
| 27 | namespace llvm { |
| 28 | namespace elfabi { |
| 29 | |
| 30 | /// Returns a new ELFStub with all members populated from an ELFObjectFile. |
| 31 | /// @param ElfObj Source ELFObjectFile. |
| 32 | template <class ELFT> |
| 33 | Expected<std::unique_ptr<ELFStub>> |
| 34 | buildStub(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 | |
| 47 | Expected<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 |