From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ADk/KBs5XWfVphYAWB0awg (envelope-from ) for ; Sat, 14 Dec 2024 02:51:55 -0500 Received: by simark.ca (Postfix, from userid 112) id A28D81E097; Sat, 14 Dec 2024 02:51:55 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable autolearn_force=no version=4.0.0 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 253E71E05C for ; Sat, 14 Dec 2024 02:51:55 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B26CB3858C32 for ; Sat, 14 Dec 2024 07:51:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B26CB3858C32 Received: from cstnet.cn (smtp84.cstnet.cn [159.226.251.84]) by sourceware.org (Postfix) with ESMTPS id 164133858D21; Sat, 14 Dec 2024 07:51:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 164133858D21 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=iscas.ac.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 164133858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.226.251.84 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734162680; cv=none; b=jIW4Uxh691hzZgC6rj6g0AOuexH0zj9WaI2np6wYnM34s5Sl6vQMV6iGlZOBqQwIf1HV3XyB1xjIxAR6ZsObhEDei6MceT9oiXyjSeT/c4VuJ3Ioyoy/l/KZyxLqqIcvYVKmGa2alhAWaJrXJYac18sMUaHDQT3DagSMSsl//D4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734162680; c=relaxed/simple; bh=/C7YUACni3oe2/Ymnvej6aEtobuLF9FHAeQbF5YiuKo=; h=Message-ID:Date:MIME-Version:Subject:To:From; b=awzqHL3OjjksBladwa4nQ7lCq/FZEQ7HRgLIhedpfK8YPyecvmrO4g9H82pU8M0IQpsiXu9JykQlqboIu+2B9i2Mj2EPLeqMN9fUDIHMc8e5q1BnF8jni+z8umeKdsywbD6Z+pYUhA3rjddwDjvrlYNxWnyRKQqnI6W/AvI8LXQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 164133858D21 Received: from [192.168.0.105] (unknown [157.0.80.132]) by APP-05 (Coremail) with SMTP id zQCowAA3n8jwOF1nW0uVAQ--.17502S2; Sat, 14 Dec 2024 15:51:13 +0800 (CST) Message-ID: <41dfdee2-dfdb-48cb-9a13-efada8154873@iscas.ac.cn> Date: Sat, 14 Dec 2024 15:51:10 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] RISC-V: Fix disassembly of partial instructions To: Charlie Jenkins , =?UTF-8?B?6YOR5a2d5p6XICjkupHnn4Up?= , Nelson Chu Cc: gdb-patches , Binutils References: <20241213-fix_objdump_partial_insn-v1-1-7a4963e655d5@rivosinc.com> From: Jiawei In-Reply-To: <20241213-fix_objdump_partial_insn-v1-1-7a4963e655d5@rivosinc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: zQCowAA3n8jwOF1nW0uVAQ--.17502S2 X-Coremail-Antispam: 1UD129KBjvJXoW3Xw1xury8WFW7XF1xCrW5GFg_yoW7Kr4Up3 y3CrZxJr4DJF1xWrnxJr15XFs8ur40kas8KrZ7J3y8ur1fXr4avF4UKry0v3WUCr15Gw1j yFn8ArWIkr1kJwUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkIb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I 8E87Iv6xkF7I0E14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xv F2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r 4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvEwIxGrwCY1x0262kKe7AKxVWUAVWUtwCF 04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r 18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vI r41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr 1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvE x4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU5L18JUUUUU== X-Originating-IP: [157.0.80.132] X-CM-SenderInfo: 5mld4v3l6l2u1dvotugofq/1tbiDAULAGddLEMV3QAAsf X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 在 2024/12/14 4:50, Charlie Jenkins 写道: > As of commit e43d8768d909 ("RISC-V: Fix disassemble fetch fail return > value.") partial instructions are no longer disassembled. While that > commit fixed the behavior of print_insn_riscv() returning the arbitrary > status value upon failure, it caused the behavior of dumping > instructions to change. Allow partial instructions to be disassembled > once again and only return -1 if no part of the instruction was able to > be disassembled. > > Fixes: e43d8768d909 ("RISC-V: Fix disassemble fetch fail return value.") > Signed-off-by: Charlie Jenkins > --- > When testing linux perf, I noticed that this behavior of objdump has > changed. Before this patch and running `perf test` on riscv the > following test fails due to objdump not returning all of the expected > bytes. > > Bytes read differ from those read by objdump > buf1 (dso): > 0x97 0xf7 0x11 0x00 0x93 0x87 0xc7 0x7c 0x22 0x85 0x7c 0xec 0xef 0x50 0x80 0x12 > 0xa6 0x85 0xce 0x86 0x4a 0x86 0x22 0x85 0xef 0x50 0x40 0x40 0xa2 0x84 0x1d 0xc9 > 0x7c 0x58 0x85 0x8b 0x85 0xc3 0x1c 0x40 0xa1 0x8b 0x89 0xcf 0x83 0x27 0x04 0x0c > 0x63 0x51 0xf0 0x04 0x97 0xf7 0x11 0x00 0x93 0x87 0x07 0x45 0xbe 0x86 0x58 0x70 > 0x74 0xec 0x7c 0xf3 0xa2 0x70 0x02 0x74 0x42 0x69 0xa2 0x69 0x26 0x85 0xe2 0x64 > 0x45 0x61 0x82 0x80 0x22 0x85 0xef 0x50 0x50 0x52 0x22 0x85 0xef 0x00 0xb1 0x39 > 0xa2 0x70 0x02 0x74 0x81 0x44 0x42 0x69 0xa2 0x69 0x26 0x85 0xe2 0x64 0x45 0x61 > 0x82 0x80 0x97 0x06 0x12 0x00 0x93 0x86 0xa6 0x8a 0x97 0xf7 0x11 0x00 0x93 0x87 > > buf2 (objdump): > 0x97 0xf7 0x11 0x00 0x93 0x87 0xc7 0x7c 0x22 0x85 0x7c 0xec 0xef 0x50 0x80 0x12 > 0xa6 0x85 0xce 0x86 0x4a 0x86 0x22 0x85 0xef 0x50 0x40 0x40 0xa2 0x84 0x1d 0xc9 > 0x7c 0x58 0x85 0x8b 0x85 0xc3 0x1c 0x40 0xa1 0x8b 0x89 0xcf 0x83 0x27 0x04 0x0c > 0x63 0x51 0xf0 0x04 0x97 0xf7 0x11 0x00 0x93 0x87 0x07 0x45 0xbe 0x86 0x58 0x70 > 0x74 0xec 0x7c 0xf3 0xa2 0x70 0x02 0x74 0x42 0x69 0xa2 0x69 0x26 0x85 0xe2 0x64 > 0x45 0x61 0x82 0x80 0x22 0x85 0xef 0x50 0x50 0x52 0x22 0x85 0xef 0x00 0xb1 0x39 > 0xa2 0x70 0x02 0x74 0x81 0x44 0x42 0x69 0xa2 0x69 0x26 0x85 0xe2 0x64 0x45 0x61 > 0x82 0x80 0x97 0x06 0x12 0x00 0x93 0x86 0xa6 0x8a 0x97 0xf7 0x11 0x00 0xad 0x00 > > ---- end(-1) ---- > 24: Object code reading : FAILED! > > After this patch, this test case no longer fails, as objdump returns the > expected values. > --- > opcodes/riscv-dis.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- > 1 file changed, 45 insertions(+), 5 deletions(-) > > diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c > index 101380f93aafbd528ba0020371f0c43a85f41bd1..b0dc67c3a18caf7437a0a6d6229108299e8514a7 100644 > --- a/opcodes/riscv-dis.c > +++ b/opcodes/riscv-dis.c > @@ -1308,6 +1308,14 @@ riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED, > (*info->fprintf_styled_func) > (info->stream, dis_style_immediate, "0x%04x", (unsigned) data); > break; > + case 3: > + info->bytes_per_line = 7; > + (*info->fprintf_styled_func) > + (info->stream, dis_style_assembler_directive, ".word"); > + (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); > + (*info->fprintf_styled_func) > + (info->stream, dis_style_immediate, "0x%06x", (unsigned) data); > + break; > case 4: > info->bytes_per_line = 8; > (*info->fprintf_styled_func) > @@ -1360,13 +1368,28 @@ riscv_init_disasm_info (struct disassemble_info *info) > return true; > } > > +/* Fetch an instruction. If only a partial instruction is able to be fetched, > + return the number of accessible bytes. */ And a hint there should be two spaces before the end . > + > +static bfd_vma > +fetch_insn (bfd_vma memaddr, bfd_byte *packet, bfd_vma dump_size, struct disassemble_info *info, volatile int *status) > +{ > + do > + { > + *status = (*info->read_memory_func) (memaddr, packet, dump_size, info); > + } > + while(*status != 0 && dump_size-- > 1); > + > + return dump_size; > +} > + > int > print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info) > { > bfd_byte packet[RISCV_MAX_INSN_LEN]; > insn_t insn = 0; > - bfd_vma dump_size; > - int status; > + volatile bfd_vma dump_size, bytes_fetched; > + volatile int status; > enum riscv_seg_mstate mstate; > int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *, > struct disassemble_info *); > @@ -1398,24 +1421,41 @@ print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info) > else > { > /* Get the first 2-bytes to check the lenghth of instruction. */ > - status = (*info->read_memory_func) (memaddr, packet, 2, info); > + bytes_fetched = fetch_insn(memaddr, packet, 2, info, &status); > if (status != 0) > { > (*info->memory_error_func) (status, memaddr, info); > return -1; > } > + else if (bytes_fetched != 2) > + { > + /* Only the first byte was able to be read. Dump the partial instruction. */ Same case at here, besides that LGTM:) Jiawei > + dump_size = bytes_fetched; > + info->bytes_per_chunk = dump_size; > + riscv_disassembler = riscv_disassemble_data; > + goto print; > + } > insn = (insn_t) bfd_getl16 (packet); > dump_size = riscv_insn_length (insn); > riscv_disassembler = riscv_disassemble_insn; > } > > - /* Fetch the instruction to dump. */ > - status = (*info->read_memory_func) (memaddr, packet, dump_size, info); > + bytes_fetched = fetch_insn(memaddr, packet, dump_size, info, &status); > + > if (status != 0) > { > (*info->memory_error_func) (status, memaddr, info); > return -1; > } > + else if (bytes_fetched != dump_size) > + { > + dump_size = bytes_fetched; > + info->bytes_per_chunk = dump_size; > + riscv_disassembler = riscv_disassemble_data; > + } > + > +print: > + > insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false); > > return (*riscv_disassembler) (memaddr, insn, packet, info); > > --- > base-commit: 978324718990b6b371d4eeeba02cfe13a0ebf120 > change-id: 20241121-fix_objdump_partial_insn-94e236f3db38