Return an optional from lookup_symbol

This commit is contained in:
Jeremy Rifkin 2025-01-28 23:26:28 -06:00
parent 7f6945c7d9
commit 06c9c14995
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
5 changed files with 14 additions and 13 deletions

View File

@ -1,4 +1,5 @@
#include "binary/elf.hpp" #include "binary/elf.hpp"
#include "utils/optional.hpp"
#if IS_LINUX #if IS_LINUX
@ -88,23 +89,23 @@ namespace detail {
return 0; return 0;
} }
std::string elf::lookup_symbol(frame_ptr pc) { optional<std::string> elf::lookup_symbol(frame_ptr pc) {
// TODO: Also search the SHT_DYNSYM at some point, maybe // TODO: Also search the SHT_DYNSYM at some point, maybe
auto symtab_ = get_symtab(); auto symtab_ = get_symtab();
if(symtab_.is_error()) { if(symtab_.is_error()) {
return ""; return nullopt;
} }
auto& maybe_symtab = symtab_.unwrap_value(); auto& maybe_symtab = symtab_.unwrap_value();
if(!maybe_symtab) { if(!maybe_symtab) {
return ""; return nullopt;
} }
auto& symtab = maybe_symtab.unwrap(); auto& symtab = maybe_symtab.unwrap();
if(symtab.strtab_link == SHN_UNDEF) { if(symtab.strtab_link == SHN_UNDEF) {
return ""; return nullopt;
} }
auto strtab_ = get_strtab(symtab.strtab_link); auto strtab_ = get_strtab(symtab.strtab_link);
if(strtab_.is_error()) { if(strtab_.is_error()) {
return ""; return nullopt;
} }
auto& strtab = strtab_.unwrap_value(); auto& strtab = strtab_.unwrap_value();
auto it = first_less_than_or_equal( auto it = first_less_than_or_equal(
@ -116,12 +117,12 @@ namespace detail {
} }
); );
if(it == symtab.entries.end()) { if(it == symtab.entries.end()) {
return ""; return nullopt;
} }
if(pc <= it->st_value + it->st_size) { if(pc <= it->st_value + it->st_size) {
return strtab.data() + it->st_name; return strtab.data() + it->st_name;
} }
return ""; return nullopt;
} }
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type> template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>

View File

@ -76,7 +76,7 @@ namespace detail {
Result<std::uintptr_t, internal_error> get_module_image_base_impl(); Result<std::uintptr_t, internal_error> get_module_image_base_impl();
public: public:
std::string lookup_symbol(frame_ptr pc); optional<std::string> lookup_symbol(frame_ptr pc);
private: private:
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>

View File

@ -416,10 +416,10 @@ namespace detail {
return symbols.unwrap(); return symbols.unwrap();
} }
std::string mach_o::lookup_symbol(frame_ptr pc) { optional<std::string> mach_o::lookup_symbol(frame_ptr pc) {
auto symtab_ = symbol_table(); auto symtab_ = symbol_table();
if(!symtab_) { if(!symtab_) {
return ""; return nullopt;
} }
const auto& symtab = symtab_.unwrap_value();; const auto& symtab = symtab_.unwrap_value();;
auto it = first_less_than_or_equal( auto it = first_less_than_or_equal(
@ -431,7 +431,7 @@ namespace detail {
} }
); );
if(it == symtab.end()) { if(it == symtab.end()) {
return ""; return nullopt;
} }
ASSERT(pc >= it->address); ASSERT(pc >= it->address);
// TODO: We subtracted one from the address so name + diff won't show up in the objdump, decide if desirable // TODO: We subtracted one from the address so name + diff won't show up in the objdump, decide if desirable

View File

@ -112,7 +112,7 @@ namespace detail {
Result<const std::vector<symbol_entry>&, internal_error> symbol_table(); Result<const std::vector<symbol_entry>&, internal_error> symbol_table();
std::string lookup_symbol(frame_ptr pc); optional<std::string> lookup_symbol(frame_ptr pc);
// produce information similar to dsymutil -dump-debug-map // produce information similar to dsymutil -dump-debug-map
static void print_debug_map(const debug_map& debug_map); static void print_debug_map(const debug_map& debug_map);

View File

@ -116,7 +116,7 @@ namespace libdwarf {
} }
#if IS_LINUX || IS_APPLE #if IS_LINUX || IS_APPLE
if(frame.frame.symbol.empty() && object.has_value()) { if(frame.frame.symbol.empty() && object.has_value()) {
frame.frame.symbol = object.unwrap_value().lookup_symbol(dlframe.object_address); frame.frame.symbol = object.unwrap_value().lookup_symbol(dlframe.object_address).value_or("");
} }
#endif #endif
} }