From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 67868 invoked by alias); 16 Feb 2019 15:02:18 -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 67831 invoked by uid 89); 16 Feb 2019 15:02:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=liu, Liu, ENDBR, endbr X-HELO: mail-pg1-f195.google.com Received: from mail-pg1-f195.google.com (HELO mail-pg1-f195.google.com) (209.85.215.195) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 16 Feb 2019 15:02:15 +0000 Received: by mail-pg1-f195.google.com with SMTP id b2so481488pgl.9 for ; Sat, 16 Feb 2019 07:02:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=MZBFjJ47Dqg8qiyNUnDsgWLp4z9PmHPnf8JFqmcDHjo=; b=vTOGpJ77rjUxb5HiDV09sNKuGx7pTDpSYTviFm1Jmk0Dn+GuMLqkfBCMlshb+MB4ui lqT/gnUJSuzMLed0lOyywdT5VvXtybzamhmOMLNK17reZQ00FGCYPCTIEeCWwlj2pU+F FT+sZITeAvbGBYJ2HLd2B1xqoqwxUh0ufiDr1Sc3npZJrjQ8BqLu6R0AUgAO8KKihxrq lpEyNth5yWT/pQ7Aom38YC3IsVAvj8qLQuKuaBny8/d/iCHoNX6Sq6vJCLywoZqvLZln lTYzOfG9UgTYTQbQnmbtwXJdO+JSgwc62aRryFb3+uODtrvulxrdRQT3al5he8Z/lnIs 2cIA== Return-Path: Received: from gnu-cfl-2.localdomain (c-73-93-86-59.hsd1.ca.comcast.net. [73.93.86.59]) by smtp.gmail.com with ESMTPSA id k1sm9781293pgq.45.2019.02.16.07.02.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 16 Feb 2019 07:02:12 -0800 (PST) Received: by gnu-cfl-2.localdomain (Postfix, from userid 1000) id 7A4D1C014F; Sat, 16 Feb 2019 07:02:11 -0800 (PST) Date: Sat, 16 Feb 2019 15:02:00 -0000 From: "H.J. Lu" To: gcc-patches@gcc.gnu.org, Uros Bizjak , Jan Hubicka Cc: =?utf-8?B?5YiY6KKL6byg?= Subject: V2 [PATCH] i386: Insert ENDBR for NOTE_INSN_DELETED_LABEL only if needed Message-ID: <20190216150211.GA13329@gmail.com> References: <20190215041332.24720-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190215041332.24720-1-hjl.tools@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-IsSubscribed: yes X-SW-Source: 2019-02/txt/msg01327.txt.bz2 On Thu, Feb 14, 2019 at 08:13:32PM -0800, H.J. Lu wrote: > NOTE_INSN_DELETED_LABEL is used to mark what used to be a 'code_label', > but was not used for other purposes than taking its address and was > transformed to mark that no code jumps to it. NOTE_INSN_DELETED_LABEL > is generated only in 3 places: > > 1. When delete_insn sees an unused label which is an explicit label in > the input source code or its address is taken, it turns the label into > a NOTE_INSN_DELETED_LABEL note. > 2. When rtl_tidy_fallthru_edge deletes a tablejump, it turns the > tablejump into a NOTE_INSN_DELETED_LABEL note. > 3. ix86_init_large_pic_reg creats a NOTE_INSN_DELETED_LABEL note, .L2, > to initialize large model PIC register: > > L2: > movabsq $_GLOBAL_OFFSET_TABLE_-.L2, %r11 > leaq .L2(%rip), %rax > movabsq $val@GOT, %rdx > addq %r11, %rax > > Among of them, ENDBR is needed only when the label address is taken. > rest_of_insert_endbranch has > > if ((LABEL_P (insn) && LABEL_PRESERVE_P (insn)) > || (NOTE_P (insn) > && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)) > /* TODO. Check /s bit also. */ > { > cet_eb = gen_nop_endbr (); > emit_insn_after (cet_eb, insn); > continue; > } > > For NOTE_INSN_DELETED_LABEL, we should check if forced_labels to see > if its address is taken. Also ix86_init_large_pic_reg shouldn't set > LABEL_PRESERVE_P (in_struct) since NOTE_INSN_DELETED_LABEL is suffcient > to keep the label. > > gcc/ > > PR target/89355 > * config/i386/i386.c (rest_of_insert_endbranch): Check > forced_labels to see if the address of NOTE_INSN_DELETED_LABEL > is taken. > (ix86_init_large_pic_reg): Don't set LABEL_PRESERVE_P. > Here is the updated patch. We should check LABEL_PRESERVE_P on NOTE_INSN_DELETED_LABEL to see if its address is taken. OK for trunk? Thanks. H.J. --- NOTE_INSN_DELETED_LABEL is used to mark what used to be a 'code_label', but was not used for other purposes than taking its address and was transformed to mark that no code jumps to it. Since LABEL_PRESERVE_P is true only if the label address was taken, check LABEL_PRESERVE_P before inserting ENDBR. 2019-02-15 H.J. Lu Hongtao Liu gcc/ PR target/89355 * config/i386/i386.c (rest_of_insert_endbranch): LABEL_PRESERVE_P to see if the address of NOTE_INSN_DELETED_LABEL is taken. (ix86_init_large_pic_reg): Don't set LABEL_PRESERVE_P. gcc/testsuite/ PR target/89355 * gcc.target/i386/cet-label-3.c: New test. * gcc.target/i386/cet-label-4.c: Likewise. * gcc.target/i386/cet-label-5.c: Likewise. --- gcc/config/i386/i386.c | 9 ++++---- gcc/testsuite/gcc.target/i386/cet-label-3.c | 23 +++++++++++++++++++++ gcc/testsuite/gcc.target/i386/cet-label-4.c | 12 +++++++++++ gcc/testsuite/gcc.target/i386/cet-label-5.c | 13 ++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/cet-label-3.c create mode 100644 gcc/testsuite/gcc.target/i386/cet-label-4.c create mode 100644 gcc/testsuite/gcc.target/i386/cet-label-5.c diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 609273e4fc4..acdc789b834 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -2734,10 +2734,10 @@ rest_of_insert_endbranch (void) continue; } - if ((LABEL_P (insn) && LABEL_PRESERVE_P (insn)) - || (NOTE_P (insn) - && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)) - /* TODO. Check /s bit also. */ + if ((LABEL_P (insn) + || (NOTE_P (insn) + && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)) + && LABEL_PRESERVE_P (insn)) { cet_eb = gen_nop_endbr (); emit_insn_after (cet_eb, insn); @@ -6997,7 +6997,6 @@ ix86_init_large_pic_reg (unsigned int tmp_regno) gcc_assert (Pmode == DImode); label = gen_label_rtx (); emit_label (label); - LABEL_PRESERVE_P (label) = 1; tmp_reg = gen_rtx_REG (Pmode, tmp_regno); gcc_assert (REGNO (pic_offset_table_rtx) != tmp_regno); emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, diff --git a/gcc/testsuite/gcc.target/i386/cet-label-3.c b/gcc/testsuite/gcc.target/i386/cet-label-3.c new file mode 100644 index 00000000000..9f427a866f3 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/cet-label-3.c @@ -0,0 +1,23 @@ +/* PR target/89355 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fcf-protection" } */ +/* { dg-final { scan-assembler-times "endbr32" 1 { target ia32 } } } */ +/* { dg-final { scan-assembler-times "endbr64" 1 { target { ! ia32 } } } } */ +int +test (int* val) +{ + int status = 99; + + if (!val) + { + status = 22; + goto end; + } + + extern int x; + *val = x; + + status = 0; +end: + return status; +} diff --git a/gcc/testsuite/gcc.target/i386/cet-label-4.c b/gcc/testsuite/gcc.target/i386/cet-label-4.c new file mode 100644 index 00000000000..d743d2bf202 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/cet-label-4.c @@ -0,0 +1,12 @@ +/* PR target/89355 */ +/* { dg-do compile { target { fpic && lp64 } } } */ +/* { dg-options "-O2 -fcf-protection -fPIC -mcmodel=large" } */ +/* { dg-final { scan-assembler-times "endbr64" 1 } } */ + +extern int val; + +int +test (void) +{ + return val; +} diff --git a/gcc/testsuite/gcc.target/i386/cet-label-5.c b/gcc/testsuite/gcc.target/i386/cet-label-5.c new file mode 100644 index 00000000000..4d5ca816598 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/cet-label-5.c @@ -0,0 +1,13 @@ +/* PR target/89355 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fcf-protection -Wno-return-local-addr" } */ +/* { dg-final { scan-assembler-times "endbr32" 2 { target ia32 } } } */ +/* { dg-final { scan-assembler-times "endbr64" 2 { target { ! ia32 } } } } */ + +void * +func (void) +{ + return &&bar; +bar: + return 0; +} -- 2.20.1