Skip to content

[yaml2obj][XOFF] Update yaml2obj for XCOFF to create valid XCOFF files in more cases. #77620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 145 additions & 87 deletions llvm/lib/ObjectYAML/XCOFFEmitter.cpp

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions llvm/test/tools/llvm-objcopy/XCOFF/invalid-read.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
# RUN: yaml2obj %s --docnum=1 -o %t1
# RUN: not llvm-objcopy %t1 %t1.out 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=ERROR1

# ERROR1: error: '[[FILE]]': The end of the file was unexpectedly encountered: section data with offset 0x70 and size 0x4 goes past the end of the file
# ERROR1: error: '[[FILE]]': The end of the file was unexpectedly encountered: section data with offset 0x70 and size 0x20 goes past the end of the file

--- !XCOFF
FileHeader:
MagicNumber: 0x01DF
Sections:
- SectionData: '00007400'
FileOffsetToData: 0x70
Size: 0x20

## Failed to read relocations.
# RUN: yaml2obj %s --docnum=2 -o %t2
Expand All @@ -35,12 +36,13 @@ Sections:
# RUN: yaml2obj %s --docnum=3 -o %t3
# RUN: not llvm-objcopy %t3 %t3.out 2>&1 | FileCheck %s -DFILE=%t3 --check-prefix=ERROR3

# ERROR3: error: '[[FILE]]': The end of the file was unexpectedly encountered: symbol table with offset 0x15 and size 0x24 goes past the end of the file
# ERROR3: error: '[[FILE]]': The end of the file was unexpectedly encountered: symbol table with offset 0x15 and size 0x36 goes past the end of the file

--- !XCOFF
FileHeader:
MagicNumber: 0x01DF
OffsetToSymbolTable: 0x15
EntriesInSymbolTable: 3
Symbols:
- Name: foo
AuxEntries:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ Symbols:
# CHECK-NEXT: 70: 00 00 00 00
# CHECK-NEXT: ...
# CHECK-NEXT: 7c: 00 12 34 00
# CHECK-NEXT: 80: 00 00 00 00
# CHECK-NEXT: 80: 00 00 00
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-objdump/XCOFF/section-headers.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# CHECK-NEXT: 1 .data 00000004 00000000 DATA
# CHECK-NEXT: 2 .bss 00000000 00000010 BSS
# CHECK-NEXT: 3 .tdata 00000004 00000000 DATA
# CHECK-NEXT: 4 .tbss 00000000 00000000 BSS
# CHECK-NEXT: 4 .tbss 00000000 00000004 BSS
# CHECK-NEXT: 5 .dwline 00000046 00000000 DEBUG
# CHECK-NEXT: 6 .debug 00000046 00000000 DEBUG

