From patchwork Thu Nov 23 06:40:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lifang Xia X-Patchwork-Id: 168729 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp256554vqx; Wed, 22 Nov 2023 22:40:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IFPYxO5y9fXEKfze99DnhxACPA7gIE1PtKmwXbPpeXyY07aY89eBgb4jqOwWYwe8mrE/o1o X-Received: by 2002:a05:620a:29ca:b0:779:d1ea:c5da with SMTP id s10-20020a05620a29ca00b00779d1eac5damr5537699qkp.37.1700721631406; Wed, 22 Nov 2023 22:40:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700721631; cv=pass; d=google.com; s=arc-20160816; b=LvkTqshNus3mpjl6H0bVfTay66jcbn7GWyD6a0sW7dPSLD4IfLHipKVrJ52lS9TTFm K1Osd4t8rvibjEiI4DurMjtaxZjemBI32kmIMl/ula3gAddabk//uxJRwBEXG3Bj+cFN OYsmgrEhvLNrt8omUeLHYnWpnzsIgXLhHlE9rA0G+UOCuIfO4dCQW7JDm4Pxa1Lv6jHl ks/nRw3UZy4JPqWcr1j4U18dpEQZmiQGwCmtnQgeUbbeoyGloqeui+9YCD0bBuyH7ZcD GMAUcEaEmOVzUOboD1NswK9CCM4wAZw+JJMkrZjwTDkOI+6huQ6cd1ZOymNGe25ZpRiS A9uA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:arc-filter :dmarc-filter:delivered-to; bh=ox6OA27KdIMnbCyWMaAIXQgjyirY/IGa8x4dRxQ/db4=; fh=prX3mKBoGhO6oMQYlWkaQU2ujfPJB+rrn2vrhT9qq/Y=; b=ThvQ7jBJaQtLXAgh3JkfaSReiOk+7TS4nAzw4RNTYrPGVwHodlfQJOu4bdW6JpsMTV Nbo1JH7SFJ3xq+8C5wDTrxPbXGRTshj7HZ5hAK2ev1sgWZtFnGmU+eErc8YKuDWqeRKO aUmps9zqLt+J5VUqoDRhmApw63W6+xE/3IeSy8cxc8Vyp5wDYsA5eKSEvp21ws51Tkwq dpmyz5UAletCiFBuqgyiibbVSYB0gYgup3BuVs9tOUspCNtOxWGDp8HouxMpO1636jT+ RXdZjCi56CLlsrfy9n8s5i5yJTnDZLejMF1wpRBmjwcsjnZKfOEZ/rUPLdVsxAyOPIC1 b+sw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s18-20020a05620a29d200b0077d67849375si584795qkp.102.2023.11.22.22.40.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 22:40:31 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.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; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F731385842E for ; Thu, 23 Nov 2023 06:40:31 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) by sourceware.org (Postfix) with ESMTPS id 28F213858D33 for ; Thu, 23 Nov 2023 06:40:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 28F213858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.alibaba.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 28F213858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=115.124.30.119 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700721625; cv=none; b=VddioCCR3bw96ipfoJfsjRQYQ4ipE9fGLGc3G2Gbfm7gXQGcHmLggVbYqhg70R3RkuJuLU7xNEj89ZtTrq5N3rDD+0ztB6HKS1OQF9lffjaPMy/w4LEKYD7iIrC1JeD5COQjG5mXMvSR1BB8Yt9pfFSR+M1UbtmAjKiqU940Mmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700721625; c=relaxed/simple; bh=K1yXahS3rDWyK5zro31ZbBzAIe442aD8X4KTBpJ5oss=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=jslvmaXhKyo7HHW6toh9ZBCvhAZ5RluAyDS0QNwa77prU2hhx7ffmtxlU39hKDU5xe0H+7tnf/DWv4R5iMAcYwIcRGcffoWd95bMWC0jvYT36LBGMwfRwx+Mf0hWZZeB/Ibpfa3kqHGu8TzGS85cy0vMNsBPR8VxOlm3Hj+h6Vc= ARC-Authentication-Results: i=1; server2.sourceware.org X-Alimail-AntiSpam: AC=PASS; BC=-1|-1; BR=01201311R271e4; CH=green; DM=||false|; DS=||; FP=0|-1|-1|-1|0|-1|-1|-1; HT=ay29a033018046060; MF=lifang_xia@linux.alibaba.com; NM=1; PH=DS; RN=6; SR=0; TI=SMTPD_---0Vwy9bys_1700721618; Received: from localhost.localdomain(mailfrom:lifang_xia@linux.alibaba.com fp:SMTPD_---0Vwy9bys_1700721618) by smtp.aliyun-inc.com; Thu, 23 Nov 2023 14:40:19 +0800 From: lifang_xia@linux.alibaba.com To: binutils@sourceware.org Cc: nelson@rivosinc.com, kito.cheng@gmail.com, andrew@sifive.com, palmer@dabbelt.com, Lifang Xia Subject: [PATCH] RISCV: Do fixup for local symbols while with "-mno-relax" Date: Thu, 23 Nov 2023 14:40:06 +0800 Message-Id: <20231123064006.96381-1-lifang_xia@linux.alibaba.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) MIME-Version: 1.0 X-Spam-Status: No, score=-21.0 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY, USER_IN_DEF_SPF_WL 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: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783335885435753914 X-GMAIL-MSGID: 1783335885435753914 From: Lifang Xia In the scenario of generating .ko files, the kernel does not relax the .ko files. However, due to the large amount of relax and local relocation information, this increases the size of the .ko files. In this patch, it will finish the fixup of the local relocations while with "-mno-relax" option. This can reduce the size of the relocation table. gas/ * config/tc-riscv.c (struct riscv_pcrel_hi_reloc): New, reference from bfd/elfnn-riscv.c. (riscv_pcrel_hi_reloc_hash): Likewise. (riscv_pcrel_reloc_hash): Likewise. (riscv_pcrel_reloc_eq): Likewise. (riscv_record_pcrel_reolc): Likewise. (md_begin): Init pcrel_hi hash. (md_apply_fix) : Do fixup and record the pcrel_hi relocs, mark as done while with "-mno-relax". (md_apply_fix) : (md_apply_fix) : Do fixup and mark as done while with "-mno-relax". (riscv_md_end): New, delete riscv_pcrel_hi_reloc_hash. * config/tc-riscv.h (md_end): Define md_end with riscv_md_end. gas/ * testsuite/gas/riscv/fixup-local*: New tests. --- gas/config/tc-riscv.c | 111 +++++++++++++++++- gas/config/tc-riscv.h | 3 + gas/testsuite/gas/riscv/fixup-local-norelax.d | 23 ++++ gas/testsuite/gas/riscv/fixup-local-relax.d | 41 +++++++ gas/testsuite/gas/riscv/fixup-local.s | 13 ++ 5 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 gas/testsuite/gas/riscv/fixup-local-norelax.d create mode 100644 gas/testsuite/gas/riscv/fixup-local-relax.d create mode 100644 gas/testsuite/gas/riscv/fixup-local.s diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index 402c46ad753..9a2163a25a9 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -1565,6 +1565,58 @@ init_opcode_hash (const struct riscv_opcode *opcodes, return hash; } +/* Record all PC-relative high-part relocs we've encountered to help us + later resolve the corresponding low-part relocs. */ +struct riscv_pcrel_hi_reloc +{ + bfd_vma address; + symbolS *symbol; + bfd_vma target; +}; + +/* Handle of the pcrel_hi hash table. */ +static htab_t riscv_pcrel_hi_reloc_hash; + +/* Get the key of a entry from the pcrel_hi hash table. */ + +static hashval_t +riscv_pcrel_reloc_hash (const void *entry) +{ + const struct riscv_pcrel_hi_reloc *e = entry; + return (hashval_t) (e->address); +} + +/* Compare the keys between two entries fo the pcrel_hi hash table. */ + +static int +riscv_pcrel_reloc_eq (const void *entry1, const void *entry2) +{ + const struct riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2; + return e1->address == e2->address; +} + +/* Record the pcrel_hi relocs. */ + +static bool +riscv_record_pcrel_reolc (htab_t p, bfd_vma address, symbolS *symbol, + bfd_vma target) +{ + struct riscv_pcrel_hi_reloc entry = { address, symbol, target }; + struct riscv_pcrel_hi_reloc **slot + = (struct riscv_pcrel_hi_reloc **)htab_find_slot (p, &entry, INSERT); + if (slot == NULL) + return false; + + *slot = (struct riscv_pcrel_hi_reloc *)xmalloc ( + sizeof (struct riscv_pcrel_hi_reloc)); + if (*slot != NULL) + { + **slot = entry; + return true; + } + return false; +} + /* This function is called once, at assembler startup time. It should set up all the tables, etc. that the MD part of the assembler will need. */ @@ -1601,6 +1653,11 @@ md_begin (void) opcode_names_hash = str_htab_create (); init_opcode_names_hash (); + /* Create pcrel_hi hash table to resolve the relocation while with + -mno-relax. */ + riscv_pcrel_hi_reloc_hash + = htab_create (1024, riscv_pcrel_reloc_hash, riscv_pcrel_reloc_eq, free); + /* Set the default alignment for the text section. */ record_alignment (text_section, riscv_opts.rvc ? 1 : 2); } @@ -4220,9 +4277,52 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) break; case BFD_RELOC_RISCV_PCREL_HI20: + /* record the pcrel_hi relocs of the local symbols. And evaluate the hi20 + of the lcoal symbols. Fill in a tentative value to improve objdump + readability for -mrelax, and set fx_done for -mno-relax. */ + if (S_IS_LOCAL (fixP->fx_addsy) && fixP->fx_addsy + && S_GET_SEGMENT (fixP->fx_addsy) == seg) + { + bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP; + bfd_vma value = target - md_pcrel_from (fixP); + + /* Record PCREL_HI20. */ + if (!riscv_record_pcrel_reolc (riscv_pcrel_hi_reloc_hash, + md_pcrel_from (fixP), fixP->fx_addsy, + target)) + { + as_warn ("too many pcrel_hi"); + } + + bfd_putl32 (bfd_getl32 (buf) + | ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)), + buf); + if (!riscv_opts.relax) + fixP->fx_done = 1; + } + relaxable = riscv_opts.relax; + break; case BFD_RELOC_RISCV_PCREL_LO12_S: case BFD_RELOC_RISCV_PCREL_LO12_I: - relaxable = riscv_opts.relax; + { + /* Resolve the low12 of the local symboles with the pcrel_hi relocs. + Fill in a tentative value to improve objdump readability for + -mrelax, and set fx_done for -mno-relax. */ + bfd_vma location_pcrel_hi = S_GET_VALUE (fixP->fx_addsy) + *valP; + struct riscv_pcrel_hi_reloc search = { location_pcrel_hi, 0, 0 }; + struct riscv_pcrel_hi_reloc *entry + = htab_find (riscv_pcrel_hi_reloc_hash, &search); + if (entry && entry->symbol && S_IS_LOCAL (entry->symbol) + && S_GET_SEGMENT (entry->symbol) == seg) + { + bfd_vma target = entry->target; + bfd_vma value = target - entry->address; + bfd_putl32 (bfd_getl32 (buf) | ENCODE_ITYPE_IMM (value), buf); + if (!riscv_opts.relax) + fixP->fx_done = 1; + } + relaxable = riscv_opts.relax; + } break; case BFD_RELOC_RISCV_ALIGN: @@ -4987,6 +5087,15 @@ riscv_md_finish (void) riscv_set_public_attributes (); if (riscv_opts.relax) bfd_map_over_sections (stdoutput, riscv_insert_uleb128_fixes, NULL); + +} + +/* Called just before the assembler exits. */ + +void +riscv_md_end (void) +{ + htab_delete (riscv_pcrel_hi_reloc_hash); } /* Adjust the symbol table. */ diff --git a/gas/config/tc-riscv.h b/gas/config/tc-riscv.h index 0c70c7d4739..9d2f05a4241 100644 --- a/gas/config/tc-riscv.h +++ b/gas/config/tc-riscv.h @@ -80,6 +80,9 @@ extern int riscv_parse_long_option (const char *); extern void riscv_pre_output_hook (void); #define GAS_SORT_RELOCS 1 +#define md_end riscv_md_end +extern void riscv_md_end (void); + /* Let the linker resolve all the relocs due to relaxation. */ #define tc_fix_adjustable(fixp) 0 #define md_allow_local_subtract(l,r,s) 0 diff --git a/gas/testsuite/gas/riscv/fixup-local-norelax.d b/gas/testsuite/gas/riscv/fixup-local-norelax.d new file mode 100644 index 00000000000..22132a32f91 --- /dev/null +++ b/gas/testsuite/gas/riscv/fixup-local-norelax.d @@ -0,0 +1,23 @@ +#as: -march=rv64i -mno-relax +#source: fixup-local.s +#objdump: -dr + +tmpdir/fixup-local.o: file format elf64-littleriscv + + +Disassembly of section .text: + +0+0000 : +[ ]+0:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+4:[ ]+00850513[ ]+addi a0,a0,8 # 8 +[ ]+8:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+8: R_RISCV_PCREL_HI20 bar +[ ]+c:[ ]+00050513[ ]+mv a0,a0 +[ ]+c: R_RISCV_PCREL_LO12_I .L0 +[ ]+10:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+10: R_RISCV_PCREL_HI20 foo +[ ]+14:[ ]+00050513[ ]+mv a0,a0 +[ ]+14: R_RISCV_PCREL_LO12_I .L0 +[ ]+18:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+1c:[ ]+00852503[ ]+lw a0,8\(a0\) # 20 +[ ]+20:[ ]+00008067[ ]+ret diff --git a/gas/testsuite/gas/riscv/fixup-local-relax.d b/gas/testsuite/gas/riscv/fixup-local-relax.d new file mode 100644 index 00000000000..49c475c5dcb --- /dev/null +++ b/gas/testsuite/gas/riscv/fixup-local-relax.d @@ -0,0 +1,41 @@ +#as: -march=rv64i +#source: fixup-local.s +#objdump: -dr + +tmpdir/fixup-local.o: file format elf64-littleriscv + + +Disassembly of section .text: + +0+0000 : +[ ]+0:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+0: R_RISCV_PCREL_HI20 .LL0 +[ ]+0: R_RISCV_RELAX \*ABS\* +[ ]+4:[ ]+00850513[ ]+addi a0,a0,8 # 8 <.LL0> +[ ]+4: R_RISCV_PCREL_LO12_I .L0 +[ ]+4: R_RISCV_RELAX \*ABS\* + +0000000000000008 <.LL0>: +[ ]+8:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+8: R_RISCV_PCREL_HI20 bar +[ ]+8: R_RISCV_RELAX \*ABS\* +[ ]+c:[ ]+00050513[ ]+mv a0,a0 +[ ]+c: R_RISCV_PCREL_LO12_I .L0 +[ ]+c: R_RISCV_RELAX \*ABS\* +[ ]+10:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+10: R_RISCV_PCREL_HI20 foo +[ ]+10: R_RISCV_RELAX \*ABS\* +[ ]+14:[ ]+00050513[ ]+mv a0,a0 +[ ]+14: R_RISCV_PCREL_LO12_I .L0 +[ ]+14: R_RISCV_RELAX \*ABS\* + +0000000000000018 <.LL1>: +[ ]+18:[ ]+00000517[ ]+auipc a0,0x0 +[ ]+18: R_RISCV_PCREL_HI20 .LL2 +[ ]+18: R_RISCV_RELAX \*ABS\* +[ ]+1c:[ ]+00852503[ ]+lw a0,8\(a0\) # 20 <.LL2> +[ ]+1c: R_RISCV_PCREL_LO12_I .LL1 +[ ]+1c: R_RISCV_RELAX \*ABS\* + +0000000000000020 <.LL2>: +[ ]+20:[ ]+00008067[ ]+ret diff --git a/gas/testsuite/gas/riscv/fixup-local.s b/gas/testsuite/gas/riscv/fixup-local.s new file mode 100644 index 00000000000..44b47311235 --- /dev/null +++ b/gas/testsuite/gas/riscv/fixup-local.s @@ -0,0 +1,13 @@ +.global foo +.global bar +foo: + la a0, .LL0 +.LL0: + la a0, bar + la a0, foo +.LL1: + auipc a0, %pcrel_hi(.LL2) + lw a0, %pcrel_lo(.LL1)(a0) + +.LL2: + ret