| 1 | /* Darwin support for GDB, the GNU debugger. |
| 2 | Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2008, 2009 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | Contributed by Apple Computer, Inc. |
| 6 | |
| 7 | This file is part of GDB. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "frame.h" |
| 24 | #include "inferior.h" |
| 25 | #include "gdbcore.h" |
| 26 | #include "target.h" |
| 27 | #include "floatformat.h" |
| 28 | #include "symtab.h" |
| 29 | #include "regcache.h" |
| 30 | #include "libbfd.h" |
| 31 | #include "objfiles.h" |
| 32 | |
| 33 | #include "i387-tdep.h" |
| 34 | #include "i386-tdep.h" |
| 35 | #include "amd64-tdep.h" |
| 36 | #include "osabi.h" |
| 37 | #include "ui-out.h" |
| 38 | #include "symtab.h" |
| 39 | #include "frame.h" |
| 40 | #include "gdb_assert.h" |
| 41 | #include "i386-darwin-tdep.h" |
| 42 | #include "solib.h" |
| 43 | #include "solib-darwin.h" |
| 44 | #include "dwarf2-frame.h" |
| 45 | |
| 46 | /* Offsets into the struct i386_thread_state where we'll find the saved regs. |
| 47 | From <mach/i386/thread_status.h> and i386-tdep.h. */ |
| 48 | int i386_darwin_thread_state_reg_offset[] = |
| 49 | { |
| 50 | 0 * 4, /* EAX */ |
| 51 | 2 * 4, /* ECX */ |
| 52 | 3 * 4, /* EDX */ |
| 53 | 1 * 4, /* EBX */ |
| 54 | 7 * 4, /* ESP */ |
| 55 | 6 * 4, /* EBP */ |
| 56 | 5 * 4, /* ESI */ |
| 57 | 4 * 4, /* EDI */ |
| 58 | 10 * 4, /* EIP */ |
| 59 | 9 * 4, /* EFLAGS */ |
| 60 | 11 * 4, /* CS */ |
| 61 | 8, /* SS */ |
| 62 | 12 * 4, /* DS */ |
| 63 | 13 * 4, /* ES */ |
| 64 | 14 * 4, /* FS */ |
| 65 | 15 * 4 /* GS */ |
| 66 | }; |
| 67 | |
| 68 | const int i386_darwin_thread_state_num_regs = |
| 69 | ARRAY_SIZE (i386_darwin_thread_state_reg_offset); |
| 70 | |
| 71 | /* Offsets into the struct x86_thread_state64 where we'll find the saved regs. |
| 72 | From <mach/i386/thread_status.h> and amd64-tdep.h. */ |
| 73 | int amd64_darwin_thread_state_reg_offset[] = |
| 74 | { |
| 75 | 0 * 8, /* %rax */ |
| 76 | 1 * 8, /* %rbx */ |
| 77 | 2 * 8, /* %rcx */ |
| 78 | 3 * 8, /* %rdx */ |
| 79 | 5 * 8, /* %rsi */ |
| 80 | 4 * 8, /* %rdi */ |
| 81 | 6 * 8, /* %rbp */ |
| 82 | 7 * 8, /* %rsp */ |
| 83 | 8 * 8, /* %r8 ... */ |
| 84 | 9 * 8, |
| 85 | 10 * 8, |
| 86 | 11 * 8, |
| 87 | 12 * 8, |
| 88 | 13 * 8, |
| 89 | 14 * 8, |
| 90 | 15 * 8, /* ... %r15 */ |
| 91 | 16 * 8, /* %rip */ |
| 92 | 17 * 8, /* %rflags */ |
| 93 | 18 * 8, /* %cs */ |
| 94 | -1, /* %ss */ |
| 95 | -1, /* %ds */ |
| 96 | -1, /* %es */ |
| 97 | 19 * 8, /* %fs */ |
| 98 | 20 * 8 /* %gs */ |
| 99 | }; |
| 100 | |
| 101 | const int amd64_darwin_thread_state_num_regs = |
| 102 | ARRAY_SIZE (amd64_darwin_thread_state_reg_offset); |
| 103 | |
| 104 | /* Assuming THIS_FRAME is a Darwin sigtramp routine, return the |
| 105 | address of the associated sigcontext structure. */ |
| 106 | |
| 107 | static CORE_ADDR |
| 108 | i386_darwin_sigcontext_addr (struct frame_info *this_frame) |
| 109 | { |
| 110 | CORE_ADDR bp; |
| 111 | CORE_ADDR si; |
| 112 | gdb_byte buf[4]; |
| 113 | |
| 114 | get_frame_register (this_frame, I386_EBP_REGNUM, buf); |
| 115 | bp = extract_unsigned_integer (buf, 4); |
| 116 | |
| 117 | /* A pointer to the ucontext is passed as the fourth argument |
| 118 | to the signal handler. */ |
| 119 | read_memory (bp + 24, buf, 4); |
| 120 | si = extract_unsigned_integer (buf, 4); |
| 121 | |
| 122 | /* The pointer to mcontext is at offset 28. */ |
| 123 | read_memory (si + 28, buf, 4); |
| 124 | |
| 125 | /* First register (eax) is at offset 12. */ |
| 126 | return extract_unsigned_integer (buf, 4) + 12; |
| 127 | } |
| 128 | |
| 129 | static CORE_ADDR |
| 130 | amd64_darwin_sigcontext_addr (struct frame_info *this_frame) |
| 131 | { |
| 132 | CORE_ADDR rbx; |
| 133 | CORE_ADDR si; |
| 134 | gdb_byte buf[8]; |
| 135 | |
| 136 | /* A pointer to the ucontext is passed as the fourth argument |
| 137 | to the signal handler, which is saved in rbx. */ |
| 138 | get_frame_register (this_frame, AMD64_RBX_REGNUM, buf); |
| 139 | rbx = extract_unsigned_integer (buf, 8); |
| 140 | |
| 141 | /* The pointer to mcontext is at offset 48. */ |
| 142 | read_memory (rbx + 48, buf, 8); |
| 143 | |
| 144 | /* First register (rax) is at offset 16. */ |
| 145 | return extract_unsigned_integer (buf, 8) + 16; |
| 146 | } |
| 147 | |
| 148 | /* Return true if the PC of THIS_FRAME is in a signal trampoline which |
| 149 | may have DWARF-2 CFI. |
| 150 | |
| 151 | On Darwin, signal trampolines have DWARF-2 CFI but it has only one FDE |
| 152 | that covers only the indirect call to the user handler. |
| 153 | Without this function, the frame is recognized as a normal frame which is |
| 154 | not expected. */ |
| 155 | |
| 156 | static int |
| 157 | darwin_dwarf_signal_frame_p (struct gdbarch *gdbarch, |
| 158 | struct frame_info *this_frame) |
| 159 | { |
| 160 | return i386_sigtramp_p (this_frame); |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | i386_darwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) |
| 165 | { |
| 166 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
| 167 | |
| 168 | /* We support the SSE registers. */ |
| 169 | tdep->num_xmm_regs = I386_NUM_XREGS - 1; |
| 170 | set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS); |
| 171 | |
| 172 | dwarf2_frame_set_signal_frame_p (gdbarch, darwin_dwarf_signal_frame_p); |
| 173 | |
| 174 | tdep->struct_return = reg_struct_return; |
| 175 | |
| 176 | tdep->sigtramp_p = i386_sigtramp_p; |
| 177 | tdep->sigcontext_addr = i386_darwin_sigcontext_addr; |
| 178 | tdep->sc_reg_offset = i386_darwin_thread_state_reg_offset; |
| 179 | tdep->sc_num_regs = i386_darwin_thread_state_num_regs; |
| 180 | |
| 181 | tdep->jb_pc_offset = 20; |
| 182 | |
| 183 | set_solib_ops (gdbarch, &darwin_so_ops); |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | x86_darwin_init_abi_64 (struct gdbarch_info info, struct gdbarch *gdbarch) |
| 188 | { |
| 189 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
| 190 | |
| 191 | amd64_init_abi (info, gdbarch); |
| 192 | |
| 193 | tdep->struct_return = reg_struct_return; |
| 194 | |
| 195 | dwarf2_frame_set_signal_frame_p (gdbarch, darwin_dwarf_signal_frame_p); |
| 196 | |
| 197 | tdep->sigtramp_p = i386_sigtramp_p; |
| 198 | tdep->sigcontext_addr = amd64_darwin_sigcontext_addr; |
| 199 | tdep->sc_reg_offset = amd64_darwin_thread_state_reg_offset; |
| 200 | tdep->sc_num_regs = amd64_darwin_thread_state_num_regs; |
| 201 | |
| 202 | tdep->jb_pc_offset = 148; |
| 203 | |
| 204 | set_solib_ops (gdbarch, &darwin_so_ops); |
| 205 | } |
| 206 | |
| 207 | static enum gdb_osabi |
| 208 | i386_mach_o_osabi_sniffer (bfd *abfd) |
| 209 | { |
| 210 | if (!bfd_check_format (abfd, bfd_object)) |
| 211 | return GDB_OSABI_UNKNOWN; |
| 212 | |
| 213 | if (bfd_get_arch (abfd) == bfd_arch_i386) |
| 214 | return GDB_OSABI_DARWIN; |
| 215 | |
| 216 | return GDB_OSABI_UNKNOWN; |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | _initialize_i386_darwin_tdep (void) |
| 221 | { |
| 222 | gdbarch_register_osabi_sniffer (bfd_arch_unknown, bfd_target_mach_o_flavour, |
| 223 | i386_mach_o_osabi_sniffer); |
| 224 | |
| 225 | gdbarch_register_osabi (bfd_arch_i386, bfd_mach_i386_i386, |
| 226 | GDB_OSABI_DARWIN, i386_darwin_init_abi); |
| 227 | |
| 228 | gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, |
| 229 | GDB_OSABI_DARWIN, x86_darwin_init_abi_64); |
| 230 | } |