On 08/06/18 11:15, Kyrill Tkachov wrote:
> Hi Christophe,
> 
> On 25/05/18 09:03, Christophe Lyon wrote:
>> When restoring a function address, we also have to restore the FDPIC
>> register value (r9).
>>
>> 2018-XX-XX  Christophe Lyon  <christophe.l...@st.com>
>>         Mickaël Guêné <mickael.gu...@st.com>
>>
>>         gcc/
>>         * ginclude/unwind-arm-common.h (unwinder_cache): Add reserved5
>>         field.
>>
>>         libgcc/
>>         * config/arm/linux-atomic.c (__ARM_ARCH__): Define.
>>         (__kernel_cmpxchg): Add FDPIC support.
>>         (__kernel_dmb): Likewise.
>>         (__fdpic_cmpxchg): New function.
>>         (__fdpic_dmb): New function.
>>         * config/arm/unwind-arm.h (gnu_Unwind_Find_got): New function.
>>         (_Unwind_decode_typeinfo_ptr): Add FDPIC support.
>>         * unwindo-arm-common.inc (UCB_PR_GOT): New.
>>         (funcdesc_t): New struct.
>>         (get_eit_entry): Add FDPIC support.
>>         (unwind_phase2): Likewise.
>>         (unwind_phase2_forced): Likewise.
>>         (__gnu_Unwind_RaiseException): Likewise.
>>         (__gnu_Unwind_Resume): Likewise.
>>         (__gnu_Unwind_Backtrace): Likewise.
>>         * unwind-pe.h (read_encoded_value_with_base): Likewise.
>>
>>         libstdc++/
>>         * libsupc++/eh_personality.cc (get_ttype_entry): Add FDPIC
>>         support.
>>
>> Change-Id: Ic0841eb3d7bfb0b3f6d187cd52a660b8fd394d85
>>
>> diff --git a/gcc/ginclude/unwind-arm-common.h
>> b/gcc/ginclude/unwind-arm-common.h
>> index 8a1a919..150bd0f 100644
>> --- a/gcc/ginclude/unwind-arm-common.h
>> +++ b/gcc/ginclude/unwind-arm-common.h
>> @@ -91,7 +91,7 @@ extern "C" {
>>            _uw reserved2;  /* Personality routine address */
>>            _uw reserved3;  /* Saved callsite address */
>>            _uw reserved4;  /* Forced unwind stop arg */
>> -         _uw reserved5;
>> +         _uw reserved5;  /* Personality routine GOT value in FDPIC
>> mode.  */
>>          }
>>        unwinder_cache;
>>        /* Propagation barrier cache (valid after phase 1): */
>> diff --git a/libgcc/config/arm/linux-atomic.c
>> b/libgcc/config/arm/linux-atomic.c
>> index d334c58..a20ad94 100644
>> --- a/libgcc/config/arm/linux-atomic.c
>> +++ b/libgcc/config/arm/linux-atomic.c
>> @@ -23,13 +23,99 @@ a copy of the GCC Runtime Library Exception along
>> with this program;
>>  see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
>>  <http://www.gnu.org/licenses/>. */
>>
>> +#if defined(__ARM_ARCH_2__)
>> +# define __ARM_ARCH__ 2
>> +#endif
>> +
>> +#if defined(__ARM_ARCH_3__)
>> +# define __ARM_ARCH__ 3
>> +#endif
>> +
>> +#if defined(__ARM_ARCH_3M__) || defined(__ARM_ARCH_4__) \
>> +  || defined(__ARM_ARCH_4T__)
>> +/* We use __ARM_ARCH__ set to 4 here, but in reality it's any
>> processor with
>> +   long multiply instructions.  That includes v3M.  */
>> +# define __ARM_ARCH__ 4
>> +#endif
>> +
> 
> Support for __ARM_ARCH_2__, __ARM_ARCH_3__, __ARM_ARCH_3M__ has been
> removed in GCC 9
> so this code is dead.

Better still, use the ACLE pre-defines rather than the awkward GCC
versions which need updating each time a new architecture variant is added.

