From patchwork Tue Sep 19 07:01:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rui Ueyama X-Patchwork-Id: 141711 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3182829vqi; Tue, 19 Sep 2023 00:02:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEutTDwqLUfaH6XVycKpqmzG/anG+2MyRxgR+mrwha18qHk+E85K4UeTbWRuCheVUQd0oyW X-Received: by 2002:a05:6402:2294:b0:530:c3be:e4d4 with SMTP id cw20-20020a056402229400b00530c3bee4d4mr6660167edb.29.1695106952692; Tue, 19 Sep 2023 00:02:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695106952; cv=none; d=google.com; s=arc-20160816; b=gfOmtlX1SrgJJNqn+0EkoiHle+cIpAPJDKk9jNADlUyDn4qrQKZ+YA6rHVlK9BrL7u ozm84Ta9Fzs+3jqU5FFZhFr+/zcBx6Z95HDlMrfxBevtSTxKy4NF8vya8mx7+yimhHnD pc7sEmFv625X3dNlpG4WHVXAsU8m5cbpqVGZPjsZJw5IRao8C1vcu1Zl1NcAYIssRrXQ tqAo0PMGVipAc+5mNzvsd56bs02DzNTIkKCgoNx4c1g36KIMYUeC488TnvaVgtMEnYKr wo0GFRgXDDu67EgcZl6Rr1Xksslbd83Z39la2gpo9HBk73o+O6JfAcMKGKQmaIYkEUAo 0wmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=5rz0JxUc7vipoOicEkSlECmozYkjx/ggJ+vQymmz0Z8=; fh=IOBfOMT8uwCsDah4JNZqyBK2zq+IlHdwaDoKSUqLeTo=; b=i6yPotahkK/iRJStAtIxFo/ED8JyE/CSr/U/Vd6IY5zZgoRroUheWFDKBCi6w5XKah 5tQn5p7oVwzroDoiY9ozJlmv/xSpl0/6eFujoI1SELW726H3lGdaWlGyjSB4hfmtgc9M K74w0HWCs0lJAX85Ft87Q9EMHCzDDk50zWxAdCY8OiroQccgU8fiNHPkxp6Gl/GZaqsi fqOlTY3E9aVWExjtxRIwXU1fYVYPkLNHl4gpias3fUnsZj+ZGxQK59TxYldlFLULzmy/ KLCLZulWVbCsHuDen1Vd1cpeqS1cYZ4HHHR7XIxdUPA4tzWL4sKsz3Sv8bBQTfNNiwxy 6snw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ooIVnGYs; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id x11-20020aa7d38b000000b00525667da231si9465546edq.82.2023.09.19.00.02.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 00:02:32 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ooIVnGYs; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9124A38582AC for ; Tue, 19 Sep 2023 07:02:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9124A38582AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695106951; bh=5rz0JxUc7vipoOicEkSlECmozYkjx/ggJ+vQymmz0Z8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ooIVnGYsBUtN+PRbnuFYjIZG4Hr40/ouaJ7uKLj3ZOj/MBbYzh5vKlR3HBPCnmrdb tCzG7bNKQdnEhdIwxomzfEXPtMvKWmaIHTXEOP0IEdgzNyTxp02f5QZSs+K5ta++Z/ IdBhEZfP9sPsaeuhHDps3+MlkNnJT2A/YlLj3mQE= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id AA05D3858D32 for ; Tue, 19 Sep 2023 07:02:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AA05D3858D32 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-68fb2e9ebcdso3970904b3a.2 for ; Tue, 19 Sep 2023 00:02:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695106941; x=1695711741; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5rz0JxUc7vipoOicEkSlECmozYkjx/ggJ+vQymmz0Z8=; b=BnH1tySL6BNEs/sGATEXFI5sHWFP0JNDiQpsET5fmteVcDl9lEyGqvq3Yc6IHZznsJ /Z3iBv+d7kZzzYD5u0YpegtiT2yUHDpN/26MrFtMJQrc/W9+pjHmlFeS6IcXOaqOEbxb kOSAOP1/Xo/g/BclaZ1yw+NQ505V/YexG60o7TGwnlTC2/K+h/hNvEUKJ71A6IiRUxh2 4hou/xn71ub6edgqdzT/hRlu3cM5drf8UdMwmgGKcazvq2lde/6uU9c0icmip8+hYmCv /sg9rXyIcNgxF4+Psyd90LkDpi8JyE6C9wYC21f7cqtX7EQRCm34Tn/gk/Mdmin3IbzV orFg== X-Gm-Message-State: AOJu0YxCz9C1+5eejHsCMSiyOt7txG8XeCFaf/AofJ3P4PF2RzbrQ1Jn e4D5bD0nR2Sa6uuca/KZwOk6UWkVirI= X-Received: by 2002:a05:6a00:23c4:b0:68e:29a6:e247 with SMTP id g4-20020a056a0023c400b0068e29a6e247mr12094845pfc.10.1695106940594; Tue, 19 Sep 2023 00:02:20 -0700 (PDT) Received: from blue.flets-east.jp ([2405:6580:8dc0:3c00:8694:ace3:6efb:65a2]) by smtp.gmail.com with ESMTPSA id s12-20020a62e70c000000b0068fda1db80bsm8030488pfh.75.2023.09.19.00.02.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 00:02:20 -0700 (PDT) X-Google-Original-From: Rui Ueyama To: binutils@sourceware.org Cc: Rui Ueyama Subject: [PATCH] RISC-V: emit R_RISCV_RELAX for the la pseudo instruction Date: Tue, 19 Sep 2023 16:01:21 +0900 Message-Id: <20230919070121.1489019-1-ruiu@bluewhale.systems> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, 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: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Rui Ueyama via Binutils From: Rui Ueyama Reply-To: Rui Ueyama Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777448467770640568 X-GMAIL-MSGID: 1777448467770640568 Some psABIs define a relaxation to turn a GOT load into a PC-relative address materialization. For example, the AArch64's psABI allows adrp+ldr to be rewritten to nop+adr to eliminate the memory load. This patch is part of the effort to make such optimization possible for RISC-V. For RISC-V, we use the la assembly pseudo instruction to load a symbol address from the GOT. The pseudo instruction is expanded to auipc+ld. If the address loaded by the instruction pair is actually a PC-relative link-time constant, we want the linker to rewrite the instruction pair with auipc+addi. We can't rewrite all existing auipc+ld pairs with auipc+addi in the linker because there might be code that jumps to the middle of the instruction pair. That should be extremely rare, if ever exists, but you can at least in theory write a program in assembly that jumps to the ld instruction of the instruction pair. We need a marker to identify that an auipc+ld can be safely relaxed (i.e. they are emitted for la). This patch is to annotate R_RISCV_GOT_HI20 with R_RISCV_RELAX only when the relocation is emitted for the la pseudo instruction. The linker will use it as a signal that the instruction pair can be safely relaxed. Proposal to the RISC-V psABI: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/397 --- bfd/bfd-in2.h | 1 + bfd/elfxx-riscv.c | 1 + gas/config/tc-riscv.c | 3 ++- gas/testsuite/gas/riscv/la-variants.d | 3 +++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index 1c4f75ae244..e15641a1d00 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -5413,6 +5413,7 @@ number for the SBIC, SBIS, SBI and CBI instructions */ BFD_RELOC_RISCV_SUB32, BFD_RELOC_RISCV_SUB64, BFD_RELOC_RISCV_GOT_HI20, + BFD_RELOC_RISCV_GOT_HI20_RELAX, BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_JMP, diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c index 6ed657171f0..71e63e7b789 100644 --- a/bfd/elfxx-riscv.c +++ b/bfd/elfxx-riscv.c @@ -913,6 +913,7 @@ static const struct elf_reloc_map riscv_reloc_map[] = { BFD_RELOC_RISCV_PCREL_HI20, R_RISCV_PCREL_HI20 }, { BFD_RELOC_RISCV_JMP, R_RISCV_JAL }, { BFD_RELOC_RISCV_GOT_HI20, R_RISCV_GOT_HI20 }, + { BFD_RELOC_RISCV_GOT_HI20_RELAX, R_RISCV_GOT_HI20 }, { BFD_RELOC_RISCV_TLS_DTPMOD32, R_RISCV_TLS_DTPMOD32 }, { BFD_RELOC_RISCV_TLS_DTPREL32, R_RISCV_TLS_DTPREL32 }, { BFD_RELOC_RISCV_TLS_DTPMOD64, R_RISCV_TLS_DTPMOD64 }, diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index 3b520ad208b..303ae18436c 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -2039,7 +2039,7 @@ macro (struct riscv_cl_insn *ip, expressionS *imm_expr, else if ((riscv_opts.pic && mask == M_LA) || mask == M_LGA) pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN, - BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); + BFD_RELOC_RISCV_GOT_HI20_RELAX, BFD_RELOC_RISCV_PCREL_LO12_I); /* Local PIC symbol, or any non-PIC symbol. */ else pcrel_load (rd, rd, imm_expr, "addi", @@ -4244,6 +4244,7 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) relaxable = true; break; + case BFD_RELOC_RISCV_GOT_HI20_RELAX: case BFD_RELOC_RISCV_PCREL_HI20: case BFD_RELOC_RISCV_PCREL_LO12_S: case BFD_RELOC_RISCV_PCREL_LO12_I: diff --git a/gas/testsuite/gas/riscv/la-variants.d b/gas/testsuite/gas/riscv/la-variants.d index b1d316983b7..e8ac09c2af2 100644 --- a/gas/testsuite/gas/riscv/la-variants.d +++ b/gas/testsuite/gas/riscv/la-variants.d @@ -21,11 +21,13 @@ Disassembly of section .text: [ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* [ ]+[0-9a-f]+:[ ]+00000617[ ]+auipc[ ]+a2,0x0 [ ]+[0-9a-f]+:[ ]+R_RISCV_GOT_HI20[ ]+a +[ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* [ ]+[0-9a-f]+:[ ]+(00062603|00063603)[ ]+(lw|ld)[ ]+a2,0\(a2\).* [ ]+[0-9a-f]+:[ ]+R_RISCV_PCREL_LO12_I[ ]+\.L0[ ]+ [ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* [ ]+[0-9a-f]+:[ ]+00000697[ ]+auipc[ ]+a3,0x0 [ ]+[0-9a-f]+:[ ]+R_RISCV_GOT_HI20[ ]+a +[ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* [ ]+[0-9a-f]+:[ ]+(0006a683|0006b683)[ ]+(lw|ld)[ ]+a3,0\(a3\).* [ ]+[0-9a-f]+:[ ]+R_RISCV_PCREL_LO12_I[ ]+\.L0[ ]+ [ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* @@ -37,6 +39,7 @@ Disassembly of section .text: [ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* [ ]+[0-9a-f]+:[ ]+00000797[ ]+auipc[ ]+a5,0x0 [ ]+[0-9a-f]+:[ ]+R_RISCV_GOT_HI20[ ]+a +[ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\* [ ]+[0-9a-f]+:[ ]+(0007a783|0007b783)[ ]+(lw|ld)[ ]+a5,0\(a5\).* [ ]+[0-9a-f]+:[ ]+R_RISCV_PCREL_LO12_I[ ]+\.L0[ ]+ [ ]+[0-9a-f]+:[ ]+R_RISCV_RELAX[ ]+\*ABS\*