Files
llvm-project/llvm/lib/Object/ELFObjectFile.cpp
T

94 lines
3.3 KiB
C++
Raw Normal View History

2011-01-20 06:38:47 +00:00
//===- 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.
2011-01-20 06:38:47 +00:00
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/MathExtras.h"
2011-01-20 06:38:47 +00:00
namespace llvm {
using namespace object;
2014-06-18 17:07:15 +00:00
static ErrorOr<ObjectFile *> createELFObjectFileAux(MemoryBuffer *Obj,
bool BufferOwned) {
std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj);
std::size_t MaxAlignment =
1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
2014-06-13 02:24:39 +00:00
std::error_code EC;
std::unique_ptr<ObjectFile> R;
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
if (MaxAlignment >= 4)
R.reset(new ELFObjectFile<ELFType<support::little, 4, false> >(
Obj, EC, BufferOwned));
else
#endif
if (MaxAlignment >= 2)
R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(
Obj, EC, BufferOwned));
else
2014-06-16 16:41:00 +00:00
return object_error::parse_failed;
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
if (MaxAlignment >= 4)
R.reset(new ELFObjectFile<ELFType<support::big, 4, false> >(Obj, EC,
BufferOwned));
else
#endif
if (MaxAlignment >= 2)
R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC,
BufferOwned));
else
2014-06-16 16:41:00 +00:00
return object_error::parse_failed;
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
if (MaxAlignment >= 8)
R.reset(new ELFObjectFile<ELFType<support::big, 8, true> >(Obj, EC,
BufferOwned));
else
#endif
if (MaxAlignment >= 2)
R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC,
BufferOwned));
else
2014-06-16 16:41:00 +00:00
return object_error::parse_failed;
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
if (MaxAlignment >= 8)
R.reset(new ELFObjectFile<ELFType<support::little, 8, true> >(
Obj, EC, BufferOwned));
else
#endif
if (MaxAlignment >= 2)
R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(
Obj, EC, BufferOwned));
else
2014-06-16 16:41:00 +00:00
return object_error::parse_failed;
2011-01-20 06:38:47 +00:00
}
else
2014-06-16 16:41:00 +00:00
llvm_unreachable("Buffer is not an ELF object file!");
2011-01-20 06:38:47 +00:00
if (EC)
return EC;
return R.release();
}
2014-06-18 17:07:15 +00:00
ErrorOr<ObjectFile *> ObjectFile::createELFObjectFile(MemoryBuffer *Obj,
bool BufferOwned) {
ErrorOr<ObjectFile *> Ret = createELFObjectFileAux(Obj, BufferOwned);
if (BufferOwned && Ret.getError())
delete Obj;
return Ret;
}
2011-01-20 06:38:47 +00:00
} // end namespace llvm