diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2013-01-04 20:36:28 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2013-01-04 20:36:28 +0000 |
commit | 4d9c5397b4a3be747bdb73f1d24c3fdbaaf438fe (patch) | |
tree | 4caf73a9a9994c1aa9d6f82f8cf356b760a268ed /lib/Object | |
parent | e13f441e002b95bb64d883d173f6aff9615556fd (diff) | |
download | external_llvm-4d9c5397b4a3be747bdb73f1d24c3fdbaaf438fe.zip external_llvm-4d9c5397b4a3be747bdb73f1d24c3fdbaaf438fe.tar.gz external_llvm-4d9c5397b4a3be747bdb73f1d24c3fdbaaf438fe.tar.bz2 |
[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
Diffstat (limited to 'lib/Object')
-rw-r--r-- | lib/Object/ELFObjectFile.cpp | 34 |
1 files changed, 28 insertions, 6 deletions
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<unsigned char, unsigned char> 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<support::little, false>(Object, ec); + if (MaxAlignment >= 4) + return new ELFObjectFile<support::little, 4, false>(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile<support::little, 2, false>(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) - return new ELFObjectFile<support::big, false>(Object, ec); + if (MaxAlignment >= 4) + return new ELFObjectFile<support::big, 4, false>(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile<support::big, 2, false>(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) - return new ELFObjectFile<support::big, true>(Object, ec); + if (MaxAlignment >= 8) + return new ELFObjectFile<support::big, 8, true>(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile<support::big, 2, true>(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { - ELFObjectFile<support::little, true> *result = - new ELFObjectFile<support::little, true>(Object, ec); - return result; + if (MaxAlignment >= 8) + return new ELFObjectFile<support::little, 8, true>(Object, ec); + else if (MaxAlignment >= 2) + return new ELFObjectFile<support::little, 2, true>(Object, ec); + else + llvm_unreachable("Invalid alignment for ELF file!"); } report_fatal_error("Buffer is not an ELF object file!"); |