From nobody Tue Dec 2 01:48:54 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 D0C5A343200 for ; Fri, 21 Nov 2025 09:54:07 +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=1763718849; cv=none; b=bvHXEC13wZcsNSJLzSaWXv1mWf6vyFvRKRP5FOZQ2uyLsnAh7XR0LL88PPHhggDEQOtP6ziNK5vO3/8F5EGOzRmTzLZ1aSYzVaJhcuj2DW3MW3EM2n/uZo5f/czWp+P7cZ+7nTr5zdJgvxFguSwdFKp/hSb/GORGzL5cPcGlglA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763718849; c=relaxed/simple; bh=a/tZqH6yoCp0oI+4zMdzIYRX9dEOb7LapX8eE/EOS5A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mGhouyX/fO4/SAc/5rVdjEjZ7F1tBUt+KpOOxKjOX3oONM7inQ/CUPEeA6el3gqq+1dEJicRj0tOodgdRmQFSa7DKUUuIjJHYiFYbP2ykSmRkx5HBrqYGHakrbzkzfHicI6JUj3SwoqoyevFun6fL00+aPbkuZFSwHDe9vQATsA= 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=AgPmCcTy; 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="AgPmCcTy" Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5AL1uhhb024577; Fri, 21 Nov 2025 09:53:53 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=acGRP E+Gaanopu1vPCh+NsD0MvL71AXjorO3QXC/qQ0=; b=AgPmCcTyBDAXwqAL9VkiW 0a3iTYuFLp1ufA8kDkmObRF7KfJxgHGCHXdWeumxdQGSRnG4d4yOyJadX1l871iW 31LBsZWgLwcLZK2rWVRErmGwjAQ45oqtLKDbLPbUGpOUvM545LYHQ2QjF/LGT4It GDXFroGDAbUyp3mkIYaNwyOwx/fYc3Jjwgf90LvuY4jv86b/AQ7dNrxmKn5oRZs2 ujp6tRo6XerWKoBQlt+cPSvSmTU+KX+Kl3chu4YTWuA04CliuMPJ8glXU69xEISL dt6LPwEkJ7n7vZoN+UWFNTkI/XlMiCYgLwnmvr2H9rwn3v9rfZK5vNnuODmki37g g== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 4aejbbk41s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Nov 2025 09:53:53 +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 5AL8T30Y004363; Fri, 21 Nov 2025 09:53:52 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 4aefyd4uej-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 21 Nov 2025 09:53:52 +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 5AL9rgUE021648; Fri, 21 Nov 2025 09:53:52 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-6; Fri, 21 Nov 2025 09:53:51 +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 05/30] objtool: Print symbol during disassembly Date: Fri, 21 Nov 2025 10:53:15 +0100 Message-ID: <20251121095340.464045-6-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-Authority-Analysis: v=2.4 cv=BoqQAIX5 c=1 sm=1 tr=0 ts=692036b1 cx=c_pps a=XiAAW1AwiKB2Y8Wsi+sD2Q==:117 a=XiAAW1AwiKB2Y8Wsi+sD2Q==:17 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=yPCof4ZbAAAA:8 a=kcBrYugwK0_vbWm54lEA:9 X-Proofpoint-ORIG-GUID: sf3PG8QQA2hnpGDQ6JN8jWG7jQY4Roov X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTE1MDAzMiBTYWx0ZWRfX81dDPrERHkaT 44Vx2UCyM3730ajBWqLXvUUrW3NcTOdyjD1s2ah1bzAP3/cWr11ILcn/2talzpW/Lz94egdiNfX ldrdsdyFN6T5C2My202jogSuh+0ymENt33si4uSIZrUvtnZn4TmIquNeSW23z5jSzR3shAzMRyC SsdUGOF8sHhZPZ47W/0MJu+/1G39USnctc3PyA0W3vvL09a7AT1rwEj4wtpfQNm9KC2hIKbL4F/ KKMyT9mz9wzA3KHa58DA4tt66C3rKa3npLToHwsfZpBzMO1wyOGsBKY9Zx27lO5yujBmvS1W6ML /DL2q0RDzUczxYCdWh3bPqJZWgSCRlZNBPiMG+wnAXMI5OFt5Rh6XYw4X3axNMVutbkntwpNMYI SFb8s/myHggz6rEQmBPMbdYxyl4xpw== X-Proofpoint-GUID: sf3PG8QQA2hnpGDQ6JN8jWG7jQY4Roov Content-Type: text/plain; charset="utf-8" Print symbols referenced during disassembly instead of just printing raw addresses. Also handle address relocation. Signed-off-by: Alexandre Chartre --- tools/objtool/check.c | 9 -- tools/objtool/disas.c | 134 ++++++++++++++++++++++++++ tools/objtool/include/objtool/check.h | 9 ++ 3 files changed, 143 insertions(+), 9 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index de156e91ee8b7..8937667f075df 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -134,15 +134,6 @@ static struct instruction *prev_insn_same_sym(struct o= bjtool_file *file, for (insn =3D next_insn_same_sec(file, insn); insn; \ insn =3D next_insn_same_sec(file, insn)) =20 -static inline struct symbol *insn_call_dest(struct instruction *insn) -{ - if (insn->type =3D=3D INSN_JUMP_DYNAMIC || - insn->type =3D=3D INSN_CALL_DYNAMIC) - return NULL; - - return insn->_call_dest; -} - static inline struct reloc *insn_jump_table(struct instruction *insn) { if (insn->type =3D=3D INSN_JUMP_DYNAMIC || diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c index 11ac2ec04afc1..dee10ab86fa29 100644 --- a/tools/objtool/disas.c +++ b/tools/objtool/disas.c @@ -14,13 +14,144 @@ =20 struct disas_context { struct objtool_file *file; + struct instruction *insn; disassembler_ftype disassembler; struct disassemble_info info; }; =20 +static int sprint_name(char *str, const char *name, unsigned long offset) +{ + int len; + + if (offset) + len =3D sprintf(str, "%s+0x%lx", name, offset); + else + len =3D sprintf(str, "%s", name); + + return len; +} + #define DINFO_FPRINTF(dinfo, ...) \ ((*(dinfo)->fprintf_func)((dinfo)->stream, __VA_ARGS__)) =20 +static void disas_print_addr_sym(struct section *sec, struct symbol *sym, + bfd_vma addr, struct disassemble_info *dinfo) +{ + char symstr[1024]; + char *str; + + if (sym) { + sprint_name(symstr, sym->name, addr - sym->offset); + DINFO_FPRINTF(dinfo, "0x%lx <%s>", addr, symstr); + } else { + str =3D offstr(sec, addr); + DINFO_FPRINTF(dinfo, "0x%lx <%s>", addr, str); + free(str); + } +} + +static void disas_print_addr_noreloc(bfd_vma addr, + struct disassemble_info *dinfo) +{ + struct disas_context *dctx =3D dinfo->application_data; + struct instruction *insn =3D dctx->insn; + struct symbol *sym =3D NULL; + + if (insn->sym && addr >=3D insn->sym->offset && + addr < insn->sym->offset + insn->sym->len) { + sym =3D insn->sym; + } + + disas_print_addr_sym(insn->sec, sym, addr, dinfo); +} + +static void disas_print_addr_reloc(bfd_vma addr, struct disassemble_info *= dinfo) +{ + struct disas_context *dctx =3D dinfo->application_data; + struct instruction *insn =3D dctx->insn; + unsigned long offset; + struct reloc *reloc; + char symstr[1024]; + char *str; + + reloc =3D find_reloc_by_dest_range(dctx->file->elf, insn->sec, + insn->offset, insn->len); + if (!reloc) { + /* + * There is no relocation for this instruction although + * the address to resolve points to the next instruction. + * So this is an effective reference to the next IP, for + * example: "lea 0x0(%rip),%rdi". The kernel can reference + * the next IP with _THIS_IP_ macro. + */ + DINFO_FPRINTF(dinfo, "0x%lx <_THIS_IP_>", addr); + return; + } + + offset =3D arch_insn_adjusted_addend(insn, reloc); + + /* + * If the relocation symbol is a section name (for example ".bss") + * then we try to further resolve the name. + */ + if (reloc->sym->type =3D=3D STT_SECTION) { + str =3D offstr(reloc->sym->sec, reloc->sym->offset + offset); + DINFO_FPRINTF(dinfo, "0x%lx <%s>", addr, str); + free(str); + } else { + sprint_name(symstr, reloc->sym->name, offset); + DINFO_FPRINTF(dinfo, "0x%lx <%s>", addr, symstr); + } +} + +/* + * Resolve an address into a "+" string. + */ +static void disas_print_address(bfd_vma addr, struct disassemble_info *din= fo) +{ + struct disas_context *dctx =3D dinfo->application_data; + struct instruction *insn =3D dctx->insn; + struct instruction *jump_dest; + struct symbol *sym; + bool is_reloc; + + /* + * If the instruction is a call/jump and it references a + * destination then this is likely the address we are looking + * up. So check it first. + */ + jump_dest =3D insn->jump_dest; + if (jump_dest && jump_dest->sym && jump_dest->offset =3D=3D addr) { + disas_print_addr_sym(jump_dest->sec, jump_dest->sym, + addr, dinfo); + return; + } + + /* + * If the address points to the next instruction then there is + * probably a relocation. It can be a false positive when the + * current instruction is referencing the address of the next + * instruction. This particular case will be handled in + * disas_print_addr_reloc(). + */ + is_reloc =3D (addr =3D=3D insn->offset + insn->len); + + /* + * The call destination offset can be the address we are looking + * up, or 0 if there is a relocation. + */ + sym =3D insn_call_dest(insn); + if (sym && (sym->offset =3D=3D addr || (sym->offset =3D=3D 0 && is_reloc)= )) { + DINFO_FPRINTF(dinfo, "0x%lx <%s>", addr, sym->name); + return; + } + + if (!is_reloc) + disas_print_addr_noreloc(addr, dinfo); + else + disas_print_addr_reloc(addr, dinfo); +} + /* * Initialize disassemble info arch, mach (32 or 64-bit) and options. */ @@ -69,6 +200,7 @@ struct disas_context *disas_context_create(struct objtoo= l_file *file) fprintf_styled); =20 dinfo->read_memory_func =3D buffer_read_memory; + dinfo->print_address_func =3D disas_print_address; dinfo->application_data =3D dctx; =20 /* @@ -121,6 +253,8 @@ static size_t disas_insn(struct disas_context *dctx, disassembler_ftype disasm =3D dctx->disassembler; struct disassemble_info *dinfo =3D &dctx->info; =20 + dctx->insn =3D insn; + if (insn->type =3D=3D INSN_NOP) { DINFO_FPRINTF(dinfo, "nop%d", insn->len); return insn->len; diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/= objtool/check.h index 674f57466d125..ad9c73504b120 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -117,6 +117,15 @@ static inline bool is_jump(struct instruction *insn) return is_static_jump(insn) || is_dynamic_jump(insn); } =20 +static inline struct symbol *insn_call_dest(struct instruction *insn) +{ + if (insn->type =3D=3D INSN_JUMP_DYNAMIC || + insn->type =3D=3D INSN_CALL_DYNAMIC) + return NULL; + + return insn->_call_dest; +} + struct instruction *find_insn(struct objtool_file *file, struct section *sec, unsigned long offset); =20 --=20 2.43.5