From 7dc65bf930b926f47200bd36c1f9bf37f9789368 Mon Sep 17 00:00:00 2001 From: Liad Mordekoviz Date: Tue, 11 Feb 2020 11:10:57 +0000 Subject: [PATCH] [llvm-objdump] Add column headers for relocation printing This allows us better readability and compatibility with what GNU objdump prints. Fixes https://bugs.llvm.org/show_bug.cgi?id=43941 Reviewed by: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D72992 --- .../ARM/dwarf-asm-multiple-sections-dwarf-2.s | 2 + test/MC/ARM/dwarf-asm-multiple-sections.s | 4 ++ test/MC/ARM/dwarf-asm-nonstandard-section.s | 2 + test/MC/ARM/dwarf-asm-single-section.s | 2 + test/MC/COFF/cfi-sections.s | 2 + test/Object/objdump-relocations.test | 2 + .../llvm-objdump/WebAssembly/relocations.test | 12 ++-- test/tools/llvm-objdump/coff-many-relocs.test | 21 +++--- .../llvm-objdump/hex-relocation-addr.test | 25 ++++---- .../relocation-xindex-symbol.test | 3 +- test/tools/llvm-objdump/relocations-elf.test | 64 ++++++++++++++++--- .../llvm-objdump/relocations-in-nonreloc.test | 3 +- tools/llvm-objdump/llvm-objdump.cpp | 10 ++- 13 files changed, 113 insertions(+), 39 deletions(-) diff --git a/test/MC/ARM/dwarf-asm-multiple-sections-dwarf-2.s b/test/MC/ARM/dwarf-asm-multiple-sections-dwarf-2.s index fbb1d689273..7de9d11262b 100644 --- a/test/MC/ARM/dwarf-asm-multiple-sections-dwarf-2.s +++ b/test/MC/ARM/dwarf-asm-multiple-sections-dwarf-2.s @@ -50,6 +50,7 @@ b: // RELOC: RELOCATION RECORDS FOR [.debug_info]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_abbrev // RELOC-NEXT: 0000000c R_ARM_ABS32 .debug_line // RELOC-NEXT: R_ARM_ABS32 .text @@ -60,6 +61,7 @@ b: // RELOC-NOT: RELOCATION RECORDS FOR [.debug_ranges]: // RELOC: RELOCATION RECORDS FOR [.debug_aranges]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_info // RELOC-NEXT: 00000010 R_ARM_ABS32 .text // RELOC-NEXT: 00000018 R_ARM_ABS32 foo diff --git a/test/MC/ARM/dwarf-asm-multiple-sections.s b/test/MC/ARM/dwarf-asm-multiple-sections.s index 906cd0b607d..69a45ed3495 100644 --- a/test/MC/ARM/dwarf-asm-multiple-sections.s +++ b/test/MC/ARM/dwarf-asm-multiple-sections.s @@ -78,9 +78,11 @@ b: // Offsets are different in DWARF v5 due to different header layout. // RELOC: RELOCATION RECORDS FOR [.debug_info]: +// RELOC4-NEXT: OFFSET TYPE VALUE // RELOC4-NEXT: 00000006 R_ARM_ABS32 .debug_abbrev // RELOC4-NEXT: 0000000c R_ARM_ABS32 .debug_line // RELOC4-NEXT: 00000010 R_ARM_ABS32 .debug_ranges +// RELOC5-NEXT: OFFSET TYPE VALUE // RELOC5-NEXT: 00000008 R_ARM_ABS32 .debug_abbrev // RELOC5-NEXT: 0000000d R_ARM_ABS32 .debug_line // RELOC5-NEXT: 00000011 R_ARM_ABS32 .debug_ranges @@ -88,10 +90,12 @@ b: // RELOC-NEXT: R_ARM_ABS32 foo // RELOC: RELOCATION RECORDS FOR [.debug_ranges]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000004 R_ARM_ABS32 .text // RELOC-NEXT: 00000014 R_ARM_ABS32 foo // RELOC: RELOCATION RECORDS FOR [.debug_aranges]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_info // RELOC-NEXT: 00000010 R_ARM_ABS32 .text // RELOC-NEXT: 00000018 R_ARM_ABS32 foo diff --git a/test/MC/ARM/dwarf-asm-nonstandard-section.s b/test/MC/ARM/dwarf-asm-nonstandard-section.s index 38e46b9d1f0..92fa6e544b0 100644 --- a/test/MC/ARM/dwarf-asm-nonstandard-section.s +++ b/test/MC/ARM/dwarf-asm-nonstandard-section.s @@ -43,6 +43,7 @@ b: // RELOC: RELOCATION RECORDS FOR [.debug_info]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_abbrev // RELOC-NEXT: 0000000c R_ARM_ABS32 .debug_line // RELOC-NEXT: R_ARM_ABS32 foo @@ -52,5 +53,6 @@ b: // RELOC-NOT: RELOCATION RECORDS FOR [.debug_ranges]: // RELOC: RELOCATION RECORDS FOR [.debug_aranges]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_info // RELOC-NEXT: 00000010 R_ARM_ABS32 foo diff --git a/test/MC/ARM/dwarf-asm-single-section.s b/test/MC/ARM/dwarf-asm-single-section.s index 709818d477e..d376e894849 100644 --- a/test/MC/ARM/dwarf-asm-single-section.s +++ b/test/MC/ARM/dwarf-asm-single-section.s @@ -42,6 +42,7 @@ a: // RELOC: RELOCATION RECORDS FOR [.debug_info]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_abbrev // RELOC-NEXT: 0000000c R_ARM_ABS32 .debug_line // RELOC-NEXT: R_ARM_ABS32 .text @@ -51,5 +52,6 @@ a: // RELOC-NOT: RELOCATION RECORDS FOR [.debug_ranges]: // RELOC: RELOCATION RECORDS FOR [.debug_aranges]: +// RELOC-NEXT: OFFSET TYPE VALUE // RELOC-NEXT: 00000006 R_ARM_ABS32 .debug_info // RELOC-NEXT: 00000010 R_ARM_ABS32 .text diff --git a/test/MC/COFF/cfi-sections.s b/test/MC/COFF/cfi-sections.s index 00a8d746c19..5d554f17301 100644 --- a/test/MC/COFF/cfi-sections.s +++ b/test/MC/COFF/cfi-sections.s @@ -14,12 +14,14 @@ f2: .cfi_endproc // COFF_X86_64: RELOCATION RECORDS FOR [.debug_frame]: +// COFF_X86_64-NEXT: {{.*}}OFFSET TYPE VALUE // COFF_X86_64-NEXT: {{.*}} IMAGE_REL_AMD64_SECREL .debug_frame // COFF_X86_64-NEXT: {{.*}} IMAGE_REL_AMD64_ADDR64 .text // COFF_X86_64-NEXT: {{.*}} IMAGE_REL_AMD64_SECREL .debug_frame // COFF_X86_64-NEXT: {{.*}} IMAGE_REL_AMD64_ADDR64 .text // COFF_I686: RELOCATION RECORDS FOR [.debug_frame]: +// COFF_I686-NEXT: {{.*}}OFFSET TYPE VALUE // COFF_I686-NEXT: {{.*}} IMAGE_REL_I386_SECREL .debug_frame // COFF_I686-NEXT: {{.*}} IMAGE_REL_I386_DIR32 .text // COFF_I686-NEXT: {{.*}} IMAGE_REL_I386_SECREL .debug_frame diff --git a/test/Object/objdump-relocations.test b/test/Object/objdump-relocations.test index bd8cd65634a..380783347f6 100644 --- a/test/Object/objdump-relocations.test +++ b/test/Object/objdump-relocations.test @@ -119,12 +119,14 @@ Symbols: # ELF-MIPSEL: R_MIPS_CALL16 SomeOtherFunction # WASM: CODE +# WASM-NEXT: OFFSET TYPE VALUE # WASM-NEXT: R_WASM_MEMORY_ADDR_SLEB .L.str # WASM-NEXT: R_WASM_FUNCTION_INDEX_LEB puts # WASM-NEXT: R_WASM_FUNCTION_INDEX_LEB .LSomeOtherFunction_bitcast # WASM-NEXT: R_WASM_FUNCTION_INDEX_LEB SomeOtherFunction # ELF-complex-x86-64: .text +# ELF-complex-x86-64-NEXT: OFFSET TYPE VALUE # ELF-complex-x86-64-NEXT: R_X86_64_8 .data-0x4 # ELF-complex-x86-64-NEXT: R_X86_64_16 .data-0x4 # ELF-complex-x86-64-NEXT: R_X86_64_32 .data-0x4 diff --git a/test/tools/llvm-objdump/WebAssembly/relocations.test b/test/tools/llvm-objdump/WebAssembly/relocations.test index acf276e7ba7..1743f2fa3b3 100644 --- a/test/tools/llvm-objdump/WebAssembly/relocations.test +++ b/test/tools/llvm-objdump/WebAssembly/relocations.test @@ -1,4 +1,4 @@ -; RUN: llc -mtriple=wasm32-unknown-unknown -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s +; RUN: llc -mtriple=wasm32-unknown-unknown -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s --strict-whitespace --match-full-lines @foo = external global i32, align 4 @bar = global i32* @foo, align 4 @@ -9,8 +9,10 @@ entry: ret void } -; CHECK: RELOCATION RECORDS FOR [CODE]: -; CHECK-NEXT: 00000006 R_WASM_TYPE_INDEX_LEB 1+0 +; CHECK:RELOCATION RECORDS FOR [CODE]: +; CHECK-NEXT:OFFSET TYPE VALUE +; CHECK-NEXT:00000006 R_WASM_TYPE_INDEX_LEB 1+0 -; CHECK: RELOCATION RECORDS FOR [DATA]: -; CHECK-NEXT: 00000006 R_WASM_MEMORY_ADDR_I32 foo+0 +; CHECK:RELOCATION RECORDS FOR [DATA]: +; CHECK-NEXT:OFFSET TYPE VALUE +; CHECK-NEXT:00000006 R_WASM_MEMORY_ADDR_I32 foo+0 diff --git a/test/tools/llvm-objdump/coff-many-relocs.test b/test/tools/llvm-objdump/coff-many-relocs.test index d6d0d608b3b..5110286a93e 100644 --- a/test/tools/llvm-objdump/coff-many-relocs.test +++ b/test/tools/llvm-objdump/coff-many-relocs.test @@ -2,13 +2,14 @@ // RUN: llvm-objdump -r %p/Inputs/many-relocs.obj-i386 | FileCheck %s CHECK: RELOCATION RECORDS FOR [.text]: -CHECK-NEXT: IMAGE_REL_I386_DIR16 foo -CHECK-NEXT: IMAGE_REL_I386_REL16 foo -CHECK-NEXT: IMAGE_REL_I386_DIR32 foo -CHECK-NEXT: IMAGE_REL_I386_DIR32NB foo -CHECK-NEXT: IMAGE_REL_I386_SEG12 foo -CHECK-NEXT: IMAGE_REL_I386_SECTION foo -CHECK-NEXT: IMAGE_REL_I386_SECREL foo -CHECK-NEXT: IMAGE_REL_I386_TOKEN foo -CHECK-NEXT: IMAGE_REL_I386_SECREL7 foo -CHECK-NEXT: IMAGE_REL_I386_REL32 foo +CHECK-NEXT: TYPE VALUE +CHECK-NEXT: IMAGE_REL_I386_DIR16 foo +CHECK-NEXT: IMAGE_REL_I386_REL16 foo +CHECK-NEXT: IMAGE_REL_I386_DIR32 foo +CHECK-NEXT: IMAGE_REL_I386_DIR32NB foo +CHECK-NEXT: IMAGE_REL_I386_SEG12 foo +CHECK-NEXT: IMAGE_REL_I386_SECTION foo +CHECK-NEXT: IMAGE_REL_I386_SECREL foo +CHECK-NEXT: IMAGE_REL_I386_TOKEN foo +CHECK-NEXT: IMAGE_REL_I386_SECREL7 foo +CHECK-NEXT: IMAGE_REL_I386_REL32 foo diff --git a/test/tools/llvm-objdump/hex-relocation-addr.test b/test/tools/llvm-objdump/hex-relocation-addr.test index 7e7e97bc412..15e1645415f 100644 --- a/test/tools/llvm-objdump/hex-relocation-addr.test +++ b/test/tools/llvm-objdump/hex-relocation-addr.test @@ -2,16 +2,17 @@ // RUN: llvm-objdump -r %p/Inputs/win64-unwind.exe.coff-x86_64.obj | FileCheck %s CHECK: RELOCATION RECORDS FOR [.pdata]: -CHECK-NEXT: 0 IMAGE_REL_AMD64_ADDR32NB func -CHECK-NEXT: 4 IMAGE_REL_AMD64_ADDR32NB func -CHECK-NEXT: 8 IMAGE_REL_AMD64_ADDR32NB .xdata -CHECK-NEXT: c IMAGE_REL_AMD64_ADDR32NB func -CHECK-NEXT: 10 IMAGE_REL_AMD64_ADDR32NB func -CHECK-NEXT: 14 IMAGE_REL_AMD64_ADDR32NB .xdata -CHECK-NEXT: 18 IMAGE_REL_AMD64_ADDR32NB smallFunc -CHECK-NEXT: 1c IMAGE_REL_AMD64_ADDR32NB smallFunc -CHECK-NEXT: 20 IMAGE_REL_AMD64_ADDR32NB .xdata -CHECK-NEXT: 24 IMAGE_REL_AMD64_ADDR32NB allocFunc -CHECK-NEXT: 28 IMAGE_REL_AMD64_ADDR32NB allocFunc -CHECK-NEXT: 2c IMAGE_REL_AMD64_ADDR32NB .xdata +CHECK-NEXT: OFFSET TYPE VALUE +CHECK-NEXT: 0000000000000000 IMAGE_REL_AMD64_ADDR32NB func +CHECK-NEXT: 0000000000000004 IMAGE_REL_AMD64_ADDR32NB func +CHECK-NEXT: 0000000000000008 IMAGE_REL_AMD64_ADDR32NB .xdata +CHECK-NEXT: 000000000000000c IMAGE_REL_AMD64_ADDR32NB func +CHECK-NEXT: 0000000000000010 IMAGE_REL_AMD64_ADDR32NB func +CHECK-NEXT: 0000000000000014 IMAGE_REL_AMD64_ADDR32NB .xdata +CHECK-NEXT: 0000000000000018 IMAGE_REL_AMD64_ADDR32NB smallFunc +CHECK-NEXT: 000000000000001c IMAGE_REL_AMD64_ADDR32NB smallFunc +CHECK-NEXT: 0000000000000020 IMAGE_REL_AMD64_ADDR32NB .xdata +CHECK-NEXT: 0000000000000024 IMAGE_REL_AMD64_ADDR32NB allocFunc +CHECK-NEXT: 0000000000000028 IMAGE_REL_AMD64_ADDR32NB allocFunc +CHECK-NEXT: 000000000000002c IMAGE_REL_AMD64_ADDR32NB .xdata diff --git a/test/tools/llvm-objdump/relocation-xindex-symbol.test b/test/tools/llvm-objdump/relocation-xindex-symbol.test index 31cf9bc0e95..c292b2cfd65 100644 --- a/test/tools/llvm-objdump/relocation-xindex-symbol.test +++ b/test/tools/llvm-objdump/relocation-xindex-symbol.test @@ -6,7 +6,8 @@ # RUN: llvm-objdump -r %t | FileCheck %s # CHECK: RELOCATION RECORDS FOR [.text]: -# CHECK-NEXT: 0000000000000000 R_X86_64_32 bar +# CHECK-NEXT: OFFSET TYPE VALUE +# CHECK-NEXT: 0000000000000000 R_X86_64_32 bar --- !ELF FileHeader: diff --git a/test/tools/llvm-objdump/relocations-elf.test b/test/tools/llvm-objdump/relocations-elf.test index 51e539c76f0..dcac50e9533 100644 --- a/test/tools/llvm-objdump/relocations-elf.test +++ b/test/tools/llvm-objdump/relocations-elf.test @@ -2,15 +2,16 @@ # RUN: llvm-objdump --reloc %t > %t1 # RUN: llvm-objdump -r %t > %t2 # RUN: cmp %t1 %t2 -# RUN: FileCheck %s --input-file=%t1 +# RUN: FileCheck %s --input-file=%t1 --strict-whitespace --match-full-lines -# CHECK: RELOCATION RECORDS FOR [.text]: -# CHECK-NEXT: 0000000000000001 R_X86_64_32 glob1 -# CHECK-NEXT: 0000000000000001 R_X86_64_32S glob2 -# CHECK-NEXT: 0000000000000002 R_X86_64_64 loc1 -# CHECK-NEXT: 0000000000000001 R_X86_64_32 glob1+0x1 -# CHECK-NEXT: 0000000000000001 R_X86_64_32S glob2+0x2 -# CHECK-NEXT: 0000000000000002 R_X86_64_64 loc1+0x3 +# CHECK:RELOCATION RECORDS FOR [.text]: +# CHECK-NEXT:OFFSET TYPE VALUE +# CHECK-NEXT:0000000000000001 R_X86_64_32 glob1 +# CHECK-NEXT:0000000000000001 R_X86_64_32S glob2 +# CHECK-NEXT:0000000000000002 R_X86_64_64 loc1 +# CHECK-NEXT:0000000000000001 R_X86_64_32 glob1+0x1 +# CHECK-NEXT:0000000000000001 R_X86_64_32S glob2+0x2 +# CHECK-NEXT:0000000000000002 R_X86_64_64 loc1+0x3 --- !ELF FileHeader: !FileHeader @@ -143,3 +144,50 @@ Symbols: Value: 0x0 Size: 0 Binding: STB_GLOBAL + +## Check relocation formatting on 32 bit as well to verify the formatting is correct. +# RUN: yaml2obj --docnum=4 %s > %t4 +# RUN: llvm-objdump -r %t4 | FileCheck %s --check-prefix=ELF32 --strict-whitespace --match-full-lines + +# ELF32:RELOCATION RECORDS FOR [.text]: +# ELF32-NEXT:OFFSET TYPE VALUE +# ELF32-NEXT:00000001 R_386_32 global +# ELF32-NEXT:00000002 R_386_PC32 local +# ELF32-NEXT:00000001 R_386_NONE global+0x1 +# ELF32-NEXT:00000002 R_386_NONE local+0x2 + +--- !ELF +FileHeader: + Class: ELFCLASS32 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_386 +Sections: +- Name: .text + Type: SHT_PROGBITS +- Name: .rel.text + Type: SHT_REL + Info: .text + Relocations: + - Offset: 0x1 + Symbol: global + Type: R_386_32 + - Offset: 0x2 + Symbol: local + Type: R_386_PC32 +- Name: .rela.text + Type: SHT_RELA + Info: .text + Relocations: + - Offset: 0x1 + Addend: 1 + Symbol: global + Type: R_386_NONE + - Offset: 0x2 + Addend: 2 + Symbol: local + Type: R_386_NONE +Symbols: + - Name: local + - Name: global + Binding: STB_GLOBAL diff --git a/test/tools/llvm-objdump/relocations-in-nonreloc.test b/test/tools/llvm-objdump/relocations-in-nonreloc.test index b68c028738b..00f716dae90 100644 --- a/test/tools/llvm-objdump/relocations-in-nonreloc.test +++ b/test/tools/llvm-objdump/relocations-in-nonreloc.test @@ -10,7 +10,8 @@ # FMT: [[FILE]]: file format ELF64-x86-64 # REL: RELOCATION RECORDS FOR []: -# REL-NEXT: 0000000000000123 R_X86_64_NONE *ABS*+0x141 +# REL-NEXT: OFFSET TYPE VALUE +# REL-NEXT: 0000000000000123 R_X86_64_NONE *ABS*+0x141 ## Executable. --- !ELF diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index a1c37bc8bff..a82fbb2cab1 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -1645,6 +1645,11 @@ void printRelocations(const ObjectFile *Obj) { for (std::pair> &P : SecToRelSec) { StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName()); outs() << "RELOCATION RECORDS FOR [" << SecName << "]:\n"; + uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8); + uint32_t TypePadding = 24; + outs() << left_justify("OFFSET", OffsetPadding) << " " + << left_justify("TYPE", TypePadding) << " " + << "VALUE\n"; for (SectionRef Section : P.second) { for (const RelocationRef &Reloc : Section.relocations()) { @@ -1657,8 +1662,9 @@ void printRelocations(const ObjectFile *Obj) { if (Error E = getRelocationValueString(Reloc, ValueStr)) reportError(std::move(E), Obj->getFileName()); - outs() << format(Fmt.data(), Address) << " " << RelocName << " " - << ValueStr << "\n"; + outs() << format(Fmt.data(), Address) << " " + << left_justify(RelocName, TypePadding) << " " << ValueStr + << "\n"; } } outs() << "\n";