Replace OwningPtr<T> with std::unique_ptr<T>.
[oota-llvm.git] / lib / Object / ELFObjectFile.cpp
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
20 ErrorOr<ObjectFile *> ObjectFile::createELFObjectFile(MemoryBuffer *Obj,
21                                                       bool BufferOwned) {
22   std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj);
23   std::size_t MaxAlignment =
24     1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
25
26   error_code EC;
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));
33     else
34 #endif
35     if (MaxAlignment >= 2)
36       R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(
37           Obj, EC, BufferOwned));
38     else
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,
44                                                                   BufferOwned));
45     else
46 #endif
47     if (MaxAlignment >= 2)
48       R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC,
49                                                                   BufferOwned));
50     else
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,
56                                                                  BufferOwned));
57     else
58 #endif
59     if (MaxAlignment >= 2)
60       R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC,
61                                                                  BufferOwned));
62     else
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));
69     else
70 #endif
71     if (MaxAlignment >= 2)
72       R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(
73           Obj, EC, BufferOwned));
74     else
75       llvm_unreachable("Invalid alignment for ELF file!");
76   }
77   else
78     report_fatal_error("Buffer is not an ELF object file!");
79
80   if (EC)
81     return EC;
82   return R.release();
83 }
84
85 } // end namespace llvm