ELF: Merge ELF{Object,DSO}Reader implementations.
The two classes are the same other than a few exceptions. This patch merges them using templates. llvm-svn: 234060
This commit is contained in:
parent
7a0cec399d
commit
25a0d3dd64
@ -18,17 +18,17 @@
|
||||
namespace lld {
|
||||
namespace elf {
|
||||
|
||||
template <typename ELFT, typename ContextT, template <typename> class FileT>
|
||||
class ELFObjectReader : public Reader {
|
||||
template <typename ELFT, typename ContextT, template <typename> class FileT,
|
||||
int FileMagic>
|
||||
class ELFReader : public Reader {
|
||||
public:
|
||||
typedef llvm::object::Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
|
||||
|
||||
ELFObjectReader(ContextT &ctx) : _ctx(ctx) {}
|
||||
ELFReader(ContextT &ctx) : _ctx(ctx) {}
|
||||
|
||||
bool canParse(file_magic magic, StringRef,
|
||||
const MemoryBuffer &buf) const override {
|
||||
return (magic == llvm::sys::fs::file_magic::elf_relocatable &&
|
||||
elfHeader(buf)->e_machine == ContextT::machine);
|
||||
return magic == FileMagic && elfHeader(buf)->e_machine == ContextT::machine;
|
||||
}
|
||||
|
||||
std::error_code
|
||||
@ -45,51 +45,20 @@ public:
|
||||
}
|
||||
|
||||
const Elf_Ehdr *elfHeader(const MemoryBuffer &buf) const {
|
||||
const uint8_t *data =
|
||||
reinterpret_cast<const uint8_t *>(buf.getBuffer().data());
|
||||
return (reinterpret_cast<const Elf_Ehdr *>(data));
|
||||
return reinterpret_cast<const Elf_Ehdr *>(buf.getBuffer().data());
|
||||
}
|
||||
|
||||
protected:
|
||||
ContextT &_ctx;
|
||||
};
|
||||
|
||||
template <typename ELFT, typename ContextT, template <typename> class FileT>
|
||||
using ELFObjectReader = ELFReader<ELFT, ContextT, FileT,
|
||||
llvm::sys::fs::file_magic::elf_relocatable>;
|
||||
|
||||
template <typename ELFT, typename ContextT>
|
||||
class ELFDSOReader : public Reader {
|
||||
public:
|
||||
typedef llvm::object::Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
|
||||
|
||||
ELFDSOReader(ContextT &ctx) : _ctx(ctx) {}
|
||||
|
||||
bool canParse(file_magic magic, StringRef,
|
||||
const MemoryBuffer &buf) const override {
|
||||
return (magic == llvm::sys::fs::file_magic::elf_shared_object &&
|
||||
elfHeader(buf)->e_machine == ContextT::machine);
|
||||
}
|
||||
|
||||
std::error_code
|
||||
loadFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &,
|
||||
std::vector<std::unique_ptr<File>> &result) const override {
|
||||
std::size_t maxAlignment =
|
||||
1ULL << llvm::countTrailingZeros(uintptr_t(mb->getBufferStart()));
|
||||
auto f =
|
||||
createELF<DynamicFile>(llvm::object::getElfArchType(mb->getBuffer()),
|
||||
maxAlignment, std::move(mb), _ctx);
|
||||
if (std::error_code ec = f.getError())
|
||||
return ec;
|
||||
result.push_back(std::move(*f));
|
||||
return std::error_code();
|
||||
}
|
||||
|
||||
const Elf_Ehdr *elfHeader(const MemoryBuffer &buf) const {
|
||||
const uint8_t *data =
|
||||
reinterpret_cast<const uint8_t *>(buf.getBuffer().data());
|
||||
return (reinterpret_cast<const Elf_Ehdr *>(data));
|
||||
}
|
||||
|
||||
protected:
|
||||
ContextT &_ctx;
|
||||
};
|
||||
using ELFDSOReader = ELFReader<ELFT, ContextT, DynamicFile,
|
||||
llvm::sys::fs::file_magic::elf_shared_object>;
|
||||
|
||||
} // namespace elf
|
||||
} // namespace lld
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user