Expand Down
3 changes: 1 addition & 2 deletions llvm/test/tools/llvm-readobj/XCOFF/file-header.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ FileHeader:
CreationTime: [[CREATTIME=1]]
EntriesInSymbolTable: [[SYMBOLCOUNT=1]]
NumberOfSections: 1
OffsetToSymbolTable: 0x3C
AuxiliaryHeaderSize: 0
Flags: 0x12
Sections:
Expand All @@ -42,7 +41,7 @@ Symbols:
# FILEHEADER64-NEXT: Magic: 0x1F7
# FILEHEADER64-NEXT: NumberOfSections: 1
# FILEHEADER64-NEXT: TimeStamp: None (0x0)
# FILEHEADER64-NEXT: SymbolTableOffset: 0x3C
# FILEHEADER64-NEXT: SymbolTableOffset: 0x60
# FILEHEADER64-NEXT: SymbolTableEntries: 1
# FILEHEADER64-NEXT: OptionalHeaderSize: 0x0
# FILEHEADER64-NEXT: Flags: 0x12
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/tools/llvm-readobj/XCOFF/sections.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# SEC32-NEXT: Name: .text
# SEC32-NEXT: PhysicalAddress: 0x0
# SEC32-NEXT: VirtualAddress: 0x0
# SEC32-NEXT: Size: 0x4
# SEC32-NEXT: Size: 0x2
# SEC32-NEXT: RawDataOffset: 0x64
# SEC32-NEXT: RelocationPointer: 0x0
# SEC32-NEXT: LineNumberPointer: 0x0
Expand All @@ -24,11 +24,11 @@
# SEC32-NEXT: Section {
# SEC32-NEXT: Index: 2
# SEC32-NEXT: Name: .data
# SEC32-NEXT: PhysicalAddress: 0x4
# SEC32-NEXT: VirtualAddress: 0x4
# SEC32-NEXT: Size: 0x4
# SEC32-NEXT: PhysicalAddress: 0x0
# SEC32-NEXT: VirtualAddress: 0x0
# SEC32-NEXT: Size: 0x2
# SEC32-NEXT: RawDataOffset: 0x68
# SEC32-NEXT: RelocationPointer: 0x6C
# SEC32-NEXT: RelocationPointer: 0x6A
# SEC32-NEXT: LineNumberPointer: 0x0
# SEC32-NEXT: NumberOfRelocations: 1
# SEC32-NEXT: NumberOfLineNumbers: 0
Expand Down Expand Up @@ -65,7 +65,7 @@ Sections:
# SEC64-NEXT: Name: .text
# SEC64-NEXT: PhysicalAddress: 0x0
# SEC64-NEXT: VirtualAddress: 0x0
# SEC64-NEXT: Size: 0x4
# SEC64-NEXT: Size: 0x2
# SEC64-NEXT: RawDataOffset: 0xA8
# SEC64-NEXT: RelocationPointer: 0x0
# SEC64-NEXT: LineNumberPointer: 0x0
Expand All @@ -76,11 +76,11 @@ Sections:
# SEC64-NEXT: Section {
# SEC64-NEXT: Index: 2
# SEC64-NEXT: Name: .data
# SEC64-NEXT: PhysicalAddress: 0x4
# SEC64-NEXT: VirtualAddress: 0x4
# SEC64-NEXT: Size: 0x4
# SEC64-NEXT: PhysicalAddress: 0x0
# SEC64-NEXT: VirtualAddress: 0x0
# SEC64-NEXT: Size: 0x2
# SEC64-NEXT: RawDataOffset: 0xAC
# SEC64-NEXT: RelocationPointer: 0xB0
# SEC64-NEXT: RelocationPointer: 0xAE
# SEC64-NEXT: LineNumberPointer: 0x0
# SEC64-NEXT: NumberOfRelocations: 1
# SEC64-NEXT: NumberOfLineNumbers: 0
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/obj2yaml/XCOFF/aix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# CHECK32-NEXT: Info: 0xF
# CHECK32-NEXT: Type: 0x3
# CHECK32-NEXT: - Name: .data
# CHECK32-NEXT: Address: 0x4
# CHECK32-NEXT: Address: 0x0
# CHECK32-NEXT: Size: 0x4
# CHECK32-NEXT: FileOffsetToData: 0x68
# CHECK32-NEXT: FileOffsetToRelocations: 0x76
Expand Down Expand Up @@ -103,7 +103,7 @@
# CHECK64-NEXT: Info: 0xF
# CHECK64-NEXT: Type: 0x3
# CHECK64-NEXT: - Name: .data
# CHECK64-NEXT: Address: 0x4
# CHECK64-NEXT: Address: 0x0
# CHECK64-NEXT: Size: 0x4
# CHECK64-NEXT: FileOffsetToData: 0xAC
# CHECK64-NEXT: FileOffsetToRelocations: 0xBE
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/tools/obj2yaml/XCOFF/invalid-section.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
# RUN: yaml2obj %s --docnum=1 -o %t1
# RUN: not obj2yaml %t1 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=ERROR1

# ERROR1: The end of the file was unexpectedly encountered: section data with offset 0x70 and size 0x4 goes past the end of the file
# ERROR1: The end of the file was unexpectedly encountered: section data with offset 0x70 and size 0x20 goes past the end of the file

--- !XCOFF
FileHeader:
MagicNumber: 0x01DF
Sections:
- SectionData: '00007400'
FileOffsetToData: 0x70
Size: 0x20

## Error2: failed to get relocations.
# RUN: yaml2obj %s --docnum=2 -o %t2
Expand Down
24 changes: 10 additions & 14 deletions llvm/test/tools/yaml2obj/XCOFF/aux-hdr-defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
# CASE1: AuxiliaryHeader {
# CASE1-NEXT: Magic: 0x10B
# CASE1-NEXT: Version: 0x1
# CASE1-NEXT: Size of .text section: 0x8
# CASE1-NEXT: Size of .data section: 0x8
# CASE1-NEXT: Size of .bss section: 0x8
# CASE1-NEXT: Size of .text section: 0x5
# CASE1-NEXT: Size of .data section: 0x5
# CASE1-NEXT: Size of .bss section: 0x5
# CASE1-NEXT: Entry point address: 0x0
# CASE1-NEXT: .text section start address: 0x4
# CASE1-NEXT: .data section start address: 0x10
# CASE1-NEXT: .text section start address: 0x0
# CASE1-NEXT: .data section start address: 0x0
# CASE1-NEXT: TOC anchor address: 0x0
# CASE1-NEXT: Section number of entryPoint: 0
# CASE1-NEXT: Section number of .text: 2
Expand Down Expand Up @@ -79,16 +79,12 @@ Sections:
# RUN: yaml2obj %s --docnum=1 -DMAGIC=0x1F7 -o %t2
# RUN: llvm-readobj --auxiliary-header %t2 | FileCheck %s --check-prefix=CASE2

## Case2: same as case1, except it is 64-bit.
# RUN: yaml2obj %s --docnum=1 -DMAGIC=0x1F7 -o %t2
# RUN: llvm-readobj --auxiliary-header %t2 | FileCheck %s --check-prefix=CASE2

