[PATCH] i386/x86-64: Work around gcc bug with noreturn functions in unwinder

Current gcc generates calls not jumps to noreturn functions. When that happens the
return address can point to the next function, which confuses the unwinder.

This patch works around it by marking asynchronous exception
frames in contrast normal call frames in the unwind information.  Then teach
the unwinder to decode this.

For normal call frames the unwinder now subtracts one from the address which avoids
this problem.  The standard libgcc unwinder uses the same trick.

It doesn't include adjustment of the printed address (i.e. for the original
example, it'd still be kernel_math_error+0 that gets displayed, but the
unwinder wouldn't get confused anymore.

This only works with binutils 2.6.17+ and some versions of H.J.Lu's 2.6.16
unfortunately because earlier binutils don't support .cfi_signal_frame

[AK: added automatic detection of the new binutils and wrote description]

Signed-off-by: Jan Beulich <jbeulich@novell.com>
Signed-off-by: Andi Kleen <ak@suse.de>
This commit is contained in:
Jan Beulich 2006-09-26 10:52:41 +02:00 committed by Andi Kleen
parent ab2e0b46cb
commit adf1423698
11 changed files with 73 additions and 9 deletions

View file

@ -50,6 +50,10 @@ CFLAGS += $(shell if [ $(call cc-version) -lt 0400 ] ; then echo $(call cc-op
cflags-y += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,) cflags-y += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,) AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
# is .cfi_signal_frame supported too?
cflags-y += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
CFLAGS += $(cflags-y) CFLAGS += $(cflags-y)
# Default subarch .c files # Default subarch .c files

View file

@ -183,18 +183,21 @@ VM_MASK = 0x00020000
#define RING0_INT_FRAME \ #define RING0_INT_FRAME \
CFI_STARTPROC simple;\ CFI_STARTPROC simple;\
CFI_SIGNAL_FRAME;\
CFI_DEF_CFA esp, 3*4;\ CFI_DEF_CFA esp, 3*4;\
/*CFI_OFFSET cs, -2*4;*/\ /*CFI_OFFSET cs, -2*4;*/\
CFI_OFFSET eip, -3*4 CFI_OFFSET eip, -3*4
#define RING0_EC_FRAME \ #define RING0_EC_FRAME \
CFI_STARTPROC simple;\ CFI_STARTPROC simple;\
CFI_SIGNAL_FRAME;\
CFI_DEF_CFA esp, 4*4;\ CFI_DEF_CFA esp, 4*4;\
/*CFI_OFFSET cs, -2*4;*/\ /*CFI_OFFSET cs, -2*4;*/\
CFI_OFFSET eip, -3*4 CFI_OFFSET eip, -3*4
#define RING0_PTREGS_FRAME \ #define RING0_PTREGS_FRAME \
CFI_STARTPROC simple;\ CFI_STARTPROC simple;\
CFI_SIGNAL_FRAME;\
CFI_DEF_CFA esp, OLDESP-EBX;\ CFI_DEF_CFA esp, OLDESP-EBX;\
/*CFI_OFFSET cs, CS-OLDESP;*/\ /*CFI_OFFSET cs, CS-OLDESP;*/\
CFI_OFFSET eip, EIP-OLDESP;\ CFI_OFFSET eip, EIP-OLDESP;\
@ -275,6 +278,7 @@ need_resched:
# sysenter call handler stub # sysenter call handler stub
ENTRY(sysenter_entry) ENTRY(sysenter_entry)
CFI_STARTPROC simple CFI_STARTPROC simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA esp, 0 CFI_DEF_CFA esp, 0
CFI_REGISTER esp, ebp CFI_REGISTER esp, ebp
movl TSS_sysenter_esp0(%esp),%esp movl TSS_sysenter_esp0(%esp),%esp

View file

