Gparser.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (c) 2003, 2005 Hewlett-Packard Development Company, L.P.
  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. #include <stddef.h>
  22. #include "dwarf_i.h"
  23. #include "libunwind_i.h"
  24. #define alloc_reg_state() (mempool_alloc (&dwarf_reg_state_pool))
  25. #define free_reg_state(rs) (mempool_free (&dwarf_reg_state_pool, rs))
  26. static inline int
  27. read_regnum (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
  28. unw_word_t *valp, void *arg)
  29. {
  30. int ret;
  31. if ((ret = dwarf_read_uleb128 (as, a, addr, valp, arg)) < 0)
  32. return ret;
  33. if (*valp >= DWARF_NUM_PRESERVED_REGS)
  34. {
  35. Debug (1, "Invalid register number %u\n", (unsigned int) *valp);
  36. return -UNW_EBADREG;
  37. }
  38. return 0;
  39. }
  40. static inline void
  41. set_reg (dwarf_state_record_t *sr, unw_word_t regnum, dwarf_where_t where,
  42. unw_word_t val)
  43. {
  44. sr->rs_current.reg[regnum].where = where;
  45. sr->rs_current.reg[regnum].val = val;
  46. }
  47. /* Run a CFI program to update the register state. */
  48. static int
  49. run_cfi_program (struct dwarf_cursor *c, dwarf_state_record_t *sr,
  50. unw_word_t ip, unw_word_t *addr, unw_word_t end_addr,
  51. struct dwarf_cie_info *dci)
  52. {
  53. unw_word_t curr_ip, operand = 0, regnum, val, len, fde_encoding;
  54. dwarf_reg_state_t *rs_stack = NULL, *new_rs, *old_rs;
  55. unw_addr_space_t as;
  56. unw_accessors_t *a;
  57. uint8_t u8, op;
  58. uint16_t u16;
  59. uint32_t u32;
  60. void *arg;
  61. int ret;
  62. as = c->as;
  63. arg = c->as_arg;
  64. if (c->pi.flags & UNW_PI_FLAG_DEBUG_FRAME)
  65. {
  66. /* .debug_frame CFI is stored in local address space. */
  67. as = unw_local_addr_space;
  68. arg = NULL;
  69. }
  70. a = unw_get_accessors (as);
  71. curr_ip = c->pi.start_ip;
  72. /* Process everything up to and including the current 'ip',
  73. including all the DW_CFA_advance_loc instructions. See
  74. 'c->use_prev_instr' use in 'fetch_proc_info' for details. */
  75. while (curr_ip <= ip && *addr < end_addr)
  76. {
  77. if ((ret = dwarf_readu8 (as, a, addr, &op, arg)) < 0)
  78. return ret;
  79. if (op & DWARF_CFA_OPCODE_MASK)
  80. {
  81. operand = op & DWARF_CFA_OPERAND_MASK;
  82. op &= ~DWARF_CFA_OPERAND_MASK;
  83. }
  84. switch ((dwarf_cfa_t) op)
  85. {
  86. case DW_CFA_advance_loc:
  87. curr_ip += operand * dci->code_align;
  88. Debug (15, "CFA_advance_loc to 0x%lx\n", (long) curr_ip);
  89. break;
  90. case DW_CFA_advance_loc1:
  91. if ((ret = dwarf_readu8 (as, a, addr, &u8, arg)) < 0)
  92. goto fail;
  93. curr_ip += u8 * dci->code_align;
  94. Debug (15, "CFA_advance_loc1 to 0x%lx\n", (long) curr_ip);
  95. break;
  96. case DW_CFA_advance_loc2:
  97. if ((ret = dwarf_readu16 (as, a, addr, &u16, arg)) < 0)
  98. goto fail;
  99. curr_ip += u16 * dci->code_align;
  100. Debug (15, "CFA_advance_loc2 to 0x%lx\n", (long) curr_ip);
  101. break;
  102. case DW_CFA_advance_loc4:
  103. if ((ret = dwarf_readu32 (as, a, addr, &u32, arg)) < 0)
  104. goto fail;
  105. curr_ip += u32 * dci->code_align;
  106. Debug (15, "CFA_advance_loc4 to 0x%lx\n", (long) curr_ip);
  107. break;
  108. case DW_CFA_MIPS_advance_loc8:
  109. #ifdef UNW_TARGET_MIPS
  110. {
  111. uint64_t u64;
  112. if ((ret = dwarf_readu64 (as, a, addr, &u64, arg)) < 0)
  113. goto fail;
  114. curr_ip += u64 * dci->code_align;
  115. Debug (15, "CFA_MIPS_advance_loc8\n");
  116. break;
  117. }
  118. #else
  119. Debug (1, "DW_CFA_MIPS_advance_loc8 on non-MIPS target\n");
  120. ret = -UNW_EINVAL;
  121. goto fail;
  122. #endif
  123. case DW_CFA_offset:
  124. regnum = operand;
  125. if (regnum >= DWARF_NUM_PRESERVED_REGS)
  126. {
  127. Debug (1, "Invalid register number %u in DW_cfa_OFFSET\n",
  128. (unsigned int) regnum);
  129. ret = -UNW_EBADREG;
  130. goto fail;
  131. }
  132. if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
  133. goto fail;
  134. set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
  135. Debug (15, "CFA_offset r%lu at cfa+0x%lx\n",
  136. (long) regnum, (long) (val * dci->data_align));
  137. break;
  138. case DW_CFA_offset_extended:
  139. if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  140. || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
  141. goto fail;
  142. set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
  143. Debug (15, "CFA_offset_extended r%lu at cf+0x%lx\n",
  144. (long) regnum, (long) (val * dci->data_align));
  145. break;
  146. case DW_CFA_offset_extended_sf:
  147. if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  148. || ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0))
  149. goto fail;
  150. set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
  151. Debug (15, "CFA_offset_extended_sf r%lu at cf+0x%lx\n",
  152. (long) regnum, (long) (val * dci->data_align));
  153. break;
  154. case DW_CFA_restore:
  155. regnum = operand;
  156. if (regnum >= DWARF_NUM_PRESERVED_REGS)
  157. {
  158. Debug (1, "Invalid register number %u in DW_CFA_restore\n",
  159. (unsigned int) regnum);
  160. ret = -UNW_EINVAL;
  161. goto fail;
  162. }
  163. sr->rs_current.reg[regnum] = sr->rs_initial.reg[regnum];
  164. Debug (15, "CFA_restore r%lu\n", (long) regnum);
  165. break;
  166. case DW_CFA_restore_extended:
  167. if ((ret = dwarf_read_uleb128 (as, a, addr, &regnum, arg)) < 0)
  168. goto fail;
  169. if (regnum >= DWARF_NUM_PRESERVED_REGS)
  170. {
  171. Debug (1, "Invalid register number %u in "
  172. "DW_CFA_restore_extended\n", (unsigned int) regnum);
  173. ret = -UNW_EINVAL;
  174. goto fail;
  175. }
  176. sr->rs_current.reg[regnum] = sr->rs_initial.reg[regnum];
  177. Debug (15, "CFA_restore_extended r%lu\n", (long) regnum);
  178. break;
  179. case DW_CFA_nop:
  180. break;
  181. case DW_CFA_set_loc:
  182. fde_encoding = dci->fde_encoding;
  183. if ((ret = dwarf_read_encoded_pointer (as, a, addr, fde_encoding,
  184. &c->pi, &curr_ip,
  185. arg)) < 0)
  186. goto fail;
  187. Debug (15, "CFA_set_loc to 0x%lx\n", (long) curr_ip);
  188. break;
  189. case DW_CFA_undefined:
  190. if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  191. goto fail;
  192. set_reg (sr, regnum, DWARF_WHERE_UNDEF, 0);
  193. Debug (15, "CFA_undefined r%lu\n", (long) regnum);
  194. break;
  195. case DW_CFA_same_value:
  196. if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  197. goto fail;
  198. set_reg (sr, regnum, DWARF_WHERE_SAME, 0);
  199. Debug (15, "CFA_same_value r%lu\n", (long) regnum);
  200. break;
  201. case DW_CFA_register:
  202. if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  203. || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
  204. goto fail;
  205. set_reg (sr, regnum, DWARF_WHERE_REG, val);
  206. Debug (15, "CFA_register r%lu to r%lu\n", (long) regnum, (long) val);
  207. break;
  208. case DW_CFA_remember_state:
  209. new_rs = alloc_reg_state ();
  210. if (!new_rs)
  211. {
  212. Debug (1, "Out of memory in DW_CFA_remember_state\n");
  213. ret = -UNW_ENOMEM;
  214. goto fail;
  215. }
  216. memcpy (new_rs->reg, sr->rs_current.reg, sizeof (new_rs->reg));
  217. new_rs->next = rs_stack;
  218. rs_stack = new_rs;
  219. Debug (15, "CFA_remember_state\n");
  220. break;
  221. case DW_CFA_restore_state:
  222. if (!rs_stack)
  223. {
  224. Debug (1, "register-state stack underflow\n");
  225. ret = -UNW_EINVAL;
  226. goto fail;
  227. }
  228. memcpy (&sr->rs_current.reg, &rs_stack->reg, sizeof (rs_stack->reg));
  229. old_rs = rs_stack;
  230. rs_stack = rs_stack->next;
  231. free_reg_state (old_rs);
  232. Debug (15, "CFA_restore_state\n");
  233. break;
  234. case DW_CFA_def_cfa:
  235. if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  236. || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
  237. goto fail;
  238. set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
  239. set_reg (sr, DWARF_CFA_OFF_COLUMN, 0, val); /* NOT factored! */
  240. Debug (15, "CFA_def_cfa r%lu+0x%lx\n", (long) regnum, (long) val);
  241. break;
  242. case DW_CFA_def_cfa_sf:
  243. if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  244. || ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0))
  245. goto fail;
  246. set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
  247. set_reg (sr, DWARF_CFA_OFF_COLUMN, 0,
  248. val * dci->data_align); /* factored! */
  249. Debug (15, "CFA_def_cfa_sf r%lu+0x%lx\n",
  250. (long) regnum, (long) (val * dci->data_align));
  251. break;
  252. case DW_CFA_def_cfa_register:
  253. if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  254. goto fail;
  255. set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
  256. Debug (15, "CFA_def_cfa_register r%lu\n", (long) regnum);
  257. break;
  258. case DW_CFA_def_cfa_offset:
  259. if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
  260. goto fail;
  261. set_reg (sr, DWARF_CFA_OFF_COLUMN, 0, val); /* NOT factored! */
  262. Debug (15, "CFA_def_cfa_offset 0x%lx\n", (long) val);
  263. break;
  264. case DW_CFA_def_cfa_offset_sf:
  265. if ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0)
  266. goto fail;
  267. set_reg (sr, DWARF_CFA_OFF_COLUMN, 0,
  268. val * dci->data_align); /* factored! */
  269. Debug (15, "CFA_def_cfa_offset_sf 0x%lx\n",
  270. (long) (val * dci->data_align));
  271. break;
  272. case DW_CFA_def_cfa_expression:
  273. /* Save the address of the DW_FORM_block for later evaluation. */
  274. set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_EXPR, *addr);
  275. if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
  276. goto fail;
  277. Debug (15, "CFA_def_cfa_expr @ 0x%lx [%lu bytes]\n",
  278. (long) *addr, (long) len);
  279. *addr += len;
  280. break;
  281. case DW_CFA_expression:
  282. if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  283. goto fail;
  284. /* Save the address of the DW_FORM_block for later evaluation. */
  285. set_reg (sr, regnum, DWARF_WHERE_EXPR, *addr);
  286. if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
  287. goto fail;
  288. Debug (15, "CFA_expression r%lu @ 0x%lx [%lu bytes]\n",
  289. (long) regnum, (long) addr, (long) len);
  290. *addr += len;
  291. break;
  292. case DW_CFA_GNU_args_size:
  293. if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
  294. goto fail;
  295. sr->args_size = val;
  296. Debug (15, "CFA_GNU_args_size %lu\n", (long) val);
  297. break;
  298. case DW_CFA_GNU_negative_offset_extended:
  299. /* A comment in GCC says that this is obsoleted by
  300. DW_CFA_offset_extended_sf, but that it's used by older
  301. PowerPC code. */
  302. if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
  303. || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
  304. goto fail;
  305. set_reg (sr, regnum, DWARF_WHERE_CFAREL, -(val * dci->data_align));
  306. Debug (15, "CFA_GNU_negative_offset_extended cfa+0x%lx\n",
  307. (long) -(val * dci->data_align));
  308. break;
  309. case DW_CFA_GNU_window_save:
  310. #ifdef UNW_TARGET_SPARC
  311. /* This is a special CFA to handle all 16 windowed registers
  312. on SPARC. */
  313. for (regnum = 16; regnum < 32; ++regnum)
  314. set_reg (sr, regnum, DWARF_WHERE_CFAREL,
  315. (regnum - 16) * sizeof (unw_word_t));
  316. Debug (15, "CFA_GNU_window_save\n");
  317. break;
  318. #else
  319. /* FALL THROUGH */
  320. #endif
  321. case DW_CFA_lo_user:
  322. case DW_CFA_hi_user:
  323. Debug (1, "Unexpected CFA opcode 0x%x\n", op);
  324. ret = -UNW_EINVAL;
  325. goto fail;
  326. }
  327. }
  328. ret = 0;
  329. fail:
  330. /* Free the register-state stack, if not empty already. */
  331. while (rs_stack)
  332. {
  333. old_rs = rs_stack;
  334. rs_stack = rs_stack->next;
  335. free_reg_state (old_rs);
  336. }
  337. return ret;
  338. }
  339. static int
  340. fetch_proc_info (struct dwarf_cursor *c, unw_word_t ip, int need_unwind_info)
  341. {
  342. int ret, dynamic = 1;
  343. /* The 'ip' can point either to the previous or next instruction
  344. depending on what type of frame we have: normal call or a place
  345. to resume execution (e.g. after signal frame).
  346. For a normal call frame we need to back up so we point within the
  347. call itself; this is important because a) the call might be the
  348. very last instruction of the function and the edge of the FDE,
  349. and b) so that run_cfi_program() runs locations up to the call
  350. but not more.
  351. For execution resume, we need to do the exact opposite and look
  352. up using the current 'ip' value. That is where execution will
  353. continue, and it's important we get this right, as 'ip' could be
  354. right at the function entry and hence FDE edge, or at instruction
  355. that manipulates CFA (push/pop). */
  356. if (c->use_prev_instr)
  357. --ip;
  358. if (c->pi_valid && !need_unwind_info)
  359. return 0;
  360. memset (&c->pi, 0, sizeof (c->pi));
  361. /* check dynamic info first --- it overrides everything else */
  362. ret = unwi_find_dynamic_proc_info (c->as, ip, &c->pi, need_unwind_info,
  363. c->as_arg);
  364. if (ret == -UNW_ENOINFO)
  365. {
  366. dynamic = 0;
  367. if ((ret = tdep_find_proc_info (c, ip, need_unwind_info)) < 0)
  368. return ret;
  369. }
  370. if (c->pi.format != UNW_INFO_FORMAT_DYNAMIC
  371. && c->pi.format != UNW_INFO_FORMAT_TABLE
  372. && c->pi.format != UNW_INFO_FORMAT_REMOTE_TABLE)
  373. return -UNW_ENOINFO;
  374. c->pi_valid = 1;
  375. c->pi_is_dynamic = dynamic;
  376. /* Let system/machine-dependent code determine frame-specific attributes. */
  377. if (ret >= 0)
  378. tdep_fetch_frame (c, ip, need_unwind_info);
  379. /* Update use_prev_instr for the next frame. */
  380. if (need_unwind_info)
  381. {
  382. assert(c->pi.unwind_info);
  383. struct dwarf_cie_info *dci = c->pi.unwind_info;
  384. c->use_prev_instr = ! dci->signal_frame;
  385. }
  386. return ret;
  387. }
  388. static int
  389. parse_dynamic (struct dwarf_cursor *c, unw_word_t ip, dwarf_state_record_t *sr)
  390. {
  391. Debug (1, "Not yet implemented\n");
  392. #if 0
  393. /* Don't forget to set the ret_addr_column! */
  394. c->ret_addr_column = XXX;
  395. #endif
  396. return -UNW_ENOINFO;
  397. }
  398. static inline void
  399. put_unwind_info (struct dwarf_cursor *c, unw_proc_info_t *pi)
  400. {
  401. if (!c->pi_valid)
  402. return;
  403. if (c->pi_is_dynamic)
  404. unwi_put_dynamic_unwind_info (c->as, pi, c->as_arg);
  405. else if (pi->unwind_info)
  406. {
  407. mempool_free (&dwarf_cie_info_pool, pi->unwind_info);
  408. pi->unwind_info = NULL;
  409. }
  410. }
  411. static inline int
  412. parse_fde (struct dwarf_cursor *c, unw_word_t ip, dwarf_state_record_t *sr)
  413. {
  414. struct dwarf_cie_info *dci;
  415. unw_word_t addr;
  416. int ret;
  417. dci = c->pi.unwind_info;
  418. c->ret_addr_column = dci->ret_addr_column;
  419. addr = dci->cie_instr_start;
  420. if ((ret = run_cfi_program (c, sr, ~(unw_word_t) 0, &addr,
  421. dci->cie_instr_end, dci)) < 0)
  422. return ret;
  423. memcpy (&sr->rs_initial, &sr->rs_current, sizeof (sr->rs_initial));
  424. addr = dci->fde_instr_start;
  425. if ((ret = run_cfi_program (c, sr, ip, &addr, dci->fde_instr_end, dci)) < 0)
  426. return ret;
  427. return 0;
  428. }
  429. static inline void
  430. flush_rs_cache (struct dwarf_rs_cache *cache)
  431. {
  432. int i;
  433. cache->lru_head = DWARF_UNW_CACHE_SIZE - 1;
  434. cache->lru_tail = 0;
  435. for (i = 0; i < DWARF_UNW_CACHE_SIZE; ++i)
  436. {
  437. if (i > 0)
  438. cache->buckets[i].lru_chain = (i - 1);
  439. cache->buckets[i].coll_chain = -1;
  440. cache->buckets[i].ip = 0;
  441. cache->buckets[i].valid = 0;
  442. }
  443. for (i = 0; i<DWARF_UNW_HASH_SIZE; ++i)
  444. cache->hash[i] = -1;
  445. }
  446. static inline struct dwarf_rs_cache *
  447. get_rs_cache (unw_addr_space_t as, intrmask_t *saved_maskp)
  448. {
  449. struct dwarf_rs_cache *cache = &as->global_cache;
  450. unw_caching_policy_t caching = as->caching_policy;
  451. if (caching == UNW_CACHE_NONE)
  452. return NULL;
  453. if (likely (caching == UNW_CACHE_GLOBAL))
  454. {
  455. Debug (16, "%s: acquiring lock\n", __FUNCTION__);
  456. lock_acquire (&cache->lock, *saved_maskp);
  457. }
  458. if (atomic_read (&as->cache_generation) != atomic_read (&cache->generation))
  459. {
  460. flush_rs_cache (cache);
  461. cache->generation = as->cache_generation;
  462. }
  463. return cache;
  464. }
  465. static inline void
  466. put_rs_cache (unw_addr_space_t as, struct dwarf_rs_cache *cache,
  467. intrmask_t *saved_maskp)
  468. {
  469. assert (as->caching_policy != UNW_CACHE_NONE);
  470. Debug (16, "unmasking signals/interrupts and releasing lock\n");
  471. if (likely (as->caching_policy == UNW_CACHE_GLOBAL))
  472. lock_release (&cache->lock, *saved_maskp);
  473. }
  474. static inline unw_hash_index_t
  475. hash (unw_word_t ip)
  476. {
  477. /* based on (sqrt(5)/2-1)*2^64 */
  478. # define magic ((unw_word_t) 0x9e3779b97f4a7c16ULL)
  479. return ip * magic >> ((sizeof(unw_word_t) * 8) - DWARF_LOG_UNW_HASH_SIZE);
  480. }
  481. static inline long
  482. cache_match (dwarf_reg_state_t *rs, unw_word_t ip)
  483. {
  484. if (rs->valid && (ip == rs->ip))
  485. return 1;
  486. return 0;
  487. }
  488. static dwarf_reg_state_t *
  489. rs_lookup (struct dwarf_rs_cache *cache, struct dwarf_cursor *c)
  490. {
  491. dwarf_reg_state_t *rs = cache->buckets + c->hint;
  492. unsigned short index;
  493. unw_word_t ip;
  494. ip = c->ip;
  495. if (cache_match (rs, ip))
  496. return rs;
  497. index = cache->hash[hash (ip)];
  498. if (index >= DWARF_UNW_CACHE_SIZE)
  499. return 0;
  500. rs = cache->buckets + index;
  501. while (1)
  502. {
  503. if (cache_match (rs, ip))
  504. {
  505. /* update hint; no locking needed: single-word writes are atomic */
  506. c->hint = cache->buckets[c->prev_rs].hint =
  507. (rs - cache->buckets);
  508. return rs;
  509. }
  510. if (rs->coll_chain >= DWARF_UNW_HASH_SIZE)
  511. return 0;
  512. rs = cache->buckets + rs->coll_chain;
  513. }
  514. }
  515. static inline dwarf_reg_state_t *
  516. rs_new (struct dwarf_rs_cache *cache, struct dwarf_cursor * c)
  517. {
  518. dwarf_reg_state_t *rs, *prev, *tmp;
  519. unw_hash_index_t index;
  520. unsigned short head;
  521. head = cache->lru_head;
  522. rs = cache->buckets + head;
  523. cache->lru_head = rs->lru_chain;
  524. /* re-insert rs at the tail of the LRU chain: */
  525. cache->buckets[cache->lru_tail].lru_chain = head;
  526. cache->lru_tail = head;
  527. /* remove the old rs from the hash table (if it's there): */
  528. if (rs->ip)
  529. {
  530. index = hash (rs->ip);
  531. tmp = cache->buckets + cache->hash[index];
  532. prev = 0;
  533. while (1)
  534. {
  535. if (tmp == rs)
  536. {
  537. if (prev)
  538. prev->coll_chain = tmp->coll_chain;
  539. else
  540. cache->hash[index] = tmp->coll_chain;
  541. break;
  542. }
  543. else
  544. prev = tmp;
  545. if (tmp->coll_chain >= DWARF_UNW_CACHE_SIZE)
  546. /* old rs wasn't in the hash-table */
  547. break;
  548. tmp = cache->buckets + tmp->coll_chain;
  549. }
  550. }
  551. /* enter new rs in the hash table */
  552. index = hash (c->ip);
  553. rs->coll_chain = cache->hash[index];
  554. cache->hash[index] = rs - cache->buckets;
  555. rs->hint = 0;
  556. rs->ip = c->ip;
  557. rs->valid = 1;
  558. rs->ret_addr_column = c->ret_addr_column;
  559. rs->signal_frame = 0;
  560. tdep_cache_frame (c, rs);
  561. return rs;
  562. }
  563. static int
  564. create_state_record_for (struct dwarf_cursor *c, dwarf_state_record_t *sr,
  565. unw_word_t ip)
  566. {
  567. int i, ret;
  568. assert (c->pi_valid);
  569. memset (sr, 0, sizeof (*sr));
  570. for (i = 0; i < DWARF_NUM_PRESERVED_REGS + 2; ++i)
  571. set_reg (sr, i, DWARF_WHERE_SAME, 0);
  572. switch (c->pi.format)
  573. {
  574. case UNW_INFO_FORMAT_TABLE:
  575. case UNW_INFO_FORMAT_REMOTE_TABLE:
  576. ret = parse_fde (c, ip, sr);
  577. break;
  578. case UNW_INFO_FORMAT_DYNAMIC:
  579. ret = parse_dynamic (c, ip, sr);
  580. break;
  581. default:
  582. Debug (1, "Unexpected unwind-info format %d\n", c->pi.format);
  583. ret = -UNW_EINVAL;
  584. }
  585. return ret;
  586. }
  587. static inline int
  588. eval_location_expr (struct dwarf_cursor *c, unw_addr_space_t as,
  589. unw_accessors_t *a, unw_word_t addr,
  590. dwarf_loc_t *locp, void *arg)
  591. {
  592. int ret, is_register;
  593. unw_word_t len, val;
  594. /* read the length of the expression: */
  595. if ((ret = dwarf_read_uleb128 (as, a, &addr, &len, arg)) < 0)
  596. return ret;
  597. /* evaluate the expression: */
  598. if ((ret = dwarf_eval_expr (c, &addr, len, &val, &is_register)) < 0)
  599. return ret;
  600. if (is_register)
  601. *locp = DWARF_REG_LOC (c, dwarf_to_unw_regnum (val));
  602. else
  603. *locp = DWARF_MEM_LOC (c, val);
  604. return 0;
  605. }
  606. static int
  607. apply_reg_state (struct dwarf_cursor *c, struct dwarf_reg_state *rs)
  608. {
  609. unw_word_t regnum, addr, cfa, ip;
  610. unw_word_t prev_ip, prev_cfa;
  611. unw_addr_space_t as;
  612. dwarf_loc_t cfa_loc;
  613. unw_accessors_t *a;
  614. int i, ret;
  615. void *arg;
  616. prev_ip = c->ip;
  617. prev_cfa = c->cfa;
  618. as = c->as;
  619. arg = c->as_arg;
  620. a = unw_get_accessors (as);
  621. /* Evaluate the CFA first, because it may be referred to by other
  622. expressions. */
  623. if (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_REG)
  624. {
  625. /* CFA is equal to [reg] + offset: */
  626. /* As a special-case, if the stack-pointer is the CFA and the
  627. stack-pointer wasn't saved, popping the CFA implicitly pops
  628. the stack-pointer as well. */
  629. if ((rs->reg[DWARF_CFA_REG_COLUMN].val == UNW_TDEP_SP)
  630. && (UNW_TDEP_SP < ARRAY_SIZE(rs->reg))
  631. && (rs->reg[UNW_TDEP_SP].where == DWARF_WHERE_SAME))
  632. cfa = c->cfa;
  633. else
  634. {
  635. regnum = dwarf_to_unw_regnum (rs->reg[DWARF_CFA_REG_COLUMN].val);
  636. if ((ret = unw_get_reg ((unw_cursor_t *) c, regnum, &cfa)) < 0)
  637. return ret;
  638. }
  639. cfa += rs->reg[DWARF_CFA_OFF_COLUMN].val;
  640. }
  641. else
  642. {
  643. /* CFA is equal to EXPR: */
  644. assert (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_EXPR);
  645. addr = rs->reg[DWARF_CFA_REG_COLUMN].val;
  646. if ((ret = eval_location_expr (c, as, a, addr, &cfa_loc, arg)) < 0)
  647. return ret;
  648. /* the returned location better be a memory location... */
  649. if (DWARF_IS_REG_LOC (cfa_loc))
  650. return -UNW_EBADFRAME;
  651. cfa = DWARF_GET_LOC (cfa_loc);
  652. }
  653. for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
  654. {
  655. switch ((dwarf_where_t) rs->reg[i].where)
  656. {
  657. case DWARF_WHERE_UNDEF:
  658. c->loc[i] = DWARF_NULL_LOC;
  659. break;
  660. case DWARF_WHERE_SAME:
  661. break;
  662. case DWARF_WHERE_CFAREL:
  663. c->loc[i] = DWARF_MEM_LOC (c, cfa + rs->reg[i].val);
  664. break;
  665. case DWARF_WHERE_REG:
  666. c->loc[i] = DWARF_REG_LOC (c, dwarf_to_unw_regnum (rs->reg[i].val));
  667. break;
  668. case DWARF_WHERE_EXPR:
  669. addr = rs->reg[i].val;
  670. if ((ret = eval_location_expr (c, as, a, addr, c->loc + i, arg)) < 0)
  671. return ret;
  672. break;
  673. }
  674. }
  675. c->cfa = cfa;
  676. /* DWARF spec says undefined return address location means end of stack. */
  677. if (DWARF_IS_NULL_LOC (c->loc[c->ret_addr_column]))
  678. c->ip = 0;
  679. else
  680. {
  681. ret = dwarf_get (c, c->loc[c->ret_addr_column], &ip);
  682. if (ret < 0)
  683. return ret;
  684. c->ip = ip;
  685. }
  686. /* XXX: check for ip to be code_aligned */
  687. if (c->ip == prev_ip && c->cfa == prev_cfa)
  688. {
  689. Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n",
  690. __FUNCTION__, (long) c->ip);
  691. return -UNW_EBADFRAME;
  692. }
  693. if (c->stash_frames)
  694. tdep_stash_frame (c, rs);
  695. return 0;
  696. }
  697. static int
  698. uncached_dwarf_find_save_locs (struct dwarf_cursor *c)
  699. {
  700. dwarf_state_record_t sr;
  701. int ret;
  702. if ((ret = fetch_proc_info (c, c->ip, 1)) < 0)
  703. return ret;
  704. if ((ret = create_state_record_for (c, &sr, c->ip)) < 0)
  705. return ret;
  706. if ((ret = apply_reg_state (c, &sr.rs_current)) < 0)
  707. return ret;
  708. put_unwind_info (c, &c->pi);
  709. return 0;
  710. }
  711. /* The function finds the saved locations and applies the register
  712. state as well. */
  713. HIDDEN int
  714. dwarf_find_save_locs (struct dwarf_cursor *c)
  715. {
  716. dwarf_state_record_t sr;
  717. dwarf_reg_state_t *rs, rs_copy;
  718. struct dwarf_rs_cache *cache;
  719. int ret = 0;
  720. intrmask_t saved_mask;
  721. if (c->as->caching_policy == UNW_CACHE_NONE)
  722. return uncached_dwarf_find_save_locs (c);
  723. cache = get_rs_cache(c->as, &saved_mask);
  724. rs = rs_lookup(cache, c);
  725. if (rs)
  726. {
  727. c->ret_addr_column = rs->ret_addr_column;
  728. c->use_prev_instr = ! rs->signal_frame;
  729. }
  730. else
  731. {
  732. if ((ret = fetch_proc_info (c, c->ip, 1)) < 0 ||
  733. (ret = create_state_record_for (c, &sr, c->ip)) < 0)
  734. {
  735. put_rs_cache (c->as, cache, &saved_mask);
  736. return ret;
  737. }
  738. rs = rs_new (cache, c);
  739. memcpy(rs, &sr.rs_current, offsetof(struct dwarf_reg_state, ip));
  740. cache->buckets[c->prev_rs].hint = rs - cache->buckets;
  741. c->hint = rs->hint;
  742. c->prev_rs = rs - cache->buckets;
  743. put_unwind_info (c, &c->pi);
  744. }
  745. memcpy (&rs_copy, rs, sizeof (rs_copy));
  746. put_rs_cache (c->as, cache, &saved_mask);
  747. tdep_reuse_frame (c, &rs_copy);
  748. if ((ret = apply_reg_state (c, &rs_copy)) < 0)
  749. return ret;
  750. return 0;
  751. }
  752. /* The proc-info must be valid for IP before this routine can be
  753. called. */
  754. HIDDEN int
  755. dwarf_create_state_record (struct dwarf_cursor *c, dwarf_state_record_t *sr)
  756. {
  757. return create_state_record_for (c, sr, c->ip);
  758. }
  759. HIDDEN int
  760. dwarf_make_proc_info (struct dwarf_cursor *c)
  761. {
  762. #if 0
  763. if (c->as->caching_policy == UNW_CACHE_NONE
  764. || get_cached_proc_info (c) < 0)
  765. #endif
  766. /* Lookup it up the slow way... */
  767. return fetch_proc_info (c, c->ip, 0);
  768. return 0;
  769. }