18 #include <linux/kernel.h>
20 #include <linux/list.h>
23 #include <linux/elf.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
29 #include <asm/sections.h>
30 #include <asm/unaligned.h>
31 #include <asm/stacktrace.h>
34 #define DWARF_FRAME_MIN_REQ 2
36 #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4)
50 static struct dwarf_cie *cached_cie;
52 static unsigned int dwarf_unwinder_ready;
65 static struct dwarf_reg *dwarf_frame_alloc_reg(
struct dwarf_frame *
frame,
68 struct dwarf_reg *
reg;
80 reg->number = reg_num;
84 list_add(®->link, &frame->reg_list);
89 static void dwarf_frame_free_regs(
struct dwarf_frame *frame)
91 struct dwarf_reg *
reg, *
n;
107 static struct dwarf_reg *dwarf_frame_reg(
struct dwarf_frame *frame,
108 unsigned int reg_num)
110 struct dwarf_reg *
reg;
113 if (reg->number == reg_num)
131 static inline int dwarf_read_addr(
unsigned long *
src,
unsigned long *
dst)
135 return sizeof(
unsigned long *);
148 static inline unsigned long dwarf_read_uleb128(
char *
addr,
unsigned int *
ret)
163 result |= (byte & 0x7f) << shift;
183 static inline unsigned long dwarf_read_leb128(
char *addr,
int *ret)
197 result |= (byte & 0x7f) << shift;
206 num_bits = 8 *
sizeof(
result);
208 if ((shift < num_bits) && (byte & 0x40))
209 result |= (-1 << shift);
226 static int dwarf_read_encoded_value(
char *addr,
unsigned long *
val,
229 unsigned long decoded_addr = 0;
232 switch (encoding & 0x70) {
236 decoded_addr = (
unsigned long)addr;
239 pr_debug(
"encoding=0x%x\n", (encoding & 0x70));
243 if ((encoding & 0x07) == 0x00)
246 switch (encoding & 0x0f) {
254 pr_debug(
"encoding=0x%x\n", encoding);
270 static inline int dwarf_entry_len(
char *addr,
unsigned long *len)
284 if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
289 if (initial_len == DW_EXT_DWARF64) {
306 static struct dwarf_cie *dwarf_lookup_cie(
unsigned long cie_ptr)
309 struct dwarf_cie *cie =
NULL;
318 if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
324 struct dwarf_cie *cie_tmp;
329 if (cie_ptr == cie_tmp->cie_pointer) {
331 cached_cie = cie_tmp;
334 if (cie_ptr < cie_tmp->cie_pointer)
335 rb_node = &(*rb_node)->
rb_left;
342 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
352 struct rb_node **rb_node = &fde_root.rb_node;
353 struct dwarf_fde *fde =
NULL;
359 struct dwarf_fde *fde_tmp;
360 unsigned long tmp_start, tmp_end;
365 tmp_start = fde_tmp->initial_location;
366 tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
368 if (pc < tmp_start) {
369 rb_node = &(*rb_node)->
rb_left;
380 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
399 static int dwarf_cfa_execute_insns(
unsigned char *insn_start,
400 unsigned char *insn_end,
401 struct dwarf_cie *cie,
402 struct dwarf_fde *fde,
403 struct dwarf_frame *frame,
407 unsigned char *current_insn;
409 struct dwarf_reg *regp;
411 current_insn = insn_start;
413 while (current_insn < insn_end && frame->pc <= pc) {
420 switch (DW_CFA_opcode(insn)) {
421 case DW_CFA_advance_loc:
422 delta = DW_CFA_operand(insn);
423 delta *= cie->code_alignment_factor;
428 reg = DW_CFA_operand(insn);
429 count = dwarf_read_uleb128(current_insn, &offset);
430 current_insn +=
count;
431 offset *= cie->data_alignment_factor;
432 regp = dwarf_frame_alloc_reg(frame, reg);
434 regp->flags |= DWARF_REG_OFFSET;
438 reg = DW_CFA_operand(insn);
450 case DW_CFA_advance_loc1:
451 delta = *current_insn++;
452 frame->pc += delta * cie->code_alignment_factor;
454 case DW_CFA_advance_loc2:
457 frame->pc += delta * cie->code_alignment_factor;
459 case DW_CFA_advance_loc4:
462 frame->pc += delta * cie->code_alignment_factor;
464 case DW_CFA_offset_extended:
465 count = dwarf_read_uleb128(current_insn, ®);
466 current_insn +=
count;
467 count = dwarf_read_uleb128(current_insn, &offset);
468 current_insn +=
count;
469 offset *= cie->data_alignment_factor;
471 case DW_CFA_restore_extended:
472 count = dwarf_read_uleb128(current_insn, ®);
473 current_insn +=
count;
475 case DW_CFA_undefined:
476 count = dwarf_read_uleb128(current_insn, ®);
477 current_insn +=
count;
478 regp = dwarf_frame_alloc_reg(frame, reg);
479 regp->flags |= DWARF_UNDEFINED;
482 count = dwarf_read_uleb128(current_insn,
483 &frame->cfa_register);
484 current_insn +=
count;
485 count = dwarf_read_uleb128(current_insn,
487 current_insn +=
count;
489 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
491 case DW_CFA_def_cfa_register:
492 count = dwarf_read_uleb128(current_insn,
493 &frame->cfa_register);
494 current_insn +=
count;
495 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
497 case DW_CFA_def_cfa_offset:
498 count = dwarf_read_uleb128(current_insn, &offset);
499 current_insn +=
count;
500 frame->cfa_offset =
offset;
502 case DW_CFA_def_cfa_expression:
503 count = dwarf_read_uleb128(current_insn, &expr_len);
504 current_insn +=
count;
506 frame->cfa_expr = current_insn;
507 frame->cfa_expr_len = expr_len;
508 current_insn += expr_len;
510 frame->flags |= DWARF_FRAME_CFA_REG_EXP;
512 case DW_CFA_offset_extended_sf:
513 count = dwarf_read_uleb128(current_insn, ®);
514 current_insn +=
count;
515 count = dwarf_read_leb128(current_insn, &offset);
516 current_insn +=
count;
517 offset *= cie->data_alignment_factor;
518 regp = dwarf_frame_alloc_reg(frame, reg);
519 regp->flags |= DWARF_REG_OFFSET;
522 case DW_CFA_val_offset:
523 count = dwarf_read_uleb128(current_insn, ®);
524 current_insn +=
count;
525 count = dwarf_read_leb128(current_insn, &offset);
526 offset *= cie->data_alignment_factor;
527 regp = dwarf_frame_alloc_reg(frame, reg);
528 regp->flags |= DWARF_VAL_OFFSET;
531 case DW_CFA_GNU_args_size:
532 count = dwarf_read_uleb128(current_insn, &offset);
533 current_insn +=
count;
535 case DW_CFA_GNU_negative_offset_extended:
536 count = dwarf_read_uleb128(current_insn, ®);
537 current_insn +=
count;
538 count = dwarf_read_uleb128(current_insn, &offset);
539 offset *= cie->data_alignment_factor;
541 regp = dwarf_frame_alloc_reg(frame, reg);
542 regp->flags |= DWARF_REG_OFFSET;
546 pr_debug(
"unhandled DWARF instruction 0x%x\n", insn);
561 dwarf_frame_free_regs(frame);
578 struct dwarf_frame *
prev)
580 struct dwarf_frame *
frame;
581 struct dwarf_cie *cie;
582 struct dwarf_fde *fde;
583 struct dwarf_reg *
reg;
590 if (!dwarf_unwinder_ready)
604 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
631 INIT_LIST_HEAD(&frame->reg_list);
634 frame->return_addr = 0;
656 cie = dwarf_lookup_cie(fde->cie_pointer);
658 frame->pc = fde->initial_location;
661 dwarf_cfa_execute_insns(cie->initial_instructions,
662 cie->instructions_end, cie, fde,
666 dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
670 switch (frame->flags) {
671 case DWARF_FRAME_CFA_REG_OFFSET:
673 reg = dwarf_frame_reg(prev, frame->cfa_register);
677 addr = prev->cfa + reg->addr;
688 frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
691 frame->cfa += frame->cfa_offset;
697 reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
704 if (!reg || reg->flags == DWARF_UNDEFINED)
709 addr = frame->cfa + reg->addr;
728 frame->return_addr = 0;
737 static int dwarf_parse_cie(
void *
entry,
void *
p,
unsigned long len,
740 struct rb_node **rb_node = &cie_root.rb_node;
741 struct rb_node *
parent = *rb_node;
742 struct dwarf_cie *cie;
758 cie->cie_pointer = (
unsigned long)entry;
760 cie->version = *(
char *)p++;
763 cie->augmentation =
p;
764 p +=
strlen(cie->augmentation) + 1;
766 count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
769 count = dwarf_read_leb128(p, &cie->data_alignment_factor);
776 if (cie->version == 1) {
780 count = dwarf_read_uleb128(p, &cie->return_address_reg);
784 if (cie->augmentation[0] ==
'z') {
786 cie->flags |= DWARF_CIE_Z_AUGMENTATION;
788 count = dwarf_read_uleb128(p, &length);
793 cie->initial_instructions = p +
length;
797 while (*cie->augmentation) {
802 if (*cie->augmentation ==
'L') {
805 }
else if (*cie->augmentation ==
'R') {
811 cie->encoding = *(
char *)p++;
813 }
else if (*cie->augmentation ==
'P') {
820 }
else if (*cie->augmentation ==
'S') {
827 p = cie->initial_instructions;
833 cie->initial_instructions =
p;
834 cie->instructions_end =
end;
840 struct dwarf_cie *cie_tmp;
846 if (cie->cie_pointer < cie_tmp->cie_pointer)
848 else if (cie->cie_pointer >= cie_tmp->cie_pointer)
854 rb_link_node(&cie->node, parent, rb_node);
857 #ifdef CONFIG_MODULES
862 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
867 static int dwarf_parse_fde(
void *entry,
u32 entry_type,
868 void *
start,
unsigned long len,
869 unsigned char *end,
struct module *mod)
871 struct rb_node **rb_node = &fde_root.rb_node;
872 struct rb_node *parent = *rb_node;
873 struct dwarf_fde *fde;
874 struct dwarf_cie *cie;
889 fde->cie_pointer = (
unsigned long)(p - entry_type - 4);
891 cie = dwarf_lookup_cie(fde->cie_pointer);
895 count = dwarf_read_encoded_value(p, &fde->initial_location,
898 count = dwarf_read_addr(p, &fde->initial_location);
903 count = dwarf_read_encoded_value(p, &fde->address_range,
904 cie->encoding & 0x0f);
906 count = dwarf_read_addr(p, &fde->address_range);
910 if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
912 count = dwarf_read_uleb128(p, &length);
917 fde->instructions =
p;
924 struct dwarf_fde *fde_tmp;
925 unsigned long tmp_start, tmp_end;
930 start = fde->initial_location;
931 end = fde->initial_location + fde->address_range;
933 tmp_start = fde_tmp->initial_location;
934 tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
938 if (start < tmp_start)
940 else if (start >= tmp_end)
946 rb_link_node(&fde->node, parent, rb_node);
949 #ifdef CONFIG_MODULES
954 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
965 struct dwarf_frame *
frame, *_frame;
966 unsigned long return_addr;
979 if (!frame || !frame->return_addr)
982 return_addr = frame->return_addr;
983 ops->
address(data, return_addr, 1);
990 static struct unwinder dwarf_unwinder = {
991 .name =
"dwarf-unwinder",
992 .dump = dwarf_unwinder_dump,
996 static void dwarf_unwinder_cleanup(
void)
998 struct rb_node **fde_rb_node = &fde_root.rb_node;
999 struct rb_node **cie_rb_node = &cie_root.rb_node;
1006 while (*fde_rb_node) {
1007 struct dwarf_fde *fde;
1014 while (*cie_rb_node) {
1015 struct dwarf_cie *cie;
1034 static int dwarf_parse_section(
char *eh_frame_start,
char *eh_frame_end,
1040 unsigned long len = 0;
1041 unsigned int c_entries, f_entries;
1046 entry = eh_frame_start;
1048 while ((
char *)entry < eh_frame_end) {
1051 count = dwarf_entry_len(p, &len);
1071 if (entry_type == DW_EH_FRAME_CIE) {
1072 err = dwarf_parse_cie(entry, p, len, end, mod);
1078 err = dwarf_parse_fde(entry, entry_type, p, len,
1086 entry = (
char *)entry + len + 4;
1089 printk(
KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
1090 c_entries, f_entries);
1098 #ifdef CONFIG_MODULES
1102 unsigned int i,
err;
1104 char *secstrings = (
void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1108 for (i = 1; i < hdr->e_shnum; i++) {
1111 && !
strcmp(secstrings+sechdrs[i].sh_name,
".eh_frame")) {
1112 start = sechdrs[
i].sh_addr;
1113 end = start + sechdrs[
i].sh_size;
1119 if (i != hdr->e_shnum) {
1120 INIT_LIST_HEAD(&me->arch.cie_list);
1121 INIT_LIST_HEAD(&me->arch.fde_list);
1122 err = dwarf_parse_section((
char *)start, (
char *)end, me);
1142 struct dwarf_fde *fde, *ftmp;
1143 struct dwarf_cie *cie, *ctmp;
1144 unsigned long flags;
1154 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
1164 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
1177 static int __init dwarf_unwinder_init(
void)
1182 sizeof(
struct dwarf_frame), 0,
1186 sizeof(
struct dwarf_reg), 0,
1192 dwarf_frame_cachep);
1193 if (!dwarf_frame_pool)
1200 if (!dwarf_reg_pool)
1203 err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame,
NULL);
1211 dwarf_unwinder_ready = 1;
1216 printk(
KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
1217 dwarf_unwinder_cleanup();