]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/lib/Object/ELFObjectFile.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / 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 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
21 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
22   std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
23   error_code ec;
24
25   std::size_t MaxAlignment =
26     1ULL << countTrailingZeros(uintptr_t(Object->getBufferStart()));
27
28   if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
29 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
30     if (MaxAlignment >= 4)
31       return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec);
32     else
33 #endif
34     if (MaxAlignment >= 2)
35       return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec);
36     else
37       llvm_unreachable("Invalid alignment for ELF file!");
38   else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
39 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
40     if (MaxAlignment >= 4)
41       return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec);
42     else
43 #endif
44     if (MaxAlignment >= 2)
45       return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec);
46     else
47       llvm_unreachable("Invalid alignment for ELF file!");
48   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
49 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
50     if (MaxAlignment >= 8)
51       return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec);
52     else
53 #endif
54     if (MaxAlignment >= 2)
55       return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec);
56     else
57       llvm_unreachable("Invalid alignment for ELF file!");
58   else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
59 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
60     if (MaxAlignment >= 8)
61       return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec);
62     else
63 #endif
64     if (MaxAlignment >= 2)
65       return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec);
66     else
67       llvm_unreachable("Invalid alignment for ELF file!");
68   }
69
70   report_fatal_error("Buffer is not an ELF object file!");
71 }
72
73 } // end namespace llvm