Gos-linux.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002-2004 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  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. PROTECTED int
  24. unw_is_signal_frame (unw_cursor_t *cursor)
  25. {
  26. struct cursor *c = (struct cursor *) cursor;
  27. unw_word_t w0, w1, ip;
  28. unw_addr_space_t as;
  29. unw_accessors_t *a;
  30. void *arg;
  31. int ret;
  32. as = c->dwarf.as;
  33. a = unw_get_accessors (as);
  34. arg = c->dwarf.as_arg;
  35. /* Check if EIP points at sigreturn() sequence. On Linux, this is:
  36. __restore:
  37. 0x58 pop %eax
  38. 0xb8 0x77 0x00 0x00 0x00 movl 0x77,%eax
  39. 0xcd 0x80 int 0x80
  40. without SA_SIGINFO, and
  41. __restore_rt:
  42. 0xb8 0xad 0x00 0x00 0x00 movl 0xad,%eax
  43. 0xcd 0x80 int 0x80
  44. 0x00
  45. if SA_SIGINFO is specified.
  46. */
  47. ip = c->dwarf.ip;
  48. if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0
  49. || (ret = (*a->access_mem) (as, ip + 4, &w1, 0, arg)) < 0)
  50. return ret;
  51. ret = ((w0 == 0x0077b858 && w1 == 0x80cd0000)
  52. || (w0 == 0x0000adb8 && (w1 & 0xffffff) == 0x80cd00));
  53. Debug (16, "returning %d\n", ret);
  54. return ret;
  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. /* c->esp points at the arguments to the handler. Without
  62. SA_SIGINFO, the arguments consist of a signal number
  63. followed by a struct sigcontext. With SA_SIGINFO, the
  64. arguments consist a signal number, a siginfo *, and a
  65. ucontext *. */
  66. unw_word_t sc_addr;
  67. unw_word_t siginfo_ptr_addr = c->dwarf.cfa + 4;
  68. unw_word_t sigcontext_ptr_addr = c->dwarf.cfa + 8;
  69. unw_word_t siginfo_ptr, sigcontext_ptr;
  70. struct dwarf_loc esp_loc, siginfo_ptr_loc, sigcontext_ptr_loc;
  71. siginfo_ptr_loc = DWARF_LOC (siginfo_ptr_addr, 0);
  72. sigcontext_ptr_loc = DWARF_LOC (sigcontext_ptr_addr, 0);
  73. ret = (dwarf_get (&c->dwarf, siginfo_ptr_loc, &siginfo_ptr)
  74. | dwarf_get (&c->dwarf, sigcontext_ptr_loc, &sigcontext_ptr));
  75. if (ret < 0)
  76. {
  77. Debug (2, "returning 0\n");
  78. return 0;
  79. }
  80. if (siginfo_ptr < c->dwarf.cfa
  81. || siginfo_ptr > c->dwarf.cfa + 256
  82. || sigcontext_ptr < c->dwarf.cfa
  83. || sigcontext_ptr > c->dwarf.cfa + 256)
  84. {
  85. /* Not plausible for SA_SIGINFO signal */
  86. c->sigcontext_format = X86_SCF_LINUX_SIGFRAME;
  87. c->sigcontext_addr = sc_addr = c->dwarf.cfa + 4;
  88. }
  89. else
  90. {
  91. /* If SA_SIGINFO were not specified, we actually read
  92. various segment pointers instead. We believe that at
  93. least fs and _fsh are always zero for linux, so it is
  94. not just unlikely, but impossible that we would end
  95. up here. */
  96. c->sigcontext_format = X86_SCF_LINUX_RT_SIGFRAME;
  97. c->sigcontext_addr = sigcontext_ptr;
  98. sc_addr = sigcontext_ptr + LINUX_UC_MCONTEXT_OFF;
  99. }
  100. esp_loc = DWARF_LOC (sc_addr + LINUX_SC_ESP_OFF, 0);
  101. ret = dwarf_get (&c->dwarf, esp_loc, &c->dwarf.cfa);
  102. if (ret < 0)
  103. {
  104. Debug (2, "returning 0\n");
  105. return 0;
  106. }
  107. c->dwarf.loc[EAX] = DWARF_LOC (sc_addr + LINUX_SC_EAX_OFF, 0);
  108. c->dwarf.loc[ECX] = DWARF_LOC (sc_addr + LINUX_SC_ECX_OFF, 0);
  109. c->dwarf.loc[EDX] = DWARF_LOC (sc_addr + LINUX_SC_EDX_OFF, 0);
  110. c->dwarf.loc[EBX] = DWARF_LOC (sc_addr + LINUX_SC_EBX_OFF, 0);
  111. c->dwarf.loc[EBP] = DWARF_LOC (sc_addr + LINUX_SC_EBP_OFF, 0);
  112. c->dwarf.loc[ESI] = DWARF_LOC (sc_addr + LINUX_SC_ESI_OFF, 0);
  113. c->dwarf.loc[EDI] = DWARF_LOC (sc_addr + LINUX_SC_EDI_OFF, 0);
  114. c->dwarf.loc[EFLAGS] = DWARF_NULL_LOC;
  115. c->dwarf.loc[TRAPNO] = DWARF_NULL_LOC;
  116. c->dwarf.loc[ST0] = DWARF_NULL_LOC;
  117. c->dwarf.loc[EIP] = DWARF_LOC (sc_addr + LINUX_SC_EIP_OFF, 0);
  118. c->dwarf.loc[ESP] = DWARF_LOC (sc_addr + LINUX_SC_ESP_OFF, 0);
  119. return 0;
  120. }
  121. HIDDEN dwarf_loc_t
  122. x86_get_scratch_loc (struct cursor *c, unw_regnum_t reg)
  123. {
  124. unw_word_t addr = c->sigcontext_addr, fpstate_addr, off;
  125. int ret, is_fpstate = 0;
  126. switch (c->sigcontext_format)
  127. {
  128. case X86_SCF_NONE:
  129. return DWARF_REG_LOC (&c->dwarf, reg);
  130. case X86_SCF_LINUX_SIGFRAME:
  131. break;
  132. case X86_SCF_LINUX_RT_SIGFRAME:
  133. addr += LINUX_UC_MCONTEXT_OFF;
  134. break;
  135. default:
  136. return DWARF_NULL_LOC;
  137. }
  138. switch (reg)
  139. {
  140. case UNW_X86_GS: off = LINUX_SC_GS_OFF; break;
  141. case UNW_X86_FS: off = LINUX_SC_FS_OFF; break;
  142. case UNW_X86_ES: off = LINUX_SC_ES_OFF; break;
  143. case UNW_X86_DS: off = LINUX_SC_DS_OFF; break;
  144. case UNW_X86_EDI: off = LINUX_SC_EDI_OFF; break;
  145. case UNW_X86_ESI: off = LINUX_SC_ESI_OFF; break;
  146. case UNW_X86_EBP: off = LINUX_SC_EBP_OFF; break;
  147. case UNW_X86_ESP: off = LINUX_SC_ESP_OFF; break;
  148. case UNW_X86_EBX: off = LINUX_SC_EBX_OFF; break;
  149. case UNW_X86_EDX: off = LINUX_SC_EDX_OFF; break;
  150. case UNW_X86_ECX: off = LINUX_SC_ECX_OFF; break;
  151. case UNW_X86_EAX: off = LINUX_SC_EAX_OFF; break;
  152. case UNW_X86_TRAPNO: off = LINUX_SC_TRAPNO_OFF; break;
  153. case UNW_X86_EIP: off = LINUX_SC_EIP_OFF; break;
  154. case UNW_X86_CS: off = LINUX_SC_CS_OFF; break;
  155. case UNW_X86_EFLAGS: off = LINUX_SC_EFLAGS_OFF; break;
  156. case UNW_X86_SS: off = LINUX_SC_SS_OFF; break;
  157. /* The following is probably not correct for all possible cases.
  158. Somebody who understands this better should review this for
  159. correctness. */
  160. case UNW_X86_FCW: is_fpstate = 1; off = LINUX_FPSTATE_CW_OFF; break;
  161. case UNW_X86_FSW: is_fpstate = 1; off = LINUX_FPSTATE_SW_OFF; break;
  162. case UNW_X86_FTW: is_fpstate = 1; off = LINUX_FPSTATE_TAG_OFF; break;
  163. case UNW_X86_FCS: is_fpstate = 1; off = LINUX_FPSTATE_CSSEL_OFF; break;
  164. case UNW_X86_FIP: is_fpstate = 1; off = LINUX_FPSTATE_IPOFF_OFF; break;
  165. case UNW_X86_FEA: is_fpstate = 1; off = LINUX_FPSTATE_DATAOFF_OFF; break;
  166. case UNW_X86_FDS: is_fpstate = 1; off = LINUX_FPSTATE_DATASEL_OFF; break;
  167. case UNW_X86_MXCSR: is_fpstate = 1; off = LINUX_FPSTATE_MXCSR_OFF; break;
  168. /* stacked fp registers */
  169. case UNW_X86_ST0: case UNW_X86_ST1: case UNW_X86_ST2: case UNW_X86_ST3:
  170. case UNW_X86_ST4: case UNW_X86_ST5: case UNW_X86_ST6: case UNW_X86_ST7:
  171. is_fpstate = 1;
  172. off = LINUX_FPSTATE_ST0_OFF + 10*(reg - UNW_X86_ST0);
  173. break;
  174. /* SSE fp registers */
  175. case UNW_X86_XMM0_lo: case UNW_X86_XMM0_hi:
  176. case UNW_X86_XMM1_lo: case UNW_X86_XMM1_hi:
  177. case UNW_X86_XMM2_lo: case UNW_X86_XMM2_hi:
  178. case UNW_X86_XMM3_lo: case UNW_X86_XMM3_hi:
  179. case UNW_X86_XMM4_lo: case UNW_X86_XMM4_hi:
  180. case UNW_X86_XMM5_lo: case UNW_X86_XMM5_hi:
  181. case UNW_X86_XMM6_lo: case UNW_X86_XMM6_hi:
  182. case UNW_X86_XMM7_lo: case UNW_X86_XMM7_hi:
  183. is_fpstate = 1;
  184. off = LINUX_FPSTATE_XMM0_OFF + 8*(reg - UNW_X86_XMM0_lo);
  185. break;
  186. case UNW_X86_XMM0:
  187. case UNW_X86_XMM1:
  188. case UNW_X86_XMM2:
  189. case UNW_X86_XMM3:
  190. case UNW_X86_XMM4:
  191. case UNW_X86_XMM5:
  192. case UNW_X86_XMM6:
  193. case UNW_X86_XMM7:
  194. is_fpstate = 1;
  195. off = LINUX_FPSTATE_XMM0_OFF + 16*(reg - UNW_X86_XMM0);
  196. break;
  197. case UNW_X86_FOP:
  198. case UNW_X86_TSS:
  199. case UNW_X86_LDT:
  200. default:
  201. return DWARF_REG_LOC (&c->dwarf, reg);
  202. }
  203. if (is_fpstate)
  204. {
  205. if ((ret = dwarf_get (&c->dwarf,
  206. DWARF_MEM_LOC (&c->dwarf,
  207. addr + LINUX_SC_FPSTATE_OFF),
  208. &fpstate_addr)) < 0)
  209. return DWARF_NULL_LOC;
  210. if (!fpstate_addr)
  211. return DWARF_NULL_LOC;
  212. return DWARF_MEM_LOC (c, fpstate_addr + off);
  213. }
  214. else
  215. return DWARF_MEM_LOC (c, addr + off);
  216. }
  217. #ifndef UNW_REMOTE_ONLY
  218. HIDDEN void *
  219. x86_r_uc_addr (ucontext_t *uc, int reg)
  220. {
  221. void *addr;
  222. switch (reg)
  223. {
  224. case UNW_X86_GS: addr = &uc->uc_mcontext.gregs[REG_GS]; break;
  225. case UNW_X86_FS: addr = &uc->uc_mcontext.gregs[REG_FS]; break;
  226. case UNW_X86_ES: addr = &uc->uc_mcontext.gregs[REG_ES]; break;
  227. case UNW_X86_DS: addr = &uc->uc_mcontext.gregs[REG_DS]; break;
  228. case UNW_X86_EAX: addr = &uc->uc_mcontext.gregs[REG_EAX]; break;
  229. case UNW_X86_EBX: addr = &uc->uc_mcontext.gregs[REG_EBX]; break;
  230. case UNW_X86_ECX: addr = &uc->uc_mcontext.gregs[REG_ECX]; break;
  231. case UNW_X86_EDX: addr = &uc->uc_mcontext.gregs[REG_EDX]; break;
  232. case UNW_X86_ESI: addr = &uc->uc_mcontext.gregs[REG_ESI]; break;
  233. case UNW_X86_EDI: addr = &uc->uc_mcontext.gregs[REG_EDI]; break;
  234. case UNW_X86_EBP: addr = &uc->uc_mcontext.gregs[REG_EBP]; break;
  235. case UNW_X86_EIP: addr = &uc->uc_mcontext.gregs[REG_EIP]; break;
  236. case UNW_X86_ESP: addr = &uc->uc_mcontext.gregs[REG_ESP]; break;
  237. case UNW_X86_TRAPNO: addr = &uc->uc_mcontext.gregs[REG_TRAPNO]; break;
  238. case UNW_X86_CS: addr = &uc->uc_mcontext.gregs[REG_CS]; break;
  239. case UNW_X86_EFLAGS: addr = &uc->uc_mcontext.gregs[REG_EFL]; break;
  240. case UNW_X86_SS: addr = &uc->uc_mcontext.gregs[REG_SS]; break;
  241. default:
  242. addr = NULL;
  243. }
  244. return addr;
  245. }
  246. HIDDEN int
  247. x86_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
  248. {
  249. struct cursor *c = (struct cursor *) cursor;
  250. ucontext_t *uc = c->uc;
  251. /* Ensure c->pi is up-to-date. On x86, it's relatively common to be
  252. missing DWARF unwind info. We don't want to fail in that case,
  253. because the frame-chain still would let us do a backtrace at
  254. least. */
  255. dwarf_make_proc_info (&c->dwarf);
  256. if (unlikely (c->sigcontext_format != X86_SCF_NONE))
  257. {
  258. #if (!HAVE_SGX)
  259. struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
  260. Debug (8, "resuming at ip=%x via sigreturn(%p)\n", c->dwarf.ip, sc);
  261. sigreturn (sc);
  262. #endif
  263. }
  264. else
  265. {
  266. Debug (8, "resuming at ip=%x via setcontext()\n", c->dwarf.ip);
  267. setcontext (uc);
  268. }
  269. return -UNW_EINVAL;
  270. }
  271. #endif