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 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
21 : ObjectFile(Type, Source) {}
23 ErrorOr<std::unique_ptr<ObjectFile>>
24 ObjectFile::createELFObjectFile(MemoryBufferRef Obj) {
25 std::pair<unsigned char, unsigned char> Ident =
26 getElfArchType(Obj.getBuffer());
27 std::size_t MaxAlignment =
28 1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
31 std::unique_ptr<ObjectFile> R;
32 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
33 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
34 if (MaxAlignment >= 4)
35 R.reset(new ELFObjectFile<ELFType<support::little, 4, false>>(Obj, EC));
38 if (MaxAlignment >= 2)
39 R.reset(new ELFObjectFile<ELFType<support::little, 2, false>>(Obj, EC));
41 return object_error::parse_failed;
42 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
43 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
44 if (MaxAlignment >= 4)
45 R.reset(new ELFObjectFile<ELFType<support::big, 4, false>>(Obj, EC));
48 if (MaxAlignment >= 2)
49 R.reset(new ELFObjectFile<ELFType<support::big, 2, false>>(Obj, EC));
51 return object_error::parse_failed;
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));
58 if (MaxAlignment >= 2)
59 R.reset(new ELFObjectFile<ELFType<support::big, 2, true>>(Obj, EC));
61 return object_error::parse_failed;
62 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
63 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
64 if (MaxAlignment >= 8)
65 R.reset(new ELFObjectFile<ELFType<support::little, 8, true>>(Obj, EC));
68 if (MaxAlignment >= 2)
69 R.reset(new ELFObjectFile<ELFType<support::little, 2, true>>(Obj, EC));
71 return object_error::parse_failed;
74 llvm_unreachable("Buffer is not an ELF object file!");
81 } // end namespace llvm