1 //===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Part of the ELFObjectFile class implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/ELFObjectFile.h"
15 #include "llvm/Support/MathExtras.h"
18 using namespace object;
20 ErrorOr<std::unique_ptr<ObjectFile>>
21 ObjectFile::createELFObjectFile(std::unique_ptr<MemoryBuffer> &Obj) {
22 std::pair<unsigned char, unsigned char> Ident =
23 getElfArchType(Obj->getBuffer());
24 std::size_t MaxAlignment =
25 1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
28 std::unique_ptr<ObjectFile> R;
29 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
30 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
31 if (MaxAlignment >= 4)
32 R.reset(new ELFObjectFile<ELFType<support::little, 4, false>>(
36 if (MaxAlignment >= 2)
37 R.reset(new ELFObjectFile<ELFType<support::little, 2, false>>(
40 return object_error::parse_failed;
41 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
42 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
43 if (MaxAlignment >= 4)
44 R.reset(new ELFObjectFile<ELFType<support::big, 4, false>>(std::move(Obj),
48 if (MaxAlignment >= 2)
49 R.reset(new ELFObjectFile<ELFType<support::big, 2, false>>(std::move(Obj),
52 return object_error::parse_failed;
53 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
54 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
55 if (MaxAlignment >= 8)
56 R.reset(new ELFObjectFile<ELFType<support::big, 8, true>>(std::move(Obj),
60 if (MaxAlignment >= 2)
61 R.reset(new ELFObjectFile<ELFType<support::big, 2, true>>(std::move(Obj),
64 return object_error::parse_failed;
65 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
66 #if !LLVM_IS_UNALIGNED_ACCESS_FAST
67 if (MaxAlignment >= 8)
68 R.reset(new ELFObjectFile<ELFType<support::little, 8, true>>(
72 if (MaxAlignment >= 2)
73 R.reset(new ELFObjectFile<ELFType<support::little, 2, true>>(
76 return object_error::parse_failed;
79 llvm_unreachable("Buffer is not an ELF object file!");
86 } // end namespace llvm