From patchwork Sun Sep 4 13:18:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 1673968 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=GqpTAMRr; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MLC121fhjz1yhP for ; Sun, 4 Sep 2022 23:19:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3EDFA3857BBE for ; Sun, 4 Sep 2022 13:19:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3EDFA3857BBE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662297569; bh=CwANqTqeW5E1Rb6UAOA7Y+Rp7xVdyxk0P/V0SLcwiLQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=GqpTAMRrL6A7ehr1diT3+v4czWMT0lJ856ywLf7ZhLfWYT5JP8wqU6WNYoYelFTb1 Nt35igGwOcr0a0RRZtmY14qqqvbCFXSMWgEiwN9gVwCKZgSNLm0OfSTA/ckKZTfkWo AAovOK7DzgSMMLaJ0fe691x5WSltptNBotzDJ+Rw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by sourceware.org (Postfix) with ESMTPS id 5EC573858C50 for ; Sun, 4 Sep 2022 13:19:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5EC573858C50 Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11b4:5800: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 2F80A6681C; Sun, 4 Sep 2022 09:19:02 -0400 (EDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH v2] LoongArch: add -mdirect-extern-access option Date: Sun, 4 Sep 2022 21:18:51 +0800 Message-Id: <20220904131851.35395-1-xry111@xry111.site> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 X-Spam-Status: No, score=-4.8 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Cc: Chenghua Xu , Huacai Chen , Lulu Cheng , Wang Xuerui Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" v1 -> v2: Move TARGET_DIRECT_EXTERN_ACCESS check from loongarch_classify_symbol to loongarch_symbol_binds_local_p, so "%plt" is not output if -mdirect-extern-access used. -- >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_symbol_binds_local_p): Return true 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 | 6 ++++++ 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, 41 insertions(+) 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..c9187bf81a7 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -1610,6 +1610,9 @@ loongarch_weak_symbol_p (const_rtx x) bool loongarch_symbol_binds_local_p (const_rtx x) { + if (TARGET_DIRECT_EXTERN_ACCESS) + return true; + if (SYMBOL_REF_P (x)) return (SYMBOL_REF_DECL (x) ? targetm.binds_local_p (SYMBOL_REF_DECL (x)) @@ -6093,6 +6096,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 dd3302fcd15..9d662e35316 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -1017,6 +1017,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} @@ -25090,6 +25091,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; }