From patchwork Wed Nov 2 05:04:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nelson Chu X-Patchwork-Id: 14028 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3402258wru; Tue, 1 Nov 2022 22:04:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7/voExwryduZF+z5XnYNjEXKyrKY5QsV2eq+25DbwM3fpIIE1iQ4UX19qmIcpUJqMduK7h X-Received: by 2002:aa7:db4e:0:b0:463:2979:5e49 with SMTP id n14-20020aa7db4e000000b0046329795e49mr17707262edt.266.1667365489860; Tue, 01 Nov 2022 22:04:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667365489; cv=none; d=google.com; s=arc-20160816; b=z+DpIPKEC1Q5etZxAakFOpYSxmtUsztraXFGa/3izG4n9PMLPM0zgPnJGic4lKaupp yNp3sNqwwnfkvJpC7ulVywRI3801CXeLFreQdBuQaqTjXvqq3rhWJFGK1aGo+R36NpIQ V/LeubSE8k7avTT1tq+5zwHZda745w41l85iMj7iK0Lj3lfNan9tsCg4rLoabQqPdgBj iUL6anRTjVGqw+0kerEaBi+uJSRFokXA+f85115mquLFWpc9uTmaKns9A0W/Lf3wk75/ N8ufSo+WbmFbvUo1aYbkwHOIz++3aA8Rob/nNRs9OfzcdxBNsMZpxDzQoXHBHgZxjjU6 toeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dmarc-filter :delivered-to; bh=ClHI93pEYv7ngcHs+p8OA8EK8gQeSHvCg2mwKj3fhgc=; b=dHMW/FGsGIrJa1tm8qnHJ4F+yO6N9SDUvJRBWA39qb5/ctkN2t84m3d/d7x9B7+qJB PCkRnNwEODach9OylIDsVEa+/GCIEv77CElSWpzL0VP43iewsrhBLbJ3pO/9QF49j0kY URnjm+ofP5lfZr23RgZU49KzfS2C4GsWZ5bYBESqI5lNRmvV/08u5woOazdrH+Ttus6u y1Aq0+l8kFgVGPc64bDd2rZi2ixh+BbhPPorzeQclsR9vrU+1XT5gj9anBxxmkkSGAxs KH3izRAHZo2OWwGIRbP81NCDSctDvV4kUY6iMt9m4EdA8DhbAreJj34wkhlu+UY/ljuq wdHg== ARC-Authentication-Results: i=1; mx.google.com; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dz19-20020a0564021d5300b00461c43d1113si16277393edb.569.2022.11.01.22.04.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 22:04:49 -0700 (PDT) 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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E91D38582BA for ; Wed, 2 Nov 2022 05:04:46 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from NelsondeMBP.localdomain (114-25-98-25.dynamic-ip.hinet.net [114.25.98.25]) by sourceware.org (Postfix) with ESMTP id 356803858C00 for ; Wed, 2 Nov 2022 05:04:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 356803858C00 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=NelsondeMBP.localdomain Received: by NelsondeMBP.localdomain (Postfix, from userid 501) id 078733BD6B4; Wed, 2 Nov 2022 13:04:33 +0800 (CST) From: Nelson Chu To: binutils@sourceware.org Subject: [committed] RISC-V: Fixed the missing $x+arch when adding odd paddings for alignment. Date: Wed, 2 Nov 2022 13:04:30 +0800 Message-Id: <20221102050430.1053-1-nelson@rivosinc.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) MIME-Version: 1.0 X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, PDS_RDNS_DYNAMIC_FP, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_PBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, TXREP, T_FILL_THIS_FORM_SHORT 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.29 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 Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748359436020207568?= X-GMAIL-MSGID: =?utf-8?q?1748359436020207568?= Consider the case, .option arch, rv32i .option norelax .option arch, +c .byte 1 .align 2 addi a0, zero, 1 Assembler adds $d for the odd .byte, and then adds $x+arch for the alignment. Since norelax, riscv_add_odd_padding_symbol will add the $d and $x for the odd alignment, but accidently remove the $x+arch because it has the same address as $d. Therefore, we will get the unexpected result before applying this patch, .byte 1 # $d .align 2 # odd alignment, $xrv32ic replaced by $d + $x After this patch, the expected result should be, .byte 1 # $d .align 2 # odd alignment, $xrv32ic replaced by $d + $xrv32ic gas/ * config/tc-riscv.c (make_mapping_symbol): If we are adding mapping symbol for odd alignment, then we probably will remove the $x+arch by accidently when it has the same address of $d. Try to add the removed $x+arch back after the $d rather than just $x. (riscv_mapping_state): Updated since parameters of make_mapping_symbol are changed. (riscv_add_odd_padding_symbol): Likewise. (riscv_remove_mapping_symbol): Removed and moved the code into the riscv_check_mapping_symbols. (riscv_check_mapping_symbols): Updated. * testsuite/gas/riscv/mapping-dis.d: Updated and added new testcase. * testsuite/gas/riscv/mapping-symbols.d: Likewise. * testsuite/gas/riscv/mapping.s: Likewise. --- gas/config/tc-riscv.c | 65 +++++++++++++---------- gas/testsuite/gas/riscv/mapping-dis.d | 13 ++++- gas/testsuite/gas/riscv/mapping-symbols.d | 16 +++--- gas/testsuite/gas/riscv/mapping.s | 11 +++- 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c index b664116af6d..2dc92ecd3c3 100644 --- a/gas/config/tc-riscv.c +++ b/gas/config/tc-riscv.c @@ -476,7 +476,8 @@ static void make_mapping_symbol (enum riscv_seg_mstate state, valueT value, fragS *frag, - bool reset_seg_arch_str) + const char *arch_str, + bool odd_data_padding) { const char *name; char *buff = NULL; @@ -486,12 +487,11 @@ make_mapping_symbol (enum riscv_seg_mstate state, name = "$d"; break; case MAP_INSN: - if (reset_seg_arch_str) + if (arch_str != NULL) { - const char *isa = riscv_rps_as.subset_list->arch_str; - size_t size = strlen (isa) + 3; /* "rv" + '\0' */ + size_t size = strlen (arch_str) + 3; /* "$x" + '\0' */ buff = xmalloc (size); - snprintf (buff, size, "$x%s", isa); + snprintf (buff, size, "$x%s", arch_str); name = buff; } else @@ -503,7 +503,7 @@ make_mapping_symbol (enum riscv_seg_mstate state, symbolS *symbol = symbol_new (name, now_seg, frag, value); symbol_get_bfdsym (symbol)->flags |= (BSF_NO_FLAGS | BSF_LOCAL); - if (reset_seg_arch_str) + if (arch_str != NULL) { /* Store current $x+arch into tc_segment_info. */ seg_info (now_seg)->tc_segment_info_data.arch_map_symbol = symbol; @@ -517,6 +517,7 @@ make_mapping_symbol (enum riscv_seg_mstate state, and .text.zero.fill.last. */ symbolS *first = frag->tc_frag_data.first_map_symbol; symbolS *last = frag->tc_frag_data.last_map_symbol; + symbolS *removed = NULL; if (value == 0) { if (first != NULL) @@ -524,7 +525,7 @@ make_mapping_symbol (enum riscv_seg_mstate state, know (S_GET_VALUE (first) == S_GET_VALUE (symbol) && first == last); /* Remove the old one. */ - symbol_remove (first, &symbol_rootP, &symbol_lastP); + removed = first; } frag->tc_frag_data.first_map_symbol = symbol; } @@ -534,9 +535,23 @@ make_mapping_symbol (enum riscv_seg_mstate state, know (S_GET_VALUE (last) <= S_GET_VALUE (symbol)); /* Remove the old one. */ if (S_GET_VALUE (last) == S_GET_VALUE (symbol)) - symbol_remove (last, &symbol_rootP, &symbol_lastP); + removed = last; } frag->tc_frag_data.last_map_symbol = symbol; + + if (removed == NULL) + return; + + if (odd_data_padding) + { + /* If the removed mapping symbol is $x+arch, then add it back to + the next $x. */ + const char *str = strncmp (S_GET_NAME (removed), "$xrv", 4) == 0 + ? S_GET_NAME (removed) + 2 : NULL; + make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, str, + false/* odd_data_padding */); + } + symbol_remove (removed, &symbol_rootP, &symbol_lastP); } /* Set the mapping state for frag_now. */ @@ -580,7 +595,10 @@ riscv_mapping_state (enum riscv_seg_mstate to_state, valueT value = (valueT) (frag_now_fix () - max_chars); seg_info (now_seg)->tc_segment_info_data.map_state = to_state; - make_mapping_symbol (to_state, value, frag_now, reset_seg_arch_str); + const char *arch_str = reset_seg_arch_str + ? riscv_rps_as.subset_list->arch_str : NULL; + make_mapping_symbol (to_state, value, frag_now, arch_str, + false/* odd_data_padding */); } /* Add the odd bytes of paddings for riscv_handle_align. */ @@ -591,25 +609,9 @@ riscv_add_odd_padding_symbol (fragS *frag) /* If there was already a mapping symbol, it should be removed in the make_mapping_symbol. - Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align. */ - make_mapping_symbol (MAP_DATA, frag->fr_fix, frag, false); - make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, false); -} - -/* If previous and current mapping symbol have same value, then remove the - current $x only if the previous is $x+arch; Otherwise, always remove the - previous. */ - -static void -riscv_remove_mapping_symbol (symbolS *pre, symbolS *cur) -{ - know (pre != NULL && cur != NULL - && S_GET_VALUE (pre) == S_GET_VALUE (cur)); - symbolS *removed = pre; - if (strncmp (S_GET_NAME (pre), "$xrv", 4) == 0 - && strcmp (S_GET_NAME (cur), "$x") == 0) - removed = cur; - symbol_remove (removed, &symbol_rootP, &symbol_lastP); + Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */ + make_mapping_symbol (MAP_DATA, frag->fr_fix, frag, + NULL/* arch_str */, true/* odd_data_padding */); } /* Remove any excess mapping symbols generated for alignment frags in @@ -654,7 +656,12 @@ riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, Please see the gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.align.A and .text.zero.fill.align.B. */ - riscv_remove_mapping_symbol (last, next_first); + know (S_GET_VALUE (last) == S_GET_VALUE (next_first)); + symbolS *removed = last; + if (strncmp (S_GET_NAME (last), "$xrv", 4) == 0 + && strcmp (S_GET_NAME (next_first), "$x") == 0) + removed = next_first; + symbol_remove (removed, &symbol_rootP, &symbol_lastP); break; } diff --git a/gas/testsuite/gas/riscv/mapping-dis.d b/gas/testsuite/gas/riscv/mapping-dis.d index 246f3672ade..b1a26fbd151 100644 --- a/gas/testsuite/gas/riscv/mapping-dis.d +++ b/gas/testsuite/gas/riscv/mapping-dis.d @@ -26,9 +26,9 @@ Disassembly of section .text.data: [ ]+[0-9a-f]+:[ ]+4509[ ]+li[ ]+a0,2 [ ]+[0-9a-f]+:[ ]+05000302[ ]+.word[ ]+0x05000302 -Disassembly of section .text.odd.align: +Disassembly of section .text.odd.align.start.insn: -0+000 <.text.odd.align>: +0+000 <.text.odd.align.start.insn>: [ ]+[0-9a-f]+:[ ]+4505[ ]+li[ ]+a0,1 [ ]+[0-9a-f]+:[ ]+01[ ]+.byte[ ]+0x01 [ ]+[0-9a-f]+:[ ]+00[ ]+.byte[ ]+0x00 @@ -36,6 +36,15 @@ Disassembly of section .text.odd.align: [ ]+[0-9a-f]+:[ ]+00200513[ ]+li[ ]+a0,2 [ ]+[0-9a-f]+:[ ]+00000013[ ]+nop +Disassembly of section .text.odd.align.start.data: + +0+000 <.text.odd.align.start.data>: +[ ]+[0-9a-f]+:[ ]+01[ ]+.byte[ ]+0x01 +[ ]+[0-9a-f]+:[ ]+00[ ]+.byte[ ]+0x00 +[ ]+[0-9a-f]+:[ ]+0001[ ]+nop +[ ]+[0-9a-f]+:[ ]+4505[ ]+li[ ]+a0,1 +[ ]+[0-9a-f]+:[ ]+0001[ ]+nop + Disassembly of section .text.zero.fill.first: 0+000 <.text.zero.fill.first>: diff --git a/gas/testsuite/gas/riscv/mapping-symbols.d b/gas/testsuite/gas/riscv/mapping-symbols.d index db9a8dd8030..40df3409736 100644 --- a/gas/testsuite/gas/riscv/mapping-symbols.d +++ b/gas/testsuite/gas/riscv/mapping-symbols.d @@ -17,10 +17,12 @@ SYMBOL TABLE: 0+00 l .text.data 0+00 \$d 0+08 l .text.data 0+00 \$xrv32i2p1_c2p0 0+0c l .text.data 0+00 \$d -0+00 l d .text.odd.align 0+00 .text.odd.align -0+00 l .text.odd.align 0+00 \$xrv32i2p1_c2p0 -0+02 l .text.odd.align 0+00 \$d -0+08 l .text.odd.align 0+00 \$xrv32i2p1 +0+00 l d .text.odd.align.start.insn 0+00 .text.odd.align.start.insn +0+00 l .text.odd.align.start.insn 0+00 \$xrv32i2p1_c2p0 +0+02 l .text.odd.align.start.insn 0+00 \$d +0+08 l .text.odd.align.start.insn 0+00 \$xrv32i2p1 +0+00 l d .text.odd.align.start.data 0+00 .text.odd.align.start.data +0+00 l .text.odd.align.start.data 0+00 \$d 0+00 l d .text.zero.fill.first 0+00 .text.zero.fill.first 0+00 l .text.zero.fill.first 0+00 \$xrv32i2p1_c2p0 0+00 l d .text.zero.fill.last 0+00 .text.zero.fill.last @@ -41,8 +43,10 @@ SYMBOL TABLE: 0+00 l .text.relax.align 0+00 \$xrv32i2p1_c2p0 0+08 l .text.relax.align 0+00 \$xrv32i2p1 0+0a l .text.section.padding 0+00 \$x -0+03 l .text.odd.align 0+00 \$d -0+04 l .text.odd.align 0+00 \$x +0+03 l .text.odd.align.start.insn 0+00 \$d +0+04 l .text.odd.align.start.insn 0+00 \$x +0+01 l .text.odd.align.start.data 0+00 \$d +0+02 l .text.odd.align.start.data 0+00 \$xrv32i2p1_c2p0 0+00 l d .riscv.attributes 0+00 .riscv.attributes 0+00 g .text.cross.section.A 0+00 funcA 0+00 g .text.corss.section.B 0+00 funcB diff --git a/gas/testsuite/gas/riscv/mapping.s b/gas/testsuite/gas/riscv/mapping.s index a0e6c744107..3014a69e792 100644 --- a/gas/testsuite/gas/riscv/mapping.s +++ b/gas/testsuite/gas/riscv/mapping.s @@ -32,7 +32,7 @@ addi a0, zero, 2 # $x, but same as previous addi, so removed .byte 5 .option pop -.section .text.odd.align, "ax" +.section .text.odd.align.start.insn, "ax" .option push .option norelax .option arch, +c @@ -43,6 +43,15 @@ addi a0, zero, 1 # $xrv32ic addi a0, zero, 2 # $xrv32i .option pop +.section .text.odd.align.start.data, "ax" +.option push +.option norelax +.option arch, +c +.byte 1 # $d +.align 2 # odd alignment, $xrv32ic replaced by $d + $xrv32ic +addi a0, zero, 1 +.option pop + .section .text.zero.fill.first, "ax" .option push .option norelax