dwarf.c 21 KB

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