@ -58,6 +58,10 @@ cflags-y += $(call cc-option,-mno-sse -mno-mmx -mno-sse2 -mno-3dnow,)
cflags-y += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,) cflags-y += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,) AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_endproc,-DCONFIG_AS_CFI=1,)
# is .cfi_signal_frame supported too?
cflags-y += $(call as-instr,.cfi_startproc\n.cfi_signal_frame\n.cfi_endproc,-DCONFIG_AS_CFI_SIGNAL_FRAME=1,)
AFLAGS += $(call as-instr,.cfi_startproc\n.cfi_signal_frame\n.cfi_endproc,-DCONFIG_AS_CFI_SIGNAL_FRAME=1,)
cflags-$(CONFIG_CC_STACKPROTECTOR) += $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-x86_64-has-stack-protector.sh $(CC) -fstack-protector ) cflags-$(CONFIG_CC_STACKPROTECTOR) += $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-x86_64-has-stack-protector.sh $(CC) -fstack-protector )
cflags-$(CONFIG_CC_STACKPROTECTOR_ALL) += $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-x86_64-has-stack-protector.sh $(CC) -fstack-protector-all ) cflags-$(CONFIG_CC_STACKPROTECTOR_ALL) += $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-x86_64-has-stack-protector.sh $(CC) -fstack-protector-all )

View file

@ -71,6 +71,7 @@
*/ */
ENTRY(ia32_sysenter_target) ENTRY(ia32_sysenter_target)
CFI_STARTPROC32 simple CFI_STARTPROC32 simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,0 CFI_DEF_CFA rsp,0
CFI_REGISTER rsp,rbp CFI_REGISTER rsp,rbp
swapgs swapgs
@ -186,6 +187,7 @@ ENDPROC(ia32_sysenter_target)
*/ */
ENTRY(ia32_cstar_target) ENTRY(ia32_cstar_target)
CFI_STARTPROC32 simple CFI_STARTPROC32 simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,PDA_STACKOFFSET CFI_DEF_CFA rsp,PDA_STACKOFFSET
CFI_REGISTER rip,rcx CFI_REGISTER rip,rcx
/*CFI_REGISTER rflags,r11*/ /*CFI_REGISTER rflags,r11*/
@ -293,6 +295,7 @@ ia32_badarg:
ENTRY(ia32_syscall) ENTRY(ia32_syscall)
CFI_STARTPROC simple CFI_STARTPROC simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,SS+8-RIP CFI_DEF_CFA rsp,SS+8-RIP
/*CFI_REL_OFFSET ss,SS-RIP*/ /*CFI_REL_OFFSET ss,SS-RIP*/
CFI_REL_OFFSET rsp,RSP-RIP CFI_REL_OFFSET rsp,RSP-RIP
@ -370,6 +373,7 @@ ENTRY(ia32_ptregs_common)
popq %r11 popq %r11
CFI_ENDPROC CFI_ENDPROC
CFI_STARTPROC32 simple CFI_STARTPROC32 simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,SS+8-ARGOFFSET CFI_DEF_CFA rsp,SS+8-ARGOFFSET
CFI_REL_OFFSET rax,RAX-ARGOFFSET CFI_REL_OFFSET rax,RAX-ARGOFFSET
CFI_REL_OFFSET rcx,RCX-ARGOFFSET CFI_REL_OFFSET rcx,RCX-ARGOFFSET

View file

