Skip to content

Commit

Permalink
Use list of prefixes to reduce symbol searching logic
Browse files Browse the repository at this point in the history
Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Jan 31, 2025
1 parent 9ffd687 commit f3e1b76
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/stirling/obj_tools/go_syms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,29 @@ StatusOr<std::string> ReadGoBuildVersion(ElfReader* elf_reader) {
return ReadGoString(elf_reader, ptr_size, ptr_addr, read_ptr);
}

// Prefixes used to search for itable symbols in the binary. Follows the format:
// <prefix>.<type_name>,<interface_name>. i.e. go.itab.<type_name>,<interface_name>
constexpr std::array<std::string_view, 2> kITablePrefixes = {
"go:itab.", // Prefix used by Go 1.20 binaries and later.
"go.itab.", // Prefix used by Go 1.19 binaries and earlier.
};

StatusOr<absl::flat_hash_map<std::string, std::vector<IntfImplTypeInfo>>> ExtractGolangInterfaces(
ElfReader* elf_reader) {
absl::flat_hash_map<std::string, std::vector<IntfImplTypeInfo>> interface_types;

// Go 1.20 binaries and later use go:itab.<type_name>,<interface_name> as the symbol name.
// Go 1.19 binaries and earlier use go.itab.<type_name>,<interface_name> as the symbol name.
// Optimistically try the newer format first.
std::string_view kITablePrefix("go:itab.");

PX_ASSIGN_OR_RETURN(std::vector<ElfReader::SymbolInfo> itable_symbols,
elf_reader->SearchSymbols(kITablePrefix, SymbolMatchType::kPrefix,
/*symbol_type*/ ELFIO::STT_OBJECT));
if (itable_symbols.empty()) {
kITablePrefix = "go.itab.";
std::vector<std::string> interface_names;
std::string_view iTablePrefix;
for (const auto& prefix : kITablePrefixes) {
PX_ASSIGN_OR_RETURN(itable_symbols,
elf_reader->SearchSymbols(kITablePrefix, SymbolMatchType::kPrefix,
elf_reader->SearchSymbols(prefix, SymbolMatchType::kPrefix,
/*symbol_type*/ ELFIO::STT_OBJECT));
if (!itable_symbols.empty()) {
iTablePrefix = prefix;
break;
}
}

for (const auto& sym : itable_symbols) {
// Expected format is:
// go.itab.<type_name>,<interface_name>
Expand All @@ -173,7 +178,7 @@ StatusOr<absl::flat_hash_map<std::string, std::vector<IntfImplTypeInfo>>> Extrac

std::string_view interface_name = sym_split[1];
std::string_view type = sym_split[0];
type.remove_prefix(kITablePrefix.size());
type.remove_prefix(iTablePrefix.size());

IntfImplTypeInfo info;

Expand Down

0 comments on commit f3e1b76

Please sign in to comment.