dwarf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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_ATOMIC);
  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->cfa_offset = 0;
  398. frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  399. break;
  400. case DW_CFA_def_cfa_offset:
  401. count = dwarf_read_uleb128(current_insn, &offset);
  402. current_insn += count;
  403. frame->cfa_offset = offset;
  404. break;
  405. case DW_CFA_def_cfa_expression:
  406. count = dwarf_read_uleb128(current_insn, &expr_len);
  407. current_insn += count;
  408. frame->cfa_expr = current_insn;
  409. frame->cfa_expr_len = expr_len;
  410. current_insn += expr_len;
  411. frame->flags |= DWARF_FRAME_CFA_REG_EXP;
  412. break;
  413. case DW_CFA_offset_extended_sf:
  414. count = dwarf_read_uleb128(current_insn, &reg);
  415. current_insn += count;
  416. count = dwarf_read_leb128(current_insn, &offset);
  417. current_insn += count;
  418. offset *= cie->data_alignment_factor;
  419. dwarf_frame_alloc_regs(frame, reg);
  420. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  421. frame->regs[reg].addr = offset;
  422. break;
  423. case DW_CFA_val_offset:
  424. count = dwarf_read_uleb128(current_insn, &reg);
  425. current_insn += count;
  426. count = dwarf_read_leb128(current_insn, &offset);
  427. offset *= cie->data_alignment_factor;
  428. frame->regs[reg].flags |= DWARF_REG_OFFSET;
  429. frame->regs[reg].addr = offset;
  430. break;
  431. default:
  432. pr_debug("unhandled DWARF instruction 0x%x\n", insn);
  433. break;
  434. }
  435. }
  436. return 0;
  437. }
  438. /**
  439. * dwarf_unwind_stack - recursively unwind the stack
  440. * @pc: address of the function to unwind
  441. * @prev: struct dwarf_frame of the previous stackframe on the callstack
  442. *
  443. * Return a struct dwarf_frame representing the most recent frame
  444. * on the callstack. Each of the lower (older) stack frames are
  445. * linked via the "prev" member.
  446. */
  447. struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
  448. struct dwarf_frame *prev)
  449. {
  450. struct dwarf_frame *frame;
  451. struct dwarf_cie *cie;
  452. struct dwarf_fde *fde;
  453. unsigned long addr;
  454. int i, offset;
  455. bool define_ra = false;
  456. /*
  457. * If this is the first invocation of this recursive function we
  458. * need get the contents of a physical register to get the CFA
  459. * in order to begin the virtual unwinding of the stack.
  460. *
  461. * Setting "define_ra" to true indictates that we want
  462. * dwarf_cfa_execute_insns() to continue executing instructions
  463. * until we know how to calculate the value of DWARF_ARCH_RA_REG
  464. * (which we need in order to kick off the whole unwinding
  465. * process).
  466. *
  467. * NOTE: the return address is guaranteed to be setup by the
  468. * time this function makes its first function call.
  469. */
  470. if (!pc && !prev) {
  471. pc = (unsigned long)&dwarf_unwind_stack;
  472. define_ra = true;
  473. }
  474. frame = kzalloc(sizeof(*frame), GFP_ATOMIC);
  475. if (!frame)
  476. return NULL;
  477. frame->prev = prev;
  478. fde = dwarf_lookup_fde(pc);
  479. if (!fde) {
  480. /*
  481. * This is our normal exit path - the one that stops the
  482. * recursion. There's two reasons why we might exit
  483. * here,
  484. *
  485. * a) pc has no asscociated DWARF frame info and so
  486. * we don't know how to unwind this frame. This is
  487. * usually the case when we're trying to unwind a
  488. * frame that was called from some assembly code
  489. * that has no DWARF info, e.g. syscalls.
  490. *
  491. * b) the DEBUG info for pc is bogus. There's
  492. * really no way to distinguish this case from the
  493. * case above, which sucks because we could print a
  494. * warning here.
  495. */
  496. return NULL;
  497. }
  498. cie = dwarf_lookup_cie(fde->cie_pointer);
  499. frame->pc = fde->initial_location;
  500. /* CIE initial instructions */
  501. dwarf_cfa_execute_insns(cie->initial_instructions,
  502. cie->instructions_end, cie, fde,
  503. frame, pc, false);
  504. /* FDE instructions */
  505. dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
  506. fde, frame, pc, define_ra);
  507. /* Calculate the CFA */
  508. switch (frame->flags) {
  509. case DWARF_FRAME_CFA_REG_OFFSET:
  510. if (prev) {
  511. BUG_ON(!prev->regs[frame->cfa_register].flags);
  512. addr = prev->cfa;
  513. addr += prev->regs[frame->cfa_register].addr;
  514. frame->cfa = __raw_readl(addr);
  515. } else {
  516. /*
  517. * Again, this is the first invocation of this
  518. * recurisve function. We need to physically
  519. * read the contents of a register in order to
  520. * get the Canonical Frame Address for this
  521. * function.
  522. */
  523. frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  524. }
  525. frame->cfa += frame->cfa_offset;
  526. break;
  527. default:
  528. BUG();
  529. }
  530. /* If we haven't seen the return address reg, we're screwed. */
  531. BUG_ON(!frame->regs[DWARF_ARCH_RA_REG].flags);
  532. for (i = 0; i <= frame->num_regs; i++) {
  533. struct dwarf_reg *reg = &frame->regs[i];
  534. if (!reg->flags)
  535. continue;
  536. offset = reg->addr;
  537. offset += frame->cfa;
  538. }
  539. addr = frame->cfa + frame->regs[DWARF_ARCH_RA_REG].addr;
  540. frame->return_addr = __raw_readl(addr);
  541. frame->next = dwarf_unwind_stack(frame->return_addr, frame);
  542. return frame;
  543. }
  544. static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
  545. unsigned char *end)
  546. {
  547. struct dwarf_cie *cie;
  548. unsigned long flags;
  549. int count;
  550. cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  551. if (!cie)
  552. return -ENOMEM;
  553. cie->length = len;
  554. /*
  555. * Record the offset into the .eh_frame section
  556. * for this CIE. It allows this CIE to be
  557. * quickly and easily looked up from the
  558. * corresponding FDE.
  559. */
  560. cie->cie_pointer = (unsigned long)entry;
  561. cie->version = *(char *)p++;
  562. BUG_ON(cie->version != 1);
  563. cie->augmentation = p;
  564. p += strlen(cie->augmentation) + 1;
  565. count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  566. p += count;
  567. count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  568. p += count;
  569. /*
  570. * Which column in the rule table contains the
  571. * return address?
  572. */
  573. if (cie->version == 1) {
  574. cie->return_address_reg = __raw_readb(p);
  575. p++;
  576. } else {
  577. count = dwarf_read_uleb128(p, &cie->return_address_reg);
  578. p += count;
  579. }
  580. if (cie->augmentation[0] == 'z') {
  581. unsigned int length, count;
  582. cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  583. count = dwarf_read_uleb128(p, &length);
  584. p += count;
  585. BUG_ON((unsigned char *)p > end);
  586. cie->initial_instructions = p + length;
  587. cie->augmentation++;
  588. }
  589. while (*cie->augmentation) {
  590. /*
  591. * "L" indicates a byte showing how the
  592. * LSDA pointer is encoded. Skip it.
  593. */
  594. if (*cie->augmentation == 'L') {
  595. p++;
  596. cie->augmentation++;
  597. } else if (*cie->augmentation == 'R') {
  598. /*
  599. * "R" indicates a byte showing
  600. * how FDE addresses are
  601. * encoded.
  602. */
  603. cie->encoding = *(char *)p++;
  604. cie->augmentation++;
  605. } else if (*cie->augmentation == 'P') {
  606. /*
  607. * "R" indicates a personality
  608. * routine in the CIE
  609. * augmentation.
  610. */
  611. BUG();
  612. } else if (*cie->augmentation == 'S') {
  613. BUG();
  614. } else {
  615. /*
  616. * Unknown augmentation. Assume
  617. * 'z' augmentation.
  618. */
  619. p = cie->initial_instructions;
  620. BUG_ON(!p);
  621. break;
  622. }
  623. }
  624. cie->initial_instructions = p;
  625. cie->instructions_end = end;
  626. /* Add to list */
  627. spin_lock_irqsave(&dwarf_cie_lock, flags);
  628. list_add_tail(&cie->link, &dwarf_cie_list);
  629. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  630. return 0;
  631. }
  632. static int dwarf_parse_fde(void *entry, u32 entry_type,
  633. void *start, unsigned long len)
  634. {
  635. struct dwarf_fde *fde;
  636. struct dwarf_cie *cie;
  637. unsigned long flags;
  638. int count;
  639. void *p = start;
  640. fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  641. if (!fde)
  642. return -ENOMEM;
  643. fde->length = len;
  644. /*
  645. * In a .eh_frame section the CIE pointer is the
  646. * delta between the address within the FDE
  647. */
  648. fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  649. cie = dwarf_lookup_cie(fde->cie_pointer);
  650. fde->cie = cie;
  651. if (cie->encoding)
  652. count = dwarf_read_encoded_value(p, &fde->initial_location,
  653. cie->encoding);
  654. else
  655. count = dwarf_read_addr(p, &fde->initial_location);
  656. p += count;
  657. if (cie->encoding)
  658. count = dwarf_read_encoded_value(p, &fde->address_range,
  659. cie->encoding & 0x0f);
  660. else
  661. count = dwarf_read_addr(p, &fde->address_range);
  662. p += count;
  663. if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  664. unsigned int length;
  665. count = dwarf_read_uleb128(p, &length);
  666. p += count + length;
  667. }
  668. /* Call frame instructions. */
  669. fde->instructions = p;
  670. fde->end = start + len;
  671. /* Add to list. */
  672. spin_lock_irqsave(&dwarf_fde_lock, flags);
  673. list_add_tail(&fde->link, &dwarf_fde_list);
  674. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  675. return 0;
  676. }
  677. static void dwarf_unwinder_dump(struct task_struct *task, struct pt_regs *regs,
  678. unsigned long *sp,
  679. const struct stacktrace_ops *ops, void *data)
  680. {
  681. struct dwarf_frame *frame;
  682. frame = dwarf_unwind_stack(0, NULL);
  683. while (frame && frame->return_addr) {
  684. ops->address(data, frame->return_addr, 1);
  685. frame = frame->next;
  686. }
  687. }
  688. static struct unwinder dwarf_unwinder = {
  689. .name = "dwarf-unwinder",
  690. .dump = dwarf_unwinder_dump,
  691. .rating = 150,
  692. };
  693. static void dwarf_unwinder_cleanup(void)
  694. {
  695. struct dwarf_cie *cie, *m;
  696. struct dwarf_fde *fde, *n;
  697. unsigned long flags;
  698. /*
  699. * Deallocate all the memory allocated for the DWARF unwinder.
  700. * Traverse all the FDE/CIE lists and remove and free all the
  701. * memory associated with those data structures.
  702. */
  703. spin_lock_irqsave(&dwarf_cie_lock, flags);
  704. list_for_each_entry_safe(cie, m, &dwarf_cie_list, link)
  705. kfree(cie);
  706. spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  707. spin_lock_irqsave(&dwarf_fde_lock, flags);
  708. list_for_each_entry_safe(fde, n, &dwarf_fde_list, link)
  709. kfree(fde);
  710. spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  711. }
  712. /**
  713. * dwarf_unwinder_init - initialise the dwarf unwinder
  714. *
  715. * Build the data structures describing the .dwarf_frame section to
  716. * make it easier to lookup CIE and FDE entries. Because the
  717. * .eh_frame section is packed as tightly as possible it is not
  718. * easy to lookup the FDE for a given PC, so we build a list of FDE
  719. * and CIE entries that make it easier.
  720. */
  721. void dwarf_unwinder_init(void)
  722. {
  723. u32 entry_type;
  724. void *p, *entry;
  725. int count, err;
  726. unsigned long len;
  727. unsigned int c_entries, f_entries;
  728. unsigned char *end;
  729. INIT_LIST_HEAD(&dwarf_cie_list);
  730. INIT_LIST_HEAD(&dwarf_fde_list);
  731. c_entries = 0;
  732. f_entries = 0;
  733. entry = &__start_eh_frame;
  734. while ((char *)entry < __stop_eh_frame) {
  735. p = entry;
  736. count = dwarf_entry_len(p, &len);
  737. if (count == 0) {
  738. /*
  739. * We read a bogus length field value. There is
  740. * nothing we can do here apart from disabling
  741. * the DWARF unwinder. We can't even skip this
  742. * entry and move to the next one because 'len'
  743. * tells us where our next entry is.
  744. */
  745. goto out;
  746. } else
  747. p += count;
  748. /* initial length does not include itself */
  749. end = p + len;
  750. entry_type = get_unaligned((u32 *)p);
  751. p += 4;
  752. if (entry_type == DW_EH_FRAME_CIE) {
  753. err = dwarf_parse_cie(entry, p, len, end);
  754. if (err < 0)
  755. goto out;
  756. else
  757. c_entries++;
  758. } else {
  759. err = dwarf_parse_fde(entry, entry_type, p, len);
  760. if (err < 0)
  761. goto out;
  762. else
  763. f_entries++;
  764. }
  765. entry = (char *)entry + len + 4;
  766. }
  767. printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
  768. c_entries, f_entries);
  769. err = unwinder_register(&dwarf_unwinder);
  770. if (err)
  771. goto out;
  772. return;
  773. out:
  774. printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
  775. dwarf_unwinder_cleanup();
  776. }