Skip to content

Commit 5822464

Browse files
committed
Use list of prefixes to reduce symbol searching logic
Signed-off-by: Dom Del Nano <ddelnano@gmail.com>
1 parent 9ffd687 commit 5822464

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/stirling/obj_tools/go_syms.cc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,29 @@ StatusOr<std::string> ReadGoBuildVersion(ElfReader* elf_reader) {
144144
return ReadGoString(elf_reader, ptr_size, ptr_addr, read_ptr);
145145
}
146146

147+
// Prefixes used to search for itable symbols in the binary. Follows the format:
148+
// <prefix>.<type_name>,<interface_name>. i.e. go.itab.<type_name>,<interface_name>
149+
constexpr std::array<std::string_view, 2> kITablePrefixes = {
150+
"go:itab.", // Prefix used by Go 1.20 binaries and later.
151+
"go.itab.", // Prefix used by Go 1.19 binaries and earlier.
152+
};
153+
147154
StatusOr<absl::flat_hash_map<std::string, std::vector<IntfImplTypeInfo>>> ExtractGolangInterfaces(
148155
ElfReader* elf_reader) {
149156
absl::flat_hash_map<std::string, std::vector<IntfImplTypeInfo>> interface_types;
150157

151-
// Go 1.20 binaries and later use go:itab.<type_name>,<interface_name> as the symbol name.
152-
// Go 1.19 binaries and earlier use go.itab.<type_name>,<interface_name> as the symbol name.
153-
// Optimistically try the newer format first.
154-
std::string_view kITablePrefix("go:itab.");
155-
156-
PX_ASSIGN_OR_RETURN(std::vector<ElfReader::SymbolInfo> itable_symbols,
157-
elf_reader->SearchSymbols(kITablePrefix, SymbolMatchType::kPrefix,
158-
/*symbol_type*/ ELFIO::STT_OBJECT));
159-
if (itable_symbols.empty()) {
160-
kITablePrefix = "go.itab.";
158+
std::vector<ElfReader::SymbolInfo> itable_symbols;
159+
std::string_view iTablePrefix;
160+
for (const auto& prefix : kITablePrefixes) {
161161
PX_ASSIGN_OR_RETURN(itable_symbols,
162-
elf_reader->SearchSymbols(kITablePrefix, SymbolMatchType::kPrefix,
162+
elf_reader->SearchSymbols(prefix, SymbolMatchType::kPrefix,
163163
/*symbol_type*/ ELFIO::STT_OBJECT));
164+
if (!itable_symbols.empty()) {
165+
iTablePrefix = prefix;
166+
break;
167+
}
164168
}
169+
165170
for (const auto& sym : itable_symbols) {
166171
// Expected format is:
167172
// go.itab.<type_name>,<interface_name>
@@ -173,7 +178,7 @@ StatusOr<absl::flat_hash_map<std::string, std::vector<IntfImplTypeInfo>>> Extrac
173178

174179
std::string_view interface_name = sym_split[1];
175180
std::string_view type = sym_split[0];
176-
type.remove_prefix(kITablePrefix.size());
181+
type.remove_prefix(iTablePrefix.size());
177182

178183
IntfImplTypeInfo info;
179184

0 commit comments

Comments
 (0)