1 //===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Part of the ELFObjectFile class implementation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Object/ELF.h" 15 #include "llvm/Support/MathExtras.h" 16 17 #include <ctype.h> 18 19 namespace llvm { 20 21 using namespace object; 22 23 // Creates an in-memory object-file by default: createELFObjectFile(Buffer) 24 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { 25 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); 26 error_code ec; 27 28 std::size_t MaxAlignment = 29 1ULL << countTrailingZeros(uintptr_t(Object->getBufferStart())); 30 31 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) 32 #if !LLVM_IS_UNALIGNED_ACCESS_FAST 33 if (MaxAlignment >= 4) 34 return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec); 35 else 36 #endif 37 if (MaxAlignment >= 2) 38 return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec); 39 else 40 llvm_unreachable("Invalid alignment for ELF file!"); 41 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) 42 #if !LLVM_IS_UNALIGNED_ACCESS_FAST 43 if (MaxAlignment >= 4) 44 return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec); 45 else 46 #endif 47 if (MaxAlignment >= 2) 48 return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec); 49 else 50 llvm_unreachable("Invalid alignment for ELF file!"); 51 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) 52 #if !LLVM_IS_UNALIGNED_ACCESS_FAST 53 if (MaxAlignment >= 8) 54 return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec); 55 else 56 #endif 57 if (MaxAlignment >= 2) 58 return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec); 59 else 60 llvm_unreachable("Invalid alignment for ELF file!"); 61 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { 62 #if !LLVM_IS_UNALIGNED_ACCESS_FAST 63 if (MaxAlignment >= 8) 64 return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec); 65 else 66 #endif 67 if (MaxAlignment >= 2) 68 return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec); 69 else 70 llvm_unreachable("Invalid alignment for ELF file!"); 71 } 72 73 report_fatal_error("Buffer is not an ELF object file!"); 74 } 75 76 } // end namespace llvm 77