[llvm] [llvm-readobj][COFF] Implement --coff-pseudoreloc in llvm-readobj to dump runtime pseudo-relocation records (PR #151816)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 11 01:15:03 PDT 2025
================
@@ -56,122 +58,173 @@ CHECK-NEXT: BitWidth: [[#BW]]
CHECK-NEXT: }
CHECK-NEXT: ]
-
-; check that silently ignore when imported symbol name is not found
-RUN: sed -E -e '/Name: *\.rdata/,/Name:/{/SectionData:/{s/%{addr1l}200000/30000000/;s/%{addr3l}200000/00FFFF00/}}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 \
-RUN: | FileCheck %s --check-prefix=INVALIDSYMBOL --match-full-lines -D#LOCAL1A=%{local} --implicit-check-not=warning
+; Test that llvm-readobj warns about missing imported symbol names.
+RUN: yaml2obj -o %t.corrupted-iat.exe-i386 %p/Inputs/pseudoreloc.i386.yaml \
+RUN: -DSYMBOL0=30000000 -DSYMBOL1=B2200000 -DSYMBOL2=00FFFF00
+RUN: llvm-readobj %t.corrupted-iat.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefix=INVALIDSYMBOL --match-full-lines -D#LOCAL1A=%{local1a_386} --implicit-check-not=warning
INVALIDSYMBOL: Symbol: 0x30
+INVALIDSYMBOL-NEXT: {{.*}}warning: {{.*}}: the reference of the symbol points out of the import table
+INVALIDSYMBOL-NEXT: SymbolName: (missing)
INVALIDSYMBOL-NEXT: Target: 0x[[#%X,LOCAL1A]]
+INVALIDSYMBOL: Symbol: 0x20B2
+INVALIDSYMBOL-NEXT: {{.*}}warning: {{.*}}: the reference of the symbol doesn't point imported symbol properly
+INVALIDSYMBOL-NEXT: SymbolName: (missing)
+INVALIDSYMBOL-NEXT: Target: 0x[[#%X,LOCAL1A+4]]
INVALIDSYMBOL: Symbol: 0xFFFF00
+INVALIDSYMBOL-NEXT: {{.*}}warning: {{.*}}: the reference of the symbol points out of the import table
+INVALIDSYMBOL-NEXT: SymbolName: (missing)
INVALIDSYMBOL-NEXT: Target: 0x[[#%X,LOCAL1A+8]]
-
-; check that shows empty list when the relocation list has no contents
-RUN: sed -E -e '/__RUNTIME_PSEUDO_RELOC_LIST_END__/,/Name:/{/Value:/s/[0-9]+/20/}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 \
-RUN: | FileCheck %s --check-prefix=EMPTY --implicit-check-not=warning
+; Assume the position of the section and the relocation list for further tests.
+RUN: FileCheck --input-file=%p/Inputs/pseudoreloc.i386.yaml %s --check-prefix=RELOCPOS --match-full-lines
+
+RELOCPOS: sections:
+RELOCPOS-NOT: - Name:
+RELOCPOS: - Name: .text
+RELOCPOS-NOT: - Name:
+RELOCPOS: - Name: .rdata
+RELOCPOS-NEXT: Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+RELOCPOS-NEXT: VirtualAddress: 8192
+RELOCPOS: - Name: .data
+RELOCPOS-NEXT: Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+RELOCPOS-NEXT: VirtualAddress: 12288
+RELOCPOS: - Name: ___RUNTIME_PSEUDO_RELOC_LIST_END__
+RELOCPOS-NEXT: Value: {{\[\[END=80\]\]}}
+RELOCPOS: - Name: ___RUNTIME_PSEUDO_RELOC_LIST__
+RELOCPOS-NEXT: Value: {{\[\[BEGIN=8\]\]}}
+RELOCPOS-NEXT: SectionNumber: {{\[\[SECTION_OF_BEGIN=2\]\]}}
+
+; Test that llvm-readobj warns if a symbol belongs to a nonexistent section.
+RUN: yaml2obj -o %t.nosection.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DSECTION_OF_LOCAL2A=999
+RUN: llvm-readobj %t.nosection.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=WARN-NOSECTION
+
+WARN-NOSECTION: section index out of bounds
+
+; Test that llvm-readobj shows an empty list if the relocation list has no contents.
+RUN: yaml2obj -o %t.empty-list.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DEND=20
+RUN: llvm-readobj %t.empty-list.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefix=EMPTY --implicit-check-not=warning
+
+; Test that llvm-readobj shows an empty list if the relocation list has no header.
+RUN: yaml2obj -o %t.no-header.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DEND=8
+RUN: llvm-readobj %t.no-header.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefix=EMPTY --implicit-check-not=warning
+
+; Test that llvm-readobj shows an empty list if the image is stripped.
+RUN: llvm-readobj --coff-pseudoreloc %p/Inputs/imports.exe.coff-i386 2>&1 | \
+RUN: FileCheck %s --check-prefix=EMPTY --implicit-check-not=warning
+
+; Test that llvm-readobj warns if the marker symbol of the relocation list is absent from the symbol table.
+RUN: sed -e 's/__RUNTIME//' %p/Inputs/pseudoreloc.i386.yaml | \
+RUN: yaml2obj -o %t.nosymbol.exe-i386
+RUN: llvm-readobj %t.nosymbol.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=EMPTY,WARN-MISSINGMARKER
+
+; Test that llvm-readobj shows an empty list if a .obj is specified.
+RUN: llvm-readobj --coff-pseudoreloc %p/Inputs/trivial.obj.coff-i386 2>&1 | \
+RUN: FileCheck %s --check-prefix=EMPTY --implicit-check-not=warning
+
+; Test that llvm-readobj warns if the header of the relocation list is broken.
+RUN: yaml2obj -o %t.broken-header.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DBEGIN=20 -DEND=48
+RUN: llvm-readobj %t.broken-header.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=EMPTY,WARN-INVALIDHEADER
+
+; Test that llvm-readobj warns if end < start.
+RUN: yaml2obj -o %t.negative-size.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DBEGIN=20 -DEND=8
+RUN: llvm-readobj %t.negative-size.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=EMPTY,WARN-LOWEREND
+
+; Test that llvm-readobj warns if the marker symbol points out of the section space.
+RUN: yaml2obj -o %t.outofrange-both.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DBEGIN=8888 -DEND=9999
+RUN: llvm-readobj %t.outofrange-both.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=EMPTY,WARN-OUTOFRANGE
+
+RUN: yaml2obj -o %t.outofrange-end.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DEND=9999
+RUN: llvm-readobj %t.outofrange-end.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=EMPTY,WARN-OUTOFRANGE
+
+; Test that llvm-readobj warns if the marker symbols point different sections.
+RUN: yaml2obj -o %t.section-differs.exe-i386 %p/Inputs/pseudoreloc.i386.yaml -DSECTION_OF_BEGIN=1
+RUN: llvm-readobj %t.section-differs.exe-i386 --coff-pseudoreloc 2>&1 | \
+RUN: FileCheck %s --check-prefixes=EMPTY,WARN-SECTIONDIFFERS
EMPTY: Format: COFF-i386
EMPTY-NEXT: Arch: i386
EMPTY-NEXT: AddressSize: 32bit
EMPTY-NEXT: PseudoReloc [
+WARN-MISSINGMARKER-NEXT: the marker symbols for runtime pseudo-relocation were not found
+WARN-INVALIDHEADER-NEXT: invalid runtime pseudo-relocation records
+WARN-LOWEREND-NEXT: the begin marker symbol for runtime pseudo-relocation must point lower address than where the end marker points
+WARN-OUTOFRANGE-NEXT: the marker symbol of runtime pseudo-relocation points to out of the valid address space
+WARN-SECTIONDIFFERS-NEXT: the marker symbols for runtime pseudo-relocation must point a same section
EMPTY-NEXT: ]
-
-; assume position of the relocation list for further tests
-RUN: yaml2obj %p/Inputs/pseudoreloc.i386.yaml | llvm-readobj --syms - | FileCheck %s --check-prefix=RELOCPOS --match-full-lines
-
-RELOCPOS: Name: ___RUNTIME_PSEUDO_RELOC_LIST__
-RELOCPOS-NEXT: Value: 8
-
-
-; check that shows message if the header of the relocation list is broken or missing
-RUN: sed -E -e '/__RUNTIME_PSEUDO_RELOC_LIST__/,/Name:/{/Value:/s/[0-9]+/20/}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 | FileCheck %s --check-prefix=NOHEADER --implicit-check-not=PseudoReloc
-
-RUN: sed -E -e '/__RUNTIME_PSEUDO_RELOC_LIST_END__/,/Name:/{/Value:/s/[0-9]+/8/}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 | FileCheck %s --check-prefix=NOHEADER --implicit-check-not=PseudoReloc
-
-NOHEADER: invalid runtime pseudo-relocation records
-
-
-; check that warn if end < start
-RUN: sed -E -e '/__RUNTIME_PSEUDO_RELOC_LIST_END__/,/Name:/{/Value:/s/[0-9]+/8/}' \
-RUN: -e '/__RUNTIME_PSEUDO_RELOC_LIST__/,/Name:/{/Value:/s/[0-9]+/20/}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 | FileCheck %s --check-prefix=INVALREGION --implicit-check-not=PseudoReloc
-
-INVALREGION: the symbols for runtime pseudo-relocation don't consist a valid region
-
-
-; check that warn if the relocation list points out of section space
-RUN: sed -E -e '/__RUNTIME_PSEUDO_RELOC_LIST_END__/,/Name:/{/Value:/s/[0-9]+/9999/}' \
-RUN: -e '/__RUNTIME_PSEUDO_RELOC_LIST__/,/Name:/{/Value:/s/[0-9]+/8888/}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 | FileCheck %s --check-prefix=OUTOFRANGE --implicit-check-not=PseudoReloc
-
-RUN: sed -E -e '/__RUNTIME_PSEUDO_RELOC_LIST_END__/,/Name:/{/Value:/s/[0-9]+/9999/}' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 | FileCheck %s --check-prefix=OUTOFRANGE --implicit-check-not=PseudoReloc
-
-OUTOFRANGE: the region of runtime pseudo-relocation records points to out of the valid location
-
-
-; check that warn if image is stripped
-RUN: llvm-readobj --coff-pseudoreloc %p/Inputs/nop.exe.coff-x86-64 2>&1 | FileCheck %s --check-prefix=NOSYM --implicit-check-not=PseudoReloc
-
-NOSYM: the symbol table has been stripped
-
-
-; check that warn if the relocation list is missing (intended for MSVC-made binaries)
-RUN: sed -e 's/__RUNTIME//' %p/Inputs/pseudoreloc.i386.yaml \
-RUN: | yaml2obj | llvm-readobj --coff-pseudoreloc - 2>&1 | FileCheck %s --check-prefix=NORELOC --implicit-check-not=PseudoReloc
-
-NORELOC: the symbols for runtime pseudo-relocation are not found
-
-
-; check that warn if .obj is specified
-RUN: llvm-readobj --coff-pseudoreloc %p/Inputs/trivial.obj.coff-i386 2>&1 | FileCheck %s --check-prefix=NOTPE --implicit-check-not=PseudoReloc
-
-NOTPE: pseudo-relocation is only meaningful for a PE image file
-
-
-pseudoreloc.*.yaml is generated by following script:
+To regenerate Inputs/pseudoreloc.*.yaml, run following one-liner and review actual address map:
----------------
jh7370 wrote:
Add a comment marker here for readability.
https://github.com/llvm/llvm-project/pull/151816
More information about the llvm-commits
mailing list