From patchwork Thu Jul 21 18:53:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 107 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e252:0:0:0:0:0 with SMTP id bl18csp1594861wrb; Thu, 21 Jul 2022 11:53:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ugqtYVJCWUgIk5jpnSwvlimcqefEXGmiR+z+suOxJslEx4ZTY+eiPxUtyA3uByeVePXfes X-Received: by 2002:a05:6402:428c:b0:43b:cd8:b679 with SMTP id g12-20020a056402428c00b0043b0cd8b679mr58082079edc.404.1658429636692; Thu, 21 Jul 2022 11:53:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658429636; cv=none; d=google.com; s=arc-20160816; b=RktHA9oC4yCtUoEu1SeVkicwrGb9YSvZ5Hf4YNnMMJ/wxfruJdh5J33IiiDQyYhGWQ 22tVr/IrwDXSca7m3L1IQTTwFaBxpMi8ptmTLd2YkiLQE197+kQM0wqjwp5X00FZh0Qr 6D4zH10EGu7dAIX1BQGe7j2n7eeKn1F19xNoMaS7D3HdPA37rIGYL1JDLdCWg06cqztP Qq9v5vydNsRXtnvDmOjA0Hh6PSsicG9HJagKLsor9WvWu+W2xPRv1cbdF2dECJOasjnn VJN7TLUKL/y1GuSyYfTzoQnEmfdEmFZ6k22rCmTmbAFruK6/dSeRjUB0yTIrI8ckYLwq W/DA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Nom7paupy275gkGNYSFUOye4cwj/Koq9qXJ2+IXfEZM=; b=x4evG3lGxw1zcyjAYqCH1/uFkI4ZMQE7jYb3X+Wqau1s9BiMFu5DmXO24/DSemGVO9 SE7vm4hhM4R6VM0lk8B8qC2GbYK8SUiPMmwUHLa/+HK7gWhc3dAAywCoUbQWgR4s9uNY po0Vh2lPYTORUtPpzXm/MbQ8IV6B5HofSNqbUd84vul/OTZEWkVRxb0vzx7xl1FLWl4n JyztsDFdMjnpeP1LZ1JFD5sDjmhAZppwPqsYrwVOJ1CBMZch2IQvOUbAqciHlqcbjTVX tG7FHSznAedniY1eXJKnfNBJlW4cjW8oc8Fx5ht9yyIutR6a/bUp9iOqppFDCsQqGlHh TjmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=LJd1sIjh; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c12-20020a170906170c00b00729c19c5104si3082370eje.437.2022.07.21.11.53.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 11:53:56 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=LJd1sIjh; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 75097383982A for ; Thu, 21 Jul 2022 18:53:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 75097383982A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658429635; bh=Nom7paupy275gkGNYSFUOye4cwj/Koq9qXJ2+IXfEZM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=LJd1sIjhLO6nA//JiagCloMtU/QPb/0/bB4XaLF6voHBihG5s96jvuUS9JYq21E6n 2t9DP3NJ9hXn3Qbs7j7+4KvVI/w3D5DCpSj6jpMV0diueNJU/TXf5P3/70slMfhM4J Mo+SuOeXG8Z5Ov56boVb3d4lUPVP6pQ1fgxNHkeQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id EAEC33839C50 for ; Thu, 21 Jul 2022 18:53:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EAEC33839C50 Received: by mail-pj1-x1031.google.com with SMTP id q41-20020a17090a1b2c00b001f2043c727aso2235876pjq.1 for ; Thu, 21 Jul 2022 11:53:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Nom7paupy275gkGNYSFUOye4cwj/Koq9qXJ2+IXfEZM=; b=rUgyQQIxJmxVyn9yqOlnUrbcrxNuOsUsNujqsw6uvm4E3DtLiV8G3eVUjQxgZqNj46 D51p8flH8dI+1wcAtlHEmlyuQDXuYsYFORu6uJhyELnLZfNvBhFMxe03KstA0y3QRHve IrSh24mTKmCAJUczBld+KxqHmEk2ovQ6USAioHJTLKqG43qDtV5iRXcFxaO3xBraKPKD aXWTMcf9IuhnU2Emy6PvcY5niPEOX4haLl5d2XQqk0vfhskLg+CUfvd1FCsS7DxTD2vZ 10dkKB/1NtV8NOTT8Qrwt8rn21weopEz2OXdsJ3Rp1Qs7Whi/GVAiFQCpsFPN7EnvGrC M2vQ== X-Gm-Message-State: AJIora9avcquwpMaHzgP9oxdGkSh7EFtpEKmnEwbTTsuMADc3U9jxb0V 9Oslbi1WZxlQqiyUcoJLvejUxK3RwJc= X-Received: by 2002:a17:90a:a08e:b0:1ef:82a7:7df1 with SMTP id r14-20020a17090aa08e00b001ef82a77df1mr12758088pjp.134.1658429588664; Thu, 21 Jul 2022 11:53:08 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.37.102]) by smtp.gmail.com with ESMTPSA id i1-20020a17090a138100b001ef899eb51fsm3952946pja.29.2022.07.21.11.53.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Jul 2022 11:53:08 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 00B66C0251; Thu, 21 Jul 2022 11:53:06 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] x86: Add ix86_ifunc_ref_local_ok Date: Thu, 21 Jul 2022 11:53:06 -0700 Message-Id: <20220721185306.1497618-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3027.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Gcc-patches" From: "Li, Pan2 via Gcc-patches" Reply-To: "H.J. Lu" Cc: liuhongt Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1738989515163228798?= X-GMAIL-MSGID: =?utf-8?q?1738989515163228798?= We can't always use the PLT entry as the function address for local IFUNC functions. When the PIC register is needed for PLT call, indirect call via the PLT entry will fail since the PIC register may not be set up properly for indirect call. Add ix86_ifunc_ref_local_ok to return false when the PLT entry can't be used as local IFUNC function pointers. gcc/ PR target/83782 * config/i386/i386.cc (ix86_ifunc_ref_local_ok): New. (TARGET_IFUNC_REF_LOCAL_OK): Use it. gcc/testsuite/ PR target/83782 * gcc.target/i386/pr83782-1.c: Require non-ia32. * gcc.target/i386/pr83782-2.c: Likewise. * gcc.target/i386/pr83782-3.c: New test. --- gcc/config/i386/i386.cc | 15 ++++++++++- gcc/testsuite/gcc.target/i386/pr83782-1.c | 8 +++--- gcc/testsuite/gcc.target/i386/pr83782-2.c | 4 +-- gcc/testsuite/gcc.target/i386/pr83782-3.c | 32 +++++++++++++++++++++++ 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr83782-3.c diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index e03f86d4a23..5e30dc884bf 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -16070,6 +16070,19 @@ ix86_call_use_plt_p (rtx call_op) return true; } +/* Implement TARGET_IFUNC_REF_LOCAL_OK. If this hook returns true, + the PLT entry will be used as the function address for local IFUNC + functions. When the PIC register is needed for PLT call, indirect + call via the PLT entry will fail since the PIC register may not be + set up properly for indirect call. In this case, we should return + false. */ + +static bool +ix86_ifunc_ref_local_ok (void) +{ + return !flag_pic || (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC); +} + /* Return true if the function being called was marked with attribute "noplt" or using -fno-plt and we are compiling for non-PIC. We need to handle the non-PIC case in the backend because there is no easy @@ -24953,7 +24966,7 @@ ix86_libgcc_floating_mode_supported_p ix86_get_multilib_abi_name #undef TARGET_IFUNC_REF_LOCAL_OK -#define TARGET_IFUNC_REF_LOCAL_OK hook_bool_void_true +#define TARGET_IFUNC_REF_LOCAL_OK ix86_ifunc_ref_local_ok #if !TARGET_MACHO && !TARGET_DLLIMPORT_DECL_ATTRIBUTES # undef TARGET_ASM_RELOC_RW_MASK diff --git a/gcc/testsuite/gcc.target/i386/pr83782-1.c b/gcc/testsuite/gcc.target/i386/pr83782-1.c index ce97b12e65d..85674346aec 100644 --- a/gcc/testsuite/gcc.target/i386/pr83782-1.c +++ b/gcc/testsuite/gcc.target/i386/pr83782-1.c @@ -1,4 +1,4 @@ -/* { dg-do compile } */ +/* { dg-do compile { target { ! ia32 } } } */ /* { dg-require-ifunc "" } */ /* { dg-options "-O2 -fpic" } */ @@ -20,7 +20,5 @@ bar(void) return foo; } -/* { dg-final { scan-assembler {leal[ \t]foo@GOTOFF\(%[^,]*\),[ \t]%eax} { target ia32 } } } */ -/* { dg-final { scan-assembler {lea(?:l|q)[ \t]foo\(%rip\),[ \t]%(?:e|r)ax} { target { ! ia32 } } } } */ -/* { dg-final { scan-assembler-not "foo@GOT\\\(" { target ia32 } } } */ -/* { dg-final { scan-assembler-not "foo@GOTPCREL\\\(" { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler {lea(?:l|q)[ \t]foo\(%rip\),[ \t]%(?:e|r)ax} } } */ +/* { dg-final { scan-assembler-not "foo@GOTPCREL\\\(" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr83782-2.c b/gcc/testsuite/gcc.target/i386/pr83782-2.c index e25d258bbda..a654ded771f 100644 --- a/gcc/testsuite/gcc.target/i386/pr83782-2.c +++ b/gcc/testsuite/gcc.target/i386/pr83782-2.c @@ -1,4 +1,4 @@ -/* { dg-do compile } */ +/* { dg-do compile { target { ! ia32 } } } */ /* { dg-require-ifunc "" } */ /* { dg-options "-O2 -fpic" } */ @@ -20,7 +20,5 @@ bar(void) return foo; } -/* { dg-final { scan-assembler {leal[ \t]foo@GOTOFF\(%[^,]*\),[ \t]%eax} { target ia32 } } } */ /* { dg-final { scan-assembler {lea(?:l|q)[ \t]foo\(%rip\),[ \t]%(?:e|r)ax} { target { ! ia32 } } } } */ -/* { dg-final { scan-assembler-not "foo@GOT\\\(" { target ia32 } } } */ /* { dg-final { scan-assembler-not "foo@GOTPCREL\\\(" { target { ! ia32 } } } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr83782-3.c b/gcc/testsuite/gcc.target/i386/pr83782-3.c new file mode 100644 index 00000000000..1536481cb79 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr83782-3.c @@ -0,0 +1,32 @@ +/* { dg-do run } */ +/* { dg-require-ifunc "" } */ +/* { dg-require-effective-target pie } */ +/* { dg-options "-fpie -pie" } */ + +#include + +static int __attribute__((noinline)) +implementation (void) +{ + printf ("'ere I am JH\n"); + return 0; +} + +static __typeof__ (implementation) *resolver (void) +{ + return (void *)implementation; +} + +extern int magic (void) __attribute__ ((ifunc ("resolver"))); + +__attribute__ ((weak)) +int +call_magic (int (*ptr) (void)) +{ + return ptr (); +} + +int main () +{ + return call_magic (magic); +}