2011-02-20 07:57:14 -05: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-04-14 09:54:10 -04:00
|
|
|
// Part of the ELFObjectFile class implementation.
|
2011-02-20 07:57:14 -05:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2013-12-21 19:04:03 -05:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2013-04-08 14:41:23 -04:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2011-02-20 07:57:14 -05:00
|
|
|
|
2012-04-14 09:54:10 -04:00
|
|
|
namespace llvm {
|
|
|
|
|
using namespace object;
|
2011-10-20 17:10:27 -04:00
|
|
|
|
2015-01-18 11:17:27 -05:00
|
|
|
ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
|
|
|
|
|
: ObjectFile(Type, Source) {}
|
|
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>>
|
|
|
|
|
ObjectFile::createELFObjectFile(MemoryBufferRef Obj) {
|
2014-11-24 04:08:18 -05:00
|
|
|
std::pair<unsigned char, unsigned char> Ident =
|
2015-01-18 11:17:27 -05:00
|
|
|
getElfArchType(Obj.getBuffer());
|
2013-04-08 14:41:23 -04:00
|
|
|
std::size_t MaxAlignment =
|
2015-01-18 11:17:27 -05:00
|
|
|
1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
|
2013-04-08 14:41:23 -04:00
|
|
|
|
2015-06-09 15:06:30 -04:00
|
|
|
if (MaxAlignment < 2)
|
|
|
|
|
return object_error::parse_failed;
|
|
|
|
|
|
2014-11-24 04:08:18 -05:00
|
|
|
std::error_code EC;
|
|
|
|
|
std::unique_ptr<ObjectFile> R;
|
2015-06-09 15:06:30 -04: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));
|
2013-04-08 14:41:23 -04:00
|
|
|
else
|
2014-11-24 04:08:18 -05:00
|
|
|
return object_error::parse_failed;
|
2015-06-09 15:06:30 -04:00
|
|
|
} else if (Ident.first == ELF::ELFCLASS64) {
|
|
|
|
|
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));
|
2013-04-08 14:41:23 -04:00
|
|
|
else
|
2014-11-24 04:08:18 -05:00
|
|
|
return object_error::parse_failed;
|
2015-06-09 15:06:30 -04:00
|
|
|
} else {
|
|
|
|
|
return object_error::parse_failed;
|
2011-02-20 07:57:14 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-24 04:08:18 -05:00
|
|
|
if (EC)
|
|
|
|
|
return EC;
|
2015-01-18 11:17:27 -05:00
|
|
|
return std::move(R);
|
2011-10-20 17:10:27 -04:00
|
|
|
}
|
|
|
|
|
|
2011-02-20 07:57:14 -05:00
|
|
|
} // end namespace llvm
|