2 * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
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
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.
13 * - DWARF64 doesn't work.
14 * - Registers with DWARF_VAL_OFFSET rules aren't handled properly.
18 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/mempool.h>
23 #include <linux/elf.h>
24 #include <linux/ftrace.h>
25 #include <linux/slab.h>
26 #include <asm/dwarf.h>
27 #include <asm/unwinder.h>
28 #include <asm/sections.h>
29 #include <asm/unaligned.h>
30 #include <asm/stacktrace.h>
32 /* Reserve enough memory for two stack frames */
33 #define DWARF_FRAME_MIN_REQ 2
34 /* ... with 4 registers per frame. */
35 #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4)
37 static struct kmem_cache *dwarf_frame_cachep;
38 static mempool_t *dwarf_frame_pool;
40 static struct kmem_cache *dwarf_reg_cachep;
41 static mempool_t *dwarf_reg_pool;
43 static struct rb_root cie_root;
44 static DEFINE_SPINLOCK(dwarf_cie_lock);
46 static struct rb_root fde_root;
47 static DEFINE_SPINLOCK(dwarf_fde_lock);
49 static struct dwarf_cie *cached_cie;
52 * dwarf_frame_alloc_reg - allocate memory for a DWARF register
53 * @frame: the DWARF frame whose list of registers we insert on
54 * @reg_num: the register number
56 * Allocate space for, and initialise, a dwarf reg from
57 * dwarf_reg_pool and insert it onto the (unsorted) linked-list of
58 * dwarf registers for @frame.
60 * Return the initialised DWARF reg.
62 static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,
65 struct dwarf_reg *reg;
67 reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);
69 printk(KERN_WARNING "Unable to allocate a DWARF register\n");
71 * Let's just bomb hard here, we have no way to
77 reg->number = reg_num;
81 list_add(®->link, &frame->reg_list);
86 static void dwarf_frame_free_regs(struct dwarf_frame *frame)
88 struct dwarf_reg *reg, *n;
90 list_for_each_entry_safe(reg, n, &frame->reg_list, link) {
92 mempool_free(reg, dwarf_reg_pool);
97 * dwarf_frame_reg - return a DWARF register
98 * @frame: the DWARF frame to search in for @reg_num
99 * @reg_num: the register number to search for
101 * Lookup and return the dwarf reg @reg_num for this frame. Return
102 * NULL if @reg_num is an register invalid number.
104 static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,
105 unsigned int reg_num)
107 struct dwarf_reg *reg;
109 list_for_each_entry(reg, &frame->reg_list, link) {
110 if (reg->number == reg_num)
118 * dwarf_read_addr - read dwarf data
119 * @src: source address of data
120 * @dst: destination address to store the data to
122 * Read 'n' bytes from @src, where 'n' is the size of an address on
123 * the native machine. We return the number of bytes read, which
124 * should always be 'n'. We also have to be careful when reading
125 * from @src and writing to @dst, because they can be arbitrarily
126 * aligned. Return 'n' - the number of bytes read.
128 static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
130 u32 val = get_unaligned(src);
131 put_unaligned(val, dst);
132 return sizeof(unsigned long *);
136 * dwarf_read_uleb128 - read unsigned LEB128 data
137 * @addr: the address where the ULEB128 data is stored
138 * @ret: address to store the result
140 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
141 * from Appendix C of the DWARF 3 spec. For information on the
142 * encodings refer to section "7.6 - Variable Length Data". Return
143 * the number of bytes read.
145 static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
156 byte = __raw_readb(addr);
160 result |= (byte & 0x7f) << shift;
173 * dwarf_read_leb128 - read signed LEB128 data
174 * @addr: the address of the LEB128 encoded data
175 * @ret: address to store the result
177 * Decode signed LEB128 data. The algorithm is taken from Appendix
178 * C of the DWARF 3 spec. Return the number of bytes read.
180 static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
192 byte = __raw_readb(addr);
194 result |= (byte & 0x7f) << shift;
202 /* The number of bits in a signed integer. */
203 num_bits = 8 * sizeof(result);
205 if ((shift < num_bits) && (byte & 0x40))
206 result |= (-1 << shift);
214 * dwarf_read_encoded_value - return the decoded value at @addr
215 * @addr: the address of the encoded value
216 * @val: where to write the decoded value
217 * @encoding: the encoding with which we can decode @addr
219 * GCC emits encoded address in the .eh_frame FDE entries. Decode
220 * the value at @addr using @encoding. The decoded value is written
221 * to @val and the number of bytes read is returned.
223 static int dwarf_read_encoded_value(char *addr, unsigned long *val,
226 unsigned long decoded_addr = 0;
229 switch (encoding & 0x70) {
230 case DW_EH_PE_absptr:
233 decoded_addr = (unsigned long)addr;
236 pr_debug("encoding=0x%x\n", (encoding & 0x70));
240 if ((encoding & 0x07) == 0x00)
241 encoding |= DW_EH_PE_udata4;
243 switch (encoding & 0x0f) {
244 case DW_EH_PE_sdata4:
245 case DW_EH_PE_udata4:
247 decoded_addr += get_unaligned((u32 *)addr);
248 __raw_writel(decoded_addr, val);
251 pr_debug("encoding=0x%x\n", encoding);
259 * dwarf_entry_len - return the length of an FDE or CIE
260 * @addr: the address of the entry
261 * @len: the length of the entry
263 * Read the initial_length field of the entry and store the size of
264 * the entry in @len. We return the number of bytes read. Return a
265 * count of 0 on error.
267 static inline int dwarf_entry_len(char *addr, unsigned long *len)
272 initial_len = get_unaligned((u32 *)addr);
276 * An initial length field value in the range DW_LEN_EXT_LO -
277 * DW_LEN_EXT_HI indicates an extension, and should not be
278 * interpreted as a length. The only extension that we currently
279 * understand is the use of DWARF64 addresses.
281 if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
283 * The 64-bit length field immediately follows the
284 * compulsory 32-bit length field.
286 if (initial_len == DW_EXT_DWARF64) {
287 *len = get_unaligned((u64 *)addr + 4);
290 printk(KERN_WARNING "Unknown DWARF extension\n");
300 * dwarf_lookup_cie - locate the cie
301 * @cie_ptr: pointer to help with lookup
303 static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
305 struct rb_node **rb_node = &cie_root.rb_node;
306 struct dwarf_cie *cie = NULL;
309 spin_lock_irqsave(&dwarf_cie_lock, flags);
312 * We've cached the last CIE we looked up because chances are
313 * that the FDE wants this CIE.
315 if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
321 struct dwarf_cie *cie_tmp;
323 cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
326 if (cie_ptr == cie_tmp->cie_pointer) {
328 cached_cie = cie_tmp;
331 if (cie_ptr < cie_tmp->cie_pointer)
332 rb_node = &(*rb_node)->rb_left;
334 rb_node = &(*rb_node)->rb_right;
339 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
344 * dwarf_lookup_fde - locate the FDE that covers pc
345 * @pc: the program counter
347 struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
349 struct rb_node **rb_node = &fde_root.rb_node;
350 struct dwarf_fde *fde = NULL;
353 spin_lock_irqsave(&dwarf_fde_lock, flags);
356 struct dwarf_fde *fde_tmp;
357 unsigned long tmp_start, tmp_end;
359 fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
362 tmp_start = fde_tmp->initial_location;
363 tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
365 if (pc < tmp_start) {
366 rb_node = &(*rb_node)->rb_left;
372 rb_node = &(*rb_node)->rb_right;
377 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
383 * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
384 * @insn_start: address of the first instruction
385 * @insn_end: address of the last instruction
386 * @cie: the CIE for this function
387 * @fde: the FDE for this function
388 * @frame: the instructions calculate the CFA for this frame
389 * @pc: the program counter of the address we're interested in
391 * Execute the Call Frame instruction sequence starting at
392 * @insn_start and ending at @insn_end. The instructions describe
393 * how to calculate the Canonical Frame Address of a stackframe.
394 * Store the results in @frame.
396 static int dwarf_cfa_execute_insns(unsigned char *insn_start,
397 unsigned char *insn_end,
398 struct dwarf_cie *cie,
399 struct dwarf_fde *fde,
400 struct dwarf_frame *frame,
404 unsigned char *current_insn;
405 unsigned int count, delta, reg, expr_len, offset;
406 struct dwarf_reg *regp;
408 current_insn = insn_start;
410 while (current_insn < insn_end && frame->pc <= pc) {
411 insn = __raw_readb(current_insn++);
414 * Firstly, handle the opcodes that embed their operands
415 * in the instructions.
417 switch (DW_CFA_opcode(insn)) {
418 case DW_CFA_advance_loc:
419 delta = DW_CFA_operand(insn);
420 delta *= cie->code_alignment_factor;
425 reg = DW_CFA_operand(insn);
426 count = dwarf_read_uleb128(current_insn, &offset);
427 current_insn += count;
428 offset *= cie->data_alignment_factor;
429 regp = dwarf_frame_alloc_reg(frame, reg);
431 regp->flags |= DWARF_REG_OFFSET;
435 reg = DW_CFA_operand(insn);
441 * Secondly, handle the opcodes that don't embed their
442 * operands in the instruction.
447 case DW_CFA_advance_loc1:
448 delta = *current_insn++;
449 frame->pc += delta * cie->code_alignment_factor;
451 case DW_CFA_advance_loc2:
452 delta = get_unaligned((u16 *)current_insn);
454 frame->pc += delta * cie->code_alignment_factor;
456 case DW_CFA_advance_loc4:
457 delta = get_unaligned((u32 *)current_insn);
459 frame->pc += delta * cie->code_alignment_factor;
461 case DW_CFA_offset_extended:
462 count = dwarf_read_uleb128(current_insn, ®);
463 current_insn += count;
464 count = dwarf_read_uleb128(current_insn, &offset);
465 current_insn += count;
466 offset *= cie->data_alignment_factor;
468 case DW_CFA_restore_extended:
469 count = dwarf_read_uleb128(current_insn, ®);
470 current_insn += count;
472 case DW_CFA_undefined:
473 count = dwarf_read_uleb128(current_insn, ®);
474 current_insn += count;
475 regp = dwarf_frame_alloc_reg(frame, reg);
476 regp->flags |= DWARF_UNDEFINED;
479 count = dwarf_read_uleb128(current_insn,
480 &frame->cfa_register);
481 current_insn += count;
482 count = dwarf_read_uleb128(current_insn,
484 current_insn += count;
486 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
488 case DW_CFA_def_cfa_register:
489 count = dwarf_read_uleb128(current_insn,
490 &frame->cfa_register);
491 current_insn += count;
492 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
494 case DW_CFA_def_cfa_offset:
495 count = dwarf_read_uleb128(current_insn, &offset);
496 current_insn += count;
497 frame->cfa_offset = offset;
499 case DW_CFA_def_cfa_expression:
500 count = dwarf_read_uleb128(current_insn, &expr_len);
501 current_insn += count;
503 frame->cfa_expr = current_insn;
504 frame->cfa_expr_len = expr_len;
505 current_insn += expr_len;
507 frame->flags |= DWARF_FRAME_CFA_REG_EXP;
509 case DW_CFA_offset_extended_sf:
510 count = dwarf_read_uleb128(current_insn, ®);
511 current_insn += count;
512 count = dwarf_read_leb128(current_insn, &offset);
513 current_insn += count;
514 offset *= cie->data_alignment_factor;
515 regp = dwarf_frame_alloc_reg(frame, reg);
516 regp->flags |= DWARF_REG_OFFSET;
519 case DW_CFA_val_offset:
520 count = dwarf_read_uleb128(current_insn, ®);
521 current_insn += count;
522 count = dwarf_read_leb128(current_insn, &offset);
523 offset *= cie->data_alignment_factor;
524 regp = dwarf_frame_alloc_reg(frame, reg);
525 regp->flags |= DWARF_VAL_OFFSET;
528 case DW_CFA_GNU_args_size:
529 count = dwarf_read_uleb128(current_insn, &offset);
530 current_insn += count;
532 case DW_CFA_GNU_negative_offset_extended:
533 count = dwarf_read_uleb128(current_insn, ®);
534 current_insn += count;
535 count = dwarf_read_uleb128(current_insn, &offset);
536 offset *= cie->data_alignment_factor;
538 regp = dwarf_frame_alloc_reg(frame, reg);
539 regp->flags |= DWARF_REG_OFFSET;
540 regp->addr = -offset;
543 pr_debug("unhandled DWARF instruction 0x%x\n", insn);
553 * dwarf_free_frame - free the memory allocated for @frame
554 * @frame: the frame to free
556 void dwarf_free_frame(struct dwarf_frame *frame)
558 dwarf_frame_free_regs(frame);
559 mempool_free(frame, dwarf_frame_pool);
562 extern void ret_from_irq(void);
565 * dwarf_unwind_stack - unwind the stack
567 * @pc: address of the function to unwind
568 * @prev: struct dwarf_frame of the previous stackframe on the callstack
570 * Return a struct dwarf_frame representing the most recent frame
571 * on the callstack. Each of the lower (older) stack frames are
572 * linked via the "prev" member.
574 struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
575 struct dwarf_frame *prev)
577 struct dwarf_frame *frame;
578 struct dwarf_cie *cie;
579 struct dwarf_fde *fde;
580 struct dwarf_reg *reg;
584 * If we're starting at the top of the stack we need get the
585 * contents of a physical register to get the CFA in order to
586 * begin the virtual unwinding of the stack.
588 * NOTE: the return address is guaranteed to be setup by the
589 * time this function makes its first function call.
592 pc = (unsigned long)current_text_addr();
594 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
596 * If our stack has been patched by the function graph tracer
597 * then we might see the address of return_to_handler() where we
598 * expected to find the real return address.
600 if (pc == (unsigned long)&return_to_handler) {
601 int index = current->curr_ret_stack;
604 * We currently have no way of tracking how many
605 * return_to_handler()'s we've seen. If there is more
606 * than one patched return address on our stack,
611 pc = current->ret_stack[index].ret;
615 frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
617 printk(KERN_ERR "Unable to allocate a dwarf frame\n");
621 INIT_LIST_HEAD(&frame->reg_list);
624 frame->return_addr = 0;
626 fde = dwarf_lookup_fde(pc);
629 * This is our normal exit path. There are two reasons
630 * why we might exit here,
632 * a) pc has no asscociated DWARF frame info and so
633 * we don't know how to unwind this frame. This is
634 * usually the case when we're trying to unwind a
635 * frame that was called from some assembly code
636 * that has no DWARF info, e.g. syscalls.
638 * b) the DEBUG info for pc is bogus. There's
639 * really no way to distinguish this case from the
640 * case above, which sucks because we could print a
646 cie = dwarf_lookup_cie(fde->cie_pointer);
648 frame->pc = fde->initial_location;
650 /* CIE initial instructions */
651 dwarf_cfa_execute_insns(cie->initial_instructions,
652 cie->instructions_end, cie, fde,
655 /* FDE instructions */
656 dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
659 /* Calculate the CFA */
660 switch (frame->flags) {
661 case DWARF_FRAME_CFA_REG_OFFSET:
663 reg = dwarf_frame_reg(prev, frame->cfa_register);
664 UNWINDER_BUG_ON(!reg);
665 UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
667 addr = prev->cfa + reg->addr;
668 frame->cfa = __raw_readl(addr);
672 * Again, we're starting from the top of the
673 * stack. We need to physically read
674 * the contents of a register in order to get
675 * the Canonical Frame Address for this
678 frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
681 frame->cfa += frame->cfa_offset;
687 reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
690 * If we haven't seen the return address register or the return
691 * address column is undefined then we must assume that this is
692 * the end of the callstack.
694 if (!reg || reg->flags == DWARF_UNDEFINED)
697 UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
699 addr = frame->cfa + reg->addr;
700 frame->return_addr = __raw_readl(addr);
703 * Ah, the joys of unwinding through interrupts.
705 * Interrupts are tricky - the DWARF info needs to be _really_
706 * accurate and unfortunately I'm seeing a lot of bogus DWARF
707 * info. For example, I've seen interrupts occur in epilogues
708 * just after the frame pointer (r14) had been restored. The
709 * problem was that the DWARF info claimed that the CFA could be
710 * reached by using the value of the frame pointer before it was
713 * So until the compiler can be trusted to produce reliable
714 * DWARF info when it really matters, let's stop unwinding once
715 * we've calculated the function that was interrupted.
717 if (prev && prev->pc == (unsigned long)ret_from_irq)
718 frame->return_addr = 0;
723 dwarf_free_frame(frame);
727 static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
728 unsigned char *end, struct module *mod)
730 struct rb_node **rb_node = &cie_root.rb_node;
731 struct rb_node *parent = *rb_node;
732 struct dwarf_cie *cie;
736 cie = kzalloc(sizeof(*cie), GFP_KERNEL);
743 * Record the offset into the .eh_frame section
744 * for this CIE. It allows this CIE to be
745 * quickly and easily looked up from the
748 cie->cie_pointer = (unsigned long)entry;
750 cie->version = *(char *)p++;
751 UNWINDER_BUG_ON(cie->version != 1);
753 cie->augmentation = p;
754 p += strlen(cie->augmentation) + 1;
756 count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
759 count = dwarf_read_leb128(p, &cie->data_alignment_factor);
763 * Which column in the rule table contains the
766 if (cie->version == 1) {
767 cie->return_address_reg = __raw_readb(p);
770 count = dwarf_read_uleb128(p, &cie->return_address_reg);
774 if (cie->augmentation[0] == 'z') {
775 unsigned int length, count;
776 cie->flags |= DWARF_CIE_Z_AUGMENTATION;
778 count = dwarf_read_uleb128(p, &length);
781 UNWINDER_BUG_ON((unsigned char *)p > end);
783 cie->initial_instructions = p + length;
787 while (*cie->augmentation) {
789 * "L" indicates a byte showing how the
790 * LSDA pointer is encoded. Skip it.
792 if (*cie->augmentation == 'L') {
795 } else if (*cie->augmentation == 'R') {
797 * "R" indicates a byte showing
798 * how FDE addresses are
801 cie->encoding = *(char *)p++;
803 } else if (*cie->augmentation == 'P') {
805 * "R" indicates a personality
810 } else if (*cie->augmentation == 'S') {
814 * Unknown augmentation. Assume
817 p = cie->initial_instructions;
823 cie->initial_instructions = p;
824 cie->instructions_end = end;
827 spin_lock_irqsave(&dwarf_cie_lock, flags);
830 struct dwarf_cie *cie_tmp;
832 cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
836 if (cie->cie_pointer < cie_tmp->cie_pointer)
837 rb_node = &parent->rb_left;
838 else if (cie->cie_pointer >= cie_tmp->cie_pointer)
839 rb_node = &parent->rb_right;
844 rb_link_node(&cie->node, parent, rb_node);
845 rb_insert_color(&cie->node, &cie_root);
848 list_add_tail(&cie->link, &mod->arch.cie_list);
850 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
855 static int dwarf_parse_fde(void *entry, u32 entry_type,
856 void *start, unsigned long len,
857 unsigned char *end, struct module *mod)
859 struct rb_node **rb_node = &fde_root.rb_node;
860 struct rb_node *parent = *rb_node;
861 struct dwarf_fde *fde;
862 struct dwarf_cie *cie;
867 fde = kzalloc(sizeof(*fde), GFP_KERNEL);
874 * In a .eh_frame section the CIE pointer is the
875 * delta between the address within the FDE
877 fde->cie_pointer = (unsigned long)(p - entry_type - 4);
879 cie = dwarf_lookup_cie(fde->cie_pointer);
883 count = dwarf_read_encoded_value(p, &fde->initial_location,
886 count = dwarf_read_addr(p, &fde->initial_location);
891 count = dwarf_read_encoded_value(p, &fde->address_range,
892 cie->encoding & 0x0f);
894 count = dwarf_read_addr(p, &fde->address_range);
898 if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
900 count = dwarf_read_uleb128(p, &length);
904 /* Call frame instructions. */
905 fde->instructions = p;
909 spin_lock_irqsave(&dwarf_fde_lock, flags);
912 struct dwarf_fde *fde_tmp;
913 unsigned long tmp_start, tmp_end;
914 unsigned long start, end;
916 fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
918 start = fde->initial_location;
919 end = fde->initial_location + fde->address_range;
921 tmp_start = fde_tmp->initial_location;
922 tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
926 if (start < tmp_start)
927 rb_node = &parent->rb_left;
928 else if (start >= tmp_end)
929 rb_node = &parent->rb_right;
934 rb_link_node(&fde->node, parent, rb_node);
935 rb_insert_color(&fde->node, &fde_root);
938 list_add_tail(&fde->link, &mod->arch.fde_list);
940 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
945 static void dwarf_unwinder_dump(struct task_struct *task,
946 struct pt_regs *regs,
948 const struct stacktrace_ops *ops,
951 struct dwarf_frame *frame, *_frame;
952 unsigned long return_addr;
958 frame = dwarf_unwind_stack(return_addr, _frame);
961 dwarf_free_frame(_frame);
965 if (!frame || !frame->return_addr)
968 return_addr = frame->return_addr;
969 ops->address(data, return_addr, 1);
973 dwarf_free_frame(frame);
976 static struct unwinder dwarf_unwinder = {
977 .name = "dwarf-unwinder",
978 .dump = dwarf_unwinder_dump,
982 static void dwarf_unwinder_cleanup(void)
984 struct rb_node **fde_rb_node = &fde_root.rb_node;
985 struct rb_node **cie_rb_node = &cie_root.rb_node;
988 * Deallocate all the memory allocated for the DWARF unwinder.
989 * Traverse all the FDE/CIE lists and remove and free all the
990 * memory associated with those data structures.
992 while (*fde_rb_node) {
993 struct dwarf_fde *fde;
995 fde = rb_entry(*fde_rb_node, struct dwarf_fde, node);
996 rb_erase(*fde_rb_node, &fde_root);
1000 while (*cie_rb_node) {
1001 struct dwarf_cie *cie;
1003 cie = rb_entry(*cie_rb_node, struct dwarf_cie, node);
1004 rb_erase(*cie_rb_node, &cie_root);
1008 kmem_cache_destroy(dwarf_reg_cachep);
1009 kmem_cache_destroy(dwarf_frame_cachep);
1013 * dwarf_parse_section - parse DWARF section
1014 * @eh_frame_start: start address of the .eh_frame section
1015 * @eh_frame_end: end address of the .eh_frame section
1016 * @mod: the kernel module containing the .eh_frame section
1018 * Parse the information in a .eh_frame section.
1020 static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
1026 unsigned long len = 0;
1027 unsigned int c_entries, f_entries;
1032 entry = eh_frame_start;
1034 while ((char *)entry < eh_frame_end) {
1037 count = dwarf_entry_len(p, &len);
1040 * We read a bogus length field value. There is
1041 * nothing we can do here apart from disabling
1042 * the DWARF unwinder. We can't even skip this
1043 * entry and move to the next one because 'len'
1044 * tells us where our next entry is.
1051 /* initial length does not include itself */
1054 entry_type = get_unaligned((u32 *)p);
1057 if (entry_type == DW_EH_FRAME_CIE) {
1058 err = dwarf_parse_cie(entry, p, len, end, mod);
1064 err = dwarf_parse_fde(entry, entry_type, p, len,
1072 entry = (char *)entry + len + 4;
1075 printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
1076 c_entries, f_entries);
1084 #ifdef CONFIG_MODULES
1085 int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
1088 unsigned int i, err;
1089 unsigned long start, end;
1090 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1094 for (i = 1; i < hdr->e_shnum; i++) {
1095 /* Alloc bit cleared means "ignore it." */
1096 if ((sechdrs[i].sh_flags & SHF_ALLOC)
1097 && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
1098 start = sechdrs[i].sh_addr;
1099 end = start + sechdrs[i].sh_size;
1104 /* Did we find the .eh_frame section? */
1105 if (i != hdr->e_shnum) {
1106 INIT_LIST_HEAD(&me->arch.cie_list);
1107 INIT_LIST_HEAD(&me->arch.fde_list);
1108 err = dwarf_parse_section((char *)start, (char *)end, me);
1110 printk(KERN_WARNING "%s: failed to parse DWARF info\n",
1120 * module_dwarf_cleanup - remove FDE/CIEs associated with @mod
1121 * @mod: the module that is being unloaded
1123 * Remove any FDEs and CIEs from the global lists that came from
1124 * @mod's .eh_frame section because @mod is being unloaded.
1126 void module_dwarf_cleanup(struct module *mod)
1128 struct dwarf_fde *fde, *ftmp;
1129 struct dwarf_cie *cie, *ctmp;
1130 unsigned long flags;
1132 spin_lock_irqsave(&dwarf_cie_lock, flags);
1134 list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) {
1135 list_del(&cie->link);
1136 rb_erase(&cie->node, &cie_root);
1140 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
1142 spin_lock_irqsave(&dwarf_fde_lock, flags);
1144 list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) {
1145 list_del(&fde->link);
1146 rb_erase(&fde->node, &fde_root);
1150 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
1152 #endif /* CONFIG_MODULES */
1155 * dwarf_unwinder_init - initialise the dwarf unwinder
1157 * Build the data structures describing the .dwarf_frame section to
1158 * make it easier to lookup CIE and FDE entries. Because the
1159 * .eh_frame section is packed as tightly as possible it is not
1160 * easy to lookup the FDE for a given PC, so we build a list of FDE
1161 * and CIE entries that make it easier.
1163 static int __init dwarf_unwinder_init(void)
1167 dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
1168 sizeof(struct dwarf_frame), 0,
1169 SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
1171 dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
1172 sizeof(struct dwarf_reg), 0,
1173 SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
1175 dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
1178 dwarf_frame_cachep);
1180 dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
1185 err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
1189 err = unwinder_register(&dwarf_unwinder);
1196 printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
1197 dwarf_unwinder_cleanup();
1200 early_initcall(dwarf_unwinder_init);