i386-nto-tdep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Target-dependent code for QNX Neutrino x86.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Contributed by QNX Software Systems Ltd.
  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 "osabi.h"
  18. #include "regcache.h"
  19. #include "target.h"
  20. #include "i386-tdep.h"
  21. #include "i387-tdep.h"
  22. #include "nto-tdep.h"
  23. #include "solib.h"
  24. #include "solib-svr4.h"
  25. #ifndef X86_CPU_FXSR
  26. #define X86_CPU_FXSR (1L << 12)
  27. #endif
  28. /* Why 13? Look in our /usr/include/x86/context.h header at the
  29. x86_cpu_registers structure and you'll see an 'exx' junk register
  30. that is just filler. Don't ask me, ask the kernel guys. */
  31. #define NUM_GPREGS 13
  32. /* Mapping between the general-purpose registers in `struct xxx'
  33. format and GDB's register cache layout. */
  34. /* From <x86/context.h>. */
  35. static int i386nto_gregset_reg_offset[] =
  36. {
  37. 7 * 4, /* %eax */
  38. 6 * 4, /* %ecx */
  39. 5 * 4, /* %edx */
  40. 4 * 4, /* %ebx */
  41. 11 * 4, /* %esp */
  42. 2 * 4, /* %epb */
  43. 1 * 4, /* %esi */
  44. 0 * 4, /* %edi */
  45. 8 * 4, /* %eip */
  46. 10 * 4, /* %eflags */
  47. 9 * 4, /* %cs */
  48. 12 * 4, /* %ss */
  49. -1 /* filler */
  50. };
  51. /* Given a GDB register number REGNUM, return the offset into
  52. Neutrino's register structure or -1 if the register is unknown. */
  53. static int
  54. nto_reg_offset (int regnum)
  55. {
  56. if (regnum >= 0 && regnum < ARRAY_SIZE (i386nto_gregset_reg_offset))
  57. return i386nto_gregset_reg_offset[regnum];
  58. return -1;
  59. }
  60. static void
  61. i386nto_supply_gregset (struct regcache *regcache, char *gpregs)
  62. {
  63. struct gdbarch *gdbarch = regcache->arch ();
  64. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  65. gdb_assert (tdep->gregset_reg_offset == i386nto_gregset_reg_offset);
  66. i386_gregset.supply_regset (&i386_gregset, regcache, -1,
  67. gpregs, NUM_GPREGS * 4);
  68. }
  69. static void
  70. i386nto_supply_fpregset (struct regcache *regcache, char *fpregs)
  71. {
  72. if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR)
  73. i387_supply_fxsave (regcache, -1, fpregs);
  74. else
  75. i387_supply_fsave (regcache, -1, fpregs);
  76. }
  77. static void
  78. i386nto_supply_regset (struct regcache *regcache, int regset, char *data)
  79. {
  80. switch (regset)
  81. {
  82. case NTO_REG_GENERAL:
  83. i386nto_supply_gregset (regcache, data);
  84. break;
  85. case NTO_REG_FLOAT:
  86. i386nto_supply_fpregset (regcache, data);
  87. break;
  88. }
  89. }
  90. static int
  91. i386nto_regset_id (int regno)
  92. {
  93. if (regno == -1)
  94. return NTO_REG_END;
  95. else if (regno < I386_NUM_GREGS)
  96. return NTO_REG_GENERAL;
  97. else if (regno < I386_NUM_GREGS + I387_NUM_REGS)
  98. return NTO_REG_FLOAT;
  99. else if (regno < I386_SSE_NUM_REGS)
  100. return NTO_REG_FLOAT; /* We store xmm registers in fxsave_area. */
  101. return -1; /* Error. */
  102. }
  103. static int
  104. i386nto_register_area (struct gdbarch *gdbarch,
  105. int regno, int regset, unsigned *off)
  106. {
  107. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  108. *off = 0;
  109. if (regset == NTO_REG_GENERAL)
  110. {
  111. if (regno == -1)
  112. return NUM_GPREGS * 4;
  113. *off = nto_reg_offset (regno);
  114. if (*off == -1)
  115. return 0;
  116. return 4;
  117. }
  118. else if (regset == NTO_REG_FLOAT)
  119. {
  120. unsigned off_adjust, regsize, regset_size, regno_base;
  121. /* The following are flags indicating number in our fxsave_area. */
  122. int first_four = (regno >= I387_FCTRL_REGNUM (tdep)
  123. && regno <= I387_FISEG_REGNUM (tdep));
  124. int second_four = (regno > I387_FISEG_REGNUM (tdep)
  125. && regno <= I387_FOP_REGNUM (tdep));
  126. int st_reg = (regno >= I387_ST0_REGNUM (tdep)
  127. && regno < I387_ST0_REGNUM (tdep) + 8);
  128. int xmm_reg = (regno >= I387_XMM0_REGNUM (tdep)
  129. && regno < I387_MXCSR_REGNUM (tdep));
  130. if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR)
  131. {
  132. off_adjust = 32;
  133. regsize = 16;
  134. regset_size = 512;
  135. /* fxsave_area structure. */
  136. if (first_four)
  137. {
  138. /* fpu_control_word, fpu_status_word, fpu_tag_word, fpu_operand
  139. registers. */
  140. regsize = 2; /* Two bytes each. */
  141. off_adjust = 0;
  142. regno_base = I387_FCTRL_REGNUM (tdep);
  143. }
  144. else if (second_four)
  145. {
  146. /* fpu_ip, fpu_cs, fpu_op, fpu_ds registers. */
  147. regsize = 4;
  148. off_adjust = 8;
  149. regno_base = I387_FISEG_REGNUM (tdep) + 1;
  150. }
  151. else if (st_reg)
  152. {
  153. /* ST registers. */
  154. regsize = 16;
  155. off_adjust = 32;
  156. regno_base = I387_ST0_REGNUM (tdep);
  157. }
  158. else if (xmm_reg)
  159. {
  160. /* XMM registers. */
  161. regsize = 16;
  162. off_adjust = 160;
  163. regno_base = I387_XMM0_REGNUM (tdep);
  164. }
  165. else if (regno == I387_MXCSR_REGNUM (tdep))
  166. {
  167. regsize = 4;
  168. off_adjust = 24;
  169. regno_base = I387_MXCSR_REGNUM (tdep);
  170. }
  171. else
  172. {
  173. /* Whole regset. */
  174. gdb_assert (regno == -1);
  175. off_adjust = 0;
  176. regno_base = 0;
  177. regsize = regset_size;
  178. }
  179. }
  180. else
  181. {
  182. regset_size = 108;
  183. /* fsave_area structure. */
  184. if (first_four || second_four)
  185. {
  186. /* fpu_control_word, ... , fpu_ds registers. */
  187. regsize = 4;
  188. off_adjust = 0;
  189. regno_base = I387_FCTRL_REGNUM (tdep);
  190. }
  191. else if (st_reg)
  192. {
  193. /* One of ST registers. */
  194. regsize = 10;
  195. off_adjust = 7 * 4;
  196. regno_base = I387_ST0_REGNUM (tdep);
  197. }
  198. else
  199. {
  200. /* Whole regset. */
  201. gdb_assert (regno == -1);
  202. off_adjust = 0;
  203. regno_base = 0;
  204. regsize = regset_size;
  205. }
  206. }
  207. if (regno != -1)
  208. *off = off_adjust + (regno - regno_base) * regsize;
  209. else
  210. *off = 0;
  211. return regsize;
  212. }
  213. return -1;
  214. }
  215. static int
  216. i386nto_regset_fill (const struct regcache *regcache, int regset, char *data)
  217. {
  218. if (regset == NTO_REG_GENERAL)
  219. {
  220. int regno;
  221. for (regno = 0; regno < NUM_GPREGS; regno++)
  222. {
  223. int offset = nto_reg_offset (regno);
  224. if (offset != -1)
  225. regcache->raw_collect (regno, data + offset);
  226. }
  227. }
  228. else if (regset == NTO_REG_FLOAT)
  229. {
  230. if (nto_cpuinfo_valid && nto_cpuinfo_flags | X86_CPU_FXSR)
  231. i387_collect_fxsave (regcache, -1, data);
  232. else
  233. i387_collect_fsave (regcache, -1, data);
  234. }
  235. else
  236. return -1;
  237. return 0;
  238. }
  239. /* Return whether THIS_FRAME corresponds to a QNX Neutrino sigtramp
  240. routine. */
  241. static int
  242. i386nto_sigtramp_p (struct frame_info *this_frame)
  243. {
  244. CORE_ADDR pc = get_frame_pc (this_frame);
  245. const char *name;
  246. find_pc_partial_function (pc, &name, NULL, NULL);
  247. return name && strcmp ("__signalstub", name) == 0;
  248. }
  249. /* Assuming THIS_FRAME is a QNX Neutrino sigtramp routine, return the
  250. address of the associated sigcontext structure. */
  251. static CORE_ADDR
  252. i386nto_sigcontext_addr (struct frame_info *this_frame)
  253. {
  254. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  255. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  256. gdb_byte buf[4];
  257. CORE_ADDR ptrctx;
  258. /* We store __ucontext_t addr in EDI register. */
  259. get_frame_register (this_frame, I386_EDI_REGNUM, buf);
  260. ptrctx = extract_unsigned_integer (buf, 4, byte_order);
  261. ptrctx += 24 /* Context pointer is at this offset. */;
  262. return ptrctx;
  263. }
  264. static void
  265. init_i386nto_ops (void)
  266. {
  267. nto_regset_id = i386nto_regset_id;
  268. nto_supply_gregset = i386nto_supply_gregset;
  269. nto_supply_fpregset = i386nto_supply_fpregset;
  270. nto_supply_altregset = nto_dummy_supply_regset;
  271. nto_supply_regset = i386nto_supply_regset;
  272. nto_register_area = i386nto_register_area;
  273. nto_regset_fill = i386nto_regset_fill;
  274. nto_fetch_link_map_offsets =
  275. svr4_ilp32_fetch_link_map_offsets;
  276. }
  277. static void
  278. i386nto_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  279. {
  280. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  281. static struct target_so_ops nto_svr4_so_ops;
  282. /* Deal with our strange signals. */
  283. nto_initialize_signals ();
  284. /* NTO uses ELF. */
  285. i386_elf_init_abi (info, gdbarch);
  286. /* Neutrino rewinds to look more normal. Need to override the i386
  287. default which is [unfortunately] to decrement the PC. */
  288. set_gdbarch_decr_pc_after_break (gdbarch, 0);
  289. tdep->gregset_reg_offset = i386nto_gregset_reg_offset;
  290. tdep->gregset_num_regs = ARRAY_SIZE (i386nto_gregset_reg_offset);
  291. tdep->sizeof_gregset = NUM_GPREGS * 4;
  292. tdep->sigtramp_p = i386nto_sigtramp_p;
  293. tdep->sigcontext_addr = i386nto_sigcontext_addr;
  294. tdep->sc_reg_offset = i386nto_gregset_reg_offset;
  295. tdep->sc_num_regs = ARRAY_SIZE (i386nto_gregset_reg_offset);
  296. /* Setjmp()'s return PC saved in EDX (5). */
  297. tdep->jb_pc_offset = 20; /* 5x32 bit ints in. */
  298. set_solib_svr4_fetch_link_map_offsets
  299. (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  300. /* Initialize this lazily, to avoid an initialization order
  301. dependency on solib-svr4.c's _initialize routine. */
  302. if (nto_svr4_so_ops.in_dynsym_resolve_code == NULL)
  303. {
  304. nto_svr4_so_ops = svr4_so_ops;
  305. /* Our loader handles solib relocations differently than svr4. */
  306. nto_svr4_so_ops.relocate_section_addresses
  307. = nto_relocate_section_addresses;
  308. /* Supply a nice function to find our solibs. */
  309. nto_svr4_so_ops.find_and_open_solib
  310. = nto_find_and_open_solib;
  311. /* Our linker code is in libc. */
  312. nto_svr4_so_ops.in_dynsym_resolve_code
  313. = nto_in_dynsym_resolve_code;
  314. }
  315. set_solib_ops (gdbarch, &nto_svr4_so_ops);
  316. set_gdbarch_wchar_bit (gdbarch, 32);
  317. set_gdbarch_wchar_signed (gdbarch, 0);
  318. }
  319. void _initialize_i386nto_tdep ();
  320. void
  321. _initialize_i386nto_tdep ()
  322. {
  323. init_i386nto_ops ();
  324. gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_QNXNTO,
  325. i386nto_init_abi);
  326. gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
  327. nto_elf_osabi_sniffer);
  328. }