[llvm-objdump] Symbolize binary addresses for low-noisy asm diff.
authorHongtao Yu <hoy@fb.com>
Mon, 20 Jul 2020 16:45:32 +0000 (09:45 -0700)
committerHongtao Yu <hoy@fb.com>
Mon, 17 Aug 2020 23:55:12 +0000 (16:55 -0700)
commit819b2d9c7901d8e6a1434374701e22e15f9cd640
treed8bfef879ed9dd3f455ee295e96d74e8345703dc
parent19bd4ef157a85aee0f6302165dd3eec9b341524a
[llvm-objdump] Symbolize binary addresses for low-noisy asm diff.

When diffing disassembly dump of two binaries, I see lots of noises from mismatched jump target addresses and global data references, which unnecessarily causes diffs on every function, making it impractical. I'm trying to symbolize the raw binary addresses to minimize the diff noise.
In this change, a local branch target is modeled as a label and the branch target operand will simply be printed as a label. Local labels are collected by a separate pre-decoding pass beforehand. A global data memory operand will be printed as a global symbol instead of the raw data address. Unfortunately, due to the way the disassembler is set up and to be less intrusive, a global symbol is always printed as the last operand of a memory access instruction. This is less than ideal but is probably acceptable from checking code quality point of view since on most targets an instruction can have at most one memory operand.

So far only the X86 disassemblers are supported.

Test Plan:

llvm-objdump -d  --x86-asm-syntax=intel --no-show-raw-insn --no-leading-addr :
```
Disassembly of section .text:

<_start>:
                push rax
                mov dword ptr [rsp + 4], 0
                mov dword ptr [rsp], 0
                mov eax, dword ptr [rsp]
                cmp eax, dword ptr [rip + 4112]  # 202182 <g>
                jge 0x20117e <_start+0x25>
                call 0x201158 <foo>
                inc dword ptr [rsp]
                jmp 0x201169 <_start+0x10>
                xor eax, eax
                pop rcx
                ret
```

llvm-objdump -d  **--symbolize-operands** --x86-asm-syntax=intel --no-show-raw-insn --no-leading-addr :
```
Disassembly of section .text:

<_start>:
                push rax
                mov dword ptr [rsp + 4], 0
                mov dword ptr [rsp], 0
<L1>:
                mov eax, dword ptr [rsp]
                cmp eax, dword ptr  <g>
                jge  <L0>
                call  <foo>
                inc dword ptr [rsp]
                jmp  <L1>
<L0>:
                xor eax, eax
                pop rcx
                ret
```

Note that the jump instructions like `jge 0x20117e <_start+0x25>` without this work is printed as a real target address and an offset from the leading symbol. With a change in the optimizer that adds/deletes an instruction, the address and offset may shift for targets placed after the instruction. This will be a problem when diffing the disassembly from two optimizers where there are unnecessary false positives due to such branch target address changes. With `--symbolize-operand`, a label is printed for a branch target instead to reduce the false positives. Similarly, the disassemble of PC-relative global variable references is also prone to instruction insertion/deletion.

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D84191
llvm/docs/CommandGuide/llvm-objdump.rst
llvm/include/llvm/MC/MCInstPrinter.h
llvm/lib/Target/X86/MCTargetDesc/X86ATTInstPrinter.cpp
llvm/lib/Target/X86/MCTargetDesc/X86InstPrinterCommon.cpp
llvm/lib/Target/X86/MCTargetDesc/X86IntelInstPrinter.cpp
llvm/test/tools/llvm-objdump/X86/elf-disassemble-symbololize-operands.yaml [new file with mode: 0644]
llvm/tools/llvm-objdump/llvm-objdump.cpp