Commit fddc6338 authored by Teng Qin's avatar Teng Qin Committed by Facebook Github Bot

Add a multi-type version of iterateSymbolsWithType

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
parent 3066914f
......@@ -97,5 +97,18 @@ const ElfSym* ElfFile::iterateSymbolsWithType(
});
}
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
......@@ -360,8 +360,8 @@ ElfFile::Symbol ElfFile::getDefinitionByAddress(uintptr_t address) const {
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.
......@@ -399,8 +399,8 @@ ElfFile::Symbol ElfFile::getSymbolByName(const char* name) const {
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.
......
......@@ -23,6 +23,7 @@
#include <link.h> // For ElfW()
#include <cstdio>
#include <initializer_list>
#include <stdexcept>
#include <system_error>
......@@ -156,6 +157,11 @@ class ElfFile {
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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment