Gstep.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2008 CodeSourcery
  3. Copyright 2011 Linaro Limited
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #include "unwind_i.h"
  22. #include "offsets.h"
  23. #include "ex_tables.h"
  24. #include <signal.h>
  25. #define arm_exidx_step UNW_OBJ(arm_exidx_step)
  26. static inline int
  27. arm_exidx_step (struct cursor *c)
  28. {
  29. unw_word_t old_ip, old_cfa;
  30. uint8_t buf[32];
  31. int ret;
  32. old_ip = c->dwarf.ip;
  33. old_cfa = c->dwarf.cfa;
  34. /* mark PC unsaved */
  35. c->dwarf.loc[UNW_ARM_R15] = DWARF_NULL_LOC;
  36. if ((ret = tdep_find_proc_info (&c->dwarf, c->dwarf.ip, 1)) < 0)
  37. return ret;
  38. if (c->dwarf.pi.format != UNW_INFO_FORMAT_ARM_EXIDX)
  39. return -UNW_ENOINFO;
  40. ret = arm_exidx_extract (&c->dwarf, buf);
  41. if (ret == -UNW_ESTOPUNWIND)
  42. return 0;
  43. else if (ret < 0)
  44. return ret;
  45. ret = arm_exidx_decode (buf, ret, &c->dwarf);
  46. if (ret < 0)
  47. return ret;
  48. if (c->dwarf.ip == old_ip && c->dwarf.cfa == old_cfa)
  49. {
  50. Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n",
  51. __FUNCTION__, (long) c->dwarf.ip);
  52. return -UNW_EBADFRAME;
  53. }
  54. return (c->dwarf.ip == 0) ? 0 : 1;
  55. }
  56. PROTECTED int
  57. unw_handle_signal_frame (unw_cursor_t *cursor)
  58. {
  59. struct cursor *c = (struct cursor *) cursor;
  60. int ret;
  61. unw_word_t sc_addr, sp, sp_addr = c->dwarf.cfa;
  62. struct dwarf_loc sp_loc = DWARF_LOC (sp_addr, 0);
  63. if ((ret = dwarf_get (&c->dwarf, sp_loc, &sp)) < 0)
  64. return -UNW_EUNSPEC;
  65. /* Obtain signal frame type (non-RT or RT). */
  66. ret = unw_is_signal_frame (cursor);
  67. /* Save the SP and PC to be able to return execution at this point
  68. later in time (unw_resume). */
  69. c->sigcontext_sp = c->dwarf.cfa;
  70. c->sigcontext_pc = c->dwarf.ip;
  71. /* Since kernel version 2.6.18 the non-RT signal frame starts with a
  72. ucontext while the RT signal frame starts with a siginfo, followed
  73. by a sigframe whose first element is an ucontext.
  74. Prior 2.6.18 the non-RT signal frame starts with a sigcontext while
  75. the RT signal frame starts with two pointers followed by a siginfo
  76. and an ucontext. The first pointer points to the start of the siginfo
  77. structure and the second one to the ucontext structure. */
  78. if (ret == 1)
  79. {
  80. /* Handle non-RT signal frames. Check if the first word on the stack
  81. is the magic number. */
  82. if (sp == 0x5ac3c35a)
  83. {
  84. c->sigcontext_format = ARM_SCF_LINUX_SIGFRAME;
  85. sc_addr = sp_addr + LINUX_UC_MCONTEXT_OFF;
  86. }
  87. else
  88. {
  89. c->sigcontext_format = ARM_SCF_LINUX_OLD_SIGFRAME;
  90. sc_addr = sp_addr;
  91. }
  92. c->sigcontext_addr = sp_addr;
  93. }
  94. else if (ret == 2)
  95. {
  96. /* Handle RT signal frames. Check if the first word on the stack is a
  97. pointer to the siginfo structure. */
  98. if (sp == sp_addr + 8)
  99. {
  100. c->sigcontext_format = ARM_SCF_LINUX_OLD_RT_SIGFRAME;
  101. c->sigcontext_addr = sp_addr + 8 + sizeof (siginfo_t);
  102. }
  103. else
  104. {
  105. c->sigcontext_format = ARM_SCF_LINUX_RT_SIGFRAME;
  106. c->sigcontext_addr = sp_addr + sizeof (siginfo_t);
  107. }
  108. sc_addr = c->sigcontext_addr + LINUX_UC_MCONTEXT_OFF;
  109. }
  110. else
  111. return -UNW_EUNSPEC;
  112. /* Update the dwarf cursor.
  113. Set the location of the registers to the corresponding addresses of the
  114. uc_mcontext / sigcontext structure contents. */
  115. c->dwarf.loc[UNW_ARM_R0] = DWARF_LOC (sc_addr + LINUX_SC_R0_OFF, 0);
  116. c->dwarf.loc[UNW_ARM_R1] = DWARF_LOC (sc_addr + LINUX_SC_R1_OFF, 0);
  117. c->dwarf.loc[UNW_ARM_R2] = DWARF_LOC (sc_addr + LINUX_SC_R2_OFF, 0);
  118. c->dwarf.loc[UNW_ARM_R3] = DWARF_LOC (sc_addr + LINUX_SC_R3_OFF, 0);
  119. c->dwarf.loc[UNW_ARM_R4] = DWARF_LOC (sc_addr + LINUX_SC_R4_OFF, 0);
  120. c->dwarf.loc[UNW_ARM_R5] = DWARF_LOC (sc_addr + LINUX_SC_R5_OFF, 0);
  121. c->dwarf.loc[UNW_ARM_R6] = DWARF_LOC (sc_addr + LINUX_SC_R6_OFF, 0);
  122. c->dwarf.loc[UNW_ARM_R7] = DWARF_LOC (sc_addr + LINUX_SC_R7_OFF, 0);
  123. c->dwarf.loc[UNW_ARM_R8] = DWARF_LOC (sc_addr + LINUX_SC_R8_OFF, 0);
  124. c->dwarf.loc[UNW_ARM_R9] = DWARF_LOC (sc_addr + LINUX_SC_R9_OFF, 0);
  125. c->dwarf.loc[UNW_ARM_R10] = DWARF_LOC (sc_addr + LINUX_SC_R10_OFF, 0);
  126. c->dwarf.loc[UNW_ARM_R11] = DWARF_LOC (sc_addr + LINUX_SC_FP_OFF, 0);
  127. c->dwarf.loc[UNW_ARM_R12] = DWARF_LOC (sc_addr + LINUX_SC_IP_OFF, 0);
  128. c->dwarf.loc[UNW_ARM_R13] = DWARF_LOC (sc_addr + LINUX_SC_SP_OFF, 0);
  129. c->dwarf.loc[UNW_ARM_R14] = DWARF_LOC (sc_addr + LINUX_SC_LR_OFF, 0);
  130. c->dwarf.loc[UNW_ARM_R15] = DWARF_LOC (sc_addr + LINUX_SC_PC_OFF, 0);
  131. /* Set SP/CFA and PC/IP. */
  132. dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R13], &c->dwarf.cfa);
  133. dwarf_get (&c->dwarf, c->dwarf.loc[UNW_ARM_R15], &c->dwarf.ip);
  134. return 1;
  135. }
  136. PROTECTED int
  137. unw_step (unw_cursor_t *cursor)
  138. {
  139. struct cursor *c = (struct cursor *) cursor;
  140. int ret = -UNW_EUNSPEC;
  141. Debug (1, "(cursor=%p)\n", c);
  142. /* Check if this is a signal frame. */
  143. if (unw_is_signal_frame (cursor))
  144. return unw_handle_signal_frame (cursor);
  145. #ifdef CONFIG_DEBUG_FRAME
  146. /* First, try DWARF-based unwinding. */
  147. if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF))
  148. {
  149. ret = dwarf_step (&c->dwarf);
  150. Debug(1, "dwarf_step()=%d\n", ret);
  151. if (likely (ret > 0))
  152. return 1;
  153. else if (unlikely (ret == -UNW_ESTOPUNWIND))
  154. return ret;
  155. if (ret < 0 && ret != -UNW_ENOINFO)
  156. {
  157. Debug (2, "returning %d\n", ret);
  158. return ret;
  159. }
  160. }
  161. #endif /* CONFIG_DEBUG_FRAME */
  162. /* Next, try extbl-based unwinding. */
  163. if (UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX))
  164. {
  165. ret = arm_exidx_step (c);
  166. if (ret > 0)
  167. return 1;
  168. if (ret == -UNW_ESTOPUNWIND || ret == 0)
  169. return ret;
  170. }
  171. /* Fall back on APCS frame parsing.
  172. Note: This won't work in case the ARM EABI is used. */
  173. if (unlikely (ret < 0))
  174. {
  175. if (UNW_TRY_METHOD(UNW_ARM_METHOD_FRAME))
  176. {
  177. ret = UNW_ESUCCESS;
  178. /* DWARF unwinding failed, try to follow APCS/optimized APCS frame chain */
  179. unw_word_t instr, i;
  180. Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
  181. dwarf_loc_t ip_loc, fp_loc;
  182. unw_word_t frame;
  183. /* Mark all registers unsaved, since we don't know where
  184. they are saved (if at all), except for the EBP and
  185. EIP. */
  186. if (dwarf_get(&c->dwarf, c->dwarf.loc[UNW_ARM_R11], &frame) < 0)
  187. {
  188. return 0;
  189. }
  190. for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i) {
  191. c->dwarf.loc[i] = DWARF_NULL_LOC;
  192. }
  193. if (frame)
  194. {
  195. if (dwarf_get(&c->dwarf, DWARF_LOC(frame, 0), &instr) < 0)
  196. {
  197. return 0;
  198. }
  199. instr -= 8;
  200. if (dwarf_get(&c->dwarf, DWARF_LOC(instr, 0), &instr) < 0)
  201. {
  202. return 0;
  203. }
  204. if ((instr & 0xFFFFD800) == 0xE92DD800)
  205. {
  206. /* Standard APCS frame. */
  207. ip_loc = DWARF_LOC(frame - 4, 0);
  208. fp_loc = DWARF_LOC(frame - 12, 0);
  209. }
  210. else
  211. {
  212. /* Codesourcery optimized normal frame. */
  213. ip_loc = DWARF_LOC(frame, 0);
  214. fp_loc = DWARF_LOC(frame - 4, 0);
  215. }
  216. if (dwarf_get(&c->dwarf, ip_loc, &c->dwarf.ip) < 0)
  217. {
  218. return 0;
  219. }
  220. c->dwarf.loc[UNW_ARM_R12] = ip_loc;
  221. c->dwarf.loc[UNW_ARM_R11] = fp_loc;
  222. Debug(15, "ip=%lx\n", c->dwarf.ip);
  223. }
  224. else
  225. {
  226. ret = -UNW_ENOINFO;
  227. }
  228. }
  229. }
  230. return ret == -UNW_ENOINFO ? 0 : 1;
  231. }