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/ELFObjectFile.h"
15 #include "llvm/Support/MathExtras.h"
18 using namespace object;
20 ErrorOr<ObjectFile *> ObjectFile::createELFObjectFile(MemoryBuffer *Obj,
22 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj);
23 std::size_t MaxAlignment =
24 1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
27 std::unique_ptr<ObjectFile> R;
28 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
29 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
30 if (MaxAlignment >= 4)
31 R.reset(new ELFObjectFile<ELFType<support::little, 4, false> >(
32 Obj, EC, BufferOwned));
35 if (MaxAlignment >= 2)
36 R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(
37 Obj, EC, BufferOwned));
39 llvm_unreachable("Invalid alignment for ELF file!");
40 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
41 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
42 if (MaxAlignment >= 4)
43 R.reset(new ELFObjectFile<ELFType<support::big, 4, false> >(Obj, EC,
47 if (MaxAlignment >= 2)
48 R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC,
51 llvm_unreachable("Invalid alignment for ELF file!");
52 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
53 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
54 if (MaxAlignment >= 8)
55 R.reset(new ELFObjectFile<ELFType<support::big, 8, true> >(Obj, EC,
59 if (MaxAlignment >= 2)
60 R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC,
63 llvm_unreachable("Invalid alignment for ELF file!");
64 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
65 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
66 if (MaxAlignment >= 8)
67 R.reset(new ELFObjectFile<ELFType<support::little, 8, true> >(
68 Obj, EC, BufferOwned));
71 if (MaxAlignment >= 2)
72 R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(
73 Obj, EC, BufferOwned));
75 llvm_unreachable("Invalid alignment for ELF file!");
78 report_fatal_error("Buffer is not an ELF object file!");
85 } // end namespace llvm