2010-07-14 07:07:23 +08:00
|
|
|
//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ObjectFileELF.h"
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
#include <cassert>
|
2010-06-09 00:52:24 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2011-01-15 08:08:44 +08:00
|
|
|
#include "lldb/Core/ArchSpec.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/DataBuffer.h"
|
|
|
|
#include "lldb/Core/Error.h"
|
2010-07-14 07:07:23 +08:00
|
|
|
#include "lldb/Core/FileSpecList.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/PluginManager.h"
|
|
|
|
#include "lldb/Core/Section.h"
|
|
|
|
#include "lldb/Core/Stream.h"
|
2011-02-23 08:35:02 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
#define CASE_AND_STREAM(s, def, width) \
|
|
|
|
case def: s->Printf("%-*s", width, #def); break;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
2010-07-14 07:07:23 +08:00
|
|
|
using namespace elf;
|
|
|
|
using namespace llvm::ELF;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Static methods.
|
|
|
|
//------------------------------------------------------------------
|
2010-06-09 00:52:24 +08:00
|
|
|
void
|
|
|
|
ObjectFileELF::Initialize()
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
PluginManager::RegisterPlugin(GetPluginNameStatic(),
|
|
|
|
GetPluginDescriptionStatic(),
|
|
|
|
CreateInstance);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectFileELF::Terminate()
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
PluginManager::UnregisterPlugin(CreateInstance);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ObjectFileELF::GetPluginNameStatic()
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
return "object-file.elf";
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ObjectFileELF::GetPluginDescriptionStatic()
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
return "ELF object file reader.";
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFile *
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::CreateInstance(Module *module,
|
|
|
|
DataBufferSP &data_sp,
|
|
|
|
const FileSpec *file, addr_t offset,
|
|
|
|
addr_t length)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
const uint8_t *magic = data_sp->GetBytes() + offset;
|
|
|
|
if (ELFHeader::MagicBytesMatch(magic))
|
|
|
|
{
|
|
|
|
unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
|
|
|
|
if (address_size == 4 || address_size == 8)
|
|
|
|
{
|
2011-01-15 08:08:44 +08:00
|
|
|
std::auto_ptr<ObjectFileELF> objfile_ap(
|
2010-07-14 07:07:23 +08:00
|
|
|
new ObjectFileELF(module, data_sp, file, offset, length));
|
2011-01-15 08:08:44 +08:00
|
|
|
ArchSpec spec = objfile_ap->GetArchitecture();
|
|
|
|
if (spec.IsValid() && objfile_ap->SetModulesArchitecture(spec))
|
2010-07-14 07:07:23 +08:00
|
|
|
return objfile_ap.release();
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-15 08:08:44 +08:00
|
|
|
ArchSpec
|
|
|
|
ObjectFileELF::GetArchitecture()
|
|
|
|
{
|
|
|
|
if (!ParseHeader())
|
|
|
|
return ArchSpec();
|
|
|
|
|
|
|
|
return ArchSpec(eArchTypeELF, m_header.e_machine, m_header.e_flags);
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// PluginInterface protocol
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
const char *
|
|
|
|
ObjectFileELF::GetPluginName()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
return "ObjectFileELF";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
ObjectFileELF::GetShortPluginName()
|
|
|
|
{
|
|
|
|
return GetPluginNameStatic();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
ObjectFileELF::GetPluginVersion()
|
|
|
|
{
|
|
|
|
return m_plugin_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectFileELF::GetPluginCommandHelp(const char *command, Stream *strm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Error
|
|
|
|
ObjectFileELF::ExecutePluginCommand(Args &command, Stream *strm)
|
|
|
|
{
|
|
|
|
Error error;
|
|
|
|
error.SetErrorString("No plug-in commands are currently supported.");
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log *
|
|
|
|
ObjectFileELF::EnablePluginLogging(Stream *strm, Args &command)
|
|
|
|
{
|
|
|
|
return NULL;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// ObjectFile protocol
|
|
|
|
//------------------------------------------------------------------
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
|
|
|
|
const FileSpec* file, addr_t offset,
|
|
|
|
addr_t length)
|
|
|
|
: ObjectFile(module, file, offset, length, dataSP),
|
|
|
|
m_header(),
|
|
|
|
m_program_headers(),
|
|
|
|
m_section_headers(),
|
|
|
|
m_sections_ap(),
|
|
|
|
m_symtab_ap(),
|
|
|
|
m_filespec_ap(),
|
|
|
|
m_shstr_data()
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (file)
|
|
|
|
m_file = *file;
|
2010-07-14 07:07:23 +08:00
|
|
|
::memset(&m_header, 0, sizeof(m_header));
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFileELF::~ObjectFileELF()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-10 07:31:02 +08:00
|
|
|
bool
|
|
|
|
ObjectFileELF::IsExecutable() const
|
|
|
|
{
|
2011-01-15 08:09:50 +08:00
|
|
|
return m_header.e_entry != 0;
|
2010-08-10 07:31:02 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 08:08:44 +08:00
|
|
|
Address
|
|
|
|
ObjectFileELF::GetEntryPoint() const
|
|
|
|
{
|
|
|
|
if (m_header.e_entry)
|
|
|
|
return Address(NULL, m_header.e_entry);
|
|
|
|
else
|
|
|
|
return Address();
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
ByteOrder
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::GetByteOrder() const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
|
|
|
|
return eByteOrderBig;
|
|
|
|
if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
|
|
|
|
return eByteOrderLittle;
|
|
|
|
return eByteOrderInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::GetAddressByteSize() const
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
return m_data.GetAddressByteSize();
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
unsigned
|
|
|
|
ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
return std::distance(m_section_headers.begin(), I) + 1;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
unsigned
|
|
|
|
ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
|
|
|
|
{
|
|
|
|
return std::distance(m_section_headers.begin(), I) + 1;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
bool
|
|
|
|
ObjectFileELF::ParseHeader()
|
|
|
|
{
|
|
|
|
uint32_t offset = GetOffset();
|
|
|
|
return m_header.Parse(m_data, &offset);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-02-05 02:53:10 +08:00
|
|
|
ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
// FIXME: Return MD5 sum here. See comment in ObjectFile.h.
|
2010-06-09 00:52:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::GetDependentModules(FileSpecList &files)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
size_t num_modules = ParseDependentModules();
|
|
|
|
uint32_t num_specs = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_modules; ++i)
|
|
|
|
{
|
|
|
|
if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
|
|
|
|
num_specs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_specs;
|
|
|
|
}
|
|
|
|
|
2011-01-15 08:08:44 +08:00
|
|
|
Address
|
|
|
|
ObjectFileELF::GetImageInfoAddress()
|
|
|
|
{
|
|
|
|
if (!ParseSectionHeaders())
|
|
|
|
return Address();
|
|
|
|
|
|
|
|
user_id_t dynsym_id = 0;
|
|
|
|
for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
|
|
|
|
sh_pos != m_section_headers.end(); ++sh_pos)
|
|
|
|
{
|
|
|
|
if (sh_pos->sh_type == SHT_DYNAMIC)
|
|
|
|
{
|
|
|
|
dynsym_id = SectionIndex(sh_pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dynsym_id)
|
|
|
|
return Address();
|
|
|
|
|
|
|
|
SectionList *section_list = GetSectionList();
|
|
|
|
if (!section_list)
|
|
|
|
return Address();
|
|
|
|
|
|
|
|
// Resolve the dynamic table entries.
|
|
|
|
Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
|
|
|
|
if (!dynsym)
|
|
|
|
return Address();
|
|
|
|
|
|
|
|
DataExtractor dynsym_data;
|
|
|
|
if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data))
|
|
|
|
{
|
|
|
|
ELFDynamic symbol;
|
|
|
|
const unsigned section_size = dynsym_data.GetByteSize();
|
|
|
|
unsigned offset = 0;
|
|
|
|
unsigned cursor = 0;
|
|
|
|
|
|
|
|
// Look for a DT_DEBUG entry.
|
|
|
|
while (cursor < section_size)
|
|
|
|
{
|
|
|
|
offset = cursor;
|
|
|
|
if (!symbol.Parse(dynsym_data, &cursor))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (symbol.d_tag != DT_DEBUG)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return Address(dynsym, offset + sizeof(symbol.d_tag));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Address();
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// ParseDependentModules
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
ObjectFileELF::ParseDependentModules()
|
|
|
|
{
|
|
|
|
if (m_filespec_ap.get())
|
|
|
|
return m_filespec_ap->GetSize();
|
|
|
|
|
|
|
|
m_filespec_ap.reset(new FileSpecList());
|
|
|
|
|
|
|
|
if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Locate the dynamic table.
|
|
|
|
user_id_t dynsym_id = 0;
|
|
|
|
user_id_t dynstr_id = 0;
|
2010-10-12 10:24:53 +08:00
|
|
|
for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
|
|
|
|
sh_pos != m_section_headers.end(); ++sh_pos)
|
2010-07-14 07:07:23 +08:00
|
|
|
{
|
2010-10-12 10:24:53 +08:00
|
|
|
if (sh_pos->sh_type == SHT_DYNAMIC)
|
2010-07-14 07:07:23 +08:00
|
|
|
{
|
2010-10-12 10:24:53 +08:00
|
|
|
dynsym_id = SectionIndex(sh_pos);
|
|
|
|
dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
|
2010-07-14 07:07:23 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dynsym_id && dynstr_id))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SectionList *section_list = GetSectionList();
|
|
|
|
if (!section_list)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Resolve and load the dynamic table entries and corresponding string
|
|
|
|
// table.
|
|
|
|
Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
|
|
|
|
Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
|
|
|
|
if (!(dynsym && dynstr))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DataExtractor dynsym_data;
|
|
|
|
DataExtractor dynstr_data;
|
|
|
|
if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) &&
|
|
|
|
dynstr->ReadSectionDataFromObjectFile(this, dynstr_data))
|
|
|
|
{
|
|
|
|
ELFDynamic symbol;
|
|
|
|
const unsigned section_size = dynsym_data.GetByteSize();
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
|
|
|
// The only type of entries we are concerned with are tagged DT_NEEDED,
|
|
|
|
// yielding the name of a required library.
|
|
|
|
while (offset < section_size)
|
|
|
|
{
|
|
|
|
if (!symbol.Parse(dynsym_data, &offset))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (symbol.d_tag != DT_NEEDED)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
|
|
|
|
const char *lib_name = dynstr_data.PeekCStr(str_index);
|
2010-10-21 04:54:39 +08:00
|
|
|
m_filespec_ap->Append(FileSpec(lib_name, true));
|
2010-07-14 07:07:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_filespec_ap->GetSize();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// ParseProgramHeaders
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
ObjectFileELF::ParseProgramHeaders()
|
|
|
|
{
|
|
|
|
// We have already parsed the program headers
|
|
|
|
if (!m_program_headers.empty())
|
|
|
|
return m_program_headers.size();
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
// If there are no program headers to read we are done.
|
|
|
|
if (m_header.e_phnum == 0)
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
m_program_headers.resize(m_header.e_phnum);
|
|
|
|
if (m_program_headers.size() != m_header.e_phnum)
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
|
|
|
|
const elf_off ph_offset = m_offset + m_header.e_phoff;
|
|
|
|
DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size));
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size)
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
DataExtractor data(buffer_sp, m_data.GetByteOrder(),
|
|
|
|
m_data.GetAddressByteSize());
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
uint32_t idx;
|
|
|
|
uint32_t offset;
|
|
|
|
for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
|
|
|
|
{
|
|
|
|
if (m_program_headers[idx].Parse(data, &offset) == false)
|
|
|
|
break;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
if (idx < m_program_headers.size())
|
|
|
|
m_program_headers.resize(idx);
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
return m_program_headers.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// ParseSectionHeaders
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
ObjectFileELF::ParseSectionHeaders()
|
|
|
|
{
|
|
|
|
// We have already parsed the section headers
|
|
|
|
if (!m_section_headers.empty())
|
|
|
|
return m_section_headers.size();
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
// If there are no section headers we are done.
|
|
|
|
if (m_header.e_shnum == 0)
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
m_section_headers.resize(m_header.e_shnum);
|
|
|
|
if (m_section_headers.size() != m_header.e_shnum)
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
|
|
|
|
const elf_off sh_offset = m_offset + m_header.e_shoff;
|
|
|
|
DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size));
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size)
|
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
DataExtractor data(buffer_sp,
|
|
|
|
m_data.GetByteOrder(),
|
|
|
|
m_data.GetAddressByteSize());
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
uint32_t idx;
|
|
|
|
uint32_t offset;
|
|
|
|
for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
|
|
|
|
{
|
|
|
|
if (m_section_headers[idx].Parse(data, &offset) == false)
|
|
|
|
break;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2010-07-14 07:07:23 +08:00
|
|
|
if (idx < m_section_headers.size())
|
|
|
|
m_section_headers.resize(idx);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
return m_section_headers.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ObjectFileELF::GetSectionHeaderStringTable()
|
|
|
|
{
|
|
|
|
if (m_shstr_data.GetByteSize() == 0)
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
const unsigned strtab_idx = m_header.e_shstrndx;
|
|
|
|
|
|
|
|
if (strtab_idx && strtab_idx < m_section_headers.size())
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
|
|
|
|
const size_t byte_size = sheader.sh_size;
|
|
|
|
const Elf64_Off offset = m_offset + sheader.sh_offset;
|
|
|
|
DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size));
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
m_shstr_data.SetData(buffer_sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m_shstr_data.GetByteSize();
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
lldb::user_id_t
|
2010-06-09 00:52:24 +08:00
|
|
|
ObjectFileELF::GetSectionIndexByName(const char *name)
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Search the collection of section headers for one with a matching name.
|
|
|
|
for (SectionHeaderCollIter I = m_section_headers.begin();
|
|
|
|
I != m_section_headers.end(); ++I)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
|
|
|
|
|
|
|
|
if (!sectionName)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(name, sectionName) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return SectionIndex(I);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
return 0;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SectionList *
|
|
|
|
ObjectFileELF::GetSectionList()
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
if (m_sections_ap.get())
|
|
|
|
return m_sections_ap.get();
|
|
|
|
|
|
|
|
if (ParseSectionHeaders() && GetSectionHeaderStringTable())
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
m_sections_ap.reset(new SectionList());
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
for (SectionHeaderCollIter I = m_section_headers.begin();
|
|
|
|
I != m_section_headers.end(); ++I)
|
|
|
|
{
|
|
|
|
const ELFSectionHeader &header = *I;
|
|
|
|
|
|
|
|
ConstString name(m_shstr_data.PeekCStr(header.sh_name));
|
|
|
|
uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
|
|
|
|
|
2010-07-22 06:54:26 +08:00
|
|
|
static ConstString g_sect_name_text (".text");
|
|
|
|
static ConstString g_sect_name_data (".data");
|
|
|
|
static ConstString g_sect_name_bss (".bss");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_info (".debug_info");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_line (".debug_line");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
|
|
|
|
static ConstString g_sect_name_dwarf_debug_str (".debug_str");
|
|
|
|
static ConstString g_sect_name_eh_frame (".eh_frame");
|
|
|
|
|
|
|
|
SectionType sect_type = eSectionTypeOther;
|
|
|
|
|
|
|
|
if (name == g_sect_name_text) sect_type = eSectionTypeCode;
|
|
|
|
else if (name == g_sect_name_data) sect_type = eSectionTypeData;
|
|
|
|
else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
|
|
|
|
else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
|
|
|
|
else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
|
|
|
|
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
SectionSP section(new Section(
|
2010-07-22 06:54:26 +08:00
|
|
|
0, // Parent section.
|
|
|
|
GetModule(), // Module to which this section belongs.
|
|
|
|
SectionIndex(I), // Section ID.
|
|
|
|
name, // Section name.
|
|
|
|
sect_type, // Section type.
|
|
|
|
header.sh_addr, // VM address.
|
|
|
|
header.sh_size, // VM size in bytes of this section.
|
|
|
|
header.sh_offset, // Offset of this section in the file.
|
|
|
|
size, // Size of the section as found in the file.
|
|
|
|
header.sh_flags)); // Flags for this section.
|
2010-07-14 07:07:23 +08:00
|
|
|
|
|
|
|
m_sections_ap->AddSection(section);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-14 07:07:23 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
return m_sections_ap.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-07-14 07:07:23 +08:00
|
|
|
ParseSymbols(Symtab *symtab, SectionList *section_list,
|
|
|
|
const ELFSectionHeader &symtab_shdr,
|
|
|
|
const DataExtractor &symtab_data,
|
|
|
|
const DataExtractor &strtab_data)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
ELFSymbol symbol;
|
2010-06-09 00:52:24 +08:00
|
|
|
uint32_t offset = 0;
|
2010-07-14 07:07:23 +08:00
|
|
|
const unsigned numSymbols =
|
|
|
|
symtab_data.GetByteSize() / symtab_shdr.sh_entsize;
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
static ConstString text_section_name(".text");
|
|
|
|
static ConstString init_section_name(".init");
|
|
|
|
static ConstString fini_section_name(".fini");
|
|
|
|
static ConstString ctors_section_name(".ctors");
|
|
|
|
static ConstString dtors_section_name(".dtors");
|
|
|
|
|
|
|
|
static ConstString data_section_name(".data");
|
|
|
|
static ConstString rodata_section_name(".rodata");
|
|
|
|
static ConstString rodata1_section_name(".rodata1");
|
|
|
|
static ConstString data2_section_name(".data1");
|
|
|
|
static ConstString bss_section_name(".bss");
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
for (unsigned i = 0; i < numSymbols; ++i)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
if (symbol.Parse(symtab_data, &offset) == false)
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
Section *symbol_section = NULL;
|
2010-06-09 00:52:24 +08:00
|
|
|
SymbolType symbol_type = eSymbolTypeInvalid;
|
2010-07-14 07:07:23 +08:00
|
|
|
Elf64_Half symbol_idx = symbol.st_shndx;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
switch (symbol_idx)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
case SHN_ABS:
|
|
|
|
symbol_type = eSymbolTypeAbsolute;
|
|
|
|
break;
|
|
|
|
case SHN_UNDEF:
|
|
|
|
symbol_type = eSymbolTypeUndefined;
|
|
|
|
break;
|
|
|
|
default:
|
2010-07-14 07:07:23 +08:00
|
|
|
symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
switch (symbol.getType())
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
default:
|
|
|
|
case STT_NOTYPE:
|
|
|
|
// The symbol's type is not specified.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STT_OBJECT:
|
2010-07-14 07:07:23 +08:00
|
|
|
// The symbol is associated with a data object, such as a variable,
|
|
|
|
// an array, etc.
|
|
|
|
symbol_type = eSymbolTypeData;
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STT_FUNC:
|
|
|
|
// The symbol is associated with a function or other executable code.
|
2010-07-14 07:07:23 +08:00
|
|
|
symbol_type = eSymbolTypeCode;
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STT_SECTION:
|
|
|
|
// The symbol is associated with a section. Symbol table entries of
|
|
|
|
// this type exist primarily for relocation and normally have
|
|
|
|
// STB_LOCAL binding.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STT_FILE:
|
|
|
|
// Conventionally, the symbol's name gives the name of the source
|
|
|
|
// file associated with the object file. A file symbol has STB_LOCAL
|
|
|
|
// binding, its section index is SHN_ABS, and it precedes the other
|
|
|
|
// STB_LOCAL symbols for the file, if it is present.
|
2010-07-14 07:07:23 +08:00
|
|
|
symbol_type = eSymbolTypeObjectFile;
|
2010-06-09 00:52:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symbol_type == eSymbolTypeInvalid)
|
|
|
|
{
|
|
|
|
if (symbol_section)
|
|
|
|
{
|
|
|
|
const ConstString §_name = symbol_section->GetName();
|
|
|
|
if (sect_name == text_section_name ||
|
|
|
|
sect_name == init_section_name ||
|
|
|
|
sect_name == fini_section_name ||
|
|
|
|
sect_name == ctors_section_name ||
|
|
|
|
sect_name == dtors_section_name)
|
|
|
|
{
|
|
|
|
symbol_type = eSymbolTypeCode;
|
|
|
|
}
|
2010-07-14 07:07:23 +08:00
|
|
|
else if (sect_name == data_section_name ||
|
|
|
|
sect_name == data2_section_name ||
|
|
|
|
sect_name == rodata_section_name ||
|
|
|
|
sect_name == rodata1_section_name ||
|
|
|
|
sect_name == bss_section_name)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
symbol_type = eSymbolTypeData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t symbol_value = symbol.st_value;
|
|
|
|
if (symbol_section != NULL)
|
|
|
|
symbol_value -= symbol_section->GetFileAddress();
|
|
|
|
const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
|
2010-07-14 07:07:23 +08:00
|
|
|
bool is_global = symbol.getBinding() == STB_GLOBAL;
|
|
|
|
uint32_t flags = symbol.st_other << 8 | symbol.st_info;
|
|
|
|
|
|
|
|
Symbol dc_symbol(
|
|
|
|
i, // ID is the original symbol table index.
|
|
|
|
symbol_name, // Symbol name.
|
|
|
|
false, // Is the symbol name mangled?
|
|
|
|
symbol_type, // Type of this symbol
|
|
|
|
is_global, // Is this globally visible?
|
|
|
|
false, // Is this symbol debug info?
|
|
|
|
false, // Is this symbol a trampoline?
|
|
|
|
false, // Is this symbol artificial?
|
|
|
|
symbol_section, // Section in which this symbol is defined or null.
|
|
|
|
symbol_value, // Offset in section or symbol value.
|
|
|
|
symbol.st_size, // Size in bytes of this symbol.
|
|
|
|
flags); // Symbol flags.
|
2010-06-09 00:52:24 +08:00
|
|
|
symtab->AddSymbol(dc_symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
void
|
|
|
|
ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
|
|
|
|
const ELFSectionHeader &symtab_hdr,
|
|
|
|
user_id_t symtab_id)
|
|
|
|
{
|
|
|
|
assert(symtab_hdr.sh_type == SHT_SYMTAB ||
|
|
|
|
symtab_hdr.sh_type == SHT_DYNSYM);
|
|
|
|
|
|
|
|
// Parse in the section list if needed.
|
|
|
|
SectionList *section_list = GetSectionList();
|
|
|
|
if (!section_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Section ID's are ones based.
|
|
|
|
user_id_t strtab_id = symtab_hdr.sh_link + 1;
|
|
|
|
|
|
|
|
Section *symtab = section_list->FindSectionByID(symtab_id).get();
|
|
|
|
Section *strtab = section_list->FindSectionByID(strtab_id).get();
|
|
|
|
if (symtab && strtab)
|
|
|
|
{
|
|
|
|
DataExtractor symtab_data;
|
|
|
|
DataExtractor strtab_data;
|
|
|
|
if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) &&
|
|
|
|
strtab->ReadSectionDataFromObjectFile(this, strtab_data))
|
|
|
|
{
|
|
|
|
ParseSymbols(symbol_table, section_list, symtab_hdr,
|
|
|
|
symtab_data, strtab_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
Symtab *
|
|
|
|
ObjectFileELF::GetSymtab()
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
if (m_symtab_ap.get())
|
|
|
|
return m_symtab_ap.get();
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
Symtab *symbol_table = new Symtab(this);
|
|
|
|
m_symtab_ap.reset(symbol_table);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-10-08 12:20:14 +08:00
|
|
|
Mutex::Locker locker (symbol_table->GetMutex ());
|
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
|
|
|
|
return symbol_table;
|
|
|
|
|
|
|
|
// Locate and parse all linker symbol tables.
|
|
|
|
for (SectionHeaderCollIter I = m_section_headers.begin();
|
|
|
|
I != m_section_headers.end(); ++I)
|
|
|
|
{
|
|
|
|
if (I->sh_type == SHT_SYMTAB)
|
|
|
|
{
|
|
|
|
const ELFSectionHeader &symtab_section = *I;
|
|
|
|
user_id_t section_id = SectionIndex(I);
|
2010-10-08 12:20:14 +08:00
|
|
|
ParseSymbolTable (symbol_table, symtab_section, section_id);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-14 07:07:23 +08:00
|
|
|
|
|
|
|
return symbol_table;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dump
|
|
|
|
//
|
|
|
|
// Dump the specifics of the runtime file container (such as any headers
|
|
|
|
// segments, sections, etc).
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
ObjectFileELF::Dump(Stream *s)
|
|
|
|
{
|
|
|
|
DumpELFHeader(s, m_header);
|
|
|
|
s->EOL();
|
|
|
|
DumpELFProgramHeaders(s);
|
|
|
|
s->EOL();
|
|
|
|
DumpELFSectionHeaders(s);
|
|
|
|
s->EOL();
|
|
|
|
SectionList *section_list = GetSectionList();
|
|
|
|
if (section_list)
|
2010-12-08 13:08:21 +08:00
|
|
|
section_list->Dump(s, NULL, true, UINT32_MAX);
|
2010-06-09 00:52:24 +08:00
|
|
|
Symtab *symtab = GetSymtab();
|
|
|
|
if (symtab)
|
2010-10-08 12:20:14 +08:00
|
|
|
symtab->Dump(s, NULL, lldb::eSortOrderNone);
|
2010-06-09 00:52:24 +08:00
|
|
|
s->EOL();
|
2010-07-14 07:07:23 +08:00
|
|
|
DumpDependentModules(s);
|
|
|
|
s->EOL();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFHeader
|
|
|
|
//
|
|
|
|
// Dump the ELF header to the specified output stream
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
|
|
|
|
{
|
|
|
|
s->PutCString("ELF Header\n");
|
|
|
|
s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
|
|
|
|
s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
|
|
|
|
header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
|
|
|
|
s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
|
|
|
|
header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
|
|
|
|
s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
|
|
|
|
header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
|
|
|
|
|
|
|
|
s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
|
|
|
|
s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
|
2010-06-09 00:52:24 +08:00
|
|
|
DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
|
|
|
|
s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
|
|
|
|
s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
|
|
|
|
|
|
|
|
s->Printf("e_type = 0x%4.4x ", header.e_type);
|
|
|
|
DumpELFHeader_e_type(s, header.e_type);
|
|
|
|
s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
|
|
|
|
s->Printf("e_version = 0x%8.8x\n", header.e_version);
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf("e_entry = 0x%8.8lx\n", header.e_entry);
|
|
|
|
s->Printf("e_phoff = 0x%8.8lx\n", header.e_phoff);
|
|
|
|
s->Printf("e_shoff = 0x%8.8lx\n", header.e_shoff);
|
2010-06-09 00:52:24 +08:00
|
|
|
s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
|
|
|
|
s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
|
|
|
|
s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
|
|
|
|
s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
|
|
|
|
s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
|
|
|
|
s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
|
|
|
|
s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFHeader_e_type
|
|
|
|
//
|
|
|
|
// Dump an token value for the ELF header member e_type
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
switch (e_type)
|
|
|
|
{
|
|
|
|
case ET_NONE: *s << "ET_NONE"; break;
|
|
|
|
case ET_REL: *s << "ET_REL"; break;
|
|
|
|
case ET_EXEC: *s << "ET_EXEC"; break;
|
|
|
|
case ET_DYN: *s << "ET_DYN"; break;
|
|
|
|
case ET_CORE: *s << "ET_CORE"; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFHeader_e_ident_EI_DATA
|
|
|
|
//
|
|
|
|
// Dump an token value for the ELF header member e_ident[EI_DATA]
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
switch (ei_data)
|
|
|
|
{
|
|
|
|
case ELFDATANONE: *s << "ELFDATANONE"; break;
|
|
|
|
case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
|
|
|
|
case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFProgramHeader
|
|
|
|
//
|
|
|
|
// Dump a single ELF program header to the specified output stream
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
DumpELFProgramHeader_p_type(s, ph.p_type);
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
|
|
|
|
s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags);
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
DumpELFProgramHeader_p_flags(s, ph.p_flags);
|
|
|
|
s->Printf(") %8.8x", ph.p_align);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFProgramHeader_p_type
|
|
|
|
//
|
|
|
|
// Dump an token value for the ELF program header member p_type which
|
|
|
|
// describes the type of the program header
|
2010-07-14 07:07:23 +08:00
|
|
|
// ----------------------------------------------------------------------
|
2010-06-09 00:52:24 +08:00
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
const int kStrWidth = 10;
|
|
|
|
switch (p_type)
|
|
|
|
{
|
|
|
|
CASE_AND_STREAM(s, PT_NULL , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
|
|
|
|
default:
|
|
|
|
s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFProgramHeader_p_flags
|
|
|
|
//
|
|
|
|
// Dump an token value for the ELF program header member p_flags
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
*s << ((p_flags & PF_X) ? "PF_X" : " ")
|
|
|
|
<< (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
|
|
|
|
<< ((p_flags & PF_W) ? "PF_W" : " ")
|
|
|
|
<< (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
|
|
|
|
<< ((p_flags & PF_R) ? "PF_R" : " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFProgramHeaders
|
|
|
|
//
|
|
|
|
// Dump all of the ELF program header to the specified output stream
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
ObjectFileELF::DumpELFProgramHeaders(Stream *s)
|
|
|
|
{
|
|
|
|
if (ParseProgramHeaders())
|
|
|
|
{
|
|
|
|
s->PutCString("Program Headers\n");
|
2010-07-14 07:07:23 +08:00
|
|
|
s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
|
|
|
|
"p_filesz p_memsz p_flags p_align\n");
|
|
|
|
s->PutCString("==== ---------- -------- -------- -------- "
|
|
|
|
"-------- -------- ------------------------- --------\n");
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
uint32_t idx = 0;
|
2010-07-14 07:07:23 +08:00
|
|
|
for (ProgramHeaderCollConstIter I = m_program_headers.begin();
|
|
|
|
I != m_program_headers.end(); ++I, ++idx)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf("[%2u] ", idx);
|
|
|
|
ObjectFileELF::DumpELFProgramHeader(s, *I);
|
2010-06-09 00:52:24 +08:00
|
|
|
s->EOL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFSectionHeader
|
|
|
|
//
|
|
|
|
// Dump a single ELF section header to the specified output stream
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf("%8.8x ", sh.sh_name);
|
2010-06-09 00:52:24 +08:00
|
|
|
DumpELFSectionHeader_sh_type(s, sh.sh_type);
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf(" %8.8lx (", sh.sh_flags);
|
2010-06-09 00:52:24 +08:00
|
|
|
DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size);
|
|
|
|
s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
|
|
|
|
s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFSectionHeader_sh_type
|
|
|
|
//
|
|
|
|
// Dump an token value for the ELF section header member sh_type which
|
|
|
|
// describes the type of the section
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
const int kStrWidth = 12;
|
|
|
|
switch (sh_type)
|
|
|
|
{
|
|
|
|
CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_REL , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
|
|
|
|
CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
|
|
|
|
default:
|
|
|
|
s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFSectionHeader_sh_flags
|
|
|
|
//
|
|
|
|
// Dump an token value for the ELF section header member sh_flags
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
2010-07-14 07:07:23 +08:00
|
|
|
ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
*s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
|
|
|
|
<< (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
|
|
|
|
<< ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
|
|
|
|
<< (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
|
|
|
|
<< ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
|
|
|
|
}
|
2010-07-14 07:07:23 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// DumpELFSectionHeaders
|
|
|
|
//
|
|
|
|
// Dump all of the ELF section header to the specified output stream
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
ObjectFileELF::DumpELFSectionHeaders(Stream *s)
|
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s->PutCString("Section Headers\n");
|
|
|
|
s->PutCString("IDX name type flags "
|
|
|
|
"addr offset size link info addralgn "
|
|
|
|
"entsize Name\n");
|
|
|
|
s->PutCString("==== -------- ------------ -------------------------------- "
|
|
|
|
"-------- -------- -------- -------- -------- -------- "
|
|
|
|
"-------- ====================\n");
|
|
|
|
|
|
|
|
uint32_t idx = 0;
|
|
|
|
for (SectionHeaderCollConstIter I = m_section_headers.begin();
|
|
|
|
I != m_section_headers.end(); ++I, ++idx)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
s->Printf("[%2u] ", idx);
|
|
|
|
ObjectFileELF::DumpELFSectionHeader(s, *I);
|
|
|
|
const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
|
|
|
|
if (section_name)
|
|
|
|
*s << ' ' << section_name << "\n";
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
void
|
|
|
|
ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
|
|
|
|
{
|
|
|
|
size_t num_modules = ParseDependentModules();
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2010-07-14 07:07:23 +08:00
|
|
|
if (num_modules > 0)
|
|
|
|
{
|
|
|
|
s->PutCString("Dependent Modules:\n");
|
|
|
|
for (unsigned i = 0; i < num_modules; ++i)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-14 07:07:23 +08:00
|
|
|
const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
|
|
|
|
s->Printf(" %s\n", spec.GetFilename().GetCString());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-02-16 05:59:32 +08:00
|
|
|
ObjectFileELF::GetArchitecture (ArchSpec &arch)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-02-23 08:35:02 +08:00
|
|
|
arch.SetArchitecture (lldb::eArchTypeELF, m_header.e_machine, m_header.e_flags);
|
|
|
|
arch.GetTriple().setOSName (Host::GetOSString().GetCString());
|
|
|
|
arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
|
2010-07-14 07:07:23 +08:00
|
|
|
return true;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|