From 4d9c5397b4a3be747bdb73f1d24c3fdbaaf438fe Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 4 Jan 2013 20:36:28 +0000 Subject: [Object][ELF] Add a maximum alignment. This is used by createELFObjectFile to create a properly aligned reader. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171520 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Object/ELFObjectFile.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'lib/Object') diff --git a/lib/Object/ELFObjectFile.cpp b/lib/Object/ELFObjectFile.cpp index 663b84e..2c8c1b1 100644 --- a/lib/Object/ELFObjectFile.cpp +++ b/lib/Object/ELFObjectFile.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Object/ELF.h" +#include "llvm/Support/MathExtras.h" namespace llvm { @@ -22,16 +23,37 @@ ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { std::pair Ident = getElfArchType(Object); error_code ec; + std::size_t MaxAlignment = + 1ULL << CountTrailingZeros_64(uintptr_t(Object->getBufferStart())); + if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) - return new ELFObjectFile(Object, ec); + if (MaxAlignment >= 4) + return new ELFObjectFile(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) - return new ELFObjectFile(Object, ec); + if (MaxAlignment >= 4) + return new ELFObjectFile(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) - return new ELFObjectFile(Object, ec); + if (MaxAlignment >= 8) + return new ELFObjectFile(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { - ELFObjectFile *result = - new ELFObjectFile(Object, ec); - return result; + if (MaxAlignment >= 8) + return new ELFObjectFile(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); } report_fatal_error("Buffer is not an ELF object file!"); -- cgit v1.1