ppc-fbsd-tdep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Target-dependent code for PowerPC systems running FreeBSD.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "frame.h"
  17. #include "gdbcore.h"
  18. #include "frame-unwind.h"
  19. #include "gdbtypes.h"
  20. #include "osabi.h"
  21. #include "regcache.h"
  22. #include "regset.h"
  23. #include "symtab.h"
  24. #include "target.h"
  25. #include "trad-frame.h"
  26. #include "ppc-tdep.h"
  27. #include "ppc64-tdep.h"
  28. #include "ppc-fbsd-tdep.h"
  29. #include "fbsd-tdep.h"
  30. #include "solib-svr4.h"
  31. #include "inferior.h"
  32. /* 32-bit regset descriptions. */
  33. static const struct ppc_reg_offsets ppc32_fbsd_reg_offsets =
  34. {
  35. /* General-purpose registers. */
  36. /* .r0_offset = */ 0,
  37. /* .gpr_size = */ 4,
  38. /* .xr_size = */ 4,
  39. /* .pc_offset = */ 144,
  40. /* .ps_offset = */ -1,
  41. /* .cr_offset = */ 132,
  42. /* .lr_offset = */ 128,
  43. /* .ctr_offset = */ 140,
  44. /* .xer_offset = */ 136,
  45. /* .mq_offset = */ -1,
  46. /* Floating-point registers. */
  47. /* .f0_offset = */ 0,
  48. /* .fpscr_offset = */ 256,
  49. /* .fpscr_size = */ 8
  50. };
  51. /* 64-bit regset descriptions. */
  52. static const struct ppc_reg_offsets ppc64_fbsd_reg_offsets =
  53. {
  54. /* General-purpose registers. */
  55. /* .r0_offset = */ 0,
  56. /* .gpr_size = */ 8,
  57. /* .xr_size = */ 8,
  58. /* .pc_offset = */ 288,
  59. /* .ps_offset = */ -1,
  60. /* .cr_offset = */ 264,
  61. /* .lr_offset = */ 256,
  62. /* .ctr_offset = */ 280,
  63. /* .xer_offset = */ 272,
  64. /* .mq_offset = */ -1,
  65. /* Floating-point registers. */
  66. /* .f0_offset = */ 0,
  67. /* .fpscr_offset = */ 256,
  68. /* .fpscr_size = */ 8
  69. };
  70. /* 32-bit general-purpose register set. */
  71. static const struct regset ppc32_fbsd_gregset = {
  72. &ppc32_fbsd_reg_offsets,
  73. ppc_supply_gregset,
  74. ppc_collect_gregset
  75. };
  76. /* 64-bit general-purpose register set. */
  77. static const struct regset ppc64_fbsd_gregset = {
  78. &ppc64_fbsd_reg_offsets,
  79. ppc_supply_gregset,
  80. ppc_collect_gregset
  81. };
  82. /* 32-/64-bit floating-point register set. */
  83. static const struct regset ppc32_fbsd_fpregset = {
  84. &ppc32_fbsd_reg_offsets,
  85. ppc_supply_fpregset,
  86. ppc_collect_fpregset
  87. };
  88. const struct regset *
  89. ppc_fbsd_gregset (int wordsize)
  90. {
  91. return wordsize == 8 ? &ppc64_fbsd_gregset : &ppc32_fbsd_gregset;
  92. }
  93. const struct regset *
  94. ppc_fbsd_fpregset (void)
  95. {
  96. return &ppc32_fbsd_fpregset;
  97. }
  98. /* Iterate over core file register note sections. */
  99. static void
  100. ppcfbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
  101. iterate_over_regset_sections_cb *cb,
  102. void *cb_data,
  103. const struct regcache *regcache)
  104. {
  105. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  106. if (tdep->wordsize == 4)
  107. cb (".reg", 148, 148, &ppc32_fbsd_gregset, NULL, cb_data);
  108. else
  109. cb (".reg", 296, 296, &ppc64_fbsd_gregset, NULL, cb_data);
  110. cb (".reg2", 264, 264, &ppc32_fbsd_fpregset, NULL, cb_data);
  111. }
  112. /* Default page size. */
  113. static const int ppcfbsd_page_size = 4096;
  114. /* Offset for sigreturn(2). */
  115. static const int ppcfbsd_sigreturn_offset[] = {
  116. 0xc, /* FreeBSD 32-bit */
  117. -1
  118. };
  119. /* Signal trampolines. */
  120. static int
  121. ppcfbsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  122. struct frame_info *this_frame,
  123. void **this_cache)
  124. {
  125. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  126. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  127. CORE_ADDR pc = get_frame_pc (this_frame);
  128. CORE_ADDR start_pc = (pc & ~(ppcfbsd_page_size - 1));
  129. const int *offset;
  130. const char *name;
  131. /* A stack trampoline is detected if no name is associated
  132. to the current pc and if it points inside a trampoline
  133. sequence. */
  134. find_pc_partial_function (pc, &name, NULL, NULL);
  135. /* If we have a name, we have no trampoline, return. */
  136. if (name)
  137. return 0;
  138. for (offset = ppcfbsd_sigreturn_offset; *offset != -1; offset++)
  139. {
  140. gdb_byte buf[2 * PPC_INSN_SIZE];
  141. unsigned long insn;
  142. if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
  143. {buf, sizeof buf}))
  144. continue;
  145. /* Check for "li r0,SYS_sigreturn". */
  146. insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
  147. if (insn != 0x380001a1)
  148. continue;
  149. /* Check for "sc". */
  150. insn = extract_unsigned_integer (buf + PPC_INSN_SIZE,
  151. PPC_INSN_SIZE, byte_order);
  152. if (insn != 0x44000002)
  153. continue;
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. static struct trad_frame_cache *
  159. ppcfbsd_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
  160. {
  161. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  162. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  163. struct trad_frame_cache *cache;
  164. CORE_ADDR addr, base, func;
  165. gdb_byte buf[PPC_INSN_SIZE];
  166. int i;
  167. if (*this_cache)
  168. return (struct trad_frame_cache *) *this_cache;
  169. cache = trad_frame_cache_zalloc (this_frame);
  170. *this_cache = cache;
  171. func = get_frame_pc (this_frame);
  172. func &= ~(ppcfbsd_page_size - 1);
  173. if (!safe_frame_unwind_memory (this_frame, func, {buf, sizeof buf}))
  174. return cache;
  175. base = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
  176. addr = base + 0x10 + 2 * tdep->wordsize;
  177. for (i = 0; i < ppc_num_gprs; i++, addr += tdep->wordsize)
  178. {
  179. int regnum = i + tdep->ppc_gp0_regnum;
  180. trad_frame_set_reg_addr (cache, regnum, addr);
  181. }
  182. trad_frame_set_reg_addr (cache, tdep->ppc_lr_regnum, addr);
  183. addr += tdep->wordsize;
  184. trad_frame_set_reg_addr (cache, tdep->ppc_cr_regnum, addr);
  185. addr += tdep->wordsize;
  186. trad_frame_set_reg_addr (cache, tdep->ppc_xer_regnum, addr);
  187. addr += tdep->wordsize;
  188. trad_frame_set_reg_addr (cache, tdep->ppc_ctr_regnum, addr);
  189. addr += tdep->wordsize;
  190. trad_frame_set_reg_addr (cache, gdbarch_pc_regnum (gdbarch), addr);
  191. /* SRR0? */
  192. addr += tdep->wordsize;
  193. /* Construct the frame ID using the function start. */
  194. trad_frame_set_id (cache, frame_id_build (base, func));
  195. return cache;
  196. }
  197. static void
  198. ppcfbsd_sigtramp_frame_this_id (struct frame_info *this_frame,
  199. void **this_cache, struct frame_id *this_id)
  200. {
  201. struct trad_frame_cache *cache =
  202. ppcfbsd_sigtramp_frame_cache (this_frame, this_cache);
  203. trad_frame_get_id (cache, this_id);
  204. }
  205. static struct value *
  206. ppcfbsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
  207. void **this_cache, int regnum)
  208. {
  209. struct trad_frame_cache *cache =
  210. ppcfbsd_sigtramp_frame_cache (this_frame, this_cache);
  211. return trad_frame_get_register (cache, this_frame, regnum);
  212. }
  213. static const struct frame_unwind ppcfbsd_sigtramp_frame_unwind = {
  214. "ppc freebsd sigtramp",
  215. SIGTRAMP_FRAME,
  216. default_frame_unwind_stop_reason,
  217. ppcfbsd_sigtramp_frame_this_id,
  218. ppcfbsd_sigtramp_frame_prev_register,
  219. NULL,
  220. ppcfbsd_sigtramp_frame_sniffer
  221. };
  222. static enum return_value_convention
  223. ppcfbsd_return_value (struct gdbarch *gdbarch, struct value *function,
  224. struct type *valtype, struct regcache *regcache,
  225. gdb_byte *readbuf, const gdb_byte *writebuf)
  226. {
  227. return ppc_sysv_abi_broken_return_value (gdbarch, function, valtype,
  228. regcache, readbuf, writebuf);
  229. }
  230. /* Implement the "get_thread_local_address" gdbarch method. */
  231. static CORE_ADDR
  232. ppcfbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
  233. CORE_ADDR lm_addr, CORE_ADDR offset)
  234. {
  235. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  236. struct regcache *regcache;
  237. int tp_offset, tp_regnum;
  238. regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
  239. ptid, gdbarch);
  240. if (tdep->wordsize == 4)
  241. {
  242. tp_offset = 0x7008;
  243. tp_regnum = PPC_R0_REGNUM + 2;
  244. }
  245. else
  246. {
  247. tp_offset = 0x7010;
  248. tp_regnum = PPC_R0_REGNUM + 13;
  249. }
  250. target_fetch_registers (regcache, tp_regnum);
  251. ULONGEST tp;
  252. if (regcache->cooked_read (tp_regnum, &tp) != REG_VALID)
  253. error (_("Unable to fetch tcb pointer"));
  254. /* tp points to the end of the TCB block. The first member of the
  255. TCB is the pointer to the DTV array. */
  256. CORE_ADDR dtv_addr = tp - tp_offset;
  257. return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
  258. }
  259. static void
  260. ppcfbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  261. {
  262. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  263. /* Generic FreeBSD support. */
  264. fbsd_init_abi (info, gdbarch);
  265. /* FreeBSD doesn't support the 128-bit `long double' from the psABI. */
  266. set_gdbarch_long_double_bit (gdbarch, 64);
  267. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  268. if (tdep->wordsize == 4)
  269. {
  270. set_gdbarch_return_value (gdbarch, ppcfbsd_return_value);
  271. set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  272. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  273. svr4_ilp32_fetch_link_map_offsets);
  274. frame_unwind_append_unwinder (gdbarch, &ppcfbsd_sigtramp_frame_unwind);
  275. set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");
  276. }
  277. if (tdep->wordsize == 8)
  278. {
  279. set_gdbarch_convert_from_func_ptr_addr
  280. (gdbarch, ppc64_convert_from_func_ptr_addr);
  281. set_gdbarch_elf_make_msymbol_special (gdbarch,
  282. ppc64_elf_make_msymbol_special);
  283. set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
  284. set_solib_svr4_fetch_link_map_offsets (gdbarch,
  285. svr4_lp64_fetch_link_map_offsets);
  286. set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
  287. }
  288. set_gdbarch_iterate_over_regset_sections
  289. (gdbarch, ppcfbsd_iterate_over_regset_sections);
  290. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  291. svr4_fetch_objfile_link_map);
  292. set_gdbarch_get_thread_local_address (gdbarch,
  293. ppcfbsd_get_thread_local_address);
  294. }
  295. void _initialize_ppcfbsd_tdep ();
  296. void
  297. _initialize_ppcfbsd_tdep ()
  298. {
  299. gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_FREEBSD,
  300. ppcfbsd_init_abi);
  301. gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_FREEBSD,
  302. ppcfbsd_init_abi);
  303. gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_FREEBSD,
  304. ppcfbsd_init_abi);
  305. }