Message ID | 20220901105412.42242-1-xry111@xry111.site |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp190531wro; Thu, 1 Sep 2022 03:57:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR6ey1hZqdxdXV2XD6RGd+kDexINuDAEuGiG8IUgtADjr451gedDMAPR2fOHmncHiS7dl3Ti X-Received: by 2002:a05:6402:11d0:b0:448:89ff:88f0 with SMTP id j16-20020a05640211d000b0044889ff88f0mr13992205edw.166.1662029850304; Thu, 01 Sep 2022 03:57:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662029850; cv=none; d=google.com; s=arc-20160816; b=ABfW5v/RwqEPW4eh/OcL4lD0vS7oyeLoAfbFdDrdEhZ3PiF9LhMzKxqC969AwD2eGx ehHqGu8eXCbNzVB/wwZ3X5Vq8ZwjYzy5ZU+WC1TybHciOpS6zmOW+/x6VnEsFDQxG7w3 5tW1Mf1tmZ3e+/JyhEhfxq8fQGRhuC496D4wZPqS4eMebtpWREIlVEgqdsd7CTPjDEcB QOnZIW35XlaDRxRNlBmtCenow3lPTyMcWSVEeASNkgzIcx+gtUJS1IP/kQ7WHHTJf4Mq FMx+49KWTIJNIp/9FXUqm1/YzOwMXkL+MQeIE4mNbs6k6MQvgxIWiOmgkRwV7TV9Zxu3 R1Qw== 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=LZNeJSgsBEAsJN/P3cobCvOyo5NgujfS9utAzDWS4O4=; b=ig2E1Wqr8a43aXqZ3CHZC3oBdx30lAyFpz3ZnlBkoz3UCWDMjEMqJf95TqxwkrkAGL E2Msj7j80X6RlACHlEcI84lI+jEfImSWq45LS/f6BvcL6vsKvtHlsUMqnZMmFe37dHpP AteeyHumalpMgWmTEKPmCR6oIDwGbQM0x6hfqe7mjnBI/LWbFD9aGIT4f9c2WHTpgMmx REeI96tT78P4zpIPD60WbeBRFM1b/W5thcoZMijUFtG0BgdAHlEuPDfFrMKgk6Sz0UoY qLMrLxY5Xkv6fW4QKzGIqM0u7+4ld64DY8MOQ2znFdwHte83TWNP7YTknjT2HFJatUP1 ifRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Wzzujr7P; 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 k7-20020aa7c047000000b0044815af49cbsi1219840edo.265.2022.09.01.03.57.30 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 03:57:30 -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=Wzzujr7P; 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 06D16383FBAA for <ouuuleilei@gmail.com>; Thu, 1 Sep 2022 10:57:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06D16383FBAA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662029849; bh=LZNeJSgsBEAsJN/P3cobCvOyo5NgujfS9utAzDWS4O4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Wzzujr7P46fot5wdM1/aejKBmrCXSU53lYch4smBxqiJ0OkkEMXuEWtHvM0zLoFaO qkgFhp5TjOufzu/PLzw8TZJQLYIwASzAm5wygIC8vZeCoBHWncVCvpEU4SXj6cFdLp PsDpNgy8DTo9ab6gkkF+66+Gow5TJxQm1TWNxGKs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 4C6FF3858C00 for <gcc-patches@gcc.gnu.org>; Thu, 1 Sep 2022 10:56:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4C6FF3858C00 Received: from xry111-x57s1.. (unknown [IPv6:240e:358:1142:2500:dc73:854d:832e:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 93F3D65922; Thu, 1 Sep 2022 06:56:40 -0400 (EDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] LoongArch: add -mdirect-extern-access option Date: Thu, 1 Sep 2022 18:54:13 +0800 Message-Id: <20220901105412.42242-1-xry111@xry111.site> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, FROM_SUSPICIOUS_NTLD_FP, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, LIKELY_SPAM_FROM, PDS_OTHER_BAD_TLD, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Xi Ruoyao via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Xi Ruoyao <xry111@xry111.site> Cc: Chenghua Xu <xuchenghua@loongson.cn>, Huacai Chen <chenhuacai@kernel.org>, Lulu Cheng <chenglulu@loongson.cn>, Wang Xuerui <i@xen0n.name> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742764612644016721?= X-GMAIL-MSGID: =?utf-8?q?1742764612644016721?= |
Series |
LoongArch: add -mdirect-extern-access option
|
|
Commit Message
Xi Ruoyao
Sept. 1, 2022, 10:54 a.m. UTC
We'd like to introduce a new codegen option to align with the old "-Wa,-mla-global-with-pcrel" and avoid a performance & size regression building the Linux kernel with new-reloc toolchain. And it should be also useful for building statically linked executables, firmwares (EDK2 for example), and other OS kernels. OK for trunk? -- >8 -- As a new target, LoongArch does not use copy relocation as it's problematic in some circumstances. One bad consequence is we are emitting GOT for all accesses to all extern objects with default visibility. The use of GOT is not needed in statically linked executables, OS kernels etc. The GOT entry just wastes space, and the GOT access just slow down the execution in those environments. Before -mexplicit-relocs, we used "-Wa,-mla-global-with-pcrel" to tell the assembler not to use GOT for extern access. But with -mexplicit-relocs, we have to opt the logic in GCC. The name "-mdirect-extern-access" is learnt from x86 port. gcc/ChangeLog: * config/loongarch/genopts/loongarch.opt.in: Add -mdirect-extern-access option. * config/loongarch/loongarch.opt: Regenerate. * config/loongarch/loongarch.cc (loongarch_classify_symbol): Don't use SYMBOL_GOT_DISP if TARGET_DIRECT_EXTERN_ACCESS. (loongarch_option_override_internal): Complain if -mdirect-extern-access is used with -fPIC or -fpic. * doc/invoke.texi: Document -mdirect-extern-access for LoongArch. gcc/testsuite/ChangeLog: * gcc.target/loongarch/direct-extern-1.c: New test. * gcc.target/loongarch/direct-extern-2.c: New test. --- gcc/config/loongarch/genopts/loongarch.opt.in | 4 ++++ gcc/config/loongarch/loongarch.cc | 5 ++++- gcc/config/loongarch/loongarch.opt | 4 ++++ gcc/doc/invoke.texi | 15 +++++++++++++++ .../gcc.target/loongarch/direct-extern-1.c | 6 ++++++ .../gcc.target/loongarch/direct-extern-2.c | 6 ++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern-1.c create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern-2.c
Comments
Hi, Ruoyao, On Thu, Sep 1, 2022 at 6:56 PM Xi Ruoyao <xry111@xry111.site> wrote: > > We'd like to introduce a new codegen option to align with the old > "-Wa,-mla-global-with-pcrel" and avoid a performance & size regression > building the Linux kernel with new-reloc toolchain. And it should be > also useful for building statically linked executables, firmwares (EDK2 > for example), and other OS kernels. > > OK for trunk? This seems drop your (1)(2)(3) approach and do a similar thing as "a new code model" discussed in another thread? Huacai > > -- >8 -- > > As a new target, LoongArch does not use copy relocation as it's > problematic in some circumstances. One bad consequence is we are > emitting GOT for all accesses to all extern objects with default > visibility. The use of GOT is not needed in statically linked > executables, OS kernels etc. The GOT entry just wastes space, and the > GOT access just slow down the execution in those environments. > > Before -mexplicit-relocs, we used "-Wa,-mla-global-with-pcrel" to tell > the assembler not to use GOT for extern access. But with > -mexplicit-relocs, we have to opt the logic in GCC. > > The name "-mdirect-extern-access" is learnt from x86 port. > > gcc/ChangeLog: > > * config/loongarch/genopts/loongarch.opt.in: Add > -mdirect-extern-access option. > * config/loongarch/loongarch.opt: Regenerate. > * config/loongarch/loongarch.cc (loongarch_classify_symbol): > Don't use SYMBOL_GOT_DISP if TARGET_DIRECT_EXTERN_ACCESS. > (loongarch_option_override_internal): Complain if > -mdirect-extern-access is used with -fPIC or -fpic. > * doc/invoke.texi: Document -mdirect-extern-access for > LoongArch. > > gcc/testsuite/ChangeLog: > > * gcc.target/loongarch/direct-extern-1.c: New test. > * gcc.target/loongarch/direct-extern-2.c: New test. > --- > gcc/config/loongarch/genopts/loongarch.opt.in | 4 ++++ > gcc/config/loongarch/loongarch.cc | 5 ++++- > gcc/config/loongarch/loongarch.opt | 4 ++++ > gcc/doc/invoke.texi | 15 +++++++++++++++ > .../gcc.target/loongarch/direct-extern-1.c | 6 ++++++ > .../gcc.target/loongarch/direct-extern-2.c | 6 ++++++ > 6 files changed, 39 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > > diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in > index ebdd9538d48..e10618777b2 100644 > --- a/gcc/config/loongarch/genopts/loongarch.opt.in > +++ b/gcc/config/loongarch/genopts/loongarch.opt.in > @@ -184,3 +184,7 @@ Enum(cmodel) String(@@STR_CMODEL_EXTREME@@) Value(CMODEL_EXTREME) > mcmodel= > Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) Init(CMODEL_NORMAL) > Specify the code model. > + > +mdirect-extern-access > +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) > +Avoid using the GOT to access external symbols. > diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc > index 77e3a105390..2875fa5b0f3 100644 > --- a/gcc/config/loongarch/loongarch.cc > +++ b/gcc/config/loongarch/loongarch.cc > @@ -1642,7 +1642,7 @@ loongarch_classify_symbol (const_rtx x) > if (SYMBOL_REF_TLS_MODEL (x)) > return SYMBOL_TLS; > > - if (!loongarch_symbol_binds_local_p (x)) > + if (!TARGET_DIRECT_EXTERN_ACCESS && !loongarch_symbol_binds_local_p (x)) > return SYMBOL_GOT_DISP; > > tree t = SYMBOL_REF_DECL (x); > @@ -6093,6 +6093,9 @@ loongarch_option_override_internal (struct gcc_options *opts) > if (loongarch_branch_cost == 0) > loongarch_branch_cost = loongarch_cost->branch_cost; > > + if (TARGET_DIRECT_EXTERN_ACCESS && flag_shlib) > + error ("%qs cannot be used for compiling a shared library", > + "-mdirect-extern-access"); > > switch (la_target.cmodel) > { > diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt > index 6395234218b..96c811c850b 100644 > --- a/gcc/config/loongarch/loongarch.opt > +++ b/gcc/config/loongarch/loongarch.opt > @@ -191,3 +191,7 @@ Enum(cmodel) String(extreme) Value(CMODEL_EXTREME) > mcmodel= > Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) Init(CMODEL_NORMAL) > Specify the code model. > + > +mdirect-extern-access > +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) > +Avoid using the GOT to access external symbols. > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi > index e5eb525a2c1..d4e86682827 100644 > --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -1016,6 +1016,7 @@ Objective-C and Objective-C++ Dialects}. > -memcpy -mno-memcpy -mstrict-align -mno-strict-align @gol > -mmax-inline-memcpy-size=@var{n} @gol > -mexplicit-relocs -mno-explicit-relocs @gol > +-mdirect-extern-access -mno-direct-extern-access @gol > -mcmodel=@var{code-model}} > > @emph{M32R/D Options} > @@ -25100,6 +25101,20 @@ GCC build-time by detecting corresponding assembler support: > @code{-mno-explicit-relocs} otherwise. This option is mostly useful for > debugging, or interoperation with assemblers different from the build-time > one. > + > +@item -mdirect-extern-access > +@itemx -mno-direct-extern-access > +@opindex mdirect-extern-access > +Do not use or use GOT to access external symbols. The default is > +@option{-mno-direct-extern-access}: GOT is used for external symbols with > +default visibility, but not used for other external symbols. > + > +With @option{-mdirect-extern-access}, GOT is not used and all external > +symbols are PC-relatively addressed. It is @strong{only} suitable for > +environments where no dynamic link is performed, like firmwares, OS > +kernels, executables linked with @option{-static} or @option{-static-pie}. > +@option{-mdirect-extern-access} is not compatible with @option{-fPIC} or > +@option{-fpic}. > @end table > > @node M32C Options > diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > new file mode 100644 > index 00000000000..85c6c1e8a88 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > @@ -0,0 +1,6 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mexplicit-relocs -mdirect-extern-access" } */ > +/* { dg-final { scan-assembler-not "got" } } */ > + > +extern int x; > +int f() { return x; } > diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > new file mode 100644 > index 00000000000..58d8bd68a06 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > @@ -0,0 +1,6 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mno-explicit-relocs -mdirect-extern-access" } */ > +/* { dg-final { scan-assembler-not "la.global" } } */ > + > +extern int x; > +int f() { return x; } > -- > 2.37.3 >
On Fri, 2022-09-02 at 11:12 +0800, Huacai Chen wrote: > On Thu, Sep 1, 2022 at 6:56 PM Xi Ruoyao <xry111@xry111.site> wrote: > > > > We'd like to introduce a new codegen option to align with the old > > "-Wa,-mla-global-with-pcrel" and avoid a performance & size > > regression > > building the Linux kernel with new-reloc toolchain. And it should > > be > > also useful for building statically linked executables, firmwares > > (EDK2 > > for example), and other OS kernels. > > > > OK for trunk? > This seems drop your (1)(2)(3) approach and do a similar thing as "a > new code model" discussed in another thread? Step (1) is rejected because they've found some issue with copy relocation impossible to be solved (there is a plan to drop copy relocation from other architectures as well). This is a modification of (2): we add -mdirect-extern-access, but without copy relocation it can only be used by kernel, static executable, etc. so it's not the default. In kernel we just use KBUILD_CFLAGS_KERNEL += -mdirect-extern-access. For modules we use GOT until we can get rid of XKPRANGE.
On Thu, 2022-09-01 at 18:54 +0800, Xi Ruoyao wrote: > We'd like to introduce a new codegen option to align with the old > "-Wa,-mla-global-with-pcrel" and avoid a performance & size regression > building the Linux kernel with new-reloc toolchain. And it should be > also useful for building statically linked executables, firmwares (EDK2 > for example), and other OS kernels. Some news: get rid of the GOT will also make the implementation of relocatable kernel easier, so I hope this can be reviewed quickly. > OK for trunk? > > -- >8 -- > > As a new target, LoongArch does not use copy relocation as it's > problematic in some circumstances. One bad consequence is we are > emitting GOT for all accesses to all extern objects with default > visibility. The use of GOT is not needed in statically linked > executables, OS kernels etc. The GOT entry just wastes space, and the > GOT access just slow down the execution in those environments. > > Before -mexplicit-relocs, we used "-Wa,-mla-global-with-pcrel" to tell > the assembler not to use GOT for extern access. But with > -mexplicit-relocs, we have to opt the logic in GCC. > > The name "-mdirect-extern-access" is learnt from x86 port. > > gcc/ChangeLog: > > * config/loongarch/genopts/loongarch.opt.in: Add > -mdirect-extern-access option. > * config/loongarch/loongarch.opt: Regenerate. > * config/loongarch/loongarch.cc (loongarch_classify_symbol): > Don't use SYMBOL_GOT_DISP if TARGET_DIRECT_EXTERN_ACCESS. > (loongarch_option_override_internal): Complain if > -mdirect-extern-access is used with -fPIC or -fpic. > * doc/invoke.texi: Document -mdirect-extern-access for > LoongArch. > > gcc/testsuite/ChangeLog: > > * gcc.target/loongarch/direct-extern-1.c: New test. > * gcc.target/loongarch/direct-extern-2.c: New test. > --- > gcc/config/loongarch/genopts/loongarch.opt.in | 4 ++++ > gcc/config/loongarch/loongarch.cc | 5 ++++- > gcc/config/loongarch/loongarch.opt | 4 ++++ > gcc/doc/invoke.texi | 15 > +++++++++++++++ > .../gcc.target/loongarch/direct-extern-1.c | 6 ++++++ > .../gcc.target/loongarch/direct-extern-2.c | 6 ++++++ > 6 files changed, 39 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern- > 1.c > create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern- > 2.c > > diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in > b/gcc/config/loongarch/genopts/loongarch.opt.in > index ebdd9538d48..e10618777b2 100644 > --- a/gcc/config/loongarch/genopts/loongarch.opt.in > +++ b/gcc/config/loongarch/genopts/loongarch.opt.in > @@ -184,3 +184,7 @@ Enum(cmodel) String(@@STR_CMODEL_EXTREME@@) > Value(CMODEL_EXTREME) > mcmodel= > Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) > Init(CMODEL_NORMAL) > Specify the code model. > + > +mdirect-extern-access > +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) > +Avoid using the GOT to access external symbols. > diff --git a/gcc/config/loongarch/loongarch.cc > b/gcc/config/loongarch/loongarch.cc > index 77e3a105390..2875fa5b0f3 100644 > --- a/gcc/config/loongarch/loongarch.cc > +++ b/gcc/config/loongarch/loongarch.cc > @@ -1642,7 +1642,7 @@ loongarch_classify_symbol (const_rtx x) > if (SYMBOL_REF_TLS_MODEL (x)) > return SYMBOL_TLS; > > - if (!loongarch_symbol_binds_local_p (x)) > + if (!TARGET_DIRECT_EXTERN_ACCESS && !loongarch_symbol_binds_local_p > (x)) > return SYMBOL_GOT_DISP; > > tree t = SYMBOL_REF_DECL (x); > @@ -6093,6 +6093,9 @@ loongarch_option_override_internal (struct > gcc_options *opts) > if (loongarch_branch_cost == 0) > loongarch_branch_cost = loongarch_cost->branch_cost; > > + if (TARGET_DIRECT_EXTERN_ACCESS && flag_shlib) > + error ("%qs cannot be used for compiling a shared library", > + "-mdirect-extern-access"); > > switch (la_target.cmodel) > { > diff --git a/gcc/config/loongarch/loongarch.opt > b/gcc/config/loongarch/loongarch.opt > index 6395234218b..96c811c850b 100644 > --- a/gcc/config/loongarch/loongarch.opt > +++ b/gcc/config/loongarch/loongarch.opt > @@ -191,3 +191,7 @@ Enum(cmodel) String(extreme) Value(CMODEL_EXTREME) > mcmodel= > Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) > Init(CMODEL_NORMAL) > Specify the code model. > + > +mdirect-extern-access > +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) > +Avoid using the GOT to access external symbols. > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi > index e5eb525a2c1..d4e86682827 100644 > --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -1016,6 +1016,7 @@ Objective-C and Objective-C++ Dialects}. > -memcpy -mno-memcpy -mstrict-align -mno-strict-align @gol > -mmax-inline-memcpy-size=@var{n} @gol > -mexplicit-relocs -mno-explicit-relocs @gol > +-mdirect-extern-access -mno-direct-extern-access @gol > -mcmodel=@var{code-model}} > > @emph{M32R/D Options} > @@ -25100,6 +25101,20 @@ GCC build-time by detecting corresponding > assembler support: > @code{-mno-explicit-relocs} otherwise. This option is mostly useful > for > debugging, or interoperation with assemblers different from the > build-time > one. > + > +@item -mdirect-extern-access > +@itemx -mno-direct-extern-access > +@opindex mdirect-extern-access > +Do not use or use GOT to access external symbols. The default is > +@option{-mno-direct-extern-access}: GOT is used for external symbols > with > +default visibility, but not used for other external symbols. > + > +With @option{-mdirect-extern-access}, GOT is not used and all > external > +symbols are PC-relatively addressed. It is @strong{only} suitable > for > +environments where no dynamic link is performed, like firmwares, OS > +kernels, executables linked with @option{-static} or @option{-static- > pie}. > +@option{-mdirect-extern-access} is not compatible with @option{-fPIC} > or > +@option{-fpic}. > @end table > > @node M32C Options > diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > new file mode 100644 > index 00000000000..85c6c1e8a88 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > @@ -0,0 +1,6 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mexplicit-relocs -mdirect-extern-access" } */ > +/* { dg-final { scan-assembler-not "got" } } */ > + > +extern int x; > +int f() { return x; } > diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > new file mode 100644 > index 00000000000..58d8bd68a06 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > @@ -0,0 +1,6 @@ > +/* { dg-do compile } */ > +/* { dg-options "-mno-explicit-relocs -mdirect-extern-access" } */ > +/* { dg-final { scan-assembler-not "la.global" } } */ > + > +extern int x; > +int f() { return x; }
On Fri, Sep 2, 2022 at 4:31 AM Xi Ruoyao via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > On Thu, 2022-09-01 at 18:54 +0800, Xi Ruoyao wrote: > > We'd like to introduce a new codegen option to align with the old > > "-Wa,-mla-global-with-pcrel" and avoid a performance & size regression > > building the Linux kernel with new-reloc toolchain. And it should be > > also useful for building statically linked executables, firmwares (EDK2 > > for example), and other OS kernels. > > Some news: get rid of the GOT will also make the implementation of > relocatable kernel easier, so I hope this can be reviewed quickly. > > > OK for trunk? > > > > -- >8 -- > > > > As a new target, LoongArch does not use copy relocation as it's > > problematic in some circumstances. One bad consequence is we are > > emitting GOT for all accesses to all extern objects with default > > visibility. The use of GOT is not needed in statically linked > > executables, OS kernels etc. The GOT entry just wastes space, and the > > GOT access just slow down the execution in those environments. > > > > Before -mexplicit-relocs, we used "-Wa,-mla-global-with-pcrel" to tell > > the assembler not to use GOT for extern access. But with > > -mexplicit-relocs, we have to opt the logic in GCC. > > > > The name "-mdirect-extern-access" is learnt from x86 port. I think a discussion about the exact option name is useful. The GCC x86 port generates GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS for -mno-direct-extern-access. The Clang -fdirect-access-external-data (which is added before -mdirect-extern-access) does not use GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. If a new port avoids copy relocations in the first place, GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS isn't really useful. > > gcc/ChangeLog: > > > > * config/loongarch/genopts/loongarch.opt.in: Add > > -mdirect-extern-access option. > > * config/loongarch/loongarch.opt: Regenerate. > > * config/loongarch/loongarch.cc (loongarch_classify_symbol): > > Don't use SYMBOL_GOT_DISP if TARGET_DIRECT_EXTERN_ACCESS. > > (loongarch_option_override_internal): Complain if > > -mdirect-extern-access is used with -fPIC or -fpic. > > * doc/invoke.texi: Document -mdirect-extern-access for > > LoongArch. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.target/loongarch/direct-extern-1.c: New test. > > * gcc.target/loongarch/direct-extern-2.c: New test. > > --- > > gcc/config/loongarch/genopts/loongarch.opt.in | 4 ++++ > > gcc/config/loongarch/loongarch.cc | 5 ++++- > > gcc/config/loongarch/loongarch.opt | 4 ++++ > > gcc/doc/invoke.texi | 15 > > +++++++++++++++ > > .../gcc.target/loongarch/direct-extern-1.c | 6 ++++++ > > .../gcc.target/loongarch/direct-extern-2.c | 6 ++++++ > > 6 files changed, 39 insertions(+), 1 deletion(-) > > create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern- > > 1.c > > create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern- > > 2.c > > > > diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in > > b/gcc/config/loongarch/genopts/loongarch.opt.in > > index ebdd9538d48..e10618777b2 100644 > > --- a/gcc/config/loongarch/genopts/loongarch.opt.in > > +++ b/gcc/config/loongarch/genopts/loongarch.opt.in > > @@ -184,3 +184,7 @@ Enum(cmodel) String(@@STR_CMODEL_EXTREME@@) > > Value(CMODEL_EXTREME) > > mcmodel= > > Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) > > Init(CMODEL_NORMAL) > > Specify the code model. > > + > > +mdirect-extern-access > > +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) > > +Avoid using the GOT to access external symbols. > > diff --git a/gcc/config/loongarch/loongarch.cc > > b/gcc/config/loongarch/loongarch.cc > > index 77e3a105390..2875fa5b0f3 100644 > > --- a/gcc/config/loongarch/loongarch.cc > > +++ b/gcc/config/loongarch/loongarch.cc > > @@ -1642,7 +1642,7 @@ loongarch_classify_symbol (const_rtx x) > > if (SYMBOL_REF_TLS_MODEL (x)) > > return SYMBOL_TLS; > > > > - if (!loongarch_symbol_binds_local_p (x)) > > + if (!TARGET_DIRECT_EXTERN_ACCESS && !loongarch_symbol_binds_local_p > > (x)) > > return SYMBOL_GOT_DISP; > > > > tree t = SYMBOL_REF_DECL (x); > > @@ -6093,6 +6093,9 @@ loongarch_option_override_internal (struct > > gcc_options *opts) > > if (loongarch_branch_cost == 0) > > loongarch_branch_cost = loongarch_cost->branch_cost; > > > > + if (TARGET_DIRECT_EXTERN_ACCESS && flag_shlib) > > + error ("%qs cannot be used for compiling a shared library", > > + "-mdirect-extern-access"); > > > > switch (la_target.cmodel) > > { > > diff --git a/gcc/config/loongarch/loongarch.opt > > b/gcc/config/loongarch/loongarch.opt > > index 6395234218b..96c811c850b 100644 > > --- a/gcc/config/loongarch/loongarch.opt > > +++ b/gcc/config/loongarch/loongarch.opt > > @@ -191,3 +191,7 @@ Enum(cmodel) String(extreme) Value(CMODEL_EXTREME) > > mcmodel= > > Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) > > Init(CMODEL_NORMAL) > > Specify the code model. > > + > > +mdirect-extern-access > > +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) > > +Avoid using the GOT to access external symbols. > > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi > > index e5eb525a2c1..d4e86682827 100644 > > --- a/gcc/doc/invoke.texi > > +++ b/gcc/doc/invoke.texi > > @@ -1016,6 +1016,7 @@ Objective-C and Objective-C++ Dialects}. > > -memcpy -mno-memcpy -mstrict-align -mno-strict-align @gol > > -mmax-inline-memcpy-size=@var{n} @gol > > -mexplicit-relocs -mno-explicit-relocs @gol > > +-mdirect-extern-access -mno-direct-extern-access @gol > > -mcmodel=@var{code-model}} > > > > @emph{M32R/D Options} > > @@ -25100,6 +25101,20 @@ GCC build-time by detecting corresponding > > assembler support: > > @code{-mno-explicit-relocs} otherwise. This option is mostly useful > > for > > debugging, or interoperation with assemblers different from the > > build-time > > one. > > + > > +@item -mdirect-extern-access > > +@itemx -mno-direct-extern-access > > +@opindex mdirect-extern-access > > +Do not use or use GOT to access external symbols. The default is > > +@option{-mno-direct-extern-access}: GOT is used for external symbols > > with > > +default visibility, but not used for other external symbols. > > + > > +With @option{-mdirect-extern-access}, GOT is not used and all > > external > > +symbols are PC-relatively addressed. It is @strong{only} suitable > > for > > +environments where no dynamic link is performed, like firmwares, OS > > +kernels, executables linked with @option{-static} or @option{-static- > > pie}. > > +@option{-mdirect-extern-access} is not compatible with @option{-fPIC} > > or > > +@option{-fpic}. > > @end table > > > > @node M32C Options > > diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > > b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > > new file mode 100644 > > index 00000000000..85c6c1e8a88 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c > > @@ -0,0 +1,6 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-mexplicit-relocs -mdirect-extern-access" } */ > > +/* { dg-final { scan-assembler-not "got" } } */ > > + > > +extern int x; > > +int f() { return x; } > > diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > > b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > > new file mode 100644 > > index 00000000000..58d8bd68a06 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c > > @@ -0,0 +1,6 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-mno-explicit-relocs -mdirect-extern-access" } */ > > +/* { dg-final { scan-assembler-not "la.global" } } */ > > + > > +extern int x; > > +int f() { return x; } > > -- > Xi Ruoyao <xry111@xry111.site> > School of Aerospace Science and Technology, Xidian University
I think the following modification should be added: git diff loongarch/predicates.md +;; Do not use GOT to access external symbols, when define +;; TARGET_DIRECT_EXTERN_ACCESS. + (define_predicate "is_const_call_local_symbol" (and (match_operand 0 "const_call_insn_operand") (ior (match_test "loongarch_global_symbol_p (op) == 0") - (match_test "loongarch_symbol_binds_local_p (op) != 0")) + (match_test "loongarch_symbol_binds_local_p (op) != 0") + (match_test "loongarch_symbol_binds_local_p (op) == 0 + && TARGET_DIRECT_EXTERN_ACCESS")) (match_test "CONSTANT_P (op)"))) (define_predicate "is_const_call_no_local_symbol" (and (match_operand 0 "const_call_insn_operand") (ior (match_test "loongarch_global_symbol_p (op) != 0") (match_test "loongarch_symbol_binds_local_p (op) == 0") - (match_test "loongarch_weak_symbol_p (op) != 0")) + (match_test "loongarch_weak_symbol_p (op) != 0") + (match_test "!TARGET_DIRECT_EXTERN_ACCESS")) (match_test "CONSTANT_P (op)"))) testsuites: extern void test1(void); void test(void) { test1(); } $ gcc test.c -o - -O2 -mdirect-extern-access If the above modifications are not added, the function call is: bl %plt(test1) now is : bl test1 在 2022/9/2 下午7:30, Xi Ruoyao 写道: > On Thu, 2022-09-01 at 18:54 +0800, Xi Ruoyao wrote: >> We'd like to introduce a new codegen option to align with the old >> "-Wa,-mla-global-with-pcrel" and avoid a performance & size regression >> building the Linux kernel with new-reloc toolchain. And it should be >> also useful for building statically linked executables, firmwares (EDK2 >> for example), and other OS kernels. > Some news: get rid of the GOT will also make the implementation of > relocatable kernel easier, so I hope this can be reviewed quickly. > >> OK for trunk? >> >> -- >8 -- >> >> As a new target, LoongArch does not use copy relocation as it's >> problematic in some circumstances. One bad consequence is we are >> emitting GOT for all accesses to all extern objects with default >> visibility. The use of GOT is not needed in statically linked >> executables, OS kernels etc. The GOT entry just wastes space, and the >> GOT access just slow down the execution in those environments. >> >> Before -mexplicit-relocs, we used "-Wa,-mla-global-with-pcrel" to tell >> the assembler not to use GOT for extern access. But with >> -mexplicit-relocs, we have to opt the logic in GCC. >> >> The name "-mdirect-extern-access" is learnt from x86 port. >> >> gcc/ChangeLog: >> >> * config/loongarch/genopts/loongarch.opt.in: Add >> -mdirect-extern-access option. >> * config/loongarch/loongarch.opt: Regenerate. >> * config/loongarch/loongarch.cc (loongarch_classify_symbol): >> Don't use SYMBOL_GOT_DISP if TARGET_DIRECT_EXTERN_ACCESS. >> (loongarch_option_override_internal): Complain if >> -mdirect-extern-access is used with -fPIC or -fpic. >> * doc/invoke.texi: Document -mdirect-extern-access for >> LoongArch. >> >> gcc/testsuite/ChangeLog: >> >> * gcc.target/loongarch/direct-extern-1.c: New test. >> * gcc.target/loongarch/direct-extern-2.c: New test. >> --- >> gcc/config/loongarch/genopts/loongarch.opt.in | 4 ++++ >> gcc/config/loongarch/loongarch.cc | 5 ++++- >> gcc/config/loongarch/loongarch.opt | 4 ++++ >> gcc/doc/invoke.texi | 15 >> +++++++++++++++ >> .../gcc.target/loongarch/direct-extern-1.c | 6 ++++++ >> .../gcc.target/loongarch/direct-extern-2.c | 6 ++++++ >> 6 files changed, 39 insertions(+), 1 deletion(-) >> create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern- >> 1.c >> create mode 100644 gcc/testsuite/gcc.target/loongarch/direct-extern- >> 2.c >> >> diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in >> b/gcc/config/loongarch/genopts/loongarch.opt.in >> index ebdd9538d48..e10618777b2 100644 >> --- a/gcc/config/loongarch/genopts/loongarch.opt.in >> +++ b/gcc/config/loongarch/genopts/loongarch.opt.in >> @@ -184,3 +184,7 @@ Enum(cmodel) String(@@STR_CMODEL_EXTREME@@) >> Value(CMODEL_EXTREME) >> mcmodel= >> Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) >> Init(CMODEL_NORMAL) >> Specify the code model. >> + >> +mdirect-extern-access >> +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) >> +Avoid using the GOT to access external symbols. >> diff --git a/gcc/config/loongarch/loongarch.cc >> b/gcc/config/loongarch/loongarch.cc >> index 77e3a105390..2875fa5b0f3 100644 >> --- a/gcc/config/loongarch/loongarch.cc >> +++ b/gcc/config/loongarch/loongarch.cc >> @@ -1642,7 +1642,7 @@ loongarch_classify_symbol (const_rtx x) >> if (SYMBOL_REF_TLS_MODEL (x)) >> return SYMBOL_TLS; >> >> - if (!loongarch_symbol_binds_local_p (x)) >> + if (!TARGET_DIRECT_EXTERN_ACCESS && !loongarch_symbol_binds_local_p >> (x)) >> return SYMBOL_GOT_DISP; >> >> tree t = SYMBOL_REF_DECL (x); >> @@ -6093,6 +6093,9 @@ loongarch_option_override_internal (struct >> gcc_options *opts) >> if (loongarch_branch_cost == 0) >> loongarch_branch_cost = loongarch_cost->branch_cost; >> >> + if (TARGET_DIRECT_EXTERN_ACCESS && flag_shlib) >> + error ("%qs cannot be used for compiling a shared library", >> + "-mdirect-extern-access"); >> >> switch (la_target.cmodel) >> { >> diff --git a/gcc/config/loongarch/loongarch.opt >> b/gcc/config/loongarch/loongarch.opt >> index 6395234218b..96c811c850b 100644 >> --- a/gcc/config/loongarch/loongarch.opt >> +++ b/gcc/config/loongarch/loongarch.opt >> @@ -191,3 +191,7 @@ Enum(cmodel) String(extreme) Value(CMODEL_EXTREME) >> mcmodel= >> Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) >> Init(CMODEL_NORMAL) >> Specify the code model. >> + >> +mdirect-extern-access >> +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) >> +Avoid using the GOT to access external symbols. >> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi >> index e5eb525a2c1..d4e86682827 100644 >> --- a/gcc/doc/invoke.texi >> +++ b/gcc/doc/invoke.texi >> @@ -1016,6 +1016,7 @@ Objective-C and Objective-C++ Dialects}. >> -memcpy -mno-memcpy -mstrict-align -mno-strict-align @gol >> -mmax-inline-memcpy-size=@var{n} @gol >> -mexplicit-relocs -mno-explicit-relocs @gol >> +-mdirect-extern-access -mno-direct-extern-access @gol >> -mcmodel=@var{code-model}} >> >> @emph{M32R/D Options} >> @@ -25100,6 +25101,20 @@ GCC build-time by detecting corresponding >> assembler support: >> @code{-mno-explicit-relocs} otherwise. This option is mostly useful >> for >> debugging, or interoperation with assemblers different from the >> build-time >> one. >> + >> +@item -mdirect-extern-access >> +@itemx -mno-direct-extern-access >> +@opindex mdirect-extern-access >> +Do not use or use GOT to access external symbols. The default is >> +@option{-mno-direct-extern-access}: GOT is used for external symbols >> with >> +default visibility, but not used for other external symbols. >> + >> +With @option{-mdirect-extern-access}, GOT is not used and all >> external >> +symbols are PC-relatively addressed. It is @strong{only} suitable >> for >> +environments where no dynamic link is performed, like firmwares, OS >> +kernels, executables linked with @option{-static} or @option{-static- >> pie}. >> +@option{-mdirect-extern-access} is not compatible with @option{-fPIC} >> or >> +@option{-fpic}. >> @end table >> >> @node M32C Options >> diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c >> b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c >> new file mode 100644 >> index 00000000000..85c6c1e8a88 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c >> @@ -0,0 +1,6 @@ >> +/* { dg-do compile } */ >> +/* { dg-options "-mexplicit-relocs -mdirect-extern-access" } */ >> +/* { dg-final { scan-assembler-not "got" } } */ >> + >> +extern int x; >> +int f() { return x; } >> diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c >> b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c >> new file mode 100644 >> index 00000000000..58d8bd68a06 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c >> @@ -0,0 +1,6 @@ >> +/* { dg-do compile } */ >> +/* { dg-options "-mno-explicit-relocs -mdirect-extern-access" } */ >> +/* { dg-final { scan-assembler-not "la.global" } } */ >> + >> +extern int x; >> +int f() { return x; }
On Sun, 2022-09-04 at 10:26 +0800, Lulu Cheng wrote: > If the above modifications are not added, the function call is: > > bl %plt(test1) > > now is : > > bl test1 Regarding "%plt(...)", in the binutils code: /* For compatible old asm code. */ if (0 == strcmp (op_c_str, "plt")) btype = BFD_RELOC_LARCH_B26; Link: https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gas/config/loongarch-parse.y;h=8704687706df50aa15aff05f97e4560d7ec6fa23;hb=refs/heads/master#l131 Zhensong: does "old asm code" here mean we should remove %plt from "new" assembly code, i. e. stop to print %plt(...) in GCC completely?
在 2022/9/4 上午10:51, Xi Ruoyao 写道: > On Sun, 2022-09-04 at 10:26 +0800, Lulu Cheng wrote: >> If the above modifications are not added, the function call is: >> >> bl %plt(test1) >> >> now is : >> >> bl test1 > Regarding "%plt(...)", in the binutils code: > > /* For compatible old asm code. */ > if (0 == strcmp (op_c_str, "plt")) > btype = BFD_RELOC_LARCH_B26; > > Link: https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gas/config/loongarch-parse.y;h=8704687706df50aa15aff05f97e4560d7ec6fa23;hb=refs/heads/master#l131 > > Zhensong: does "old asm code" here mean we should remove %plt from "new" > assembly code, i. e. stop to print %plt(...) in GCC completely? > I think '%plt' also needs to be removed from the readability of the assembly code.:-\
On Sun, 2022-09-04 at 11:22 +0800, Lulu Cheng wrote: > > 在 2022/9/4 上午10:51, Xi Ruoyao 写道: > > > On Sun, 2022-09-04 at 10:26 +0800, Lulu Cheng wrote: > > > > > If the above modifications are not added, the function call is: > > > > > > bl %plt(test1) > > > > > > now is : > > > > > > bl test1 > > Regarding "%plt(...)", in the binutils code: > > > > /* For compatible old asm code. */ > > if (0 == strcmp (op_c_str, "plt")) > > btype = BFD_RELOC_LARCH_B26; > > > > Link: > > https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gas/config/loongarch-parse.y;h=8704687706df50aa15aff05f97e4560d7ec6fa23;hb=refs/heads/master#l131 > > > > Zhensong: does "old asm code" here mean we should remove %plt from > > "new" > > assembly code, i. e. stop to print %plt(...) in GCC completely? > > > > I think '%plt' also needs to be removed from the readability of the > assembly code.:-\ I understand, but maybe we should remove %plt unconditionally, with or without -mdirect-extern-access. Note that for -mcmodel=medium we don't say something like "%pc_hi20(%plt(x))" either.
在 2022/9/4 下午2:35, Xi Ruoyao 写道: > On Sun, 2022-09-04 at 11:22 +0800, Lulu Cheng wrote: >> 在 2022/9/4 上午10:51, Xi Ruoyao 写道: >> >>> On Sun, 2022-09-04 at 10:26 +0800, Lulu Cheng wrote: >>> >>>> If the above modifications are not added, the function call is: >>>> >>>> bl %plt(test1) >>>> >>>> now is : >>>> >>>> bl test1 >>> Regarding "%plt(...)", in the binutils code: >>> >>> /* For compatible old asm code. */ >>> if (0 == strcmp (op_c_str, "plt")) >>> btype = BFD_RELOC_LARCH_B26; >>> >>> Link: >>> https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gas/config/loongarch-parse.y;h=8704687706df50aa15aff05f97e4560d7ec6fa23;hb=refs/heads/master#l131 >>> >>> Zhensong: does "old asm code" here mean we should remove %plt from >>> "new" >>> assembly code, i. e. stop to print %plt(...) in GCC completely? >>> >> >> I think '%plt' also needs to be removed from the readability of the >> assembly code.:-\ > I understand, but maybe we should remove %plt unconditionally, with or > without -mdirect-extern-access. Note that for -mcmodel=medium we don't > say something like "%pc_hi20(%plt(x))" either. I have thought about this problem. For example, there is no '%plt' in aarch64, but I think it can be added and easily distinguished at the assembly code level, so this is not removed.
On Sun, Sep 4, 2022 at 12:00 AM Lulu Cheng <chenglulu@loongson.cn> wrote: > > > 在 2022/9/4 下午2:35, Xi Ruoyao 写道: > > On Sun, 2022-09-04 at 11:22 +0800, Lulu Cheng wrote: > >> 在 2022/9/4 上午10:51, Xi Ruoyao 写道: > >> > >>> On Sun, 2022-09-04 at 10:26 +0800, Lulu Cheng wrote: > >>> > >>>> If the above modifications are not added, the function call is: > >>>> > >>>> bl %plt(test1) > >>>> > >>>> now is : > >>>> > >>>> bl test1 > >>> Regarding "%plt(...)", in the binutils code: > >>> > >>> /* For compatible old asm code. */ > >>> if (0 == strcmp (op_c_str, "plt")) > >>> btype = BFD_RELOC_LARCH_B26; > >>> > >>> Link: > >>> https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=gas/config/loongarch-parse.y;h=8704687706df50aa15aff05f97e4560d7ec6fa23;hb=refs/heads/master#l131 > >>> > >>> Zhensong: does "old asm code" here mean we should remove %plt from > >>> "new" > >>> assembly code, i. e. stop to print %plt(...) in GCC completely? > >>> > >> > >> I think '%plt' also needs to be removed from the readability of the > >> assembly code.:-\ > > I understand, but maybe we should remove %plt unconditionally, with or > > without -mdirect-extern-access. Note that for -mcmodel=medium we don't > > say something like "%pc_hi20(%plt(x))" either. > > > I have thought about this problem. For example, there is no '%plt' in > aarch64, but I think it can be added and easily distinguished at the > assembly code level, > > so this is not removed. I think @plt should be removed unconditionally. It was a mistake in some ABI (e.g. i386, riscv).
On Sun, 2022-09-04 at 00:38 -0700, Fangrui Song wrote: > On Sun, Sep 4, 2022 at 12:00 AM Lulu Cheng <chenglulu@loongson.cn> > wrote: > > I have thought about this problem. For example, there is no '%plt' > > in > > aarch64, but I think it can be added and easily distinguished at the > > assembly code level, > > > > so this is not removed. > > I think @plt should be removed unconditionally. It was a mistake in > some ABI (e.g. i386, riscv). I've sent v2 (https://gcc.gnu.org/pipermail/gcc-patches/2022-September/600955.html), with TARGET_DIRECT_EXTERN_ACCESS check in loongarch_symbol_binds_local_p so we don't need to duplicate the check 3 times. Regarding "%plt", I still suggest to remove it globally. It's not so easy to be distinguished in the compile time, as only the linker will know if the PLT should be used (unless, if -fPIC -fplt and calling a default-visible function then we know PLT must be used). An explicit "%plt" sometimes misleads newbies to think %plt would force the linker to emit a PLT entry but it's not true. You can discuss this with Zhensong internally in the following week, I think. But let's defer the "controversial" thing and eliminate the GOT in upcoming Linux-6.1 first...
diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in index ebdd9538d48..e10618777b2 100644 --- a/gcc/config/loongarch/genopts/loongarch.opt.in +++ b/gcc/config/loongarch/genopts/loongarch.opt.in @@ -184,3 +184,7 @@ Enum(cmodel) String(@@STR_CMODEL_EXTREME@@) Value(CMODEL_EXTREME) mcmodel= Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) Init(CMODEL_NORMAL) Specify the code model. + +mdirect-extern-access +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) +Avoid using the GOT to access external symbols. diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index 77e3a105390..2875fa5b0f3 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -1642,7 +1642,7 @@ loongarch_classify_symbol (const_rtx x) if (SYMBOL_REF_TLS_MODEL (x)) return SYMBOL_TLS; - if (!loongarch_symbol_binds_local_p (x)) + if (!TARGET_DIRECT_EXTERN_ACCESS && !loongarch_symbol_binds_local_p (x)) return SYMBOL_GOT_DISP; tree t = SYMBOL_REF_DECL (x); @@ -6093,6 +6093,9 @@ loongarch_option_override_internal (struct gcc_options *opts) if (loongarch_branch_cost == 0) loongarch_branch_cost = loongarch_cost->branch_cost; + if (TARGET_DIRECT_EXTERN_ACCESS && flag_shlib) + error ("%qs cannot be used for compiling a shared library", + "-mdirect-extern-access"); switch (la_target.cmodel) { diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt index 6395234218b..96c811c850b 100644 --- a/gcc/config/loongarch/loongarch.opt +++ b/gcc/config/loongarch/loongarch.opt @@ -191,3 +191,7 @@ Enum(cmodel) String(extreme) Value(CMODEL_EXTREME) mcmodel= Target RejectNegative Joined Enum(cmodel) Var(la_opt_cmodel) Init(CMODEL_NORMAL) Specify the code model. + +mdirect-extern-access +Target Var(TARGET_DIRECT_EXTERN_ACCESS) Init(0) +Avoid using the GOT to access external symbols. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e5eb525a2c1..d4e86682827 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -1016,6 +1016,7 @@ Objective-C and Objective-C++ Dialects}. -memcpy -mno-memcpy -mstrict-align -mno-strict-align @gol -mmax-inline-memcpy-size=@var{n} @gol -mexplicit-relocs -mno-explicit-relocs @gol +-mdirect-extern-access -mno-direct-extern-access @gol -mcmodel=@var{code-model}} @emph{M32R/D Options} @@ -25100,6 +25101,20 @@ GCC build-time by detecting corresponding assembler support: @code{-mno-explicit-relocs} otherwise. This option is mostly useful for debugging, or interoperation with assemblers different from the build-time one. + +@item -mdirect-extern-access +@itemx -mno-direct-extern-access +@opindex mdirect-extern-access +Do not use or use GOT to access external symbols. The default is +@option{-mno-direct-extern-access}: GOT is used for external symbols with +default visibility, but not used for other external symbols. + +With @option{-mdirect-extern-access}, GOT is not used and all external +symbols are PC-relatively addressed. It is @strong{only} suitable for +environments where no dynamic link is performed, like firmwares, OS +kernels, executables linked with @option{-static} or @option{-static-pie}. +@option{-mdirect-extern-access} is not compatible with @option{-fPIC} or +@option{-fpic}. @end table @node M32C Options diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c new file mode 100644 index 00000000000..85c6c1e8a88 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-1.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-mexplicit-relocs -mdirect-extern-access" } */ +/* { dg-final { scan-assembler-not "got" } } */ + +extern int x; +int f() { return x; } diff --git a/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c new file mode 100644 index 00000000000..58d8bd68a06 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/direct-extern-2.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-mno-explicit-relocs -mdirect-extern-access" } */ +/* { dg-final { scan-assembler-not "la.global" } } */ + +extern int x; +int f() { return x; }