Gex_tables.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* libunwind - a platform-independent unwind library
  2. Copyright 2011 Linaro Limited
  3. This file is part of libunwind.
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  20. /* This file contains functionality for parsing and interpreting the ARM
  21. specific unwind information. Documentation about the exception handling
  22. ABI for the ARM architecture can be found at:
  23. http://infocenter.arm.com/help/topic/com.arm.doc.ihi0038a/IHI0038A_ehabi.pdf
  24. */
  25. #include "libunwind_i.h"
  26. #define ARM_EXBUF_START(x) (((x) >> 4) & 0x0f)
  27. #define ARM_EXBUF_COUNT(x) ((x) & 0x0f)
  28. #define ARM_EXBUF_END(x) (ARM_EXBUF_START(x) + ARM_EXBUF_COUNT(x))
  29. #define ARM_EXIDX_CANT_UNWIND 0x00000001
  30. #define ARM_EXIDX_COMPACT 0x80000000
  31. #define ARM_EXTBL_OP_FINISH 0xb0
  32. enum arm_exbuf_cmd_flags {
  33. ARM_EXIDX_VFP_SHIFT_16 = 1 << 16,
  34. ARM_EXIDX_VFP_DOUBLE = 1 << 17,
  35. };
  36. struct arm_cb_data
  37. {
  38. /* in: */
  39. unw_word_t ip; /* instruction-pointer we're looking for */
  40. unw_proc_info_t *pi; /* proc-info pointer */
  41. /* out: */
  42. unw_dyn_info_t di; /* info about the ARM exidx segment */
  43. };
  44. static inline uint32_t
  45. prel31_read (uint32_t prel31)
  46. {
  47. return ((int32_t)prel31 << 1) >> 1;
  48. }
  49. static inline int
  50. prel31_to_addr (unw_addr_space_t as, void *arg, unw_word_t prel31,
  51. unw_word_t *val)
  52. {
  53. unw_word_t offset;
  54. if ((*as->acc.access_mem)(as, prel31, &offset, 0, arg) < 0)
  55. return -UNW_EINVAL;
  56. offset = ((long)offset << 1) >> 1;
  57. *val = prel31 + offset;
  58. return 0;
  59. }
  60. /**
  61. * Applies the given command onto the new state to the given dwarf_cursor.
  62. */
  63. HIDDEN int
  64. arm_exidx_apply_cmd (struct arm_exbuf_data *edata, struct dwarf_cursor *c)
  65. {
  66. int ret = 0;
  67. unsigned i;
  68. switch (edata->cmd)
  69. {
  70. case ARM_EXIDX_CMD_FINISH:
  71. /* Set LR to PC if not set already. */
  72. if (DWARF_IS_NULL_LOC (c->loc[UNW_ARM_R15]))
  73. c->loc[UNW_ARM_R15] = c->loc[UNW_ARM_R14];
  74. /* Set IP. */
  75. dwarf_get (c, c->loc[UNW_ARM_R15], &c->ip);
  76. break;
  77. case ARM_EXIDX_CMD_DATA_PUSH:
  78. Debug (2, "vsp = vsp - %d\n", edata->data);
  79. c->cfa -= edata->data;
  80. break;
  81. case ARM_EXIDX_CMD_DATA_POP:
  82. Debug (2, "vsp = vsp + %d\n", edata->data);
  83. c->cfa += edata->data;
  84. break;
  85. case ARM_EXIDX_CMD_REG_POP:
  86. for (i = 0; i < 16; i++)
  87. if (edata->data & (1 << i))
  88. {
  89. Debug (2, "pop {r%d}\n", i);
  90. c->loc[UNW_ARM_R0 + i] = DWARF_LOC (c->cfa, 0);
  91. c->cfa += 4;
  92. }
  93. /* Set cfa in case the SP got popped. */
  94. if (edata->data & (1 << 13))
  95. dwarf_get (c, c->loc[UNW_ARM_R13], &c->cfa);
  96. break;
  97. case ARM_EXIDX_CMD_REG_TO_SP:
  98. assert (edata->data < 16);
  99. Debug (2, "vsp = r%d\n", edata->data);
  100. c->loc[UNW_ARM_R13] = c->loc[UNW_ARM_R0 + edata->data];
  101. dwarf_get (c, c->loc[UNW_ARM_R13], &c->cfa);
  102. break;
  103. case ARM_EXIDX_CMD_VFP_POP:
  104. /* Skip VFP registers, but be sure to adjust stack */
  105. for (i = ARM_EXBUF_START (edata->data); i <= ARM_EXBUF_END (edata->data);
  106. i++)
  107. c->cfa += 8;
  108. if (!(edata->data & ARM_EXIDX_VFP_DOUBLE))
  109. c->cfa += 4;
  110. break;
  111. case ARM_EXIDX_CMD_WREG_POP:
  112. for (i = ARM_EXBUF_START (edata->data); i <= ARM_EXBUF_END (edata->data);
  113. i++)
  114. c->cfa += 8;
  115. break;
  116. case ARM_EXIDX_CMD_WCGR_POP:
  117. for (i = 0; i < 4; i++)
  118. if (edata->data & (1 << i))
  119. c->cfa += 4;
  120. break;
  121. case ARM_EXIDX_CMD_REFUSED:
  122. case ARM_EXIDX_CMD_RESERVED:
  123. ret = -1;
  124. break;
  125. }
  126. return ret;
  127. }
  128. /**
  129. * Decodes the given unwind instructions into arm_exbuf_data and calls
  130. * arm_exidx_apply_cmd that applies the command onto the dwarf_cursor.
  131. */
  132. HIDDEN int
  133. arm_exidx_decode (const uint8_t *buf, uint8_t len, struct dwarf_cursor *c)
  134. {
  135. #define READ_OP() *buf++
  136. const uint8_t *end = buf + len;
  137. int ret;
  138. struct arm_exbuf_data edata;
  139. assert(buf != NULL);
  140. assert(len > 0);
  141. while (buf < end)
  142. {
  143. uint8_t op = READ_OP ();
  144. if ((op & 0xc0) == 0x00)
  145. {
  146. edata.cmd = ARM_EXIDX_CMD_DATA_POP;
  147. edata.data = (((int)op & 0x3f) << 2) + 4;
  148. }
  149. else if ((op & 0xc0) == 0x40)
  150. {
  151. edata.cmd = ARM_EXIDX_CMD_DATA_PUSH;
  152. edata.data = (((int)op & 0x3f) << 2) + 4;
  153. }
  154. else if ((op & 0xf0) == 0x80)
  155. {
  156. uint8_t op2 = READ_OP ();
  157. if (op == 0x80 && op2 == 0x00)
  158. edata.cmd = ARM_EXIDX_CMD_REFUSED;
  159. else
  160. {
  161. edata.cmd = ARM_EXIDX_CMD_REG_POP;
  162. edata.data = ((op & 0xf) << 8) | op2;
  163. edata.data = edata.data << 4;
  164. }
  165. }
  166. else if ((op & 0xf0) == 0x90)
  167. {
  168. if (op == 0x9d || op == 0x9f)
  169. edata.cmd = ARM_EXIDX_CMD_RESERVED;
  170. else
  171. {
  172. edata.cmd = ARM_EXIDX_CMD_REG_TO_SP;
  173. edata.data = op & 0x0f;
  174. }
  175. }
  176. else if ((op & 0xf0) == 0xa0)
  177. {
  178. unsigned end = (op & 0x07);
  179. edata.data = (1 << (end + 1)) - 1;
  180. edata.data = edata.data << 4;
  181. if (op & 0x08)
  182. edata.data |= 1 << 14;
  183. edata.cmd = ARM_EXIDX_CMD_REG_POP;
  184. }
  185. else if (op == ARM_EXTBL_OP_FINISH)
  186. {
  187. edata.cmd = ARM_EXIDX_CMD_FINISH;
  188. buf = end;
  189. }
  190. else if (op == 0xb1)
  191. {
  192. uint8_t op2 = READ_OP ();
  193. if (op2 == 0 || (op2 & 0xf0))
  194. edata.cmd = ARM_EXIDX_CMD_RESERVED;
  195. else
  196. {
  197. edata.cmd = ARM_EXIDX_CMD_REG_POP;
  198. edata.data = op2 & 0x0f;
  199. }
  200. }
  201. else if (op == 0xb2)
  202. {
  203. uint32_t offset = 0;
  204. uint8_t byte, shift = 0;
  205. do
  206. {
  207. byte = READ_OP ();
  208. offset |= (byte & 0x7f) << shift;
  209. shift += 7;
  210. }
  211. while (byte & 0x80);
  212. edata.data = offset * 4 + 0x204;
  213. edata.cmd = ARM_EXIDX_CMD_DATA_POP;
  214. }
  215. else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
  216. {
  217. edata.cmd = ARM_EXIDX_CMD_VFP_POP;
  218. edata.data = READ_OP ();
  219. if (op == 0xc8)
  220. edata.data |= ARM_EXIDX_VFP_SHIFT_16;
  221. if (op != 0xb3)
  222. edata.data |= ARM_EXIDX_VFP_DOUBLE;
  223. }
  224. else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
  225. {
  226. edata.cmd = ARM_EXIDX_CMD_VFP_POP;
  227. edata.data = 0x80 | (op & 0x07);
  228. if ((op & 0xf8) == 0xd0)
  229. edata.data |= ARM_EXIDX_VFP_DOUBLE;
  230. }
  231. else if (op >= 0xc0 && op <= 0xc5)
  232. {
  233. edata.cmd = ARM_EXIDX_CMD_WREG_POP;
  234. edata.data = 0xa0 | (op & 0x07);
  235. }
  236. else if (op == 0xc6)
  237. {
  238. edata.cmd = ARM_EXIDX_CMD_WREG_POP;
  239. edata.data = READ_OP ();
  240. }
  241. else if (op == 0xc7)
  242. {
  243. uint8_t op2 = READ_OP ();
  244. if (op2 == 0 || (op2 & 0xf0))
  245. edata.cmd = ARM_EXIDX_CMD_RESERVED;
  246. else
  247. {
  248. edata.cmd = ARM_EXIDX_CMD_WCGR_POP;
  249. edata.data = op2 & 0x0f;
  250. }
  251. }
  252. else
  253. edata.cmd = ARM_EXIDX_CMD_RESERVED;
  254. ret = arm_exidx_apply_cmd (&edata, c);
  255. if (ret < 0)
  256. return ret;
  257. }
  258. return 0;
  259. }
  260. /**
  261. * Reads the entry from the given cursor and extracts the unwind instructions
  262. * into buf. Returns the number of the extracted unwind insns or
  263. * -UNW_ESTOPUNWIND if the special bit pattern ARM_EXIDX_CANT_UNWIND (0x1) was
  264. * found.
  265. */
  266. HIDDEN int
  267. arm_exidx_extract (struct dwarf_cursor *c, uint8_t *buf)
  268. {
  269. int nbuf = 0;
  270. unw_word_t entry = (unw_word_t) c->pi.unwind_info;
  271. unw_word_t addr;
  272. uint32_t data;
  273. /* An ARM unwind entry consists of a prel31 offset to the start of a
  274. function followed by 31bits of data:
  275. * if set to 0x1: the function cannot be unwound (EXIDX_CANTUNWIND)
  276. * if bit 31 is one: this is a table entry itself (ARM_EXIDX_COMPACT)
  277. * if bit 31 is zero: this is a prel31 offset of the start of the
  278. table entry for this function */
  279. if (prel31_to_addr(c->as, c->as_arg, entry, &addr) < 0)
  280. return -UNW_EINVAL;
  281. if ((*c->as->acc.access_mem)(c->as, entry + 4, &data, 0, c->as_arg) < 0)
  282. return -UNW_EINVAL;
  283. if (data == ARM_EXIDX_CANT_UNWIND)
  284. {
  285. Debug (2, "0x1 [can't unwind]\n");
  286. nbuf = -UNW_ESTOPUNWIND;
  287. }
  288. else if (data & ARM_EXIDX_COMPACT)
  289. {
  290. Debug (2, "%p compact model %d [%8.8x]\n", (void *)addr,
  291. (data >> 24) & 0x7f, data);
  292. buf[nbuf++] = data >> 16;
  293. buf[nbuf++] = data >> 8;
  294. buf[nbuf++] = data;
  295. }
  296. else
  297. {
  298. unw_word_t extbl_data;
  299. unsigned int n_table_words = 0;
  300. if (prel31_to_addr(c->as, c->as_arg, entry + 4, &extbl_data) < 0)
  301. return -UNW_EINVAL;
  302. if ((*c->as->acc.access_mem)(c->as, extbl_data, &data, 0, c->as_arg) < 0)
  303. return -UNW_EINVAL;
  304. if (data & ARM_EXIDX_COMPACT)
  305. {
  306. int pers = (data >> 24) & 0x0f;
  307. Debug (2, "%p compact model %d [%8.8x]\n", (void *)addr, pers, data);
  308. if (pers == 1 || pers == 2)
  309. {
  310. n_table_words = (data >> 16) & 0xff;
  311. extbl_data += 4;
  312. }
  313. else
  314. buf[nbuf++] = data >> 16;
  315. buf[nbuf++] = data >> 8;
  316. buf[nbuf++] = data;
  317. }
  318. else
  319. {
  320. unw_word_t pers;
  321. if (prel31_to_addr (c->as, c->as_arg, extbl_data, &pers) < 0)
  322. return -UNW_EINVAL;
  323. Debug (2, "%p Personality routine: %8p\n", (void *)addr,
  324. (void *)pers);
  325. if ((*c->as->acc.access_mem)(c->as, extbl_data + 4, &data, 0,
  326. c->as_arg) < 0)
  327. return -UNW_EINVAL;
  328. n_table_words = data >> 24;
  329. buf[nbuf++] = data >> 16;
  330. buf[nbuf++] = data >> 8;
  331. buf[nbuf++] = data;
  332. extbl_data += 8;
  333. }
  334. assert (n_table_words <= 5);
  335. unsigned j;
  336. for (j = 0; j < n_table_words; j++)
  337. {
  338. if ((*c->as->acc.access_mem)(c->as, extbl_data, &data, 0,
  339. c->as_arg) < 0)
  340. return -UNW_EINVAL;
  341. extbl_data += 4;
  342. buf[nbuf++] = data >> 24;
  343. buf[nbuf++] = data >> 16;
  344. buf[nbuf++] = data >> 8;
  345. buf[nbuf++] = data >> 0;
  346. }
  347. }
  348. if (nbuf > 0 && buf[nbuf - 1] != ARM_EXTBL_OP_FINISH)
  349. buf[nbuf++] = ARM_EXTBL_OP_FINISH;
  350. return nbuf;
  351. }
  352. PROTECTED int
  353. tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
  354. unw_dyn_info_t *di, unw_proc_info_t *pi,
  355. int need_unwind_info, void *arg)
  356. {
  357. if (UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX)
  358. && di->format == UNW_INFO_FORMAT_ARM_EXIDX)
  359. {
  360. /* The .ARM.exidx section contains a sorted list of key-value pairs -
  361. the unwind entries. The 'key' is a prel31 offset to the start of a
  362. function. We binary search this section in order to find the
  363. appropriate unwind entry. */
  364. unw_word_t first = di->u.rti.table_data;
  365. unw_word_t last = di->u.rti.table_data + di->u.rti.table_len - 8;
  366. unw_word_t entry, val;
  367. if (prel31_to_addr (as, arg, first, &val) < 0 || ip < val)
  368. return -UNW_ENOINFO;
  369. if (prel31_to_addr (as, arg, last, &val) < 0)
  370. return -UNW_EINVAL;
  371. if (ip >= val)
  372. {
  373. entry = last;
  374. if (prel31_to_addr (as, arg, last, &pi->start_ip) < 0)
  375. return -UNW_EINVAL;
  376. pi->end_ip = di->end_ip -1;
  377. }
  378. else
  379. {
  380. while (first < last - 8)
  381. {
  382. entry = first + (((last - first) / 8 + 1) >> 1) * 8;
  383. if (prel31_to_addr (as, arg, entry, &val) < 0)
  384. return -UNW_EINVAL;
  385. if (ip < val)
  386. last = entry;
  387. else
  388. first = entry;
  389. }
  390. entry = first;
  391. if (prel31_to_addr (as, arg, entry, &pi->start_ip) < 0)
  392. return -UNW_EINVAL;
  393. if (prel31_to_addr (as, arg, entry + 8, &pi->end_ip) < 0)
  394. return -UNW_EINVAL;
  395. pi->end_ip--;
  396. }
  397. if (need_unwind_info)
  398. {
  399. pi->unwind_info_size = 8;
  400. pi->unwind_info = (void *) entry;
  401. pi->format = UNW_INFO_FORMAT_ARM_EXIDX;
  402. }
  403. return 0;
  404. }
  405. else if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF)
  406. && di->format != UNW_INFO_FORMAT_ARM_EXIDX)
  407. return dwarf_search_unwind_table (as, ip, di, pi, need_unwind_info, arg);
  408. return -UNW_ENOINFO;
  409. }
  410. #ifndef UNW_REMOTE_ONLY
  411. /**
  412. * Callback to dl_iterate_phdr to find infos about the ARM exidx segment.
  413. */
  414. static int
  415. arm_phdr_cb (struct dl_phdr_info *info, size_t size, void *data)
  416. {
  417. struct arm_cb_data *cb_data = data;
  418. const Elf_W(Phdr) *p_text = NULL;
  419. const Elf_W(Phdr) *p_arm_exidx = NULL;
  420. const Elf_W(Phdr) *phdr = info->dlpi_phdr;
  421. long n;
  422. for (n = info->dlpi_phnum; --n >= 0; phdr++)
  423. {
  424. switch (phdr->p_type)
  425. {
  426. case PT_LOAD:
  427. if (cb_data->ip >= phdr->p_vaddr + info->dlpi_addr &&
  428. cb_data->ip < phdr->p_vaddr + info->dlpi_addr + phdr->p_memsz)
  429. p_text = phdr;
  430. break;
  431. case PT_ARM_EXIDX:
  432. p_arm_exidx = phdr;
  433. break;
  434. default:
  435. break;
  436. }
  437. }
  438. if (p_text && p_arm_exidx)
  439. {
  440. cb_data->di.format = UNW_INFO_FORMAT_ARM_EXIDX;
  441. cb_data->di.start_ip = p_text->p_vaddr + info->dlpi_addr;
  442. cb_data->di.end_ip = cb_data->di.start_ip + p_text->p_memsz;
  443. cb_data->di.u.rti.name_ptr = (unw_word_t) info->dlpi_name;
  444. cb_data->di.u.rti.table_data = p_arm_exidx->p_vaddr + info->dlpi_addr;
  445. cb_data->di.u.rti.table_len = p_arm_exidx->p_memsz;
  446. return 1;
  447. }
  448. return 0;
  449. }
  450. HIDDEN int
  451. arm_find_proc_info (unw_addr_space_t as, unw_word_t ip,
  452. unw_proc_info_t *pi, int need_unwind_info, void *arg)
  453. {
  454. int ret = -1;
  455. intrmask_t saved_mask;
  456. Debug (14, "looking for IP=0x%lx\n", (long) ip);
  457. if (UNW_TRY_METHOD(UNW_ARM_METHOD_DWARF))
  458. {
  459. struct dwarf_callback_data cb_data;
  460. memset (&cb_data, 0, sizeof (cb_data));
  461. cb_data.ip = ip;
  462. cb_data.pi = pi;
  463. cb_data.need_unwind_info = need_unwind_info;
  464. cb_data.di.format = -1;
  465. cb_data.di_debug.format = -1;
  466. SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &saved_mask);
  467. ret = dl_iterate_phdr (dwarf_callback, &cb_data);
  468. SIGPROCMASK (SIG_SETMASK, &saved_mask, NULL);
  469. if (cb_data.single_fde)
  470. /* already got the result in *pi */
  471. return 0;
  472. if (cb_data.di_debug.format != -1)
  473. ret = tdep_search_unwind_table (as, ip, &cb_data.di_debug, pi,
  474. need_unwind_info, arg);
  475. else
  476. ret = -UNW_ENOINFO;
  477. }
  478. if (ret < 0 && UNW_TRY_METHOD (UNW_ARM_METHOD_EXIDX))
  479. {
  480. struct arm_cb_data cb_data;
  481. memset (&cb_data, 0, sizeof (cb_data));
  482. cb_data.ip = ip;
  483. cb_data.pi = pi;
  484. cb_data.di.format = -1;
  485. SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &saved_mask);
  486. ret = dl_iterate_phdr (arm_phdr_cb, &cb_data);
  487. SIGPROCMASK (SIG_SETMASK, &saved_mask, NULL);
  488. if (cb_data.di.format != -1)
  489. ret = tdep_search_unwind_table (as, ip, &cb_data.di, pi,
  490. need_unwind_info, arg);
  491. else
  492. ret = -UNW_ENOINFO;
  493. }
  494. if (ret < 0)
  495. Debug (14, "IP=0x%lx not found\n", (long) ip);
  496. return ret;
  497. }
  498. HIDDEN void
  499. arm_put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
  500. {
  501. /* it's a no-op */
  502. }
  503. #endif /* !UNW_REMOTE_ONLY */