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/ELFObjectFile.h"
15 #include "llvm/Support/MathExtras.h"
16
17 namespace llvm {
18 using namespace object;
19
ELFObjectFileBase(unsigned int Type,MemoryBufferRef Source)20 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
21 : ObjectFile(Type, Source) {}
22
23 ErrorOr<std::unique_ptr<ObjectFile>>
createELFObjectFile(MemoryBufferRef Obj)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()));
29
30 std::error_code EC;
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));
36 else
37 #endif
38 if (MaxAlignment >= 2)
39 R.reset(new ELFObjectFile<ELFType<support::little, 2, false>>(Obj, EC));
40 else
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));
46 else
47 #endif
48 if (MaxAlignment >= 2)
49 R.reset(new ELFObjectFile<ELFType<support::big, 2, false>>(Obj, EC));
50 else
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));
56 else
57 #endif
58 if (MaxAlignment >= 2)
59 R.reset(new ELFObjectFile<ELFType<support::big, 2, true>>(Obj, EC));
60 else
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));
66 else
67 #endif
68 if (MaxAlignment >= 2)
69 R.reset(new ELFObjectFile<ELFType<support::little, 2, true>>(Obj, EC));
70 else
71 return object_error::parse_failed;
72 }
73 else
74 llvm_unreachable("Buffer is not an ELF object file!");
75
76 if (EC)
77 return EC;
78 return std::move(R);
79 }
80
81 } // end namespace llvm
82