[libcxx-commits] [PATCH] D59694: [PPC64][libunwind] Fix r2 not properly restored

Leandro Lupori via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 22 06:55:35 PDT 2019


luporl created this revision.
Herald added subscribers: libcxx-commits, jdoerfert, jsji, christof.
Herald added a project: libc++.

This change makes each unwind step inspect the instruction at the
return address and, if needed, read r2 from its saved location and
modify the context appropriately.

One point that (maybe) could be improved is to try to find if the
binary being unwound is ELFv1 or ELFv2 at run time, instead of at
compile time, or, find out the correct save location of r2 in some
other way.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D59694

Files:
  libunwind/src/DwarfInstructions.hpp


Index: libunwind/src/DwarfInstructions.hpp
===================================================================
--- libunwind/src/DwarfInstructions.hpp
+++ libunwind/src/DwarfInstructions.hpp
@@ -232,6 +232,27 @@
       }
 #endif
 
+#if defined(_LIBUNWIND_TARGET_PPC64)
+      // If the instruction at return address is a TOC (r2) restore
+      // then r2 was saved and needs to be restored
+      if (R::getArch() == REGISTERS_PPC64 && returnAddress != 0 &&
+          addressSpace.get32(returnAddress)
+#if defined(_CALL_ELF) && _CALL_ELF == 2
+              == 0xe8410018 // ld r2,24(r1)
+#else
+              == 0xe8410028 // ld r2,40(r1)
+#endif
+      ) {
+        pint_t sp = newRegisters.getRegister(UNW_REG_SP);
+#if defined(_CALL_ELF) && _CALL_ELF == 2
+        pint_t r2 = addressSpace.get64(sp + 24);
+#else
+        pint_t r2 = addressSpace.get64(sp + 40);
+#endif
+        newRegisters.setRegister(UNW_PPC64_R2, r2);
+      }
+#endif
+
       // Return address is address after call site instruction, so setting IP to
       // that does simualates a return.
       newRegisters.setIP(returnAddress);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59694.191875.patch
Type: text/x-patch
Size: 1115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190322/48ee980b/attachment.bin>


More information about the libcxx-commits mailing list