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 {
|
|
|
|
using namespace object;
|
2011-09-09 04:52:17 +08:00
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
|
|
|
|
: ObjectFile(Type, Source) {}
|
2014-08-18 01:52:10 +08:00
|
|
|
|
2014-07-31 11:12:45 +08:00
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>>
|
2014-08-20 02:44:46 +08:00
|
|
|
ObjectFile::createELFObjectFile(MemoryBufferRef Obj) {
|
2014-07-05 19:38:52 +08:00
|
|
|
std::pair<unsigned char, unsigned char> Ident =
|
2014-08-20 02:44:46 +08:00
|
|
|
getElfArchType(Obj.getBuffer());
|
2013-01-05 04:36:28 +08:00
|
|
|
std::size_t MaxAlignment =
|
2014-08-20 02:44:46 +08:00
|
|
|
1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
|
2013-01-05 04:36:28 +08:00
|
|
|
|
2015-06-02 20:05:27 +08:00
|
|
|
if (MaxAlignment < 2)
|
|
|
|
return object_error::parse_failed;
|
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC;
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<ObjectFile> R;
|
2015-06-02 20:05:27 +08:00
|
|
|
if (Ident.first == ELF::ELFCLASS32) {
|
|
|
|
if (Ident.second == ELF::ELFDATA2LSB)
|
|
|
|
R.reset(new ELFObjectFile<ELFType<support::little, false>>(Obj, EC));
|
|
|
|
else if (Ident.second == ELF::ELFDATA2MSB)
|
|
|
|
R.reset(new ELFObjectFile<ELFType<support::big, false>>(Obj, EC));
|
|
|
|
else
|
2015-06-05 06:58:25 +08:00
|
|
|
return object_error::parse_failed;
|
2015-06-05 07:14:43 +08:00
|
|
|
} else if (Ident.first == ELF::ELFCLASS64) {
|
2015-06-02 20:05:27 +08:00
|
|
|
if (Ident.second == ELF::ELFDATA2LSB)
|
|
|
|
R.reset(new ELFObjectFile<ELFType<support::little, true>>(Obj, EC));
|
|
|
|
else if (Ident.second == ELF::ELFDATA2MSB)
|
|
|
|
R.reset(new ELFObjectFile<ELFType<support::big, true>>(Obj, EC));
|
|
|
|
else
|
2015-06-05 06:58:25 +08:00
|
|
|
return object_error::parse_failed;
|
2015-06-05 07:14:43 +08:00
|
|
|
} else {
|
|
|
|
return object_error::parse_failed;
|
2011-10-11 11:18:58 +08:00
|
|
|
}
|
2011-10-08 03:25:32 +08:00
|
|
|
|
2014-01-22 07:06:54 +08:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2014-07-31 11:12:45 +08:00
|
|
|
return std::move(R);
|
2011-09-09 04:52:17 +08:00
|
|
|
}
|
|
|
|
|
2011-01-20 14:38:47 +08:00
|
|
|
} // end namespace llvm
|