| 1 | /* Target-dependent code for QNX Neutrino x86. |
| 2 | |
| 3 | Copyright 2003, 2004 Free Software Foundation, Inc. |
| 4 | |
| 5 | Contributed by QNX Software Systems Ltd. |
| 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 2 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, write to the Free Software |
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | Boston, MA 02111-1307, USA. */ |
| 23 | |
| 24 | #include "defs.h" |
| 25 | #include "frame.h" |
| 26 | #include "osabi.h" |
| 27 | #include "regcache.h" |
| 28 | #include "target.h" |
| 29 | |
| 30 | #include "gdb_assert.h" |
| 31 | #include "gdb_string.h" |
| 32 | |
| 33 | #include "i386-tdep.h" |
| 34 | #include "i387-tdep.h" |
| 35 | #include "nto-tdep.h" |
| 36 | #include "solib-svr4.h" |
| 37 | |
| 38 | /* Target vector for QNX NTO x86. */ |
| 39 | static struct nto_target_ops i386_nto_target; |
| 40 | |
| 41 | #ifndef X86_CPU_FXSR |
| 42 | #define X86_CPU_FXSR (1L << 12) |
| 43 | #endif |
| 44 | |
| 45 | /* Why 13? Look in our /usr/include/x86/context.h header at the |
| 46 | x86_cpu_registers structure and you'll see an 'exx' junk register |
| 47 | that is just filler. Don't ask me, ask the kernel guys. */ |
| 48 | #define NUM_GPREGS 13 |
| 49 | |
| 50 | /* Mapping between the general-purpose registers in `struct xxx' |
| 51 | format and GDB's register cache layout. */ |
| 52 | |
| 53 | /* From <x86/context.h>. */ |
| 54 | static int i386nto_gregset_reg_offset[] = |
| 55 | { |
| 56 | 7 * 4, /* %eax */ |
| 57 | 6 * 4, /* %ecx */ |
| 58 | 5 * 4, /* %edx */ |
| 59 | 4 * 4, /* %ebx */ |
| 60 | 11 * 4, /* %esp */ |
| 61 | 2 * 4, /* %epb */ |
| 62 | 1 * 4, /* %esi */ |
| 63 | 0 * 4, /* %edi */ |
| 64 | 8 * 4, /* %eip */ |
| 65 | 10 * 4, /* %eflags */ |
| 66 | 9 * 4, /* %cs */ |
| 67 | 12 * 4, /* %ss */ |
| 68 | -1 /* filler */ |
| 69 | }; |
| 70 | |
| 71 | /* Given a GDB register number REGNUM, return the offset into |
| 72 | Neutrino's register structure or -1 if the register is unknown. */ |
| 73 | |
| 74 | static int |
| 75 | nto_reg_offset (int regnum) |
| 76 | { |
| 77 | if (regnum >= 0 && regnum < ARRAY_SIZE (i386nto_gregset_reg_offset)) |
| 78 | return i386nto_gregset_reg_offset[regnum]; |
| 79 | |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | i386nto_supply_gregset (char *gpregs) |
| 85 | { |
| 86 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); |
| 87 | |
| 88 | if(tdep->gregset == NULL) |
| 89 | tdep->gregset = regset_alloc (current_gdbarch, i386_supply_gregset, |
| 90 | i386_collect_gregset); |
| 91 | |
| 92 | gdb_assert (tdep->gregset_reg_offset == i386nto_gregset_reg_offset); |
| 93 | tdep->gregset->supply_regset (tdep->gregset, current_regcache, -1, |
| 94 | gpregs, NUM_GPREGS * 4); |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | i386nto_supply_fpregset (char *fpregs) |
| 99 | { |
| 100 | if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR) |
| 101 | i387_supply_fxsave (current_regcache, -1, fpregs); |
| 102 | else |
| 103 | i387_supply_fsave (current_regcache, -1, fpregs); |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | i386nto_supply_regset (int regset, char *data) |
| 108 | { |
| 109 | switch (regset) |
| 110 | { |
| 111 | case NTO_REG_GENERAL: |
| 112 | i386nto_supply_gregset (data); |
| 113 | break; |
| 114 | case NTO_REG_FLOAT: |
| 115 | i386nto_supply_fpregset (data); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | i386nto_regset_id (int regno) |
| 122 | { |
| 123 | if (regno == -1) |
| 124 | return NTO_REG_END; |
| 125 | else if (regno < I386_NUM_GREGS) |
| 126 | return NTO_REG_GENERAL; |
| 127 | else if (regno < I386_NUM_GREGS + I386_NUM_FREGS) |
| 128 | return NTO_REG_FLOAT; |
| 129 | |
| 130 | return -1; /* Error. */ |
| 131 | } |
| 132 | |
| 133 | static int |
| 134 | i386nto_register_area (int regno, int regset, unsigned *off) |
| 135 | { |
| 136 | int len; |
| 137 | |
| 138 | *off = 0; |
| 139 | if (regset == NTO_REG_GENERAL) |
| 140 | { |
| 141 | if (regno == -1) |
| 142 | return NUM_GPREGS * 4; |
| 143 | |
| 144 | *off = nto_reg_offset (regno); |
| 145 | if (*off == -1) |
| 146 | return 0; |
| 147 | return 4; |
| 148 | } |
| 149 | else if (regset == NTO_REG_FLOAT) |
| 150 | { |
| 151 | unsigned off_adjust, regsize, regset_size; |
| 152 | |
| 153 | if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR) |
| 154 | { |
| 155 | off_adjust = 32; |
| 156 | regsize = 16; |
| 157 | regset_size = 512; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | off_adjust = 28; |
| 162 | regsize = 10; |
| 163 | regset_size = 128; |
| 164 | } |
| 165 | |
| 166 | if (regno == -1) |
| 167 | return regset_size; |
| 168 | |
| 169 | *off = (regno - FP0_REGNUM) * regsize + off_adjust; |
| 170 | return 10; |
| 171 | /* Why 10 instead of regsize? GDB only stores 10 bytes per FP |
| 172 | register so if we're sending a register back to the target, |
| 173 | we only want pdebug to write 10 bytes so as not to clobber |
| 174 | the reserved 6 bytes in the fxsave structure. */ |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | static int |
| 180 | i386nto_regset_fill (int regset, char *data) |
| 181 | { |
| 182 | if (regset == NTO_REG_GENERAL) |
| 183 | { |
| 184 | int regno; |
| 185 | |
| 186 | for (regno = 0; regno < NUM_GPREGS; regno++) |
| 187 | { |
| 188 | int offset = nto_reg_offset (regno); |
| 189 | if (offset != -1) |
| 190 | regcache_raw_collect (current_regcache, regno, data + offset); |
| 191 | } |
| 192 | } |
| 193 | else if (regset == NTO_REG_FLOAT) |
| 194 | { |
| 195 | if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR) |
| 196 | i387_fill_fxsave (data, -1); |
| 197 | else |
| 198 | i387_fill_fsave (data, -1); |
| 199 | } |
| 200 | else |
| 201 | return -1; |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* Return whether the frame preceding NEXT_FRAME corresponds to a QNX |
| 207 | Neutrino sigtramp routine. */ |
| 208 | |
| 209 | static int |
| 210 | i386nto_sigtramp_p (struct frame_info *next_frame) |
| 211 | { |
| 212 | CORE_ADDR pc = frame_pc_unwind (next_frame); |
| 213 | char *name; |
| 214 | |
| 215 | find_pc_partial_function (pc, &name, NULL, NULL); |
| 216 | return name && strcmp ("__signalstub", name) == 0; |
| 217 | } |
| 218 | |
| 219 | #define I386_NTO_SIGCONTEXT_OFFSET 136 |
| 220 | |
| 221 | /* Assuming NEXT_FRAME is a frame following a QNX Neutrino sigtramp |
| 222 | routine, return the address of the associated sigcontext structure. */ |
| 223 | |
| 224 | static CORE_ADDR |
| 225 | i386nto_sigcontext_addr (struct frame_info *next_frame) |
| 226 | { |
| 227 | char buf[4]; |
| 228 | CORE_ADDR sp; |
| 229 | |
| 230 | frame_unwind_register (next_frame, I386_ESP_REGNUM, buf); |
| 231 | sp = extract_unsigned_integer (buf, 4); |
| 232 | |
| 233 | return sp + I386_NTO_SIGCONTEXT_OFFSET; |
| 234 | } |
| 235 | |
| 236 | static void |
| 237 | init_i386nto_ops (void) |
| 238 | { |
| 239 | i386_nto_target.regset_id = i386nto_regset_id; |
| 240 | i386_nto_target.supply_gregset = i386nto_supply_gregset; |
| 241 | i386_nto_target.supply_fpregset = i386nto_supply_fpregset; |
| 242 | i386_nto_target.supply_altregset = nto_dummy_supply_regset; |
| 243 | i386_nto_target.supply_regset = i386nto_supply_regset; |
| 244 | i386_nto_target.register_area = i386nto_register_area; |
| 245 | i386_nto_target.regset_fill = i386nto_regset_fill; |
| 246 | i386_nto_target.fetch_link_map_offsets = |
| 247 | svr4_ilp32_fetch_link_map_offsets; |
| 248 | } |
| 249 | |
| 250 | static void |
| 251 | i386nto_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) |
| 252 | { |
| 253 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
| 254 | |
| 255 | /* Deal with our strange signals. */ |
| 256 | nto_initialize_signals (); |
| 257 | |
| 258 | /* NTO uses ELF. */ |
| 259 | i386_elf_init_abi (info, gdbarch); |
| 260 | |
| 261 | /* Neutrino rewinds to look more normal. Need to override the i386 |
| 262 | default which is [unfortunately] to decrement the PC. */ |
| 263 | set_gdbarch_decr_pc_after_break (gdbarch, 0); |
| 264 | |
| 265 | /* NTO has shared libraries. */ |
| 266 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); |
| 267 | |
| 268 | tdep->gregset_reg_offset = i386nto_gregset_reg_offset; |
| 269 | tdep->gregset_num_regs = ARRAY_SIZE (i386nto_gregset_reg_offset); |
| 270 | tdep->sizeof_gregset = NUM_GPREGS * 4; |
| 271 | |
| 272 | tdep->sigtramp_p = i386nto_sigtramp_p; |
| 273 | tdep->sigcontext_addr = i386nto_sigcontext_addr; |
| 274 | tdep->sc_pc_offset = 56; |
| 275 | tdep->sc_sp_offset = 68; |
| 276 | |
| 277 | /* Setjmp()'s return PC saved in EDX (5). */ |
| 278 | tdep->jb_pc_offset = 20; /* 5x32 bit ints in. */ |
| 279 | |
| 280 | set_solib_svr4_fetch_link_map_offsets |
| 281 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); |
| 282 | |
| 283 | /* Our loader handles solib relocations slightly differently than svr4. */ |
| 284 | TARGET_SO_RELOCATE_SECTION_ADDRESSES = nto_relocate_section_addresses; |
| 285 | |
| 286 | /* Supply a nice function to find our solibs. */ |
| 287 | TARGET_SO_FIND_AND_OPEN_SOLIB = nto_find_and_open_solib; |
| 288 | |
| 289 | /* Our linker code is in libc. */ |
| 290 | TARGET_SO_IN_DYNSYM_RESOLVE_CODE = nto_in_dynsym_resolve_code; |
| 291 | |
| 292 | nto_set_target (&i386_nto_target); |
| 293 | } |
| 294 | |
| 295 | void |
| 296 | _initialize_i386nto_tdep (void) |
| 297 | { |
| 298 | init_i386nto_ops (); |
| 299 | gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_QNXNTO, |
| 300 | i386nto_init_abi); |
| 301 | gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour, |
| 302 | nto_elf_osabi_sniffer); |
| 303 | } |