LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ELFObjectFile.cpp
Go to the documentation of this file.
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 
16 
17 namespace llvm {
18 using namespace object;
19 
20 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
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
static ObjectFile * createELFObjectFile(MemoryBuffer *Object)
const char * getBufferStart() const
Definition: MemoryBuffer.h:51
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
#define llvm_unreachable(msg)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:49
std::pair< unsigned char, unsigned char > getElfArchType(MemoryBuffer *Object)
Definition: Object/ELF.h:43