# CASE2: AuxiliaryHeader {
# CASE2-NEXT: Magic: 0x10B
# CASE2-NEXT: Version: 0x1
# CASE2-NEXT: Reserved for debugger: 0x0
# CASE2-NEXT: .text section start address: 0x2
# CASE2-NEXT: .data section start address: 0xE
# CASE2-NEXT: .text section start address: 0x0
# CASE2-NEXT: .data section start address: 0x0
# CASE2-NEXT: TOC anchor address: 0x0
# CASE2-NEXT: Section number of entryPoint: 0
# CASE2-NEXT: Section number of .text: 2
Expand All @@ -106,9 +102,9 @@ Sections:
# CASE2-NEXT: Stack page size: 0x0
# CASE2-NEXT: Flag: 0x80
# CASE2-NEXT: Alignment of thread-local storage: 0x0
# CASE2-NEXT: Size of .text section: 0x8
# CASE2-NEXT: Size of .data section: 0x8
# CASE2-NEXT: Size of .bss section: 0x8
# CASE2-NEXT: Size of .text section: 0x5
# CASE2-NEXT: Size of .data section: 0x5
# CASE2-NEXT: Size of .bss section: 0x5
# CASE2-NEXT: Entry point address: 0x0
# CASE2-NEXT: Maximum stack size: 0x0
# CASE2-NEXT: Maximum data size: 0x0
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/yaml2obj/XCOFF/basic-doc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Symbols:
# CHECK-NEXT: Section {
# CHECK-NEXT: Index: 2
# CHECK-NEXT: Name: .data
# CHECK-NEXT: PhysicalAddress: 0x8
# CHECK-NEXT: VirtualAddress: 0x8
# CHECK-NEXT: PhysicalAddress: 0x0
# CHECK-NEXT: VirtualAddress: 0x0
# CHECK-NEXT: Size: 0x8
# CHECK-NEXT: RawDataOffset: 0xE4
# CHECK-NEXT: RelocationPointer: 0xF0
Expand Down
91 changes: 91 additions & 0 deletions llvm/test/tools/yaml2obj/XCOFF/offset-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
## Report errors when specified file offsets are invalid.

# RUN: not yaml2obj %s -DTEXTRAWDATAOFFSET=0xFFFFFFF0 -o %t 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR1
# ERROR1: current file offset (4294967288) is bigger than the specified FileOffsetToData for the .data section (108)

# RUN: not yaml2obj %s -DDATARAWDATAOFFSET=0xFFFFFFF0 -o %t 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR2
# ERROR2: current file offset (4294967284) is bigger than the specified FileOffsetToRelocations for the .text section (112)

# RUN: not yaml2obj %s -DRELOCOFFSET=0xFFFFFFF0 -o %t 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR3
# ERROR3: current file offset (4294967290) is bigger than the specified SymbolTableOffset (122)

# RUN: not yaml2obj %s -DSYMTABOFFSET=0x100000000 -o %t 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR4
# ERROR4: maximum object size of 4294967295 exceeded when writing symbols

# RUN: not yaml2obj %s -DRELOCOFFSET=0x100000000 -o %t 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR5
# ERROR5: maximum object size (4294967295) exceeded when writing relocation data for section .text

# RUN: not yaml2obj %s -DDATARAWDATAOFFSET=0x100000000 -o %t 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR6
# ERROR6: maximum object size (4294967295) exceeded when writing data for section 2 (.data)

--- !XCOFF
FileHeader:
MagicNumber: 0x1DF
NumberOfSections: 2
OffsetToSymbolTable: [[SYMTABOFFSET=0x7A]]
EntriesInSymbolTable: 6
AuxiliaryHeaderSize: 0
Flags: 0x0
Sections:
- Name: .text
Address: 0x0
Size: 0x8
FileOffsetToData: [[TEXTRAWDATAOFFSET=0x64]]
FileOffsetToRelocations: [[RELOCOFFSET=0x70]]
NumberOfRelocations: 0x1
Flags: [ STYP_TEXT ]
SectionData: 386000004BFFFFFC
Relocations:
- Address: 0x4
Symbol: 0x2
Info: 0x99
Type: 0x1A
- Name: .data
Address: 0x0
Size: 0x4
FileOffsetToData: [[DATARAWDATAOFFSET=0x6C]]
FileOffsetToRelocations: 0
Flags: [ STYP_DATA ]
SectionData: 3210ABCD
Symbols:
- Name: .bar
Value: 0x0
Section: N_UNDEF
Type: 0x0
StorageClass: C_EXT
NumberOfAuxEntries: 1
AuxEntries:
- Type: AUX_CSECT
SymbolAlignmentAndType: 0
StorageMappingClass: XMC_PR
SectionOrLength: 0
- Name: ''
Value: 0x0
Section: .text
Type: 0x0
StorageClass: C_HIDEXT
NumberOfAuxEntries: 1
AuxEntries:
- Type: AUX_CSECT
SymbolAlignmentAndType: 17
StorageMappingClass: XMC_PR
SectionOrLength: 8
- Name: .foo
Value: 0x0
Section: .text
Type: 0x0
StorageClass: C_EXT
NumberOfAuxEntries: 1
AuxEntries:
- Type: AUX_CSECT
SymbolAlignmentAndType: 2
StorageMappingClass: XMC_PR
SectionOrLength: 2
StringTable: {}
...