i386-darwin-tdep.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Darwin support for GDB, the GNU debugger.
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Contributed by Apple Computer, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "frame.h"
  17. #include "inferior.h"
  18. #include "gdbcore.h"
  19. #include "target.h"
  20. #include "symtab.h"
  21. #include "regcache.h"
  22. #include "objfiles.h"
  23. #include "i387-tdep.h"
  24. #include "i386-tdep.h"
  25. #include "osabi.h"
  26. #include "ui-out.h"
  27. #include "i386-darwin-tdep.h"
  28. #include "solib.h"
  29. #include "solib-darwin.h"
  30. #include "dwarf2/frame.h"
  31. #include <algorithm>
  32. /* Offsets into the struct i386_thread_state where we'll find the saved regs.
  33. From <mach/i386/thread_status.h> and i386-tdep.h. */
  34. int i386_darwin_thread_state_reg_offset[] =
  35. {
  36. 0 * 4, /* EAX */
  37. 2 * 4, /* ECX */
  38. 3 * 4, /* EDX */
  39. 1 * 4, /* EBX */
  40. 7 * 4, /* ESP */
  41. 6 * 4, /* EBP */
  42. 5 * 4, /* ESI */
  43. 4 * 4, /* EDI */
  44. 10 * 4, /* EIP */
  45. 9 * 4, /* EFLAGS */
  46. 11 * 4, /* CS */
  47. 8 * 4, /* SS */
  48. 12 * 4, /* DS */
  49. 13 * 4, /* ES */
  50. 14 * 4, /* FS */
  51. 15 * 4 /* GS */
  52. };
  53. const int i386_darwin_thread_state_num_regs =
  54. ARRAY_SIZE (i386_darwin_thread_state_reg_offset);
  55. /* Assuming THIS_FRAME is a Darwin sigtramp routine, return the
  56. address of the associated sigcontext structure. */
  57. static CORE_ADDR
  58. i386_darwin_sigcontext_addr (struct frame_info *this_frame)
  59. {
  60. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  61. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  62. CORE_ADDR bp;
  63. CORE_ADDR si;
  64. gdb_byte buf[4];
  65. get_frame_register (this_frame, I386_EBP_REGNUM, buf);
  66. bp = extract_unsigned_integer (buf, 4, byte_order);
  67. /* A pointer to the ucontext is passed as the fourth argument
  68. to the signal handler. */
  69. read_memory (bp + 24, buf, 4);
  70. si = extract_unsigned_integer (buf, 4, byte_order);
  71. /* The pointer to mcontext is at offset 28. */
  72. read_memory (si + 28, buf, 4);
  73. /* First register (eax) is at offset 12. */
  74. return extract_unsigned_integer (buf, 4, byte_order) + 12;
  75. }
  76. /* Return true if the PC of THIS_FRAME is in a signal trampoline which
  77. may have DWARF-2 CFI.
  78. On Darwin, signal trampolines have DWARF-2 CFI but it has only one FDE
  79. that covers only the indirect call to the user handler.
  80. Without this function, the frame is recognized as a normal frame which is
  81. not expected. */
  82. int
  83. darwin_dwarf_signal_frame_p (struct gdbarch *gdbarch,
  84. struct frame_info *this_frame)
  85. {
  86. return i386_sigtramp_p (this_frame);
  87. }
  88. /* Check whether TYPE is a 128-bit vector (__m128, __m128d or __m128i). */
  89. static int
  90. i386_m128_p (struct type *type)
  91. {
  92. return (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
  93. && TYPE_LENGTH (type) == 16);
  94. }
  95. /* Return the alignment for TYPE when passed as an argument. */
  96. static int
  97. i386_darwin_arg_type_alignment (struct type *type)
  98. {
  99. type = check_typedef (type);
  100. /* According to Mac OS X ABI document (passing arguments):
  101. 6. The caller places 64-bit vectors (__m64) on the parameter area,
  102. aligned to 8-byte boundaries.
  103. 7. [...] The caller aligns 128-bit vectors in the parameter area to
  104. 16-byte boundaries. */
  105. if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  106. return TYPE_LENGTH (type);
  107. /* 4. The caller places all the fields of structures (or unions) with no
  108. vector elements in the parameter area. These structures are 4-byte
  109. aligned.
  110. 5. The caller places structures with vector elements on the stack,
  111. 16-byte aligned. */
  112. if (type->code () == TYPE_CODE_STRUCT
  113. || type->code () == TYPE_CODE_UNION)
  114. {
  115. int i;
  116. int res = 4;
  117. for (i = 0; i < type->num_fields (); i++)
  118. {
  119. int align
  120. = i386_darwin_arg_type_alignment (type->field (i).type ());
  121. res = std::max (res, align);
  122. }
  123. return res;
  124. }
  125. /* 2. The caller aligns nonvector arguments to 4-byte boundaries. */
  126. return 4;
  127. }
  128. static CORE_ADDR
  129. i386_darwin_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  130. struct regcache *regcache, CORE_ADDR bp_addr,
  131. int nargs, struct value **args, CORE_ADDR sp,
  132. function_call_return_method return_method,
  133. CORE_ADDR struct_addr)
  134. {
  135. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  136. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  137. gdb_byte buf[4];
  138. int i;
  139. int write_pass;
  140. /* Determine the total space required for arguments and struct
  141. return address in a first pass, then push arguments in a second pass. */
  142. for (write_pass = 0; write_pass < 2; write_pass++)
  143. {
  144. int args_space = 0;
  145. int num_m128 = 0;
  146. if (return_method == return_method_struct)
  147. {
  148. if (write_pass)
  149. {
  150. /* Push value address. */
  151. store_unsigned_integer (buf, 4, byte_order, struct_addr);
  152. write_memory (sp, buf, 4);
  153. }
  154. args_space += 4;
  155. }
  156. for (i = 0; i < nargs; i++)
  157. {
  158. struct type *arg_type = value_enclosing_type (args[i]);
  159. if (i386_m128_p (arg_type) && num_m128 < 4)
  160. {
  161. if (write_pass)
  162. {
  163. const gdb_byte *val = value_contents_all (args[i]).data ();
  164. regcache->raw_write (I387_MM0_REGNUM(tdep) + num_m128, val);
  165. }
  166. num_m128++;
  167. }
  168. else
  169. {
  170. args_space = align_up (args_space,
  171. i386_darwin_arg_type_alignment (arg_type));
  172. if (write_pass)
  173. write_memory (sp + args_space,
  174. value_contents_all (args[i]).data (),
  175. TYPE_LENGTH (arg_type));
  176. /* The System V ABI says that:
  177. "An argument's size is increased, if necessary, to make it a
  178. multiple of [32-bit] words. This may require tail padding,
  179. depending on the size of the argument."
  180. This makes sure the stack stays word-aligned. */
  181. args_space += align_up (TYPE_LENGTH (arg_type), 4);
  182. }
  183. }
  184. /* Darwin i386 ABI:
  185. 1. The caller ensures that the stack is 16-byte aligned at the point
  186. of the function call. */
  187. if (!write_pass)
  188. sp = align_down (sp - args_space, 16);
  189. }
  190. /* Store return address. */
  191. sp -= 4;
  192. store_unsigned_integer (buf, 4, byte_order, bp_addr);
  193. write_memory (sp, buf, 4);
  194. /* Finally, update the stack pointer... */
  195. store_unsigned_integer (buf, 4, byte_order, sp);
  196. regcache->cooked_write (I386_ESP_REGNUM, buf);
  197. /* ...and fake a frame pointer. */
  198. regcache->cooked_write (I386_EBP_REGNUM, buf);
  199. /* MarkK wrote: This "+ 8" is all over the place:
  200. (i386_frame_this_id, i386_sigtramp_frame_this_id,
  201. i386_dummy_id). It's there, since all frame unwinders for
  202. a given target have to agree (within a certain margin) on the
  203. definition of the stack address of a frame. Otherwise frame id
  204. comparison might not work correctly. Since DWARF2/GCC uses the
  205. stack address *before* the function call as a frame's CFA. On
  206. the i386, when %ebp is used as a frame pointer, the offset
  207. between the contents %ebp and the CFA as defined by GCC. */
  208. return sp + 8;
  209. }
  210. static void
  211. i386_darwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  212. {
  213. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  214. /* We support the SSE registers. */
  215. tdep->num_xmm_regs = I386_NUM_XREGS - 1;
  216. set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS);
  217. dwarf2_frame_set_signal_frame_p (gdbarch, darwin_dwarf_signal_frame_p);
  218. set_gdbarch_push_dummy_call (gdbarch, i386_darwin_push_dummy_call);
  219. tdep->struct_return = reg_struct_return;
  220. tdep->sigtramp_p = i386_sigtramp_p;
  221. tdep->sigcontext_addr = i386_darwin_sigcontext_addr;
  222. tdep->sc_reg_offset = i386_darwin_thread_state_reg_offset;
  223. tdep->sc_num_regs = i386_darwin_thread_state_num_regs;
  224. tdep->jb_pc_offset = 48;
  225. /* Although the i387 extended floating-point has only 80 significant
  226. bits, a `long double' actually takes up 128, probably to enforce
  227. alignment. */
  228. set_gdbarch_long_double_bit (gdbarch, 128);
  229. set_solib_ops (gdbarch, &darwin_so_ops);
  230. }
  231. static enum gdb_osabi
  232. i386_mach_o_osabi_sniffer (bfd *abfd)
  233. {
  234. if (!bfd_check_format (abfd, bfd_object))
  235. return GDB_OSABI_UNKNOWN;
  236. if (bfd_get_arch (abfd) == bfd_arch_i386)
  237. return GDB_OSABI_DARWIN;
  238. return GDB_OSABI_UNKNOWN;
  239. }
  240. void _initialize_i386_darwin_tdep ();
  241. void
  242. _initialize_i386_darwin_tdep ()
  243. {
  244. gdbarch_register_osabi_sniffer (bfd_arch_unknown, bfd_target_mach_o_flavour,
  245. i386_mach_o_osabi_sniffer);
  246. gdbarch_register_osabi (bfd_arch_i386, bfd_mach_i386_i386,
  247. GDB_OSABI_DARWIN, i386_darwin_init_abi);
  248. }