dwarf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * This is an implementation of a DWARF unwinder. Its main purpose is
  9. * for generating stacktrace information. Based on the DWARF 3
  10. * specification from http://www.dwarfstd.org.
  11. *
  12. * TODO:
  13. * - DWARF64 doesn't work.
  14. */
  15. /* #define DEBUG */
  16. #include <linux/kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/list.h>
  19. #include <linux/mm.h>
  20. #include <asm/dwarf.h>
  21. #include <asm/unwinder.h>
  22. #include <asm/sections.h>
  23. #include <asm/unaligned.h>
  24. #include <asm/dwarf.h>
  25. #include <asm/stacktrace.h>
  26. static LIST_HEAD(dwarf_cie_list);
  27. DEFINE_SPINLOCK(dwarf_cie_lock);
  28. static LIST_HEAD(dwarf_fde_list);
  29. DEFINE_SPINLOCK(dwarf_fde_lock);
  30. static struct dwarf_cie *cached_cie;
  31. /*
  32. * Figure out whether we need to allocate some dwarf registers. If dwarf
  33. * registers have already been allocated then we may need to realloc
  34. * them. "reg" is a register number that we need to be able to access
  35. * after this call.
  36. *
  37. * Register numbers start at zero, therefore we need to allocate space
  38. * for "reg" + 1 registers.
  39. */
  40. static void dwarf_frame_alloc_regs(struct dwarf_frame *frame,
  41. unsigned int reg)
  42. {
  43. struct dwarf_reg *regs;
  44. unsigned int num_regs = reg + 1;
  45. size_t new_size;
  46. size_t old_size;
  47. new_size = num_regs * sizeof(*regs);
  48. old_size = frame->num_regs * sizeof(*regs);
  49. /* Fast path: don't allocate any regs if we've already got enough. */
  50. if (frame->num_regs >= num_regs)
  51. return;
  52. regs = kzalloc(new_size, GFP_KERNEL);
  53. if (!regs) {
  54. printk(KERN_WARNING "Unable to allocate DWARF registers\n");
  55. /*
  56. * Let's just bomb hard here, we have no way to
  57. * gracefully recover.
  58. */
  59. BUG();
  60. }
  61. if (frame->regs) {
  62. memcpy(regs, frame->regs, old_size);
  63. kfree(frame->regs);
  64. }
  65. frame->regs = regs;
  66. frame->num_regs = num_regs;
  67. }
  68. /**
  69. * dwarf_read_addr - read dwarf data
  70. * @src: source address of data
  71. * @dst: destination address to store the data to
  72. *
  73. * Read 'n' bytes from @src, where 'n' is the size of an address on
  74. * the native machine. We return the number of bytes read, which
  75. * should always be 'n'. We also have to be careful when reading
  76. * from @src and writing to @dst, because they can be arbitrarily
  77. * aligned. Return 'n' - the number of bytes read.
  78. */
  79. static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
  80. {
  81. u32 val = get_unaligned(src);
  82. put_unaligned(val, dst);
  83. return sizeof(unsigned long *);
  84. }
  85. /**
  86. * dwarf_read_uleb128 - read unsigned LEB128 data
  87. * @addr: the address where the ULEB128 data is stored
  88. * @ret: address to store the result
  89. *
  90. * Decode an unsigned LEB128 encoded datum. The algorithm is taken
  91. * from Appendix C of the DWARF 3 spec. For information on the
  92. * encodings refer to section "7.6 - Variable Length Data". Return
  93. * the number of bytes read.
  94. */
  95. static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
  96. {
  97. unsigned int result;
  98. unsigned char byte;
  99. int shift, count;
  100. result = 0;
  101. shift = 0;
  102. count = 0;
  103. while (1) {
  104. byte = __raw_readb(addr);
  105. addr++;
  106. count++;
  107. result |= (byte & 0x7f) << shift;
  108. shift += 7;
  109. if (!(byte & 0x80))
  110. break;
  111. }
  112. *ret = result;
  113. return count;
  114. }
  115. /**
  116. * dwarf_read_leb128 - read signed LEB128 data
  117. * @addr: the address of the LEB128 encoded data
  118. * @ret: address to store the result
  119. *
  120. * Decode signed LEB128 data. The algorithm is taken from Appendix
  121. * C of the DWARF 3 spec. Return the number of bytes read.
  122. */
  123. static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
  124. {
  125. unsigned char byte;
  126. int result, shift;
  127. int num_bits;
  128. int count;
  129. result = 0;
  130. shift = 0;
  131. count = 0;
  132. while (1) {
  133. byte = __raw_readb(addr);
  134. addr++;
  135. result |= (byte & 0x7f) << shift;
  136. shift += 7;
  137. count++;
  138. if (!(byte & 0x80))
  139. break;
  140. }
  141. /* The number of bits in a signed integer. */
  142. num_bits = 8 * sizeof(result);
  143. if ((shift < num_bits) && (byte & 0x40))
  144. result |= (-1 << shift);
  145. *ret = result;
  146. return count;
  147. }
  148. /**
  149. * dwarf_read_encoded_value - return the decoded value at @addr
  150. * @addr: the address of the encoded value
  151. * @val: where to write the decoded value
  152. * @encoding: the encoding with which we can decode @addr
  153. *
  154. * GCC emits encoded address in the .eh_frame FDE entries. Decode
  155. * the value at @addr using @encoding. The decoded value is written
  156. * to @val and the number of bytes read is returned.
  157. */
  158. static int dwarf_read_encoded_value(char *addr, unsigned long *val,
  159. char encoding)
  160. {
  161. unsigned long decoded_addr = 0;
  162. int count = 0;
  163. switch (encoding & 0x70) {
  164. case DW_EH_PE_absptr:
  165. break;
  166. case DW_EH_PE_pcrel:
  167. decoded_addr = (unsigned long)addr;
  168. break;
  169. default:
  170. pr_debug("encoding=0x%x\n", (encoding & 0x70));
  171. BUG();
  172. }
  173. if ((encoding & 0x07) == 0x00)
  174. encoding |= DW_EH_PE_udata4;
  175. switch (encoding & 0x0f) {
  176. case DW_EH_PE_sdata4:
  177. case DW_EH_PE_udata4:
  178. count += 4;
  179. decoded_addr += get_unaligned((u32 *)addr);
  180. __raw_writel(decoded_addr, val);
  181. break;
  182. default:
  183. pr_debug("encoding=0x%x\n", encoding);
  184. BUG();
  185. }
  186. return count;
  187. }
  188. /**
  189. * dwarf_entry_len - return the length of an FDE or CIE
  190. * @addr: the address of the entry
  191. * @len: the length of the entry
  192. *
  193. * Read the initial_length field of the entry and store the size of
  194. * the entry in @len. We return the number of bytes read. Return a
  195. * count of 0 on error.
  196. */
  197. static inline int dwarf_entry_len(char *addr, unsigned long *len)
  198. {
  199. u32 initial_len;
  200. int count;
  201. initial_len = get_unaligned((u32 *)addr);
  202. count = 4;
  203. /*
  204. * An initial length field value in the range DW_LEN_EXT_LO -
  205. * DW_LEN_EXT_HI indicates an extension, and should not be
  206. * interpreted as a length. The only extension that we currently
  207. * understand is the use of DWARF64 addresses.
  208. */
  209. if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
  210. /*
  211. * The 64-bit length field immediately follows the
  212. * compulsory 32-bit length field.
  213. */
  214. if (initial_len == DW_EXT_DWARF64) {
  215. *len = get_unaligned((u64 *)addr + 4);
  216. count = 12;
  217. } else {
  218. printk(KERN_WARNING "Unknown DWARF extension\n");
  219. count = 0;
  220. }
  221. } else
  222. *len = initial_len;
  223. return count;
  224. }
  225. /**
  226. * dwarf_lookup_cie - locate the cie
  227. * @cie_ptr: pointer to help with lookup
  228. */
  229. static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
  230. {
  231. struct dwarf_cie *cie, *n;
  232. unsigned long flags;
  233. spin_lock_irqsave(&dwarf_cie_lock, flags);
  234. /*
  235. * We've cached the last CIE we looked up because chances are
  236. * that the FDE wants this CIE.
  237. */
  238. if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
  239. cie = cached_cie;
  240. goto out;
  241. }
  242. list_for_each_entry_safe(cie, n, &dwarf_cie_list, link) {
  243. if (cie->cie_pointer == cie_ptr) {
  244. cached_cie = cie;
  245. break;
  246. }
  247. }
  248. /* Couldn't find the entry in the list. */
  249. if (&cie->link == &dwarf_cie_list)
  250. cie = NULL;
  251. out:
  252. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  253. return cie;
  254. }
  255. /**
  256. * dwarf_lookup_fde - locate the FDE that covers pc
  257. * @pc: the program counter
  258. */
  259. struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
  260. {
  261. unsigned long flags;
  262. struct dwarf_fde *fde, *n;
  263. spin_lock_irqsave(&dwarf_fde_lock, flags);
  264. list_for_each_entry_safe(fde, n, &dwarf_fde_list, link) {
  265. unsigned long start, end;
  266. start = fde->initial_location;
  267. end = fde->initial_location + fde->address_range;
  268. if (pc >= start && pc < end)
  269. break;
  270. }
  271. /* Couldn't find the entry in the list. */
  272. if (&fde->link == &dwarf_fde_list)
  273. fde = NULL;
  274. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  275. return fde;
  276. }
  277. /**
  278. * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
  279. * @insn_start: address of the first instruction
  280. * @insn_end: address of the last instruction
  281. * @cie: the CIE for this function
  282. * @fde: the FDE for this function
  283. * @frame: the instructions calculate the CFA for this frame
  284. * @pc: the program counter of the address we're interested in
  285. * @define_ra: keep executing insns until the return addr reg is defined?
  286. *
  287. * Execute the Call Frame instruction sequence starting at
  288. * @insn_start and ending at @insn_end. The instructions describe
  289. * how to calculate the Canonical Frame Address of a stackframe.
  290. * Store the results in @frame.
  291. */
  292. static int dwarf_cfa_execute_insns(unsigned char *insn_start,
  293. unsigned char *insn_end,
  294. struct dwarf_cie *cie,
  295. struct dwarf_fde *fde,
  296. struct dwarf_frame *frame,
  297. unsigned long pc,
  298. bool define_ra)
  299. {
  300. unsigned char insn;
  301. unsigned char *current_insn;
  302. unsigned int count, delta, reg, expr_len, offset;
  303. bool seen_ra_reg;
  304. current_insn = insn_start;
  305. /*
  306. * If we're executing instructions for the dwarf_unwind_stack()
  307. * FDE we need to keep executing instructions until the value of
  308. * DWARF_ARCH_RA_REG is defined. See the comment in
  309. * dwarf_unwind_stack() for more details.
  310. */
  311. if (define_ra)
  312. seen_ra_reg = false;
  313. else
  314. seen_ra_reg = true;
  315. while (current_insn < insn_end && (frame->pc <= pc || !seen_ra_reg) ) {
  316. insn = __raw_readb(current_insn++);
  317. if (!seen_ra_reg) {
  318. if (frame->num_regs >= DWARF_ARCH_RA_REG &&
  319. frame->regs[DWARF_ARCH_RA_REG].flags)
  320. seen_ra_reg = true;
  321. }
  322. /*
  323. * Firstly, handle the opcodes that embed their operands
  324. * in the instructions.
  325. */
  326. switch (DW_CFA_opcode(insn)) {
  327. case DW_CFA_advance_loc:
  328. delta = DW_CFA_operand(insn);
  329. delta *= cie->code_alignment_factor;
  330. frame->pc += delta;
  331. continue;
  332. /* NOTREACHED */
  333. case DW_CFA_offset:
  334. reg = DW_CFA_operand(insn);
  335. count = dwarf_read_uleb128(current_insn, &offset);
  336. current_insn += count;
  337. offset *= cie->data_alignment_factor;
  338. dwarf_frame_alloc_regs(frame, reg);
  339. frame->regs[reg].addr = offset;
  340. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  341. continue;
  342. /* NOTREACHED */
  343. case DW_CFA_restore:
  344. reg = DW_CFA_operand(insn);
  345. continue;
  346. /* NOTREACHED */
  347. }
  348. /*
  349. * Secondly, handle the opcodes that don't embed their
  350. * operands in the instruction.
  351. */
  352. switch (insn) {
  353. case DW_CFA_nop:
  354. continue;
  355. case DW_CFA_advance_loc1:
  356. delta = *current_insn++;
  357. frame->pc += delta * cie->code_alignment_factor;
  358. break;
  359. case DW_CFA_advance_loc2:
  360. delta = get_unaligned((u16 *)current_insn);
  361. current_insn += 2;
  362. frame->pc += delta * cie->code_alignment_factor;
  363. break;
  364. case DW_CFA_advance_loc4:
  365. delta = get_unaligned((u32 *)current_insn);
  366. current_insn += 4;
  367. frame->pc += delta * cie->code_alignment_factor;
  368. break;
  369. case DW_CFA_offset_extended:
  370. count = dwarf_read_uleb128(current_insn, &reg);
  371. current_insn += count;
  372. count = dwarf_read_uleb128(current_insn, &offset);
  373. current_insn += count;
  374. offset *= cie->data_alignment_factor;
  375. break;
  376. case DW_CFA_restore_extended:
  377. count = dwarf_read_uleb128(current_insn, &reg);
  378. current_insn += count;
  379. break;
  380. case DW_CFA_undefined:
  381. count = dwarf_read_uleb128(current_insn, &reg);
  382. current_insn += count;
  383. break;
  384. case DW_CFA_def_cfa:
  385. count = dwarf_read_uleb128(current_insn,
  386. &frame->cfa_register);
  387. current_insn += count;
  388. count = dwarf_read_uleb128(current_insn,
  389. &frame->cfa_offset);
  390. current_insn += count;
  391. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  392. break;
  393. case DW_CFA_def_cfa_register:
  394. count = dwarf_read_uleb128(current_insn,
  395. &frame->cfa_register);
  396. current_insn += count;
  397. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  398. break;
  399. case DW_CFA_def_cfa_offset:
  400. count = dwarf_read_uleb128(current_insn, &offset);
  401. current_insn += count;
  402. frame->cfa_offset = offset;
  403. break;
  404. case DW_CFA_def_cfa_expression:
  405. count = dwarf_read_uleb128(current_insn, &expr_len);
  406. current_insn += count;
  407. frame->cfa_expr = current_insn;
  408. frame->cfa_expr_len = expr_len;
  409. current_insn += expr_len;
  410. frame->flags |= DWARF_FRAME_CFA_REG_EXP;
  411. break;
  412. case DW_CFA_offset_extended_sf:
  413. count = dwarf_read_uleb128(current_insn, &reg);
  414. current_insn += count;
  415. count = dwarf_read_leb128(current_insn, &offset);
  416. current_insn += count;
  417. offset *= cie->data_alignment_factor;
  418. dwarf_frame_alloc_regs(frame, reg);
  419. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  420. frame->regs[reg].addr = offset;
  421. break;
  422. case DW_CFA_val_offset:
  423. count = dwarf_read_uleb128(current_insn, &reg);
  424. current_insn += count;
  425. count = dwarf_read_leb128(current_insn, &offset);
  426. offset *= cie->data_alignment_factor;
  427. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  428. frame->regs[reg].addr = offset;
  429. break;
  430. default:
  431. pr_debug("unhandled DWARF instruction 0x%x\n", insn);
  432. break;
  433. }
  434. }
  435. return 0;
  436. }
  437. /**
  438. * dwarf_unwind_stack - recursively unwind the stack
  439. * @pc: address of the function to unwind
  440. * @prev: struct dwarf_frame of the previous stackframe on the callstack
  441. *
  442. * Return a struct dwarf_frame representing the most recent frame
  443. * on the callstack. Each of the lower (older) stack frames are
  444. * linked via the "prev" member.
  445. */
  446. struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
  447. struct dwarf_frame *prev)
  448. {
  449. struct dwarf_frame *frame;
  450. struct dwarf_cie *cie;
  451. struct dwarf_fde *fde;
  452. unsigned long addr;
  453. int i, offset;
  454. bool define_ra = false;
  455. /*
  456. * If this is the first invocation of this recursive function we
  457. * need get the contents of a physical register to get the CFA
  458. * in order to begin the virtual unwinding of the stack.
  459. *
  460. * Setting "define_ra" to true indictates that we want
  461. * dwarf_cfa_execute_insns() to continue executing instructions
  462. * until we know how to calculate the value of DWARF_ARCH_RA_REG
  463. * (which we need in order to kick off the whole unwinding
  464. * process).
  465. *
  466. * NOTE: the return address is guaranteed to be setup by the
  467. * time this function makes its first function call.
  468. */
  469. if (!pc && !prev) {
  470. pc = (unsigned long)&dwarf_unwind_stack;
  471. define_ra = true;
  472. }
  473. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  474. if (!frame)
  475. return NULL;
  476. frame->prev = prev;
  477. fde = dwarf_lookup_fde(pc);
  478. if (!fde) {
  479. /*
  480. * This is our normal exit path - the one that stops the
  481. * recursion. There's two reasons why we might exit
  482. * here,
  483. *
  484. * a) pc has no asscociated DWARF frame info and so
  485. * we don't know how to unwind this frame. This is
  486. * usually the case when we're trying to unwind a
  487. * frame that was called from some assembly code
  488. * that has no DWARF info, e.g. syscalls.
  489. *
  490. * b) the DEBUG info for pc is bogus. There's
  491. * really no way to distinguish this case from the
  492. * case above, which sucks because we could print a
  493. * warning here.
  494. */
  495. return NULL;
  496. }
  497. cie = dwarf_lookup_cie(fde->cie_pointer);
  498. frame->pc = fde->initial_location;
  499. /* CIE initial instructions */
  500. dwarf_cfa_execute_insns(cie->initial_instructions,
  501. cie->instructions_end, cie, fde,
  502. frame, pc, false);
  503. /* FDE instructions */
  504. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  505. fde, frame, pc, define_ra);
  506. /* Calculate the CFA */
  507. switch (frame->flags) {
  508. case DWARF_FRAME_CFA_REG_OFFSET:
  509. if (prev) {
  510. BUG_ON(!prev->regs[frame->cfa_register].flags);
  511. addr = prev->cfa;
  512. addr += prev->regs[frame->cfa_register].addr;
  513. frame->cfa = __raw_readl(addr);
  514. } else {
  515. /*
  516. * Again, this is the first invocation of this
  517. * recurisve function. We need to physically
  518. * read the contents of a register in order to
  519. * get the Canonical Frame Address for this
  520. * function.
  521. */
  522. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  523. }
  524. frame->cfa += frame->cfa_offset;
  525. break;
  526. default:
  527. BUG();
  528. }
  529. /* If we haven't seen the return address reg, we're screwed. */
  530. BUG_ON(!frame->regs[DWARF_ARCH_RA_REG].flags);
  531. for (i = 0; i <= frame->num_regs; i++) {
  532. struct dwarf_reg *reg = &frame->regs[i];
  533. if (!reg->flags)
  534. continue;
  535. offset = reg->addr;
  536. offset += frame->cfa;
  537. }
  538. addr = frame->cfa + frame->regs[DWARF_ARCH_RA_REG].addr;
  539. frame->return_addr = __raw_readl(addr);
  540. frame->next = dwarf_unwind_stack(frame->return_addr, frame);
  541. return frame;
  542. }
  543. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  544. unsigned char *end)
  545. {
  546. struct dwarf_cie *cie;
  547. unsigned long flags;
  548. int count;
  549. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  550. if (!cie)
  551. return -ENOMEM;
  552. cie->length = len;
  553. /*
  554. * Record the offset into the .eh_frame section
  555. * for this CIE. It allows this CIE to be
  556. * quickly and easily looked up from the
  557. * corresponding FDE.
  558. */
  559. cie->cie_pointer = (unsigned long)entry;
  560. cie->version = *(char *)p++;
  561. BUG_ON(cie->version != 1);
  562. cie->augmentation = p;
  563. p += strlen(cie->augmentation) + 1;
  564. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  565. p += count;
  566. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  567. p += count;
  568. /*
  569. * Which column in the rule table contains the
  570. * return address?
  571. */
  572. if (cie->version == 1) {
  573. cie->return_address_reg = __raw_readb(p);
  574. p++;
  575. } else {
  576. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  577. p += count;
  578. }
  579. if (cie->augmentation[0] == 'z') {
  580. unsigned int length, count;
  581. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  582. count = dwarf_read_uleb128(p, &length);
  583. p += count;
  584. BUG_ON((unsigned char *)p > end);
  585. cie->initial_instructions = p + length;
  586. cie->augmentation++;
  587. }
  588. while (*cie->augmentation) {
  589. /*
  590. * "L" indicates a byte showing how the
  591. * LSDA pointer is encoded. Skip it.
  592. */
  593. if (*cie->augmentation == 'L') {
  594. p++;
  595. cie->augmentation++;
  596. } else if (*cie->augmentation == 'R') {
  597. /*
  598. * "R" indicates a byte showing
  599. * how FDE addresses are
  600. * encoded.
  601. */
  602. cie->encoding = *(char *)p++;
  603. cie->augmentation++;
  604. } else if (*cie->augmentation == 'P') {
  605. /*
  606. * "R" indicates a personality
  607. * routine in the CIE
  608. * augmentation.
  609. */
  610. BUG();
  611. } else if (*cie->augmentation == 'S') {
  612. BUG();
  613. } else {
  614. /*
  615. * Unknown augmentation. Assume
  616. * 'z' augmentation.
  617. */
  618. p = cie->initial_instructions;
  619. BUG_ON(!p);
  620. break;
  621. }
  622. }
  623. cie->initial_instructions = p;
  624. cie->instructions_end = end;
  625. /* Add to list */
  626. spin_lock_irqsave(&dwarf_cie_lock, flags);
  627. list_add_tail(&cie->link, &dwarf_cie_list);
  628. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  629. return 0;
  630. }
  631. static int dwarf_parse_fde(void *entry, u32 entry_type,
  632. void *start, unsigned long len)
  633. {
  634. struct dwarf_fde *fde;
  635. struct dwarf_cie *cie;
  636. unsigned long flags;
  637. int count;
  638. void *p = start;
  639. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  640. if (!fde)
  641. return -ENOMEM;
  642. fde->length = len;
  643. /*
  644. * In a .eh_frame section the CIE pointer is the
  645. * delta between the address within the FDE
  646. */
  647. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  648. cie = dwarf_lookup_cie(fde->cie_pointer);
  649. fde->cie = cie;
  650. if (cie->encoding)
  651. count = dwarf_read_encoded_value(p, &fde->initial_location,
  652. cie->encoding);
  653. else
  654. count = dwarf_read_addr(p, &fde->initial_location);
  655. p += count;
  656. if (cie->encoding)
  657. count = dwarf_read_encoded_value(p, &fde->address_range,
  658. cie->encoding & 0x0f);
  659. else
  660. count = dwarf_read_addr(p, &fde->address_range);
  661. p += count;
  662. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  663. unsigned int length;
  664. count = dwarf_read_uleb128(p, &length);
  665. p += count + length;
  666. }
  667. /* Call frame instructions. */
  668. fde->instructions = p;
  669. fde->end = start + len;
  670. /* Add to list. */
  671. spin_lock_irqsave(&dwarf_fde_lock, flags);
  672. list_add_tail(&fde->link, &dwarf_fde_list);
  673. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  674. return 0;
  675. }
  676. static void dwarf_unwinder_dump(struct task_struct *task, struct pt_regs *regs,
  677. unsigned long *sp,
  678. const struct stacktrace_ops *ops, void *data)
  679. {
  680. struct dwarf_frame *frame;
  681. frame = dwarf_unwind_stack(0, NULL);
  682. while (frame && frame->return_addr) {
  683. ops->address(data, frame->return_addr, 1);
  684. frame = frame->next;
  685. }
  686. }
  687. static struct unwinder dwarf_unwinder = {
  688. .name = "dwarf-unwinder",
  689. .dump = dwarf_unwinder_dump,
  690. .rating = 150,
  691. };
  692. static void dwarf_unwinder_cleanup(void)
  693. {
  694. struct dwarf_cie *cie, *m;
  695. struct dwarf_fde *fde, *n;
  696. unsigned long flags;
  697. /*
  698. * Deallocate all the memory allocated for the DWARF unwinder.
  699. * Traverse all the FDE/CIE lists and remove and free all the
  700. * memory associated with those data structures.
  701. */
  702. spin_lock_irqsave(&dwarf_cie_lock, flags);
  703. list_for_each_entry_safe(cie, m, &dwarf_cie_list, link)
  704. kfree(cie);
  705. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  706. spin_lock_irqsave(&dwarf_fde_lock, flags);
  707. list_for_each_entry_safe(fde, n, &dwarf_fde_list, link)
  708. kfree(fde);
  709. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  710. }
  711. /**
  712. * dwarf_unwinder_init - initialise the dwarf unwinder
  713. *
  714. * Build the data structures describing the .dwarf_frame section to
  715. * make it easier to lookup CIE and FDE entries. Because the
  716. * .eh_frame section is packed as tightly as possible it is not
  717. * easy to lookup the FDE for a given PC, so we build a list of FDE
  718. * and CIE entries that make it easier.
  719. */
  720. void dwarf_unwinder_init(void)
  721. {
  722. u32 entry_type;
  723. void *p, *entry;
  724. int count, err;
  725. unsigned long len;
  726. unsigned int c_entries, f_entries;
  727. unsigned char *end;
  728. INIT_LIST_HEAD(&dwarf_cie_list);
  729. INIT_LIST_HEAD(&dwarf_fde_list);
  730. c_entries = 0;
  731. f_entries = 0;
  732. entry = &__start_eh_frame;
  733. while ((char *)entry < __stop_eh_frame) {
  734. p = entry;
  735. count = dwarf_entry_len(p, &len);
  736. if (count == 0) {
  737. /*
  738. * We read a bogus length field value. There is
  739. * nothing we can do here apart from disabling
  740. * the DWARF unwinder. We can't even skip this
  741. * entry and move to the next one because 'len'
  742. * tells us where our next entry is.
  743. */
  744. goto out;
  745. } else
  746. p += count;
  747. /* initial length does not include itself */
  748. end = p + len;
  749. entry_type = get_unaligned((u32 *)p);
  750. p += 4;
  751. if (entry_type == DW_EH_FRAME_CIE) {
  752. err = dwarf_parse_cie(entry, p, len, end);
  753. if (err < 0)
  754. goto out;
  755. else
  756. c_entries++;
  757. } else {
  758. err = dwarf_parse_fde(entry, entry_type, p, len);
  759. if (err < 0)
  760. goto out;
  761. else
  762. f_entries++;
  763. }
  764. entry = (char *)entry + len + 4;
  765. }
  766. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  767. c_entries, f_entries);
  768. err = unwinder_register(&dwarf_unwinder);
  769. if (err)
  770. goto out;
  771. return;
  772. out:
  773. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  774. dwarf_unwinder_cleanup();
  775. }