ELFObjectFile.cpp revision 261991
155997Simp//===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===//
255997Simp//
355997Simp//                     The LLVM Compiler Infrastructure
455997Simp//
555997Simp// This file is distributed under the University of Illinois Open Source
655997Simp// License. See LICENSE.TXT for details.
755997Simp//
855997Simp//===----------------------------------------------------------------------===//
955997Simp//
1055997Simp// Part of the ELFObjectFile class implementation.
1155997Simp//
1255997Simp//===----------------------------------------------------------------------===//
1355997Simp
1455997Simp#include "llvm/Object/ELFObjectFile.h"
1555997Simp#include "llvm/Support/MathExtras.h"
1655997Simp
1755997Simpnamespace llvm {
1855997Simpusing namespace object;
1955997Simp
2055997Simp// Creates an in-memory object-file by default: createELFObjectFile(Buffer)
2155997SimpObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
2255997Simp  std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
2355997Simp  error_code ec;
2455997Simp
2555997Simp  std::size_t MaxAlignment =
2655997Simp    1ULL << countTrailingZeros(uintptr_t(Object->getBufferStart()));
27119418Sobrien
28119418Sobrien  if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
29119418Sobrien#if !LLVM_IS_UNALIGNED_ACCESS_FAST
3055997Simp    if (MaxAlignment >= 4)
31241591Sjhb      return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec);
3255997Simp    else
33241591Sjhb#endif
3455997Simp    if (MaxAlignment >= 2)
35241591Sjhb      return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec);
3655997Simp    else
3755997Simp      llvm_unreachable("Invalid alignment for ELF file!");
3855997Simp  else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
3955997Simp#if !LLVM_IS_UNALIGNED_ACCESS_FAST
4055997Simp    if (MaxAlignment >= 4)
4155997Simp      return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec);
4255997Simp    else
4370782Simp#endif
4455997Simp    if (MaxAlignment >= 2)
4570782Simp      return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec);
46129764Simp    else
4770782Simp      llvm_unreachable("Invalid alignment for ELF file!");
4855997Simp  else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
4955997Simp#if !LLVM_IS_UNALIGNED_ACCESS_FAST
5055997Simp    if (MaxAlignment >= 8)
5155997Simp      return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec);
5255997Simp    else
5355997Simp#endif
5455997Simp    if (MaxAlignment >= 2)
5570782Simp      return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec);
5670782Simp    else
5770782Simp      llvm_unreachable("Invalid alignment for ELF file!");
5870782Simp  else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
5955997Simp#if !LLVM_IS_UNALIGNED_ACCESS_FAST
60162979Simp    if (MaxAlignment >= 8)
61147580Simp      return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec);
62147580Simp    else
63147580Simp#endif
64147580Simp    if (MaxAlignment >= 2)
65147580Simp      return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec);
6670782Simp    else
6770782Simp      llvm_unreachable("Invalid alignment for ELF file!");
6870782Simp  }
6955997Simp
7055997Simp  report_fatal_error("Buffer is not an ELF object file!");
7155997Simp}
7255997Simp
7355997Simp} // end namespace llvm
7455997Simp