2c8c1b16d11c1f6aa461e136aeadb0a175c23646
[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/ELF.h"
15 #include "llvm/Support/MathExtras.h"
16
17 namespace llvm {
18
19 using namespace object;
20
21 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
22 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
23   std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
24   error_code ec;
25
26   std::size_t MaxAlignment =
27     1ULL << CountTrailingZeros_64(uintptr_t(Object->getBufferStart()));
28
29   if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
30     if (MaxAlignment >= 4)
31       return new ELFObjectFile<support::little, 4, false>(Object, ec);
32     else if (MaxAlignment >= 2)
33       return new ELFObjectFile<support::little, 2, false>(Object, ec);
34     else
35       llvm_unreachable("Invalid alignment for ELF file!");
36   else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
37     if (MaxAlignment >= 4)
38       return new ELFObjectFile<support::big, 4, false>(Object, ec);
39     else if (MaxAlignment >= 2)
40       return new ELFObjectFile<support::big, 2, false>(Object, ec);
41     else
42       llvm_unreachable("Invalid alignment for ELF file!");
43   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
44     if (MaxAlignment >= 8)
45       return new ELFObjectFile<support::big, 8, true>(Object, ec);
46     else if (MaxAlignment >= 2)
47       return new ELFObjectFile<support::big, 2, true>(Object, ec);
48     else
49       llvm_unreachable("Invalid alignment for ELF file!");
50   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
51     if (MaxAlignment >= 8)
52       return new ELFObjectFile<support::little, 8, true>(Object, ec);
53     else if (MaxAlignment >= 2)
54       return new ELFObjectFile<support::little, 2, true>(Object, ec);
55     else
56       llvm_unreachable("Invalid alignment for ELF file!");
57   }
58
59   report_fatal_error("Buffer is not an ELF object file!");
60 }
61
62 } // end namespace llvm