Be a bit more consistent about using ErrorOr when constructing Binary objects.
[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   std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj);
22   std::size_t MaxAlignment =
23     1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
24
25   error_code EC;
26   OwningPtr<ObjectFile> R;
27   if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
28 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
29     if (MaxAlignment >= 4)
30       R.reset(new ELFObjectFile<ELFType<support::little, 4, false> >(Obj, EC));
31     else
32 #endif
33     if (MaxAlignment >= 2)
34       R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(Obj, EC));
35     else
36       llvm_unreachable("Invalid alignment for ELF file!");
37   else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
38 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
39     if (MaxAlignment >= 4)
40       R.reset(new ELFObjectFile<ELFType<support::big, 4, false> >(Obj, EC));
41     else
42 #endif
43     if (MaxAlignment >= 2)
44       R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC));
45     else
46       llvm_unreachable("Invalid alignment for ELF file!");
47   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
48 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
49     if (MaxAlignment >= 8)
50       R.reset(new ELFObjectFile<ELFType<support::big, 8, true> >(Obj, EC));
51     else
52 #endif
53     if (MaxAlignment >= 2)
54       R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC));
55     else
56       llvm_unreachable("Invalid alignment for ELF file!");
57   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
58 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
59     if (MaxAlignment >= 8)
60       R.reset(new ELFObjectFile<ELFType<support::little, 8, true> >(Obj, EC));
61     else
62 #endif
63     if (MaxAlignment >= 2)
64       R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(Obj, EC));
65     else
66       llvm_unreachable("Invalid alignment for ELF file!");
67   }
68   else
69     report_fatal_error("Buffer is not an ELF object file!");
70
71   if (EC)
72     return EC;
73   return R.take();
74 }
75
76 } // end namespace llvm