R.

> 
> I notice that in the removal I've missed out an occurrence of these in
> config/arm/lib1funcs.S.
> If you want to remove those occurrences as a separate patch that would
> be preapproved.
> 
> Thanks,
> Kyrill
> 
>> +#if defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) \
>> +  || defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) \
>> +  || defined(__ARM_ARCH_5TEJ__)
>> +# define __ARM_ARCH__ 5
>> +#endif
>> +
>> +#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \
>> +  || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) \
>> +  || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) \
>> +  || defined(__ARM_ARCH_6M__)
>> +# define __ARM_ARCH__ 6
>> +#endif
>> +
>> +#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
>> +  || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
>> +  || defined(__ARM_ARCH_7EM__)
>> +# define __ARM_ARCH__ 7
>> +#endif
>> +
>> +#ifndef __ARM_ARCH__
>> +#error Unable to determine architecture.
>> +#endif
>> +
>>  /* Kernel helper for compare-and-exchange.  */
>>  typedef int (__kernel_cmpxchg_t) (int oldval, int newval, int *ptr);
>> +#if __FDPIC__
>> +#define __kernel_cmpxchg __fdpic_cmpxchg
>> +#else
>>  #define __kernel_cmpxchg (*(__kernel_cmpxchg_t *) 0xffff0fc0)
>> +#endif
>>
>>  /* Kernel helper for memory barrier.  */
>>  typedef void (__kernel_dmb_t) (void);
>> +#if __FDPIC__
>> +#define __kernel_dmb __fdpic_dmb
>> +#else
>>  #define __kernel_dmb (*(__kernel_dmb_t *) 0xffff0fa0)
>> +#endif
>> +
>> +#if __FDPIC__
>> +static int __fdpic_cmpxchg (int oldval, int newval, int *ptr)
>> +{
>> +#if __ARM_ARCH__ < 6
>> +  #error architecture support not yet implemented
>> +  /* Use swap instruction (but is it always safe ? (interrupt?))  */
>> +#else
>> +  int result;
>> +
>> +  asm volatile ("1: ldrex r3, [%[ptr]]\n\t"
>> +               "subs  r3, r3, %[oldval]\n\t"
>> +               "itt eq\n\t"
>> +               "strexeq r3, %[newval], [%[ptr]]\n\t"
>> +               "teqeq r3, #1\n\t"
>> +               "it eq\n\t"
>> +               "beq 1b\n\t"
>> +               "rsbs  %[result], r3, #0\n\t"
>> +               : [result] "=r" (result)
>> +               : [oldval] "r" (oldval) , [newval] "r" (newval), [ptr]
>> "r" (ptr)
>> +               : "r3");
>> +    return result;
>> +#endif
>> +}
>> +
>> +static void __fdpic_dmb ()
>> +{
>> +#if __ARM_ARCH__ < 6
>> +  /* No op? Perhaps flush write buffer ?  */
>> +  return ;
>> +#else
>> + #if __ARM_ARCH__ >= 7
>> +  asm volatile ("dmb\n\t");
>> + #elif __ARM_ARCH__ == 6
>> +  asm volatile ("mcr p15, 0, r0, c7, c10, 5\n\t");
>> + #endif
>> +#endif
>> +}
>> +
>> +#endif
>>
>>  /* Note: we implement byte, short and int versions of atomic
>> operations using
>>     the above kernel helpers; see linux-atomic-64bit.c for "long long"
>> (64-bit)
>> diff --git a/libgcc/config/arm/unwind-arm.h
>> b/libgcc/config/arm/unwind-arm.h
>> index 9f7d3f2..a9598eb 100644
>> --- a/libgcc/config/arm/unwind-arm.h
>> +++ b/libgcc/config/arm/unwind-arm.h
>> @@ -36,6 +36,25 @@
>>  #ifdef __cplusplus
>>  extern "C" {
>>  #endif
>> +_Unwind_Ptr __attribute__((weak)) __gnu_Unwind_Find_got (_Unwind_Ptr);
>> +
>> +static inline _Unwind_Ptr gnu_Unwind_Find_got (_Unwind_Ptr ptr)
>> +{
>> +    _Unwind_Ptr res;
>> +
>> +    if (__gnu_Unwind_Find_got)
>> +       res =  __gnu_Unwind_Find_got (ptr);
>> +    else
>> +      {
>> +       asm volatile ("mov %[result], r9"
>> +                     : [result]"=r" (res)
>> +                     :
>> +                     :);
>> +    }
>> +
>> +    return res;
>> +}
>> +
>>    /* Decode an R_ARM_TARGET2 relocation.  */
>>    static inline _Unwind_Word
>>    _Unwind_decode_typeinfo_ptr (_Unwind_Word base __attribute__
>> ((unused)),
>> @@ -48,7 +67,12 @@ extern "C" {
>>        if (!tmp)
>>          return 0;
>>
>> -#if (defined(linux) && !defined(__uClinux__)) || defined(__NetBSD__) \
>> +#if __FDPIC__
>> +      /* For FDPIC, we store the offset of the GOT entry. */
>> +      /* So, first get GOT from dynamic linker and then use indirect
>> access.  */
>> +      tmp += gnu_Unwind_Find_got (ptr);
>> +      tmp = *(_Unwind_Word *) tmp;
>> +#elif (defined(linux) && !defined(__uClinux__)) || defined(__NetBSD__) \
>>      || defined(__FreeBSD__) || defined(__fuchsia__)
>>        /* Pc-relative indirect.  */
>>  #define _GLIBCXX_OVERRIDE_TTYPE_ENCODING (DW_EH_PE_pcrel |
>> DW_EH_PE_indirect)
>> diff --git a/libgcc/unwind-arm-common.inc b/libgcc/unwind-arm-common.inc
>> index 76f8fc3..f5415c1 100644
>> --- a/libgcc/unwind-arm-common.inc
>> +++ b/libgcc/unwind-arm-common.inc
>> @@ -62,6 +62,7 @@ __gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
>>  #define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
>>  #define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
>>  #define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
>> +#define UCB_PR_GOT(ucbp) ((ucbp)->unwinder_cache.reserved5)
>>
>>  /* Unwind descriptors.  */
>>
>> @@ -85,6 +86,12 @@ typedef struct __EIT_entry
>>    _uw content;
>>  } __EIT_entry;
>>
>> +/* Only used in FDPIC case.  */
>> +struct funcdesc_t {
>> +    unsigned int ptr;
>> +    unsigned int got;
>> +};
>> +
>>  /* Assembly helper functions.  */
>>
>>  /* Restore core register state.  Never returns.  */
>> @@ -259,7 +266,23 @@ get_eit_entry (_Unwind_Control_Block *ucbp, _uw
>> return_address)
>>      {
>>        /* One of the predefined standard routines.  */
>>        _uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
>> +#if __FDPIC__
>> +      {
>> +       struct funcdesc_t *funcdesc
>> +         = (struct funcdesc_t *) __gnu_unwind_get_pr_addr (idx);
>> +       if (funcdesc)
>> +         {
>> +           UCB_PR_ADDR (ucbp) = funcdesc->ptr;
>> +           UCB_PR_GOT (ucbp) = funcdesc->got;
>> +         }
>> +       else
>> +         {
>> +           UCB_PR_ADDR (ucbp) = 0;
>> +         }
>> +      }
>> +#else
>>        UCB_PR_ADDR (ucbp) = __gnu_unwind_get_pr_addr (idx);
>> +#endif
>>        if (UCB_PR_ADDR (ucbp) == 0)
>>          {
>>            /* Failed */
>> @@ -269,7 +292,13 @@ get_eit_entry (_Unwind_Control_Block *ucbp, _uw
>> return_address)
>>    else
>>      {
>>        /* Execute region offset to PR */
>> +#if __FDPIC__
>>        UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
>> +      UCB_PR_GOT (ucbp)
>> +       = (unsigned int) gnu_Unwind_Find_got ((_Unwind_Ptr)
>> UCB_PR_ADDR (ucbp));
>> +#else
>> +      UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
>> +#endif
>>      }
>>    return _URC_OK;
>>  }
>> @@ -291,14 +320,29 @@ unwind_phase2 (_Unwind_Control_Block * ucbp,
>> phase2_vrs * vrs)
>>        UCB_SAVED_CALLSITE_ADDR (ucbp) = VRS_PC(vrs);
>>
>>        /* Call the pr to decide what to do.  */
>> +#if __FDPIC__
>> +      {
>> +       volatile struct funcdesc_t funcdesc;
>> +       funcdesc.ptr = UCB_PR_ADDR (ucbp);
>> +       funcdesc.got = UCB_PR_GOT (ucbp);
>> +       pr_result = ((personality_routine) &funcdesc)
>> +         (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
>> +      }
>> +#else
>>        pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
>>          (_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
>> +#endif
>>      }
>>    while (pr_result == _URC_CONTINUE_UNWIND);
>>
>>    if (pr_result != _URC_INSTALL_CONTEXT)
>>      abort();
>>
>> +#if __FDPIC__
>> +      /* r9 could have been lost due to PLT jump.  Restore correct
>> value.  */
>> +      vrs->core.r[9] = gnu_Unwind_Find_got (VRS_PC (vrs));
>> +#endif
>> +
>>    uw_restore_core_regs (vrs, &vrs->core);
>>  }
>>
>> @@ -346,8 +390,18 @@ unwind_phase2_forced (_Unwind_Control_Block
>> *ucbp, phase2_vrs *entry_vrs,
>>            next_vrs = saved_vrs;
>>
>>            /* Call the pr to decide what to do.  */
>> +#if __FDPIC__
>> +         {
>> +           volatile struct funcdesc_t funcdesc;
>> +           funcdesc.ptr = UCB_PR_ADDR (ucbp);
>> +           funcdesc.got = UCB_PR_GOT (ucbp);
>> +           pr_result = ((personality_routine) &funcdesc)
>> +             (action, ucbp, (void *) &next_vrs);
>> +         }
>> +#else
>>            pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
>>              (action, ucbp, (void *) &next_vrs);
>> +#endif
>>
>>            saved_vrs.prev_sp = VRS_SP (&next_vrs);
>>          }
>> @@ -384,6 +438,11 @@ unwind_phase2_forced (_Unwind_Control_Block
>> *ucbp, phase2_vrs *entry_vrs,
>>        return _URC_FAILURE;
>>      }
>>
>> +#if __FDPIC__
>> +  /* r9 could have been lost due to PLT jump.  Restore correct
>> value.  */
>> +  saved_vrs.core.r[9] = gnu_Unwind_Find_got (VRS_PC (&saved_vrs));
>> +#endif
>> +
>>    uw_restore_core_regs (&saved_vrs, &saved_vrs.core);
>>  }
>>
>> @@ -429,8 +488,18 @@ __gnu_Unwind_RaiseException
>> (_Unwind_Control_Block * ucbp,
>>          return _URC_FAILURE;
>>
>>        /* Call the pr to decide what to do.  */
>> +#if __FDPIC__
>> +      {
>> +       volatile struct funcdesc_t funcdesc;
>> +       funcdesc.ptr = UCB_PR_ADDR (ucbp);
>> +       funcdesc.got = UCB_PR_GOT (ucbp);
>> +       pr_result = ((personality_routine) &funcdesc)
>> +         (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
>> +      }
>> +#else
>>        pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
>>          (_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
>> +#endif
>>      }
>>    while (pr_result == _URC_CONTINUE_UNWIND);
>>
>> @@ -488,13 +557,27 @@ __gnu_Unwind_Resume (_Unwind_Control_Block *
>> ucbp, phase2_vrs * entry_vrs)
>>      }
>>
>>    /* Call the cached PR.  */
>> +#if __FDPIC__
>> +  {
>> +    volatile struct funcdesc_t funcdesc;
>> +    funcdesc.ptr = UCB_PR_ADDR (ucbp);
>> +    funcdesc.got = UCB_PR_GOT (ucbp);
>> +    pr_result = ((personality_routine) &funcdesc)
>> +      (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
>> +  }
>> +#else
>>    pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
>>          (_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
>> +#endif
>>
>>    switch (pr_result)
>>      {
>>      case _URC_INSTALL_CONTEXT:
>>        /* Upload the registers to enter the landing pad.  */
>> +#if __FDPIC__
>> +      /* r9 could have been lost due to PLT jump.  Restore correct
>> value.  */
>> +      entry_vrs->core.r[9] = gnu_Unwind_Find_got (VRS_PC (entry_vrs));
>> +#endif
>>        uw_restore_core_regs (entry_vrs, &entry_vrs->core);
>>
>>      case _URC_CONTINUE_UNWIND:
>> @@ -586,9 +669,20 @@ __gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace,
>> void * trace_argument,
>>          }
>>
>>        /* Call the pr to decide what to do.  */
>> +#if __FDPIC__
>> +      {
>> +       volatile struct funcdesc_t funcdesc;
>> +       funcdesc.ptr = UCB_PR_ADDR (ucbp);
>> +       funcdesc.got = UCB_PR_GOT (ucbp);
>> +       code = ((personality_routine) &funcdesc)
>> +         (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
>> +          ucbp, (void *) &saved_vrs);
>> +      }
>> +#else
>>        code = ((personality_routine) UCB_PR_ADDR (ucbp))
>>          (_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND,
>>           ucbp, (void *) &saved_vrs);
>> +#endif
>>      }
>>    while (code != _URC_END_OF_STACK
>>           && code != _URC_FAILURE);
>> diff --git a/libgcc/unwind-pe.h b/libgcc/unwind-pe.h
>> index dd5ae95..c18dffe 100644
>> --- a/libgcc/unwind-pe.h
>> +++ b/libgcc/unwind-pe.h
>> @@ -259,10 +259,25 @@ read_encoded_value_with_base (unsigned char
>> encoding, _Unwind_Ptr base,
>>
>>        if (result != 0)
>>          {
>> +#if __FDPIC__
>> +    if ((encoding & DW_EH_PE_pcrel) && (encoding & DW_EH_PE_indirect))
>> +      {
>> +       result += gnu_Unwind_Find_got ((_Unwind_Ptr) u);
>> +       result = *(_Unwind_Internal_Ptr *) result;
>> +      }
>> +    else
>> +      {
>> +       result += ((encoding & 0x70) == DW_EH_PE_pcrel
>> +                  ? (_Unwind_Internal_Ptr) u : base);
>> +       if (encoding & DW_EH_PE_indirect)
>> +         result = *(_Unwind_Internal_Ptr *) result;
>> +      }
>> +#else
>>            result += ((encoding & 0x70) == DW_EH_PE_pcrel
>>                       ? (_Unwind_Internal_Ptr) u : base);
>>            if (encoding & DW_EH_PE_indirect)
>>              result = *(_Unwind_Internal_Ptr *) result;
>> +#endif
>>          }
>>      }
>>
>> diff --git a/libstdc++-v3/libsupc++/eh_personality.cc
>> b/libstdc++-v3/libsupc++/eh_personality.cc
>> index 1b336c7..138b606 100644
>> --- a/libstdc++-v3/libsupc++/eh_personality.cc
>> +++ b/libstdc++-v3/libsupc++/eh_personality.cc
>> @@ -93,7 +93,13 @@ get_ttype_entry (lsda_header_info *info, _uleb128_t i)
>>    _Unwind_Ptr ptr;
>>
>>    i *= size_of_encoded_value (info->ttype_encoding);
>> -  read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
>> +  read_encoded_value_with_base (
>> +#if __FDPIC__
>> +               (DW_EH_PE_pcrel | DW_EH_PE_indirect),
>> +#else
>> +               info->ttype_encoding,
>> +#endif
>> +               info->ttype_base,
>>                                  info->TType - i, &ptr);
>>
>>    return reinterpret_cast<const std::type_info *>(ptr);
>> -- 
>> 2.6.3
>>
> 

Reply via email to