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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-02-12 14:12:10 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
2011-01-20 14:38:47 +08:00
|
|
|
|
2012-02-12 14:12:10 +08:00
|
|
|
namespace llvm {
|
2011-09-09 04:52:17 +08:00
|
|
|
|
2012-02-12 14:12:10 +08:00
|
|
|
using namespace object;
|
2011-09-09 04:52:17 +08:00
|
|
|
|
2011-01-20 14:38:47 +08:00
|
|
|
namespace {
|
2012-02-12 14:12:10 +08:00
|
|
|
std::pair<unsigned char, unsigned char>
|
|
|
|
getElfArchType(MemoryBuffer *Object) {
|
|
|
|
if (Object->getBufferSize() < ELF::EI_NIDENT)
|
|
|
|
return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
|
|
|
|
return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
|
|
|
|
, (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
|
2011-10-08 03:25:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-12 14:12:10 +08: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 11:18:58 +08:00
|
|
|
|
2012-02-12 14:12:10 +08:00
|
|
|
if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
|
|
|
|
return new ELFObjectFile<support::little, false>(Object, ec);
|
|
|
|
else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
|
|
|
|
return new ELFObjectFile<support::big, false>(Object, ec);
|
|
|
|
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
|
|
|
|
return new ELFObjectFile<support::big, true>(Object, ec);
|
|
|
|
else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
|
|
|
|
ELFObjectFile<support::little, true> *result =
|
|
|
|
new ELFObjectFile<support::little, true>(Object, ec);
|
|
|
|
return result;
|
2011-10-11 11:18:58 +08:00
|
|
|
}
|
2011-10-08 03:25:32 +08:00
|
|
|
|
2012-02-12 14:12:10 +08:00
|
|
|
report_fatal_error("Buffer is not an ELF object file!");
|
2011-09-09 04:52:17 +08:00
|
|
|
}
|
|
|
|
|
2011-01-20 14:38:47 +08:00
|
|
|
} // end namespace llvm
|