[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250619145659.1377970-6-alexandre.chartre@oracle.com>
Date: Thu, 19 Jun 2025 16:56:47 +0200
From: Alexandre Chartre <alexandre.chartre@...cle.com>
To: linux-kernel@...r.kernel.org, mingo@...nel.org, jpoimboe@...nel.org,
peterz@...radead.org
Cc: alexandre.chartre@...cle.com
Subject: [RFC PATCH v2 05/17] objtool: Print symbol during disassembly
Print symbols referenced during disassembly instead of just printing
raw addresses. Also handle address relocation.
Signed-off-by: Alexandre Chartre <alexandre.chartre@...cle.com>
---
tools/objtool/arch/loongarch/decode.c | 7 +++
tools/objtool/arch/powerpc/decode.c | 7 +++
tools/objtool/arch/x86/decode.c | 14 +++++
tools/objtool/check.c | 9 ---
tools/objtool/disas.c | 89 +++++++++++++++++++++++++++
tools/objtool/include/objtool/arch.h | 2 +
tools/objtool/include/objtool/check.h | 9 +++
tools/objtool/include/objtool/warn.h | 12 ++++
8 files changed, 140 insertions(+), 9 deletions(-)
diff --git a/tools/objtool/arch/loongarch/decode.c b/tools/objtool/arch/loongarch/decode.c
index 5bf383e7e2e5..09c43f008c9a 100644
--- a/tools/objtool/arch/loongarch/decode.c
+++ b/tools/objtool/arch/loongarch/decode.c
@@ -28,6 +28,13 @@ bool arch_pc_relative_reloc(struct reloc *reloc)
return false;
}
+unsigned long arch_pc_relative_offset(struct instruction *insn,
+ struct reloc *reloc)
+{
+ /* no PC relative relocation */
+ return 0;
+}
+
bool arch_callee_saved_reg(unsigned char reg)
{
switch (reg) {
diff --git a/tools/objtool/arch/powerpc/decode.c b/tools/objtool/arch/powerpc/decode.c
index 3c6fced37bcc..9c3f49c45587 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -20,6 +20,13 @@ unsigned long arch_dest_reloc_offset(int addend)
return addend;
}
+unsigned long arch_pc_relative_offset(struct instruction *insn,
+ struct reloc *reloc)
+{
+ /* no PC relative relocation */
+ return 0;
+}
+
bool arch_callee_saved_reg(unsigned char reg)
{
return false;
diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index 3c85506f9414..6a39cc619d63 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -103,6 +103,20 @@ bool arch_pc_relative_reloc(struct reloc *reloc)
return false;
}
+unsigned long arch_pc_relative_offset(struct instruction *insn,
+ struct reloc *reloc)
+{
+ /*
+ * Relocation information for a RIP-relative instruction is
+ * based on the RIP value at the end of the instruction. So
+ * to get the effective relocated address, the reference has
+ * to be adjusted with the number of bytes between the
+ * relocation offset and the end of the instruction.
+ */
+ return reloc_addend(reloc) +
+ insn->offset + insn->len - reloc_offset(reloc);
+}
+
#define ADD_OP(op) \
if (!(op = calloc(1, sizeof(*op)))) \
return -1; \
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 8ba69e0c273b..a4d0a6c62bc0 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -132,15 +132,6 @@ static struct instruction *prev_insn_same_sym(struct objtool_file *file,
for (insn = next_insn_same_sec(file, insn); insn; \
insn = next_insn_same_sec(file, insn))
-static inline struct symbol *insn_call_dest(struct instruction *insn)
-{
- if (insn->type == INSN_JUMP_DYNAMIC ||
- insn->type == INSN_CALL_DYNAMIC)
- return NULL;
-
- return insn->_call_dest;
-}
-
static inline struct reloc *insn_jump_table(struct instruction *insn)
{
if (insn->type == INSN_JUMP_DYNAMIC ||
diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c
index 9fead6281102..91d23f7518e3 100644
--- a/tools/objtool/disas.c
+++ b/tools/objtool/disas.c
@@ -14,10 +14,96 @@
struct disas_context {
struct objtool_file *file;
+ struct instruction *insn;
disassembler_ftype disassembler;
struct disassemble_info info;
};
+#define DINFO_FPRINTF(dinfo, ...) \
+ ((*(dinfo)->fprintf_func)((dinfo)->stream, __VA_ARGS__))
+
+static void disas_print_address(bfd_vma addr, struct disassemble_info *dinfo)
+{
+ struct disas_context *dctx = dinfo->application_data;
+ struct instruction *insn = dctx->insn;
+ struct objtool_file *file = dctx->file;
+ struct instruction *jump_dest;
+ struct symbol *call_dest;
+ unsigned long offset;
+ struct reloc *reloc;
+ bool is_reloc;
+ char symstr[1024];
+ char *str;
+
+ /*
+ * 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 = insn->jump_dest;
+ if (jump_dest && jump_dest->sym && jump_dest->offset == addr) {
+ sprint_name(symstr, jump_dest->sym->name, jump_dest->offset - jump_dest->sym->offset);
+ DINFO_FPRINTF(dinfo, "%lx <%s>", addr, symstr);
+ return;
+ }
+
+ /*
+ * Assume the address is a relocation if it points to the next
+ * instruction.
+ */
+ is_reloc = (addr == insn->offset + insn->len);
+
+ /*
+ * The call destination offset can be the address we are looking
+ * up, or 0 if there is a relocation.
+ */
+ call_dest = insn_call_dest(insn);
+ if (call_dest) {
+ if (call_dest->offset == addr) {
+ DINFO_FPRINTF(dinfo, "%lx <%s>", addr, call_dest->name);
+ return;
+ }
+ if (call_dest->offset == 0 && is_reloc) {
+ DINFO_FPRINTF(dinfo, "%s", call_dest->name);
+ return;
+ }
+ }
+
+ if (!is_reloc) {
+ DINFO_FPRINTF(dinfo, "0x%lx", addr);
+ return;
+ }
+
+ /*
+ * If this is a relocation, check if we have relocation information
+ * for this instruction.
+ */
+ reloc = find_reloc_by_dest_range(file->elf, insn->sec,
+ insn->offset, insn->len);
+ if (!reloc) {
+ DINFO_FPRINTF(dinfo, "0x%lx", addr);
+ return;
+ }
+
+ if (arch_pc_relative_reloc(reloc))
+ offset = arch_pc_relative_offset(insn, reloc);
+ else
+ offset = reloc_addend(reloc);
+
+ /*
+ * If the relocation symbol is a section name (for example ".bss")
+ * then we try to further resolve the name.
+ */
+ if (reloc->sym->type == STT_SECTION) {
+ str = offstr(reloc->sym->sec, reloc->sym->offset + offset);
+ DINFO_FPRINTF(dinfo, "%s", str);
+ free(str);
+ } else {
+ sprint_name(symstr, reloc->sym->name, offset);
+ DINFO_FPRINTF(dinfo, "%s", symstr);
+ }
+}
+
/*
* Initialize disassemble info arch, mach (32 or 64-bit) and options.
*/
@@ -66,6 +152,7 @@ struct disas_context *disas_context_create(struct objtool_file *file)
fprintf_styled);
dinfo->read_memory_func = buffer_read_memory;
+ dinfo->print_address_func = disas_print_address;
dinfo->application_data = dctx;
/*
@@ -118,6 +205,8 @@ static size_t disas_insn(struct disas_context *dctx,
disassembler_ftype disasm = dctx->disassembler;
struct disassemble_info *dinfo = &dctx->info;
+ dctx->insn = insn;
+
/*
* Set the disassembler buffer to read data from the section
* containing the instruction to disassemble.
diff --git a/tools/objtool/include/objtool/arch.h b/tools/objtool/include/objtool/arch.h
index 19b1dec2db15..baa6eee1977f 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -97,6 +97,8 @@ bool arch_is_embedded_insn(struct symbol *sym);
int arch_rewrite_retpolines(struct objtool_file *file);
bool arch_pc_relative_reloc(struct reloc *reloc);
+unsigned long arch_pc_relative_offset(struct instruction *insn,
+ struct reloc *reloc);
unsigned int arch_reloc_size(struct reloc *reloc);
unsigned long arch_jump_table_sym_offset(struct reloc *reloc, struct reloc *table);
diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h
index 5290ac1ebbc1..4adbcd760c6f 100644
--- a/tools/objtool/include/objtool/check.h
+++ b/tools/objtool/include/objtool/check.h
@@ -115,6 +115,15 @@ static inline bool is_jump(struct instruction *insn)
return is_static_jump(insn) || is_dynamic_jump(insn);
}
+static inline struct symbol *insn_call_dest(struct instruction *insn)
+{
+ if (insn->type == INSN_JUMP_DYNAMIC ||
+ insn->type == INSN_CALL_DYNAMIC)
+ return NULL;
+
+ return insn->_call_dest;
+}
+
struct instruction *find_insn(struct objtool_file *file,
struct section *sec, unsigned long offset);
diff --git a/tools/objtool/include/objtool/warn.h b/tools/objtool/include/objtool/warn.h
index cb8fe846d9dd..125093d568be 100644
--- a/tools/objtool/include/objtool/warn.h
+++ b/tools/objtool/include/objtool/warn.h
@@ -17,6 +17,18 @@
extern const char *objname;
+static inline int sprint_name(char *str, const char *name, unsigned long offset)
+{
+ int len;
+
+ if (offset)
+ len = sprintf(str, "%s+0x%lx", name, offset);
+ else
+ len = sprintf(str, "%s", name);
+
+ return len;
+}
+
static inline char *offstr(struct section *sec, unsigned long offset)
{
bool is_text = (sec->sh.sh_flags & SHF_EXECINSTR);
--
2.43.5
Powered by blists - more mailing lists