From patchwork Wed Aug 8 12:39:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 143622 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp796804ljj; Wed, 8 Aug 2018 06:01:42 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwYWdyQ9znqxogOv2MvrQnEGh2HVmFOKNO3CP+doUyW7uJFTQburvO7plU7k5SAsPFCmigu X-Received: by 2002:a0c:8993:: with SMTP id 19-v6mr2379235qvr.56.1533733301914; Wed, 08 Aug 2018 06:01:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533733301; cv=none; d=google.com; s=arc-20160816; b=X+8zifGhaHzp9r//m9pHlw3Yv96Djr2DNVKW8vnNRuLpwmdgiiVJ0R7HvHaSBF6c8p g7B3fQ8Y4tzoMoPe5kHCpQnBHeI57ByZgYXPYpRjzVn1gXZM5DsfAfQupui8M7D0VCCm zukEXHCQh8ANWE9eO8gbFEqWJsfSz/C7DfS5z51Qk7LbVKYb08FJqSVFGWIN9Lxqpom7 EkP6Db7zc86GW49rePSHw/GIMCy/SBYF6vB7hvRZfhnho7SXlyUrxnzQLezhnF8auTCZ YHwthzowD93SE9jj9FLNMrMilZkgeYb2eSvrHm0beMJJ6ea9ViTd84qC6++uJvtfOOnp hGYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=wFgRh7TrCYNNSQSqotvwC6XFsN3uzWP5JoyFlB3dohg=; b=sPpu24gga0DocKJthmjG6e0Tc658qeToBUtuO9Q/RHY2PeSqfj9VhBLhNW5LtPX8eH Bve3elolWrtd23xCHLP8kMKkscMAKwPVCxn6yjJKDqNuDhqtxueBwP45F2oocxVNsIq/ msU+ikFk817dSRhyTbYsyCDaHmmd+OyMZjY8YlN9Ei4Ntl8DfT9XgeZzuwb0RjxedEqM UkEJXUeMxwOKiLTxxX+JmQ1Sw4Brjb9aPdxiPr4DGjTkAG6SRBknd1yfyzBgxDxKzDEX kUY28+W1HKY1GLhqL4hgP0m/Lr+/nCgrzyN1dL2tivuGyy1Umi886Kkh64NEXOW/g8uD W6eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=idKG4YKO; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id v79-v6si4141031qkv.35.2018.08.08.06.01.41 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 08 Aug 2018 06:01:41 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=idKG4YKO; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:43584 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fnO5t-0006sV-B9 for patch@linaro.org; Wed, 08 Aug 2018 09:01:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35432) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fnNka-0004WQ-Se for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:39:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fnNkZ-0006wF-H5 for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:39:40 -0400 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]:51102) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fnNkZ-0006tg-8E for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:39:39 -0400 Received: by mail-wm0-x236.google.com with SMTP id s12-v6so2598363wmc.0 for ; Wed, 08 Aug 2018 05:39:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wFgRh7TrCYNNSQSqotvwC6XFsN3uzWP5JoyFlB3dohg=; b=idKG4YKOJnN7TRAvZcN3BPccXTPM/LnleJoUJV9O3gD5a8NKQ60mKjLNzJAcX4C63u qF5lPlQ2xFWGZQzyzCAv0KpubcqFFhcRuD40qBHOIE7stTQZB9FETzsxCMtcu19TJjj0 otpxN2WfMQpIKGKLTk+sgJMupOq2o/I4q5i0A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wFgRh7TrCYNNSQSqotvwC6XFsN3uzWP5JoyFlB3dohg=; b=NB2ZwSfeUgcMZLe379nXdjtKKL2yFByW/0ScNIzzuu1aR+KhCIGRdUmaPNYXJYTwkL Fdcw6YIzcQmtlRcoz2CPEKYS9m3U5U1QsaXMLf2u5XzCTARssI63vg0+n4fglbNu9bBv Fc/7sU9+FRcIBj/rP0nsuzkaK8AQBqgUcFyS9PeUloRaryul5787BlaGo4CqADlaTkaf fklAV8H/pDF/TwvzLTPznvuFW/DbG+SD0YDzYfD5p17lQxhNtkmXU7XydS/KPK1wyBvP doX5xfkpKybMFQg0Jht/5RhLp6Ng0xPyecuWdnJZs1yqCzC5puA4RqHkyI1w3j4hCHQb s1AA== X-Gm-Message-State: AOUpUlHJXp4S/K2/hc6raugZbTo4IRiKqyZfvONtUqxOlcamCt4QraIM Bh0EI7fH3aQdZU7s5yS8lE/1eQ== X-Received: by 2002:a1c:a6cc:: with SMTP id p195-v6mr1886889wme.76.1533731978067; Wed, 08 Aug 2018 05:39:38 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id l4-v6sm3302266wrv.23.2018.08.08.05.39.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 08 Aug 2018 05:39:35 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id A8C713E062B; Wed, 8 Aug 2018 13:39:34 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Wed, 8 Aug 2018 13:39:34 +0100 Message-Id: <20180808123934.17450-5-alex.bennee@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180808123934.17450-1-alex.bennee@linaro.org> References: <20180808123934.17450-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::236 Subject: [Qemu-devel] [RFC PATCH 4/4] disas: allow capstone to defer to a fallback function on failure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-arm@nongnu.org, richard.henderson@linaro.org, Peter Maydell Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" We can abuse the CS_OPT_SKIPDATA by providing a call back when capstone can't disassemble something. The passing of the string to the dump function is a little clunky but works. Signed-off-by: Alex Bennée --- disas.c | 30 +++++++++++++++++++++++++++++- include/disas/bfd.h | 11 ++++++++++- target/arm/cpu.c | 4 ++++ 3 files changed, 43 insertions(+), 2 deletions(-) -- 2.17.1 diff --git a/disas.c b/disas.c index 5325b7e6be..dfd2c251c5 100644 --- a/disas.c +++ b/disas.c @@ -178,6 +178,20 @@ static int print_insn_od_target(bfd_vma pc, disassemble_info *info) to share this across calls and across host vs target disassembly. */ static __thread cs_insn *cap_insn; + +/* Handle fall-back dissasembly. We don't print here but we do set + * cap_fallback_str for cap_dump_insn to used*/ +static size_t cap_disas_fallback(const uint8_t *code, size_t code_size, + size_t offset, void *user_data) +{ + disassemble_info *info = (disassemble_info *) user_data; + info->cap_fallback_str = g_malloc0(256); + size_t skip = info->capstone_fallback_func(code + offset, + info->cap_fallback_str, 256); + return skip; +} + + /* Initialize the Capstone library. */ /* ??? It would be nice to cache this. We would need one handle for the host and one for the target. For most targets we can reset specific @@ -206,6 +220,14 @@ static cs_err cap_disas_start(disassemble_info *info, csh *handle) cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); } + if (info->capstone_fallback_func) { + cs_opt_skipdata skipdata = { + .callback = cap_disas_fallback, + .user_data = info, + }; + cs_option(*handle, CS_OPT_SKIPDATA_SETUP, (size_t) &skipdata); + } + /* "Disassemble" unknown insns as ".byte W,X,Y,Z". */ cs_option(*handle, CS_OPT_SKIPDATA, CS_OPT_ON); @@ -281,7 +303,13 @@ static void cap_dump_insn(disassemble_info *info, cs_insn *insn) } /* Print the actual instruction. */ - print(info->stream, " %-8s %s\n", insn->mnemonic, insn->op_str); + if (info->cap_fallback_str) { + print(info->stream, " %s\n", info->cap_fallback_str); + g_free(info->cap_fallback_str); + info->cap_fallback_str = NULL; + } else { + print(info->stream, " %-8s %s\n", insn->mnemonic, insn->op_str); + } /* Dump any remaining part of the insn on subsequent lines. */ for (i = split; i < n; i += split) { diff --git a/include/disas/bfd.h b/include/disas/bfd.h index 1f69a6e9d3..9d99bfef48 100644 --- a/include/disas/bfd.h +++ b/include/disas/bfd.h @@ -377,6 +377,12 @@ typedef struct disassemble_info { int cap_insn_unit; int cap_insn_split; + /* Fallback function to disassemble things capstone can't. */ + size_t (*capstone_fallback_func) + (const uint8_t *insn, char *ptr, size_t n); + + char *cap_fallback_str; + } disassemble_info; @@ -491,7 +497,10 @@ int generic_symbol_at_address(bfd_vma, struct disassemble_info *); (INFO).bytes_per_chunk = 0, \ (INFO).display_endian = BFD_ENDIAN_UNKNOWN, \ (INFO).disassembler_options = NULL, \ - (INFO).insn_info_valid = 0 + (INFO).insn_info_valid = 0, \ + (INFO).capstone_fallback_func = NULL, \ + (INFO).cap_fallback_str = NULL + #ifndef ATTRIBUTE_UNUSED #define ATTRIBUTE_UNUSED __attribute__((unused)) diff --git a/target/arm/cpu.c b/target/arm/cpu.c index 64a8005a4b..cfefbfb0b9 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -519,6 +519,10 @@ static void arm_disas_set_info(CPUState *cpu, disassemble_info *info) info->cap_arch = CS_ARCH_ARM64; info->cap_insn_unit = 4; info->cap_insn_split = 4; + +#if defined(TARGET_AARCH64) + info->capstone_fallback_func = do_aarch64_fallback_disassembly; +#endif } else { int cap_mode; if (env->thumb) {