arm-get-next-pcs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* Common code for ARM software single stepping support.
  2. Copyright (C) 1988-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 "gdbsupport/common-defs.h"
  15. #include "gdbsupport/gdb_vecs.h"
  16. #include "gdbsupport/common-regcache.h"
  17. #include "arm.h"
  18. #include "arm-get-next-pcs.h"
  19. #include "count-one-bits.h"
  20. /* See arm-get-next-pcs.h. */
  21. void
  22. arm_get_next_pcs_ctor (struct arm_get_next_pcs *self,
  23. struct arm_get_next_pcs_ops *ops,
  24. int byte_order,
  25. int byte_order_for_code,
  26. int has_thumb2_breakpoint,
  27. struct regcache *regcache)
  28. {
  29. self->ops = ops;
  30. self->byte_order = byte_order;
  31. self->byte_order_for_code = byte_order_for_code;
  32. self->has_thumb2_breakpoint = has_thumb2_breakpoint;
  33. self->regcache = regcache;
  34. }
  35. /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
  36. instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
  37. is found, attempt to step through it. The end of the sequence address is
  38. added to the next_pcs list. */
  39. static std::vector<CORE_ADDR>
  40. thumb_deal_with_atomic_sequence_raw (struct arm_get_next_pcs *self)
  41. {
  42. int byte_order_for_code = self->byte_order_for_code;
  43. CORE_ADDR breaks[2] = {CORE_ADDR_MAX, CORE_ADDR_MAX};
  44. CORE_ADDR pc = regcache_read_pc (self->regcache);
  45. CORE_ADDR loc = pc;
  46. unsigned short insn1, insn2;
  47. int insn_count;
  48. int index;
  49. int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
  50. const int atomic_sequence_length = 16; /* Instruction sequence length. */
  51. ULONGEST status, itstate;
  52. /* We currently do not support atomic sequences within an IT block. */
  53. status = regcache_raw_get_unsigned (self->regcache, ARM_PS_REGNUM);
  54. itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
  55. if (itstate & 0x0f)
  56. return {};
  57. /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction. */
  58. insn1 = self->ops->read_mem_uint (loc, 2, byte_order_for_code);
  59. loc += 2;
  60. if (thumb_insn_size (insn1) != 4)
  61. return {};
  62. insn2 = self->ops->read_mem_uint (loc, 2, byte_order_for_code);
  63. loc += 2;
  64. if (!((insn1 & 0xfff0) == 0xe850
  65. || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
  66. return {};
  67. /* Assume that no atomic sequence is longer than "atomic_sequence_length"
  68. instructions. */
  69. for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
  70. {
  71. insn1 = self->ops->read_mem_uint (loc, 2,byte_order_for_code);
  72. loc += 2;
  73. if (thumb_insn_size (insn1) != 4)
  74. {
  75. /* Assume that there is at most one conditional branch in the
  76. atomic sequence. If a conditional branch is found, put a
  77. breakpoint in its destination address. */
  78. if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
  79. {
  80. if (last_breakpoint > 0)
  81. return {}; /* More than one conditional branch found,
  82. fallback to the standard code. */
  83. breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
  84. last_breakpoint++;
  85. }
  86. /* We do not support atomic sequences that use any *other*
  87. instructions but conditional branches to change the PC.
  88. Fall back to standard code to avoid losing control of
  89. execution. */
  90. else if (thumb_instruction_changes_pc (insn1))
  91. return {};
  92. }
  93. else
  94. {
  95. insn2 = self->ops->read_mem_uint (loc, 2, byte_order_for_code);
  96. loc += 2;
  97. /* Assume that there is at most one conditional branch in the
  98. atomic sequence. If a conditional branch is found, put a
  99. breakpoint in its destination address. */
  100. if ((insn1 & 0xf800) == 0xf000
  101. && (insn2 & 0xd000) == 0x8000
  102. && (insn1 & 0x0380) != 0x0380)
  103. {
  104. int sign, j1, j2, imm1, imm2;
  105. unsigned int offset;
  106. sign = sbits (insn1, 10, 10);
  107. imm1 = bits (insn1, 0, 5);
  108. imm2 = bits (insn2, 0, 10);
  109. j1 = bit (insn2, 13);
  110. j2 = bit (insn2, 11);
  111. offset = (sign << 20) + (j2 << 19) + (j1 << 18);
  112. offset += (imm1 << 12) + (imm2 << 1);
  113. if (last_breakpoint > 0)
  114. return {}; /* More than one conditional branch found,
  115. fallback to the standard code. */
  116. breaks[1] = loc + offset;
  117. last_breakpoint++;
  118. }
  119. /* We do not support atomic sequences that use any *other*
  120. instructions but conditional branches to change the PC.
  121. Fall back to standard code to avoid losing control of
  122. execution. */
  123. else if (thumb2_instruction_changes_pc (insn1, insn2))
  124. return {};
  125. /* If we find a strex{,b,h,d}, we're done. */
  126. if ((insn1 & 0xfff0) == 0xe840
  127. || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
  128. break;
  129. }
  130. }
  131. /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
  132. if (insn_count == atomic_sequence_length)
  133. return {};
  134. /* Insert a breakpoint right after the end of the atomic sequence. */
  135. breaks[0] = loc;
  136. /* Check for duplicated breakpoints. Check also for a breakpoint
  137. placed (branch instruction's destination) anywhere in sequence. */
  138. if (last_breakpoint
  139. && (breaks[1] == breaks[0]
  140. || (breaks[1] >= pc && breaks[1] < loc)))
  141. last_breakpoint = 0;
  142. std::vector<CORE_ADDR> next_pcs;
  143. /* Adds the breakpoints to the list to be inserted. */
  144. for (index = 0; index <= last_breakpoint; index++)
  145. next_pcs.push_back (MAKE_THUMB_ADDR (breaks[index]));
  146. return next_pcs;
  147. }
  148. /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
  149. instruction and ending with a STREX{,B,H,D} instruction. If such a sequence
  150. is found, attempt to step through it. The end of the sequence address is
  151. added to the next_pcs list. */
  152. static std::vector<CORE_ADDR>
  153. arm_deal_with_atomic_sequence_raw (struct arm_get_next_pcs *self)
  154. {
  155. int byte_order_for_code = self->byte_order_for_code;
  156. CORE_ADDR breaks[2] = {CORE_ADDR_MAX, CORE_ADDR_MAX};
  157. CORE_ADDR pc = regcache_read_pc (self->regcache);
  158. CORE_ADDR loc = pc;
  159. unsigned int insn;
  160. int insn_count;
  161. int index;
  162. int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
  163. const int atomic_sequence_length = 16; /* Instruction sequence length. */
  164. /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
  165. Note that we do not currently support conditionally executed atomic
  166. instructions. */
  167. insn = self->ops->read_mem_uint (loc, 4, byte_order_for_code);
  168. loc += 4;
  169. if ((insn & 0xff9000f0) != 0xe1900090)
  170. return {};
  171. /* Assume that no atomic sequence is longer than "atomic_sequence_length"
  172. instructions. */
  173. for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
  174. {
  175. insn = self->ops->read_mem_uint (loc, 4, byte_order_for_code);
  176. loc += 4;
  177. /* Assume that there is at most one conditional branch in the atomic
  178. sequence. If a conditional branch is found, put a breakpoint in
  179. its destination address. */
  180. if (bits (insn, 24, 27) == 0xa)
  181. {
  182. if (last_breakpoint > 0)
  183. return {}; /* More than one conditional branch found, fallback
  184. to the standard single-step code. */
  185. breaks[1] = BranchDest (loc - 4, insn);
  186. last_breakpoint++;
  187. }
  188. /* We do not support atomic sequences that use any *other* instructions
  189. but conditional branches to change the PC. Fall back to standard
  190. code to avoid losing control of execution. */
  191. else if (arm_instruction_changes_pc (insn))
  192. return {};
  193. /* If we find a strex{,b,h,d}, we're done. */
  194. if ((insn & 0xff9000f0) == 0xe1800090)
  195. break;
  196. }
  197. /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence. */
  198. if (insn_count == atomic_sequence_length)
  199. return {};
  200. /* Insert a breakpoint right after the end of the atomic sequence. */
  201. breaks[0] = loc;
  202. /* Check for duplicated breakpoints. Check also for a breakpoint
  203. placed (branch instruction's destination) anywhere in sequence. */
  204. if (last_breakpoint
  205. && (breaks[1] == breaks[0]
  206. || (breaks[1] >= pc && breaks[1] < loc)))
  207. last_breakpoint = 0;
  208. std::vector<CORE_ADDR> next_pcs;
  209. /* Adds the breakpoints to the list to be inserted. */
  210. for (index = 0; index <= last_breakpoint; index++)
  211. next_pcs.push_back (breaks[index]);
  212. return next_pcs;
  213. }
  214. /* Find the next possible PCs for thumb mode. */
  215. static std::vector<CORE_ADDR>
  216. thumb_get_next_pcs_raw (struct arm_get_next_pcs *self)
  217. {
  218. int byte_order = self->byte_order;
  219. int byte_order_for_code = self->byte_order_for_code;
  220. CORE_ADDR pc = regcache_read_pc (self->regcache);
  221. unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
  222. unsigned short inst1;
  223. CORE_ADDR nextpc = pc + 2; /* Default is next instruction. */
  224. ULONGEST status, itstate;
  225. struct regcache *regcache = self->regcache;
  226. std::vector<CORE_ADDR> next_pcs;
  227. nextpc = MAKE_THUMB_ADDR (nextpc);
  228. pc_val = MAKE_THUMB_ADDR (pc_val);
  229. inst1 = self->ops->read_mem_uint (pc, 2, byte_order_for_code);
  230. /* Thumb-2 conditional execution support. There are eight bits in
  231. the CPSR which describe conditional execution state. Once
  232. reconstructed (they're in a funny order), the low five bits
  233. describe the low bit of the condition for each instruction and
  234. how many instructions remain. The high three bits describe the
  235. base condition. One of the low four bits will be set if an IT
  236. block is active. These bits read as zero on earlier
  237. processors. */
  238. status = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
  239. itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
  240. /* If-Then handling. On GNU/Linux, where this routine is used, we
  241. use an undefined instruction as a breakpoint. Unlike BKPT, IT
  242. can disable execution of the undefined instruction. So we might
  243. miss the breakpoint if we set it on a skipped conditional
  244. instruction. Because conditional instructions can change the
  245. flags, affecting the execution of further instructions, we may
  246. need to set two breakpoints. */
  247. if (self->has_thumb2_breakpoint)
  248. {
  249. if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
  250. {
  251. /* An IT instruction. Because this instruction does not
  252. modify the flags, we can accurately predict the next
  253. executed instruction. */
  254. itstate = inst1 & 0x00ff;
  255. pc += thumb_insn_size (inst1);
  256. while (itstate != 0 && ! condition_true (itstate >> 4, status))
  257. {
  258. inst1 = self->ops->read_mem_uint (pc, 2,byte_order_for_code);
  259. pc += thumb_insn_size (inst1);
  260. itstate = thumb_advance_itstate (itstate);
  261. }
  262. next_pcs.push_back (MAKE_THUMB_ADDR (pc));
  263. return next_pcs;
  264. }
  265. else if (itstate != 0)
  266. {
  267. /* We are in a conditional block. Check the condition. */
  268. if (! condition_true (itstate >> 4, status))
  269. {
  270. /* Advance to the next executed instruction. */
  271. pc += thumb_insn_size (inst1);
  272. itstate = thumb_advance_itstate (itstate);
  273. while (itstate != 0 && ! condition_true (itstate >> 4, status))
  274. {
  275. inst1 = self->ops->read_mem_uint (pc, 2, byte_order_for_code);
  276. pc += thumb_insn_size (inst1);
  277. itstate = thumb_advance_itstate (itstate);
  278. }
  279. next_pcs.push_back (MAKE_THUMB_ADDR (pc));
  280. return next_pcs;
  281. }
  282. else if ((itstate & 0x0f) == 0x08)
  283. {
  284. /* This is the last instruction of the conditional
  285. block, and it is executed. We can handle it normally
  286. because the following instruction is not conditional,
  287. and we must handle it normally because it is
  288. permitted to branch. Fall through. */
  289. }
  290. else
  291. {
  292. int cond_negated;
  293. /* There are conditional instructions after this one.
  294. If this instruction modifies the flags, then we can
  295. not predict what the next executed instruction will
  296. be. Fortunately, this instruction is architecturally
  297. forbidden to branch; we know it will fall through.
  298. Start by skipping past it. */
  299. pc += thumb_insn_size (inst1);
  300. itstate = thumb_advance_itstate (itstate);
  301. /* Set a breakpoint on the following instruction. */
  302. gdb_assert ((itstate & 0x0f) != 0);
  303. next_pcs.push_back (MAKE_THUMB_ADDR (pc));
  304. cond_negated = (itstate >> 4) & 1;
  305. /* Skip all following instructions with the same
  306. condition. If there is a later instruction in the IT
  307. block with the opposite condition, set the other
  308. breakpoint there. If not, then set a breakpoint on
  309. the instruction after the IT block. */
  310. do
  311. {
  312. inst1 = self->ops->read_mem_uint (pc, 2, byte_order_for_code);
  313. pc += thumb_insn_size (inst1);
  314. itstate = thumb_advance_itstate (itstate);
  315. }
  316. while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
  317. next_pcs.push_back (MAKE_THUMB_ADDR (pc));
  318. return next_pcs;
  319. }
  320. }
  321. }
  322. else if (itstate & 0x0f)
  323. {
  324. /* We are in a conditional block. Check the condition. */
  325. int cond = itstate >> 4;
  326. if (! condition_true (cond, status))
  327. {
  328. /* Advance to the next instruction. All the 32-bit
  329. instructions share a common prefix. */
  330. next_pcs.push_back (MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1)));
  331. }
  332. return next_pcs;
  333. /* Otherwise, handle the instruction normally. */
  334. }
  335. if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
  336. {
  337. CORE_ADDR sp;
  338. /* Fetch the saved PC from the stack. It's stored above
  339. all of the other registers. */
  340. unsigned long offset
  341. = count_one_bits (bits (inst1, 0, 7)) * ARM_INT_REGISTER_SIZE;
  342. sp = regcache_raw_get_unsigned (regcache, ARM_SP_REGNUM);
  343. nextpc = self->ops->read_mem_uint (sp + offset, 4, byte_order);
  344. }
  345. else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
  346. {
  347. unsigned long cond = bits (inst1, 8, 11);
  348. if (cond == 0x0f) /* 0x0f = SWI */
  349. {
  350. nextpc = self->ops->syscall_next_pc (self);
  351. }
  352. else if (cond != 0x0f && condition_true (cond, status))
  353. nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
  354. }
  355. else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
  356. {
  357. nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
  358. }
  359. else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
  360. {
  361. unsigned short inst2;
  362. inst2 = self->ops->read_mem_uint (pc + 2, 2, byte_order_for_code);
  363. /* Default to the next instruction. */
  364. nextpc = pc + 4;
  365. nextpc = MAKE_THUMB_ADDR (nextpc);
  366. if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
  367. {
  368. /* Branches and miscellaneous control instructions. */
  369. if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
  370. {
  371. /* B, BL, BLX. */
  372. int j1, j2, imm1, imm2;
  373. imm1 = sbits (inst1, 0, 10);
  374. imm2 = bits (inst2, 0, 10);
  375. j1 = bit (inst2, 13);
  376. j2 = bit (inst2, 11);
  377. unsigned long offset = ((imm1 << 12) + (imm2 << 1));
  378. offset ^= ((!j2) << 22) | ((!j1) << 23);
  379. nextpc = pc_val + offset;
  380. /* For BLX make sure to clear the low bits. */
  381. if (bit (inst2, 12) == 0)
  382. nextpc = nextpc & 0xfffffffc;
  383. }
  384. else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
  385. {
  386. /* SUBS PC, LR, #imm8. */
  387. nextpc = regcache_raw_get_unsigned (regcache, ARM_LR_REGNUM);
  388. nextpc -= inst2 & 0x00ff;
  389. }
  390. else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
  391. {
  392. /* Conditional branch. */
  393. if (condition_true (bits (inst1, 6, 9), status))
  394. {
  395. int sign, j1, j2, imm1, imm2;
  396. sign = sbits (inst1, 10, 10);
  397. imm1 = bits (inst1, 0, 5);
  398. imm2 = bits (inst2, 0, 10);
  399. j1 = bit (inst2, 13);
  400. j2 = bit (inst2, 11);
  401. unsigned long offset
  402. = (sign << 20) + (j2 << 19) + (j1 << 18);
  403. offset += (imm1 << 12) + (imm2 << 1);
  404. nextpc = pc_val + offset;
  405. }
  406. }
  407. }
  408. else if ((inst1 & 0xfe50) == 0xe810)
  409. {
  410. /* Load multiple or RFE. */
  411. int rn, offset, load_pc = 1;
  412. rn = bits (inst1, 0, 3);
  413. if (bit (inst1, 7) && !bit (inst1, 8))
  414. {
  415. /* LDMIA or POP */
  416. if (!bit (inst2, 15))
  417. load_pc = 0;
  418. offset = count_one_bits (inst2) * 4 - 4;
  419. }
  420. else if (!bit (inst1, 7) && bit (inst1, 8))
  421. {
  422. /* LDMDB */
  423. if (!bit (inst2, 15))
  424. load_pc = 0;
  425. offset = -4;
  426. }
  427. else if (bit (inst1, 7) && bit (inst1, 8))
  428. {
  429. /* RFEIA */
  430. offset = 0;
  431. }
  432. else if (!bit (inst1, 7) && !bit (inst1, 8))
  433. {
  434. /* RFEDB */
  435. offset = -8;
  436. }
  437. else
  438. load_pc = 0;
  439. if (load_pc)
  440. {
  441. CORE_ADDR addr = regcache_raw_get_unsigned (regcache, rn);
  442. nextpc = self->ops->read_mem_uint (addr + offset, 4, byte_order);
  443. }
  444. }
  445. else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
  446. {
  447. /* MOV PC or MOVS PC. */
  448. nextpc = regcache_raw_get_unsigned (regcache, bits (inst2, 0, 3));
  449. nextpc = MAKE_THUMB_ADDR (nextpc);
  450. }
  451. else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
  452. {
  453. /* LDR PC. */
  454. CORE_ADDR base;
  455. int rn, load_pc = 1;
  456. rn = bits (inst1, 0, 3);
  457. base = regcache_raw_get_unsigned (regcache, rn);
  458. if (rn == ARM_PC_REGNUM)
  459. {
  460. base = (base + 4) & ~(CORE_ADDR) 0x3;
  461. if (bit (inst1, 7))
  462. base += bits (inst2, 0, 11);
  463. else
  464. base -= bits (inst2, 0, 11);
  465. }
  466. else if (bit (inst1, 7))
  467. base += bits (inst2, 0, 11);
  468. else if (bit (inst2, 11))
  469. {
  470. if (bit (inst2, 10))
  471. {
  472. if (bit (inst2, 9))
  473. base += bits (inst2, 0, 7);
  474. else
  475. base -= bits (inst2, 0, 7);
  476. }
  477. }
  478. else if ((inst2 & 0x0fc0) == 0x0000)
  479. {
  480. int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
  481. base += regcache_raw_get_unsigned (regcache, rm) << shift;
  482. }
  483. else
  484. /* Reserved. */
  485. load_pc = 0;
  486. if (load_pc)
  487. nextpc
  488. = self->ops->read_mem_uint (base, 4, byte_order);
  489. }
  490. else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
  491. {
  492. /* TBB. */
  493. CORE_ADDR tbl_reg, table, offset, length;
  494. tbl_reg = bits (inst1, 0, 3);
  495. if (tbl_reg == 0x0f)
  496. table = pc + 4; /* Regcache copy of PC isn't right yet. */
  497. else
  498. table = regcache_raw_get_unsigned (regcache, tbl_reg);
  499. offset = regcache_raw_get_unsigned (regcache, bits (inst2, 0, 3));
  500. length = 2 * self->ops->read_mem_uint (table + offset, 1, byte_order);
  501. nextpc = pc_val + length;
  502. }
  503. else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
  504. {
  505. /* TBH. */
  506. CORE_ADDR tbl_reg, table, offset, length;
  507. tbl_reg = bits (inst1, 0, 3);
  508. if (tbl_reg == 0x0f)
  509. table = pc + 4; /* Regcache copy of PC isn't right yet. */
  510. else
  511. table = regcache_raw_get_unsigned (regcache, tbl_reg);
  512. offset = 2 * regcache_raw_get_unsigned (regcache, bits (inst2, 0, 3));
  513. length = 2 * self->ops->read_mem_uint (table + offset, 2, byte_order);
  514. nextpc = pc_val + length;
  515. }
  516. }
  517. else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
  518. {
  519. if (bits (inst1, 3, 6) == 0x0f)
  520. nextpc = UNMAKE_THUMB_ADDR (pc_val);
  521. else
  522. nextpc = regcache_raw_get_unsigned (regcache, bits (inst1, 3, 6));
  523. }
  524. else if ((inst1 & 0xff87) == 0x4687) /* mov pc, REG */
  525. {
  526. if (bits (inst1, 3, 6) == 0x0f)
  527. nextpc = pc_val;
  528. else
  529. nextpc = regcache_raw_get_unsigned (regcache, bits (inst1, 3, 6));
  530. nextpc = MAKE_THUMB_ADDR (nextpc);
  531. }
  532. else if ((inst1 & 0xf500) == 0xb100)
  533. {
  534. /* CBNZ or CBZ. */
  535. int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
  536. ULONGEST reg = regcache_raw_get_unsigned (regcache, bits (inst1, 0, 2));
  537. if (bit (inst1, 11) && reg != 0)
  538. nextpc = pc_val + imm;
  539. else if (!bit (inst1, 11) && reg == 0)
  540. nextpc = pc_val + imm;
  541. }
  542. next_pcs.push_back (nextpc);
  543. return next_pcs;
  544. }
  545. /* Get the raw next possible addresses. PC in next_pcs is the current program
  546. counter, which is assumed to be executing in ARM mode.
  547. The values returned have the execution state of the next instruction
  548. encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
  549. in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
  550. address in GDB and arm_addr_bits_remove in GDBServer. */
  551. static std::vector<CORE_ADDR>
  552. arm_get_next_pcs_raw (struct arm_get_next_pcs *self)
  553. {
  554. int byte_order = self->byte_order;
  555. int byte_order_for_code = self->byte_order_for_code;
  556. unsigned long pc_val;
  557. unsigned long this_instr = 0;
  558. unsigned long status;
  559. CORE_ADDR nextpc;
  560. struct regcache *regcache = self->regcache;
  561. CORE_ADDR pc = regcache_read_pc (self->regcache);
  562. std::vector<CORE_ADDR> next_pcs;
  563. pc_val = (unsigned long) pc;
  564. this_instr = self->ops->read_mem_uint (pc, 4, byte_order_for_code);
  565. status = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
  566. nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
  567. if (bits (this_instr, 28, 31) == INST_NV)
  568. switch (bits (this_instr, 24, 27))
  569. {
  570. case 0xa:
  571. case 0xb:
  572. {
  573. /* Branch with Link and change to Thumb. */
  574. nextpc = BranchDest (pc, this_instr);
  575. nextpc |= bit (this_instr, 24) << 1;
  576. nextpc = MAKE_THUMB_ADDR (nextpc);
  577. break;
  578. }
  579. case 0xc:
  580. case 0xd:
  581. case 0xe:
  582. /* Coprocessor register transfer. */
  583. if (bits (this_instr, 12, 15) == 15)
  584. error (_("Invalid update to pc in instruction"));
  585. break;
  586. }
  587. else if (condition_true (bits (this_instr, 28, 31), status))
  588. {
  589. switch (bits (this_instr, 24, 27))
  590. {
  591. case 0x0:
  592. case 0x1: /* data processing */
  593. case 0x2:
  594. case 0x3:
  595. {
  596. unsigned long operand1, operand2, result = 0;
  597. unsigned long rn;
  598. int c;
  599. if (bits (this_instr, 12, 15) != 15)
  600. break;
  601. if (bits (this_instr, 22, 25) == 0
  602. && bits (this_instr, 4, 7) == 9) /* multiply */
  603. error (_("Invalid update to pc in instruction"));
  604. /* BX <reg>, BLX <reg> */
  605. if (bits (this_instr, 4, 27) == 0x12fff1
  606. || bits (this_instr, 4, 27) == 0x12fff3)
  607. {
  608. rn = bits (this_instr, 0, 3);
  609. nextpc = ((rn == ARM_PC_REGNUM)
  610. ? (pc_val + 8)
  611. : regcache_raw_get_unsigned (regcache, rn));
  612. next_pcs.push_back (nextpc);
  613. return next_pcs;
  614. }
  615. /* Multiply into PC. */
  616. c = (status & FLAG_C) ? 1 : 0;
  617. rn = bits (this_instr, 16, 19);
  618. operand1 = ((rn == ARM_PC_REGNUM)
  619. ? (pc_val + 8)
  620. : regcache_raw_get_unsigned (regcache, rn));
  621. if (bit (this_instr, 25))
  622. {
  623. unsigned long immval = bits (this_instr, 0, 7);
  624. unsigned long rotate = 2 * bits (this_instr, 8, 11);
  625. operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
  626. & 0xffffffff;
  627. }
  628. else /* operand 2 is a shifted register. */
  629. operand2 = shifted_reg_val (regcache, this_instr, c,
  630. pc_val, status);
  631. switch (bits (this_instr, 21, 24))
  632. {
  633. case 0x0: /*and */
  634. result = operand1 & operand2;
  635. break;
  636. case 0x1: /*eor */
  637. result = operand1 ^ operand2;
  638. break;
  639. case 0x2: /*sub */
  640. result = operand1 - operand2;
  641. break;
  642. case 0x3: /*rsb */
  643. result = operand2 - operand1;
  644. break;
  645. case 0x4: /*add */
  646. result = operand1 + operand2;
  647. break;
  648. case 0x5: /*adc */
  649. result = operand1 + operand2 + c;
  650. break;
  651. case 0x6: /*sbc */
  652. result = operand1 - operand2 + c;
  653. break;
  654. case 0x7: /*rsc */
  655. result = operand2 - operand1 + c;
  656. break;
  657. case 0x8:
  658. case 0x9:
  659. case 0xa:
  660. case 0xb: /* tst, teq, cmp, cmn */
  661. result = (unsigned long) nextpc;
  662. break;
  663. case 0xc: /*orr */
  664. result = operand1 | operand2;
  665. break;
  666. case 0xd: /*mov */
  667. /* Always step into a function. */
  668. result = operand2;
  669. break;
  670. case 0xe: /*bic */
  671. result = operand1 & ~operand2;
  672. break;
  673. case 0xf: /*mvn */
  674. result = ~operand2;
  675. break;
  676. }
  677. nextpc = self->ops->addr_bits_remove (self, result);
  678. break;
  679. }
  680. case 0x4:
  681. case 0x5: /* data transfer */
  682. case 0x6:
  683. case 0x7:
  684. if (bits (this_instr, 25, 27) == 0x3 && bit (this_instr, 4) == 1)
  685. {
  686. /* Media instructions and architecturally undefined
  687. instructions. */
  688. break;
  689. }
  690. if (bit (this_instr, 20))
  691. {
  692. /* load */
  693. if (bits (this_instr, 12, 15) == 15)
  694. {
  695. /* rd == pc */
  696. unsigned long rn;
  697. unsigned long base;
  698. if (bit (this_instr, 22))
  699. error (_("Invalid update to pc in instruction"));
  700. /* byte write to PC */
  701. rn = bits (this_instr, 16, 19);
  702. base = ((rn == ARM_PC_REGNUM)
  703. ? (pc_val + 8)
  704. : regcache_raw_get_unsigned (regcache, rn));
  705. if (bit (this_instr, 24))
  706. {
  707. /* pre-indexed */
  708. int c = (status & FLAG_C) ? 1 : 0;
  709. unsigned long offset =
  710. (bit (this_instr, 25)
  711. ? shifted_reg_val (regcache, this_instr, c,
  712. pc_val, status)
  713. : bits (this_instr, 0, 11));
  714. if (bit (this_instr, 23))
  715. base += offset;
  716. else
  717. base -= offset;
  718. }
  719. nextpc
  720. = (CORE_ADDR) self->ops->read_mem_uint ((CORE_ADDR) base,
  721. 4, byte_order);
  722. }
  723. }
  724. break;
  725. case 0x8:
  726. case 0x9: /* block transfer */
  727. if (bit (this_instr, 20))
  728. {
  729. /* LDM */
  730. if (bit (this_instr, 15))
  731. {
  732. /* loading pc */
  733. int offset = 0;
  734. CORE_ADDR rn_val_offset = 0;
  735. unsigned long rn_val
  736. = regcache_raw_get_unsigned (regcache,
  737. bits (this_instr, 16, 19));
  738. if (bit (this_instr, 23))
  739. {
  740. /* up */
  741. unsigned long reglist = bits (this_instr, 0, 14);
  742. offset = count_one_bits_l (reglist) * 4;
  743. if (bit (this_instr, 24)) /* pre */
  744. offset += 4;
  745. }
  746. else if (bit (this_instr, 24))
  747. offset = -4;
  748. rn_val_offset = rn_val + offset;
  749. nextpc = (CORE_ADDR) self->ops->read_mem_uint (rn_val_offset,
  750. 4, byte_order);
  751. }
  752. }
  753. break;
  754. case 0xb: /* branch & link */
  755. case 0xa: /* branch */
  756. {
  757. nextpc = BranchDest (pc, this_instr);
  758. break;
  759. }
  760. case 0xc:
  761. case 0xd:
  762. case 0xe: /* coproc ops */
  763. break;
  764. case 0xf: /* SWI */
  765. {
  766. nextpc = self->ops->syscall_next_pc (self);
  767. }
  768. break;
  769. default:
  770. error (_("Bad bit-field extraction"));
  771. return next_pcs;
  772. }
  773. }
  774. next_pcs.push_back (nextpc);
  775. return next_pcs;
  776. }
  777. /* See arm-get-next-pcs.h. */
  778. std::vector<CORE_ADDR>
  779. arm_get_next_pcs (struct arm_get_next_pcs *self)
  780. {
  781. std::vector<CORE_ADDR> next_pcs;
  782. if (self->ops->is_thumb (self))
  783. {
  784. next_pcs = thumb_deal_with_atomic_sequence_raw (self);
  785. if (next_pcs.empty ())
  786. next_pcs = thumb_get_next_pcs_raw (self);
  787. }
  788. else
  789. {
  790. next_pcs = arm_deal_with_atomic_sequence_raw (self);
  791. if (next_pcs.empty ())
  792. next_pcs = arm_get_next_pcs_raw (self);
  793. }
  794. if (self->ops->fixup != NULL)
  795. {
  796. for (CORE_ADDR &pc_ref : next_pcs)
  797. pc_ref = self->ops->fixup (self, pc_ref);
  798. }
  799. return next_pcs;
  800. }