Skip to content

Commit

Permalink
Return an optional from lookup_symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Jan 29, 2025
1 parent 7f6945c commit 06c9c14
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
15 changes: 8 additions & 7 deletions src/binary/elf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "binary/elf.hpp"
#include "utils/optional.hpp"

#if IS_LINUX

Expand Down Expand Up @@ -88,23 +89,23 @@ namespace detail {
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
auto symtab_ = get_symtab();
if(symtab_.is_error()) {
return "";
return nullopt;
}
auto& maybe_symtab = symtab_.unwrap_value();
if(!maybe_symtab) {
return "";
return nullopt;
}
auto& symtab = maybe_symtab.unwrap();
if(symtab.strtab_link == SHN_UNDEF) {
return "";
return nullopt;
}
auto strtab_ = get_strtab(symtab.strtab_link);
if(strtab_.is_error()) {
return "";
return nullopt;
}
auto& strtab = strtab_.unwrap_value();
auto it = first_less_than_or_equal(
Expand All @@ -116,12 +117,12 @@ namespace detail {
}
);
if(it == symtab.entries.end()) {
return "";
return nullopt;
}
if(pc <= it->st_value + it->st_size) {
return strtab.data() + it->st_name;
}
return "";
return nullopt;
}

template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>
Expand Down
2 changes: 1 addition & 1 deletion src/binary/elf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace detail {
Result<std::uintptr_t, internal_error> get_module_image_base_impl();

public:
std::string lookup_symbol(frame_ptr pc);
optional<std::string> lookup_symbol(frame_ptr pc);

private:
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
Expand Down
6 changes: 3 additions & 3 deletions src/binary/mach-o.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ namespace detail {
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();
if(!symtab_) {
return "";
return nullopt;
}
const auto& symtab = symtab_.unwrap_value();;
auto it = first_less_than_or_equal(
Expand All @@ -431,7 +431,7 @@ namespace detail {
}
);
if(it == symtab.end()) {
return "";
return nullopt;
}
ASSERT(pc >= it->address);
// TODO: We subtracted one from the address so name + diff won't show up in the objdump, decide if desirable
Expand Down
2 changes: 1 addition & 1 deletion src/binary/mach-o.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace detail {

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
static void print_debug_map(const debug_map& debug_map);
Expand Down
2 changes: 1 addition & 1 deletion src/symbols/symbols_with_libdwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace libdwarf {
}
#if IS_LINUX || IS_APPLE
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
}
Expand Down

0 comments on commit 06c9c14

Please sign in to comment.