1 //===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Part of the ELFObjectFile class implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/ELF.h"
15 #include "llvm/Support/MathExtras.h"
19 using namespace object;
21 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
22 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
23 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
26 std::size_t MaxAlignment =
27 1ULL << CountTrailingZeros_64(uintptr_t(Object->getBufferStart()));
29 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
30 if (MaxAlignment >= 4)
31 return new ELFObjectFile<support::little, 4, false>(Object, ec);
32 else if (MaxAlignment >= 2)
33 return new ELFObjectFile<support::little, 2, false>(Object, ec);
35 llvm_unreachable("Invalid alignment for ELF file!");
36 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
37 if (MaxAlignment >= 4)
38 return new ELFObjectFile<support::big, 4, false>(Object, ec);
39 else if (MaxAlignment >= 2)
40 return new ELFObjectFile<support::big, 2, false>(Object, ec);
42 llvm_unreachable("Invalid alignment for ELF file!");
43 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
44 if (MaxAlignment >= 8)
45 return new ELFObjectFile<support::big, 8, true>(Object, ec);
46 else if (MaxAlignment >= 2)
47 return new ELFObjectFile<support::big, 2, true>(Object, ec);
49 llvm_unreachable("Invalid alignment for ELF file!");
50 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
51 if (MaxAlignment >= 8)
52 return new ELFObjectFile<support::little, 8, true>(Object, ec);
53 else if (MaxAlignment >= 2)
54 return new ELFObjectFile<support::little, 2, true>(Object, ec);
56 llvm_unreachable("Invalid alignment for ELF file!");
59 report_fatal_error("Buffer is not an ELF object file!");
62 } // end namespace llvm