mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:27:43 +00:00
LibELF: Add a find_symbol() API that finds a Symbol for an address
Also add ELFImage::Symbol::raw_data() to get a StringView containing the entire symbol contents.
This commit is contained in:
parent
31d0dbe2a0
commit
5b91d848a7
4 changed files with 60 additions and 1 deletions
|
@ -414,3 +414,9 @@ bool ELFImage::validate_program_headers(const Elf32_Ehdr& elf_header, size_t fil
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringView ELFImage::Symbol::raw_data() const
|
||||||
|
{
|
||||||
|
auto& section = this->section();
|
||||||
|
return { section.raw_data() + (value() - section.address()), size() };
|
||||||
|
}
|
||||||
|
|
|
@ -73,6 +73,7 @@ public:
|
||||||
unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
|
unsigned type() const { return ELF32_ST_TYPE(m_sym.st_info); }
|
||||||
unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
|
unsigned bind() const { return ELF32_ST_BIND(m_sym.st_info); }
|
||||||
const Section section() const { return m_image.section(section_index()); }
|
const Section section() const { return m_image.section(section_index()); }
|
||||||
|
StringView raw_data() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const ELFImage& m_image;
|
const ELFImage& m_image;
|
||||||
|
|
|
@ -151,6 +151,56 @@ char* ELFLoader::symbol_ptr(const char* name)
|
||||||
return found_ptr;
|
return found_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef KERNEL
|
||||||
|
Optional<ELFImage::Symbol> ELFLoader::find_symbol(u32 address, u32* out_offset) const
|
||||||
|
{
|
||||||
|
if (!m_symbol_count)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
SortedSymbol* sorted_symbols = nullptr;
|
||||||
|
#ifdef KERNEL
|
||||||
|
if (!m_sorted_symbols_region) {
|
||||||
|
m_sorted_symbols_region = MM.allocate_kernel_region(PAGE_ROUND_UP(m_symbol_count * sizeof(SortedSymbol)), "Sorted symbols", Kernel::Region::Access::Read | Kernel::Region::Access::Write);
|
||||||
|
sorted_symbols = (SortedSymbol*)m_sorted_symbols_region->vaddr().as_ptr();
|
||||||
|
size_t index = 0;
|
||||||
|
m_image.for_each_symbol([&](auto& symbol) {
|
||||||
|
sorted_symbols[index++] = { symbol.value(), symbol.name() };
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
quick_sort(sorted_symbols, sorted_symbols + m_symbol_count, [](auto& a, auto& b) {
|
||||||
|
return a.address < b.address;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sorted_symbols = (SortedSymbol*)m_sorted_symbols_region->vaddr().as_ptr();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (m_sorted_symbols.is_empty()) {
|
||||||
|
m_sorted_symbols.ensure_capacity(m_symbol_count);
|
||||||
|
m_image.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();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (size_t i = 0; i < m_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 {};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
String ELFLoader::symbolicate(u32 address, u32* out_offset) const
|
String ELFLoader::symbolicate(u32 address, u32* out_offset) const
|
||||||
{
|
{
|
||||||
if (!m_symbol_count) {
|
if (!m_symbol_count) {
|
||||||
|
@ -178,7 +228,7 @@ String ELFLoader::symbolicate(u32 address, u32* out_offset) const
|
||||||
if (m_sorted_symbols.is_empty()) {
|
if (m_sorted_symbols.is_empty()) {
|
||||||
m_sorted_symbols.ensure_capacity(m_symbol_count);
|
m_sorted_symbols.ensure_capacity(m_symbol_count);
|
||||||
m_image.for_each_symbol([this](auto& symbol) {
|
m_image.for_each_symbol([this](auto& symbol) {
|
||||||
m_sorted_symbols.append({ symbol.value(), symbol.name(), {} });
|
m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, {} });
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
|
quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
|
||||||
|
|
|
@ -57,6 +57,7 @@ public:
|
||||||
bool has_symbols() const { return m_symbol_count; }
|
bool has_symbols() const { return m_symbol_count; }
|
||||||
|
|
||||||
String symbolicate(u32 address, u32* offset = nullptr) const;
|
String symbolicate(u32 address, u32* offset = nullptr) const;
|
||||||
|
Optional<ELFImage::Symbol> find_symbol(u32 address, u32* offset = nullptr) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool layout();
|
bool layout();
|
||||||
|
@ -85,6 +86,7 @@ private:
|
||||||
StringView name;
|
StringView name;
|
||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
String demangled_name;
|
String demangled_name;
|
||||||
|
Optional<ELFImage::Symbol> symbol;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
#ifdef KERNEL
|
#ifdef KERNEL
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue