From d25e572421a66270c0ee8d51c96256f2958a6f1d Mon Sep 17 00:00:00 2001 From: Igor Kudrin Date: Mon, 28 Jun 2021 14:24:39 +0700 Subject: [PATCH] [llvm-objdump] Print memory operand addresses as regular comments The patch reuses the common code to print memory operand addresses as instruction comments. This helps to align the comments and enables using target-specific comment markers when `evaluateMemoryOperandAddress()` is implemented for them. Differential Revision: https://reviews.llvm.org/D104861 --- lld/test/MachO/dso-handle.s | 8 +++--- lld/test/MachO/dylink-ordinal.s | 4 +-- lld/test/MachO/mattrs.ll | 3 ++- lld/test/MachO/static-link.s | 2 +- lld/test/MachO/tapi-link.s | 2 +- lld/test/MachO/tlv-dylib.s | 6 ++--- lld/test/MachO/tlv.s | 8 +++--- lld/test/MachO/weak-binding.s | 12 ++++----- lld/test/MachO/x86-64-reloc-got-load.s | 2 +- lld/test/MachO/x86-64-reloc-signed.s | 14 ++++++----- .../llvm-objdump/X86/disassemble-align.s | 4 +-- llvm/tools/llvm-objdump/llvm-objdump.cpp | 25 +++++++++++++------ 12 files changed, 51 insertions(+), 39 deletions(-) diff --git a/lld/test/MachO/dso-handle.s b/lld/test/MachO/dso-handle.s index 2104d1dcf75a..260a0495989c 100644 --- a/lld/test/MachO/dso-handle.s +++ b/lld/test/MachO/dso-handle.s @@ -3,13 +3,13 @@ # RUN: %lld -lSystem %t.o -o %t # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s -# CHECK: leaq {{.*}} # 0x100000000 -# CHECK-NEXT: leaq {{.*}} # 0x100000000 +# CHECK: leaq {{.*}} ## 0x100000000 +# CHECK-NEXT: leaq {{.*}} ## 0x100000000 # RUN: %lld -dylib %t.o -o %t.dylib # RUN: llvm-objdump -d --no-show-raw-insn --rebase --section-headers %t.dylib | FileCheck %s --check-prefix=DYLIB-CHECK -# DYLIB-CHECK: leaq {{.*}} # 0x0 -# DYLIB-CHECK-NEXT: leaq {{.*}} # 0x0 +# DYLIB-CHECK: leaq {{.*}} ## 0x0 +# DYLIB-CHECK-NEXT: leaq {{.*}} ## 0x0 # DYLIB-LABEL: Sections: # DYLIB: __data 00000008 [[#%x,DATA:]] DATA diff --git a/lld/test/MachO/dylink-ordinal.s b/lld/test/MachO/dylink-ordinal.s index 9dcdaccf7cb9..7edb218f7321 100644 --- a/lld/test/MachO/dylink-ordinal.s +++ b/lld/test/MachO/dylink-ordinal.s @@ -10,8 +10,8 @@ # CHECK: callq 0x[[#%x,FOO_OFF:]] # CHECK-NEXT: callq 0x[[#%x,BAR_OFF:]] -# CHECK: [[#%x,BAR_OFF]]: jmpq {{.*}} # 0x[[#%x,BAR_BIND:]] -# CHECK: [[#%x,FOO_OFF]]: jmpq {{.*}} # 0x[[#%x,FOO_BIND:]] +# CHECK: [[#%x,BAR_OFF]]: jmpq {{.*}} ## 0x[[#%x,BAR_BIND:]] +# CHECK: [[#%x,FOO_OFF]]: jmpq {{.*}} ## 0x[[#%x,FOO_BIND:]] # CHECK-LABEL: Lazy bind table: # CHECK-DAG: __DATA __la_symbol_ptr 0x[[#%x,FOO_BIND]] Foo _foo diff --git a/lld/test/MachO/mattrs.ll b/lld/test/MachO/mattrs.ll index 25ed6f0cf73a..fad0b5e3bc61 100644 --- a/lld/test/MachO/mattrs.ll +++ b/lld/test/MachO/mattrs.ll @@ -18,7 +18,8 @@ ; NO-FMA: <_foo>: ; NO-FMA-NEXT: vrcpss %xmm0, %xmm0, %xmm1 ; NO-FMA-NEXT: vmulss %xmm1, %xmm0, %xmm0 -; NO-FMA-NEXT: vmovss [[#]](%rip), %xmm2 +; NO-FMA-NEXT: vmovss [[#]](%rip), %xmm2 ## xmm2 = +; NO-FMA-NEXT: ## 0x ; NO-FMA-NEXT: vsubss %xmm0, %xmm2, %xmm0 ; NO-FMA-NEXT: vmulss %xmm0, %xmm1, %xmm0 ; NO-FMA-NEXT: vaddss %xmm0, %xmm1, %xmm0 diff --git a/lld/test/MachO/static-link.s b/lld/test/MachO/static-link.s index b3e197cda80d..c3760f13af95 100644 --- a/lld/test/MachO/static-link.s +++ b/lld/test/MachO/static-link.s @@ -15,7 +15,7 @@ # CHECK: Disassembly of section __TEXT,__text # CHECK-LABEL: <_main>: -# CHECK: leaq {{.*}}(%rip), %rsi # 0x[[ADDR]] <_goodbye_world> +# CHECK: leaq {{.*}}(%rip), %rsi ## 0x[[ADDR]] <_goodbye_world> .section __TEXT,__text .global _main diff --git a/lld/test/MachO/tapi-link.s b/lld/test/MachO/tapi-link.s index 8e073b844f7d..e46871a5b271 100644 --- a/lld/test/MachO/tapi-link.s +++ b/lld/test/MachO/tapi-link.s @@ -14,7 +14,7 @@ # RUN: llvm-objdump --bind --no-show-raw-insn -d -r %t/with-reexport | FileCheck %s # CHECK: Disassembly of section __TEXT,__text: -# CHECK: movq {{.*}} # 0x[[ADDR:[0-9a-f]+]] +# CHECK: movq {{.*}} ## 0x[[ADDR:[0-9a-f]+]] # CHECK: Bind table: # CHECK-DAG: __DATA_CONST __got 0x[[ADDR]] pointer 0 libSystem ___nan diff --git a/lld/test/MachO/tlv-dylib.s b/lld/test/MachO/tlv-dylib.s index 7a083615492f..dc6bdef1433e 100644 --- a/lld/test/MachO/tlv-dylib.s +++ b/lld/test/MachO/tlv-dylib.s @@ -17,9 +17,9 @@ # RUN: %lld -lSystem -L%t -ltlv %t/test.o -o %t/test # RUN: llvm-objdump --bind -d --no-show-raw-insn %t/test | FileCheck %s -# CHECK: movq [[#]](%rip), %rax # 0x[[#%x, FOO:]] -# CHECK-NEXT: movq [[#]](%rip), %rax # 0x[[#%x, BAR:]] -# CHECK-NEXT: movq [[#]](%rip), %rax # 0x[[#%x, BAZ:]] +# CHECK: movq [[#]](%rip), %rax ## 0x[[#%x, FOO:]] +# CHECK-NEXT: movq [[#]](%rip), %rax ## 0x[[#%x, BAR:]] +# CHECK-NEXT: movq [[#]](%rip), %rax ## 0x[[#%x, BAZ:]] # CHECK-LABEL: Bind table: # CHECK-DAG: __DATA __thread_ptrs 0x{{0*}}[[#%x, FOO]] pointer 0 libtlv _foo diff --git a/lld/test/MachO/tlv.s b/lld/test/MachO/tlv.s index a7e032b01a4a..9c74ea082ba7 100644 --- a/lld/test/MachO/tlv.s +++ b/lld/test/MachO/tlv.s @@ -34,13 +34,13 @@ # HEADER: MH_HAS_TLV_DESCRIPTORS # REG: <_main>: -# REG-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_foo> -# REG-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_bar> +# REG-NEXT: leaq {{.*}}(%rip), %rax ## {{.*}} <_foo> +# REG-NEXT: leaq {{.*}}(%rip), %rax ## {{.*}} <_bar> # REG-NEXT: retq # TBSS: <_f>: -# TBSS-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_baz> -# TBSS-NEXT: leaq {{.*}}(%rip), %rax # {{.*}} <_qux> +# TBSS-NEXT: leaq {{.*}}(%rip), %rax ## {{.*}} <_baz> +# TBSS-NEXT: leaq {{.*}}(%rip), %rax ## {{.*}} <_qux> # TBSS-NEXT: retq # REG-TLVP: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 diff --git a/lld/test/MachO/weak-binding.s b/lld/test/MachO/weak-binding.s index 11b7562c69e9..0c79f981137a 100644 --- a/lld/test/MachO/weak-binding.s +++ b/lld/test/MachO/weak-binding.s @@ -19,12 +19,12 @@ # CHECK-NEXT: {{[0-9a-f]+}} {{[0-9a-f ]*[1-9a-f]+[0-9a-f ]*}} # CHECK: <_main>: -# CHECK-NEXT: movq [[#]](%rip), %rax # 0x[[#%X,WEAK_DY_GOT_ADDR:]] -# CHECK-NEXT: movq [[#]](%rip), %rax # 0x[[#%X,WEAK_EXT_GOT_ADDR:]] -# CHECK-NEXT: leaq [[#]](%rip), %rax # 0x[[#%X,WEAK_INT_GOT_ADDR:]] -# CHECK-NEXT: movq [[#]](%rip), %rax # 0x[[#%X,WEAK_TLV_ADDR:]] -# CHECK-NEXT: movq [[#]](%rip), %rax # 0x[[#%X,WEAK_DY_TLV_ADDR:]] -# CHECK-NEXT: leaq [[#]](%rip), %rax # 0x[[#%X,WEAK_INT_TLV_ADDR:]] +# CHECK-NEXT: movq [[#]](%rip), %rax ## 0x[[#%X,WEAK_DY_GOT_ADDR:]] +# CHECK-NEXT: movq [[#]](%rip), %rax ## 0x[[#%X,WEAK_EXT_GOT_ADDR:]] +# CHECK-NEXT: leaq [[#]](%rip), %rax ## 0x[[#%X,WEAK_INT_GOT_ADDR:]] +# CHECK-NEXT: movq [[#]](%rip), %rax ## 0x[[#%X,WEAK_TLV_ADDR:]] +# CHECK-NEXT: movq [[#]](%rip), %rax ## 0x[[#%X,WEAK_DY_TLV_ADDR:]] +# CHECK-NEXT: leaq [[#]](%rip), %rax ## 0x[[#%X,WEAK_INT_TLV_ADDR:]] # CHECK-NEXT: callq 0x{{[0-9a-f]*}} # CHECK-NEXT: callq 0x{{[0-9a-f]*}} # CHECK-NEXT: callq 0x{{[0-9a-f]*}} diff --git a/lld/test/MachO/x86-64-reloc-got-load.s b/lld/test/MachO/x86-64-reloc-got-load.s index 9bc65ecefa4d..b5443126866e 100644 --- a/lld/test/MachO/x86-64-reloc-got-load.s +++ b/lld/test/MachO/x86-64-reloc-got-load.s @@ -6,7 +6,7 @@ # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o # RUN: %lld -o %t %t.o # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s -# CHECK: leaq [[#]](%rip), %rax # {{.*}} <_foo> +# CHECK: leaq [[#]](%rip), %rax ## {{.*}} <_foo> .globl _main, _foo diff --git a/lld/test/MachO/x86-64-reloc-signed.s b/lld/test/MachO/x86-64-reloc-signed.s index fd12b9bff701..00643c838cde 100644 --- a/lld/test/MachO/x86-64-reloc-signed.s +++ b/lld/test/MachO/x86-64-reloc-signed.s @@ -12,18 +12,20 @@ # CHECK-LABEL: Disassembly of section # CHECK: <_main>: -# CHECK-NEXT: movl {{.*}} # 0x[[#S]] +# CHECK-NEXT: movl {{.*}} ## imm = +# CHECK-NEXT: ## 0x[[#S]] # CHECK-NEXT: callq {{.*}} -# CHECK-NEXT: movl {{.*}} # 0x[[#S + 2]] +# CHECK-NEXT: movl {{.*}} ## 0x[[#S + 2]] # CHECK-NEXT: callq {{.*}} -# CHECK-NEXT: movb {{.*}} # 0x[[#S]] +# CHECK-NEXT: movb {{.*}} ## 0x[[#S]] # CHECK-NEXT: callq {{.*}} # CHECK: <__not_text>: -# CHECK-NEXT: movl {{.*}} # 0x[[#FOO + 8]] +# CHECK-NEXT: movl {{.*}} ## imm = +# CHECK-NEXT: ## 0x[[#FOO + 8]] # CHECK-NEXT: callq {{.*}} -# CHECK-NEXT: movl {{.*}} # 0x[[#FOO + 8 + 2]] +# CHECK-NEXT: movl {{.*}} ## 0x[[#FOO + 8 + 2]] # CHECK-NEXT: callq {{.*}} -# CHECK-NEXT: movb {{.*}} # 0x[[#FOO + 8]] +# CHECK-NEXT: movb {{.*}} ## 0x[[#FOO + 8]] # CHECK-NEXT: callq {{.*}} .section __TEXT,__text diff --git a/llvm/test/tools/llvm-objdump/X86/disassemble-align.s b/llvm/test/tools/llvm-objdump/X86/disassemble-align.s index d3ccc59e64ec..68565fe9732e 100644 --- a/llvm/test/tools/llvm-objdump/X86/disassemble-align.s +++ b/llvm/test/tools/llvm-objdump/X86/disassemble-align.s @@ -10,14 +10,14 @@ # Instructions are expected to be aligned if the instruction in hex is not too long. # CHECK: 0: c3 |retq -# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax # 0x12345e <.text+0x12345e> +# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax # 0x12345e <.text+0x12345e> # CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554 # CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 |lwpval|$0x0, 0x40(%rdx,%rax), %r15d # CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 |lwpins|$0x0, 0x1cf01cf0, %r15d # CHECK-NEXT: 2b: ff ff | # NORAW: 0: |retq -# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax # 0x12345e <.text+0x12345e> +# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax # 0x12345e <.text+0x12345e> # NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554 # NORAW-NEXT: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d # NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index c75d0e482dbc..39ce32d892aa 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -1433,6 +1433,8 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, // address (jump target or memory operand address) and print it on the // right of the instruction. if (Disassembled && MIA) { + // Branch targets are printed just after the instructions. + llvm::raw_ostream *TargetOS = &FOS; uint64_t Target; bool PrintTarget = MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target); @@ -1443,8 +1445,11 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, Target = *MaybeTarget; PrintTarget = true; // Do not print real address when symbolizing. - if (!SymbolizeOperands) - FOS << " # 0x" << Twine::utohexstr(Target); + if (!SymbolizeOperands) { + // Memory operand addresses are printed as comments. + TargetOS = &CommentStream; + *TargetOS << "0x" << Twine::utohexstr(Target); + } } if (PrintTarget) { // In a relocatable object, the target's section must reside in @@ -1503,22 +1508,26 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, if (Demangle) TargetName = demangle(TargetName); - FOS << " <"; + *TargetOS << " <"; if (!Disp) { // Always Print the binary symbol precisely corresponding to // the target address. - FOS << TargetName; + *TargetOS << TargetName; } else if (!LabelAvailable) { // Always Print the binary symbol plus an offset if there's no // local label corresponding to the target address. - FOS << TargetName << "+0x" << Twine::utohexstr(Disp); + *TargetOS << TargetName << "+0x" << Twine::utohexstr(Disp); } else { - FOS << AllLabels[Target]; + *TargetOS << AllLabels[Target]; } - FOS << ">"; + *TargetOS << ">"; } else if (LabelAvailable) { - FOS << " <" << AllLabels[Target] << ">"; + *TargetOS << " <" << AllLabels[Target] << ">"; } + // By convention, each record in the comment stream should be + // terminated. + if (TargetOS == &CommentStream) + *TargetOS << "\n"; } } }