[libunwind] Optimize dl_iterate_phdr's findUnwindSectionsByPhdr

Currently, findUnwindSectionsByPhdr is slightly micro-optimized for the
case where the first callback has the target address, and is otherwise
very inefficient -- it decodes .eh_frame_hdr even when no PT_LOAD
matches the PC. (If the FrameHeaderCache is enabled, then the
micro-optimization only helps the first time unwind info is looked up.)

Instead, it makes more sense to optimize for the case where the
callback *doesn't* find the target address, so search for a PT_LOAD
segment first, and only look for the unwind info section if a matching
PT_LOAD is found.

This change helps on an Android benchmark with 100 shared objects,
where the DSO at the end of the dl_iterate_phdr list throws 10000
exceptions. Assuming the frame cache is disabled, this change cuts
about 30-40% off the benchmark's runtime.

Reviewed By: compnerd, saugustine, #libunwind

Differential Revision: https://reviews.llvm.org/D87881

Cr-Mirrored-From: https://chromium.googlesource.com/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 881aba7071c6e4cc2417e875ca5027ec7c0a92a3
diff --git a/src/AddressSpace.hpp b/src/AddressSpace.hpp
index 7a926ec..171318f 100644
--- a/src/AddressSpace.hpp
+++ b/src/AddressSpace.hpp
@@ -462,29 +462,37 @@
 #endif
 
   Elf_Addr image_base = calculateImageBase(pinfo);
+
+  // Most shared objects seen in this callback function likely don't contain the
+  // target address, so optimize for that. Scan for a matching PT_LOAD segment
+  // first and bail when it isn't found.
   bool found_text = false;
-  bool found_unwind = false;
-
-  // Third phdr is usually the executable phdr.
-  if (pinfo->dlpi_phnum > 2)
-    found_text = checkAddrInSegment(&pinfo->dlpi_phdr[2], image_base, cbdata);
-
-  // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate
-  // backward. We already know that there is one or more phdrs.
-  for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) {
-    const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1];
-    if (!found_unwind && checkForUnwindInfoSegment(phdr, image_base, cbdata))
-      found_unwind = true;
-    else if (!found_text && checkAddrInSegment(phdr, image_base, cbdata))
+  for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) {
+    if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) {
       found_text = true;
-    if (found_text && found_unwind) {
-#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
-      TheFrameHeaderCache.add(cbdata->sects);
-#endif
-      return 1;
+      break;
     }
   }
-  return 0;
+  if (!found_text)
+    return 0;
+
+  // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate
+  // backward.
+  bool found_unwind = false;
+  for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) {
+    const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1];
+    if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) {
+      found_unwind = true;
+      break;
+    }
+  }
+  if (!found_unwind)
+    return 0;
+
+#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
+  TheFrameHeaderCache.add(cbdata->sects);
+#endif
+  return 1;
 }
 
 #endif  // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)