Add a multi-type version of iterateSectionsWithType
authorTeng Qin <qinteng@fb.com>
Thu, 9 Nov 2017 05:43:58 +0000 (21:43 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 9 Nov 2017 06:00:31 +0000 (22:00 -0800)
Summary:
When using `folly::symbolizer`, it's very often that we want to use `iterateSectionsWithType` iterate through sections of a few types using the same callback. Current approach would require iterating the section header multiple times.

This Diff add `iterateSectionsWithTypes`, which is basically just `iterateSectionsWithType` but accepts multiple section types.

It is very similar to D6279651. However, in this Diff we did not change implementation of `getDefinitionByAddress` and `getSymbolByName`, since going through `.dynsym` separately would improve the efficiency of single-address or single-symbol lookup. However, for the use cases that we want to iterate through all symbols of an ELF file, this new interface would be useful.

Reviewed By: anakryiko, yfeldblum

Differential Revision: D6281449

fbshipit-source-id: f9afe0a0e95d9fafcd041014abad8ca86d1a882f

folly/experimental/symbolizer/Elf-inl.h
folly/experimental/symbolizer/Elf.h

index 3e2f4dcf3ba70a9f2d306c0d8e2e2dcb4b87b8f3..51890e40e455420f16509592e9345033fc07f384 100644 (file)
@@ -51,6 +51,16 @@ const ElfShdr* ElfFile::iterateSectionsWithType(uint32_t type, Fn fn) const {
       [&](const ElfShdr& sh) { return sh.sh_type == type && fn(sh); });
 }
 
+template <class Fn>
+const ElfShdr* ElfFile::iterateSectionsWithTypes(
+    std::initializer_list<uint32_t> types,
+    Fn fn) const {
+  return iterateSections([&](const ElfShdr& sh) {
+    auto const it = std::find(types.begin(), types.end(), sh.sh_type);
+    return it != types.end() && fn(sh);
+  });
+}
+
 template <class Fn>
 const char* ElfFile::iterateStrings(const ElfShdr& stringTable, Fn fn) const {
   validateStringTable(stringTable);
index 477d8798b512f50968a6825081ecfb427748f238..a4f5afd22553ca9045c2571bf5ff98b07dcdaf6e 100644 (file)
@@ -140,12 +140,21 @@ class ElfFile {
   const ElfShdr* iterateSections(Fn fn) const;
 
   /**
-   * Iterate over all sections with a given type.  Similar to
+   * Iterate over all sections with a given type. Similar to
    * iterateSections(), but filtered only for sections with the given type.
    */
   template <class Fn>
   const ElfShdr* iterateSectionsWithType(uint32_t type, Fn fn) const;
 
+  /**
+   * Iterate over all sections with a given types. Similar to
+   * iterateSectionWithTypes(), but filtered on multiple types.
+   */
+  template <class Fn>
+  const ElfShdr* iterateSectionsWithTypes(
+      std::initializer_list<uint32_t> types,
+      Fn fn) const;
+
   /**
    * Iterate over all symbols witin a given section.
    *