123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- /*
- * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/Debug.h>
- #include <AK/Demangle.h>
- #include <AK/Memory.h>
- #include <AK/QuickSort.h>
- #include <AK/StringBuilder.h>
- #include <AK/StringView.h>
- #include <LibELF/Image.h>
- #include <LibELF/Validation.h>
- namespace ELF {
- Image::Image(ReadonlyBytes bytes, bool verbose_logging)
- : m_buffer(bytes.data())
- , m_size(bytes.size())
- , m_verbose_logging(verbose_logging)
- {
- parse();
- }
- Image::Image(const u8* buffer, size_t size, bool verbose_logging)
- : Image(ReadonlyBytes { buffer, size }, verbose_logging)
- {
- }
- Image::~Image()
- {
- }
- #if ELF_IMAGE_DEBUG
- static const char* object_file_type_to_string(Elf32_Half type)
- {
- switch (type) {
- 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 "(?)";
- }
- }
- #endif
- StringView Image::section_index_to_string(unsigned index) const
- {
- VERIFY(m_valid);
- if (index == SHN_UNDEF)
- return "Undefined"sv;
- if (index >= SHN_LORESERVE)
- return "Reserved"sv;
- return section(index).name();
- }
- unsigned Image::symbol_count() const
- {
- VERIFY(m_valid);
- if (!section_count())
- return 0;
- return section(m_symbol_table_section_index).entry_count();
- }
- void Image::dump() const
- {
- #if ELF_IMAGE_DEBUG
- dbgln("ELF::Image({:p}) {{", this);
- dbgln(" is_valid: {}", is_valid());
- if (!is_valid()) {
- dbgln("}}");
- return;
- }
- 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);
- for_each_program_header([&](const ProgramHeader& program_header) {
- 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(" }}");
- return IterationDecision::Continue;
- });
- for (unsigned i = 0; i < header().e_shnum; ++i) {
- const auto& section = this->section(i);
- dbgln(" Section {}: {{", i);
- dbgln(" name: {}", section.name());
- dbgln(" type: {:x}", section.type());
- dbgln(" offset: {:x}", section.offset());
- dbgln(" size: {}", section.size());
- dbgln(" ");
- dbgln(" }}");
- }
- dbgln("Symbol count: {} (table is {})", symbol_count(), m_symbol_table_section_index);
- for (unsigned i = 1; i < symbol_count(); ++i) {
- const auto& sym = symbol(i);
- 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());
- }
- dbgln("}}");
- #endif
- }
- unsigned Image::section_count() const
- {
- VERIFY(m_valid);
- return header().e_shnum;
- }
- unsigned Image::program_header_count() const
- {
- VERIFY(m_valid);
- return header().e_phnum;
- }
- bool Image::parse()
- {
- if (m_size < sizeof(Elf32_Ehdr) || !validate_elf_header(header(), m_size, m_verbose_logging)) {
- if (m_verbose_logging)
- dbgln("ELF::Image::parse(): ELF Header not valid");
- return m_valid = false;
- }
- if (!validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging)) {
- if (m_verbose_logging)
- dbgln("ELF::Image::parse(): ELF Program Headers not valid");
- return m_valid = false;
- }
- m_valid = true;
- // First locate the string tables.
- for (unsigned i = 0; i < section_count(); ++i) {
- auto& sh = section_header(i);
- if (sh.sh_type == SHT_SYMTAB) {
- if (m_symbol_table_section_index && m_symbol_table_section_index != i)
- return m_valid = false;
- m_symbol_table_section_index = i;
- }
- if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
- if (section_header_table_string(sh.sh_name) == ELF_STRTAB)
- m_string_table_section_index = i;
- }
- }
- return m_valid;
- }
- StringView Image::table_string(unsigned table_index, unsigned offset) const
- {
- VERIFY(m_valid);
- auto& sh = section_header(table_index);
- if (sh.sh_type != SHT_STRTAB)
- return nullptr;
- size_t computed_offset = sh.sh_offset + offset;
- if (computed_offset >= m_size) {
- if (m_verbose_logging)
- dbgln("SHENANIGANS! Image::table_string() computed offset outside image.");
- return {};
- }
- size_t max_length = min(m_size - computed_offset, (size_t)PAGE_SIZE);
- size_t length = strnlen(raw_data(sh.sh_offset + offset), max_length);
- return { raw_data(sh.sh_offset + offset), length };
- }
- StringView Image::section_header_table_string(unsigned offset) const
- {
- VERIFY(m_valid);
- return table_string(header().e_shstrndx, offset);
- }
- StringView Image::table_string(unsigned offset) const
- {
- VERIFY(m_valid);
- return table_string(m_string_table_section_index, offset);
- }
- const char* Image::raw_data(unsigned offset) const
- {
- VERIFY(offset < m_size); // Callers must check indices into raw_data()'s result are also in bounds.
- return reinterpret_cast<const char*>(m_buffer) + offset;
- }
- const Elf32_Ehdr& Image::header() const
- {
- VERIFY(m_size >= sizeof(Elf32_Ehdr));
- return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
- }
- const Elf32_Phdr& Image::program_header_internal(unsigned index) const
- {
- VERIFY(m_valid);
- VERIFY(index < header().e_phnum);
- return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
- }
- const Elf32_Shdr& Image::section_header(unsigned index) const
- {
- VERIFY(m_valid);
- VERIFY(index < header().e_shnum);
- return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
- }
- Image::Symbol Image::symbol(unsigned index) const
- {
- VERIFY(m_valid);
- VERIFY(index < symbol_count());
- auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
- return Symbol(*this, index, raw_syms[index]);
- }
- Image::Section Image::section(unsigned index) const
- {
- VERIFY(m_valid);
- VERIFY(index < section_count());
- return Section(*this, index);
- }
- Image::ProgramHeader Image::program_header(unsigned index) const
- {
- VERIFY(m_valid);
- VERIFY(index < program_header_count());
- return ProgramHeader(*this, index);
- }
- Image::Relocation Image::RelocationSection::relocation(unsigned index) const
- {
- VERIFY(index < relocation_count());
- auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
- return Relocation(m_image, rels[index]);
- }
- Image::RelocationSection Image::Section::relocations() const
- {
- StringBuilder builder;
- builder.append(".rel"sv);
- builder.append(name());
- auto relocation_section = m_image.lookup_section(builder.to_string());
- if (relocation_section.type() != SHT_REL)
- return static_cast<const RelocationSection>(m_image.section(0));
- dbgln_if(ELF_IMAGE_DEBUG, "Found relocations for {} in {}", name(), relocation_section.name());
- return static_cast<const RelocationSection>(relocation_section);
- }
- Image::Section Image::lookup_section(const String& name) const
- {
- VERIFY(m_valid);
- for (unsigned i = 0; i < section_count(); ++i) {
- auto section = this->section(i);
- if (section.name() == name)
- return section;
- }
- return section(0);
- }
- StringView Image::Symbol::raw_data() const
- {
- auto section = this->section();
- return { section.raw_data() + (value() - section.address()), size() };
- }
- 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;
- if (symbol.is_undefined())
- return IterationDecision::Continue;
- 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;
- }
- 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](const 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](const 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) {
- 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;
- }
- return String::formatted("{} +{:#x}", demangled_name, address - symbol.address);
- }
- }
- if (out_offset)
- *out_offset = 0;
- return "??";
- }
- } // end namespace ELF
|