Re: [RFC 04/13] objtool: Print symbol during disassembly
From: Josh Poimboeuf
Date: Tue Jun 10 2025 - 17:55:53 EST
On Fri, Jun 06, 2025 at 05:34:31PM +0200, Alexandre Chartre wrote:
> +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 symbol *call_dest, *sym;
> + struct instruction *jump_dest;
> + struct section *sec;
> + struct reloc *reloc;
> + bool is_reloc;
> + s64 offset;
> +
> + /*
> + * 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->offset == addr) {
> + DINFO_FPRINTF(dinfo, "%lx <%s+0x%lx>", addr,
> + jump_dest->sym->name,
> + jump_dest->offset - jump_dest->sym->offset);
> + return;
> + }
IIRC, there may be a few cases where an instruction's 'sym' field can be
NULL, might want to check for !jump_dest->sym here.
> + /*
> + * 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 (reloc_type(reloc) == R_X86_64_PC32 ||
> + reloc_type(reloc) == R_X86_64_PLT32)
Can use arch_pc_relative_reloc() here.
> + offset = arch_dest_reloc_offset(reloc_addend(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.
> + */
This can be checked with reloc->sym->type == STT_SECTION.
> + sec = find_section_by_name(file->elf, reloc->sym->name);
> + if (sec) {
> + sym = find_symbol_containing(sec, offset);
> + if (sym) {
> + if (sym->offset == offset)
> + DINFO_FPRINTF(dinfo, "%s+0x%lx = %s",
> + reloc->sym->name, offset, sym->name);
> + else
> + DINFO_FPRINTF(dinfo, "%s+0x%lx = %s+0x%lx",
> + reloc->sym->name, offset,
> + sym->name, offset - sym->offset);
> + return;
> + }
> + }
> +
> + if (offset)
> + DINFO_FPRINTF(dinfo, "%s+0x%lx", reloc->sym->name, offset);
> + else
> + DINFO_FPRINTF(dinfo, "%s", reloc->sym->name);
We have offstr() which does similar things. You might be able to get
away with replacing the above hunk with something like:
DINFO_FPRINTF(dinfo, "%s", offstr(reloc->sym->sec, sym->offset + offset));
--
Josh