From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 105031 invoked by alias); 4 Sep 2019 14:19:27 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 105022 invoked by uid 89); 4 Sep 2019 14:19:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.0 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=libgcc X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.110.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 04 Sep 2019 14:19:25 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 49C8628; Wed, 4 Sep 2019 07:19:24 -0700 (PDT) Received: from [10.2.206.47] (e120808-lin.cambridge.arm.com [10.2.206.47]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DF36D3F59C; Wed, 4 Sep 2019 07:19:23 -0700 (PDT) Subject: Re: [ARM/FDPIC v5 11/21] [ARM] FDPIC: Add support to unwind FDPIC signal frame To: Christophe Lyon , "gcc-patches@gcc.gnu.org" References: <20190515124006.25840-1-christophe.lyon@st.com> <20190515124006.25840-12-christophe.lyon@st.com> From: Kyrill Tkachov Message-ID: <06f46aa0-e799-0236-60b4-b1fe708ad247@foss.arm.com> Date: Wed, 04 Sep 2019 14:19:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 In-Reply-To: <20190515124006.25840-12-christophe.lyon@st.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-SW-Source: 2019-09/txt/msg00196.txt.bz2 Hi Christophe, On 5/15/19 1:39 PM, Christophe Lyon wrote: > 2019-XX-XX  Christophe Lyon >         Mickaël Guêné > >         libgcc/ >         * unwind-arm-common.inc (ARM_SET_R7_RT_SIGRETURN) >         (THUMB2_SET_R7_RT_SIGRETURN, FDPIC_LDR_R12_WITH_FUNCDESC) >         (FDPIC_LDR_R9_WITH_GOT, FDPIC_LDR_PC_WITH_RESTORER) >         (FDPIC_FUNCDESC_OFFSET, ARM_NEW_RT_SIGFRAME_UCONTEXT) >         (ARM_UCONTEXT_SIGCONTEXT, ARM_SIGCONTEXT_R0, > FDPIC_T2_LDR_R12_WITH_FUNCDESC) >         (FDPIC_T2_LDR_R9_WITH_GOT, FDPIC_T2_LDR_PC_WITH_RESTORER): New. >         (__gnu_personality_sigframe_fdpic): New. >         (get_eit_entry): Add FDPIC signal frame support. > At this point I'm looking that the code is FDPIC-specific and thus won't break other targets. I'll have to trust your testing that this is correct for FDPIC. Ok. Thanks, Kyrill > Change-Id: I7f9527cc50665dd1a731b7badf71c319fb38bf57 > > diff --git a/libgcc/unwind-arm-common.inc b/libgcc/unwind-arm-common.inc > index 0bacc11..c9a82b7 100644 > --- a/libgcc/unwind-arm-common.inc > +++ b/libgcc/unwind-arm-common.inc > @@ -30,6 +30,26 @@ >  #include >  #endif > > +#if __FDPIC__ > +/* Load r7 with rt_sigreturn value.  */ > +#define ARM_SET_R7_RT_SIGRETURN 0xe3a070ad      /* mov   r7, #0xad */ > +#define THUMB2_SET_R7_RT_SIGRETURN     0x07adf04f      /* mov.w r7, > #0xad */ > + > +/* FDPIC jump to restorer sequence.  */ > +#define FDPIC_LDR_R12_WITH_FUNCDESC    0xe59fc004      /* ldr   r12, > [pc, #4] */ > +#define FDPIC_LDR_R9_WITH_GOT          0xe59c9004      /* ldr   r9, > [r12, #4] */ > +#define FDPIC_LDR_PC_WITH_RESTORER     0xe59cf000      /* ldr   pc, > [r12] */ > +#define FDPIC_T2_LDR_R12_WITH_FUNCDESC  0xc008f8df     /* ldr.w r12, > [pc, #8] */ > +#define FDPIC_T2_LDR_R9_WITH_GOT       0x9004f8dc      /* ldr.w r9, > [r12, #4] */ > +#define FDPIC_T2_LDR_PC_WITH_RESTORER   0xf000f8dc     /* ldr.w pc, > [r12] */ > +#define FDPIC_FUNCDESC_OFFSET          12 > + > +/* Signal frame offsets.  */ > +#define ARM_NEW_RT_SIGFRAME_UCONTEXT   0x80 > +#define ARM_UCONTEXT_SIGCONTEXT                0x14 > +#define ARM_SIGCONTEXT_R0              0xc > +#endif > + >  /* We add a prototype for abort here to avoid creating a dependency on >     target headers.  */ >  extern void abort (void); > @@ -199,6 +219,45 @@ search_EIT_table (const __EIT_entry * table, int > nrec, _uw return_address) >      } >  } > > +#if __FDPIC__ > +/* VFP is not restored, but this is sufficient to allow unwinding.  */ > +static _Unwind_Reason_Code > +__gnu_personality_sigframe_fdpic (_Unwind_State state, > +                                 _Unwind_Control_Block *ucbp, > +                                 _Unwind_Context *context) > +{ > +    unsigned int sp; > +    unsigned int pc; > +    unsigned int funcdesc; > +    unsigned int handler; > +    unsigned int first_handler_instruction; > +    int i; > + > +    _Unwind_VRS_Get (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32, &sp); > +    _Unwind_VRS_Get (context, _UVRSC_CORE, R_PC, _UVRSD_UINT32, &pc); > + > +    funcdesc = *(unsigned int *)((pc & ~1) + FDPIC_FUNCDESC_OFFSET); > +    handler = *(unsigned int *)(funcdesc); > +    first_handler_instruction = *(unsigned int *)(handler & ~1); > + > +    /* Adjust SP to point to the start of registers according to > +       signal type.  */ > +    if (first_handler_instruction == ARM_SET_R7_RT_SIGRETURN > +       || first_handler_instruction == THUMB2_SET_R7_RT_SIGRETURN) > +       sp += ARM_NEW_RT_SIGFRAME_UCONTEXT > +         + ARM_UCONTEXT_SIGCONTEXT > +         + ARM_SIGCONTEXT_R0; > +    else > +       sp += ARM_UCONTEXT_SIGCONTEXT > +         + ARM_SIGCONTEXT_R0; > +    /* Restore regs saved on stack by the kernel.  */ > +    for (i = 0; i < 16; i++) > +       _Unwind_VRS_Set (context, _UVRSC_CORE, i, _UVRSD_UINT32, sp + > 4 * i); > + > +    return _URC_CONTINUE_UNWIND; > +} > +#endif > + >  /* Find the exception index table eintry for the given address. >     Fill in the relevant fields of the UCB. >     Returns _URC_FAILURE if an error occurred, _URC_OK on success.  */ > @@ -222,6 +281,27 @@ get_eit_entry (_Unwind_Control_Block *ucbp, _uw > return_address) > &nrec); >        if (!eitp) >          { > +#if __FDPIC__ > +         /* If we are unwinding a signal handler then perhaps we have > +            reached a trampoline.  Try to detect jump to restorer > +            sequence.  */ > +         _uw *pc = (_uw *)((return_address+2) & ~1); > +         if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC > +              && pc[1] == FDPIC_LDR_R9_WITH_GOT > +              && pc[2] == FDPIC_LDR_PC_WITH_RESTORER) > +             || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC > +                 && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT > +                 && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER)) > +           { > +             struct funcdesc_t *funcdesc > +               = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic; > + > +             UCB_PR_ADDR (ucbp) = funcdesc->ptr; > +             UCB_PR_GOT (ucbp) = funcdesc->got; > + > +             return _URC_OK; > +           } > +#endif >            UCB_PR_ADDR (ucbp) = 0; >            return _URC_FAILURE; >          } > @@ -236,6 +316,27 @@ get_eit_entry (_Unwind_Control_Block *ucbp, _uw > return_address) > >    if (!eitp) >      { > +#if __FDPIC__ > +      /* If we are unwinding a signal handler then perhaps we have > +        reached a trampoline.  Try to detect jump to restorer > +        sequence.  */ > +      _uw *pc = (_uw *)((return_address+2) & ~1); > +      if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC > +          && pc[1] == FDPIC_LDR_R9_WITH_GOT > +          && pc[2] == FDPIC_LDR_PC_WITH_RESTORER) > +         || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC > +             && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT > +             && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER)) > +       { > +         struct funcdesc_t *funcdesc > +           = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic; > + > +         UCB_PR_ADDR (ucbp) = funcdesc->ptr; > +         UCB_PR_GOT (ucbp) = funcdesc->got; > + > +         return _URC_OK; > +       } > +#endif >        UCB_PR_ADDR (ucbp) = 0; >        return _URC_FAILURE; >      } > @@ -244,6 +345,27 @@ get_eit_entry (_Unwind_Control_Block *ucbp, _uw > return_address) >    /* Can this frame be unwound at all?  */ >    if (eitp->content == EXIDX_CANTUNWIND) >      { > +#if __FDPIC__ > +      /* If we are unwinding a signal handler then perhaps we have > +        reached a trampoline.  Try to detect jump to restorer > +        sequence.  */ > +      _uw *pc = (_uw *)((return_address+2) & ~1); > +      if ((pc[0] == FDPIC_LDR_R12_WITH_FUNCDESC > +          && pc[1] == FDPIC_LDR_R9_WITH_GOT > +          && pc[2] == FDPIC_LDR_PC_WITH_RESTORER) > +         || (pc[0] == FDPIC_T2_LDR_R12_WITH_FUNCDESC > +             && pc[1] == FDPIC_T2_LDR_R9_WITH_GOT > +             && pc[2] == FDPIC_T2_LDR_PC_WITH_RESTORER)) > +       { > +         struct funcdesc_t *funcdesc > +           = (struct funcdesc_t *) &__gnu_personality_sigframe_fdpic; > + > +         UCB_PR_ADDR (ucbp) = funcdesc->ptr; > +         UCB_PR_GOT (ucbp) = funcdesc->got; > + > +         return _URC_OK; > +       } > +#endif >        UCB_PR_ADDR (ucbp) = 0; >        return _URC_END_OF_STACK; >      } > -- > 2.6.3 >