summaryrefslogtreecommitdiff
path: root/lib/ReaderWriter/ELF/FileCommon.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ReaderWriter/ELF/FileCommon.h')
-rw-r--r--lib/ReaderWriter/ELF/FileCommon.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/ReaderWriter/ELF/FileCommon.h b/lib/ReaderWriter/ELF/FileCommon.h
new file mode 100644
index 000000000000..eaff12afe72f
--- /dev/null
+++ b/lib/ReaderWriter/ELF/FileCommon.h
@@ -0,0 +1,45 @@
+//===- lib/ReaderWriter/ELF/FileCommon.h ----------------------------------===//
+//
+// The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_READER_WRITER_ELF_FILE_COMMON_H
+#define LLD_READER_WRITER_ELF_FILE_COMMON_H
+
+#include "lld/ReaderWriter/ELFLinkingContext.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/ELF.h"
+
+namespace lld {
+namespace elf {
+
+template <class ELFT>
+std::error_code checkCompatibility(unsigned char size, unsigned char endian);
+
+template <typename ELFT>
+std::error_code isCompatible(MemoryBufferRef mb, ELFLinkingContext &ctx) {
+ typedef llvm::object::Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
+
+ if (uintptr_t(mb.getBufferStart()) & 1)
+ return make_dynamic_error_code("invalid alignment");
+
+ auto *hdr = reinterpret_cast<const Elf_Ehdr *>(mb.getBuffer().data());
+ if (hdr->e_machine != ctx.getMachineType())
+ return make_dynamic_error_code("incompatible machine type");
+
+ unsigned char size;
+ unsigned char endian;
+ std::tie(size, endian) = llvm::object::getElfArchType(mb.getBuffer());
+ if (std::error_code ec = checkCompatibility<ELFT>(size, endian))
+ return ec;
+ return std::error_code();
+}
+
+} // end namespace elf
+} // end namespace lld
+
+#endif