From nobody Tue Dec 2 01:48:51 2025 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C27F346E66 for ; Fri, 21 Nov 2025 09:54:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763718871; cv=none; b=lMK5N7E+5NYmPYZbDT37yw9Z38i/ti+eDXMNEzeL1c0M5L61iB2LqsEQjERQ5iERkTCdpcFrOUfaemgkCQedM7tdhPQRa5h1PBAkyWus9IodlDPzqljHNop6qqitHDTYhjMG+t+xOwF6ZjmyqnRJmVtVF12E7GtKUc0rIgN/XVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763718871; c=relaxed/simple; bh=bXqTHg1x2Dr5B4buJd9aKu5euqZBgL7fuAf+ufwlcyI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pgT6Wh//PF5WgSSFPoE3TnSHMR0VPXG7H9yiGRVxXkihtBq3Nu5C7iPV2mA6RqnIAf3AnxbGYug2WqALvfI/UHBWWeaBcCxMPL5VAwF27q8+7gazjj/mPfRZnTPUbtAhmB1SOyQjPeUdFKIR8iHm0wMyDFt5XW0YOtmG+rFy0Sw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=QwhK4A1Y; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="QwhK4A1Y" Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5AL1uJC1030155; Fri, 21 Nov 2025 09:54:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2025-04-25; bh=u/Bhr 4URxntszJbGWLGpAiWG85vJrt5EbSFEopmaJ9Q=; b=QwhK4A1Y+f1RsvNDHCOR+ 47SWF+nYTSJmPWshP3pl4rrSx2ay5xcoYn8TRYoGS5ngzqsEiTai7RQXUeLl8dYy FU7K3NWtnRovm1q/Z+yERGx4Y+YcwNUzTcdttU8t5CfbPG68wFQGZXd9hk58aGWp QhAacbwl8G7GFdvj8MysB8WPe4cKsm8kD80XYuU76XxdlpPL7c7gXn5+uHG2PFl1 iOIauURPtWhxgnsx9GmAz1eG/F0ELbGApD9/FBb0otHsN13EzMhOtbyflgeL0XQz rZWaFY5Dj3SqeylChdfiJ/HYFJ4GSp6SJVgaKvud1TtEA+5Td2ABcltqB4+pZTgg Q== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 4aejbc2new-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Nov 2025 09:54:21 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 5AL9pSQP004274; Fri, 21 Nov 2025 09:54:20 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 4aefyd4uuv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Nov 2025 09:54:20 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 5AL9rgUi021648; Fri, 21 Nov 2025 09:54:20 GMT Received: from laptop-dell-latitude7430.nl.oracle.com (dhcp-10-154-175-150.vpn.oracle.com [10.154.175.150]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 4aefyd4ub2-21; Fri, 21 Nov 2025 09:54:20 +0000 From: Alexandre Chartre To: linux-kernel@vger.kernel.org, mingo@kernel.org, jpoimboe@kernel.org, peterz@infradead.org, david.laight.linux@gmail.com Cc: alexandre.chartre@oracle.com Subject: [PATCH v6 20/30] objtool: Disassemble group alternatives Date: Fri, 21 Nov 2025 10:53:30 +0100 Message-ID: <20251121095340.464045-21-alexandre.chartre@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20251121095340.464045-1-alexandre.chartre@oracle.com> References: <20251121095340.464045-1-alexandre.chartre@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-21_03,2025-11-20_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 mlxscore=0 bulkscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2510240000 definitions=main-2511210075 X-Proofpoint-GUID: MvCeNNJ7tkz9UJNq88TNR27s2P-Iw9Gk X-Authority-Analysis: v=2.4 cv=JZyxbEKV c=1 sm=1 tr=0 ts=692036cd cx=c_pps a=XiAAW1AwiKB2Y8Wsi+sD2Q==:117 a=XiAAW1AwiKB2Y8Wsi+sD2Q==:17 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=yPCof4ZbAAAA:8 a=Jt4dBLFmlIf84eIfz1kA:9 X-Proofpoint-ORIG-GUID: MvCeNNJ7tkz9UJNq88TNR27s2P-Iw9Gk X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTE1MDAzMiBTYWx0ZWRfXypeAG7HbAXaB KejrlwL7bJP5LuZ6+eIia6Kkk9hUJ12XVkg/dGWfq/bVPNkQ960CXS1akE1WOPu2fNhl8+hjIt2 g6nLsQwriTpsSSf8Y593xvyGjKhgk/rJjZ/Gxq59hYIagl36wH/VfcBV4db404rC5zrFiC//IQM 33l7rn2Y4AFKcrY0GLdv/2oN83FKJeILVpT7JWZlcYjxKouEpZMHfCMzY3FnPIeNowORj9zW7Ri GvYNCUIIHq2ocPcZyG9bldbeW0Nr/ZQ3Wp0oYxsDG2B8MsUE+ZuqYUpkhUGO4dCdaJPfXs9CgXm 7dBNRgtJc7YqByNChiO7FcuGJWIFFpYA5S3kcwpuGNuspuFYZxWuC10fBDaKB9uBM8MAqco1BNc 1sfvjaVLPM0xMAny5VTRnI4RHcO24w== Content-Type: text/plain; charset="utf-8" When using the --disas option, disassemble all group alternatives. Jump tables and exception tables (which are handled as alternatives) are not disassembled at the moment. Signed-off-by: Alexandre Chartre --- tools/objtool/disas.c | 166 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 149 insertions(+), 17 deletions(-) diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c index f9b13d56acab7..ae69bef2eb37e 100644 --- a/tools/objtool/disas.c +++ b/tools/objtool/disas.c @@ -47,8 +47,14 @@ struct disas_alt { struct alternative *alt; /* alternative or NULL if default code */ char *name; /* name for this alternative */ int width; /* formatting width */ + char *insn[DISAS_ALT_INSN_MAX]; /* alternative instructions */ }; =20 +#define DALT_DEFAULT(dalt) (!(dalt)->alt) +#define DALT_INSN(dalt) (DALT_DEFAULT(dalt) ? (dalt)->orig_insn : (dalt)-= >alt->insn) +#define DALT_GROUP(dalt) (DALT_INSN(dalt)->alt_group) +#define DALT_ALTID(dalt) ((dalt)->orig_insn->offset) + /* * Wrapper around asprintf() to allocate and format a string. * Return the allocated string or NULL on error. @@ -506,6 +512,21 @@ size_t disas_insn(struct disas_context *dctx, struct i= nstruction *insn) return disasm(insn->offset, &dctx->info); } =20 +static struct instruction *next_insn_same_alt(struct objtool_file *file, + struct alt_group *alt_grp, + struct instruction *insn) +{ + if (alt_grp->last_insn =3D=3D insn || alt_grp->nop =3D=3D insn) + return NULL; + + return next_insn_same_sec(file, insn); +} + +#define alt_for_each_insn(file, alt_grp, insn) \ + for (insn =3D alt_grp->first_insn; \ + insn; \ + insn =3D next_insn_same_alt(file, alt_grp, insn)) + /* * Provide a name for the type of alternatives present at the * specified instruction. @@ -594,23 +615,107 @@ static int disas_alt_init(struct disas_alt *dalt, return 0; } =20 +static int disas_alt_add_insn(struct disas_alt *dalt, int index, char *ins= n_str) +{ + int len; + + if (index >=3D DISAS_ALT_INSN_MAX) { + WARN("Alternative %lx.%s has more instructions than supported", + DALT_ALTID(dalt), dalt->name); + return -1; + } + + len =3D strlen(insn_str); + dalt->insn[index] =3D insn_str; + if (len > dalt->width) + dalt->width =3D len; + + return 0; +} + +/* + * Disassemble an alternative and store instructions in the disas_alt + * structure. Return the number of instructions in the alternative. + */ +static int disas_alt_group(struct disas_context *dctx, struct disas_alt *d= alt) +{ + struct objtool_file *file; + struct instruction *insn; + char *str; + int count; + int err; + + file =3D dctx->file; + count =3D 0; + + alt_for_each_insn(file, DALT_GROUP(dalt), insn) { + + disas_insn(dctx, insn); + str =3D strdup(disas_result(dctx)); + if (!str) + return -1; + + err =3D disas_alt_add_insn(dalt, count, str); + if (err) + break; + count++; + } + + return count; +} + +/* + * Disassemble the default alternative. + */ +static int disas_alt_default(struct disas_context *dctx, struct disas_alt = *dalt) +{ + char *str; + int err; + + if (DALT_GROUP(dalt)) + return disas_alt_group(dctx, dalt); + + /* + * Default alternative with no alt_group: this is the default + * code associated with either a jump table or an exception + * table and no other instruction alternatives. In that case + * the default alternative is made of a single instruction. + */ + disas_insn(dctx, dalt->orig_insn); + str =3D strdup(disas_result(dctx)); + if (!str) + return -1; + err =3D disas_alt_add_insn(dalt, 0, str); + if (err) + return -1; + + return 1; +} + /* * Print all alternatives one above the other. */ static void disas_alt_print_compact(char *alt_name, struct disas_alt *dalt= s, - int alt_count) + int alt_count, int insn_count) { struct instruction *orig_insn; + int i, j; int len; - int i; =20 orig_insn =3D dalts[0].orig_insn; =20 len =3D disas_print(stdout, orig_insn->sec, orig_insn->offset, 0, NULL); printf("%s\n", alt_name); =20 - for (i =3D 0; i < alt_count; i++) + for (i =3D 0; i < alt_count; i++) { printf("%*s=3D %s\n", len, "", dalts[i].name); + for (j =3D 0; j < insn_count; j++) { + if (!dalts[i].insn[j]) + break; + printf("%*s| %s\n", len, "", dalts[i].insn[j]); + } + printf("%*s|\n", len, ""); + } } =20 /* @@ -624,11 +729,15 @@ static void *disas_alt(struct disas_context *dctx, struct instruction *orig_insn) { struct disas_alt dalts[DISAS_ALT_MAX] =3D { 0 }; + struct instruction *last_insn =3D NULL; struct alternative *alt; + struct disas_alt *dalt; + int insn_count =3D 0; int alt_count =3D 0; char *alt_name; + int count; + int i, j; int err; - int i; =20 alt_name =3D strfmt("<%s.%lx>", disas_alt_type_name(orig_insn), orig_insn->offset); @@ -639,7 +748,7 @@ static void *disas_alt(struct disas_context *dctx, } =20 /* - * Initialize the default alternative. + * Initialize and disassemble the default alternative. */ err =3D disas_alt_init(&dalts[0], orig_insn, NULL); if (err) { @@ -647,8 +756,14 @@ static void *disas_alt(struct disas_context *dctx, goto done; } =20 + insn_count =3D disas_alt_default(dctx, &dalts[0]); + if (insn_count < 0) { + WARN("%s: failed to disassemble default alternative", alt_name); + goto done; + } + /* - * Initialize all other alternatives. + * Initialize and disassemble all other alternatives. */ i =3D 1; for (alt =3D orig_insn->alts; alt; alt =3D alt->next) { @@ -656,35 +771,52 @@ static void *disas_alt(struct disas_context *dctx, WARN("%s has more alternatives than supported", alt_name); break; } - err =3D disas_alt_init(&dalts[i], orig_insn, alt); + dalt =3D &dalts[i]; + err =3D disas_alt_init(dalt, orig_insn, alt); if (err) { WARN("%s: failed to disassemble alternative", alt_name); goto done; } =20 + /* + * Only group alternatives are supported at the moment. + */ + switch (dalt->alt->type) { + case ALT_TYPE_INSTRUCTIONS: + count =3D disas_alt_group(dctx, dalt); + break; + default: + count =3D 0; + } + if (count < 0) { + WARN("%s: failed to disassemble alternative %s", + alt_name, dalt->name); + goto done; + } + + insn_count =3D count > insn_count ? count : insn_count; i++; } alt_count =3D i; =20 /* * Print default and non-default alternatives. - * - * At the moment, this just prints an header for each alternative. */ - disas_alt_print_compact(alt_name, dalts, alt_count); + disas_alt_print_compact(alt_name, dalts, alt_count, insn_count); + + last_insn =3D orig_insn->alt_group ? orig_insn->alt_group->last_insn : + orig_insn; =20 done: - for (i =3D 0; i < alt_count; i++) + for (i =3D 0; i < alt_count; i++) { free(dalts[i].name); + for (j =3D 0; j < insn_count; j++) + free(dalts[i].insn[j]); + } =20 free(alt_name); =20 - /* - * Currently we are not disassembling any alternative but just - * printing alternative names. Return NULL to have disas_func() - * resume the disassembly with the default alternative. - */ - return NULL; + return last_insn; } =20 /* --=20 2.43.5