public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Christophe Lyon <christophe.lyon@linaro.org>
To: Kyrill Tkachov <kyrylo.tkachov@foss.arm.com>
Cc: christophe lyon St <christophe.lyon@st.com>,
	gcc Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [ARM/FDPIC v2 09/21] [ARM] FDPIC: Add support for taking address of nested function
Date: Fri, 31 Aug 2018 14:17:00 -0000	[thread overview]
Message-ID: <CAKdteOaiPH6EniqDxT82v5uEwkuYwDsOgfa+stioBXDVtK-x3Q@mail.gmail.com> (raw)
In-Reply-To: <5B867CED.4070402@foss.arm.com>

On Wed, 29 Aug 2018 at 13:01, Kyrill Tkachov
<kyrylo.tkachov@foss.arm.com> wrote:
>
> Hi Christophe,
>
> On 13/07/18 17:11, christophe.lyon@st.com wrote:
> > From: Christophe Lyon <christophe.lyon@linaro.org>
> >
> > In FDPIC mode, the trampoline generated to support pointers to nested
> > functions looks like:
> >
> >            .word trampoline address
> >            .word trampoline GOT address
> >            ldr            r12, [pc, #8]
> >            ldr            r9, [pc, #8]
> >            ldr           pc, [pc]
>
> The comment in the code says the last one is:
> ldr           pc,  [pc, #8] ; #4 for Thumb2
>
> I'm assuming the code one is correct.

Right, it looks like a typo in the commit message.

>
> >            .word static chain value
> >            .word GOT address
> >            .word function's address
> >
> > because in FDPIC function pointers are actually pointers to function
> > descriptors, we have to actually generate a function descriptor for
> > the trampoline.
> >
> > 2018-XX-XX  Christophe Lyon  <christophe.lyon@st.com>
> >         Mickaël Guêné <mickael.guene@st.com>
> >
> >         gcc/
> >         * config/arm/arm.c (arm_asm_trampoline_template): Add FDPIC
> >         support.
> >         (arm_trampoline_init): Likewise.
> >         (arm_trampoline_init): Likewise.
> >         * config/arm/arm.h (TRAMPOLINE_SIZE): Likewise.
> >
> > Change-Id: I4b5127261a9aefa0f0318f110574ec07a856aeb1
> >
> > diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
> > index 51da2bc..ffc9128 100644
> > --- a/gcc/config/arm/arm.c
> > +++ b/gcc/config/arm/arm.c
> > @@ -3950,13 +3950,50 @@ arm_warn_func_return (tree decl)
> >             .word static chain value
> >             .word function's address
> >     XXX FIXME: When the trampoline returns, r8 will be clobbered.  */
> > +/* In FDPIC mode, the trampoline looks like:
> > +          .word trampoline address
> > +          .word trampoline GOT address
> > +          ldr            r12, [pc, #8] ; #4 for Thumb2
> > +          ldr            r9,  [pc, #8] ; #4 for Thumb2
> > +          ldr           pc,  [pc, #8] ; #4 for Thumb2
> > +          .word static chain value
> > +          .word GOT address
> > +          .word function's address
> > +*/
> >  static void
> >  arm_asm_trampoline_template (FILE *f)
> >  {
> >    fprintf (f, "\t.syntax unified\n");
> >
> > -  if (TARGET_ARM)
> > +  if (TARGET_FDPIC)
> > +    {
> > +      /* The first two words are a function descriptor pointing to the
> > +        trampoline code just below.  */
> > +      if (TARGET_ARM)
> > +       fprintf (f, "\t.arm\n");
> > +      else if (TARGET_THUMB2)
> > +       fprintf (f, "\t.thumb\n");
> > +      else
> > +       /* Only ARM and Thumb-2 are supported.  */
> > +       gcc_assert ( !TARGET_ARM && !TARGET_THUMB2);
> > +
>
> This cannot trigger based on the two clauses above. I think you want to just make it gcc_unreachable ().
>
OK

> > +      assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
> > +      assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
> > +      /* Trampoline code which sets the static chain register but also
> > +        PIC register before jumping into real code.  */
> > +      asm_fprintf (f, "\tldr\t%r, [%r, #%d]\n",
> > +                  STATIC_CHAIN_REGNUM, PC_REGNUM,
> > +                  TARGET_THUMB2 ? 8 : 4);
> > +      asm_fprintf (f, "\tldr\t%r, [%r, #%d]\n",
> > +                  PIC_OFFSET_TABLE_REGNUM, PC_REGNUM,
> > +                  TARGET_THUMB2 ? 8 : 4);
> > +      asm_fprintf (f, "\tldr\t%r, [%r, #%d]\n",
> > +                  PC_REGNUM, PC_REGNUM,
> > +                  TARGET_THUMB2 ? 8 : 4);
> > +      assemble_aligned_integer (UNITS_PER_WORD, const0_rtx);
> > +    }
> > +  else if (TARGET_ARM)
> >      {
> >        fprintf (f, "\t.arm\n");
> >        asm_fprintf (f, "\tldr\t%r, [%r, #0]\n", STATIC_CHAIN_REGNUM, PC_REGNUM);
> > @@ -3997,12 +4034,37 @@ arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
> >    emit_block_move (m_tramp, assemble_trampoline_template (),
> >                     GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
> >
> > -  mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
> > -  emit_move_insn (mem, chain_value);
> > +  if (TARGET_FDPIC)
> > +    {
> > +      rtx funcdesc = XEXP (DECL_RTL (fndecl), 0);
> > +      rtx fnaddr = gen_rtx_MEM (Pmode, funcdesc);
> > +      rtx gotaddr = gen_rtx_MEM (Pmode, plus_constant (Pmode, funcdesc, 4));
> > +      rtx trampoline_code_start
> > +       = plus_constant (Pmode, XEXP (m_tramp, 0), TARGET_THUMB2 ? 9 : 8);
>
> 9? Can you comment on this value?
>
The function start address is a offset 8, but in Thumb mode we want
bit 0 set to 1 to indicate thumb-ness.

> > +
> > +      /* Write initial funcdesc which points to the trampoline.  */
> > +      mem = adjust_address (m_tramp, SImode, 0);
> > +      emit_move_insn (mem, trampoline_code_start);
> > +      mem = adjust_address (m_tramp, SImode, 4);
> > +      emit_move_insn (mem, gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
> > +      /* Setup static chain.  */
> > +      mem = adjust_address (m_tramp, SImode, 20);
> > +      emit_move_insn (mem, chain_value);
> > +      /* GOT + real function entry point.  */
> > +      mem = adjust_address (m_tramp, SImode, 24);
> > +      emit_move_insn (mem, gotaddr);
> > +      mem = adjust_address (m_tramp, SImode, 28);
> > +      emit_move_insn (mem, fnaddr);
> > +    }
> > +  else
> > +    {
> > +      mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 8 : 12);
> > +      emit_move_insn (mem, chain_value);
> >
> > -  mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
> > -  fnaddr = XEXP (DECL_RTL (fndecl), 0);
> > -  emit_move_insn (mem, fnaddr);
> > +      mem = adjust_address (m_tramp, SImode, TARGET_32BIT ? 12 : 16);
> > +      fnaddr = XEXP (DECL_RTL (fndecl), 0);
> > +      emit_move_insn (mem, fnaddr);
> > +    }
> >
> >    a_tramp = XEXP (m_tramp, 0);
> >    emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__clear_cache"),
> > @@ -4016,7 +4078,9 @@ arm_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
> >  static rtx
> >  arm_trampoline_adjust_address (rtx addr)
> >  {
> > -  if (TARGET_THUMB)
> > +  /* For FDPIC don't fix trampoline address since it's a function
> > +     descriptor and not a function address.  */
> > +  if (TARGET_THUMB && !TARGET_FDPIC)
> >      addr = expand_simple_binop (Pmode, IOR, addr, const1_rtx,
> >                                  NULL, 0, OPTAB_LIB_WIDEN);
> >    return addr;
> > diff --git a/gcc/config/arm/arm.h b/gcc/config/arm/arm.h
> > index 4671d64..22a65a1 100644
> > --- a/gcc/config/arm/arm.h
> > +++ b/gcc/config/arm/arm.h
> > @@ -1581,7 +1581,7 @@ typedef struct
> >  #define INIT_EXPANDERS  arm_init_expanders ()
> >
> >  /* Length in units of the trampoline for entering a nested function.  */
> > -#define TRAMPOLINE_SIZE  (TARGET_32BIT ? 16 : 20)
> > +#define TRAMPOLINE_SIZE  (TARGET_FDPIC ? 32 : (TARGET_32BIT ? 16 : 20))
> >
> >  /* Alignment required for a trampoline in bits.  */
> >  #define TRAMPOLINE_ALIGNMENT  32
> > --
> > 2.6.3
> >
>

  reply	other threads:[~2018-08-31 14:17 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-13 16:12 [ARM/FDPIC v2 00/21] FDPIC ABI for ARM christophe.lyon
2018-07-13 16:12 ` [ARM/FDPIC v2 01/21] [ARM] FDPIC: Add -mfdpic option support christophe.lyon
2018-08-29 10:46   ` Kyrill Tkachov
2018-08-31 14:09     ` Christophe Lyon
2018-09-21 15:41       ` Christophe Lyon
2018-07-13 16:12 ` [ARM/FDPIC v2 02/21] [ARM] FDPIC: Handle arm*-*-uclinuxfdpiceabi in configure scripts christophe.lyon
2018-08-29 10:46   ` Kyrill Tkachov
2018-08-31 14:13     ` Christophe Lyon
2018-09-09  8:16       ` Christophe Lyon
2018-07-13 16:13 ` [ARM/FDPIC v2 03/21] [ARM] FDPIC: Force FDPIC related options unless -mno-fdpic is provided christophe.lyon
2018-07-13 16:13 ` [ARM/FDPIC v2 04/21] [ARM] FDPIC: Add support for FDPIC for arm architecture christophe.lyon
2018-08-29 10:46   ` Kyrill Tkachov
2018-09-04 15:29     ` Richard Earnshaw (lists)
2018-09-05 12:07       ` Christophe Lyon
2018-07-13 16:13 ` [ARM/FDPIC v2 05/21] [ARM] FDPIC: Fix __do_global_dtors_aux and frame_dummy generation christophe.lyon
2018-07-13 16:14 ` [ARM/FDPIC v2 06/21] [ARM] FDPIC: Add support for c++ exceptions christophe.lyon
2018-08-29 10:49   ` Kyrill Tkachov
2018-07-13 16:14 ` [ARM/FDPIC v2 07/21] [ARM] FDPIC: Avoid saving/restoring r9 on stack since it is RO christophe.lyon
2018-08-29 10:52   ` Kyrill Tkachov
2018-07-13 16:14 ` [ARM/FDPIC v2 08/21] [ARM] FDPIC: Ensure local/global binding for function descriptors christophe.lyon
2018-07-13 16:15 ` [ARM/FDPIC v2 10/21] [ARM] FDPIC: Implement TLS support christophe.lyon
2018-07-13 16:15 ` [ARM/FDPIC v2 09/21] [ARM] FDPIC: Add support for taking address of nested function christophe.lyon
2018-08-29 11:01   ` Kyrill Tkachov
2018-08-31 14:17     ` Christophe Lyon [this message]
2018-07-13 16:15 ` [ARM/FDPIC v2 11/21] [ARM] FDPIC: Add support to unwind FDPIC signal frame christophe.lyon
2018-07-13 16:16 ` [ARM/FDPIC v2 13/21] [ARM] FDPIC: Force LSB bit for PC in Cortex-M architecture christophe.lyon
2018-07-13 16:16 ` [ARM/FDPIC v2 12/21] [ARM] FDPIC: Restore r9 after we call __aeabi_read_tp christophe.lyon
2018-07-13 16:16 ` [ARM/FDPIC v2 14/21] [ARM][testsuite] FDPIC: Skip unsupported tests christophe.lyon
2018-07-13 16:17 ` [ARM/FDPIC v2 15/21] [ARM][testsuite] FDPIC: Adjust scan-assembler patterns christophe.lyon
2018-07-13 16:17 ` [ARM/FDPIC v2 16/21] [ARM][testsuite] FDPIC: Skip v8-m and v6-m tests that currently produce an ICE christophe.lyon
2018-07-13 16:17 ` [ARM/FDPIC v2 17/21] [ARM][testsuite] FDPIC: Skip tests that don't work in PIC mode christophe.lyon
2018-07-13 16:18 ` [ARM/FDPIC v2 18/21] [ARM][testsuite] FDPIC: Handle *-*-uclinux* christophe.lyon
2018-07-13 16:18 ` [ARM/FDPIC v2 19/21] [ARM][testsuite] FDPIC: Enable tests on pie_enabled targets christophe.lyon
2018-07-13 16:18 ` [ARM/FDPIC v2 20/21] [ARM][testsuite] FDPIC: Adjust pr43698.c to avoid clash with uclibc christophe.lyon
2018-07-13 16:19 ` [ARM/FDPIC v2 21/21] [ARM][testsuite] FDPIC: Skip tests using architecture older than v7 christophe.lyon
2018-08-01 14:03 ` [ARM/FDPIC v2 00/21] FDPIC ABI for ARM Christophe Lyon
2018-08-16 22:20   ` Christophe Lyon
2018-08-28 16:09     ` Christophe Lyon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKdteOaiPH6EniqDxT82v5uEwkuYwDsOgfa+stioBXDVtK-x3Q@mail.gmail.com \
    --to=christophe.lyon@linaro.org \
    --cc=christophe.lyon@st.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=kyrylo.tkachov@foss.arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).