unwind_decoder.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2001-2002 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. /*
  22. * Generic IA-64 unwind info decoder.
  23. *
  24. * This file is used both by the Linux kernel and objdump. Please keep
  25. * the two copies of this file in sync.
  26. *
  27. * You need to customize the decoder by defining the following
  28. * macros/constants before including this file:
  29. *
  30. * Types:
  31. * unw_word Unsigned integer type with at least 64 bits
  32. *
  33. * Register names:
  34. * UNW_REG_BSP
  35. * UNW_REG_BSPSTORE
  36. * UNW_REG_FPSR
  37. * UNW_REG_LC
  38. * UNW_REG_PFS
  39. * UNW_REG_PR
  40. * UNW_REG_RNAT
  41. * UNW_REG_PSP
  42. * UNW_REG_RP
  43. * UNW_REG_UNAT
  44. *
  45. * Decoder action macros:
  46. * UNW_DEC_BAD_CODE(code)
  47. * UNW_DEC_ABI(fmt,abi,context,arg)
  48. * UNW_DEC_BR_GR(fmt,brmask,gr,arg)
  49. * UNW_DEC_BR_MEM(fmt,brmask,arg)
  50. * UNW_DEC_COPY_STATE(fmt,label,arg)
  51. * UNW_DEC_EPILOGUE(fmt,t,ecount,arg)
  52. * UNW_DEC_FRGR_MEM(fmt,grmask,frmask,arg)
  53. * UNW_DEC_FR_MEM(fmt,frmask,arg)
  54. * UNW_DEC_GR_GR(fmt,grmask,gr,arg)
  55. * UNW_DEC_GR_MEM(fmt,grmask,arg)
  56. * UNW_DEC_LABEL_STATE(fmt,label,arg)
  57. * UNW_DEC_MEM_STACK_F(fmt,t,size,arg)
  58. * UNW_DEC_MEM_STACK_V(fmt,t,arg)
  59. * UNW_DEC_PRIUNAT_GR(fmt,r,arg)
  60. * UNW_DEC_PRIUNAT_WHEN_GR(fmt,t,arg)
  61. * UNW_DEC_PRIUNAT_WHEN_MEM(fmt,t,arg)
  62. * UNW_DEC_PRIUNAT_WHEN_PSPREL(fmt,pspoff,arg)
  63. * UNW_DEC_PRIUNAT_WHEN_SPREL(fmt,spoff,arg)
  64. * UNW_DEC_PROLOGUE(fmt,body,rlen,arg)
  65. * UNW_DEC_PROLOGUE_GR(fmt,rlen,mask,grsave,arg)
  66. * UNW_DEC_REG_PSPREL(fmt,reg,pspoff,arg)
  67. * UNW_DEC_REG_REG(fmt,src,dst,arg)
  68. * UNW_DEC_REG_SPREL(fmt,reg,spoff,arg)
  69. * UNW_DEC_REG_WHEN(fmt,reg,t,arg)
  70. * UNW_DEC_RESTORE(fmt,t,abreg,arg)
  71. * UNW_DEC_RESTORE_P(fmt,qp,t,abreg,arg)
  72. * UNW_DEC_SPILL_BASE(fmt,pspoff,arg)
  73. * UNW_DEC_SPILL_MASK(fmt,imaskp,arg)
  74. * UNW_DEC_SPILL_PSPREL(fmt,t,abreg,pspoff,arg)
  75. * UNW_DEC_SPILL_PSPREL_P(fmt,qp,t,abreg,pspoff,arg)
  76. * UNW_DEC_SPILL_REG(fmt,t,abreg,x,ytreg,arg)
  77. * UNW_DEC_SPILL_REG_P(fmt,qp,t,abreg,x,ytreg,arg)
  78. * UNW_DEC_SPILL_SPREL(fmt,t,abreg,spoff,arg)
  79. * UNW_DEC_SPILL_SPREL_P(fmt,qp,t,abreg,pspoff,arg)
  80. */
  81. static unw_word
  82. unw_decode_uleb128 (unsigned char **dpp)
  83. {
  84. unsigned shift = 0;
  85. unw_word byte, result = 0;
  86. unsigned char *bp = *dpp;
  87. while (1)
  88. {
  89. byte = *bp++;
  90. result |= (byte & 0x7f) << shift;
  91. if ((byte & 0x80) == 0)
  92. break;
  93. shift += 7;
  94. }
  95. *dpp = bp;
  96. return result;
  97. }
  98. static unsigned char *
  99. unw_decode_x1 (unsigned char *dp, unsigned char code, void *arg)
  100. {
  101. unsigned char byte1, abreg;
  102. unw_word t, off;
  103. byte1 = *dp++;
  104. t = unw_decode_uleb128 (&dp);
  105. off = unw_decode_uleb128 (&dp);
  106. abreg = (byte1 & 0x7f);
  107. if (byte1 & 0x80)
  108. UNW_DEC_SPILL_SPREL(X1, t, abreg, off, arg);
  109. else
  110. UNW_DEC_SPILL_PSPREL(X1, t, abreg, off, arg);
  111. return dp;
  112. }
  113. static unsigned char *
  114. unw_decode_x2 (unsigned char *dp, unsigned char code, void *arg)
  115. {
  116. unsigned char byte1, byte2, abreg, x, ytreg;
  117. unw_word t;
  118. byte1 = *dp++; byte2 = *dp++;
  119. t = unw_decode_uleb128 (&dp);
  120. abreg = (byte1 & 0x7f);
  121. ytreg = byte2;
  122. x = (byte1 >> 7) & 1;
  123. if ((byte1 & 0x80) == 0 && ytreg == 0)
  124. UNW_DEC_RESTORE(X2, t, abreg, arg);
  125. else
  126. UNW_DEC_SPILL_REG(X2, t, abreg, x, ytreg, arg);
  127. return dp;
  128. }
  129. static unsigned char *
  130. unw_decode_x3 (unsigned char *dp, unsigned char code, void *arg)
  131. {
  132. unsigned char byte1, byte2, abreg, qp;
  133. unw_word t, off;
  134. byte1 = *dp++; byte2 = *dp++;
  135. t = unw_decode_uleb128 (&dp);
  136. off = unw_decode_uleb128 (&dp);
  137. qp = (byte1 & 0x3f);
  138. abreg = (byte2 & 0x7f);
  139. if (byte1 & 0x80)
  140. UNW_DEC_SPILL_SPREL_P(X3, qp, t, abreg, off, arg);
  141. else
  142. UNW_DEC_SPILL_PSPREL_P(X3, qp, t, abreg, off, arg);
  143. return dp;
  144. }
  145. static unsigned char *
  146. unw_decode_x4 (unsigned char *dp, unsigned char code, void *arg)
  147. {
  148. unsigned char byte1, byte2, byte3, qp, abreg, x, ytreg;
  149. unw_word t;
  150. byte1 = *dp++; byte2 = *dp++; byte3 = *dp++;
  151. t = unw_decode_uleb128 (&dp);
  152. qp = (byte1 & 0x3f);
  153. abreg = (byte2 & 0x7f);
  154. x = (byte2 >> 7) & 1;
  155. ytreg = byte3;
  156. if ((byte2 & 0x80) == 0 && byte3 == 0)
  157. UNW_DEC_RESTORE_P(X4, qp, t, abreg, arg);
  158. else
  159. UNW_DEC_SPILL_REG_P(X4, qp, t, abreg, x, ytreg, arg);
  160. return dp;
  161. }
  162. static inline unsigned char *
  163. unw_decode_r1 (unsigned char *dp, unsigned char code, void *arg)
  164. {
  165. int body = (code & 0x20) != 0;
  166. unw_word rlen;
  167. rlen = (code & 0x1f);
  168. UNW_DEC_PROLOGUE(R1, body, rlen, arg);
  169. return dp;
  170. }
  171. static inline unsigned char *
  172. unw_decode_r2 (unsigned char *dp, unsigned char code, void *arg)
  173. {
  174. unsigned char byte1, mask, grsave;
  175. unw_word rlen;
  176. byte1 = *dp++;
  177. mask = ((code & 0x7) << 1) | ((byte1 >> 7) & 1);
  178. grsave = (byte1 & 0x7f);
  179. rlen = unw_decode_uleb128 (&dp);
  180. UNW_DEC_PROLOGUE_GR(R2, rlen, mask, grsave, arg);
  181. return dp;
  182. }
  183. static inline unsigned char *
  184. unw_decode_r3 (unsigned char *dp, unsigned char code, void *arg)
  185. {
  186. unw_word rlen;
  187. rlen = unw_decode_uleb128 (&dp);
  188. UNW_DEC_PROLOGUE(R3, ((code & 0x3) == 1), rlen, arg);
  189. return dp;
  190. }
  191. static inline unsigned char *
  192. unw_decode_p1 (unsigned char *dp, unsigned char code, void *arg)
  193. {
  194. unsigned char brmask = (code & 0x1f);
  195. UNW_DEC_BR_MEM(P1, brmask, arg);
  196. return dp;
  197. }
  198. static inline unsigned char *
  199. unw_decode_p2_p5 (unsigned char *dp, unsigned char code, void *arg)
  200. {
  201. if ((code & 0x10) == 0)
  202. {
  203. unsigned char byte1 = *dp++;
  204. UNW_DEC_BR_GR(P2, ((code & 0xf) << 1) | ((byte1 >> 7) & 1),
  205. (byte1 & 0x7f), arg);
  206. }
  207. else if ((code & 0x08) == 0)
  208. {
  209. unsigned char byte1 = *dp++, r, dst;
  210. r = ((code & 0x7) << 1) | ((byte1 >> 7) & 1);
  211. dst = (byte1 & 0x7f);
  212. switch (r)
  213. {
  214. case 0: UNW_DEC_REG_GR(P3, UNW_REG_PSP, dst, arg); break;
  215. case 1: UNW_DEC_REG_GR(P3, UNW_REG_RP, dst, arg); break;
  216. case 2: UNW_DEC_REG_GR(P3, UNW_REG_PFS, dst, arg); break;
  217. case 3: UNW_DEC_REG_GR(P3, UNW_REG_PR, dst, arg); break;
  218. case 4: UNW_DEC_REG_GR(P3, UNW_REG_UNAT, dst, arg); break;
  219. case 5: UNW_DEC_REG_GR(P3, UNW_REG_LC, dst, arg); break;
  220. case 6: UNW_DEC_RP_BR(P3, dst, arg); break;
  221. case 7: UNW_DEC_REG_GR(P3, UNW_REG_RNAT, dst, arg); break;
  222. case 8: UNW_DEC_REG_GR(P3, UNW_REG_BSP, dst, arg); break;
  223. case 9: UNW_DEC_REG_GR(P3, UNW_REG_BSPSTORE, dst, arg); break;
  224. case 10: UNW_DEC_REG_GR(P3, UNW_REG_FPSR, dst, arg); break;
  225. case 11: UNW_DEC_PRIUNAT_GR(P3, dst, arg); break;
  226. default: UNW_DEC_BAD_CODE(r); break;
  227. }
  228. }
  229. else if ((code & 0x7) == 0)
  230. UNW_DEC_SPILL_MASK(P4, dp, arg);
  231. else if ((code & 0x7) == 1)
  232. {
  233. unw_word grmask, frmask, byte1, byte2, byte3;
  234. byte1 = *dp++; byte2 = *dp++; byte3 = *dp++;
  235. grmask = ((byte1 >> 4) & 0xf);
  236. frmask = ((byte1 & 0xf) << 16) | (byte2 << 8) | byte3;
  237. UNW_DEC_FRGR_MEM(P5, grmask, frmask, arg);
  238. }
  239. else
  240. UNW_DEC_BAD_CODE(code);
  241. return dp;
  242. }
  243. static inline unsigned char *
  244. unw_decode_p6 (unsigned char *dp, unsigned char code, void *arg)
  245. {
  246. int gregs = (code & 0x10) != 0;
  247. unsigned char mask = (code & 0x0f);
  248. if (gregs)
  249. UNW_DEC_GR_MEM(P6, mask, arg);
  250. else
  251. UNW_DEC_FR_MEM(P6, mask, arg);
  252. return dp;
  253. }
  254. static inline unsigned char *
  255. unw_decode_p7_p10 (unsigned char *dp, unsigned char code, void *arg)
  256. {
  257. unsigned char r, byte1, byte2;
  258. unw_word t, size;
  259. if ((code & 0x10) == 0)
  260. {
  261. r = (code & 0xf);
  262. t = unw_decode_uleb128 (&dp);
  263. switch (r)
  264. {
  265. case 0:
  266. size = unw_decode_uleb128 (&dp);
  267. UNW_DEC_MEM_STACK_F(P7, t, size, arg);
  268. break;
  269. case 1: UNW_DEC_MEM_STACK_V(P7, t, arg); break;
  270. case 2: UNW_DEC_SPILL_BASE(P7, t, arg); break;
  271. case 3: UNW_DEC_REG_SPREL(P7, UNW_REG_PSP, t, arg); break;
  272. case 4: UNW_DEC_REG_WHEN(P7, UNW_REG_RP, t, arg); break;
  273. case 5: UNW_DEC_REG_PSPREL(P7, UNW_REG_RP, t, arg); break;
  274. case 6: UNW_DEC_REG_WHEN(P7, UNW_REG_PFS, t, arg); break;
  275. case 7: UNW_DEC_REG_PSPREL(P7, UNW_REG_PFS, t, arg); break;
  276. case 8: UNW_DEC_REG_WHEN(P7, UNW_REG_PR, t, arg); break;
  277. case 9: UNW_DEC_REG_PSPREL(P7, UNW_REG_PR, t, arg); break;
  278. case 10: UNW_DEC_REG_WHEN(P7, UNW_REG_LC, t, arg); break;
  279. case 11: UNW_DEC_REG_PSPREL(P7, UNW_REG_LC, t, arg); break;
  280. case 12: UNW_DEC_REG_WHEN(P7, UNW_REG_UNAT, t, arg); break;
  281. case 13: UNW_DEC_REG_PSPREL(P7, UNW_REG_UNAT, t, arg); break;
  282. case 14: UNW_DEC_REG_WHEN(P7, UNW_REG_FPSR, t, arg); break;
  283. case 15: UNW_DEC_REG_PSPREL(P7, UNW_REG_FPSR, t, arg); break;
  284. default: UNW_DEC_BAD_CODE(r); break;
  285. }
  286. }
  287. else
  288. {
  289. switch (code & 0xf)
  290. {
  291. case 0x0: /* p8 */
  292. {
  293. r = *dp++;
  294. t = unw_decode_uleb128 (&dp);
  295. switch (r)
  296. {
  297. case 1: UNW_DEC_REG_SPREL(P8, UNW_REG_RP, t, arg); break;
  298. case 2: UNW_DEC_REG_SPREL(P8, UNW_REG_PFS, t, arg); break;
  299. case 3: UNW_DEC_REG_SPREL(P8, UNW_REG_PR, t, arg); break;
  300. case 4: UNW_DEC_REG_SPREL(P8, UNW_REG_LC, t, arg); break;
  301. case 5: UNW_DEC_REG_SPREL(P8, UNW_REG_UNAT, t, arg); break;
  302. case 6: UNW_DEC_REG_SPREL(P8, UNW_REG_FPSR, t, arg); break;
  303. case 7: UNW_DEC_REG_WHEN(P8, UNW_REG_BSP, t, arg); break;
  304. case 8: UNW_DEC_REG_PSPREL(P8, UNW_REG_BSP, t, arg); break;
  305. case 9: UNW_DEC_REG_SPREL(P8, UNW_REG_BSP, t, arg); break;
  306. case 10: UNW_DEC_REG_WHEN(P8, UNW_REG_BSPSTORE, t, arg); break;
  307. case 11: UNW_DEC_REG_PSPREL(P8, UNW_REG_BSPSTORE, t, arg); break;
  308. case 12: UNW_DEC_REG_SPREL(P8, UNW_REG_BSPSTORE, t, arg); break;
  309. case 13: UNW_DEC_REG_WHEN(P8, UNW_REG_RNAT, t, arg); break;
  310. case 14: UNW_DEC_REG_PSPREL(P8, UNW_REG_RNAT, t, arg); break;
  311. case 15: UNW_DEC_REG_SPREL(P8, UNW_REG_RNAT, t, arg); break;
  312. case 16: UNW_DEC_PRIUNAT_WHEN_GR(P8, t, arg); break;
  313. case 17: UNW_DEC_PRIUNAT_PSPREL(P8, t, arg); break;
  314. case 18: UNW_DEC_PRIUNAT_SPREL(P8, t, arg); break;
  315. case 19: UNW_DEC_PRIUNAT_WHEN_MEM(P8, t, arg); break;
  316. default: UNW_DEC_BAD_CODE(r); break;
  317. }
  318. }
  319. break;
  320. case 0x1:
  321. byte1 = *dp++; byte2 = *dp++;
  322. UNW_DEC_GR_GR(P9, (byte1 & 0xf), (byte2 & 0x7f), arg);
  323. break;
  324. case 0xf: /* p10 */
  325. byte1 = *dp++; byte2 = *dp++;
  326. UNW_DEC_ABI(P10, byte1, byte2, arg);
  327. break;
  328. case 0x9:
  329. return unw_decode_x1 (dp, code, arg);
  330. case 0xa:
  331. return unw_decode_x2 (dp, code, arg);
  332. case 0xb:
  333. return unw_decode_x3 (dp, code, arg);
  334. case 0xc:
  335. return unw_decode_x4 (dp, code, arg);
  336. default:
  337. UNW_DEC_BAD_CODE(code);
  338. break;
  339. }
  340. }
  341. return dp;
  342. }
  343. static inline unsigned char *
  344. unw_decode_b1 (unsigned char *dp, unsigned char code, void *arg)
  345. {
  346. unw_word label = (code & 0x1f);
  347. if ((code & 0x20) != 0)
  348. UNW_DEC_COPY_STATE(B1, label, arg);
  349. else
  350. UNW_DEC_LABEL_STATE(B1, label, arg);
  351. return dp;
  352. }
  353. static inline unsigned char *
  354. unw_decode_b2 (unsigned char *dp, unsigned char code, void *arg)
  355. {
  356. unw_word t;
  357. t = unw_decode_uleb128 (&dp);
  358. UNW_DEC_EPILOGUE(B2, t, (code & 0x1f), arg);
  359. return dp;
  360. }
  361. static inline unsigned char *
  362. unw_decode_b3_x4 (unsigned char *dp, unsigned char code, void *arg)
  363. {
  364. unw_word t, ecount, label;
  365. if ((code & 0x10) == 0)
  366. {
  367. t = unw_decode_uleb128 (&dp);
  368. ecount = unw_decode_uleb128 (&dp);
  369. UNW_DEC_EPILOGUE(B3, t, ecount, arg);
  370. }
  371. else if ((code & 0x07) == 0)
  372. {
  373. label = unw_decode_uleb128 (&dp);
  374. if ((code & 0x08) != 0)
  375. UNW_DEC_COPY_STATE(B4, label, arg);
  376. else
  377. UNW_DEC_LABEL_STATE(B4, label, arg);
  378. }
  379. else
  380. switch (code & 0x7)
  381. {
  382. case 1: return unw_decode_x1 (dp, code, arg);
  383. case 2: return unw_decode_x2 (dp, code, arg);
  384. case 3: return unw_decode_x3 (dp, code, arg);
  385. case 4: return unw_decode_x4 (dp, code, arg);
  386. default: UNW_DEC_BAD_CODE(code); break;
  387. }
  388. return dp;
  389. }
  390. typedef unsigned char *(*unw_decoder) (unsigned char *, unsigned char, void *);
  391. /*
  392. * Decode one descriptor and return address of next descriptor.
  393. */
  394. static inline unsigned char *
  395. unw_decode (unsigned char *dp, int inside_body, void *arg)
  396. {
  397. unsigned char code, primary;
  398. code = *dp++;
  399. primary = code >> 5;
  400. if (primary < 2)
  401. dp = unw_decode_r1 (dp, code, arg);
  402. else if (primary == 2)
  403. dp = unw_decode_r2 (dp, code, arg);
  404. else if (primary == 3)
  405. dp = unw_decode_r3 (dp, code, arg);
  406. else if (inside_body)
  407. switch (primary)
  408. {
  409. case 4:
  410. case 5: dp = unw_decode_b1 (dp, code, arg); break;
  411. case 6: dp = unw_decode_b2 (dp, code, arg); break;
  412. case 7: dp = unw_decode_b3_x4 (dp, code, arg); break;
  413. }
  414. else
  415. switch (primary)
  416. {
  417. case 4: dp = unw_decode_p1 (dp, code, arg); break;
  418. case 5: dp = unw_decode_p2_p5 (dp, code, arg); break;
  419. case 6: dp = unw_decode_p6 (dp, code, arg); break;
  420. case 7: dp = unw_decode_p7_p10 (dp, code, arg); break;
  421. }
  422. return dp;
  423. }