From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 105209 invoked by alias); 29 Aug 2018 11:01:07 -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 105193 invoked by uid 89); 29 Aug 2018 11:01:06 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.4 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,SEM_URI,SEM_URIRED autolearn=ham version=3.3.2 spammy=fndecl X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 29 Aug 2018 11:01:05 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 76D1780D; Wed, 29 Aug 2018 04:01:03 -0700 (PDT) Received: from [10.2.207.77] (e100706-lin.cambridge.arm.com [10.2.207.77]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C62C13F557; Wed, 29 Aug 2018 04:01:02 -0700 (PDT) Message-ID: <5B867CED.4070402@foss.arm.com> Date: Wed, 29 Aug 2018 11:01:00 -0000 From: Kyrill Tkachov User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: "christophe.lyon@st.com" , "gcc-patches@gcc.gnu.org" CC: "christophe.lyon@linaro.org" Subject: Re: [ARM/FDPIC v2 09/21] [ARM] FDPIC: Add support for taking address of nested function References: <20180713161136.29104-1-christophe.lyon@st.com> <20180713161136.29104-10-christophe.lyon@st.com> In-Reply-To: <20180713161136.29104-10-christophe.lyon@st.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-SW-Source: 2018-08/txt/msg01855.txt.bz2 Hi Christophe, On 13/07/18 17:11, christophe.lyon@st.com wrote: > From: Christophe Lyon > > 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. > .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 > Mickaël Guêné > > 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 (). > + 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? > + > + /* 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 >