@ -123,6 +123,7 @@
.macro CFI_DEFAULT_STACK start=1 .macro CFI_DEFAULT_STACK start=1
.if \start .if \start
CFI_STARTPROC simple CFI_STARTPROC simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,SS+8 CFI_DEF_CFA rsp,SS+8
.else .else
CFI_DEF_CFA_OFFSET SS+8 CFI_DEF_CFA_OFFSET SS+8
@ -207,6 +208,7 @@ END(ret_from_fork)
ENTRY(system_call) ENTRY(system_call)
CFI_STARTPROC simple CFI_STARTPROC simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,PDA_STACKOFFSET CFI_DEF_CFA rsp,PDA_STACKOFFSET
CFI_REGISTER rip,rcx CFI_REGISTER rip,rcx
/*CFI_REGISTER rflags,r11*/ /*CFI_REGISTER rflags,r11*/
@ -324,6 +326,7 @@ END(system_call)
*/ */
ENTRY(int_ret_from_sys_call) ENTRY(int_ret_from_sys_call)
CFI_STARTPROC simple CFI_STARTPROC simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,SS+8-ARGOFFSET CFI_DEF_CFA rsp,SS+8-ARGOFFSET
/*CFI_REL_OFFSET ss,SS-ARGOFFSET*/ /*CFI_REL_OFFSET ss,SS-ARGOFFSET*/
CFI_REL_OFFSET rsp,RSP-ARGOFFSET CFI_REL_OFFSET rsp,RSP-ARGOFFSET
@ -484,6 +487,7 @@ END(stub_rt_sigreturn)
*/ */
.macro _frame ref .macro _frame ref
CFI_STARTPROC simple CFI_STARTPROC simple
CFI_SIGNAL_FRAME
CFI_DEF_CFA rsp,SS+8-\ref CFI_DEF_CFA rsp,SS+8-\ref
/*CFI_REL_OFFSET ss,SS-\ref*/ /*CFI_REL_OFFSET ss,SS-\ref*/
CFI_REL_OFFSET rsp,RSP-\ref CFI_REL_OFFSET rsp,RSP-\ref

View file

