diff options
author | Stephen Wilson <wilsons@start.ca> | 2010-07-13 23:07:23 +0000 |
---|---|---|
committer | Stephen Wilson <wilsons@start.ca> | 2010-07-13 23:07:23 +0000 |
commit | f325ba9d451a247eaa13cd5e6419f68edab214b9 (patch) | |
tree | 2b6d05538a1dbcf25a79f1744a0ddb23031d2efa /lldb/source/Plugins/ObjectFile | |
parent | cb89a2a150bc58a88170ba4eec7a3de675ad3c3a (diff) | |
download | bcm5719-llvm-f325ba9d451a247eaa13cd5e6419f68edab214b9.tar.gz bcm5719-llvm-f325ba9d451a247eaa13cd5e6419f68edab214b9.zip |
Combine 32 and 64 bit ELF readers.
This patch provides a generic ELF reader plugin to handle both 32 and 64 bit
formats.
llvm-svn: 108292
Diffstat (limited to 'lldb/source/Plugins/ObjectFile')
-rw-r--r-- | lldb/source/Plugins/ObjectFile/ELF/ELFHeader.cpp | 302 | ||||
-rw-r--r-- | lldb/source/Plugins/ObjectFile/ELF/ELFHeader.h | 295 | ||||
-rw-r--r-- | lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp | 871 | ||||
-rw-r--r-- | lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h | 225 | ||||
-rw-r--r-- | lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.cpp | 1017 | ||||
-rw-r--r-- | lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.h | 209 |
6 files changed, 1203 insertions, 1716 deletions
diff --git a/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.cpp b/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.cpp new file mode 100644 index 00000000000..3fd795be4c1 --- /dev/null +++ b/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.cpp @@ -0,0 +1,302 @@ +//===-- ELFHeader.cpp ----------------------------------------- -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include <cstring> + +#include "lldb/Core/DataExtractor.h" + +#include "ELFHeader.h" + +using namespace elf; +using namespace lldb; +using namespace llvm::ELF; + +//------------------------------------------------------------------------------ +// Static utility functions. +// +// GetMaxU64 and GetMaxS64 wrap the similarly named methods from DataExtractor +// with error handling code and provide for parsing a sequence of values. +static bool +GetMaxU64(const lldb_private::DataExtractor &data, + uint32_t *offset, uint64_t *value, uint32_t byte_size) +{ + const uint32_t saved_offset = *offset; + *value = data.GetMaxU64(offset, byte_size); + return *offset != saved_offset; +} + +static bool +GetMaxU64(const lldb_private::DataExtractor &data, + uint32_t *offset, uint64_t *value, uint32_t byte_size, + uint32_t count) +{ + uint32_t saved_offset = *offset; + + for (uint32_t i = 0; i < count; ++i, ++value) + { + if (GetMaxU64(data, offset, value, byte_size) == false) + { + *offset = saved_offset; + return false; + } + } + return true; +} + +static bool +GetMaxS64(const lldb_private::DataExtractor &data, + uint32_t *offset, int64_t *value, uint32_t byte_size) +{ + const uint32_t saved_offset = *offset; + *value = data.GetMaxS64(offset, byte_size); + return *offset != saved_offset; +} + +static bool +GetMaxS64(const lldb_private::DataExtractor &data, + uint32_t *offset, int64_t *value, uint32_t byte_size, + uint32_t count) +{ + uint32_t saved_offset = *offset; + + for (uint32_t i = 0; i < count; ++i, ++value) + { + if (GetMaxS64(data, offset, value, byte_size) == false) + { + *offset = saved_offset; + return false; + } + } + return true; +} + +//------------------------------------------------------------------------------ +// ELFHeader + +ELFHeader::ELFHeader() +{ + memset(this, 0, sizeof(ELFHeader)); +} + +ByteOrder +ELFHeader::GetByteOrder() const +{ + if (e_ident[EI_DATA] == ELFDATA2MSB) + return eByteOrderBig; + if (e_ident[EI_DATA] == ELFDATA2LSB) + return eByteOrderLittle; + return eByteOrderInvalid; +} + +bool +ELFHeader::Parse(lldb_private::DataExtractor &data, uint32_t *offset) +{ + // Read e_ident. This provides byte order and address size info. + if (data.GetU8(offset, &e_ident, EI_NIDENT) == NULL) + return false; + + const unsigned byte_size = Is32Bit() ? 4 : 8; + data.SetByteOrder(GetByteOrder()); + data.SetAddressByteSize(byte_size); + + // Read e_type and e_machine. + if (data.GetU16(offset, &e_type, 2) == NULL) + return false; + + // Read e_version. + if (data.GetU32(offset, &e_version, 1) == NULL) + return false; + + // Read e_entry, e_phoff and e_shoff. + if (GetMaxU64(data, offset, &e_entry, byte_size, 3) == false) + return false; + + // Read e_flags. + if (data.GetU32(offset, &e_flags, 1) == NULL) + return false; + + // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and + // e_shstrndx. + if (data.GetU16(offset, &e_ehsize, 6) == NULL) + return false; + + return true; +} + +bool +ELFHeader::MagicBytesMatch(const uint8_t *magic) +{ + return memcmp(magic, ElfMagic, strlen(ElfMagic)) == 0; +} + +unsigned +ELFHeader::AddressSizeInBytes(const uint8_t *magic) +{ + unsigned address_size = 0; + + switch (magic[EI_CLASS]) + { + case ELFCLASS32: + address_size = 4; + break; + + case ELFCLASS64: + address_size = 8; + break; + } + return address_size; +} + +//------------------------------------------------------------------------------ +// ELFSectionHeader + +ELFSectionHeader::ELFSectionHeader() +{ + memset(this, 0, sizeof(ELFSectionHeader)); +} + +bool +ELFSectionHeader::Parse(const lldb_private::DataExtractor &data, + uint32_t *offset) +{ + const unsigned byte_size = data.GetAddressByteSize(); + + // Read sh_name and sh_type. + if (data.GetU32(offset, &sh_name, 2) == NULL) + return false; + + // Read sh_flags. + if (GetMaxU64(data, offset, &sh_flags, byte_size) == false) + return false; + + // Read sh_addr, sh_off and sh_size. + if (GetMaxU64(data, offset, &sh_addr, byte_size, 3) == false) + return false; + + // Read sh_link and sh_info. + if (data.GetU32(offset, &sh_link, 2) == NULL) + return false; + + // Read sh_addralign and sh_entsize. + if (GetMaxU64(data, offset, &sh_addralign, byte_size, 2) == false) + return false; + + return true; +} + +//------------------------------------------------------------------------------ +// ELFSymbol + +ELFSymbol::ELFSymbol() +{ + memset(this, 0, sizeof(ELFSymbol)); +} + +bool +ELFSymbol::Parse(const lldb_private::DataExtractor &data, uint32_t *offset) +{ + const unsigned byte_size = data.GetAddressByteSize(); + const bool parsing_32 = byte_size == 4; + + // Read st_name. + if (data.GetU32(offset, &st_name, 1) == NULL) + return false; + + if (parsing_32) + { + // Read st_value and st_size. + if (GetMaxU64(data, offset, &st_value, byte_size, 2) == false) + return false; + + // Read st_info and st_other. + if (data.GetU8(offset, &st_info, 2) == NULL) + return false; + + // Read st_shndx. + if (data.GetU16(offset, &st_shndx, 1) == NULL) + return false; + } + else + { + // Read st_info and st_other. + if (data.GetU8(offset, &st_info, 2) == NULL) + return false; + + // Read st_shndx. + if (data.GetU16(offset, &st_shndx, 1) == NULL) + return false; + + // Read st_value and st_size. + if (data.GetU64(offset, &st_value, 2) == NULL) + return false; + } + return true; +} + +//------------------------------------------------------------------------------ +// ELFProgramHeader + +ELFProgramHeader::ELFProgramHeader() +{ + memset(this, 0, sizeof(ELFProgramHeader)); +} + +bool +ELFProgramHeader::Parse(const lldb_private::DataExtractor &data, + uint32_t *offset) +{ + const uint32_t byte_size = data.GetAddressByteSize(); + const bool parsing_32 = byte_size == 4; + + // Read p_type; + if (data.GetU32(offset, &p_type, 1) == NULL) + return false; + + if (parsing_32) { + // Read p_offset, p_vaddr, p_paddr, p_filesz and p_memsz. + if (GetMaxU64(data, offset, &p_offset, byte_size, 5) == false) + return false; + + // Read p_flags. + if (data.GetU32(offset, &p_flags, 1) == NULL) + return false; + + // Read p_align. + if (GetMaxU64(data, offset, &p_align, byte_size) == false) + return false; + } + else { + // Read p_flags. + if (data.GetU32(offset, &p_flags, 1) == NULL) + return false; + + // Read p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align. + if (GetMaxU64(data, offset, &p_offset, byte_size, 6) == false) + return false; + } + + return true; +} + +//------------------------------------------------------------------------------ +// ELFDynamic + +ELFDynamic::ELFDynamic() +{ + memset(this, 0, sizeof(ELFDynamic)); +} + +bool +ELFDynamic::Parse(const lldb_private::DataExtractor &data, uint32_t *offset) +{ + const unsigned byte_size = data.GetAddressByteSize(); + return GetMaxS64(data, offset, &d_tag, byte_size, 2); +} + + diff --git a/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.h b/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.h new file mode 100644 index 00000000000..f6b1653e50d --- /dev/null +++ b/lldb/source/Plugins/ObjectFile/ELF/ELFHeader.h @@ -0,0 +1,295 @@ +//===-- ELFHeader.h ------------------------------------------- -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +/// @file +/// @brief Generic structures and typedefs for ELF files. +/// +/// This file provides definitions for the various entities comprising an ELF +/// file. The structures are generic in the sense that they do not correspond +/// to the exact binary layout of an ELF, but can be used to hold the +/// information present in both 32 and 64 bit variants of the format. Each +/// entity provides a \c Parse method which is capable of transparently reading +/// both 32 and 64 bit instances of the object. +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ELFHeader_h_ +#define liblldb_ELFHeader_h_ + +#include "llvm/Support/ELF.h" + +#include "lldb/lldb-enumerations.h" + +namespace lldb_private +{ +class DataExtractor; +} // End namespace lldb_private. + +namespace elf +{ + +//------------------------------------------------------------------------------ +/// @name ELF type definitions. +/// +/// Types used to represent the various components of ELF structures. All types +/// are signed or unsigned integral types wide enough to hold values from both +/// 32 and 64 bit ELF variants. +//@{ +typedef uint64_t elf_addr; +typedef uint64_t elf_off; +typedef uint16_t elf_half; +typedef uint32_t elf_word; +typedef int32_t elf_sword; +typedef uint64_t elf_size; +typedef uint64_t elf_xword; +typedef int64_t elf_sxword; +//@} + +//------------------------------------------------------------------------------ +/// @class ELFHeader +/// @brief Generic representation of an ELF file header. +/// +/// This object is used to identify the general attributes on an ELF file and to +/// locate additional sections within the file. +struct ELFHeader +{ + unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification. + elf_addr e_entry; ///< Virtual address program entry point. + elf_off e_phoff; ///< File offset of program header table. + elf_off e_shoff; ///< File offset of section header table. + elf_word e_flags; ///< Processor specific flags. + elf_word e_version; ///< Version of object file (always 1). + elf_half e_type; ///< Object file type. + elf_half e_machine; ///< Target architecture. + elf_half e_ehsize; ///< Byte size of the ELF header. + elf_half e_phentsize; ///< Size of a program header table entry. + elf_half e_phnum; ///< Number of program header entrys. + elf_half e_shentsize; ///< Size of a section header table entry. + elf_half e_shnum; ///< Number of section header entrys. + elf_half e_shstrndx; ///< String table section index. + + ELFHeader(); + + //-------------------------------------------------------------------------- + /// Returns true if this is a 32 bit ELF file header. + /// + /// @return + /// True if this is a 32 bit ELF file header. + bool Is32Bit() const { + return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32; + } + + //-------------------------------------------------------------------------- + /// Returns true if this is a 64 bit ELF file header. + /// + /// @return + /// True if this is a 64 bit ELF file header. + bool Is64Bit() const { + return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64; + } + + //-------------------------------------------------------------------------- + /// The byte order of this ELF file header. + /// + /// @return + /// The byte order of this ELF file as described by the header. + lldb::ByteOrder + GetByteOrder() const; + + //-------------------------------------------------------------------------- + /// Parse an ELFSectionHeader entry starting at position \p offset and + /// update the data extractor with the address size and byte order + /// attributes as defined by the header. + /// + /// @param[in,out] data + /// The DataExtractor to read from. Updated with the address size and + /// byte order attributes appropriate to this header. + /// + /// @param[in,out] offset + /// Pointer to an offset in the data. On return the offset will be + /// advanced by the number of bytes read. + /// + /// @return + /// True if the ELFSectionHeader was successfully read and false + /// otherwise. + bool + Parse(lldb_private::DataExtractor &data, uint32_t *offset); + + //-------------------------------------------------------------------------- + /// Examines at most EI_NIDENT bytes starting from the given pointer and + /// determines if the magic ELF identification exists. + /// + /// @return + /// True if the given sequence of bytes identifies an ELF file. + static bool + MagicBytesMatch(const uint8_t *magic); + + //-------------------------------------------------------------------------- + /// Examines at most EI_NIDENT bytes starting from the given address and + /// determines the address size of the underlying ELF file. This function + /// should only be called on an pointer for which MagicBytesMatch returns + /// true. + /// + /// @return + /// The number of bytes forming an address in the ELF file (either 4 or + /// 8), else zero if the address size could not be determined. + static unsigned + AddressSizeInBytes(const uint8_t *magic); +}; + +//------------------------------------------------------------------------------ +/// @class ELFSectionHeader +/// @brief Generic representation of an ELF section header. +struct ELFSectionHeader +{ + elf_word sh_name; ///< Section name string index. + elf_word sh_type; ///< Section type. + elf_xword sh_flags; ///< Section attributes. + elf_addr sh_addr; ///< Virtual address of the section in memory. + elf_off sh_offset; ///< Start of section from beginning of file. + elf_xword sh_size; ///< Number of bytes occupied in the file. + elf_word sh_link; ///< Index of associated section. + elf_word sh_info; ///< Extra section info (overloaded). + elf_xword sh_addralign; ///< Power of two alignment constraint. + elf_xword sh_entsize; ///< Byte size of each section entry. + + ELFSectionHeader(); + + //-------------------------------------------------------------------------- + /// Parse an ELFSectionHeader entry from the given DataExtracter starting at + /// position \p offset. + /// + /// @param[in] data + /// The DataExtractor to read from. The address size of the extractor + /// determines if a 32 or 64 bit object should be read. + /// + /// @param[in,out] offset + /// Pointer to an offset in the data. On return the offset will be + /// advanced by the number of bytes read. + /// + /// @return + /// True if the ELFSectionHeader was successfully read and false + /// otherwise. + bool + Parse(const lldb_private::DataExtractor &data, uint32_t *offset); +}; + +//------------------------------------------------------------------------------ +/// @class ELFProgramHeader +/// @brief Generic representation of an ELF program header. +struct ELFProgramHeader +{ + elf_word p_type; ///< Type of program segement. + elf_word p_flags; ///< Segement attibutes. + elf_off p_offset; ///< Start of segment from begining of file. + elf_addr p_vaddr; ///< Virtual address of segment in memory. + elf_addr p_paddr; ///< Physical address (for non-VM systems). + elf_xword p_filesz; ///< Byte size of the segment in file. + elf_xword p_memsz; ///< Byte size of the segment in memory. + elf_xword p_align; ///< Segement alignement constraint. + + ELFProgramHeader(); + + /// Parse an ELFProgramHeader entry from the given DataExtracter starting at + /// position \p offset. The address size of the DataExtracter determines if + /// a 32 or 64 bit object is to be parsed. + /// + /// @param[in] data + /// The DataExtractor to read from. The address size of the extractor + /// determines if a 32 or 64 bit object should be read. + /// + /// @param[in,out] offset + /// Pointer to an offset in the data. On return the offset will be + /// advanced by the number of bytes read. + /// + /// @return + /// True if the ELFProgramHeader was successfully read and false + /// otherwise. + bool + Parse(const lldb_private::DataExtractor &data, uint32_t *offset); +}; + +//------------------------------------------------------------------------------ +/// @class ELFSymbol +/// @brief Represents a symbol within an ELF symbol table. +struct ELFSymbol +{ + elf_addr st_value; ///< Absolute or relocatable address. + elf_xword st_size; ///< Size of the symbol or zero. + elf_word st_name; ///< Symbol name string index. + unsigned char st_info; ///< Symbol type and binding attributes. + unsigned char st_other; ///< Reserved for future use. + elf_half st_shndx; ///< Section to which this symbol applies. + + ELFSymbol(); + + /// Returns the binding attribute of the st_info member. + unsigned char getBinding() const { return st_info >> 4; } + + /// Returns the type attribute of the st_info member. + unsigned char getType() const { return st_info & 0x0F; } + + /// Sets the bining and type of the st_info member. + void setBindingAndType(unsigned char binding, unsigned char type) { + st_info = (binding << 4) + (type & 0x0F); + } + + /// Parse an ELFSymbol entry from the given DataExtracter starting at + /// position \p offset. The address size of the DataExtracter determines if + /// a 32 or 64 bit object is to be parsed. + /// + /// @param[in] data + /// The DataExtractor to read from. The address size of the extractor + /// determines if a 32 or 64 bit object should be read. + /// + /// @param[in,out] offset + /// Pointer to an offset in the data. On return the offset will be + /// advanced by the number of bytes read. + /// + /// @return + /// True if the ELFSymbol was successfully read and false otherwise. + bool + Parse(const lldb_private::DataExtractor &data, uint32_t *offset); +}; + +//------------------------------------------------------------------------------ +/// @class ELFDynamic +/// @brief Represents an entry in an ELF dynamic table. +struct ELFDynamic +{ + elf_sxword d_tag; ///< Type of dynamic table entry. + union + { + elf_xword d_val; ///< Integer value of the table entry. + elf_addr d_ptr; ///< Pointer value of the table entry. + }; + + ELFDynamic(); + + /// Parse an ELFDynamic entry from the given DataExtracter starting at + /// position \p offset. The address size of the DataExtracter determines if + /// a 32 or 64 bit object is to be parsed. + /// + /// @param[in] data + /// The DataExtractor to read from. The address size of the extractor + /// determines if a 32 or 64 bit object should be read. + /// + /// @param[in,out] offset + /// Pointer to an offset in the data. On return the offset will be + /// advanced by the number of bytes read. + /// + /// @return + /// True if the ELFDynamic entry was successfully read and false + /// otherwise. + bool + Parse(const lldb_private::DataExtractor &data, uint32_t *offset); +}; + +} // End namespace elf. + +#endif // #ifndef liblldb_ELFHeader_h_ diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp index d20a8d6b7d8..c4772202b7a 100644 --- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp +++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp @@ -1,4 +1,4 @@ -//===-- ObjectFileELF.cpp ---------------------------------------*- C++ -*-===// +//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -9,109 +9,144 @@ #include "ObjectFileELF.h" -#include <assert.h> - +#include <cassert> #include <algorithm> -#include <stdint.h> -#include "elf.h" #include "lldb/Core/DataBuffer.h" #include "lldb/Core/Error.h" +#include "lldb/Core/FileSpecList.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/Section.h" #include "lldb/Core/Stream.h" -#include "lldb/Symbol/ObjectFile.h" -#define CASE_AND_STREAM(s, def, width) case def: s->Printf("%-*s", width, #def); break; +#define CASE_AND_STREAM(s, def, width) \ + case def: s->Printf("%-*s", width, #def); break; using namespace lldb; using namespace lldb_private; -using namespace std; - +using namespace elf; +using namespace llvm::ELF; +//------------------------------------------------------------------ +// Static methods. +//------------------------------------------------------------------ void ObjectFileELF::Initialize() { - PluginManager::RegisterPlugin (GetPluginNameStatic(), - GetPluginDescriptionStatic(), - CreateInstance); + PluginManager::RegisterPlugin(GetPluginNameStatic(), + GetPluginDescriptionStatic(), + CreateInstance); } void ObjectFileELF::Terminate() { - PluginManager::UnregisterPlugin (CreateInstance); + PluginManager::UnregisterPlugin(CreateInstance); } - const char * ObjectFileELF::GetPluginNameStatic() { - return "object-file.elf32"; + return "object-file.elf"; } const char * ObjectFileELF::GetPluginDescriptionStatic() { - return "ELF object file reader (32-bit)."; + return "ELF object file reader."; } - ObjectFile * -ObjectFileELF::CreateInstance (Module* module, DataBufferSP& dataSP, const FileSpec* file, addr_t offset, addr_t length) +ObjectFileELF::CreateInstance(Module *module, + DataBufferSP &data_sp, + const FileSpec *file, addr_t offset, + addr_t length) { - if (ObjectFileELF::MagicBytesMatch(dataSP)) + if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset)) { - std::auto_ptr<ObjectFile> objfile_ap(new ObjectFileELF (module, dataSP, file, offset, length)); - if (objfile_ap.get() && objfile_ap->ParseHeader()) - return objfile_ap.release(); + 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) + { + std::auto_ptr<ObjectFile> objfile_ap( + new ObjectFileELF(module, data_sp, file, offset, length)); + if (objfile_ap->ParseHeader()) + return objfile_ap.release(); + } + } } return NULL; } -bool -ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp) +//------------------------------------------------------------------ +// PluginInterface protocol +//------------------------------------------------------------------ +const char * +ObjectFileELF::GetPluginName() { - if (data_sp && data_sp->GetByteSize() > EI_PAD) - { - const uint8_t* magic = data_sp->GetBytes(); - if (magic != NULL) - { - bool have_magic = (magic[EI_MAG0] == 0x7f && - magic[EI_MAG1] == 'E' && - magic[EI_MAG2] == 'L' && - magic[EI_MAG3] == 'F'); - - bool have_32bit = magic[EI_CLASS] == ELFCLASS32; - - return have_magic && have_32bit; - } - } - return false; + 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; } +//------------------------------------------------------------------ +// ObjectFile protocol +//------------------------------------------------------------------ -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_shstr_data() +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() { if (file) m_file = *file; - ::bzero (&m_header, sizeof(m_header)); + ::memset(&m_header, 0, sizeof(m_header)); } - ObjectFileELF::~ObjectFileELF() { } ByteOrder -ObjectFileELF::GetByteOrder () const +ObjectFileELF::GetByteOrder() const { if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) return eByteOrderBig; @@ -121,46 +156,120 @@ ObjectFileELF::GetByteOrder () const } size_t -ObjectFileELF::GetAddressByteSize () const +ObjectFileELF::GetAddressByteSize() const { return m_data.GetAddressByteSize(); } -bool -ObjectFileELF::ParseHeader () +unsigned +ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) { - m_data.SetAddressByteSize(4); - uint32_t offset = GetOffset(); - if (m_data.GetU8(&offset, m_header.e_ident, EI_NIDENT) == NULL) - return false; - - m_data.SetByteOrder(GetByteOrder()); - - // Read e_type and e_machine - if (m_data.GetU16(&offset, &m_header.e_type, 2) == NULL) - return false; - - // read e_version, e_entry, e_phoff, e_shoff, e_flags - if (m_data.GetU32(&offset, &m_header.e_version, 5) == NULL) - return false; + return std::distance(m_section_headers.begin(), I) + 1; +} - // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx - if (m_data.GetU16(&offset, &m_header.e_ehsize, 6) == NULL) - return false; +unsigned +ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const +{ + return std::distance(m_section_headers.begin(), I) + 1; +} - return true; +bool +ObjectFileELF::ParseHeader() +{ + uint32_t offset = GetOffset(); + return m_header.Parse(m_data, &offset); } bool -ObjectFileELF::GetUUID (UUID* uuid) +ObjectFileELF::GetUUID(UUID* uuid) { + // FIXME: Return MD5 sum here. See comment in ObjectFile.h. return false; } uint32_t -ObjectFileELF::GetDependentModules(FileSpecList& files) +ObjectFileELF::GetDependentModules(FileSpecList &files) { - return 0; + 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; +} + +//---------------------------------------------------------------------- +// 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; + for (SectionHeaderCollIter I = m_section_headers.begin(); + I != m_section_headers.end(); ++I) + { + if (I->sh_type == SHT_DYNAMIC) + { + dynsym_id = SectionIndex(I); + dynstr_id = I->sh_link + 1; // Section ID's are 1 based. + 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); + m_filespec_ap->Append(FileSpec(lib_name)); + } + } + + return m_filespec_ap->GetSize(); } //---------------------------------------------------------------------- @@ -173,36 +282,38 @@ ObjectFileELF::ParseProgramHeaders() if (!m_program_headers.empty()) return m_program_headers.size(); - uint32_t offset = 0; - if (m_header.e_phnum > 0) - { - m_program_headers.resize(m_header.e_phnum); + // If there are no program headers to read we are done. + if (m_header.e_phnum == 0) + return 0; - if (m_program_headers.size() != m_header.e_phnum) - return 0; + m_program_headers.resize(m_header.e_phnum); + if (m_program_headers.size() != m_header.e_phnum) + return 0; - const size_t byte_size = m_header.e_phnum * m_header.e_phentsize; - DataBufferSP buffer_sp(m_file.ReadFileContents(m_offset + m_header.e_phoff, byte_size)); + 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)); - if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size) - return 0; + if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size) + return 0; - DataExtractor data(buffer_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); + DataExtractor data(buffer_sp, m_data.GetByteOrder(), + m_data.GetAddressByteSize()); - uint32_t idx; - for (idx = 0; idx < m_header.e_phnum; ++idx) - { - if (data.GetU32(&offset, &m_program_headers[idx].p_type, 8) == NULL) - return 0; - } - if (idx < m_program_headers.size()) - m_program_headers.resize(idx); + 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; } + if (idx < m_program_headers.size()) + m_program_headers.resize(idx); + return m_program_headers.size(); } - //---------------------------------------------------------------------- // ParseSectionHeaders //---------------------------------------------------------------------- @@ -213,32 +324,34 @@ ObjectFileELF::ParseSectionHeaders() if (!m_section_headers.empty()) return m_section_headers.size(); - if (m_header.e_shnum > 0) - { - uint32_t offset = 0; - - m_section_headers.resize(m_header.e_shnum); + // If there are no section headers we are done. + if (m_header.e_shnum == 0) + return 0; - if (m_section_headers.size() != m_header.e_shnum) - return 0; + m_section_headers.resize(m_header.e_shnum); + if (m_section_headers.size() != m_header.e_shnum) + return 0; - const size_t byte_size = m_header.e_shnum * m_header.e_shentsize; - DataBufferSP buffer_sp(m_file.ReadFileContents(m_offset + m_header.e_shoff, byte_size)); + 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)); - if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size) - return 0; + if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size) + return 0; - DataExtractor data(buffer_sp, m_data.GetByteOrder(), m_data.GetAddressByteSize()); + DataExtractor data(buffer_sp, + m_data.GetByteOrder(), + m_data.GetAddressByteSize()); - uint32_t idx; - for (idx = 0; idx < m_header.e_shnum; ++idx) - { - if (data.GetU32(&offset, &m_section_headers[idx].sh_name, 10) == NULL) - break; - } - if (idx < m_section_headers.size()) - m_section_headers.resize(idx); + 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; } + if (idx < m_section_headers.size()) + m_section_headers.resize(idx); return m_section_headers.size(); } @@ -248,10 +361,14 @@ ObjectFileELF::GetSectionHeaderStringTable() { if (m_shstr_data.GetByteSize() == 0) { - if (m_header.e_shstrndx && m_header.e_shstrndx < m_section_headers.size()) + const unsigned strtab_idx = m_header.e_shstrndx; + + if (strtab_idx && strtab_idx < m_section_headers.size()) { - const size_t byte_size = m_section_headers[m_header.e_shstrndx].sh_size; - DataBufferSP buffer_sp(m_file.ReadFileContents(m_offset + m_section_headers[m_header.e_shstrndx].sh_offset, byte_size)); + 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)); if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size) return 0; @@ -262,83 +379,78 @@ ObjectFileELF::GetSectionHeaderStringTable() return m_shstr_data.GetByteSize(); } -uint32_t +lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) { - if (ParseSectionHeaders() && GetSectionHeaderStringTable()) + 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) { - uint32_t offset = 1; // Skip leading NULL string at offset 0; - while (m_shstr_data.ValidOffset(offset)) - { - uint32_t sh_name = offset; // Save offset in case we find a match - const char* sectionHeaderName = m_shstr_data.GetCStr(&offset); - if (sectionHeaderName) - { - if (strcmp(name, sectionHeaderName) == 0) - { - SectionHeaderCollIter pos; - for (pos = m_section_headers.begin(); pos != m_section_headers.end(); ++pos) - { - if ( (*pos).sh_name == sh_name ) - { - // section indexes are 1 based - return std::distance(m_section_headers.begin(), pos) + 1; - } - } - return UINT32_MAX; - } - } - else - { - return UINT32_MAX; - } - } + const char *sectionName = m_shstr_data.PeekCStr(I->sh_name); + + if (!sectionName) + return 0; + + if (strcmp(name, sectionName) != 0) + continue; + + return SectionIndex(I); } - return UINT32_MAX; + return 0; } SectionList * ObjectFileELF::GetSectionList() { - if (m_sections_ap.get() == NULL) + if (m_sections_ap.get()) + return m_sections_ap.get(); + + if (ParseSectionHeaders() && GetSectionHeaderStringTable()) { m_sections_ap.reset(new SectionList()); - if (ParseSectionHeaders() && GetSectionHeaderStringTable()) - { - uint32_t sh_idx = 0; - const size_t num_sections = m_section_headers.size(); - for (sh_idx = 0; sh_idx < num_sections; ++sh_idx) - { - ConstString section_name(m_shstr_data.PeekCStr(m_section_headers[sh_idx].sh_name)); - uint64_t section_file_size = m_section_headers[sh_idx].sh_type == SHT_NOBITS ? 0 : m_section_headers[sh_idx].sh_size; - SectionSP section_sp(new Section(NULL, // Parent section - GetModule(), // Module to which this section belongs - sh_idx + 1, // Section ID is the 1 based - section_name, // Name of this section - eSectionTypeOther, // TODO: fill this in appropriately for ELF... - m_section_headers[sh_idx].sh_addr, // File VM address - m_section_headers[sh_idx].sh_size, // VM size in bytes of this section - m_section_headers[sh_idx].sh_offset, // Offset to the data for this section in the file - section_file_size, // Size in bytes of this section as found in the the file - m_section_headers[sh_idx].sh_flags)); // Flags for this section - if (section_sp.get()) - m_sections_ap->AddSection(section_sp); - } + 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; + + SectionSP section(new Section( + 0, // Parent section. + GetModule(), // Module to which this section belongs. + SectionIndex(I), // Section ID. + name, // Section name. + eSectionTypeOther, // FIXME: Fill in as appropriate. + 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. + + m_sections_ap->AddSection(section); } } + return m_sections_ap.get(); } static void -ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symtab_shdr, const DataExtractor& symtab_data, const DataExtractor& strtab_data) +ParseSymbols(Symtab *symtab, SectionList *section_list, + const ELFSectionHeader &symtab_shdr, + const DataExtractor &symtab_data, + const DataExtractor &strtab_data) { - assert (sizeof(Elf32_Sym) == symtab_shdr.sh_entsize); - const uint32_t num_symbols = symtab_data.GetByteSize() / sizeof(Elf32_Sym); + ELFSymbol symbol; uint32_t offset = 0; - Elf32_Sym symbol; - uint32_t i; + const unsigned numSymbols = + symtab_data.GetByteSize() / symtab_shdr.sh_entsize; + static ConstString text_section_name(".text"); static ConstString init_section_name(".init"); static ConstString fini_section_name(".fini"); @@ -351,25 +463,16 @@ ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symta static ConstString data2_section_name(".data1"); static ConstString bss_section_name(".bss"); - for (i=0; i<num_symbols; ++i) + for (unsigned i = 0; i < numSymbols; ++i) { - // if (symtab_data.GetU32(&offset, &symbol.st_name, 3) == 0) - // break; - - if (!symtab_data.ValidOffsetForDataOfSize(offset, sizeof(Elf32_Sym))) + if (symbol.Parse(symtab_data, &offset) == false) break; - symbol.st_name = symtab_data.GetU32 (&offset); - symbol.st_value = symtab_data.GetU32 (&offset); - symbol.st_size = symtab_data.GetU32 (&offset); - symbol.st_info = symtab_data.GetU8 (&offset); - symbol.st_other = symtab_data.GetU8 (&offset); - symbol.st_shndx = symtab_data.GetU16 (&offset); - - Section * symbol_section = NULL; + Section *symbol_section = NULL; SymbolType symbol_type = eSymbolTypeInvalid; + Elf64_Half symbol_idx = symbol.st_shndx; - switch (symbol.st_shndx) + switch (symbol_idx) { case SHN_ABS: symbol_type = eSymbolTypeAbsolute; @@ -378,11 +481,11 @@ ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symta symbol_type = eSymbolTypeUndefined; break; default: - symbol_section = section_list->GetSectionAtIndex (symbol.st_shndx).get(); + symbol_section = section_list->GetSectionAtIndex(symbol_idx).get(); break; } - switch (ELF_ST_BIND (symbol.st_info)) + switch (symbol.getType()) { default: case STT_NOTYPE: @@ -390,13 +493,14 @@ ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symta break; case STT_OBJECT: - // The symbol is associated with a data object, such as a variable, an array, etc. - symbol_type == eSymbolTypeData; + // The symbol is associated with a data object, such as a variable, + // an array, etc. + symbol_type = eSymbolTypeData; break; case STT_FUNC: // The symbol is associated with a function or other executable code. - symbol_type == eSymbolTypeCode; + symbol_type = eSymbolTypeCode; break; case STT_SECTION: @@ -410,7 +514,7 @@ ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symta // 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. - symbol_type == eSymbolTypeObjectFile; + symbol_type = eSymbolTypeObjectFile; break; } @@ -427,12 +531,11 @@ ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symta { symbol_type = eSymbolTypeCode; } - 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) + 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) { symbol_type = eSymbolTypeData; } @@ -443,112 +546,84 @@ ParseSymbols (Symtab *symtab, SectionList *section_list, const Elf32_Shdr &symta if (symbol_section != NULL) symbol_value -= symbol_section->GetFileAddress(); const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); - - 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 - ELF_ST_BIND (symbol.st_info) == STB_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 pointer if symbol_value is an offset within a section, else NULL - symbol_value, // offset from section if section is non-NULL, else the value for this symbol - symbol.st_size, // size in bytes of this symbol - symbol.st_other << 8 | symbol.st_info); // symbol flags + 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. symtab->AddSymbol(dc_symbol); } } +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); + } + } +} Symtab * ObjectFileELF::GetSymtab() { - if (m_symtab_ap.get() == NULL) - { - m_symtab_ap.reset(new Symtab(this)); + if (m_symtab_ap.get()) + return m_symtab_ap.get(); - if (ParseSectionHeaders() && GetSectionHeaderStringTable()) - { - uint32_t symtab_idx = UINT32_MAX; - uint32_t dynsym_idx = UINT32_MAX; - uint32_t sh_idx = 0; - const size_t num_sections = m_section_headers.size(); - for (sh_idx = 0; sh_idx < num_sections; ++sh_idx) - { - if (m_section_headers[sh_idx].sh_type == SHT_SYMTAB) - { - symtab_idx = sh_idx; - break; - } - if (m_section_headers[sh_idx].sh_type == SHT_DYNSYM) - { - dynsym_idx = sh_idx; - } - } + Symtab *symbol_table = new Symtab(this); + m_symtab_ap.reset(symbol_table); - SectionList *section_list = NULL; - static ConstString g_symtab(".symtab"); - static ConstString g_strtab(".strtab"); - static ConstString g_dynsym(".dynsym"); - static ConstString g_dynstr(".dynstr"); - // Check if we found a full symbol table? - if (symtab_idx < num_sections) - { - section_list = GetSectionList(); - if (section_list) - { - Section *symtab_section = section_list->FindSectionByName(g_symtab).get(); - Section *strtab_section = section_list->FindSectionByName(g_strtab).get(); - if (symtab_section && strtab_section) - { - DataExtractor symtab_data; - DataExtractor strtab_data; - if (symtab_section->ReadSectionDataFromObjectFile (this, symtab_data) > 0 && - strtab_section->ReadSectionDataFromObjectFile (this, strtab_data) > 0) - { - ParseSymbols (m_symtab_ap.get(), section_list, m_section_headers[symtab_idx], symtab_data, strtab_data); - } - } - } - } - // Check if we found a reduced symbol table that gets used for dynamic linking? - else if (dynsym_idx < num_sections) - { - section_list = GetSectionList(); - if (section_list) - { - Section *dynsym_section = section_list->FindSectionByName(g_dynsym).get(); - Section *dynstr_section = section_list->FindSectionByName(g_dynstr).get(); - if (dynsym_section && dynstr_section) - { - DataExtractor dynsym_data; - DataExtractor dynstr_data; - if (dynsym_section->ReadSectionDataFromObjectFile (this, dynsym_data) > 0 && - dynstr_section->ReadSectionDataFromObjectFile (this, dynstr_data) > 0) - { - ParseSymbols (m_symtab_ap.get(), section_list, m_section_headers[dynsym_idx], dynsym_data, dynstr_data); - } - } - } - } + 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); + ParseSymbolTable(symbol_table, symtab_section, section_id); } } - return m_symtab_ap.get(); + + return symbol_table; } -// -////---------------------------------------------------------------------- -//// GetNListSymtab -////---------------------------------------------------------------------- -//bool -//ELF32RuntimeFileParser::GetNListSymtab(BinaryDataRef& stabs_data, BinaryDataRef& stabstr_data, bool locals_only, uint32_t& value_size) -//{ -// value_size = 4; // Size in bytes of the nlist n_value member -// return GetSectionInfo(GetSectionIndexByName(".stab"), NULL, NULL, NULL, NULL, NULL, NULL, &stabs_data, NULL) && -// GetSectionInfo(GetSectionIndexByName(".stabstr"), NULL, NULL, NULL, NULL, NULL, NULL, &stabstr_data, NULL); -//} -// //===----------------------------------------------------------------------===// // Dump // @@ -571,6 +646,8 @@ ObjectFileELF::Dump(Stream *s) if (symtab) symtab->Dump(s, NULL); s->EOL(); + DumpDependentModules(s); + s->EOL(); } //---------------------------------------------------------------------- @@ -579,16 +656,19 @@ ObjectFileELF::Dump(Stream *s) // Dump the ELF header to the specified output stream //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFHeader(Stream *s, const Elf32_Ehdr& 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]); +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]); 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]); @@ -597,9 +677,9 @@ ObjectFileELF::DumpELFHeader(Stream *s, const Elf32_Ehdr& header) 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); - s->Printf("e_entry = 0x%8.8x\n", header.e_entry); - s->Printf("e_phoff = 0x%8.8x\n", header.e_phoff); - s->Printf("e_shoff = 0x%8.8x\n", header.e_shoff); + 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); 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); @@ -615,7 +695,7 @@ ObjectFileELF::DumpELFHeader(Stream *s, const Elf32_Ehdr& header) // Dump an token value for the ELF header member e_type //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFHeader_e_type(Stream *s, uint16_t e_type) +ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { switch (e_type) { @@ -635,7 +715,7 @@ ObjectFileELF::DumpELFHeader_e_type(Stream *s, uint16_t e_type) // Dump an token value for the ELF header member e_ident[EI_DATA] //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, uint16_t ei_data) +ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) { switch (ei_data) { @@ -654,10 +734,12 @@ ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, uint16_t ei_data) // Dump a single ELF program header to the specified output stream //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFProgramHeader(Stream *s, const Elf32_Phdr& ph) +ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) { DumpELFProgramHeader_p_type(s, ph.p_type); - s->Printf(" %8.8x %8.8x %8.8x %8.8x %8.8x %8.8x (", ph.p_offset, ph.p_vaddr, ph.p_paddr, ph.p_filesz, ph.p_memsz, ph.p_flags); + 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); + DumpELFProgramHeader_p_flags(s, ph.p_flags); s->Printf(") %8.8x", ph.p_align); } @@ -667,9 +749,9 @@ ObjectFileELF::DumpELFProgramHeader(Stream *s, const Elf32_Phdr& ph) // // Dump an token value for the ELF program header member p_type which // describes the type of the program header -//---------------------------------------------------------------------- +// ---------------------------------------------------------------------- void -ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, Elf32_Word p_type) +ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { const int kStrWidth = 10; switch (p_type) @@ -694,7 +776,7 @@ ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, Elf32_Word p_type) // Dump an token value for the ELF program header member p_flags //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, Elf32_Word p_flags) +ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { *s << ((p_flags & PF_X) ? "PF_X" : " ") << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') @@ -714,36 +796,37 @@ ObjectFileELF::DumpELFProgramHeaders(Stream *s) if (ParseProgramHeaders()) { s->PutCString("Program Headers\n"); - s->PutCString("IDX p_type p_offset p_vaddr p_paddr p_filesz p_memsz p_flags p_align\n"); - s->PutCString("==== ---------- -------- -------- -------- -------- -------- ------------------------- --------\n"); + s->PutCString("IDX p_type p_offset p_vaddr p_paddr " + "p_filesz p_memsz p_flags p_align\n"); + s->PutCString("==== ---------- -------- -------- -------- " + "-------- -------- ------------------------- --------\n"); uint32_t idx = 0; - ProgramHeaderCollConstIter pos; - - for (pos = m_program_headers.begin(); pos != m_program_headers.end(); ++pos, ++idx) + for (ProgramHeaderCollConstIter I = m_program_headers.begin(); + I != m_program_headers.end(); ++I, ++idx) { - s->Printf ("[%2u] ", idx); - ObjectFileELF::DumpELFProgramHeader(s, *pos); + s->Printf("[%2u] ", idx); + ObjectFileELF::DumpELFProgramHeader(s, *I); s->EOL(); } } } - //---------------------------------------------------------------------- // DumpELFSectionHeader // // Dump a single ELF section header to the specified output stream //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFSectionHeader(Stream *s, const Elf32_Shdr& sh) +ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh) { - s->Printf ("%8.8x ", sh.sh_name); + s->Printf("%8.8x ", sh.sh_name); DumpELFSectionHeader_sh_type(s, sh.sh_type); - s->Printf (" %8.8x (", sh.sh_flags); + s->Printf(" %8.8lx (", sh.sh_flags); DumpELFSectionHeader_sh_flags(s, sh.sh_flags); - s->Printf (") %8.8x %8.8x %8.8x %8.8x %8.8x %8.8x %8.8x", - sh.sh_addr, sh.sh_offset, sh.sh_size, sh.sh_link, sh.sh_info, sh.sh_addralign, sh.sh_entsize); + 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); } //---------------------------------------------------------------------- @@ -753,7 +836,7 @@ ObjectFileELF::DumpELFSectionHeader(Stream *s, const Elf32_Shdr& sh) // describes the type of the section //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, Elf32_Word sh_type) +ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { const int kStrWidth = 12; switch (sh_type) @@ -780,14 +863,13 @@ ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, Elf32_Word sh_type) } } - //---------------------------------------------------------------------- // DumpELFSectionHeader_sh_flags // // Dump an token value for the ELF section header member sh_flags //---------------------------------------------------------------------- void -ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, Elf32_Word sh_flags) +ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags) { *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') @@ -795,6 +877,7 @@ ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, Elf32_Word sh_flags) << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); } + //---------------------------------------------------------------------- // DumpELFSectionHeaders // @@ -803,100 +886,80 @@ ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, Elf32_Word sh_flags) void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { - if (ParseSectionHeaders() && GetSectionHeaderStringTable()) + 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) { - s->PutCString("Section Headers\n"); - s->PutCString("IDX name type flags addr offset size link info addralgn entsize Name\n"); - s->PutCString("==== -------- ------------ -------------------------------- -------- -------- -------- -------- -------- -------- -------- ====================\n"); + 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"; + } +} - uint32_t idx = 0; - SectionHeaderCollConstIter pos; +void +ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) +{ + size_t num_modules = ParseDependentModules(); - for (pos = m_section_headers.begin(); pos != m_section_headers.end(); ++pos, ++idx) + if (num_modules > 0) + { + s->PutCString("Dependent Modules:\n"); + for (unsigned i = 0; i < num_modules; ++i) { - s->Printf ("[%2u] ", idx); - ObjectFileELF::DumpELFSectionHeader(s, *pos); - const char* section_name = m_shstr_data.PeekCStr(pos->sh_name); - if (section_name) - *s << ' ' << section_name << "\n"; + const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); + s->Printf(" %s\n", spec.GetFilename().GetCString()); } } } bool -ObjectFileELF::GetTargetTriple (ConstString &target_triple) +ObjectFileELF::GetTargetTriple(ConstString &target_triple) { static ConstString g_target_triple; if (g_target_triple) { target_triple = g_target_triple; + return true; } - else + + std::string triple; + switch (m_header.e_machine) { - std::string triple; - switch (m_header.e_machine) - { - case EM_SPARC: triple.assign("sparc-"); break; - case EM_386: triple.assign("i386-"); break; - case EM_68K: triple.assign("68k-"); break; - case EM_88K: triple.assign("88k-"); break; - case EM_860: triple.assign("i860-"); break; - case EM_MIPS: triple.assign("mips-"); break; - case EM_PPC: triple.assign("powerpc-"); break; - case EM_PPC64: triple.assign("powerpc64-"); break; - case EM_ARM: triple.assign("arm-"); break; - } - // TODO: determine if there is a vendor in the ELF? Default to "linux" for now - triple += "linux-"; - // TODO: determine if there is an OS in the ELF? Default to "gnu" for now - triple += "gnu"; - g_target_triple.SetCString(triple.c_str()); - target_triple = g_target_triple; + default: + assert(false && "Unexpected machine type."); + break; + case EM_SPARC: triple.assign("sparc-"); break; + case EM_386: triple.assign("i386-"); break; + case EM_68K: triple.assign("68k-"); break; + case EM_88K: triple.assign("88k-"); break; + case EM_860: triple.assign("i860-"); break; + case EM_MIPS: triple.assign("mips-"); break; + case EM_PPC: triple.assign("powerpc-"); break; + case EM_PPC64: triple.assign("powerpc64-"); break; + case EM_ARM: triple.assign("arm-"); break; + case EM_X86_64: triple.assign("x86_64-"); break; } - return !target_triple.IsEmpty(); -} - + // TODO: determine if there is a vendor in the ELF? Default to "linux" for now + triple += "linux-"; + // TODO: determine if there is an OS in the ELF? Default to "gnu" for now + triple += "gnu"; + g_target_triple.SetCString(triple.c_str()); + target_triple = g_target_triple; -//------------------------------------------------------------------ -// PluginInterface protocol -//------------------------------------------------------------------ -const char * -ObjectFileELF::GetPluginName() -{ - return "ObjectFileELF"; -} - -const char * -ObjectFileELF::GetShortPluginName() -{ - return GetPluginNameStatic(); -} - -uint32_t -ObjectFileELF::GetPluginVersion() -{ - return 1; -} - -void -ObjectFileELF::GetPluginCommandHelp (const char *command, Stream *strm) -{ -} - -Error -ObjectFileELF::ExecutePluginCommand (Args &command, Stream *strm) -{ - Error error; - error.SetErrorString("No plug-in command are currently supported."); - return error; -} - -Log * -ObjectFileELF::EnablePluginLogging (Stream *strm, Args &command) -{ - return NULL; + return true; } - - diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h index 5d5778c78fb..7c941a70e0e 100644 --- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h +++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h @@ -1,4 +1,4 @@ -//===-- ObjectFileELF.h -----------------------------------------*- C++ -*-===// +//===-- ObjectFileELF.h --------------------------------------- -*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -17,12 +17,14 @@ #include "lldb/Core/FileSpec.h" #include "lldb/Symbol/ObjectFile.h" -#include "elf.h" +#include "ELFHeader.h" -//---------------------------------------------------------------------- -// This class needs to be hidden as eventually belongs in a plugin that -// will export the ObjectFile protocol -//---------------------------------------------------------------------- +//------------------------------------------------------------------------------ +/// @class ObjectFileELF +/// @brief Generic ELF object file reader. +/// +/// This class provides a generic ELF (32/64 bit) reader plugin implementing the +/// ObjectFile protocol. class ObjectFileELF : public lldb_private::ObjectFile { @@ -43,34 +45,49 @@ public: GetPluginDescriptionStatic(); static lldb_private::ObjectFile * - CreateInstance (lldb_private::Module* module, - lldb::DataBufferSP& dataSP, - const lldb_private::FileSpec* file, - lldb::addr_t offset, - lldb::addr_t length); - static bool - MagicBytesMatch (lldb::DataBufferSP& dataSP); - - //------------------------------------------------------------------ - // Member Functions - //------------------------------------------------------------------ - ObjectFileELF (lldb_private::Module* module, + CreateInstance(lldb_private::Module* module, lldb::DataBufferSP& dataSP, const lldb_private::FileSpec* file, lldb::addr_t offset, lldb::addr_t length); + //------------------------------------------------------------------ + // PluginInterface protocol + //------------------------------------------------------------------ + virtual const char * + GetPluginName(); + + virtual const char * + GetShortPluginName(); + + virtual uint32_t + GetPluginVersion(); + + virtual void + GetPluginCommandHelp(const char *command, lldb_private::Stream *strm); + + virtual lldb_private::Error + ExecutePluginCommand(lldb_private::Args &command, + lldb_private::Stream *strm); + + virtual lldb_private::Log * + EnablePluginLogging(lldb_private::Stream *strm, + lldb_private::Args &command); + + //------------------------------------------------------------------ + // ObjectFile Protocol. + //------------------------------------------------------------------ virtual ~ObjectFileELF(); virtual bool - ParseHeader (); + ParseHeader(); virtual lldb::ByteOrder - GetByteOrder () const; + GetByteOrder() const; virtual size_t - GetAddressByteSize () const; + GetAddressByteSize() const; virtual lldb_private::Symtab * GetSymtab(); @@ -79,119 +96,155 @@ public: GetSectionList(); virtual void - Dump (lldb_private::Stream *s); + Dump(lldb_private::Stream *s); virtual bool - GetTargetTriple (lldb_private::ConstString &target_triple); + GetTargetTriple(lldb_private::ConstString &target_triple); virtual bool - GetUUID (lldb_private::UUID* uuid); + GetUUID(lldb_private::UUID* uuid); virtual uint32_t GetDependentModules(lldb_private::FileSpecList& files); - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - virtual const char * - GetPluginName(); - - virtual const char * - GetShortPluginName(); - - virtual uint32_t - GetPluginVersion(); - - virtual void - GetPluginCommandHelp (const char *command, lldb_private::Stream *strm); - - virtual lldb_private::Error - ExecutePluginCommand (lldb_private::Args &command, lldb_private::Stream *strm); - - virtual lldb_private::Log * - EnablePluginLogging (lldb_private::Stream *strm, lldb_private::Args &command); - - +private: + ObjectFileELF(lldb_private::Module* module, + lldb::DataBufferSP& dataSP, + const lldb_private::FileSpec* file, + lldb::addr_t offset, + lldb::addr_t length); -protected: - typedef std::vector<Elf32_Phdr> ProgramHeaderColl; + typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl; typedef ProgramHeaderColl::iterator ProgramHeaderCollIter; typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter; - typedef std::vector<Elf32_Shdr> SectionHeaderColl; + typedef std::vector<elf::ELFSectionHeader> SectionHeaderColl; typedef SectionHeaderColl::iterator SectionHeaderCollIter; typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; - Elf32_Ehdr m_header; + /// Version of this reader common to all plugins based on this class. + static const uint32_t m_plugin_version = 1; + + /// ELF file header. + elf::ELFHeader m_header; + + /// Collection of program headers. ProgramHeaderColl m_program_headers; + + /// Collection of section headers. SectionHeaderColl m_section_headers; + + /// List of sections present in this ELF object file. mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap; + + /// Table of all non-dynamic symbols present in this object file. mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap; + + /// List of file specifications corresponding to the modules (shared + /// libraries) on which this object file depends. + mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap; + + /// Data extractor holding the string table used to resolve section names. lldb_private::DataExtractor m_shstr_data; + /// Returns a 1 based index of the given section header. + unsigned + SectionIndex(const SectionHeaderCollIter &I); + + /// Returns a 1 based index of the given section header. + unsigned + SectionIndex(const SectionHeaderCollConstIter &I) const; + + /// Parses all section headers present in this object file and populates + /// m_program_headers. This method will compute the header list only once. + /// Returns the number of headers parsed. size_t - ParseSections (); + ParseProgramHeaders(); + /// Parses all section headers present in this object file and populates + /// m_section_headers. This method will compute the header list only once. + /// Returns the number of headers parsed. size_t - ParseSymtab (bool minimize); + ParseSectionHeaders(); -private: + /// Scans the dynamic section and locates all dependent modules (shared + /// libaries) populating m_filespec_ap. This method will compute the + /// dependent module list only once. Returns the number of dependent + /// modules parsed. + size_t + ParseDependentModules(); + + /// Populates m_symtab_ap will all non-dynamic linker symbols. This method + /// will parse the symbols only once. Returns the number of symbols parsed. + void + ParseSymbolTable(lldb_private::Symtab *symbol_table, + const elf::ELFSectionHeader &symtab_section, + lldb::user_id_t symtab_id); + + /// Loads the section name string table into m_shstr_data. Returns the + /// number of bytes constituting the table. + size_t + GetSectionHeaderStringTable(); - // ELF header dump routines + /// Utility method for looking up a section given its name. Returns the + /// index of the corresponding section or zero if no section with the given + /// name can be found (note that section indices are always 1 based, and so + /// section index 0 is never valid). + lldb::user_id_t + GetSectionIndexByName(const char *name); + + /// @name ELF header dump routines + //@{ static void - DumpELFHeader (lldb_private::Stream *s, - const Elf32_Ehdr& header); + DumpELFHeader(lldb_private::Stream *s, const elf::ELFHeader& header); static void - DumpELFHeader_e_ident_EI_DATA (lldb_private::Stream *s, - uint16_t ei_data); + DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s, + unsigned char ei_data); + static void - DumpELFHeader_e_type (lldb_private::Stream *s, - uint16_t e_type); + DumpELFHeader_e_type(lldb_private::Stream *s, elf::elf_half e_type); + //@} - // ELF program header dump routines + /// @name ELF program header dump routines + //@{ void - DumpELFProgramHeaders (lldb_private::Stream *s); + DumpELFProgramHeaders(lldb_private::Stream *s); static void - DumpELFProgramHeader (lldb_private::Stream *s, - const Elf32_Phdr& ph); + DumpELFProgramHeader(lldb_private::Stream *s, + const elf::ELFProgramHeader &ph); static void - DumpELFProgramHeader_p_type (lldb_private::Stream *s, - Elf32_Word p_type); + DumpELFProgramHeader_p_type(lldb_private::Stream *s, elf::elf_word p_type); static void - DumpELFProgramHeader_p_flags (lldb_private::Stream *s, - Elf32_Word p_flags); + DumpELFProgramHeader_p_flags(lldb_private::Stream *s, + elf::elf_word p_flags); + //@} - // ELF section header dump routines + /// @name ELF section header dump routines + //@{ void - DumpELFSectionHeaders (lldb_private::Stream *s); + DumpELFSectionHeaders(lldb_private::Stream *s); static void - DumpELFSectionHeader (lldb_private::Stream *s, - const Elf32_Shdr& sh); + DumpELFSectionHeader(lldb_private::Stream *s, + const elf::ELFSectionHeader& sh); static void - DumpELFSectionHeader_sh_type (lldb_private::Stream *s, - Elf32_Word sh_type); + DumpELFSectionHeader_sh_type(lldb_private::Stream *s, + elf::elf_word sh_type); static void - DumpELFSectionHeader_sh_flags (lldb_private::Stream *s, - Elf32_Word sh_flags); - - size_t - ParseProgramHeaders (); + DumpELFSectionHeader_sh_flags(lldb_private::Stream *s, + elf::elf_word sh_flags); + //@} - size_t - ParseSectionHeaders (); - - size_t - GetSectionHeaderStringTable (); + /// ELF dependent module dump routine. + void + DumpDependentModules(lldb_private::Stream *s); - uint32_t - GetSectionIndexByName (const char *name); }; #endif // #ifndef liblldb_ObjectFileELF_h_ diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.cpp deleted file mode 100644 index df764889a13..00000000000 --- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.cpp +++ /dev/null @@ -1,1017 +0,0 @@ -//===-- ObjectFileELF64.cpp ----------------------------------- -*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "ObjectFileELF64.h" - -#include <cassert> - -#include <algorithm> - -#include "lldb/Core/DataBuffer.h" -#include "lldb/Core/Error.h" -#include "lldb/Core/FileSpecList.h" -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/Section.h" -#include "lldb/Core/Stream.h" - -#define CASE_AND_STREAM(s, def, width) \ - case def: s->Printf("%-*s", width, #def); break; - -using namespace lldb; -using namespace lldb_private; - -void -ObjectFileELF64::Initialize() -{ - PluginManager::RegisterPlugin(GetPluginNameStatic(), - GetPluginDescriptionStatic(), - CreateInstance); -} - -void -ObjectFileELF64::Terminate() -{ - PluginManager::UnregisterPlugin(CreateInstance); -} - -const char * -ObjectFileELF64::GetPluginNameStatic() -{ - return "object-file.elf64"; -} - -const char * -ObjectFileELF64::GetPluginDescriptionStatic() -{ - return "ELF object file reader (64-bit)."; -} - -ObjectFile * -ObjectFileELF64::CreateInstance(Module *module, - DataBufferSP &dataSP, - const FileSpec *file, addr_t offset, - addr_t length) -{ - if (ObjectFileELF64::MagicBytesMatch(dataSP)) - { - std::auto_ptr<ObjectFile> objfile_ap( - new ObjectFileELF64(module, dataSP, file, offset, length)); - if (objfile_ap->ParseHeader()) - return objfile_ap.release(); - } - return NULL; -} - -bool -ObjectFileELF64::MagicBytesMatch(DataBufferSP& data_sp) -{ - if (data_sp && data_sp->GetByteSize() > EI_PAD) - { - const uint8_t* magic = data_sp->GetBytes(); - if (magic != NULL) - { - bool have_magic = (magic[EI_MAG0] == 0x7f && - magic[EI_MAG1] == 'E' && - magic[EI_MAG2] == 'L' && - magic[EI_MAG3] == 'F'); - - bool have_64bit = magic[EI_CLASS] == ELFCLASS64; - - return have_magic && have_64bit; - } - } - return false; -} - -ObjectFileELF64::ObjectFileELF64(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() -{ - if (file) - m_file = *file; - ::memset(&m_header, 0, sizeof(m_header)); -} - - -ObjectFileELF64::~ObjectFileELF64() -{ -} - -ByteOrder -ObjectFileELF64::GetByteOrder() const -{ - if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) - return eByteOrderBig; - if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) - return eByteOrderLittle; - return eByteOrderInvalid; -} - -size_t -ObjectFileELF64::GetAddressByteSize() const -{ - return m_data.GetAddressByteSize(); -} - -unsigned -ObjectFileELF64::SectionIndex(const SectionHeaderCollIter &I) -{ - return std::distance(m_section_headers.begin(), I) + 1; -} - -unsigned -ObjectFileELF64::SectionIndex(const SectionHeaderCollConstIter &I) const -{ - return std::distance(m_section_headers.begin(), I) + 1; -} - -bool -ObjectFileELF64::ParseHeader() -{ - m_data.SetAddressByteSize(8); - uint32_t offset = GetOffset(); - if (m_data.GetU8(&offset, m_header.e_ident, EI_NIDENT) == NULL) - return false; - - m_data.SetByteOrder(GetByteOrder()); - - // Read e_type and e_machine. - if (m_data.GetU16(&offset, &m_header.e_type, 2) == NULL) - return false; - - // Read e_version. - if (m_data.GetU32(&offset, &m_header.e_version, 1) == NULL) - return false; - - // Read e_entry, e_phoff and e_shoff. - if (m_data.GetU64(&offset, &m_header.e_entry, 3) == NULL) - return false; - - // Read e_flags. - if (m_data.GetU32(&offset, &m_header.e_flags, 1) == NULL) - return false; - - // Read e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum and e_shstrndx. - if (m_data.GetU16(&offset, &m_header.e_ehsize, 6) == NULL) - return false; - - return true; -} - -bool -ObjectFileELF64::GetUUID(UUID* uuid) -{ - // FIXME: Return MD5 sum here. See comment in ObjectFile.h. - return false; -} - -uint32_t -ObjectFileELF64::GetDependentModules(FileSpecList &files) -{ - 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; -} - -//---------------------------------------------------------------------- -// ParseDependentModules -//---------------------------------------------------------------------- -size_t -ObjectFileELF64::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; - for (SectionHeaderCollIter I = m_section_headers.begin(); - I != m_section_headers.end(); ++I) - { - if (I->sh_type == SHT_DYNAMIC) - { - dynsym_id = SectionIndex(I); - dynstr_id = I->sh_link + 1; // Section ID's are 1 based. - 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)) - { - Elf64_Dyn symbol; - const unsigned num_syms = dynsym_data.GetByteSize() / sizeof(Elf64_Dyn); - unsigned offset = 0; - - // The only type of entries we are concerned with are tagged DT_NEEDED, - // yielding the name of a required library. - for (unsigned i = 0; i < num_syms; ++i) - { - if (!dynsym_data.ValidOffsetForDataOfSize(offset, sizeof(Elf64_Dyn))) - break; - - symbol.d_tag = dynsym_data.GetU64(&offset); - symbol.d_un.d_val = dynsym_data.GetU64(&offset); - - if (symbol.d_tag != DT_NEEDED) - continue; - - const char *lib_name = dynstr_data.PeekCStr(symbol.d_un.d_val); - m_filespec_ap->Append(FileSpec(lib_name)); - } - } - - return m_filespec_ap->GetSize(); -} - -//---------------------------------------------------------------------- -// ParseProgramHeaders -//---------------------------------------------------------------------- -size_t -ObjectFileELF64::ParseProgramHeaders() -{ - // We have already parsed the program headers - if (!m_program_headers.empty()) - return m_program_headers.size(); - - // If there are no program headers to read we are done. - if (m_header.e_phnum == 0) - return 0; - - m_program_headers.resize(m_header.e_phnum); - if (m_program_headers.size() != m_header.e_phnum) - return 0; - - const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; - const Elf64_Off ph_offset = m_offset + m_header.e_phoff; - DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size)); - - if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size) - return 0; - - DataExtractor data(buffer_sp, m_data.GetByteOrder(), - m_data.GetAddressByteSize()); - - uint32_t idx; - uint32_t offset; - for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) - { - if (data.GetU32(&offset, &m_program_headers[idx], 8) == NULL) - return 0; - } - - if (idx < m_program_headers.size()) - m_program_headers.resize(idx); - - return m_program_headers.size(); -} - -//---------------------------------------------------------------------- -// ParseSectionHeaders -//---------------------------------------------------------------------- -size_t -ObjectFileELF64::ParseSectionHeaders() -{ - // We have already parsed the section headers - if (!m_section_headers.empty()) - return m_section_headers.size(); - - // If there are no section headers we are done. - if (m_header.e_shnum == 0) - return 0; - - m_section_headers.resize(m_header.e_shnum); - if (m_section_headers.size() != m_header.e_shnum) - return 0; - - const size_t sh_size = m_header.e_shnum * m_header.e_shentsize; - const Elf64_Off sh_offset = m_offset + m_header.e_shoff; - DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size)); - - if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size) - return 0; - - DataExtractor data(buffer_sp, - m_data.GetByteOrder(), - m_data.GetAddressByteSize()); - - uint32_t idx; - uint32_t offset; - for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx) - { - // Read sh_name and sh_type. - if (data.GetU32(&offset, &m_section_headers[idx].sh_name, 2) == NULL) - break; - - // Read sh_flags, sh_addr, sh_offset and sh_size. - if (data.GetU64(&offset, &m_section_headers[idx].sh_flags, 4) == NULL) - break; - - // Read sh_link and sh_info. - if (data.GetU32(&offset, &m_section_headers[idx].sh_link, 2) == NULL) - break; - - // Read sh_addralign and sh_entsize. - if (data.GetU64(&offset, &m_section_headers[idx].sh_addralign, 2) == NULL) - break; - } - if (idx < m_section_headers.size()) - m_section_headers.resize(idx); - - return m_section_headers.size(); -} - -size_t -ObjectFileELF64::GetSectionHeaderStringTable() -{ - if (m_shstr_data.GetByteSize() == 0) - { - if (m_header.e_shstrndx && m_header.e_shstrndx < m_section_headers.size()) - { - const Elf64_Shdr &sheader = m_section_headers[m_header.e_shstrndx]; - 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)); - - if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size) - return 0; - - m_shstr_data.SetData(buffer_sp); - } - } - return m_shstr_data.GetByteSize(); -} - -uint32_t -ObjectFileELF64::GetSectionIndexByName(const char *name) -{ - if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) - return UINT32_MAX; - - // 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) - { - const char *sectionName = m_shstr_data.PeekCStr(I->sh_name); - - if (!sectionName) - return UINT32_MAX; - - if (strcmp(name, sectionName) != 0) - continue; - - return SectionIndex(I); - } - - return UINT32_MAX; -} - -SectionList * -ObjectFileELF64::GetSectionList() -{ - if (m_sections_ap.get()) - return m_sections_ap.get(); - - if (ParseSectionHeaders() && GetSectionHeaderStringTable()) - { - m_sections_ap.reset(new SectionList()); - - for (SectionHeaderCollIter I = m_section_headers.begin(); - I != m_section_headers.end(); ++I) - { - const Elf64_Shdr &header = *I; - - ConstString name(m_shstr_data.PeekCStr(header.sh_name)); - uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; - - SectionSP section(new Section( - 0, // Parent section. - GetModule(), // Module to which this section belongs. - SectionIndex(I), // Section ID. - name, // Section name. - eSectionTypeOther, // FIXME: Fill in as appropriate. - 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. - - m_sections_ap->AddSection(section); - } - } - - return m_sections_ap.get(); -} - -static void -ParseSymbols(Symtab *symtab, SectionList *section_list, - const Elf64_Shdr &symtab_shdr, - const DataExtractor &symtab_data, - const DataExtractor &strtab_data) -{ - assert (sizeof(Elf64_Sym) == symtab_shdr.sh_entsize); - const unsigned numSymbols = symtab_data.GetByteSize() / sizeof(Elf64_Sym); - unsigned offset = 0; - Elf64_Sym symbol; - - 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"); - - for (unsigned i = 0; i < numSymbols; ++i) - { - if (!symtab_data.ValidOffsetForDataOfSize(offset, sizeof(Elf64_Sym))) - break; - - symbol.st_name = symtab_data.GetU32(&offset); - symbol.st_info = symtab_data.GetU8(&offset); - symbol.st_other = symtab_data.GetU8(&offset); - symbol.st_shndx = symtab_data.GetU16(&offset); - symbol.st_value = symtab_data.GetU64(&offset); - symbol.st_size = symtab_data.GetU64(&offset); - - Section *symbol_section = NULL; - SymbolType symbol_type = eSymbolTypeInvalid; - Elf64_Half symbol_idx = symbol.st_shndx; - - switch (symbol_idx) - { - case SHN_ABS: - symbol_type = eSymbolTypeAbsolute; - break; - case SHN_UNDEF: - symbol_type = eSymbolTypeUndefined; - break; - default: - symbol_section = section_list->GetSectionAtIndex(symbol_idx).get(); - break; - } - - switch (ELF_ST_TYPE(symbol.st_info)) - { - default: - case STT_NOTYPE: - // The symbol's type is not specified. - break; - - case STT_OBJECT: - // The symbol is associated with a data object, such as a variable, - // an array, etc. - symbol_type = eSymbolTypeData; - break; - - case STT_FUNC: - // The symbol is associated with a function or other executable code. - symbol_type = eSymbolTypeCode; - 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. - symbol_type = eSymbolTypeObjectFile; - 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; - } - 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) - { - 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); - bool is_global = ELF_ST_BIND(symbol.st_info) == 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. - symtab->AddSymbol(dc_symbol); - } -} - -void -ObjectFileELF64::ParseSymbolTable(Symtab *symbol_table, - const Elf64_Shdr &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); - } - } -} - -Symtab * -ObjectFileELF64::GetSymtab() -{ - if (m_symtab_ap.get()) - return m_symtab_ap.get(); - - Symtab *symbol_table = new Symtab(this); - m_symtab_ap.reset(symbol_table); - - 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 || I->sh_type == SHT_DYNSYM) - { - const Elf64_Shdr &symtab_section = *I; - user_id_t section_id = SectionIndex(I); - ParseSymbolTable(symbol_table, symtab_section, section_id); - } - } - - return symbol_table; -} - -//===----------------------------------------------------------------------===// -// Dump -// -// Dump the specifics of the runtime file container (such as any headers -// segments, sections, etc). -// ---------------------------------------------------------------------- -void -ObjectFileELF64::Dump(Stream *s) -{ - DumpELFHeader(s, m_header); - s->EOL(); - DumpELFProgramHeaders(s); - s->EOL(); - DumpELFSectionHeaders(s); - s->EOL(); - SectionList *section_list = GetSectionList(); - if (section_list) - section_list->Dump(s, NULL, true); - Symtab *symtab = GetSymtab(); - if (symtab) - symtab->Dump(s, NULL); - s->EOL(); - DumpDependentModules(s); - s->EOL(); -} - -//---------------------------------------------------------------------- -// DumpELFHeader -// -// Dump the ELF header to the specified output stream -//---------------------------------------------------------------------- -void -ObjectFileELF64::DumpELFHeader(Stream *s, const Elf64_Ehdr& 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]); - 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); - s->Printf("e_entry = 0x%8.8x\n", header.e_entry); - s->Printf("e_phoff = 0x%8.8x\n", header.e_phoff); - s->Printf("e_shoff = 0x%8.8x\n", header.e_shoff); - 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 -ObjectFileELF64::DumpELFHeader_e_type(Stream *s, Elf64_Half e_type) -{ - 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 -ObjectFileELF64::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) -{ - 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 -ObjectFileELF64::DumpELFProgramHeader(Stream *s, const Elf64_Phdr &ph) -{ - DumpELFProgramHeader_p_type(s, ph.p_type); - s->Printf(" %8.8x %8.8x %8.8x %8.8x %8.8x %8.8x (", - ph.p_offset, ph.p_vaddr, ph.p_paddr, ph.p_filesz, ph.p_memsz, - ph.p_flags); - 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 -// ---------------------------------------------------------------------- -void -ObjectFileELF64::DumpELFProgramHeader_p_type(Stream *s, Elf64_Word p_type) -{ - 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 -ObjectFileELF64::DumpELFProgramHeader_p_flags(Stream *s, Elf64_Word p_flags) -{ - *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 -ObjectFileELF64::DumpELFProgramHeaders(Stream *s) -{ - if (ParseProgramHeaders()) - { - s->PutCString("Program Headers\n"); - s->PutCString("IDX p_type p_offset p_vaddr p_paddr " - "p_filesz p_memsz p_flags p_align\n"); - s->PutCString("==== ---------- -------- -------- -------- " - "-------- -------- ------------------------- --------\n"); - - uint32_t idx = 0; - for (ProgramHeaderCollConstIter I = m_program_headers.begin(); - I != m_program_headers.end(); ++I, ++idx) - { - s->Printf("[%2u] ", idx); - ObjectFileELF64::DumpELFProgramHeader(s, *I); - s->EOL(); - } - } -} - -//---------------------------------------------------------------------- -// DumpELFSectionHeader -// -// Dump a single ELF section header to the specified output stream -//---------------------------------------------------------------------- -void -ObjectFileELF64::DumpELFSectionHeader(Stream *s, const Elf64_Shdr &sh) -{ - s->Printf("%8.8x ", sh.sh_name); - DumpELFSectionHeader_sh_type(s, sh.sh_type); - s->Printf(" %8.8x (", sh.sh_flags); - DumpELFSectionHeader_sh_flags(s, sh.sh_flags); - s->Printf(") %8.8x %8.8x %8.8x %8.8x %8.8x %8.8x %8.8x", - sh.sh_addr, sh.sh_offset, sh.sh_size, sh.sh_link, sh.sh_info, - sh.sh_addralign, sh.sh_entsize); -} - -//---------------------------------------------------------------------- -// DumpELFSectionHeader_sh_type -// -// Dump an token value for the ELF section header member sh_type which -// describes the type of the section -//---------------------------------------------------------------------- -void -ObjectFileELF64::DumpELFSectionHeader_sh_type(Stream *s, Elf64_Word sh_type) -{ - 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 -ObjectFileELF64::DumpELFSectionHeader_sh_flags(Stream *s, Elf64_Word sh_flags) -{ - *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" : " "); -} - -//---------------------------------------------------------------------- -// DumpELFSectionHeaders -// -// Dump all of the ELF section header to the specified output stream -//---------------------------------------------------------------------- -void -ObjectFileELF64::DumpELFSectionHeaders(Stream *s) -{ - 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) - { - s->Printf("[%2u] ", idx); - ObjectFileELF64::DumpELFSectionHeader(s, *I); - const char* section_name = m_shstr_data.PeekCStr(I->sh_name); - if (section_name) - *s << ' ' << section_name << "\n"; - } -} - -void -ObjectFileELF64::DumpDependentModules(lldb_private::Stream *s) -{ - size_t num_modules = ParseDependentModules(); - - if (num_modules > 0) - { - s->PutCString("Dependent Modules:\n"); - for (unsigned i = 0; i < num_modules; ++i) - { - const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); - s->Printf(" %s\n", spec.GetFilename().GetCString()); - } - } -} - -bool -ObjectFileELF64::GetTargetTriple(ConstString &target_triple) -{ - static ConstString g_target_triple; - - if (g_target_triple) - { - target_triple = g_target_triple; - return true; - } - - std::string triple; - switch (m_header.e_machine) - { - default: - assert(false && "Unexpected machine type."); - break; - case EM_SPARC: triple.assign("sparc-"); break; - case EM_386: triple.assign("i386-"); break; - case EM_68K: triple.assign("68k-"); break; - case EM_88K: triple.assign("88k-"); break; - case EM_860: triple.assign("i860-"); break; - case EM_MIPS: triple.assign("mips-"); break; - case EM_PPC: triple.assign("powerpc-"); break; - case EM_PPC64: triple.assign("powerpc64-"); break; - case EM_ARM: triple.assign("arm-"); break; - case EM_X86_64: triple.assign("x86_64-"); break; - } - // TODO: determine if there is a vendor in the ELF? Default to "linux" for now - triple += "linux-"; - // TODO: determine if there is an OS in the ELF? Default to "gnu" for now - triple += "gnu"; - g_target_triple.SetCString(triple.c_str()); - target_triple = g_target_triple; - - return true; -} - -//------------------------------------------------------------------ -// PluginInterface protocol -//------------------------------------------------------------------ -const char * -ObjectFileELF64::GetPluginName() -{ - return "ObjectFileELF64"; -} - -const char * -ObjectFileELF64::GetShortPluginName() -{ - return GetPluginNameStatic(); -} - -uint32_t -ObjectFileELF64::GetPluginVersion() -{ - return 1; -} - -void -ObjectFileELF64::GetPluginCommandHelp (const char *command, Stream *strm) -{ -} - -Error -ObjectFileELF64::ExecutePluginCommand (Args &command, Stream *strm) -{ - Error error; - error.SetErrorString("No plug-in commands are currently supported."); - return error; -} - -Log * -ObjectFileELF64::EnablePluginLogging (Stream *strm, Args &command) -{ - return NULL; -} diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.h b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.h deleted file mode 100644 index 939c78a3997..00000000000 --- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF64.h +++ /dev/null @@ -1,209 +0,0 @@ -//===-- ObjectFileELF64.h ------------------------------------- -*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_ObjectFileELF64_h_ -#define liblldb_ObjectFileELF64_h_ - -#include <stdint.h> -#include <vector> - -#include "lldb/lldb-private.h" -#include "lldb/Core/FileSpec.h" -#include "lldb/Symbol/ObjectFile.h" - -#include "elf.h" - -//---------------------------------------------------------------------- -// This class needs to be hidden as eventually belongs in a plugin that -// will export the ObjectFile protocol -//---------------------------------------------------------------------- -class ObjectFileELF64 : - public lldb_private::ObjectFile -{ -public: - //------------------------------------------------------------------ - // Static Functions - //------------------------------------------------------------------ - static void - Initialize(); - - static void - Terminate(); - - static const char * - GetPluginNameStatic(); - - static const char * - GetPluginDescriptionStatic(); - - static lldb_private::ObjectFile * - CreateInstance(lldb_private::Module* module, - lldb::DataBufferSP& dataSP, - const lldb_private::FileSpec* file, - lldb::addr_t offset, - lldb::addr_t length); - - static bool - MagicBytesMatch(lldb::DataBufferSP& dataSP); - - //------------------------------------------------------------------ - // Member Functions - //------------------------------------------------------------------ - ObjectFileELF64(lldb_private::Module* module, - lldb::DataBufferSP& dataSP, - const lldb_private::FileSpec* file, - lldb::addr_t offset, - lldb::addr_t length); - - virtual - ~ObjectFileELF64(); - - virtual bool - ParseHeader(); - - virtual lldb::ByteOrder - GetByteOrder() const; - - virtual size_t - GetAddressByteSize() const; - - virtual lldb_private::Symtab * - GetSymtab(); - - virtual lldb_private::SectionList * - GetSectionList(); - - virtual void - Dump(lldb_private::Stream *s); - - virtual bool - GetTargetTriple(lldb_private::ConstString &target_triple); - - virtual bool - GetUUID(lldb_private::UUID* uuid); - - virtual uint32_t - GetDependentModules(lldb_private::FileSpecList& files); - - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - virtual const char * - GetPluginName(); - - virtual const char * - GetShortPluginName(); - - virtual uint32_t - GetPluginVersion(); - - virtual void - GetPluginCommandHelp(const char *command, lldb_private::Stream *strm); - - virtual lldb_private::Error - ExecutePluginCommand(lldb_private::Args &command, - lldb_private::Stream *strm); - - virtual lldb_private::Log * - EnablePluginLogging(lldb_private::Stream *strm, - lldb_private::Args &command); - -protected: - typedef std::vector<Elf64_Phdr> ProgramHeaderColl; - typedef ProgramHeaderColl::iterator ProgramHeaderCollIter; - typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter; - - typedef std::vector<Elf64_Shdr> SectionHeaderColl; - typedef SectionHeaderColl::iterator SectionHeaderCollIter; - typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter; - - Elf64_Ehdr m_header; - ProgramHeaderColl m_program_headers; - SectionHeaderColl m_section_headers; - mutable std::auto_ptr<lldb_private::SectionList> m_sections_ap; - mutable std::auto_ptr<lldb_private::Symtab> m_symtab_ap; - mutable std::auto_ptr<lldb_private::FileSpecList> m_filespec_ap; - lldb_private::DataExtractor m_shstr_data; - - size_t - ParseSections(); - - size_t - ParseSymtab(bool minimize); - -private: - // Returns the 1 based index of a section header. - unsigned - SectionIndex(const SectionHeaderCollIter &I); - - unsigned - SectionIndex(const SectionHeaderCollConstIter &I) const; - - // ELF header dump routines - static void - DumpELFHeader(lldb_private::Stream *s, const Elf64_Ehdr& header); - - static void - DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s, - unsigned char ei_data); - - static void - DumpELFHeader_e_type(lldb_private::Stream *s, Elf64_Half e_type); - - // ELF program header dump routines - void - DumpELFProgramHeaders(lldb_private::Stream *s); - - static void - DumpELFProgramHeader(lldb_private::Stream *s, const Elf64_Phdr &ph); - - static void - DumpELFProgramHeader_p_type(lldb_private::Stream *s, Elf64_Word p_type); - - static void - DumpELFProgramHeader_p_flags(lldb_private::Stream *s, Elf64_Word p_flags); - - // ELF section header dump routines - void - DumpELFSectionHeaders(lldb_private::Stream *s); - - static void - DumpELFSectionHeader(lldb_private::Stream *s, const Elf64_Shdr& sh); - - static void - DumpELFSectionHeader_sh_type(lldb_private::Stream *s, Elf64_Word sh_type); - - static void - DumpELFSectionHeader_sh_flags(lldb_private::Stream *s, Elf64_Word sh_flags); - - void - DumpDependentModules(lldb_private::Stream *s); - - size_t - ParseProgramHeaders(); - - size_t - ParseSectionHeaders(); - - size_t - ParseDependentModules(); - - void - ParseSymbolTable(lldb_private::Symtab *symbol_table, - const Elf64_Shdr &symtab_section, - lldb::user_id_t symtab_id); - - size_t - GetSectionHeaderStringTable(); - - uint32_t - GetSectionIndexByName(const char *name); -}; - -#endif // #ifndef liblldb_ObjectFileELF64_h_ |