fix _dyld_find_unwind_sections() for pre-10.7.  Patch by Jeremy Sequoia

git-svn-id: https://llvm.org/svn/llvm-project/libunwind/trunk@285636 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/src/AddressSpace.hpp b/src/AddressSpace.hpp
index 2792e0e..67aa664 100644
--- a/src/AddressSpace.hpp
+++ b/src/AddressSpace.hpp
@@ -321,28 +321,34 @@
     // In 10.7.0 or later, libSystem.dylib implements this function.
     extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
   #else
-    // In 10.6.x and earlier, we need to implement this functionality.
+    // In 10.6.x and earlier, we need to implement this functionality. Note
+    // that this requires a newer version of libmacho (from cctools) than is
+    // present in libSystem on 10.6.x (for getsectiondata).
     static inline bool _dyld_find_unwind_sections(void* addr, 
                                                     dyld_unwind_sections* info) {
       // Find mach-o image containing address.
       Dl_info dlinfo;
       if (!dladdr(addr, &dlinfo))
         return false;
-      const mach_header *mh = (const mach_header *)dlinfo.dli_saddr;
-      
-      // Find DWARF unwind section in that image.
-      unsigned long size;
-      const uint8_t *p = getsectiondata(mh, "__TEXT", "__eh_frame", &size);
-      if (!p)
-        return false;
-      
-      // Fill in return struct.
-      info->mh = mh;
-      info->dwarf_section = p;
-      info->dwarf_section_length = size;
-      info->compact_unwind_section = 0;
-      info->compact_unwind_section_length = 0;
-     
+#if __LP64__
+      const struct mach_header_64 *mh = (const struct mach_header_64 *)dlinfo.dli_fbase;
+#else
+      const struct mach_header *mh = (const struct mach_header *)dlinfo.dli_fbase;
+#endif
+
+      // Initialize the return struct
+      info->mh = (const struct mach_header *)mh;
+      info->dwarf_section = getsectiondata(mh, "__TEXT", "__eh_frame", &info->dwarf_section_length);
+      info->compact_unwind_section = getsectiondata(mh, "__TEXT", "__unwind_info", &info->compact_unwind_section_length);
+
+      if (!info->dwarf_section) {
+        info->dwarf_section_length = 0;
+      }
+
+      if (!info->compact_unwind_section) {
+        info->compact_unwind_section_length = 0;
+      }
+
       return true;
     }
   #endif