llvm-6502/lib/Object/ELFObjectFile.cpp
Michael J. Spencer ac97f5ce48 [Object][ELF] Simplify ELFObjectFile by using ELFType.
This simplifies the usage and implementation of ELFObjectFile by using ELFType
to replace:

<endianness target_endianness, std::size_t max_alignment, bool is64Bits>

This does complicate the base ELF types as they must now use template template
parameters to partially specialize for the 32 and 64bit cases. However these
are only defined once.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172515 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-15 07:44:25 +00:00

63 lines
2.4 KiB
C++

//===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Part of the ELFObjectFile class implementation.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/ELF.h"
#include "llvm/Support/MathExtras.h"
namespace llvm {
using namespace object;
// Creates an in-memory object-file by default: createELFObjectFile(Buffer)
ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
error_code ec;
std::size_t MaxAlignment =
1ULL << CountTrailingZeros_64(uintptr_t(Object->getBufferStart()));
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
if (MaxAlignment >= 4)
return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec);
else if (MaxAlignment >= 2)
return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec);
else
llvm_unreachable("Invalid alignment for ELF file!");
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
if (MaxAlignment >= 4)
return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec);
else if (MaxAlignment >= 2)
return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec);
else
llvm_unreachable("Invalid alignment for ELF file!");
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
if (MaxAlignment >= 8)
return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec);
else if (MaxAlignment >= 2)
return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec);
else
llvm_unreachable("Invalid alignment for ELF file!");
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
if (MaxAlignment >= 8)
return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec);
else if (MaxAlignment >= 2)
return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec);
else
llvm_unreachable("Invalid alignment for ELF file!");
}
report_fatal_error("Buffer is not an ELF object file!");
}
} // end namespace llvm