Add a multi-type version of iterateSymbolsWithType
[folly.git] / folly / experimental / symbolizer / Elf-inl.h
index 1679f5c7afc68aaa20f2262e27b574b16096142e..3e2f4dcf3ba70a9f2d306c0d8e2e2dcb4b87b8f3 100644 (file)
 namespace folly {
 namespace symbolizer {
 
+template <class Fn>
+const ElfPhdr* ElfFile::iterateProgramHeaders(Fn fn) const {
+  const ElfPhdr* ptr = &at<ElfPhdr>(elfHeader().e_phoff);
+  for (size_t i = 0; i < elfHeader().e_phnum; i++, ptr++) {
+    if (fn(*ptr)) {
+      return ptr;
+    }
+  }
+
+  return nullptr;
+}
+
 template <class Fn>
 const ElfShdr* ElfFile::iterateSections(Fn fn) const {
   const ElfShdr* ptr = &at<ElfShdr>(elfHeader().e_shoff);
@@ -85,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