libunwind_i.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2008 CodeSourcery
  3. This file is part of libunwind.
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  20. #ifndef MIPS_LIBUNWIND_I_H
  21. #define MIPS_LIBUNWIND_I_H
  22. /* Target-dependent definitions that are internal to libunwind but need
  23. to be shared with target-independent code. */
  24. #include <stdlib.h>
  25. #include <libunwind.h>
  26. #if !defined(UNW_REMOTE_ONLY) && _MIPS_SIM == _ABI64
  27. # include "elf64.h"
  28. #else
  29. # include "elf32.h"
  30. #endif
  31. #include "mempool.h"
  32. #include "dwarf.h"
  33. typedef struct
  34. {
  35. /* no mips-specific fast trace */
  36. }
  37. unw_tdep_frame_t;
  38. struct unw_addr_space
  39. {
  40. struct unw_accessors acc;
  41. int big_endian;
  42. mips_abi_t abi;
  43. unsigned int addr_size;
  44. unw_caching_policy_t caching_policy;
  45. #ifdef HAVE_ATOMIC_OPS_H
  46. AO_t cache_generation;
  47. #else
  48. uint32_t cache_generation;
  49. #endif
  50. unw_word_t dyn_generation; /* see dyn-common.h */
  51. unw_word_t dyn_info_list_addr; /* (cached) dyn_info_list_addr */
  52. struct dwarf_rs_cache global_cache;
  53. struct unw_debug_frame_list *debug_frames;
  54. };
  55. #define tdep_big_endian(as) ((as)->big_endian)
  56. struct cursor
  57. {
  58. struct dwarf_cursor dwarf; /* must be first */
  59. unw_word_t sigcontext_addr;
  60. };
  61. #define DWARF_GET_LOC(l) ((l).val)
  62. #ifndef UNW_REMOTE_ONLY
  63. # if _MIPS_SIM == _ABIN32
  64. typedef long long mips_reg_t;
  65. # else
  66. typedef long mips_reg_t;
  67. # endif
  68. #endif
  69. #ifdef UNW_LOCAL_ONLY
  70. # define DWARF_NULL_LOC DWARF_LOC (0, 0)
  71. # define DWARF_IS_NULL_LOC(l) (DWARF_GET_LOC (l) == 0)
  72. # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r) })
  73. # define DWARF_IS_REG_LOC(l) 0
  74. # define DWARF_REG_LOC(c,r) (DWARF_LOC((unw_word_t) (intptr_t) \
  75. tdep_uc_addr((c)->as_arg, (r)), 0))
  76. # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
  77. # define DWARF_FPREG_LOC(c,r) (DWARF_LOC((unw_word_t) (intptr_t) \
  78. tdep_uc_addr((c)->as_arg, (r)), 0))
  79. /* FIXME: Implement these for the MIPS FPU. */
  80. static inline int
  81. dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
  82. {
  83. if (!DWARF_GET_LOC (loc))
  84. return -1;
  85. *val = *(unw_fpreg_t *) (intptr_t) DWARF_GET_LOC (loc);
  86. return 0;
  87. }
  88. static inline int
  89. dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
  90. {
  91. if (!DWARF_GET_LOC (loc))
  92. return -1;
  93. *(unw_fpreg_t *) (intptr_t) DWARF_GET_LOC (loc) = val;
  94. return 0;
  95. }
  96. static inline int
  97. dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
  98. {
  99. if (!DWARF_GET_LOC (loc))
  100. return -1;
  101. *val = *(mips_reg_t *) (intptr_t) DWARF_GET_LOC (loc);
  102. return 0;
  103. }
  104. static inline int
  105. dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
  106. {
  107. if (!DWARF_GET_LOC (loc))
  108. return -1;
  109. *(mips_reg_t *) (intptr_t) DWARF_GET_LOC (loc) = val;
  110. return 0;
  111. }
  112. #else /* !UNW_LOCAL_ONLY */
  113. # define DWARF_LOC_TYPE_FP (1 << 0)
  114. # define DWARF_LOC_TYPE_REG (1 << 1)
  115. # define DWARF_NULL_LOC DWARF_LOC (0, 0)
  116. # define DWARF_IS_NULL_LOC(l) \
  117. ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
  118. # define DWARF_LOC(r, t) ((dwarf_loc_t) { .val = (r), .type = (t) })
  119. # define DWARF_IS_REG_LOC(l) (((l).type & DWARF_LOC_TYPE_REG) != 0)
  120. # define DWARF_IS_FP_LOC(l) (((l).type & DWARF_LOC_TYPE_FP) != 0)
  121. # define DWARF_REG_LOC(c,r) DWARF_LOC((r), DWARF_LOC_TYPE_REG)
  122. # define DWARF_MEM_LOC(c,m) DWARF_LOC ((m), 0)
  123. # define DWARF_FPREG_LOC(c,r) DWARF_LOC((r), (DWARF_LOC_TYPE_REG \
  124. | DWARF_LOC_TYPE_FP))
  125. static inline int
  126. read_s32 (struct dwarf_cursor *c, unw_word_t addr, unw_word_t *val)
  127. {
  128. int offset = addr & 4;
  129. int ret;
  130. unw_word_t memval;
  131. ret = (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 0, c->as_arg);
  132. if (ret < 0)
  133. return ret;
  134. if ((offset != 0) == tdep_big_endian (c->as))
  135. *val = (int32_t) memval;
  136. else
  137. *val = (int32_t) (memval >> 32);
  138. return 0;
  139. }
  140. static inline int
  141. write_s32 (struct dwarf_cursor *c, unw_word_t addr, const unw_word_t *val)
  142. {
  143. int offset = addr & 4;
  144. int ret;
  145. unw_word_t memval;
  146. ret = (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 0, c->as_arg);
  147. if (ret < 0)
  148. return ret;
  149. if ((offset != 0) == tdep_big_endian (c->as))
  150. memval = (memval & ~0xffffffffLL) | (uint32_t) *val;
  151. else
  152. memval = (memval & 0xffffffffLL) | (uint32_t) (*val << 32);
  153. return (*c->as->acc.access_mem) (c->as, addr - offset, &memval, 1, c->as_arg);
  154. }
  155. /* FIXME: Implement these for the MIPS FPU. */
  156. static inline int
  157. dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
  158. {
  159. char *valp = (char *) &val;
  160. unw_word_t addr;
  161. int ret;
  162. if (DWARF_IS_NULL_LOC (loc))
  163. return -UNW_EBADREG;
  164. if (DWARF_IS_REG_LOC (loc))
  165. return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
  166. val, 0, c->as_arg);
  167. addr = DWARF_GET_LOC (loc);
  168. if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
  169. 0, c->as_arg)) < 0)
  170. return ret;
  171. return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1, 0,
  172. c->as_arg);
  173. }
  174. static inline int
  175. dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
  176. {
  177. char *valp = (char *) &val;
  178. unw_word_t addr;
  179. int ret;
  180. if (DWARF_IS_NULL_LOC (loc))
  181. return -UNW_EBADREG;
  182. if (DWARF_IS_REG_LOC (loc))
  183. return (*c->as->acc.access_fpreg) (c->as, DWARF_GET_LOC (loc),
  184. &val, 1, c->as_arg);
  185. addr = DWARF_GET_LOC (loc);
  186. if ((ret = (*c->as->acc.access_mem) (c->as, addr + 0, (unw_word_t *) valp,
  187. 1, c->as_arg)) < 0)
  188. return ret;
  189. return (*c->as->acc.access_mem) (c->as, addr + 4, (unw_word_t *) valp + 1,
  190. 1, c->as_arg);
  191. }
  192. static inline int
  193. dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
  194. {
  195. if (DWARF_IS_NULL_LOC (loc))
  196. return -UNW_EBADREG;
  197. /* If a code-generator were to save a value of type unw_word_t in a
  198. floating-point register, we would have to support this case. I
  199. suppose it could happen with MMX registers, but does it really
  200. happen? */
  201. assert (!DWARF_IS_FP_LOC (loc));
  202. if (DWARF_IS_REG_LOC (loc))
  203. return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
  204. 0, c->as_arg);
  205. else if (c->as->abi == UNW_MIPS_ABI_O32)
  206. return read_s32 (c, DWARF_GET_LOC (loc), val);
  207. else
  208. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
  209. 0, c->as_arg);
  210. }
  211. static inline int
  212. dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
  213. {
  214. if (DWARF_IS_NULL_LOC (loc))
  215. return -UNW_EBADREG;
  216. /* If a code-generator were to save a value of type unw_word_t in a
  217. floating-point register, we would have to support this case. I
  218. suppose it could happen with MMX registers, but does it really
  219. happen? */
  220. assert (!DWARF_IS_FP_LOC (loc));
  221. if (DWARF_IS_REG_LOC (loc))
  222. return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
  223. 1, c->as_arg);
  224. else if (c->as->abi == UNW_MIPS_ABI_O32)
  225. return write_s32 (c, DWARF_GET_LOC (loc), &val);
  226. else
  227. return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
  228. 1, c->as_arg);
  229. }
  230. #endif /* !UNW_LOCAL_ONLY */
  231. #define tdep_getcontext_trace unw_getcontext
  232. #define tdep_needs_initialization UNW_OBJ(needs_initialization)
  233. #define tdep_init UNW_OBJ(init)
  234. /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
  235. tdep_search_unwind_table. */
  236. #define tdep_search_unwind_table dwarf_search_unwind_table
  237. #define tdep_find_unwind_table dwarf_find_unwind_table
  238. #define tdep_uc_addr UNW_ARCH_OBJ(uc_addr)
  239. #define tdep_get_elf_image UNW_ARCH_OBJ(get_elf_image)
  240. #define tdep_access_reg UNW_OBJ(access_reg)
  241. #define tdep_access_fpreg UNW_OBJ(access_fpreg)
  242. #define tdep_fetch_frame(c,ip,n) do {} while(0)
  243. #define tdep_cache_frame(c,rs) do {} while(0)
  244. #define tdep_reuse_frame(c,rs) do {} while(0)
  245. #define tdep_stash_frame(c,rs) do {} while(0)
  246. #define tdep_trace(cur,addr,n) (-UNW_ENOINFO)
  247. #ifdef UNW_LOCAL_ONLY
  248. # define tdep_find_proc_info(c,ip,n) \
  249. dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n), \
  250. (c)->as_arg)
  251. # define tdep_put_unwind_info(as,pi,arg) \
  252. dwarf_put_unwind_info((as), (pi), (arg))
  253. #else
  254. # define tdep_find_proc_info(c,ip,n) \
  255. (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n), \
  256. (c)->as_arg)
  257. # define tdep_put_unwind_info(as,pi,arg) \
  258. (*(as)->acc.put_unwind_info)((as), (pi), (arg))
  259. #endif
  260. #define tdep_get_as(c) ((c)->dwarf.as)
  261. #define tdep_get_as_arg(c) ((c)->dwarf.as_arg)
  262. #define tdep_get_ip(c) ((c)->dwarf.ip)
  263. extern int tdep_needs_initialization;
  264. extern void tdep_init (void);
  265. extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
  266. unw_dyn_info_t *di, unw_proc_info_t *pi,
  267. int need_unwind_info, void *arg);
  268. extern void *tdep_uc_addr (ucontext_t *uc, int reg);
  269. extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
  270. unsigned long *segbase, unsigned long *mapoff,
  271. char *path, size_t pathlen);
  272. extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
  273. unw_word_t *valp, int write);
  274. extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
  275. unw_fpreg_t *valp, int write);
  276. #endif /* MIPS_LIBUNWIND_I_H */