@ -28,6 +28,12 @@
#define CFI_RESTORE_STATE .cfi_restore_state #define CFI_RESTORE_STATE .cfi_restore_state
#define CFI_UNDEFINED .cfi_undefined #define CFI_UNDEFINED .cfi_undefined
#ifdef CONFIG_AS_CFI_SIGNAL_FRAME
#define CFI_SIGNAL_FRAME .cfi_signal_frame
#else
#define CFI_SIGNAL_FRAME
#endif
#else #else
/* Due to the structure of pre-exisiting code, don't use assembler line /* Due to the structure of pre-exisiting code, don't use assembler line
@ -48,6 +54,7 @@
#define CFI_REMEMBER_STATE ignore #define CFI_REMEMBER_STATE ignore
#define CFI_RESTORE_STATE ignore #define CFI_RESTORE_STATE ignore
#define CFI_UNDEFINED ignore #define CFI_UNDEFINED ignore
#define CFI_SIGNAL_FRAME ignore
#endif #endif

View file

@ -18,6 +18,7 @@ struct unwind_frame_info
{ {
struct pt_regs regs; struct pt_regs regs;
struct task_struct *task; struct task_struct *task;
unsigned call_frame:1;
}; };
#define UNW_PC(frame) (frame)->regs.eip #define UNW_PC(frame) (frame)->regs.eip
@ -44,6 +45,10 @@ struct unwind_frame_info
PTREGS_INFO(edi), \ PTREGS_INFO(edi), \
PTREGS_INFO(eip) PTREGS_INFO(eip)
#define UNW_DEFAULT_RA(raItem, dataAlign) \
((raItem).where == Memory && \
!((raItem).value * (dataAlign) + 4))
static inline void arch_unw_init_frame_info(struct unwind_frame_info *info, static inline void arch_unw_init_frame_info(struct unwind_frame_info *info,
/*const*/ struct pt_regs *regs) /*const*/ struct pt_regs *regs)
{ {

View file

@ -28,6 +28,11 @@
#define CFI_REMEMBER_STATE .cfi_remember_state #define CFI_REMEMBER_STATE .cfi_remember_state
#define CFI_RESTORE_STATE .cfi_restore_state #define CFI_RESTORE_STATE .cfi_restore_state
#define CFI_UNDEFINED .cfi_undefined #define CFI_UNDEFINED .cfi_undefined
#ifdef CONFIG_AS_CFI_SIGNAL_FRAME
#define CFI_SIGNAL_FRAME .cfi_signal_frame
#else
#define CFI_SIGNAL_FRAME
#endif
#else #else
@ -45,6 +50,7 @@
#define CFI_REMEMBER_STATE # #define CFI_REMEMBER_STATE #
#define CFI_RESTORE_STATE # #define CFI_RESTORE_STATE #
#define CFI_UNDEFINED # #define CFI_UNDEFINED #
#define CFI_SIGNAL_FRAME #
#endif #endif

View file

@ -18,6 +18,7 @@ struct unwind_frame_info
{ {
struct pt_regs regs; struct pt_regs regs;
struct task_struct *task; struct task_struct *task;
unsigned call_frame:1;
}; };
#define UNW_PC(frame) (frame)->regs.rip #define UNW_PC(frame) (frame)->regs.rip
@ -57,6 +58,10 @@ struct unwind_frame_info
PTREGS_INFO(r15), \ PTREGS_INFO(r15), \
PTREGS_INFO(rip) PTREGS_INFO(rip)
#define UNW_DEFAULT_RA(raItem, dataAlign) \
((raItem).where == Memory && \
!((raItem).value * (dataAlign) + 8))
static inline void arch_unw_init_frame_info(struct unwind_frame_info *info, static inline void arch_unw_init_frame_info(struct unwind_frame_info *info,
/*const*/ struct pt_regs *regs) /*const*/ struct pt_regs *regs)
{ {

View file

@ -603,6 +603,7 @@ int unwind(struct unwind_frame_info *frame)
#define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs]) #define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
const u32 *fde = NULL, *cie = NULL; const u32 *fde = NULL, *cie = NULL;
const u8 *ptr = NULL, *end = NULL; const u8 *ptr = NULL, *end = NULL;
unsigned long pc = UNW_PC(frame) - frame->call_frame;
unsigned long startLoc = 0, endLoc = 0, cfa; unsigned long startLoc = 0, endLoc = 0, cfa;
unsigned i; unsigned i;
signed ptrType = -1; signed ptrType = -1;
@ -612,7 +613,7 @@ int unwind(struct unwind_frame_info *frame)
if (UNW_PC(frame) == 0) if (UNW_PC(frame) == 0)
return -EINVAL; return -EINVAL;
if ((table = find_table(UNW_PC(frame))) != NULL if ((table = find_table(pc)) != NULL
&& !(table->size & (sizeof(*fde) - 1))) { && !(table->size & (sizeof(*fde) - 1))) {
unsigned long tableSize = table->size; unsigned long tableSize = table->size;
@ -647,7 +648,7 @@ int unwind(struct unwind_frame_info *frame)
ptrType & DW_EH_PE_indirect ptrType & DW_EH_PE_indirect
? ptrType ? ptrType
: ptrType & (DW_EH_PE_FORM|DW_EH_PE_signed)); : ptrType & (DW_EH_PE_FORM|DW_EH_PE_signed));
if (UNW_PC(frame) >= startLoc && UNW_PC(frame) < endLoc) if (pc >= startLoc && pc < endLoc)
break; break;
cie = NULL; cie = NULL;
} }
@ -657,16 +658,28 @@ int unwind(struct unwind_frame_info *frame)
state.cieEnd = ptr; /* keep here temporarily */ state.cieEnd = ptr; /* keep here temporarily */
ptr = (const u8 *)(cie + 2); ptr = (const u8 *)(cie + 2);
end = (const u8 *)(cie + 1) + *cie; end = (const u8 *)(cie + 1) + *cie;
frame->call_frame = 1;
if ((state.version = *ptr) != 1) if ((state.version = *ptr) != 1)
cie = NULL; /* unsupported version */ cie = NULL; /* unsupported version */
else if (*++ptr) { else if (*++ptr) {
/* check if augmentation size is first (and thus present) */ /* check if augmentation size is first (and thus present) */
if (*ptr == 'z') { if (*ptr == 'z') {
/* check for ignorable (or already handled) while (++ptr < end && *ptr) {
* nul-terminated augmentation string */ switch(*ptr) {
while (++ptr < end && *ptr) /* check for ignorable (or already handled)
if (strchr("LPR", *ptr) == NULL) * nul-terminated augmentation string */
case 'L':
case 'P':
case 'R':
continue;
case 'S':
frame->call_frame = 0;
continue;
default:
break; break;
}
break;
}
} }
if (ptr >= end || *ptr) if (ptr >= end || *ptr)
cie = NULL; cie = NULL;
@ -755,7 +768,7 @@ int unwind(struct unwind_frame_info *frame)
state.org = startLoc; state.org = startLoc;
memcpy(&state.cfa, &badCFA, sizeof(state.cfa)); memcpy(&state.cfa, &badCFA, sizeof(state.cfa));
/* process instructions */ /* process instructions */
if (!processCFI(ptr, end, UNW_PC(frame), ptrType, &state) if (!processCFI(ptr, end, pc, ptrType, &state)
|| state.loc > endLoc || state.loc > endLoc
|| state.regs[retAddrReg].where == Nowhere || state.regs[retAddrReg].where == Nowhere
|| state.cfa.reg >= ARRAY_SIZE(reg_info) || state.cfa.reg >= ARRAY_SIZE(reg_info)
@ -763,6 +776,11 @@ int unwind(struct unwind_frame_info *frame)
|| state.cfa.offs % sizeof(unsigned long)) || state.cfa.offs % sizeof(unsigned long))
return -EIO; return -EIO;
/* update frame */ /* update frame */
#ifndef CONFIG_AS_CFI_SIGNAL_FRAME
if(frame->call_frame
&& !UNW_DEFAULT_RA(state.regs[retAddrReg], state.dataAlign))
frame->call_frame = 0;
#endif
cfa = FRAME_REG(state.cfa.reg, unsigned long) + state.cfa.offs; cfa = FRAME_REG(state.cfa.reg, unsigned long) + state.cfa.offs;
startLoc = min((unsigned long)UNW_SP(frame), cfa); startLoc = min((unsigned long)UNW_SP(frame), cfa);
endLoc = max((unsigned long)UNW_SP(frame), cfa); endLoc = max((unsigned long)UNW_SP(frame), cfa);
@ -866,6 +884,7 @@ int unwind_init_frame_info(struct unwind_frame_info *info,
/*const*/ struct pt_regs *regs) /*const*/ struct pt_regs *regs)
{ {
info->task = tsk; info->task = tsk;
info->call_frame = 0;
arch_unw_init_frame_info(info, regs); arch_unw_init_frame_info(info, regs);
return 0; return 0;
@ -879,6 +898,7 @@ int unwind_init_blocked(struct unwind_frame_info *info,
struct task_struct *tsk) struct task_struct *tsk)
{ {
info->task = tsk; info->task = tsk;
info->call_frame = 0;
arch_unw_init_blocked(info); arch_unw_init_blocked(info);
return 0; return 0;
@ -894,6 +914,7 @@ int unwind_init_running(struct unwind_frame_info *info,
void *arg) void *arg)
{ {
info->task = current; info->task = current;
info->call_frame = 0;
return arch_unwind_init_running(info, callback, arg); return arch_unwind_init_running(info, callback, arg);
} }

View file

@ -66,8 +66,8 @@ as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
# as-instr # as-instr
# Usage: cflags-y += $(call as-instr, instr, option1, option2) # Usage: cflags-y += $(call as-instr, instr, option1, option2)
as-instr = $(shell if echo -e "$(1)" | $(AS) -Z -o astest$$$$.out \ as-instr = $(shell if echo -e "$(1)" | $(AS) >/dev/null 2>&1 -W -Z -o astest$$$$.out ; \
2>&1 >/dev/null ; then echo "$(2)"; else echo "$(3)"; fi; \ then echo "$(2)"; else echo "$(3)"; fi; \
rm -f astest$$$$.out) rm -f astest$$$$.out)
# cc-option # cc-option