Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,14 @@
add_lldb_library(lldbPluginObjectFileELF PLUGIN
ELFHeader.cpp
ObjectFileELF.cpp
LINK_LIBS
lldbCore
lldbHost
lldbSymbol
lldbTarget
LINK_COMPONENTS
BinaryFormat
Object
Support
)

View File

@@ -0,0 +1,448 @@
//===-- 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/Section.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Stream.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,
lldb::offset_t *offset, uint64_t *value,
uint32_t byte_size) {
const lldb::offset_t saved_offset = *offset;
*value = data.GetMaxU64(offset, byte_size);
return *offset != saved_offset;
}
static bool GetMaxU64(const lldb_private::DataExtractor &data,
lldb::offset_t *offset, uint64_t *value,
uint32_t byte_size, uint32_t count) {
lldb::offset_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,
lldb::offset_t *offset, int64_t *value,
uint32_t byte_size) {
const lldb::offset_t saved_offset = *offset;
*value = data.GetMaxS64(offset, byte_size);
return *offset != saved_offset;
}
static bool GetMaxS64(const lldb_private::DataExtractor &data,
lldb::offset_t *offset, int64_t *value,
uint32_t byte_size, uint32_t count) {
lldb::offset_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::HasHeaderExtension() const {
bool result = false;
// Check if any of these values looks like sentinel.
result |= e_phnum_hdr == 0xFFFF; // PN_XNUM
result |= e_shnum_hdr == SHN_UNDEF;
result |= e_shstrndx_hdr == SHN_XINDEX;
// If header extension is present, the section offset cannot be null.
result &= e_shoff != 0;
// Done.
return result;
}
void ELFHeader::ParseHeaderExtension(lldb_private::DataExtractor &data) {
// Extract section #0 header.
ELFSectionHeader section_zero;
lldb::offset_t offset = 0;
lldb_private::DataExtractor sh_data(data, e_shoff, e_shentsize);
bool ok = section_zero.Parse(sh_data, &offset);
// If we succeeded, fix the header.
if (ok) {
if (e_phnum_hdr == 0xFFFF) // PN_XNUM
e_phnum = section_zero.sh_info;
if (e_shnum_hdr == SHN_UNDEF)
e_shnum = section_zero.sh_size;
if (e_shstrndx_hdr == SHN_XINDEX)
e_shstrndx = section_zero.sh_link;
}
}
bool ELFHeader::Parse(lldb_private::DataExtractor &data,
lldb::offset_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;
// Initialize e_phnum, e_shnum, and e_shstrndx with the values
// read from the header.
e_phnum = e_phnum_hdr;
e_shnum = e_shnum_hdr;
e_shstrndx = e_shstrndx_hdr;
// See if we have extended header in section #0.
if (HasHeaderExtension())
ParseHeaderExtension(data);
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;
}
unsigned ELFHeader::GetRelocationJumpSlotType() const {
unsigned slot = 0;
switch (e_machine) {
default:
assert(false && "architecture not supported");
break;
case EM_PPC:
slot = R_PPC_JMP_SLOT;
break;
case EM_PPC64:
slot = R_PPC64_JMP_SLOT;
break;
case EM_386:
case EM_IAMCU: // FIXME: is this correct?
slot = R_386_JUMP_SLOT;
break;
case EM_X86_64:
slot = R_X86_64_JUMP_SLOT;
break;
case EM_ARM:
slot = R_ARM_JUMP_SLOT;
break;
case EM_HEXAGON:
slot = R_HEX_JMP_SLOT;
break;
case EM_AARCH64:
slot = R_AARCH64_JUMP_SLOT;
break;
case EM_MIPS:
slot = R_MIPS_JUMP_SLOT;
break;
case EM_S390:
slot = R_390_JMP_SLOT;
break;
}
return slot;
}
//------------------------------------------------------------------------------
// ELFSectionHeader
ELFSectionHeader::ELFSectionHeader() {
memset(this, 0, sizeof(ELFSectionHeader));
}
bool ELFSectionHeader::Parse(const lldb_private::DataExtractor &data,
lldb::offset_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)); }
#define ENUM_TO_CSTR(e) \
case e: \
return #e
const char *ELFSymbol::bindingToCString(unsigned char binding) {
switch (binding) {
ENUM_TO_CSTR(STB_LOCAL);
ENUM_TO_CSTR(STB_GLOBAL);
ENUM_TO_CSTR(STB_WEAK);
ENUM_TO_CSTR(STB_LOOS);
ENUM_TO_CSTR(STB_HIOS);
ENUM_TO_CSTR(STB_LOPROC);
ENUM_TO_CSTR(STB_HIPROC);
}
return "";
}
const char *ELFSymbol::typeToCString(unsigned char type) {
switch (type) {
ENUM_TO_CSTR(STT_NOTYPE);
ENUM_TO_CSTR(STT_OBJECT);
ENUM_TO_CSTR(STT_FUNC);
ENUM_TO_CSTR(STT_SECTION);
ENUM_TO_CSTR(STT_FILE);
ENUM_TO_CSTR(STT_COMMON);
ENUM_TO_CSTR(STT_TLS);
ENUM_TO_CSTR(STT_GNU_IFUNC);
ENUM_TO_CSTR(STT_HIOS);
ENUM_TO_CSTR(STT_LOPROC);
ENUM_TO_CSTR(STT_HIPROC);
}
return "";
}
const char *ELFSymbol::sectionIndexToCString(
elf_half shndx, const lldb_private::SectionList *section_list) {
switch (shndx) {
ENUM_TO_CSTR(SHN_UNDEF);
ENUM_TO_CSTR(SHN_LOPROC);
ENUM_TO_CSTR(SHN_HIPROC);
ENUM_TO_CSTR(SHN_LOOS);
ENUM_TO_CSTR(SHN_HIOS);
ENUM_TO_CSTR(SHN_ABS);
ENUM_TO_CSTR(SHN_COMMON);
ENUM_TO_CSTR(SHN_XINDEX);
default: {
const lldb_private::Section *section =
section_list->GetSectionAtIndex(shndx).get();
if (section)
return section->GetName().AsCString("");
} break;
}
return "";
}
void ELFSymbol::Dump(lldb_private::Stream *s, uint32_t idx,
const lldb_private::DataExtractor *strtab_data,
const lldb_private::SectionList *section_list) {
s->Printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64
" 0x%8.8x 0x%2.2x (%-10s %-13s) 0x%2.2x 0x%4.4x (%-10s) %s\n",
idx, st_value, st_size, st_name, st_info,
bindingToCString(getBinding()), typeToCString(getType()), st_other,
st_shndx, sectionIndexToCString(st_shndx, section_list),
strtab_data ? strtab_data->PeekCStr(st_name) : "");
}
bool ELFSymbol::Parse(const lldb_private::DataExtractor &data,
lldb::offset_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,
lldb::offset_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,
lldb::offset_t *offset) {
const unsigned byte_size = data.GetAddressByteSize();
return GetMaxS64(data, offset, &d_tag, byte_size, 2);
}
//------------------------------------------------------------------------------
// ELFRel
ELFRel::ELFRel() { memset(this, 0, sizeof(ELFRel)); }
bool ELFRel::Parse(const lldb_private::DataExtractor &data,
lldb::offset_t *offset) {
const unsigned byte_size = data.GetAddressByteSize();
// Read r_offset and r_info.
if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
return false;
return true;
}
//------------------------------------------------------------------------------
// ELFRela
ELFRela::ELFRela() { memset(this, 0, sizeof(ELFRela)); }
bool ELFRela::Parse(const lldb_private::DataExtractor &data,
lldb::offset_t *offset) {
const unsigned byte_size = data.GetAddressByteSize();
// Read r_offset and r_info.
if (GetMaxU64(data, offset, &r_offset, byte_size, 2) == false)
return false;
// Read r_addend;
if (GetMaxS64(data, offset, &r_addend, byte_size) == false)
return false;
return true;
}

