rs6000-aix-tdep.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /* Native support code for PPC AIX, for GDB the GNU debugger.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Free Software Foundation, 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 "osabi.h"
  17. #include "regcache.h"
  18. #include "regset.h"
  19. #include "gdbtypes.h"
  20. #include "gdbcore.h"
  21. #include "target.h"
  22. #include "value.h"
  23. #include "infcall.h"
  24. #include "objfiles.h"
  25. #include "breakpoint.h"
  26. #include "ppc-tdep.h"
  27. #include "rs6000-aix-tdep.h"
  28. #include "xcoffread.h"
  29. #include "solib.h"
  30. #include "solib-aix.h"
  31. #include "target-float.h"
  32. #include "gdbsupport/xml-utils.h"
  33. #include "trad-frame.h"
  34. #include "frame-unwind.h"
  35. /* If the kernel has to deliver a signal, it pushes a sigcontext
  36. structure on the stack and then calls the signal handler, passing
  37. the address of the sigcontext in an argument register. Usually
  38. the signal handler doesn't save this register, so we have to
  39. access the sigcontext structure via an offset from the signal handler
  40. frame.
  41. The following constants were determined by experimentation on AIX 3.2.
  42. sigcontext structure have the mstsave saved under the
  43. sc_jmpbuf.jmp_context. STKMIN(minimum stack size) is 56 for 32-bit
  44. processes, and iar offset under sc_jmpbuf.jmp_context is 40.
  45. ie offsetof(struct sigcontext, sc_jmpbuf.jmp_context.iar).
  46. so PC offset in this case is STKMIN+iar offset, which is 96. */
  47. #define SIG_FRAME_PC_OFFSET 96
  48. #define SIG_FRAME_LR_OFFSET 108
  49. /* STKMIN+grp1 offset, which is 56+228=284 */
  50. #define SIG_FRAME_FP_OFFSET 284
  51. /* 64 bit process.
  52. STKMIN64 is 112 and iar offset is 312. So 112+312=424 */
  53. #define SIG_FRAME_LR_OFFSET64 424
  54. /* STKMIN64+grp1 offset. 112+56=168 */
  55. #define SIG_FRAME_FP_OFFSET64 168
  56. /* Minimum possible text address in AIX. */
  57. #define AIX_TEXT_SEGMENT_BASE 0x10000000
  58. static struct trad_frame_cache *
  59. aix_sighandle_frame_cache (struct frame_info *this_frame,
  60. void **this_cache)
  61. {
  62. LONGEST backchain;
  63. CORE_ADDR base, base_orig, func;
  64. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  65. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  66. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  67. struct trad_frame_cache *this_trad_cache;
  68. if ((*this_cache) != NULL)
  69. return (struct trad_frame_cache *) (*this_cache);
  70. this_trad_cache = trad_frame_cache_zalloc (this_frame);
  71. (*this_cache) = this_trad_cache;
  72. base = get_frame_register_unsigned (this_frame,
  73. gdbarch_sp_regnum (gdbarch));
  74. base_orig = base;
  75. if (tdep->wordsize == 4)
  76. {
  77. func = read_memory_unsigned_integer (base_orig +
  78. SIG_FRAME_PC_OFFSET + 8,
  79. tdep->wordsize, byte_order);
  80. safe_read_memory_integer (base_orig + SIG_FRAME_FP_OFFSET + 8,
  81. tdep->wordsize, byte_order, &backchain);
  82. base = (CORE_ADDR)backchain;
  83. }
  84. else
  85. {
  86. func = read_memory_unsigned_integer (base_orig +
  87. SIG_FRAME_LR_OFFSET64,
  88. tdep->wordsize, byte_order);
  89. safe_read_memory_integer (base_orig + SIG_FRAME_FP_OFFSET64,
  90. tdep->wordsize, byte_order, &backchain);
  91. base = (CORE_ADDR)backchain;
  92. }
  93. trad_frame_set_reg_value (this_trad_cache, gdbarch_pc_regnum (gdbarch), func);
  94. trad_frame_set_reg_value (this_trad_cache, gdbarch_sp_regnum (gdbarch), base);
  95. if (tdep->wordsize == 4)
  96. trad_frame_set_reg_addr (this_trad_cache, tdep->ppc_lr_regnum,
  97. base_orig + 0x38 + 52 + 8);
  98. else
  99. trad_frame_set_reg_addr (this_trad_cache, tdep->ppc_lr_regnum,
  100. base_orig + 0x70 + 320);
  101. trad_frame_set_id (this_trad_cache, frame_id_build (base, func));
  102. trad_frame_set_this_base (this_trad_cache, base);
  103. return this_trad_cache;
  104. }
  105. static void
  106. aix_sighandle_frame_this_id (struct frame_info *this_frame,
  107. void **this_prologue_cache,
  108. struct frame_id *this_id)
  109. {
  110. struct trad_frame_cache *this_trad_cache
  111. = aix_sighandle_frame_cache (this_frame, this_prologue_cache);
  112. trad_frame_get_id (this_trad_cache, this_id);
  113. }
  114. static struct value *
  115. aix_sighandle_frame_prev_register (struct frame_info *this_frame,
  116. void **this_prologue_cache, int regnum)
  117. {
  118. struct trad_frame_cache *this_trad_cache
  119. = aix_sighandle_frame_cache (this_frame, this_prologue_cache);
  120. return trad_frame_get_register (this_trad_cache, this_frame, regnum);
  121. }
  122. static int
  123. aix_sighandle_frame_sniffer (const struct frame_unwind *self,
  124. struct frame_info *this_frame,
  125. void **this_prologue_cache)
  126. {
  127. CORE_ADDR pc = get_frame_pc (this_frame);
  128. if (pc && pc < AIX_TEXT_SEGMENT_BASE)
  129. return 1;
  130. return 0;
  131. }
  132. /* AIX signal handler frame unwinder */
  133. static const struct frame_unwind aix_sighandle_frame_unwind = {
  134. "rs6000 aix sighandle",
  135. SIGTRAMP_FRAME,
  136. default_frame_unwind_stop_reason,
  137. aix_sighandle_frame_this_id,
  138. aix_sighandle_frame_prev_register,
  139. NULL,
  140. aix_sighandle_frame_sniffer
  141. };
  142. /* Core file support. */
  143. static struct ppc_reg_offsets rs6000_aix32_reg_offsets =
  144. {
  145. /* General-purpose registers. */
  146. 208, /* r0_offset */
  147. 4, /* gpr_size */
  148. 4, /* xr_size */
  149. 24, /* pc_offset */
  150. 28, /* ps_offset */
  151. 32, /* cr_offset */
  152. 36, /* lr_offset */
  153. 40, /* ctr_offset */
  154. 44, /* xer_offset */
  155. 48, /* mq_offset */
  156. /* Floating-point registers. */
  157. 336, /* f0_offset */
  158. 56, /* fpscr_offset */
  159. 4 /* fpscr_size */
  160. };
  161. static struct ppc_reg_offsets rs6000_aix64_reg_offsets =
  162. {
  163. /* General-purpose registers. */
  164. 0, /* r0_offset */
  165. 8, /* gpr_size */
  166. 4, /* xr_size */
  167. 264, /* pc_offset */
  168. 256, /* ps_offset */
  169. 288, /* cr_offset */
  170. 272, /* lr_offset */
  171. 280, /* ctr_offset */
  172. 292, /* xer_offset */
  173. -1, /* mq_offset */
  174. /* Floating-point registers. */
  175. 312, /* f0_offset */
  176. 296, /* fpscr_offset */
  177. 4 /* fpscr_size */
  178. };
  179. /* Supply register REGNUM in the general-purpose register set REGSET
  180. from the buffer specified by GREGS and LEN to register cache
  181. REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
  182. static void
  183. rs6000_aix_supply_regset (const struct regset *regset,
  184. struct regcache *regcache, int regnum,
  185. const void *gregs, size_t len)
  186. {
  187. ppc_supply_gregset (regset, regcache, regnum, gregs, len);
  188. ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
  189. }
  190. /* Collect register REGNUM in the general-purpose register set
  191. REGSET, from register cache REGCACHE into the buffer specified by
  192. GREGS and LEN. If REGNUM is -1, do this for all registers in
  193. REGSET. */
  194. static void
  195. rs6000_aix_collect_regset (const struct regset *regset,
  196. const struct regcache *regcache, int regnum,
  197. void *gregs, size_t len)
  198. {
  199. ppc_collect_gregset (regset, regcache, regnum, gregs, len);
  200. ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
  201. }
  202. /* AIX register set. */
  203. static const struct regset rs6000_aix32_regset =
  204. {
  205. &rs6000_aix32_reg_offsets,
  206. rs6000_aix_supply_regset,
  207. rs6000_aix_collect_regset,
  208. };
  209. static const struct regset rs6000_aix64_regset =
  210. {
  211. &rs6000_aix64_reg_offsets,
  212. rs6000_aix_supply_regset,
  213. rs6000_aix_collect_regset,
  214. };
  215. /* Iterate over core file register note sections. */
  216. static void
  217. rs6000_aix_iterate_over_regset_sections (struct gdbarch *gdbarch,
  218. iterate_over_regset_sections_cb *cb,
  219. void *cb_data,
  220. const struct regcache *regcache)
  221. {
  222. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  223. if (tdep->wordsize == 4)
  224. cb (".reg", 592, 592, &rs6000_aix32_regset, NULL, cb_data);
  225. else
  226. cb (".reg", 576, 576, &rs6000_aix64_regset, NULL, cb_data);
  227. }
  228. /* Pass the arguments in either registers, or in the stack. In RS/6000,
  229. the first eight words of the argument list (that might be less than
  230. eight parameters if some parameters occupy more than one word) are
  231. passed in r3..r10 registers. Float and double parameters are
  232. passed in fpr's, in addition to that. Rest of the parameters if any
  233. are passed in user stack. There might be cases in which half of the
  234. parameter is copied into registers, the other half is pushed into
  235. stack.
  236. Stack must be aligned on 64-bit boundaries when synthesizing
  237. function calls.
  238. If the function is returning a structure, then the return address is passed
  239. in r3, then the first 7 words of the parameters can be passed in registers,
  240. starting from r4. */
  241. static CORE_ADDR
  242. rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  243. struct regcache *regcache, CORE_ADDR bp_addr,
  244. int nargs, struct value **args, CORE_ADDR sp,
  245. function_call_return_method return_method,
  246. CORE_ADDR struct_addr)
  247. {
  248. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  249. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  250. int ii;
  251. int len = 0;
  252. int argno; /* current argument number */
  253. int argbytes; /* current argument byte */
  254. gdb_byte tmp_buffer[50];
  255. int f_argno = 0; /* current floating point argno */
  256. int wordsize = tdep->wordsize;
  257. CORE_ADDR func_addr = find_function_addr (function, NULL);
  258. struct value *arg = 0;
  259. struct type *type;
  260. ULONGEST saved_sp;
  261. /* The calling convention this function implements assumes the
  262. processor has floating-point registers. We shouldn't be using it
  263. on PPC variants that lack them. */
  264. gdb_assert (ppc_floating_point_unit_p (gdbarch));
  265. /* The first eight words of ther arguments are passed in registers.
  266. Copy them appropriately. */
  267. ii = 0;
  268. /* If the function is returning a `struct', then the first word
  269. (which will be passed in r3) is used for struct return address.
  270. In that case we should advance one word and start from r4
  271. register to copy parameters. */
  272. if (return_method == return_method_struct)
  273. {
  274. regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  275. struct_addr);
  276. ii++;
  277. }
  278. /* effectively indirect call... gcc does...
  279. return_val example( float, int);
  280. eabi:
  281. float in fp0, int in r3
  282. offset of stack on overflow 8/16
  283. for varargs, must go by type.
  284. power open:
  285. float in r3&r4, int in r5
  286. offset of stack on overflow different
  287. both:
  288. return in r3 or f0. If no float, must study how gcc emulates floats;
  289. pay attention to arg promotion.
  290. User may have to cast\args to handle promotion correctly
  291. since gdb won't know if prototype supplied or not. */
  292. for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
  293. {
  294. int reg_size = register_size (gdbarch, ii + 3);
  295. arg = args[argno];
  296. type = check_typedef (value_type (arg));
  297. len = TYPE_LENGTH (type);
  298. if (type->code () == TYPE_CODE_FLT)
  299. {
  300. /* Floating point arguments are passed in fpr's, as well as gpr's.
  301. There are 13 fpr's reserved for passing parameters. At this point
  302. there is no way we would run out of them.
  303. Always store the floating point value using the register's
  304. floating-point format. */
  305. const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
  306. gdb_byte reg_val[PPC_MAX_REGISTER_SIZE];
  307. struct type *reg_type = register_type (gdbarch, fp_regnum);
  308. gdb_assert (len <= 8);
  309. target_float_convert (value_contents (arg).data (), type, reg_val,
  310. reg_type);
  311. regcache->cooked_write (fp_regnum, reg_val);
  312. ++f_argno;
  313. }
  314. if (len > reg_size)
  315. {
  316. /* Argument takes more than one register. */
  317. while (argbytes < len)
  318. {
  319. gdb_byte word[PPC_MAX_REGISTER_SIZE];
  320. memset (word, 0, reg_size);
  321. memcpy (word,
  322. ((char *) value_contents (arg).data ()) + argbytes,
  323. (len - argbytes) > reg_size
  324. ? reg_size : len - argbytes);
  325. regcache->cooked_write (tdep->ppc_gp0_regnum + 3 + ii, word);
  326. ++ii, argbytes += reg_size;
  327. if (ii >= 8)
  328. goto ran_out_of_registers_for_arguments;
  329. }
  330. argbytes = 0;
  331. --ii;
  332. }
  333. else
  334. {
  335. /* Argument can fit in one register. No problem. */
  336. gdb_byte word[PPC_MAX_REGISTER_SIZE];
  337. memset (word, 0, reg_size);
  338. memcpy (word, value_contents (arg).data (), len);
  339. regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
  340. }
  341. ++argno;
  342. }
  343. ran_out_of_registers_for_arguments:
  344. regcache_cooked_read_unsigned (regcache,
  345. gdbarch_sp_regnum (gdbarch),
  346. &saved_sp);
  347. /* Location for 8 parameters are always reserved. */
  348. sp -= wordsize * 8;
  349. /* Another six words for back chain, TOC register, link register, etc. */
  350. sp -= wordsize * 6;
  351. /* Stack pointer must be quadword aligned. */
  352. sp &= -16;
  353. /* If there are more arguments, allocate space for them in
  354. the stack, then push them starting from the ninth one. */
  355. if ((argno < nargs) || argbytes)
  356. {
  357. int space = 0, jj;
  358. if (argbytes)
  359. {
  360. space += ((len - argbytes + 3) & -4);
  361. jj = argno + 1;
  362. }
  363. else
  364. jj = argno;
  365. for (; jj < nargs; ++jj)
  366. {
  367. struct value *val = args[jj];
  368. space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
  369. }
  370. /* Add location required for the rest of the parameters. */
  371. space = (space + 15) & -16;
  372. sp -= space;
  373. /* This is another instance we need to be concerned about
  374. securing our stack space. If we write anything underneath %sp
  375. (r1), we might conflict with the kernel who thinks he is free
  376. to use this area. So, update %sp first before doing anything
  377. else. */
  378. regcache_raw_write_signed (regcache,
  379. gdbarch_sp_regnum (gdbarch), sp);
  380. /* If the last argument copied into the registers didn't fit there
  381. completely, push the rest of it into stack. */
  382. if (argbytes)
  383. {
  384. write_memory (sp + 24 + (ii * 4),
  385. value_contents (arg).data () + argbytes,
  386. len - argbytes);
  387. ++argno;
  388. ii += ((len - argbytes + 3) & -4) / 4;
  389. }
  390. /* Push the rest of the arguments into stack. */
  391. for (; argno < nargs; ++argno)
  392. {
  393. arg = args[argno];
  394. type = check_typedef (value_type (arg));
  395. len = TYPE_LENGTH (type);
  396. /* Float types should be passed in fpr's, as well as in the
  397. stack. */
  398. if (type->code () == TYPE_CODE_FLT && f_argno < 13)
  399. {
  400. gdb_assert (len <= 8);
  401. regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
  402. value_contents (arg).data ());
  403. ++f_argno;
  404. }
  405. write_memory (sp + 24 + (ii * 4), value_contents (arg).data (), len);
  406. ii += ((len + 3) & -4) / 4;
  407. }
  408. }
  409. /* Set the stack pointer. According to the ABI, the SP is meant to
  410. be set _before_ the corresponding stack space is used. On AIX,
  411. this even applies when the target has been completely stopped!
  412. Not doing this can lead to conflicts with the kernel which thinks
  413. that it still has control over this not-yet-allocated stack
  414. region. */
  415. regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
  416. /* Set back chain properly. */
  417. store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
  418. write_memory (sp, tmp_buffer, wordsize);
  419. /* Point the inferior function call's return address at the dummy's
  420. breakpoint. */
  421. regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
  422. /* Set the TOC register value. */
  423. regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum,
  424. solib_aix_get_toc_value (func_addr));
  425. target_store_registers (regcache, -1);
  426. return sp;
  427. }
  428. static enum return_value_convention
  429. rs6000_return_value (struct gdbarch *gdbarch, struct value *function,
  430. struct type *valtype, struct regcache *regcache,
  431. gdb_byte *readbuf, const gdb_byte *writebuf)
  432. {
  433. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  434. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  435. /* The calling convention this function implements assumes the
  436. processor has floating-point registers. We shouldn't be using it
  437. on PowerPC variants that lack them. */
  438. gdb_assert (ppc_floating_point_unit_p (gdbarch));
  439. /* AltiVec extension: Functions that declare a vector data type as a
  440. return value place that return value in VR2. */
  441. if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
  442. && TYPE_LENGTH (valtype) == 16)
  443. {
  444. if (readbuf)
  445. regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
  446. if (writebuf)
  447. regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
  448. return RETURN_VALUE_REGISTER_CONVENTION;
  449. }
  450. /* If the called subprogram returns an aggregate, there exists an
  451. implicit first argument, whose value is the address of a caller-
  452. allocated buffer into which the callee is assumed to store its
  453. return value. All explicit parameters are appropriately
  454. relabeled. */
  455. if (valtype->code () == TYPE_CODE_STRUCT
  456. || valtype->code () == TYPE_CODE_UNION
  457. || valtype->code () == TYPE_CODE_ARRAY)
  458. return RETURN_VALUE_STRUCT_CONVENTION;
  459. /* Scalar floating-point values are returned in FPR1 for float or
  460. double, and in FPR1:FPR2 for quadword precision. Fortran
  461. complex*8 and complex*16 are returned in FPR1:FPR2, and
  462. complex*32 is returned in FPR1:FPR4. */
  463. if (valtype->code () == TYPE_CODE_FLT
  464. && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
  465. {
  466. struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
  467. gdb_byte regval[8];
  468. /* FIXME: kettenis/2007-01-01: Add support for quadword
  469. precision and complex. */
  470. if (readbuf)
  471. {
  472. regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
  473. target_float_convert (regval, regtype, readbuf, valtype);
  474. }
  475. if (writebuf)
  476. {
  477. target_float_convert (writebuf, valtype, regval, regtype);
  478. regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
  479. }
  480. return RETURN_VALUE_REGISTER_CONVENTION;
  481. }
  482. /* Values of the types int, long, short, pointer, and char (length
  483. is less than or equal to four bytes), as well as bit values of
  484. lengths less than or equal to 32 bits, must be returned right
  485. justified in GPR3 with signed values sign extended and unsigned
  486. values zero extended, as necessary. */
  487. if (TYPE_LENGTH (valtype) <= tdep->wordsize)
  488. {
  489. if (readbuf)
  490. {
  491. ULONGEST regval;
  492. /* For reading we don't have to worry about sign extension. */
  493. regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  494. &regval);
  495. store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
  496. regval);
  497. }
  498. if (writebuf)
  499. {
  500. /* For writing, use unpack_long since that should handle any
  501. required sign extension. */
  502. regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  503. unpack_long (valtype, writebuf));
  504. }
  505. return RETURN_VALUE_REGISTER_CONVENTION;
  506. }
  507. /* Eight-byte non-floating-point scalar values must be returned in
  508. GPR3:GPR4. */
  509. if (TYPE_LENGTH (valtype) == 8)
  510. {
  511. gdb_assert (valtype->code () != TYPE_CODE_FLT);
  512. gdb_assert (tdep->wordsize == 4);
  513. if (readbuf)
  514. {
  515. gdb_byte regval[8];
  516. regcache->cooked_read (tdep->ppc_gp0_regnum + 3, regval);
  517. regcache->cooked_read (tdep->ppc_gp0_regnum + 4, regval + 4);
  518. memcpy (readbuf, regval, 8);
  519. }
  520. if (writebuf)
  521. {
  522. regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
  523. regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
  524. }
  525. return RETURN_VALUE_REGISTER_CONVENTION;
  526. }
  527. return RETURN_VALUE_STRUCT_CONVENTION;
  528. }
  529. /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).
  530. Usually a function pointer's representation is simply the address
  531. of the function. On the RS/6000 however, a function pointer is
  532. represented by a pointer to an OPD entry. This OPD entry contains
  533. three words, the first word is the address of the function, the
  534. second word is the TOC pointer (r2), and the third word is the
  535. static chain value. Throughout GDB it is currently assumed that a
  536. function pointer contains the address of the function, which is not
  537. easy to fix. In addition, the conversion of a function address to
  538. a function pointer would require allocation of an OPD entry in the
  539. inferior's memory space, with all its drawbacks. To be able to
  540. call C++ virtual methods in the inferior (which are called via
  541. function pointers), find_function_addr uses this function to get the
  542. function address from a function pointer. */
  543. /* Return real function address if ADDR (a function pointer) is in the data
  544. space and is therefore a special function pointer. */
  545. static CORE_ADDR
  546. rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
  547. CORE_ADDR addr,
  548. struct target_ops *targ)
  549. {
  550. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  551. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  552. struct obj_section *s;
  553. s = find_pc_section (addr);
  554. /* Normally, functions live inside a section that is executable.
  555. So, if ADDR points to a non-executable section, then treat it
  556. as a function descriptor and return the target address iff
  557. the target address itself points to a section that is executable. */
  558. if (s && (s->the_bfd_section->flags & SEC_CODE) == 0)
  559. {
  560. CORE_ADDR pc = 0;
  561. struct obj_section *pc_section;
  562. try
  563. {
  564. pc = read_memory_unsigned_integer (addr, tdep->wordsize, byte_order);
  565. }
  566. catch (const gdb_exception_error &e)
  567. {
  568. /* An error occured during reading. Probably a memory error
  569. due to the section not being loaded yet. This address
  570. cannot be a function descriptor. */
  571. return addr;
  572. }
  573. pc_section = find_pc_section (pc);
  574. if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
  575. return pc;
  576. }
  577. return addr;
  578. }
  579. /* Calculate the destination of a branch/jump. Return -1 if not a branch. */
  580. static CORE_ADDR
  581. branch_dest (struct regcache *regcache, int opcode, int instr,
  582. CORE_ADDR pc, CORE_ADDR safety)
  583. {
  584. struct gdbarch *gdbarch = regcache->arch ();
  585. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  586. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  587. CORE_ADDR dest;
  588. int immediate;
  589. int absolute;
  590. int ext_op;
  591. absolute = (int) ((instr >> 1) & 1);
  592. switch (opcode)
  593. {
  594. case 18:
  595. immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
  596. if (absolute)
  597. dest = immediate;
  598. else
  599. dest = pc + immediate;
  600. break;
  601. case 16:
  602. immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
  603. if (absolute)
  604. dest = immediate;
  605. else
  606. dest = pc + immediate;
  607. break;
  608. case 19:
  609. ext_op = (instr >> 1) & 0x3ff;
  610. if (ext_op == 16) /* br conditional register */
  611. {
  612. dest = regcache_raw_get_unsigned (regcache, tdep->ppc_lr_regnum) & ~3;
  613. /* If we are about to return from a signal handler, dest is
  614. something like 0x3c90. The current frame is a signal handler
  615. caller frame, upon completion of the sigreturn system call
  616. execution will return to the saved PC in the frame. */
  617. if (dest < AIX_TEXT_SEGMENT_BASE)
  618. {
  619. struct frame_info *frame = get_current_frame ();
  620. dest = read_memory_unsigned_integer
  621. (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
  622. tdep->wordsize, byte_order);
  623. }
  624. }
  625. else if (ext_op == 528) /* br cond to count reg */
  626. {
  627. dest = regcache_raw_get_unsigned (regcache,
  628. tdep->ppc_ctr_regnum) & ~3;
  629. /* If we are about to execute a system call, dest is something
  630. like 0x22fc or 0x3b00. Upon completion the system call
  631. will return to the address in the link register. */
  632. if (dest < AIX_TEXT_SEGMENT_BASE)
  633. dest = regcache_raw_get_unsigned (regcache,
  634. tdep->ppc_lr_regnum) & ~3;
  635. }
  636. else
  637. return -1;
  638. break;
  639. default:
  640. return -1;
  641. }
  642. return (dest < AIX_TEXT_SEGMENT_BASE) ? safety : dest;
  643. }
  644. /* AIX does not support PT_STEP. Simulate it. */
  645. static std::vector<CORE_ADDR>
  646. rs6000_software_single_step (struct regcache *regcache)
  647. {
  648. struct gdbarch *gdbarch = regcache->arch ();
  649. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  650. int ii, insn;
  651. CORE_ADDR loc;
  652. CORE_ADDR breaks[2];
  653. int opcode;
  654. loc = regcache_read_pc (regcache);
  655. insn = read_memory_integer (loc, 4, byte_order);
  656. std::vector<CORE_ADDR> next_pcs = ppc_deal_with_atomic_sequence (regcache);
  657. if (!next_pcs.empty ())
  658. return next_pcs;
  659. breaks[0] = loc + PPC_INSN_SIZE;
  660. opcode = insn >> 26;
  661. breaks[1] = branch_dest (regcache, opcode, insn, loc, breaks[0]);
  662. /* Don't put two breakpoints on the same address. */
  663. if (breaks[1] == breaks[0])
  664. breaks[1] = -1;
  665. for (ii = 0; ii < 2; ++ii)
  666. {
  667. /* ignore invalid breakpoint. */
  668. if (breaks[ii] == -1)
  669. continue;
  670. next_pcs.push_back (breaks[ii]);
  671. }
  672. errno = 0; /* FIXME, don't ignore errors! */
  673. /* What errors? {read,write}_memory call error(). */
  674. return next_pcs;
  675. }
  676. /* Implement the "auto_wide_charset" gdbarch method for this platform. */
  677. static const char *
  678. rs6000_aix_auto_wide_charset (void)
  679. {
  680. return "UTF-16";
  681. }
  682. /* Implement an osabi sniffer for RS6000/AIX.
  683. This function assumes that ABFD's flavour is XCOFF. In other words,
  684. it should be registered as a sniffer for bfd_target_xcoff_flavour
  685. objfiles only. A failed assertion will be raised if this condition
  686. is not met. */
  687. static enum gdb_osabi
  688. rs6000_aix_osabi_sniffer (bfd *abfd)
  689. {
  690. gdb_assert (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
  691. /* The only noticeable difference between Lynx178 XCOFF files and
  692. AIX XCOFF files comes from the fact that there are no shared
  693. libraries on Lynx178. On AIX, we are betting that an executable
  694. linked with no shared library will never exist. */
  695. if (xcoff_get_n_import_files (abfd) <= 0)
  696. return GDB_OSABI_UNKNOWN;
  697. return GDB_OSABI_AIX;
  698. }
  699. /* A structure encoding the offset and size of a field within
  700. a struct. */
  701. struct field_info
  702. {
  703. int offset;
  704. int size;
  705. };
  706. /* A structure describing the layout of all the fields of interest
  707. in AIX's struct ld_info. Each field in this struct corresponds
  708. to the field of the same name in struct ld_info. */
  709. struct ld_info_desc
  710. {
  711. struct field_info ldinfo_next;
  712. struct field_info ldinfo_fd;
  713. struct field_info ldinfo_textorg;
  714. struct field_info ldinfo_textsize;
  715. struct field_info ldinfo_dataorg;
  716. struct field_info ldinfo_datasize;
  717. struct field_info ldinfo_filename;
  718. };
  719. /* The following data has been generated by compiling and running
  720. the following program on AIX 5.3. */
  721. #if 0
  722. #include <stddef.h>
  723. #include <stdio.h>
  724. #define __LDINFO_PTRACE32__
  725. #define __LDINFO_PTRACE64__
  726. #include <sys/ldr.h>
  727. #define pinfo(type,member) \
  728. { \
  729. struct type ldi = {0}; \
  730. \
  731. printf (" {%d, %d},\t/* %s */\n", \
  732. offsetof (struct type, member), \
  733. sizeof (ldi.member), \
  734. #member); \
  735. } \
  736. while (0)
  737. int
  738. main (void)
  739. {
  740. printf ("static const struct ld_info_desc ld_info32_desc =\n{\n");
  741. pinfo (__ld_info32, ldinfo_next);
  742. pinfo (__ld_info32, ldinfo_fd);
  743. pinfo (__ld_info32, ldinfo_textorg);
  744. pinfo (__ld_info32, ldinfo_textsize);
  745. pinfo (__ld_info32, ldinfo_dataorg);
  746. pinfo (__ld_info32, ldinfo_datasize);
  747. pinfo (__ld_info32, ldinfo_filename);
  748. printf ("};\n");
  749. printf ("\n");
  750. printf ("static const struct ld_info_desc ld_info64_desc =\n{\n");
  751. pinfo (__ld_info64, ldinfo_next);
  752. pinfo (__ld_info64, ldinfo_fd);
  753. pinfo (__ld_info64, ldinfo_textorg);
  754. pinfo (__ld_info64, ldinfo_textsize);
  755. pinfo (__ld_info64, ldinfo_dataorg);
  756. pinfo (__ld_info64, ldinfo_datasize);
  757. pinfo (__ld_info64, ldinfo_filename);
  758. printf ("};\n");
  759. return 0;
  760. }
  761. #endif /* 0 */
  762. /* Layout of the 32bit version of struct ld_info. */
  763. static const struct ld_info_desc ld_info32_desc =
  764. {
  765. {0, 4}, /* ldinfo_next */
  766. {4, 4}, /* ldinfo_fd */
  767. {8, 4}, /* ldinfo_textorg */
  768. {12, 4}, /* ldinfo_textsize */
  769. {16, 4}, /* ldinfo_dataorg */
  770. {20, 4}, /* ldinfo_datasize */
  771. {24, 2}, /* ldinfo_filename */
  772. };
  773. /* Layout of the 64bit version of struct ld_info. */
  774. static const struct ld_info_desc ld_info64_desc =
  775. {
  776. {0, 4}, /* ldinfo_next */
  777. {8, 4}, /* ldinfo_fd */
  778. {16, 8}, /* ldinfo_textorg */
  779. {24, 8}, /* ldinfo_textsize */
  780. {32, 8}, /* ldinfo_dataorg */
  781. {40, 8}, /* ldinfo_datasize */
  782. {48, 2}, /* ldinfo_filename */
  783. };
  784. /* A structured representation of one entry read from the ld_info
  785. binary data provided by the AIX loader. */
  786. struct ld_info
  787. {
  788. ULONGEST next;
  789. int fd;
  790. CORE_ADDR textorg;
  791. ULONGEST textsize;
  792. CORE_ADDR dataorg;
  793. ULONGEST datasize;
  794. char *filename;
  795. char *member_name;
  796. };
  797. /* Return a struct ld_info object corresponding to the entry at
  798. LDI_BUF.
  799. Note that the filename and member_name strings still point
  800. to the data in LDI_BUF. So LDI_BUF must not be deallocated
  801. while the struct ld_info object returned is in use. */
  802. static struct ld_info
  803. rs6000_aix_extract_ld_info (struct gdbarch *gdbarch,
  804. const gdb_byte *ldi_buf)
  805. {
  806. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  807. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  808. struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  809. const struct ld_info_desc desc
  810. = tdep->wordsize == 8 ? ld_info64_desc : ld_info32_desc;
  811. struct ld_info info;
  812. info.next = extract_unsigned_integer (ldi_buf + desc.ldinfo_next.offset,
  813. desc.ldinfo_next.size,
  814. byte_order);
  815. info.fd = extract_signed_integer (ldi_buf + desc.ldinfo_fd.offset,
  816. desc.ldinfo_fd.size,
  817. byte_order);
  818. info.textorg = extract_typed_address (ldi_buf + desc.ldinfo_textorg.offset,
  819. ptr_type);
  820. info.textsize
  821. = extract_unsigned_integer (ldi_buf + desc.ldinfo_textsize.offset,
  822. desc.ldinfo_textsize.size,
  823. byte_order);
  824. info.dataorg = extract_typed_address (ldi_buf + desc.ldinfo_dataorg.offset,
  825. ptr_type);
  826. info.datasize
  827. = extract_unsigned_integer (ldi_buf + desc.ldinfo_datasize.offset,
  828. desc.ldinfo_datasize.size,
  829. byte_order);
  830. info.filename = (char *) ldi_buf + desc.ldinfo_filename.offset;
  831. info.member_name = info.filename + strlen (info.filename) + 1;
  832. return info;
  833. }
  834. /* Append to OBJSTACK an XML string description of the shared library
  835. corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
  836. format. */
  837. static void
  838. rs6000_aix_shared_library_to_xml (struct ld_info *ldi,
  839. struct obstack *obstack)
  840. {
  841. obstack_grow_str (obstack, "<library name=\"");
  842. std::string p = xml_escape_text (ldi->filename);
  843. obstack_grow_str (obstack, p.c_str ());
  844. obstack_grow_str (obstack, "\"");
  845. if (ldi->member_name[0] != '\0')
  846. {
  847. obstack_grow_str (obstack, " member=\"");
  848. p = xml_escape_text (ldi->member_name);
  849. obstack_grow_str (obstack, p.c_str ());
  850. obstack_grow_str (obstack, "\"");
  851. }
  852. obstack_grow_str (obstack, " text_addr=\"");
  853. obstack_grow_str (obstack, core_addr_to_string (ldi->textorg));
  854. obstack_grow_str (obstack, "\"");
  855. obstack_grow_str (obstack, " text_size=\"");
  856. obstack_grow_str (obstack, pulongest (ldi->textsize));
  857. obstack_grow_str (obstack, "\"");
  858. obstack_grow_str (obstack, " data_addr=\"");
  859. obstack_grow_str (obstack, core_addr_to_string (ldi->dataorg));
  860. obstack_grow_str (obstack, "\"");
  861. obstack_grow_str (obstack, " data_size=\"");
  862. obstack_grow_str (obstack, pulongest (ldi->datasize));
  863. obstack_grow_str (obstack, "\"");
  864. obstack_grow_str (obstack, "></library>");
  865. }
  866. /* Convert the ld_info binary data provided by the AIX loader into
  867. an XML representation following the TARGET_OBJECT_LIBRARIES_AIX
  868. format.
  869. LDI_BUF is a buffer containing the ld_info data.
  870. READBUF, OFFSET and LEN follow the same semantics as target_ops'
  871. to_xfer_partial target_ops method.
  872. If CLOSE_LDINFO_FD is nonzero, then this routine also closes
  873. the ldinfo_fd file descriptor. This is useful when the ldinfo
  874. data is obtained via ptrace, as ptrace opens a file descriptor
  875. for each and every entry; but we cannot use this descriptor
  876. as the consumer of the XML library list might live in a different
  877. process. */
  878. ULONGEST
  879. rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
  880. gdb_byte *readbuf, ULONGEST offset, ULONGEST len,
  881. int close_ldinfo_fd)
  882. {
  883. struct obstack obstack;
  884. const char *buf;
  885. ULONGEST len_avail;
  886. obstack_init (&obstack);
  887. obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");
  888. while (1)
  889. {
  890. struct ld_info ldi = rs6000_aix_extract_ld_info (gdbarch, ldi_buf);
  891. rs6000_aix_shared_library_to_xml (&ldi, &obstack);
  892. if (close_ldinfo_fd)
  893. close (ldi.fd);
  894. if (!ldi.next)
  895. break;
  896. ldi_buf = ldi_buf + ldi.next;
  897. }
  898. obstack_grow_str0 (&obstack, "</library-list-aix>\n");
  899. buf = (const char *) obstack_finish (&obstack);
  900. len_avail = strlen (buf);
  901. if (offset >= len_avail)
  902. len= 0;
  903. else
  904. {
  905. if (len > len_avail - offset)
  906. len = len_avail - offset;
  907. memcpy (readbuf, buf + offset, len);
  908. }
  909. obstack_free (&obstack, NULL);
  910. return len;
  911. }
  912. /* Implement the core_xfer_shared_libraries_aix gdbarch method. */
  913. static ULONGEST
  914. rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
  915. gdb_byte *readbuf,
  916. ULONGEST offset,
  917. ULONGEST len)
  918. {
  919. struct bfd_section *ldinfo_sec;
  920. int ldinfo_size;
  921. ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
  922. if (ldinfo_sec == NULL)
  923. error (_("cannot find .ldinfo section from core file: %s"),
  924. bfd_errmsg (bfd_get_error ()));
  925. ldinfo_size = bfd_section_size (ldinfo_sec);
  926. gdb::byte_vector ldinfo_buf (ldinfo_size);
  927. if (! bfd_get_section_contents (core_bfd, ldinfo_sec,
  928. ldinfo_buf.data (), 0, ldinfo_size))
  929. error (_("unable to read .ldinfo section from core file: %s"),
  930. bfd_errmsg (bfd_get_error ()));
  931. return rs6000_aix_ld_info_to_xml (gdbarch, ldinfo_buf.data (), readbuf,
  932. offset, len, 0);
  933. }
  934. static void
  935. rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
  936. {
  937. ppc_gdbarch_tdep *tdep = (ppc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  938. /* RS6000/AIX does not support PT_STEP. Has to be simulated. */
  939. set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
  940. /* Displaced stepping is currently not supported in combination with
  941. software single-stepping. These override the values set by
  942. rs6000_gdbarch_init. */
  943. set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
  944. set_gdbarch_displaced_step_fixup (gdbarch, NULL);
  945. set_gdbarch_displaced_step_prepare (gdbarch, NULL);
  946. set_gdbarch_displaced_step_finish (gdbarch, NULL);
  947. set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
  948. set_gdbarch_return_value (gdbarch, rs6000_return_value);
  949. set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  950. /* Handle RS/6000 function pointers (which are really function
  951. descriptors). */
  952. set_gdbarch_convert_from_func_ptr_addr
  953. (gdbarch, rs6000_convert_from_func_ptr_addr);
  954. /* Core file support. */
  955. set_gdbarch_iterate_over_regset_sections
  956. (gdbarch, rs6000_aix_iterate_over_regset_sections);
  957. set_gdbarch_core_xfer_shared_libraries_aix
  958. (gdbarch, rs6000_aix_core_xfer_shared_libraries_aix);
  959. if (tdep->wordsize == 8)
  960. tdep->lr_frame_offset = 16;
  961. else
  962. tdep->lr_frame_offset = 8;
  963. if (tdep->wordsize == 4)
  964. /* PowerOpen / AIX 32 bit. The saved area or red zone consists of
  965. 19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
  966. Problem is, 220 isn't frame (16 byte) aligned. Round it up to
  967. 224. */
  968. set_gdbarch_frame_red_zone_size (gdbarch, 224);
  969. else
  970. set_gdbarch_frame_red_zone_size (gdbarch, 0);
  971. if (tdep->wordsize == 8)
  972. set_gdbarch_wchar_bit (gdbarch, 32);
  973. else
  974. set_gdbarch_wchar_bit (gdbarch, 16);
  975. set_gdbarch_wchar_signed (gdbarch, 0);
  976. set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
  977. set_solib_ops (gdbarch, &solib_aix_so_ops);
  978. frame_unwind_append_unwinder (gdbarch, &aix_sighandle_frame_unwind);
  979. }
  980. void _initialize_rs6000_aix_tdep ();
  981. void
  982. _initialize_rs6000_aix_tdep ()
  983. {
  984. gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
  985. bfd_target_xcoff_flavour,
  986. rs6000_aix_osabi_sniffer);
  987. gdbarch_register_osabi_sniffer (bfd_arch_powerpc,
  988. bfd_target_xcoff_flavour,
  989. rs6000_aix_osabi_sniffer);
  990. gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_AIX,
  991. rs6000_aix_init_osabi);
  992. gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_AIX,
  993. rs6000_aix_init_osabi);
  994. }