diff options
Diffstat (limited to 'lld/lib/ReaderWriter/ELF/ELFReader.h')
| -rw-r--r-- | lld/lib/ReaderWriter/ELF/ELFReader.h | 47 |
1 files changed, 6 insertions, 41 deletions
diff --git a/lld/lib/ReaderWriter/ELF/ELFReader.h b/lld/lib/ReaderWriter/ELF/ELFReader.h index 09e90797481..723109f1dc6 100644 --- a/lld/lib/ReaderWriter/ELF/ELFReader.h +++ b/lld/lib/ReaderWriter/ELF/ELFReader.h @@ -20,60 +20,25 @@ namespace lld { namespace elf { -template <typename ELFT, typename ContextT, template <typename> class FileT> -class ELFReader : public Reader { +template <typename FileT> class ELFReader : public Reader { public: - typedef llvm::object::Elf_Ehdr_Impl<ELFT> Elf_Ehdr; - - ELFReader(ContextT &ctx) : _ctx(ctx) {} + ELFReader(ELFLinkingContext &ctx) : _ctx(ctx) {} bool canParse(file_magic magic, const MemoryBuffer &mb) const override { - return (FileT<ELFT>::canParse(magic) && - elfHeader(mb)->e_machine == ContextT::machine); + return FileT::canParse(magic); } std::error_code loadFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &, std::vector<std::unique_ptr<File>> &result) const override { - const Elf_Ehdr *hdr = elfHeader(*mb); - if (auto ec = _ctx.mergeHeaderFlags(hdr->getFileClass(), hdr->e_flags)) + if (std::error_code ec = FileT::isCompatible(*mb, _ctx)) return ec; - result.push_back(createELF(std::move(mb))); + result.push_back(llvm::make_unique<FileT>(std::move(mb), _ctx)); return std::error_code(); } private: - /// Create an object depending on the runtime attributes and alignment - /// of an ELF file. - std::unique_ptr<File> createELF(std::unique_ptr<MemoryBuffer> mb) const { - using namespace llvm::ELF; - using namespace llvm::support; - - if (uintptr_t(mb->getBufferStart()) & 1) - llvm_unreachable("Invalid alignment for ELF file!"); - unsigned char size; - unsigned char endian; - std::tie(size, endian) = llvm::object::getElfArchType(mb->getBuffer()); - File *file = nullptr; - if (size == ELFCLASS32 && endian == ELFDATA2LSB) { - file = new FileT<ELF32LE>(std::move(mb), _ctx); - } else if (size == ELFCLASS32 && endian == ELFDATA2MSB) { - file = new FileT<ELF32BE>(std::move(mb), _ctx); - } else if (size == ELFCLASS64 && endian == ELFDATA2LSB) { - file = new FileT<ELF64LE>(std::move(mb), _ctx); - } else if (size == ELFCLASS64 && endian == ELFDATA2MSB) { - file = new FileT<ELF64BE>(std::move(mb), _ctx); - } - if (!file) - llvm_unreachable("Invalid ELF type!"); - return std::unique_ptr<File>(file); - } - - static const Elf_Ehdr *elfHeader(const MemoryBuffer &buf) { - return reinterpret_cast<const Elf_Ehdr *>(buf.getBuffer().data()); - } - - ContextT &_ctx; + ELFLinkingContext &_ctx; }; } // namespace elf |

