2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-01-10 11:33:56 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-01-24 17:28:26 +03:00
|
|
|
#include <AK/Debug.h>
|
2020-11-28 16:04:20 +03:00
|
|
|
#include <AK/Demangle.h>
|
2020-03-08 14:05:14 +03:00
|
|
|
#include <AK/Memory.h>
|
2020-12-25 04:14:56 +03:00
|
|
|
#include <AK/QuickSort.h>
|
2020-01-05 10:37:05 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2020-04-11 21:24:07 +03:00
|
|
|
#include <LibELF/Image.h>
|
2020-04-11 21:32:38 +03:00
|
|
|
#include <LibELF/Validation.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
namespace ELF {
|
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
Image::Image(ReadonlyBytes bytes, bool verbose_logging)
|
|
|
|
: m_buffer(bytes.data())
|
|
|
|
, m_size(bytes.size())
|
2020-08-10 16:55:17 +03:00
|
|
|
, m_verbose_logging(verbose_logging)
|
2018-10-18 16:03:10 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
parse();
|
2018-10-18 16:03:10 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
Image::Image(const u8* buffer, size_t size, bool verbose_logging)
|
|
|
|
: Image(ReadonlyBytes { buffer, size }, verbose_logging)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
Image::~Image()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-24 17:28:26 +03:00
|
|
|
#if ELF_IMAGE_DEBUG
|
2019-01-31 19:31:23 +03:00
|
|
|
static const char* object_file_type_to_string(Elf32_Half type)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
switch (type) {
|
2019-06-07 18:12:30 +03:00
|
|
|
case ET_NONE:
|
|
|
|
return "None";
|
|
|
|
case ET_REL:
|
|
|
|
return "Relocatable";
|
|
|
|
case ET_EXEC:
|
|
|
|
return "Executable";
|
|
|
|
case ET_DYN:
|
|
|
|
return "Shared object";
|
|
|
|
case ET_CORE:
|
|
|
|
return "Core";
|
|
|
|
default:
|
|
|
|
return "(?)";
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-10 11:33:56 +03:00
|
|
|
#endif
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
StringView Image::section_index_to_string(unsigned index) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-10-10 12:53:07 +03:00
|
|
|
if (index == SHN_UNDEF)
|
|
|
|
return "Undefined";
|
|
|
|
if (index >= SHN_LORESERVE)
|
|
|
|
return "Reserved";
|
|
|
|
return section(index).name();
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
unsigned Image::symbol_count() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2020-12-21 20:37:53 +03:00
|
|
|
if (!section_count())
|
|
|
|
return 0;
|
2018-11-04 16:09:30 +03:00
|
|
|
return section(m_symbol_table_section_index).entry_count();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
void Image::dump() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-01-24 17:28:26 +03:00
|
|
|
#if ELF_IMAGE_DEBUG
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("ELF::Image({:p}) {{", this);
|
|
|
|
dbgln(" is_valid: {}", is_valid());
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-04 16:09:30 +03:00
|
|
|
if (!is_valid()) {
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("}}");
|
2018-10-10 12:53:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln(" type: {}", object_file_type_to_string(header().e_type));
|
|
|
|
dbgln(" machine: {}", header().e_machine);
|
|
|
|
dbgln(" entry: {:x}", header().e_entry);
|
|
|
|
dbgln(" shoff: {}", header().e_shoff);
|
|
|
|
dbgln(" shnum: {}", header().e_shnum);
|
|
|
|
dbgln(" phoff: {}", header().e_phoff);
|
|
|
|
dbgln(" phnum: {}", header().e_phnum);
|
|
|
|
dbgln(" shstrndx: {}", header().e_shstrndx);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-01-11 04:25:12 +03:00
|
|
|
for_each_program_header([&](const ProgramHeader& program_header) {
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln(" Program Header {}: {{", program_header.index());
|
|
|
|
dbgln(" type: {:x}", program_header.type());
|
|
|
|
dbgln(" offset: {:x}", program_header.offset());
|
|
|
|
dbgln(" flags: {:x}", program_header.flags());
|
|
|
|
dbgln(" }}");
|
2020-12-25 16:42:42 +03:00
|
|
|
return IterationDecision::Continue;
|
2020-01-11 04:25:12 +03:00
|
|
|
});
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
for (unsigned i = 0; i < header().e_shnum; ++i) {
|
|
|
|
auto& section = this->section(i);
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln(" Section {}: {{", i);
|
|
|
|
dbgln(" name: {}", section.name());
|
|
|
|
dbgln(" type: {:x}", section.type());
|
|
|
|
dbgln(" offset: {:x}", section.offset());
|
|
|
|
dbgln(" size: {}", section.size());
|
|
|
|
dbgln(" ");
|
|
|
|
dbgln(" }}");
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("Symbol count: {} (table is {})", symbol_count(), m_symbol_table_section_index);
|
2018-11-04 16:09:30 +03:00
|
|
|
for (unsigned i = 1; i < symbol_count(); ++i) {
|
2018-10-10 12:53:07 +03:00
|
|
|
auto& sym = symbol(i);
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("Symbol @{}:", i);
|
|
|
|
dbgln(" Name: {}", sym.name());
|
|
|
|
dbgln(" In section: {}", section_index_to_string(sym.section_index()));
|
|
|
|
dbgln(" Value: {}", sym.value());
|
|
|
|
dbgln(" Size: {}", sym.size());
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("}}");
|
|
|
|
#endif
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
unsigned Image::section_count() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-10-10 12:53:07 +03:00
|
|
|
return header().e_shnum;
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
unsigned Image::program_header_count() const
|
2018-11-03 12:11:56 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-11-03 12:11:56 +03:00
|
|
|
return header().e_phnum;
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
bool Image::parse()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
if (m_size < sizeof(Elf32_Ehdr) || !validate_elf_header(header(), m_size, m_verbose_logging)) {
|
|
|
|
if (m_verbose_logging)
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("ELF::Image::parse(): ELF Header not valid");
|
2020-08-10 16:55:17 +03:00
|
|
|
return m_valid = false;
|
2019-03-27 03:29:49 +03:00
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2020-12-01 09:21:55 +03:00
|
|
|
if (!validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging)) {
|
|
|
|
if (m_verbose_logging)
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("ELF::Image::parse(): ELF Program Headers not valid");
|
2020-12-01 09:21:55 +03:00
|
|
|
return m_valid = false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:55:17 +03:00
|
|
|
m_valid = true;
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
// First locate the string tables.
|
2018-11-04 16:09:30 +03:00
|
|
|
for (unsigned i = 0; i < section_count(); ++i) {
|
|
|
|
auto& sh = section_header(i);
|
2018-10-10 12:53:07 +03:00
|
|
|
if (sh.sh_type == SHT_SYMTAB) {
|
2020-08-10 16:55:17 +03:00
|
|
|
if (m_symbol_table_section_index && m_symbol_table_section_index != i)
|
|
|
|
return m_valid = false;
|
2018-11-04 16:09:30 +03:00
|
|
|
m_symbol_table_section_index = i;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
|
2020-02-21 18:16:23 +03:00
|
|
|
if (section_header_table_string(sh.sh_name) == ELF_STRTAB)
|
2020-01-01 00:45:50 +03:00
|
|
|
m_string_table_section_index = i;
|
|
|
|
}
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
2019-11-28 22:53:02 +03:00
|
|
|
|
|
|
|
// Then create a name-to-index map.
|
|
|
|
for (unsigned i = 0; i < section_count(); ++i) {
|
|
|
|
auto& section = this->section(i);
|
|
|
|
m_sections.set(section.name(), move(i));
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:55:17 +03:00
|
|
|
return m_valid;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
StringView Image::table_string(unsigned table_index, unsigned offset) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2020-01-17 00:04:44 +03:00
|
|
|
auto& sh = section_header(table_index);
|
2018-10-10 12:53:07 +03:00
|
|
|
if (sh.sh_type != SHT_STRTAB)
|
|
|
|
return nullptr;
|
2020-01-17 00:04:44 +03:00
|
|
|
size_t computed_offset = sh.sh_offset + offset;
|
|
|
|
if (computed_offset >= m_size) {
|
2020-08-10 16:55:17 +03:00
|
|
|
if (m_verbose_logging)
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("SHENANIGANS! Image::table_string() computed offset outside image.");
|
2020-01-17 00:04:44 +03:00
|
|
|
return {};
|
|
|
|
}
|
2021-01-30 06:11:50 +03:00
|
|
|
size_t max_length = min(m_size - computed_offset, (size_t)PAGE_SIZE);
|
2020-01-17 00:04:44 +03:00
|
|
|
size_t length = strnlen(raw_data(sh.sh_offset + offset), max_length);
|
|
|
|
return { raw_data(sh.sh_offset + offset), length };
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
StringView Image::section_header_table_string(unsigned offset) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2020-01-17 00:04:44 +03:00
|
|
|
return table_string(header().e_shstrndx, offset);
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
StringView Image::table_string(unsigned offset) const
|
2020-01-17 00:04:44 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2020-01-17 00:04:44 +03:00
|
|
|
return table_string(m_string_table_section_index, offset);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const char* Image::raw_data(unsigned offset) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(offset < m_size); // Callers must check indices into raw_data()'s result are also in bounds.
|
2018-11-08 23:20:09 +03:00
|
|
|
return reinterpret_cast<const char*>(m_buffer) + offset;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Elf32_Ehdr& Image::header() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_size >= sizeof(Elf32_Ehdr));
|
2018-11-04 16:09:30 +03:00
|
|
|
return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Elf32_Phdr& Image::program_header_internal(unsigned index) const
|
2018-11-03 12:11:56 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-11-03 12:11:56 +03:00
|
|
|
ASSERT(index < header().e_phnum);
|
2018-11-04 16:09:30 +03:00
|
|
|
return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
|
2018-11-03 12:11:56 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Elf32_Shdr& Image::section_header(unsigned index) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-10-10 12:53:07 +03:00
|
|
|
ASSERT(index < header().e_shnum);
|
2020-01-01 00:45:50 +03:00
|
|
|
return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Image::Symbol Image::symbol(unsigned index) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-11-04 16:09:30 +03:00
|
|
|
ASSERT(index < symbol_count());
|
2019-01-31 19:31:23 +03:00
|
|
|
auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
|
|
|
|
return Symbol(*this, index, raw_syms[index]);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Image::Section Image::section(unsigned index) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-11-04 16:09:30 +03:00
|
|
|
ASSERT(index < section_count());
|
2018-10-10 12:53:07 +03:00
|
|
|
return Section(*this, index);
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Image::ProgramHeader Image::program_header(unsigned index) const
|
2018-11-03 12:11:56 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2018-11-03 12:11:56 +03:00
|
|
|
ASSERT(index < program_header_count());
|
|
|
|
return ProgramHeader(*this, index);
|
|
|
|
}
|
2019-11-28 22:53:02 +03:00
|
|
|
|
2020-10-10 18:17:49 +03:00
|
|
|
FlatPtr Image::program_header_table_offset() const
|
|
|
|
{
|
|
|
|
return header().e_phoff;
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Image::Relocation Image::RelocationSection::relocation(unsigned index) const
|
2019-11-28 22:53:02 +03:00
|
|
|
{
|
|
|
|
ASSERT(index < relocation_count());
|
|
|
|
auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
|
|
|
|
return Relocation(m_image, rels[index]);
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Image::RelocationSection Image::Section::relocations() const
|
2019-11-28 22:53:02 +03:00
|
|
|
{
|
2020-01-05 10:37:05 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(".rel");
|
|
|
|
builder.append(name());
|
2019-11-28 22:53:02 +03:00
|
|
|
|
2020-01-05 10:37:05 +03:00
|
|
|
auto relocation_section = m_image.lookup_section(builder.to_string());
|
2019-11-28 22:53:02 +03:00
|
|
|
if (relocation_section.type() != SHT_REL)
|
|
|
|
return static_cast<const RelocationSection>(m_image.section(0));
|
|
|
|
|
2021-01-24 17:28:26 +03:00
|
|
|
#if ELF_IMAGE_DEBUG
|
2021-01-10 11:33:56 +03:00
|
|
|
dbgln("Found relocations for {} in {}", name(), relocation_section.name());
|
2019-11-28 22:53:02 +03:00
|
|
|
#endif
|
|
|
|
return static_cast<const RelocationSection>(relocation_section);
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
const Image::Section Image::lookup_section(const String& name) const
|
2019-11-28 22:53:02 +03:00
|
|
|
{
|
2020-08-10 16:55:17 +03:00
|
|
|
ASSERT(m_valid);
|
2019-11-28 22:53:02 +03:00
|
|
|
if (auto it = m_sections.find(name); it != m_sections.end())
|
|
|
|
return section((*it).value);
|
|
|
|
return section(0);
|
|
|
|
}
|
2020-01-11 04:25:12 +03:00
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
StringView Image::Symbol::raw_data() const
|
2020-04-11 19:45:17 +03:00
|
|
|
{
|
|
|
|
auto& section = this->section();
|
|
|
|
return { section.raw_data() + (value() - section.address()), size() };
|
|
|
|
}
|
2020-04-11 21:24:07 +03:00
|
|
|
|
2020-11-28 16:04:20 +03:00
|
|
|
Optional<Image::Symbol> Image::find_demangled_function(const String& name) const
|
|
|
|
{
|
|
|
|
Optional<Image::Symbol> found;
|
|
|
|
for_each_symbol([&](const Image::Symbol symbol) {
|
|
|
|
if (symbol.type() != STT_FUNC)
|
|
|
|
return IterationDecision::Continue;
|
2021-01-06 22:44:47 +03:00
|
|
|
if (symbol.is_undefined())
|
|
|
|
return IterationDecision::Continue;
|
2020-11-28 16:04:20 +03:00
|
|
|
auto demangled = demangle(symbol.name());
|
|
|
|
auto index_of_paren = demangled.index_of("(");
|
|
|
|
if (index_of_paren.has_value()) {
|
|
|
|
demangled = demangled.substring(0, index_of_paren.value());
|
|
|
|
}
|
|
|
|
if (demangled != name)
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
found = symbol;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
});
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2020-12-25 04:14:56 +03:00
|
|
|
Optional<Image::Symbol> Image::find_symbol(u32 address, u32* out_offset) const
|
|
|
|
{
|
|
|
|
auto symbol_count = this->symbol_count();
|
|
|
|
if (!symbol_count)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
SortedSymbol* sorted_symbols = nullptr;
|
|
|
|
if (m_sorted_symbols.is_empty()) {
|
|
|
|
m_sorted_symbols.ensure_capacity(symbol_count);
|
|
|
|
for_each_symbol([this](auto& symbol) {
|
|
|
|
m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
|
|
|
|
return a.address < b.address;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
sorted_symbols = m_sorted_symbols.data();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < symbol_count; ++i) {
|
|
|
|
if (sorted_symbols[i].address > address) {
|
|
|
|
if (i == 0)
|
|
|
|
return {};
|
|
|
|
auto& symbol = sorted_symbols[i - 1];
|
|
|
|
if (out_offset)
|
|
|
|
*out_offset = address - symbol.address;
|
|
|
|
return symbol.symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
String Image::symbolicate(u32 address, u32* out_offset) const
|
|
|
|
{
|
|
|
|
auto symbol_count = this->symbol_count();
|
|
|
|
if (!symbol_count) {
|
|
|
|
if (out_offset)
|
|
|
|
*out_offset = 0;
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
SortedSymbol* sorted_symbols = nullptr;
|
|
|
|
|
|
|
|
if (m_sorted_symbols.is_empty()) {
|
|
|
|
m_sorted_symbols.ensure_capacity(symbol_count);
|
|
|
|
for_each_symbol([this](auto& symbol) {
|
2021-01-09 12:03:42 +03:00
|
|
|
m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
|
2020-12-25 04:14:56 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
|
|
|
|
return a.address < b.address;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
sorted_symbols = m_sorted_symbols.data();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < symbol_count; ++i) {
|
|
|
|
if (sorted_symbols[i].address > address) {
|
|
|
|
if (i == 0) {
|
|
|
|
if (out_offset)
|
|
|
|
*out_offset = 0;
|
|
|
|
return "!!";
|
|
|
|
}
|
|
|
|
auto& symbol = sorted_symbols[i - 1];
|
|
|
|
|
|
|
|
auto& demangled_name = symbol.demangled_name;
|
|
|
|
if (demangled_name.is_null()) {
|
|
|
|
demangled_name = demangle(symbol.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_offset) {
|
|
|
|
*out_offset = address - symbol.address;
|
|
|
|
return demangled_name;
|
|
|
|
}
|
2021-01-02 03:38:43 +03:00
|
|
|
return String::format("%s +0x%x", demangled_name.characters(), address - symbol.address);
|
2020-12-25 04:14:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out_offset)
|
|
|
|
*out_offset = 0;
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:24:07 +03:00
|
|
|
} // end namespace ELF
|