View File

@@ -0,0 +1,412 @@
//===-- 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/BinaryFormat/ELF.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-types.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_hdr; ///< Number of program header entries.
elf_half e_shentsize; ///< Size of a section header table entry.
elf_half e_shnum_hdr; ///< Number of section header entries.
elf_half e_shstrndx_hdr; ///< String table section index.
// In some cases these numbers do not fit in 16 bits and they are
// stored outside of the header in section #0. Here are the actual
// values.
elf_word e_phnum; ///< Number of program header entries.
elf_word e_shnum; ///< Number of section header entries.
elf_word 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;
//--------------------------------------------------------------------------
/// The jump slot relocation type of this ELF.
unsigned GetRelocationJumpSlotType() const;
//--------------------------------------------------------------------------
/// Check if there should be header extension in section header #0
///
/// @return
/// True if parsing the ELFHeader requires reading header extension
/// and false otherwise.
bool HasHeaderExtension() const;
//--------------------------------------------------------------------------
/// Parse an ELFHeader 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 ELFHeader was successfully read and false
/// otherwise.
bool Parse(lldb_private::DataExtractor &data, lldb::offset_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);
private:
//--------------------------------------------------------------------------
/// Parse an ELFHeader header extension entry. This method is called
/// by Parse().
///
/// @param[in] data
/// The DataExtractor to read from.
void ParseHeaderExtension(lldb_private::DataExtractor &data);
};
//------------------------------------------------------------------------------
/// @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, lldb::offset_t *offset);
};
//------------------------------------------------------------------------------
/// @class ELFProgramHeader
/// @brief Generic representation of an ELF program header.
struct ELFProgramHeader {
elf_word p_type; ///< Type of program segment.
elf_word p_flags; ///< Segment attributes.
elf_off p_offset; ///< Start of segment from beginning 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; ///< Segment alignment constraint.
ELFProgramHeader();
/// Parse an ELFProgramHeader entry from the given DataExtractor starting at
/// position \p offset. The address size of the DataExtractor 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, lldb::offset_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 binding and type of the st_info member.
void setBindingAndType(unsigned char binding, unsigned char type) {
st_info = (binding << 4) + (type & 0x0F);
}
static const char *bindingToCString(unsigned char binding);
static const char *typeToCString(unsigned char type);
static const char *
sectionIndexToCString(elf_half shndx,
const lldb_private::SectionList *section_list);
/// Parse an ELFSymbol entry from the given DataExtractor starting at
/// position \p offset. The address size of the DataExtractor 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, lldb::offset_t *offset);
void Dump(lldb_private::Stream *s, uint32_t idx,
const lldb_private::DataExtractor *strtab_data,
const lldb_private::SectionList *section_list);
};
//------------------------------------------------------------------------------
/// @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 DataExtractor starting at
/// position \p offset. The address size of the DataExtractor 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, lldb::offset_t *offset);
};
//------------------------------------------------------------------------------
/// @class ELFRel
/// @brief Represents a relocation entry with an implicit addend.
struct ELFRel {
elf_addr r_offset; ///< Address of reference.
elf_xword r_info; ///< symbol index and type of relocation.
ELFRel();
/// Parse an ELFRel entry from the given DataExtractor starting at position
/// \p offset. The address size of the DataExtractor 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 ELFRel entry was successfully read and false otherwise.
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
/// Returns the type when the given entry represents a 32-bit relocation.
static unsigned RelocType32(const ELFRel &rel) { return rel.r_info & 0x0ff; }
/// Returns the type when the given entry represents a 64-bit relocation.
static unsigned RelocType64(const ELFRel &rel) {
return rel.r_info & 0xffffffff;
}
/// Returns the symbol index when the given entry represents a 32-bit
/// relocation.
static unsigned RelocSymbol32(const ELFRel &rel) { return rel.r_info >> 8; }
/// Returns the symbol index when the given entry represents a 64-bit
/// relocation.
static unsigned RelocSymbol64(const ELFRel &rel) { return rel.r_info >> 32; }
};
//------------------------------------------------------------------------------
/// @class ELFRela
/// @brief Represents a relocation entry with an explicit addend.
struct ELFRela {
elf_addr r_offset; ///< Address of reference.
elf_xword r_info; ///< Symbol index and type of relocation.
elf_sxword r_addend; ///< Constant part of expression.
ELFRela();
/// Parse an ELFRela entry from the given DataExtractor starting at position
/// \p offset. The address size of the DataExtractor 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 ELFRela entry was successfully read and false otherwise.
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
/// Returns the type when the given entry represents a 32-bit relocation.
static unsigned RelocType32(const ELFRela &rela) {
return rela.r_info & 0x0ff;
}
/// Returns the type when the given entry represents a 64-bit relocation.
static unsigned RelocType64(const ELFRela &rela) {
return rela.r_info & 0xffffffff;
}
/// Returns the symbol index when the given entry represents a 32-bit
/// relocation.
static unsigned RelocSymbol32(const ELFRela &rela) {
return rela.r_info >> 8;
}
/// Returns the symbol index when the given entry represents a 64-bit
/// relocation.
static unsigned RelocSymbol64(const ELFRela &rela) {
return rela.r_info >> 32;
}
};
} // End namespace elf.
#endif // #ifndef liblldb_ELFHeader_h_

