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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-02-12 06:12:10 +00:00
|
|
|
// Part of the ELFObjectFile class implementation.
|
2011-01-20 06:38:47 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-08-08 22:27:13 +00:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2013-01-04 20:36:28 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2011-01-20 06:38:47 +00:00
|
|
|
|
2012-02-12 06:12:10 +00:00
|
|
|
namespace llvm {
|
|
|
|
using namespace object;
|
2011-09-08 20:52:17 +00:00
|
|
|
|
2012-02-12 06:12:10 +00:00
|
|
|
// 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;
|
2011-10-11 03:18:58 +00:00
|
|
|
|
2013-01-04 20:36:28 +00:00
|
|
|
std::size_t MaxAlignment =
|
2013-05-24 22:23:49 +00:00
|
|
|
1ULL << countTrailingZeros(uintptr_t(Object->getBufferStart()));
|
2013-01-04 20:36:28 +00:00
|
|
|
|
2012-02-12 06:12:10 +00:00
|
|
|
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
|
2013-02-03 10:48:31 +00:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-04 20:36:28 +00:00
|
|
|
if (MaxAlignment >= 4)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec);
|
2013-02-03 10:48:31 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec);
|
2013-01-04 20:36:28 +00:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2012-02-12 06:12:10 +00:00
|
|
|
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
|
2013-02-03 10:48:31 +00:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-04 20:36:28 +00:00
|
|
|
if (MaxAlignment >= 4)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec);
|
2013-02-03 10:48:31 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec);
|
2013-01-04 20:36:28 +00:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2012-02-12 06:12:10 +00:00
|
|
|
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
|
2013-02-03 10:48:31 +00:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-04 20:36:28 +00:00
|
|
|
if (MaxAlignment >= 8)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec);
|
2013-02-03 10:48:31 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec);
|
2013-01-04 20:36:28 +00:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2012-02-12 06:12:10 +00:00
|
|
|
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
|
2013-02-03 10:48:31 +00:00
|
|
|
#if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
2013-01-04 20:36:28 +00:00
|
|
|
if (MaxAlignment >= 8)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec);
|
2013-02-03 10:48:31 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (MaxAlignment >= 2)
|
2013-01-15 07:44:25 +00:00
|
|
|
return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec);
|
2013-01-04 20:36:28 +00:00
|
|
|
else
|
|
|
|
llvm_unreachable("Invalid alignment for ELF file!");
|
2011-10-11 03:18:58 +00:00
|
|
|
}
|
2011-10-07 19:25:32 +00:00
|
|
|
|
2012-02-12 06:12:10 +00:00
|
|
|
report_fatal_error("Buffer is not an ELF object file!");
|
2011-09-08 20:52:17 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 06:38:47 +00:00
|
|
|
} // end namespace llvm
|