Ginit.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unwind_i.h"
  23. #ifdef UNW_REMOTE_ONLY
  24. /* unw_local_addr_space is a NULL pointer in this case. */
  25. PROTECTED unw_addr_space_t unw_local_addr_space;
  26. #else /* !UNW_REMOTE_ONLY */
  27. static struct unw_addr_space local_addr_space;
  28. PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
  29. /* Return the address of the 64-bit slot in UC for REG (even for o32,
  30. where registers are 32-bit, the slots are still 64-bit). */
  31. static inline void *
  32. uc_addr (ucontext_t *uc, int reg)
  33. {
  34. if (reg >= UNW_MIPS_R0 && reg < UNW_MIPS_R0 + 32)
  35. return &uc->uc_mcontext.gregs[reg - UNW_MIPS_R0];
  36. else
  37. return NULL;
  38. }
  39. # ifdef UNW_LOCAL_ONLY
  40. HIDDEN void *
  41. tdep_uc_addr (ucontext_t *uc, int reg)
  42. {
  43. char *addr = uc_addr (uc, reg);
  44. if (reg >= UNW_MIPS_R0 && reg <= UNW_MIPS_R31
  45. && tdep_big_endian (unw_local_addr_space)
  46. && unw_local_addr_space->abi == UNW_MIPS_ABI_O32)
  47. addr += 4;
  48. return addr;
  49. }
  50. # endif /* UNW_LOCAL_ONLY */
  51. HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
  52. /* XXX fix me: there is currently no way to locate the dyn-info list
  53. by a remote unwinder. On ia64, this is done via a special
  54. unwind-table entry. Perhaps something similar can be done with
  55. DWARF2 unwind info. */
  56. static void
  57. put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
  58. {
  59. /* it's a no-op */
  60. }
  61. static int
  62. get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
  63. void *arg)
  64. {
  65. *dyn_info_list_addr = (unw_word_t) (intptr_t) &_U_dyn_info_list;
  66. return 0;
  67. }
  68. static int
  69. access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
  70. void *arg)
  71. {
  72. if (write)
  73. {
  74. Debug (16, "mem[%llx] <- %llx\n", (long long) addr, (long long) *val);
  75. *(unw_word_t *) (intptr_t) addr = *val;
  76. }
  77. else
  78. {
  79. *val = *(unw_word_t *) (intptr_t) addr;
  80. Debug (16, "mem[%llx] -> %llx\n", (long long) addr, (long long) *val);
  81. }
  82. return 0;
  83. }
  84. static int
  85. access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
  86. void *arg)
  87. {
  88. unw_word_t *addr;
  89. ucontext_t *uc = arg;
  90. if (unw_is_fpreg (reg))
  91. goto badreg;
  92. Debug (16, "reg = %s\n", unw_regname (reg));
  93. if (!(addr = uc_addr (uc, reg)))
  94. goto badreg;
  95. if (write)
  96. {
  97. *(unw_word_t *) (intptr_t) addr = (mips_reg_t) *val;
  98. Debug (12, "%s <- %llx\n", unw_regname (reg), (long long) *val);
  99. }
  100. else
  101. {
  102. *val = (mips_reg_t) *(unw_word_t *) (intptr_t) addr;
  103. Debug (12, "%s -> %llx\n", unw_regname (reg), (long long) *val);
  104. }
  105. return 0;
  106. badreg:
  107. Debug (1, "bad register number %u\n", reg);
  108. return -UNW_EBADREG;
  109. }
  110. static int
  111. access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
  112. int write, void *arg)
  113. {
  114. ucontext_t *uc = arg;
  115. unw_fpreg_t *addr;
  116. if (!unw_is_fpreg (reg))
  117. goto badreg;
  118. if (!(addr = uc_addr (uc, reg)))
  119. goto badreg;
  120. if (write)
  121. {
  122. Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
  123. ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
  124. *(unw_fpreg_t *) (intptr_t) addr = *val;
  125. }
  126. else
  127. {
  128. *val = *(unw_fpreg_t *) (intptr_t) addr;
  129. Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
  130. ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
  131. }
  132. return 0;
  133. badreg:
  134. Debug (1, "bad register number %u\n", reg);
  135. /* attempt to access a non-preserved register */
  136. return -UNW_EBADREG;
  137. }
  138. static int
  139. get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
  140. char *buf, size_t buf_len, unw_word_t *offp,
  141. void *arg)
  142. {
  143. return elf_w (get_proc_name) (as, getpid (), ip, buf, buf_len, offp);
  144. }
  145. HIDDEN void
  146. mips_local_addr_space_init (void)
  147. {
  148. memset (&local_addr_space, 0, sizeof (local_addr_space));
  149. local_addr_space.big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
  150. #if _MIPS_SIM == _ABIO32
  151. local_addr_space.abi = UNW_MIPS_ABI_O32;
  152. #elif _MIPS_SIM == _ABIN32
  153. local_addr_space.abi = UNW_MIPS_ABI_N32;
  154. #elif _MIPS_SIM == _ABI64
  155. local_addr_space.abi = UNW_MIPS_ABI_N64;
  156. #else
  157. # error Unsupported ABI
  158. #endif
  159. local_addr_space.addr_size = sizeof (void *);
  160. local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
  161. local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
  162. local_addr_space.acc.put_unwind_info = put_unwind_info;
  163. local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
  164. local_addr_space.acc.access_mem = access_mem;
  165. local_addr_space.acc.access_reg = access_reg;
  166. local_addr_space.acc.access_fpreg = access_fpreg;
  167. local_addr_space.acc.resume = 0; /* mips_local_resume? FIXME! */
  168. local_addr_space.acc.get_proc_name = get_static_proc_name;
  169. unw_flush_cache (&local_addr_space, 0, 0);
  170. }
  171. #endif /* !UNW_REMOTE_ONLY */