]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
objtool: Print symbol during disassembly
authorAlexandre Chartre <alexandre.chartre@oracle.com>
Fri, 21 Nov 2025 09:53:15 +0000 (10:53 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Fri, 21 Nov 2025 14:30:07 +0000 (15:30 +0100)
Print symbols referenced during disassembly instead of just printing
raw addresses. Also handle address relocation.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Link: https://patch.msgid.link/20251121095340.464045-6-alexandre.chartre@oracle.com
tools/objtool/check.c
tools/objtool/disas.c
tools/objtool/include/objtool/check.h

index 21d45a35f3c975a3f494869465eb9d34b49f4ce1..0999717abc9cb2523bc0f134aa02f3cd83e3f14d 100644 (file)
@@ -134,15 +134,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 ||
index 11ac2ec04afc18ce8ea0cc11415352d484f69112..dee10ab86fa29753474ae7f8927ab2d9f526f6b4 100644 (file)
 
 struct disas_context {
        struct objtool_file *file;
+       struct instruction *insn;
        disassembler_ftype disassembler;
        struct disassemble_info info;
 };
 
+static 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;
+}
+
 #define DINFO_FPRINTF(dinfo, ...)      \
        ((*(dinfo)->fprintf_func)((dinfo)->stream, __VA_ARGS__))
 
+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 = 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 = dinfo->application_data;
+       struct instruction *insn = dctx->insn;
+       struct symbol *sym = NULL;
+
+       if (insn->sym && addr >= insn->sym->offset &&
+           addr < insn->sym->offset + insn->sym->len) {
+               sym = 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 = dinfo->application_data;
+       struct instruction *insn = dctx->insn;
+       unsigned long offset;
+       struct reloc *reloc;
+       char symstr[1024];
+       char *str;
+
+       reloc = 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 = 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 == STT_SECTION) {
+               str = 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 "<symbol>+<offset>" string.
+ */
+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 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 = insn->jump_dest;
+       if (jump_dest && jump_dest->sym && jump_dest->offset == 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 = (addr == insn->offset + insn->len);
+
+       /*
+        * The call destination offset can be the address we are looking
+        * up, or 0 if there is a relocation.
+        */
+       sym = insn_call_dest(insn);
+       if (sym && (sym->offset == addr || (sym->offset == 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 objtool_file *file)
                                     fprintf_styled);
 
        dinfo->read_memory_func = buffer_read_memory;
+       dinfo->print_address_func = disas_print_address;
        dinfo->application_data = dctx;
 
        /*
@@ -121,6 +253,8 @@ static size_t disas_insn(struct disas_context *dctx,
        disassembler_ftype disasm = dctx->disassembler;
        struct disassemble_info *dinfo = &dctx->info;
 
+       dctx->insn = insn;
+
        if (insn->type == INSN_NOP) {
                DINFO_FPRINTF(dinfo, "nop%d", insn->len);
                return insn->len;
index 674f57466d1250a5b2e0a1ade63ee8b882a356e3..ad9c73504b120df51a2cc273a47b64809723756e 100644 (file)
@@ -117,6 +117,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);