View File

@@ -0,0 +1 @@
36027dde04325fe3274373fcef9cff7ad4a1f50d

View File

@@ -0,0 +1,388 @@
//===-- ObjectFileELF.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_ObjectFileELF_h_
#define liblldb_ObjectFileELF_h_
// C Includes
#include <stdint.h>
// C++ Includes
#include <vector>
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/UUID.h"
#include "lldb/lldb-private.h"
#include "ELFHeader.h"
struct ELFNote {
elf::elf_word n_namesz;
elf::elf_word n_descsz;
elf::elf_word n_type;
std::string n_name;
ELFNote() : n_namesz(0), n_descsz(0), n_type(0) {}
/// Parse an ELFNote entry from the given DataExtractor starting at position
/// \p offset.
///
/// @param[in] data
/// The DataExtractor to read from.
///
/// @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 ELFRel entry was successfully read and false otherwise.
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
size_t GetByteSize() const {
return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4);
}
};
//------------------------------------------------------------------------------
/// @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 {
public:
~ObjectFileELF() override;
//------------------------------------------------------------------
// Static Functions
//------------------------------------------------------------------
static void Initialize();
static void Terminate();
static lldb_private::ConstString GetPluginNameStatic();
static const char *GetPluginDescriptionStatic();
static lldb_private::ObjectFile *
CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
lldb::offset_t data_offset, const lldb_private::FileSpec *file,
lldb::offset_t file_offset, lldb::offset_t length);
static lldb_private::ObjectFile *CreateMemoryInstance(
const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
lldb::DataBufferSP &data_sp,
lldb::offset_t data_offset,
lldb::offset_t file_offset,
lldb::offset_t length,
lldb_private::ModuleSpecList &specs);
static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
lldb::addr_t length);
//------------------------------------------------------------------
// PluginInterface protocol
//------------------------------------------------------------------
lldb_private::ConstString GetPluginName() override;
uint32_t GetPluginVersion() override;
//------------------------------------------------------------------
// ObjectFile Protocol.
//------------------------------------------------------------------
bool ParseHeader() override;
bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
bool value_is_offset) override;
lldb::ByteOrder GetByteOrder() const override;
bool IsExecutable() const override;
uint32_t GetAddressByteSize() const override;
lldb::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
lldb_private::Symtab *GetSymtab() override;
bool IsStripped() override;
void CreateSections(lldb_private::SectionList &unified_section_list) override;
void Dump(lldb_private::Stream *s) override;
bool GetArchitecture(lldb_private::ArchSpec &arch) override;
bool GetUUID(lldb_private::UUID *uuid) override;
lldb_private::FileSpecList GetDebugSymbolFilePaths() override;
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
lldb_private::Address
GetImageInfoAddress(lldb_private::Target *target) override;
lldb_private::Address GetEntryPointAddress() override;
ObjectFile::Type CalculateType() override;
ObjectFile::Strata CalculateStrata() override;
size_t ReadSectionData(lldb_private::Section *section,
lldb::offset_t section_offset, void *dst,
size_t dst_len) override;
size_t ReadSectionData(lldb_private::Section *section,
lldb_private::DataExtractor &section_data) override;
// Returns number of program headers found in the ELF file.
size_t GetProgramHeaderCount();
// Returns the program header with the given index.
const elf::ELFProgramHeader *GetProgramHeaderByIndex(lldb::user_id_t id);
// Returns segment data for the given index.
lldb_private::DataExtractor GetSegmentDataByIndex(lldb::user_id_t id);
llvm::StringRef
StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
void RelocateSection(lldb_private::Section *section) override;
private:
ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
lldb::offset_t data_offset, const lldb_private::FileSpec *file,
lldb::offset_t offset, lldb::offset_t length);
ObjectFileELF(const lldb::ModuleSP &module_sp,
lldb::DataBufferSP &header_data_sp,
const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
typedef ProgramHeaderColl::iterator ProgramHeaderCollIter;
typedef ProgramHeaderColl::const_iterator ProgramHeaderCollConstIter;
struct ELFSectionHeaderInfo : public elf::ELFSectionHeader {
lldb_private::ConstString section_name;
};
typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
typedef SectionHeaderColl::iterator SectionHeaderCollIter;
typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
typedef std::map<lldb::addr_t, lldb::AddressClass>
FileAddressToAddressClassMap;
/// Version of this reader common to all plugins based on this class.
static const uint32_t m_plugin_version = 1;
static const uint32_t g_core_uuid_magic;
/// ELF file header.
elf::ELFHeader m_header;
/// ELF build ID.
lldb_private::UUID m_uuid;
/// ELF .gnu_debuglink file and crc data if available.
std::string m_gnu_debuglink_file;
uint32_t m_gnu_debuglink_crc;
/// Collection of program headers.
ProgramHeaderColl m_program_headers;
/// Collection of section headers.
SectionHeaderColl m_section_headers;
/// Collection of symbols from the dynamic table.
DynamicSymbolColl m_dynamic_symbols;
/// List of file specifications corresponding to the modules (shared
/// libraries) on which this object file depends.
mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_ap;
/// Cached value of the entry point for this module.
lldb_private::Address m_entry_point_address;
/// The architecture detected from parsing elf file contents.
lldb_private::ArchSpec m_arch_spec;
/// The address class for each symbol in the elf file
FileAddressToAddressClassMap m_address_class_map;
/// Returns a 1 based index of the given section header.
size_t SectionIndex(const SectionHeaderCollIter &I);
/// Returns a 1 based index of the given section header.
size_t SectionIndex(const SectionHeaderCollConstIter &I) const;
// Parses the ELF program headers.
static size_t GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
lldb_private::DataExtractor &object_data,
const elf::ELFHeader &header);
// Finds PT_NOTE segments and calculates their crc sum.
static uint32_t
CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl &program_headers,
lldb_private::DataExtractor &data);
/// 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 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 ParseSectionHeaders();
static void ParseARMAttributes(lldb_private::DataExtractor &data,
uint64_t length,
lldb_private::ArchSpec &arch_spec);
/// Parses the elf section headers and returns the uuid, debug link name, crc,
/// archspec.
static size_t GetSectionHeaderInfo(SectionHeaderColl &section_headers,
lldb_private::DataExtractor &object_data,
const elf::ELFHeader &header,
lldb_private::UUID &uuid,
std::string &gnu_debuglink_file,
uint32_t &gnu_debuglink_crc,
lldb_private::ArchSpec &arch_spec);
/// Scans the dynamic section and locates all dependent modules (shared
/// libraries) populating m_filespec_ap. This method will compute the
/// dependent module list only once. Returns the number of dependent
/// modules parsed.
size_t ParseDependentModules();
/// Parses the dynamic symbol table and populates m_dynamic_symbols. The
/// vector retains the order as found in the object file. Returns the
/// number of dynamic symbols parsed.
size_t ParseDynamicSymbols();
/// Populates m_symtab_ap will all non-dynamic linker symbols. This method
/// will parse the symbols only once. Returns the number of symbols parsed.
unsigned ParseSymbolTable(lldb_private::Symtab *symbol_table,
lldb::user_id_t start_id,
lldb_private::Section *symtab);
/// Helper routine for ParseSymbolTable().
unsigned ParseSymbols(lldb_private::Symtab *symbol_table,
lldb::user_id_t start_id,
lldb_private::SectionList *section_list,
const size_t num_symbols,
const lldb_private::DataExtractor &symtab_data,
const lldb_private::DataExtractor &strtab_data);
/// Scans the relocation entries and adds a set of artificial symbols to the
/// given symbol table for each PLT slot. Returns the number of symbols
/// added.
unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
lldb::user_id_t start_id,
const ELFSectionHeaderInfo *rela_hdr,
lldb::user_id_t section_id);
void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
lldb_private::DWARFCallFrameInfo *eh_frame);
/// Relocates debug sections
unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
lldb::user_id_t rel_id,
lldb_private::Symtab *thetab);
unsigned ApplyRelocations(lldb_private::Symtab *symtab,
const elf::ELFHeader *hdr,
const elf::ELFSectionHeader *rel_hdr,
const elf::ELFSectionHeader *symtab_hdr,
const elf::ELFSectionHeader *debug_hdr,
lldb_private::DataExtractor &rel_data,
lldb_private::DataExtractor &symtab_data,
lldb_private::DataExtractor &debug_data,
lldb_private::Section *rel_section);
/// Loads the section name string table into m_shstr_data. Returns the
/// number of bytes constituting the table.
size_t GetSectionHeaderStringTable();
/// 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);
// Returns the ID of the first section that has the given type.
lldb::user_id_t GetSectionIndexByType(unsigned type);
/// Returns the section header with the given id or NULL.
const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id);
/// @name ELF header dump routines
//@{
static void DumpELFHeader(lldb_private::Stream *s,
const elf::ELFHeader &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,
elf::elf_half e_type);
//@}
/// @name ELF program header dump routines
//@{
void DumpELFProgramHeaders(lldb_private::Stream *s);
static void DumpELFProgramHeader(lldb_private::Stream *s,
const elf::ELFProgramHeader &ph);
static void DumpELFProgramHeader_p_type(lldb_private::Stream *s,
elf::elf_word p_type);
static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
elf::elf_word p_flags);
//@}
/// @name ELF section header dump routines
//@{
void DumpELFSectionHeaders(lldb_private::Stream *s);
static void DumpELFSectionHeader(lldb_private::Stream *s,
const ELFSectionHeaderInfo &sh);
static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
elf::elf_word sh_type);
static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
elf::elf_xword sh_flags);
//@}
/// ELF dependent module dump routine.
void DumpDependentModules(lldb_private::Stream *s);
const elf::ELFDynamic *FindDynamicSymbol(unsigned tag);
unsigned PLTRelocationType();
static lldb_private::Status
RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
lldb_private::ArchSpec &arch_spec,
lldb_private::UUID &uuid);
};
#endif // liblldb_ObjectFileELF_h_