Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up ELF symbol lookup #1116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist/
api/rust/cargo/target/*
api/rust/examples/target/*
Cargo.lock
.cache
2 changes: 1 addition & 1 deletion examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ foreach(example ${LIEF_CPP_EXAMPLES})
set_target_properties(
${output_name}
PROPERTIES POSITION_INDEPENDENT_CODE ON
CXX_STANDARD 11
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)

target_link_libraries(${output_name} PUBLIC LIB_LIEF)
Expand Down
7 changes: 7 additions & 0 deletions include/LIEF/ELF/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#ifndef LIEF_ELF_BINARY_H
#define LIEF_ELF_BINARY_H

#include <string_view>
#include <vector>
#include <map>
#include <memory>

#include "LIEF/visibility.h"
Expand Down Expand Up @@ -150,6 +152,9 @@ class LIEF_API Binary : public LIEF::Binary {
//! Internal container for storing ELF's Symbol
using symbols_t = std::vector<std::unique_ptr<Symbol>>;

//! Internal container for fast lookup of symbols
using symbol_indices_t = std::map<std::string_view, uint32_t>;

//! Iterator which outputs the Dynamic Symbol& object
using it_dynamic_symbols = ref_iterator<symbols_t&, Symbol*>;

Expand Down Expand Up @@ -1021,6 +1026,8 @@ class LIEF_API Binary : public LIEF::Binary {
dynamic_entries_t dynamic_entries_;
symbols_t dynamic_symbols_;
symbols_t symtab_symbols_;
symbol_indices_t dynamic_symbol_indices_;
symbol_indices_t symtab_symbol_indices_;
relocations_t relocations_;
symbols_version_t symbol_version_table_;
symbols_version_requirement_t symbol_version_requirements_;
Expand Down
60 changes: 13 additions & 47 deletions src/ELF/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,10 @@ void Binary::remove(Note::TYPE type) {


int64_t Binary::symtab_idx(const std::string& name) const {
if (symtab_symbols_.empty()) {
if (!symtab_symbol_indices_.count(name)) {
return -1;
}

auto it = std::find_if(symtab_symbols_.begin(), symtab_symbols_.end(),
[&name] (const std::unique_ptr<Symbol>& S) {
return S->name() == name;
}
);

if (it == symtab_symbols_.end()) {
return -1;
}

return std::distance(symtab_symbols_.begin(), it);
return symtab_symbol_indices_.at(name);
}

int64_t Binary::symtab_idx(const Symbol& sym) const {
Expand All @@ -282,19 +271,10 @@ int64_t Binary::dynsym_idx(const Symbol& sym) const {
}

int64_t Binary::dynsym_idx(const std::string& name) const {
if (dynamic_symbols_.empty()) {
if (!dynamic_symbol_indices_.count(name)) {
return -1;
}

auto it = std::find_if(dynamic_symbols_.begin(), dynamic_symbols_.end(),
[&name] (const std::unique_ptr<Symbol>& S) {
return S->name() == name;
}
);
if (it == dynamic_symbols_.end()) {
return -1;
}
return std::distance(dynamic_symbols_.begin(), it);
return dynamic_symbol_indices_.at(name);
}


Expand Down Expand Up @@ -410,43 +390,25 @@ Symbol& Binary::add_exported_function(uint64_t address, const std::string& name)


bool Binary::has_dynamic_symbol(const std::string& name) const {
const auto it_symbol = std::find_if(
std::begin(dynamic_symbols_), std::end(dynamic_symbols_),
[&name] (const std::unique_ptr<Symbol>& s) {
return s->name() == name;
}
);

return it_symbol != std::end(dynamic_symbols_);
return dynamic_symbol_indices_.count(name);
}

const Symbol* Binary::get_dynamic_symbol(const std::string& name) const {
const auto it_symbol = std::find_if(
std::begin(dynamic_symbols_), std::end(dynamic_symbols_),
[&name] (const std::unique_ptr<Symbol>& s) {
return s->name() == name;
});

if (it_symbol == std::end(dynamic_symbols_)) {
if (!dynamic_symbol_indices_.count(name)) {
return nullptr;
}
return it_symbol->get();
return dynamic_symbols_[dynamic_symbol_indices_.at(name)].get();
}

Symbol* Binary::get_dynamic_symbol(const std::string& name) {
return const_cast<Symbol*>(static_cast<const Binary*>(this)->get_dynamic_symbol(name));
}

const Symbol* Binary::get_symtab_symbol(const std::string& name) const {
const auto it_symbol = std::find_if(
std::begin(symtab_symbols_), std::end(symtab_symbols_),
[&name] (const std::unique_ptr<Symbol>& s) {
return s->name() == name;
});
if (it_symbol == std::end(symtab_symbols_)) {
if (!symtab_symbol_indices_.count(name)) {
return nullptr;
}
return it_symbol->get();
return symtab_symbols_[symtab_symbol_indices_.at(name)].get();
}


Expand Down Expand Up @@ -569,6 +531,7 @@ void Binary::remove_symtab_symbol(Symbol* symbol) {
return;
}

symtab_symbol_indices_.erase((*it_symbol)->name());
symtab_symbols_.erase(it_symbol);
}

Expand Down Expand Up @@ -683,6 +646,7 @@ void Binary::remove_dynamic_symbol(Symbol* symbol) {
}
}

dynamic_symbol_indices_.erase((*it_symbol)->name());
dynamic_symbols_.erase(it_symbol);
}

Expand Down Expand Up @@ -1575,6 +1539,7 @@ void Binary::strip() {

Symbol& Binary::add_symtab_symbol(const Symbol& symbol) {
symtab_symbols_.push_back(std::make_unique<Symbol>(symbol));
symtab_symbol_indices_.insert({symtab_symbols_.back()->name(), symtab_symbols_.size() - 1});
return *symtab_symbols_.back();
}

Expand All @@ -1591,6 +1556,7 @@ Symbol& Binary::add_dynamic_symbol(const Symbol& symbol, const SymbolVersion* ve
sym->symbol_version_ = symver.get();

dynamic_symbols_.push_back(std::move(sym));
dynamic_symbol_indices_.insert({dynamic_symbols_.back()->name(), dynamic_symbols_.size() - 1});
symbol_version_table_.push_back(std::move(symver));
return *dynamic_symbols_.back();
}
Expand Down
2 changes: 2 additions & 0 deletions src/ELF/Parser.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ ok_error_t Parser::parse_symtab_symbols(uint64_t offset, uint32_t nb_symbols,
}
link_symbol_section(*symbol);
binary_->symtab_symbols_.push_back(std::move(symbol));
binary_->symtab_symbol_indices_.insert({binary_->symtab_symbols_.back()->name(), binary_->symtab_symbols_.size() - 1});
}
return ok();
}
Expand Down Expand Up @@ -1257,6 +1258,7 @@ ok_error_t Parser::parse_dynamic_symbols(uint64_t offset) {
}
link_symbol_section(*symbol);
binary_->dynamic_symbols_.push_back(std::move(symbol));
binary_->dynamic_symbol_indices_.insert({binary_->dynamic_symbols_.back()->name(), binary_->dynamic_symbols_.size() - 1});
}
binary_->sizing_info_->dynsym = binary_->dynamic_symbols_.size() * sizeof(Elf_Sym);
if (const auto* dt_strsz = binary_->get(DynamicEntry::TAG::STRSZ)) {
Expand Down