Summary:
When using `folly::symbolizer`, it's very often that we want to use `iterateSymbolsWithType` iterate through symbols of a few types using the same callback. Current approach would require iterating the section multiple times.
This Diff adds `iterateSymbolsWithTypes`, which is basically just `iterateSymbolsWithType` but accepts symbol types.
This Diff also updated implementation of `getDefinitionByAddress` and `getSymbolByName` which currently does two iterations for `STT_OBJECT` and `STT_FUNC`.
Reviewed By: yfeldblum
Differential Revision:
D6279651
fbshipit-source-id:
a661dd15f18e4f2f63dbcca615f5a86d92e528ea
});
}
+template <class Fn>
+const ElfSym* ElfFile::iterateSymbolsWithTypes(
+ const ElfShdr& section,
+ std::initializer_list<uint32_t> types,
+ Fn fn) const {
+ // N.B. st_info has the same representation on 32- and 64-bit platforms
+ return iterateSymbols(section, [&](const ElfSym& sym) -> bool {
+ auto const elfType = ELF32_ST_TYPE(sym.st_info);
+ auto const it = std::find(types.begin(), types.end(), elfType);
+ return it != types.end() && fn(sym);
+ });
+}
+
} // namespace symbolizer
} // namespace folly
return false;
};
- return iterateSymbolsWithType(section, STT_OBJECT, findSymbols) ||
- iterateSymbolsWithType(section, STT_FUNC, findSymbols);
+ return iterateSymbolsWithTypes(
+ section, {STT_OBJECT, STT_FUNC}, findSymbols);
};
// Try the .dynsym section first if it exists, it's smaller.
return false;
};
- return iterateSymbolsWithType(section, STT_OBJECT, findSymbols) ||
- iterateSymbolsWithType(section, STT_FUNC, findSymbols);
+ return iterateSymbolsWithTypes(
+ section, {STT_OBJECT, STT_FUNC}, findSymbols);
};
// Try the .dynsym section first if it exists, it's smaller.
#include <link.h> // For ElfW()
#include <cstdio>
+#include <initializer_list>
#include <stdexcept>
#include <system_error>
template <class Fn>
const ElfSym*
iterateSymbolsWithType(const ElfShdr& section, uint32_t type, Fn fn) const;
+ template <class Fn>
+ const ElfSym* iterateSymbolsWithTypes(
+ const ElfShdr& section,
+ std::initializer_list<uint32_t> types,
+ Fn fn) const;
/**
* Find symbol definition by address.