From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id AE+mMrvFYGeQhh0AWB0awg (envelope-from ) for ; Mon, 16 Dec 2024 19:28:43 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=2VdiKpqJ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id CBED71E097; Mon, 16 Dec 2024 19:28:43 -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, DKIM_SIGNED,DKIM_VALID,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 34BE11E05C for ; Mon, 16 Dec 2024 19:28:43 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BF03F3858C66 for ; Tue, 17 Dec 2024 00:28:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF03F3858C66 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=2VdiKpqJ Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id D74183858D20 for ; Tue, 17 Dec 2024 00:27:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D74183858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D74183858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734395236; cv=none; b=Ttsbn0r4tjY2U4MMg8y4dkV5Lk5vkJt1fv0Kyfbz4qQL+IKydSV5XljTGc2w0WFJWoHwvfgb1CeAMeJtNRe3RwTus5Vi12UgnK+rMR38ckea0fVpapIMnOKoHMnolV6E/JpuILfUMyUKQspK0uVI528eDkNHxDJfWH11oL7u0wc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734395236; c=relaxed/simple; bh=ZMEVzZNP4SIzDWp+UsVL3sDhwnhCIyj40pAY8djwHRg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=sEmWJGPVO/d2xSLyTdqN8PnTDSJ2Enel0/TUWQ2Ph4LNbBift2+t7OaubZrKdHFiAMcaL41lyKJ30UkTGkj9pvoi87qOyzrnNQPdQhQFlCMVTblygQtU7XQ2dLtvep7ej8/Bw94jHYtxWspTUoQACOYwbF7RyrnFfAjiLKlTd5w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D74183858D20 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-216281bc30fso49459675ad.0 for ; Mon, 16 Dec 2024 16:27:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1734395235; x=1735000035; darn=sourceware.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=w+bnmmx0vtksWAGLIFySen7wvhbSBGsbjh5HhfBiB5A=; b=2VdiKpqJT7kEN+YiVnghS+R429UchUP2IJU5pfi3OueeRmcCatKtDHke+Vo1WdJUea 1ViG2DXP5qBJ3z4q7DU1fDofnVBgjHxFp30os7BRcILENnokbFLbytwOtN8UFc5+Y8tU rqYiGClkhU8UCVph2tausTEytU+wymlABF+KwI1htuJu0md4ntPMmxVXaf5wBtwRENuO fqBTiFYjPnveUggwc7JN/JwlFPBq0s8mqYWhdI68kS7ScdPNUHqOq2jwSxbDU2evpM7q RiCDNQZpFmlD7L+QSms41saacsB7O8v/x6rhL2Tq8NSUgyq2w+IQGJXpI9itCr8S95Xy sSxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734395235; x=1735000035; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=w+bnmmx0vtksWAGLIFySen7wvhbSBGsbjh5HhfBiB5A=; b=epBdwO7Z5L+3ENEHKf0SNu662HWZcfkn3UzZPwMhA0wcF6JUucKsWw80AmLKzdvwOT SUrtTdzFhz1VijN82JqGtTVqNWmo3xo/1+98EWY1mHdESRZ4ZxLvm6NI8sfgfpNIBx0z Wg3WWIUWeG2TC5ZfxEuurhoijsHRy5lv1PPNPy5YENQJvOAbU1BRDZbOYNQfxii4JZrO 7HNpRQqy7HNZ54I3PVpbcwxkQQCvucyMATm45mirfUV3AQ3RB7HAoSIIuOTCwaVRnHy/ 9+IkZL7WsVnIVIR7rO1C0G2oeGceJkKSR0FLoUqvlTIccCpVr3FaInVHpals7tuiXv26 x03g== X-Forwarded-Encrypted: i=1; AJvYcCXB+32OH9NwByHAEyHJpradPZC6WatXkElNbKHbRGD4/lySMwBC6+9/DtvxdAOc6fiAJAkILGBJMRJ6UQ==@sourceware.org X-Gm-Message-State: AOJu0YymuZfMkX5s09asztwUzVN+0CGdoB7rup+hUzhornM+AH3zk1XU Z+3wQQWrUDqh8GhMSJWDUNQdRTh1auk//DyQr/IYgHqJMke76KzIV8Zx3Ct++2o= X-Gm-Gg: ASbGncsu2Mt166G0xR1P5+2LqavCT1AP/ZqofBkaAFejfxun6tDT15I1iNlcy+Z+Oq6 EMIrZfkPLp6MG/BR1ZSni0gujBHS7uWj0QgwlXcmSdWBCbe0f9bDto0Mnk5TVVlkDcrHarRQiuo u2SiITWOKK4iEbtb7PmVsqZRO5Zaa2KlcMtQAVPNhHjAMLsI1oEuOhOFgElGeJIrjYrhLO4udNX UXml9RjfeaAUFQz9qZW03P+TQh13HCtKMbpfGZt1VN2UVo= X-Google-Smtp-Source: AGHT+IG4f32spGa5cpLcvf14xeY08w2cWF6pG+2DQysEDRai7sLBp0vY0dQuvMQxdePOT48/kB086g== X-Received: by 2002:a17:902:ec85:b0:215:4a4e:9260 with SMTP id d9443c01a7336-21892a0b436mr208694695ad.14.1734395234866; Mon, 16 Dec 2024 16:27:14 -0800 (PST) Received: from ghost ([50.145.13.30]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-218a1e5437asm48481655ad.162.2024.12.16.16.27.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 16:27:14 -0800 (PST) Date: Mon, 16 Dec 2024 16:27:12 -0800 From: Charlie Jenkins To: Jiawei Cc: =?utf-8?B?6YOR5a2d5p6XICjkupHnn4Up?= , Nelson Chu , gdb-patches , Binutils Subject: Re: [PATCH] RISC-V: Fix disassembly of partial instructions Message-ID: References: <20241213-fix_objdump_partial_insn-v1-1-7a4963e655d5@rivosinc.com> <41dfdee2-dfdb-48cb-9a13-efada8154873@iscas.ac.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <41dfdee2-dfdb-48cb-9a13-efada8154873@iscas.ac.cn> 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 On Sat, Dec 14, 2024 at 03:51:10PM +0800, Jiawei wrote: > 在 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:) Thanks! I sent out a new version: https://sourceware.org/pipermail/binutils/2024-December/138139.html - Charlie > > 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 >