2011-01-20 14:38:47 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-02-12 14:12:10 +08:00
|
|
|
// Part of the ELFObjectFile class implementation.
|
2011-01-20 14:38:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-08-09 06:27:13 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2013-01-05 04:36:28 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2011-01-20 14:38:47 +08:00
|
|
|
|
2012-02-12 14:12:10 +08:00
|
|
|
namespace llvm {
|
2014-06-13 05:46:39 +08:00
|
|
|
using std::error_code;
|
2012-02-12 14:12:10 +08:00
|
|
|
using namespace object;
|
2011-09-09 04:52:17 +08:00
|
|
|
|
2014-01-25 05:32:21 +08:00
|
|
|
ErrorOr<ObjectFile *> ObjectFile::createELFObjectFile(MemoryBuffer *Obj,
|
|
|
|
bool BufferOwned) {
|
2014-01-22 07:06:54 +08:00
|
|
|
std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj);
|
2013-01-05 04:36:28 +08:00
|
|
|
std::size_t MaxAlignment =
|
2014-01-22 07:06:54 +08:00
|
|
|
1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart()));
|
2013-01-05 04:36:28 +08:00
|
|
|
|
2014-01-22 07:06:54 +08:00
|
|
|
error_code EC;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<ObjectFile> R;
|
2012-02-12 14:12:10 +08:00
|
|
|
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
|
2013-02-03 18:48:31 +08:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-05 04:36:28 +08:00
|
|
|
if (MaxAlignment >= 4)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::little, 4, false> >(
|
|
|
|
Obj, EC, BufferOwned));
|
2013-02-03 18:48:31 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(
|
|
|
|
Obj, EC, BufferOwned));
|
2013-01-05 04:36:28 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2012-02-12 14:12:10 +08:00
|
|
|
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
|
2013-02-03 18:48:31 +08:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-05 04:36:28 +08:00
|
|
|
if (MaxAlignment >= 4)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::big, 4, false> >(Obj, EC,
|
|
|
|
BufferOwned));
|
2013-02-03 18:48:31 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC,
|
|
|
|
BufferOwned));
|
2013-01-05 04:36:28 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2012-02-12 14:12:10 +08:00
|
|
|
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
|
2013-02-03 18:48:31 +08:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-05 04:36:28 +08:00
|
|
|
if (MaxAlignment >= 8)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::big, 8, true> >(Obj, EC,
|
|
|
|
BufferOwned));
|
2013-02-03 18:48:31 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC,
|
|
|
|
BufferOwned));
|
2013-01-05 04:36:28 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2012-02-12 14:12:10 +08:00
|
|
|
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
|
2013-02-03 18:48:31 +08:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-05 04:36:28 +08:00
|
|
|
if (MaxAlignment >= 8)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::little, 8, true> >(
|
|
|
|
Obj, EC, BufferOwned));
|
2013-02-03 18:48:31 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2014-01-25 05:32:21 +08:00
|
|
|
R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(
|
|
|
|
Obj, EC, BufferOwned));
|
2013-01-05 04:36:28 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2011-10-11 11:18:58 +08:00
|
|
|
}
|
2014-01-22 07:06:54 +08:00
|
|
|
else
|
|
|
|
report_fatal_error("Buffer is not an ELF object file!");
|
2011-10-08 03:25:32 +08:00
|
|
|
|
2014-01-22 07:06:54 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2014-03-05 18:19:29 +08:00
|
|
|
return R.release();
|
2011-09-09 04:52:17 +08:00
|
|
|
}
|
|
|
|
|
2011-01-20 14:38:47 +08:00
|
|
|
} // end namespace llvm
|