[libcxx-commits] [PATCH] D87881: [libunwind] Optimize dl_iterate_phdr's findUnwindSectionsByPhdr

Ryan Prichard via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 23 15:42:33 PDT 2020


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG881aba7071c6: [libunwind] Optimize dl_iterate_phdr's findUnwindSectionsByPhdr (authored by rprichard).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87881/new/

https://reviews.llvm.org/D87881

Files:
  libunwind/src/AddressSpace.hpp


Index: libunwind/src/AddressSpace.hpp
===================================================================
--- libunwind/src/AddressSpace.hpp
+++ libunwind/src/AddressSpace.hpp
@@ -462,29 +462,37 @@
 #endif
 
   Elf_Addr image_base = calculateImageBase(pinfo);
-  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);
+  // 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;
+  for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) {
+    if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) {
+      found_text = true;
+      break;
+    }
+  }
+  if (!found_text)
+    return 0;
 
   // 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.
+  // 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 (!found_unwind && checkForUnwindInfoSegment(phdr, image_base, cbdata))
+    if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) {
       found_unwind = true;
-    else if (!found_text && checkAddrInSegment(phdr, 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_unwind)
+    return 0;
+
+#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
+  TheFrameHeaderCache.add(cbdata->sects);
+#endif
+  return 1;
 }
 
 #endif  // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87881.293882.patch
Type: text/x-patch
Size: 1897 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200923/b18ca51a/attachment.bin>


More information about the libcxx-commits mailing list