rx-tdep.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /* Target-dependent code for the Renesas RX for GDB, the GNU debugger.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, 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 "arch-utils.h"
  17. #include "prologue-value.h"
  18. #include "target.h"
  19. #include "regcache.h"
  20. #include "opcode/rx.h"
  21. #include "dis-asm.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "frame-unwind.h"
  25. #include "frame-base.h"
  26. #include "value.h"
  27. #include "gdbcore.h"
  28. #include "dwarf2/frame.h"
  29. #include "remote.h"
  30. #include "target-descriptions.h"
  31. #include "gdbarch.h"
  32. #include "elf/rx.h"
  33. #include "elf-bfd.h"
  34. #include <algorithm>
  35. #include "features/rx.c"
  36. /* Certain important register numbers. */
  37. enum
  38. {
  39. RX_SP_REGNUM = 0,
  40. RX_R1_REGNUM = 1,
  41. RX_R4_REGNUM = 4,
  42. RX_FP_REGNUM = 6,
  43. RX_R15_REGNUM = 15,
  44. RX_USP_REGNUM = 16,
  45. RX_PSW_REGNUM = 18,
  46. RX_PC_REGNUM = 19,
  47. RX_BPSW_REGNUM = 21,
  48. RX_BPC_REGNUM = 22,
  49. RX_FPSW_REGNUM = 24,
  50. RX_ACC_REGNUM = 25,
  51. RX_NUM_REGS = 26
  52. };
  53. /* RX frame types. */
  54. enum rx_frame_type {
  55. RX_FRAME_TYPE_NORMAL,
  56. RX_FRAME_TYPE_EXCEPTION,
  57. RX_FRAME_TYPE_FAST_INTERRUPT
  58. };
  59. /* Architecture specific data. */
  60. struct rx_gdbarch_tdep : gdbarch_tdep
  61. {
  62. /* The ELF header flags specify the multilib used. */
  63. int elf_flags = 0;
  64. /* Type of PSW and BPSW. */
  65. struct type *rx_psw_type = nullptr;
  66. /* Type of FPSW. */
  67. struct type *rx_fpsw_type = nullptr;
  68. };
  69. /* This structure holds the results of a prologue analysis. */
  70. struct rx_prologue
  71. {
  72. /* Frame type, either a normal frame or one of two types of exception
  73. frames. */
  74. enum rx_frame_type frame_type;
  75. /* The offset from the frame base to the stack pointer --- always
  76. zero or negative.
  77. Calling this a "size" is a bit misleading, but given that the
  78. stack grows downwards, using offsets for everything keeps one
  79. from going completely sign-crazy: you never change anything's
  80. sign for an ADD instruction; always change the second operand's
  81. sign for a SUB instruction; and everything takes care of
  82. itself. */
  83. int frame_size;
  84. /* Non-zero if this function has initialized the frame pointer from
  85. the stack pointer, zero otherwise. */
  86. int has_frame_ptr;
  87. /* If has_frame_ptr is non-zero, this is the offset from the frame
  88. base to where the frame pointer points. This is always zero or
  89. negative. */
  90. int frame_ptr_offset;
  91. /* The address of the first instruction at which the frame has been
  92. set up and the arguments are where the debug info says they are
  93. --- as best as we can tell. */
  94. CORE_ADDR prologue_end;
  95. /* reg_offset[R] is the offset from the CFA at which register R is
  96. saved, or 1 if register R has not been saved. (Real values are
  97. always zero or negative.) */
  98. int reg_offset[RX_NUM_REGS];
  99. };
  100. /* RX register names */
  101. static const char *const rx_register_names[] = {
  102. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  103. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  104. "usp", "isp", "psw", "pc", "intb", "bpsw","bpc","fintv",
  105. "fpsw", "acc",
  106. };
  107. /* Function for finding saved registers in a 'struct pv_area'; this
  108. function is passed to pv_area::scan.
  109. If VALUE is a saved register, ADDR says it was saved at a constant
  110. offset from the frame base, and SIZE indicates that the whole
  111. register was saved, record its offset. */
  112. static void
  113. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  114. {
  115. struct rx_prologue *result = (struct rx_prologue *) result_untyped;
  116. if (value.kind == pvk_register
  117. && value.k == 0
  118. && pv_is_register (addr, RX_SP_REGNUM)
  119. && size == register_size (target_gdbarch (), value.reg))
  120. result->reg_offset[value.reg] = addr.k;
  121. }
  122. /* Define a "handle" struct for fetching the next opcode. */
  123. struct rx_get_opcode_byte_handle
  124. {
  125. CORE_ADDR pc;
  126. };
  127. /* Fetch a byte on behalf of the opcode decoder. HANDLE contains
  128. the memory address of the next byte to fetch. If successful,
  129. the address in the handle is updated and the byte fetched is
  130. returned as the value of the function. If not successful, -1
  131. is returned. */
  132. static int
  133. rx_get_opcode_byte (void *handle)
  134. {
  135. struct rx_get_opcode_byte_handle *opcdata
  136. = (struct rx_get_opcode_byte_handle *) handle;
  137. int status;
  138. gdb_byte byte;
  139. status = target_read_code (opcdata->pc, &byte, 1);
  140. if (status == 0)
  141. {
  142. opcdata->pc += 1;
  143. return byte;
  144. }
  145. else
  146. return -1;
  147. }
  148. /* Analyze a prologue starting at START_PC, going no further than
  149. LIMIT_PC. Fill in RESULT as appropriate. */
  150. static void
  151. rx_analyze_prologue (CORE_ADDR start_pc, CORE_ADDR limit_pc,
  152. enum rx_frame_type frame_type,
  153. struct rx_prologue *result)
  154. {
  155. CORE_ADDR pc, next_pc;
  156. int rn;
  157. pv_t reg[RX_NUM_REGS];
  158. CORE_ADDR after_last_frame_setup_insn = start_pc;
  159. memset (result, 0, sizeof (*result));
  160. result->frame_type = frame_type;
  161. for (rn = 0; rn < RX_NUM_REGS; rn++)
  162. {
  163. reg[rn] = pv_register (rn, 0);
  164. result->reg_offset[rn] = 1;
  165. }
  166. pv_area stack (RX_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
  167. if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
  168. {
  169. /* This code won't do anything useful at present, but this is
  170. what happens for fast interrupts. */
  171. reg[RX_BPSW_REGNUM] = reg[RX_PSW_REGNUM];
  172. reg[RX_BPC_REGNUM] = reg[RX_PC_REGNUM];
  173. }
  174. else
  175. {
  176. /* When an exception occurs, the PSW is saved to the interrupt stack
  177. first. */
  178. if (frame_type == RX_FRAME_TYPE_EXCEPTION)
  179. {
  180. reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  181. stack.store (reg[RX_SP_REGNUM], 4, reg[RX_PSW_REGNUM]);
  182. }
  183. /* The call instruction (or an exception/interrupt) has saved the return
  184. address on the stack. */
  185. reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  186. stack.store (reg[RX_SP_REGNUM], 4, reg[RX_PC_REGNUM]);
  187. }
  188. pc = start_pc;
  189. while (pc < limit_pc)
  190. {
  191. int bytes_read;
  192. struct rx_get_opcode_byte_handle opcode_handle;
  193. RX_Opcode_Decoded opc;
  194. opcode_handle.pc = pc;
  195. bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
  196. &opcode_handle);
  197. next_pc = pc + bytes_read;
  198. if (opc.id == RXO_pushm /* pushm r1, r2 */
  199. && opc.op[1].type == RX_Operand_Register
  200. && opc.op[2].type == RX_Operand_Register)
  201. {
  202. int r1, r2;
  203. int r;
  204. r1 = opc.op[1].reg;
  205. r2 = opc.op[2].reg;
  206. for (r = r2; r >= r1; r--)
  207. {
  208. reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  209. stack.store (reg[RX_SP_REGNUM], 4, reg[r]);
  210. }
  211. after_last_frame_setup_insn = next_pc;
  212. }
  213. else if (opc.id == RXO_mov /* mov.l rdst, rsrc */
  214. && opc.op[0].type == RX_Operand_Register
  215. && opc.op[1].type == RX_Operand_Register
  216. && opc.size == RX_Long)
  217. {
  218. int rdst, rsrc;
  219. rdst = opc.op[0].reg;
  220. rsrc = opc.op[1].reg;
  221. reg[rdst] = reg[rsrc];
  222. if (rdst == RX_FP_REGNUM && rsrc == RX_SP_REGNUM)
  223. after_last_frame_setup_insn = next_pc;
  224. }
  225. else if (opc.id == RXO_mov /* mov.l rsrc, [-SP] */
  226. && opc.op[0].type == RX_Operand_Predec
  227. && opc.op[0].reg == RX_SP_REGNUM
  228. && opc.op[1].type == RX_Operand_Register
  229. && opc.size == RX_Long)
  230. {
  231. int rsrc;
  232. rsrc = opc.op[1].reg;
  233. reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  234. stack.store (reg[RX_SP_REGNUM], 4, reg[rsrc]);
  235. after_last_frame_setup_insn = next_pc;
  236. }
  237. else if (opc.id == RXO_add /* add #const, rsrc, rdst */
  238. && opc.op[0].type == RX_Operand_Register
  239. && opc.op[1].type == RX_Operand_Immediate
  240. && opc.op[2].type == RX_Operand_Register)
  241. {
  242. int rdst = opc.op[0].reg;
  243. int addend = opc.op[1].addend;
  244. int rsrc = opc.op[2].reg;
  245. reg[rdst] = pv_add_constant (reg[rsrc], addend);
  246. /* Negative adjustments to the stack pointer or frame pointer
  247. are (most likely) part of the prologue. */
  248. if ((rdst == RX_SP_REGNUM || rdst == RX_FP_REGNUM) && addend < 0)
  249. after_last_frame_setup_insn = next_pc;
  250. }
  251. else if (opc.id == RXO_mov
  252. && opc.op[0].type == RX_Operand_Indirect
  253. && opc.op[1].type == RX_Operand_Register
  254. && opc.size == RX_Long
  255. && (opc.op[0].reg == RX_SP_REGNUM
  256. || opc.op[0].reg == RX_FP_REGNUM)
  257. && (RX_R1_REGNUM <= opc.op[1].reg
  258. && opc.op[1].reg <= RX_R4_REGNUM))
  259. {
  260. /* This moves an argument register to the stack. Don't
  261. record it, but allow it to be a part of the prologue. */
  262. }
  263. else if (opc.id == RXO_branch
  264. && opc.op[0].type == RX_Operand_Immediate
  265. && next_pc < opc.op[0].addend)
  266. {
  267. /* When a loop appears as the first statement of a function
  268. body, gcc 4.x will use a BRA instruction to branch to the
  269. loop condition checking code. This BRA instruction is
  270. marked as part of the prologue. We therefore set next_pc
  271. to this branch target and also stop the prologue scan.
  272. The instructions at and beyond the branch target should
  273. no longer be associated with the prologue.
  274. Note that we only consider forward branches here. We
  275. presume that a forward branch is being used to skip over
  276. a loop body.
  277. A backwards branch is covered by the default case below.
  278. If we were to encounter a backwards branch, that would
  279. most likely mean that we've scanned through a loop body.
  280. We definitely want to stop the prologue scan when this
  281. happens and that is precisely what is done by the default
  282. case below. */
  283. after_last_frame_setup_insn = opc.op[0].addend;
  284. break; /* Scan no further if we hit this case. */
  285. }
  286. else
  287. {
  288. /* Terminate the prologue scan. */
  289. break;
  290. }
  291. pc = next_pc;
  292. }
  293. /* Is the frame size (offset, really) a known constant? */
  294. if (pv_is_register (reg[RX_SP_REGNUM], RX_SP_REGNUM))
  295. result->frame_size = reg[RX_SP_REGNUM].k;
  296. /* Was the frame pointer initialized? */
  297. if (pv_is_register (reg[RX_FP_REGNUM], RX_SP_REGNUM))
  298. {
  299. result->has_frame_ptr = 1;
  300. result->frame_ptr_offset = reg[RX_FP_REGNUM].k;
  301. }
  302. /* Record where all the registers were saved. */
  303. stack.scan (check_for_saved, (void *) result);
  304. result->prologue_end = after_last_frame_setup_insn;
  305. }
  306. /* Implement the "skip_prologue" gdbarch method. */
  307. static CORE_ADDR
  308. rx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  309. {
  310. const char *name;
  311. CORE_ADDR func_addr, func_end;
  312. struct rx_prologue p;
  313. /* Try to find the extent of the function that contains PC. */
  314. if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  315. return pc;
  316. /* The frame type doesn't matter here, since we only care about
  317. where the prologue ends. We'll use RX_FRAME_TYPE_NORMAL. */
  318. rx_analyze_prologue (pc, func_end, RX_FRAME_TYPE_NORMAL, &p);
  319. return p.prologue_end;
  320. }
  321. /* Given a frame described by THIS_FRAME, decode the prologue of its
  322. associated function if there is not cache entry as specified by
  323. THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
  324. return that struct as the value of this function. */
  325. static struct rx_prologue *
  326. rx_analyze_frame_prologue (struct frame_info *this_frame,
  327. enum rx_frame_type frame_type,
  328. void **this_prologue_cache)
  329. {
  330. if (!*this_prologue_cache)
  331. {
  332. CORE_ADDR func_start, stop_addr;
  333. *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rx_prologue);
  334. func_start = get_frame_func (this_frame);
  335. stop_addr = get_frame_pc (this_frame);
  336. /* If we couldn't find any function containing the PC, then
  337. just initialize the prologue cache, but don't do anything. */
  338. if (!func_start)
  339. stop_addr = func_start;
  340. rx_analyze_prologue (func_start, stop_addr, frame_type,
  341. (struct rx_prologue *) *this_prologue_cache);
  342. }
  343. return (struct rx_prologue *) *this_prologue_cache;
  344. }
  345. /* Determine type of frame by scanning the function for a return
  346. instruction. */
  347. static enum rx_frame_type
  348. rx_frame_type (struct frame_info *this_frame, void **this_cache)
  349. {
  350. const char *name;
  351. CORE_ADDR pc, start_pc, lim_pc;
  352. int bytes_read;
  353. struct rx_get_opcode_byte_handle opcode_handle;
  354. RX_Opcode_Decoded opc;
  355. gdb_assert (this_cache != NULL);
  356. /* If we have a cached value, return it. */
  357. if (*this_cache != NULL)
  358. {
  359. struct rx_prologue *p = (struct rx_prologue *) *this_cache;
  360. return p->frame_type;
  361. }
  362. /* No cached value; scan the function. The frame type is cached in
  363. rx_analyze_prologue / rx_analyze_frame_prologue. */
  364. pc = get_frame_pc (this_frame);
  365. /* Attempt to find the last address in the function. If it cannot
  366. be determined, set the limit to be a short ways past the frame's
  367. pc. */
  368. if (!find_pc_partial_function (pc, &name, &start_pc, &lim_pc))
  369. lim_pc = pc + 20;
  370. while (pc < lim_pc)
  371. {
  372. opcode_handle.pc = pc;
  373. bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
  374. &opcode_handle);
  375. if (bytes_read <= 0 || opc.id == RXO_rts)
  376. return RX_FRAME_TYPE_NORMAL;
  377. else if (opc.id == RXO_rtfi)
  378. return RX_FRAME_TYPE_FAST_INTERRUPT;
  379. else if (opc.id == RXO_rte)
  380. return RX_FRAME_TYPE_EXCEPTION;
  381. pc += bytes_read;
  382. }
  383. return RX_FRAME_TYPE_NORMAL;
  384. }
  385. /* Given the next frame and a prologue cache, return this frame's
  386. base. */
  387. static CORE_ADDR
  388. rx_frame_base (struct frame_info *this_frame, void **this_cache)
  389. {
  390. enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
  391. struct rx_prologue *p
  392. = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
  393. /* In functions that use alloca, the distance between the stack
  394. pointer and the frame base varies dynamically, so we can't use
  395. the SP plus static information like prologue analysis to find the
  396. frame base. However, such functions must have a frame pointer,
  397. to be able to restore the SP on exit. So whenever we do have a
  398. frame pointer, use that to find the base. */
  399. if (p->has_frame_ptr)
  400. {
  401. CORE_ADDR fp = get_frame_register_unsigned (this_frame, RX_FP_REGNUM);
  402. return fp - p->frame_ptr_offset;
  403. }
  404. else
  405. {
  406. CORE_ADDR sp = get_frame_register_unsigned (this_frame, RX_SP_REGNUM);
  407. return sp - p->frame_size;
  408. }
  409. }
  410. /* Implement the "frame_this_id" method for unwinding frames. */
  411. static void
  412. rx_frame_this_id (struct frame_info *this_frame, void **this_cache,
  413. struct frame_id *this_id)
  414. {
  415. *this_id = frame_id_build (rx_frame_base (this_frame, this_cache),
  416. get_frame_func (this_frame));
  417. }
  418. /* Implement the "frame_prev_register" method for unwinding frames. */
  419. static struct value *
  420. rx_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  421. int regnum)
  422. {
  423. enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
  424. struct rx_prologue *p
  425. = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
  426. CORE_ADDR frame_base = rx_frame_base (this_frame, this_cache);
  427. if (regnum == RX_SP_REGNUM)
  428. {
  429. if (frame_type == RX_FRAME_TYPE_EXCEPTION)
  430. {
  431. struct value *psw_val;
  432. CORE_ADDR psw;
  433. psw_val = rx_frame_prev_register (this_frame, this_cache,
  434. RX_PSW_REGNUM);
  435. psw = extract_unsigned_integer
  436. (value_contents_all (psw_val).data (), 4,
  437. gdbarch_byte_order (get_frame_arch (this_frame)));
  438. if ((psw & 0x20000 /* U bit */) != 0)
  439. return rx_frame_prev_register (this_frame, this_cache,
  440. RX_USP_REGNUM);
  441. /* Fall through for the case where U bit is zero. */
  442. }
  443. return frame_unwind_got_constant (this_frame, regnum, frame_base);
  444. }
  445. if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
  446. {
  447. if (regnum == RX_PC_REGNUM)
  448. return rx_frame_prev_register (this_frame, this_cache,
  449. RX_BPC_REGNUM);
  450. if (regnum == RX_PSW_REGNUM)
  451. return rx_frame_prev_register (this_frame, this_cache,
  452. RX_BPSW_REGNUM);
  453. }
  454. /* If prologue analysis says we saved this register somewhere,
  455. return a description of the stack slot holding it. */
  456. if (p->reg_offset[regnum] != 1)
  457. return frame_unwind_got_memory (this_frame, regnum,
  458. frame_base + p->reg_offset[regnum]);
  459. /* Otherwise, presume we haven't changed the value of this
  460. register, and get it from the next frame. */
  461. return frame_unwind_got_register (this_frame, regnum, regnum);
  462. }
  463. /* Return TRUE if the frame indicated by FRAME_TYPE is a normal frame. */
  464. static int
  465. normal_frame_p (enum rx_frame_type frame_type)
  466. {
  467. return (frame_type == RX_FRAME_TYPE_NORMAL);
  468. }
  469. /* Return TRUE if the frame indicated by FRAME_TYPE is an exception
  470. frame. */
  471. static int
  472. exception_frame_p (enum rx_frame_type frame_type)
  473. {
  474. return (frame_type == RX_FRAME_TYPE_EXCEPTION
  475. || frame_type == RX_FRAME_TYPE_FAST_INTERRUPT);
  476. }
  477. /* Common code used by both normal and exception frame sniffers. */
  478. static int
  479. rx_frame_sniffer_common (const struct frame_unwind *self,
  480. struct frame_info *this_frame,
  481. void **this_cache,
  482. int (*sniff_p)(enum rx_frame_type) )
  483. {
  484. gdb_assert (this_cache != NULL);
  485. if (*this_cache == NULL)
  486. {
  487. enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
  488. if (sniff_p (frame_type))
  489. {
  490. /* The call below will fill in the cache, including the frame
  491. type. */
  492. (void) rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
  493. return 1;
  494. }
  495. else
  496. return 0;
  497. }
  498. else
  499. {
  500. struct rx_prologue *p = (struct rx_prologue *) *this_cache;
  501. return sniff_p (p->frame_type);
  502. }
  503. }
  504. /* Frame sniffer for normal (non-exception) frames. */
  505. static int
  506. rx_frame_sniffer (const struct frame_unwind *self,
  507. struct frame_info *this_frame,
  508. void **this_cache)
  509. {
  510. return rx_frame_sniffer_common (self, this_frame, this_cache,
  511. normal_frame_p);
  512. }
  513. /* Frame sniffer for exception frames. */
  514. static int
  515. rx_exception_sniffer (const struct frame_unwind *self,
  516. struct frame_info *this_frame,
  517. void **this_cache)
  518. {
  519. return rx_frame_sniffer_common (self, this_frame, this_cache,
  520. exception_frame_p);
  521. }
  522. /* Data structure for normal code using instruction-based prologue
  523. analyzer. */
  524. static const struct frame_unwind rx_frame_unwind = {
  525. "rx prologue",
  526. NORMAL_FRAME,
  527. default_frame_unwind_stop_reason,
  528. rx_frame_this_id,
  529. rx_frame_prev_register,
  530. NULL,
  531. rx_frame_sniffer
  532. };
  533. /* Data structure for exception code using instruction-based prologue
  534. analyzer. */
  535. static const struct frame_unwind rx_exception_unwind = {
  536. "rx exception",
  537. /* SIGTRAMP_FRAME could be used here, but backtraces are less informative. */
  538. NORMAL_FRAME,
  539. default_frame_unwind_stop_reason,
  540. rx_frame_this_id,
  541. rx_frame_prev_register,
  542. NULL,
  543. rx_exception_sniffer
  544. };
  545. /* Implement the "push_dummy_call" gdbarch method. */
  546. static CORE_ADDR
  547. rx_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  548. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  549. struct value **args, CORE_ADDR sp,
  550. function_call_return_method return_method,
  551. CORE_ADDR struct_addr)
  552. {
  553. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  554. int write_pass;
  555. int sp_off = 0;
  556. CORE_ADDR cfa;
  557. int num_register_candidate_args;
  558. struct type *func_type = value_type (function);
  559. /* Dereference function pointer types. */
  560. while (func_type->code () == TYPE_CODE_PTR)
  561. func_type = TYPE_TARGET_TYPE (func_type);
  562. /* The end result had better be a function or a method. */
  563. gdb_assert (func_type->code () == TYPE_CODE_FUNC
  564. || func_type->code () == TYPE_CODE_METHOD);
  565. /* Functions with a variable number of arguments have all of their
  566. variable arguments and the last non-variable argument passed
  567. on the stack.
  568. Otherwise, we can pass up to four arguments on the stack.
  569. Once computed, we leave this value alone. I.e. we don't update
  570. it in case of a struct return going in a register or an argument
  571. requiring multiple registers, etc. We rely instead on the value
  572. of the ``arg_reg'' variable to get these other details correct. */
  573. if (func_type->has_varargs ())
  574. num_register_candidate_args = func_type->num_fields () - 1;
  575. else
  576. num_register_candidate_args = 4;
  577. /* We make two passes; the first does the stack allocation,
  578. the second actually stores the arguments. */
  579. for (write_pass = 0; write_pass <= 1; write_pass++)
  580. {
  581. int i;
  582. int arg_reg = RX_R1_REGNUM;
  583. if (write_pass)
  584. sp = align_down (sp - sp_off, 4);
  585. sp_off = 0;
  586. if (return_method == return_method_struct)
  587. {
  588. struct type *return_type = TYPE_TARGET_TYPE (func_type);
  589. gdb_assert (return_type->code () == TYPE_CODE_STRUCT
  590. || func_type->code () == TYPE_CODE_UNION);
  591. if (TYPE_LENGTH (return_type) > 16
  592. || TYPE_LENGTH (return_type) % 4 != 0)
  593. {
  594. if (write_pass)
  595. regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
  596. struct_addr);
  597. }
  598. }
  599. /* Push the arguments. */
  600. for (i = 0; i < nargs; i++)
  601. {
  602. struct value *arg = args[i];
  603. const gdb_byte *arg_bits = value_contents_all (arg).data ();
  604. struct type *arg_type = check_typedef (value_type (arg));
  605. ULONGEST arg_size = TYPE_LENGTH (arg_type);
  606. if (i == 0 && struct_addr != 0
  607. && return_method != return_method_struct
  608. && arg_type->code () == TYPE_CODE_PTR
  609. && extract_unsigned_integer (arg_bits, 4,
  610. byte_order) == struct_addr)
  611. {
  612. /* This argument represents the address at which C++ (and
  613. possibly other languages) store their return value.
  614. Put this value in R15. */
  615. if (write_pass)
  616. regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
  617. struct_addr);
  618. }
  619. else if (arg_type->code () != TYPE_CODE_STRUCT
  620. && arg_type->code () != TYPE_CODE_UNION
  621. && arg_size <= 8)
  622. {
  623. /* Argument is a scalar. */
  624. if (arg_size == 8)
  625. {
  626. if (i < num_register_candidate_args
  627. && arg_reg <= RX_R4_REGNUM - 1)
  628. {
  629. /* If argument registers are going to be used to pass
  630. an 8 byte scalar, the ABI specifies that two registers
  631. must be available. */
  632. if (write_pass)
  633. {
  634. regcache_cooked_write_unsigned (regcache, arg_reg,
  635. extract_unsigned_integer
  636. (arg_bits, 4,
  637. byte_order));
  638. regcache_cooked_write_unsigned (regcache,
  639. arg_reg + 1,
  640. extract_unsigned_integer
  641. (arg_bits + 4, 4,
  642. byte_order));
  643. }
  644. arg_reg += 2;
  645. }
  646. else
  647. {
  648. sp_off = align_up (sp_off, 4);
  649. /* Otherwise, pass the 8 byte scalar on the stack. */
  650. if (write_pass)
  651. write_memory (sp + sp_off, arg_bits, 8);
  652. sp_off += 8;
  653. }
  654. }
  655. else
  656. {
  657. ULONGEST u;
  658. gdb_assert (arg_size <= 4);
  659. u =
  660. extract_unsigned_integer (arg_bits, arg_size, byte_order);
  661. if (i < num_register_candidate_args
  662. && arg_reg <= RX_R4_REGNUM)
  663. {
  664. if (write_pass)
  665. regcache_cooked_write_unsigned (regcache, arg_reg, u);
  666. arg_reg += 1;
  667. }
  668. else
  669. {
  670. int p_arg_size = 4;
  671. if (func_type->is_prototyped ()
  672. && i < func_type->num_fields ())
  673. {
  674. struct type *p_arg_type =
  675. func_type->field (i).type ();
  676. p_arg_size = TYPE_LENGTH (p_arg_type);
  677. }
  678. sp_off = align_up (sp_off, p_arg_size);
  679. if (write_pass)
  680. write_memory_unsigned_integer (sp + sp_off,
  681. p_arg_size, byte_order,
  682. u);
  683. sp_off += p_arg_size;
  684. }
  685. }
  686. }
  687. else
  688. {
  689. /* Argument is a struct or union. Pass as much of the struct
  690. in registers, if possible. Pass the rest on the stack. */
  691. while (arg_size > 0)
  692. {
  693. if (i < num_register_candidate_args
  694. && arg_reg <= RX_R4_REGNUM
  695. && arg_size <= 4 * (RX_R4_REGNUM - arg_reg + 1)
  696. && arg_size % 4 == 0)
  697. {
  698. int len = std::min (arg_size, (ULONGEST) 4);
  699. if (write_pass)
  700. regcache_cooked_write_unsigned (regcache, arg_reg,
  701. extract_unsigned_integer
  702. (arg_bits, len,
  703. byte_order));
  704. arg_bits += len;
  705. arg_size -= len;
  706. arg_reg++;
  707. }
  708. else
  709. {
  710. sp_off = align_up (sp_off, 4);
  711. if (write_pass)
  712. write_memory (sp + sp_off, arg_bits, arg_size);
  713. sp_off += align_up (arg_size, 4);
  714. arg_size = 0;
  715. }
  716. }
  717. }
  718. }
  719. }
  720. /* Keep track of the stack address prior to pushing the return address.
  721. This is the value that we'll return. */
  722. cfa = sp;
  723. /* Push the return address. */
  724. sp = sp - 4;
  725. write_memory_unsigned_integer (sp, 4, byte_order, bp_addr);
  726. /* Update the stack pointer. */
  727. regcache_cooked_write_unsigned (regcache, RX_SP_REGNUM, sp);
  728. return cfa;
  729. }
  730. /* Implement the "return_value" gdbarch method. */
  731. static enum return_value_convention
  732. rx_return_value (struct gdbarch *gdbarch,
  733. struct value *function,
  734. struct type *valtype,
  735. struct regcache *regcache,
  736. gdb_byte *readbuf, const gdb_byte *writebuf)
  737. {
  738. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  739. ULONGEST valtype_len = TYPE_LENGTH (valtype);
  740. if (TYPE_LENGTH (valtype) > 16
  741. || ((valtype->code () == TYPE_CODE_STRUCT
  742. || valtype->code () == TYPE_CODE_UNION)
  743. && TYPE_LENGTH (valtype) % 4 != 0))
  744. return RETURN_VALUE_STRUCT_CONVENTION;
  745. if (readbuf)
  746. {
  747. ULONGEST u;
  748. int argreg = RX_R1_REGNUM;
  749. int offset = 0;
  750. while (valtype_len > 0)
  751. {
  752. int len = std::min (valtype_len, (ULONGEST) 4);
  753. regcache_cooked_read_unsigned (regcache, argreg, &u);
  754. store_unsigned_integer (readbuf + offset, len, byte_order, u);
  755. valtype_len -= len;
  756. offset += len;
  757. argreg++;
  758. }
  759. }
  760. if (writebuf)
  761. {
  762. ULONGEST u;
  763. int argreg = RX_R1_REGNUM;
  764. int offset = 0;
  765. while (valtype_len > 0)
  766. {
  767. int len = std::min (valtype_len, (ULONGEST) 4);
  768. u = extract_unsigned_integer (writebuf + offset, len, byte_order);
  769. regcache_cooked_write_unsigned (regcache, argreg, u);
  770. valtype_len -= len;
  771. offset += len;
  772. argreg++;
  773. }
  774. }
  775. return RETURN_VALUE_REGISTER_CONVENTION;
  776. }
  777. constexpr gdb_byte rx_break_insn[] = { 0x00 };
  778. typedef BP_MANIPULATION (rx_break_insn) rx_breakpoint;
  779. /* Implement the dwarf_reg_to_regnum" gdbarch method. */
  780. static int
  781. rx_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  782. {
  783. if (0 <= reg && reg <= 15)
  784. return reg;
  785. else if (reg == 16)
  786. return RX_PSW_REGNUM;
  787. else if (reg == 17)
  788. return RX_PC_REGNUM;
  789. else
  790. return -1;
  791. }
  792. /* Allocate and initialize a gdbarch object. */
  793. static struct gdbarch *
  794. rx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  795. {
  796. struct gdbarch *gdbarch;
  797. int elf_flags;
  798. tdesc_arch_data_up tdesc_data;
  799. const struct target_desc *tdesc = info.target_desc;
  800. /* Extract the elf_flags if available. */
  801. if (info.abfd != NULL
  802. && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  803. elf_flags = elf_elfheader (info.abfd)->e_flags;
  804. else
  805. elf_flags = 0;
  806. /* Try to find the architecture in the list of already defined
  807. architectures. */
  808. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  809. arches != NULL;
  810. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  811. {
  812. rx_gdbarch_tdep *tdep
  813. = (rx_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  814. if (tdep->elf_flags != elf_flags)
  815. continue;
  816. return arches->gdbarch;
  817. }
  818. if (tdesc == NULL)
  819. tdesc = tdesc_rx;
  820. /* Check any target description for validity. */
  821. if (tdesc_has_registers (tdesc))
  822. {
  823. const struct tdesc_feature *feature;
  824. bool valid_p = true;
  825. feature = tdesc_find_feature (tdesc, "org.gnu.gdb.rx.core");
  826. if (feature != NULL)
  827. {
  828. tdesc_data = tdesc_data_alloc ();
  829. for (int i = 0; i < RX_NUM_REGS; i++)
  830. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  831. rx_register_names[i]);
  832. }
  833. if (!valid_p)
  834. return NULL;
  835. }
  836. gdb_assert(tdesc_data != NULL);
  837. rx_gdbarch_tdep *tdep = new rx_gdbarch_tdep;
  838. gdbarch = gdbarch_alloc (&info, tdep);
  839. tdep->elf_flags = elf_flags;
  840. set_gdbarch_num_regs (gdbarch, RX_NUM_REGS);
  841. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  842. set_gdbarch_num_pseudo_regs (gdbarch, 0);
  843. set_gdbarch_pc_regnum (gdbarch, RX_PC_REGNUM);
  844. set_gdbarch_sp_regnum (gdbarch, RX_SP_REGNUM);
  845. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  846. set_gdbarch_decr_pc_after_break (gdbarch, 1);
  847. set_gdbarch_breakpoint_kind_from_pc (gdbarch, rx_breakpoint::kind_from_pc);
  848. set_gdbarch_sw_breakpoint_from_kind (gdbarch, rx_breakpoint::bp_from_kind);
  849. set_gdbarch_skip_prologue (gdbarch, rx_skip_prologue);
  850. /* Target builtin data types. */
  851. set_gdbarch_char_signed (gdbarch, 0);
  852. set_gdbarch_short_bit (gdbarch, 16);
  853. set_gdbarch_int_bit (gdbarch, 32);
  854. set_gdbarch_long_bit (gdbarch, 32);
  855. set_gdbarch_long_long_bit (gdbarch, 64);
  856. set_gdbarch_ptr_bit (gdbarch, 32);
  857. set_gdbarch_float_bit (gdbarch, 32);
  858. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  859. if (elf_flags & E_FLAG_RX_64BIT_DOUBLES)
  860. {
  861. set_gdbarch_double_bit (gdbarch, 64);
  862. set_gdbarch_long_double_bit (gdbarch, 64);
  863. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  864. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  865. }
  866. else
  867. {
  868. set_gdbarch_double_bit (gdbarch, 32);
  869. set_gdbarch_long_double_bit (gdbarch, 32);
  870. set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  871. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
  872. }
  873. /* DWARF register mapping. */
  874. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rx_dwarf_reg_to_regnum);
  875. /* Frame unwinding. */
  876. frame_unwind_append_unwinder (gdbarch, &rx_exception_unwind);
  877. dwarf2_append_unwinders (gdbarch);
  878. frame_unwind_append_unwinder (gdbarch, &rx_frame_unwind);
  879. /* Methods setting up a dummy call, and extracting the return value from
  880. a call. */
  881. set_gdbarch_push_dummy_call (gdbarch, rx_push_dummy_call);
  882. set_gdbarch_return_value (gdbarch, rx_return_value);
  883. /* Virtual tables. */
  884. set_gdbarch_vbit_in_delta (gdbarch, 1);
  885. return gdbarch;
  886. }
  887. /* Register the above initialization routine. */
  888. void _initialize_rx_tdep ();
  889. void
  890. _initialize_rx_tdep ()
  891. {
  892. register_gdbarch_init (bfd_arch_rx, rx_gdbarch_init);
  893. initialize_tdesc_rx ();
  894. }