Skip to content

Commit b5abaea

Browse files
[yaml2obj][XOFF] Update yaml2obj for XCOFF to create valid XCOFF files in more cases. (#77620)
yaml2obj creates invalid object files even when the input was created by obj2yaml using a valid object file. On the other hand, yaml2obj is used to intentionally create invalid object files for testing purposes. This update balances using specified input values when provided and computing file offsets and sizes if necessary.
1 parent 0de859c commit b5abaea

File tree

11 files changed

+269
-122
lines changed

11 files changed

+269
-122
lines changed

llvm/lib/ObjectYAML/XCOFFEmitter.cpp

Lines changed: 145 additions & 87 deletions
Large diffs are not rendered by default.

llvm/test/tools/llvm-objcopy/XCOFF/invalid-read.test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
# RUN: yaml2obj %s --docnum=1 -o %t1
66
# RUN: not llvm-objcopy %t1 %t1.out 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=ERROR1
77

8-
# 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
8+
# 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
99

1010
--- !XCOFF
1111
FileHeader:
1212
MagicNumber: 0x01DF
1313
Sections:
1414
- SectionData: '00007400'
1515
FileOffsetToData: 0x70
16+
Size: 0x20
1617

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

38-
# 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
39+
# 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
3940

4041
--- !XCOFF
4142
FileHeader:
4243
MagicNumber: 0x01DF
4344
OffsetToSymbolTable: 0x15
45+
EntriesInSymbolTable: 3
4446
Symbols:
4547
- Name: foo
4648
AuxEntries:

llvm/test/tools/llvm-objdump/XCOFF/disassemble-traceback-table.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,4 @@ Symbols:
112112
# CHECK-NEXT: 70: 00 00 00 00
113113
# CHECK-NEXT: ...
114114
# CHECK-NEXT: 7c: 00 12 34 00
115-
# CHECK-NEXT: 80: 00 00 00 00
115+
# CHECK-NEXT: 80: 00 00 00

llvm/test/tools/llvm-objdump/XCOFF/section-headers.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# CHECK-NEXT: 1 .data 00000004 00000000 DATA
1111
# CHECK-NEXT: 2 .bss 00000000 00000010 BSS
1212
# CHECK-NEXT: 3 .tdata 00000004 00000000 DATA
13-
# CHECK-NEXT: 4 .tbss 00000000 00000000 BSS
13+
# CHECK-NEXT: 4 .tbss 00000000 00000004 BSS
1414
# CHECK-NEXT: 5 .dwline 00000046 00000000 DEBUG
1515
# CHECK-NEXT: 6 .debug 00000046 00000000 DEBUG
1616

llvm/test/tools/llvm-readobj/XCOFF/file-header.test

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ FileHeader:
2323
CreationTime: [[CREATTIME=1]]
2424
EntriesInSymbolTable: [[SYMBOLCOUNT=1]]
2525
NumberOfSections: 1
26-
OffsetToSymbolTable: 0x3C
2726
AuxiliaryHeaderSize: 0
2827
Flags: 0x12
2928
Sections:
@@ -42,7 +41,7 @@ Symbols:
4241
# FILEHEADER64-NEXT: Magic: 0x1F7
4342
# FILEHEADER64-NEXT: NumberOfSections: 1
4443
# FILEHEADER64-NEXT: TimeStamp: None (0x0)
45-
# FILEHEADER64-NEXT: SymbolTableOffset: 0x3C
44+
# FILEHEADER64-NEXT: SymbolTableOffset: 0x60
4645
# FILEHEADER64-NEXT: SymbolTableEntries: 1
4746
# FILEHEADER64-NEXT: OptionalHeaderSize: 0x0
4847
# FILEHEADER64-NEXT: Flags: 0x12

llvm/test/tools/llvm-readobj/XCOFF/sections.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# SEC32-NEXT: Name: .text
1414
# SEC32-NEXT: PhysicalAddress: 0x0
1515
# SEC32-NEXT: VirtualAddress: 0x0
16-
# SEC32-NEXT: Size: 0x4
16+
# SEC32-NEXT: Size: 0x2
1717
# SEC32-NEXT: RawDataOffset: 0x64
1818
# SEC32-NEXT: RelocationPointer: 0x0
1919
# SEC32-NEXT: LineNumberPointer: 0x0
@@ -24,11 +24,11 @@
2424
# SEC32-NEXT: Section {
2525
# SEC32-NEXT: Index: 2
2626
# SEC32-NEXT: Name: .data
27-
# SEC32-NEXT: PhysicalAddress: 0x4
28-
# SEC32-NEXT: VirtualAddress: 0x4
29-
# SEC32-NEXT: Size: 0x4
27+
# SEC32-NEXT: PhysicalAddress: 0x0
28+
# SEC32-NEXT: VirtualAddress: 0x0
29+
# SEC32-NEXT: Size: 0x2
3030
# SEC32-NEXT: RawDataOffset: 0x68
31-
# SEC32-NEXT: RelocationPointer: 0x6C
31+
# SEC32-NEXT: RelocationPointer: 0x6A
3232
# SEC32-NEXT: LineNumberPointer: 0x0
3333
# SEC32-NEXT: NumberOfRelocations: 1
3434
# SEC32-NEXT: NumberOfLineNumbers: 0
@@ -65,7 +65,7 @@ Sections:
6565
# SEC64-NEXT: Name: .text
6666
# SEC64-NEXT: PhysicalAddress: 0x0
6767
# SEC64-NEXT: VirtualAddress: 0x0
68-
# SEC64-NEXT: Size: 0x4
68+
# SEC64-NEXT: Size: 0x2
6969
# SEC64-NEXT: RawDataOffset: 0xA8
7070
# SEC64-NEXT: RelocationPointer: 0x0
7171
# SEC64-NEXT: LineNumberPointer: 0x0
@@ -76,11 +76,11 @@ Sections:
7676
# SEC64-NEXT: Section {
7777
# SEC64-NEXT: Index: 2
7878
# SEC64-NEXT: Name: .data
79-
# SEC64-NEXT: PhysicalAddress: 0x4
80-
# SEC64-NEXT: VirtualAddress: 0x4
81-
# SEC64-NEXT: Size: 0x4
79+
# SEC64-NEXT: PhysicalAddress: 0x0
80+
# SEC64-NEXT: VirtualAddress: 0x0
81+
# SEC64-NEXT: Size: 0x2
8282
# SEC64-NEXT: RawDataOffset: 0xAC
83-
# SEC64-NEXT: RelocationPointer: 0xB0
83+
# SEC64-NEXT: RelocationPointer: 0xAE
8484
# SEC64-NEXT: LineNumberPointer: 0x0
8585
# SEC64-NEXT: NumberOfRelocations: 1
8686
# SEC64-NEXT: NumberOfLineNumbers: 0

llvm/test/tools/obj2yaml/XCOFF/aix.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# CHECK32-NEXT: Info: 0xF
3232
# CHECK32-NEXT: Type: 0x3
3333
# CHECK32-NEXT: - Name: .data
34-
# CHECK32-NEXT: Address: 0x4
34+
# CHECK32-NEXT: Address: 0x0
3535
# CHECK32-NEXT: Size: 0x4
3636
# CHECK32-NEXT: FileOffsetToData: 0x68
3737
# CHECK32-NEXT: FileOffsetToRelocations: 0x76
@@ -105,7 +105,7 @@
105105
# CHECK64-NEXT: Info: 0xF
106106
# CHECK64-NEXT: Type: 0x3
107107
# CHECK64-NEXT: - Name: .data
108-
# CHECK64-NEXT: Address: 0x4
108+
# CHECK64-NEXT: Address: 0x0
109109
# CHECK64-NEXT: Size: 0x4
110110
# CHECK64-NEXT: FileOffsetToData: 0xAC
111111
# CHECK64-NEXT: FileOffsetToRelocations: 0xBE

llvm/test/tools/obj2yaml/XCOFF/invalid-section.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
# RUN: yaml2obj %s --docnum=1 -o %t1
66
# RUN: not obj2yaml %t1 2>&1 | FileCheck %s -DFILE=%t1 --check-prefix=ERROR1
77

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

1010
--- !XCOFF
1111
FileHeader:
1212
MagicNumber: 0x01DF
1313
Sections:
1414
- SectionData: '00007400'
1515
FileOffsetToData: 0x70
16+
Size: 0x20
1617

1718
## Error2: failed to get relocations.
1819
# RUN: yaml2obj %s --docnum=2 -o %t2

llvm/test/tools/yaml2obj/XCOFF/aux-hdr-defaults.yaml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
# CASE1: AuxiliaryHeader {
1111
# CASE1-NEXT: Magic: 0x10B
1212
# CASE1-NEXT: Version: 0x1
13-
# CASE1-NEXT: Size of .text section: 0x8
14-
# CASE1-NEXT: Size of .data section: 0x8
15-
# CASE1-NEXT: Size of .bss section: 0x8
13+
# CASE1-NEXT: Size of .text section: 0x5
14+
# CASE1-NEXT: Size of .data section: 0x5
15+
# CASE1-NEXT: Size of .bss section: 0x5
1616
# CASE1-NEXT: Entry point address: 0x0
17-
# CASE1-NEXT: .text section start address: 0x4
18-
# CASE1-NEXT: .data section start address: 0x10
17+
# CASE1-NEXT: .text section start address: 0x0
18+
# CASE1-NEXT: .data section start address: 0x0
1919
# CASE1-NEXT: TOC anchor address: 0x0
2020
# CASE1-NEXT: Section number of entryPoint: 0
2121
# CASE1-NEXT: Section number of .text: 2
@@ -79,16 +79,12 @@ Sections:
7979
# RUN: yaml2obj %s --docnum=1 -DMAGIC=0x1F7 -o %t2
8080
# RUN: llvm-readobj --auxiliary-header %t2 | FileCheck %s --check-prefix=CASE2
8181

82-
## Case2: same as case1, except it is 64-bit.
83-
# RUN: yaml2obj %s --docnum=1 -DMAGIC=0x1F7 -o %t2
84-
# RUN: llvm-readobj --auxiliary-header %t2 | FileCheck %s --check-prefix=CASE2
85-
8682
# CASE2: AuxiliaryHeader {
8783
# CASE2-NEXT: Magic: 0x10B
8884
# CASE2-NEXT: Version: 0x1
8985
# CASE2-NEXT: Reserved for debugger: 0x0
90-
# CASE2-NEXT: .text section start address: 0x2
91-
# CASE2-NEXT: .data section start address: 0xE
86+
# CASE2-NEXT: .text section start address: 0x0
87+
# CASE2-NEXT: .data section start address: 0x0
9288
# CASE2-NEXT: TOC anchor address: 0x0
9389
# CASE2-NEXT: Section number of entryPoint: 0
9490
# CASE2-NEXT: Section number of .text: 2
@@ -106,9 +102,9 @@ Sections:
106102
# CASE2-NEXT: Stack page size: 0x0
107103
# CASE2-NEXT: Flag: 0x80
108104
# CASE2-NEXT: Alignment of thread-local storage: 0x0
109-
# CASE2-NEXT: Size of .text section: 0x8
110-
# CASE2-NEXT: Size of .data section: 0x8
111-
# CASE2-NEXT: Size of .bss section: 0x8
105+
# CASE2-NEXT: Size of .text section: 0x5
106+
# CASE2-NEXT: Size of .data section: 0x5
107+
# CASE2-NEXT: Size of .bss section: 0x5
112108
# CASE2-NEXT: Entry point address: 0x0
113109
# CASE2-NEXT: Maximum stack size: 0x0
114110
# CASE2-NEXT: Maximum data size: 0x0

llvm/test/tools/yaml2obj/XCOFF/basic-doc.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ Symbols:
6565
# CHECK-NEXT: Section {
6666
# CHECK-NEXT: Index: 2
6767
# CHECK-NEXT: Name: .data
68-
# CHECK-NEXT: PhysicalAddress: 0x8
69-
# CHECK-NEXT: VirtualAddress: 0x8
68+
# CHECK-NEXT: PhysicalAddress: 0x0
69+
# CHECK-NEXT: VirtualAddress: 0x0
7070
# CHECK-NEXT: Size: 0x8
7171
# CHECK-NEXT: RawDataOffset: 0xE4
7272
# CHECK-NEXT: RelocationPointer: 0xF0
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
## Report errors when specified file offsets are invalid.
2+
3+
# RUN: not yaml2obj %s -DTEXTRAWDATAOFFSET=0xFFFFFFF0 -o %t 2>&1 | \
4+
# RUN: FileCheck %s --check-prefix=ERROR1
5+
# ERROR1: current file offset (4294967288) is bigger than the specified FileOffsetToData for the .data section (108)
6+
7+
# RUN: not yaml2obj %s -DDATARAWDATAOFFSET=0xFFFFFFF0 -o %t 2>&1 | \
8+
# RUN: FileCheck %s --check-prefix=ERROR2
9+
# ERROR2: current file offset (4294967284) is bigger than the specified FileOffsetToRelocations for the .text section (112)
10+
11+
# RUN: not yaml2obj %s -DRELOCOFFSET=0xFFFFFFF0 -o %t 2>&1 | \
12+
# RUN: FileCheck %s --check-prefix=ERROR3
13+
# ERROR3: current file offset (4294967290) is bigger than the specified SymbolTableOffset (122)
14+
15+
# RUN: not yaml2obj %s -DSYMTABOFFSET=0x100000000 -o %t 2>&1 | \
16+
# RUN: FileCheck %s --check-prefix=ERROR4
17+
# ERROR4: maximum object size of 4294967295 exceeded when writing symbols
18+
19+
# RUN: not yaml2obj %s -DRELOCOFFSET=0x100000000 -o %t 2>&1 | \
20+
# RUN: FileCheck %s --check-prefix=ERROR5
21+
# ERROR5: maximum object size (4294967295) exceeded when writing relocation data for section .text
22+
23+
# RUN: not yaml2obj %s -DDATARAWDATAOFFSET=0x100000000 -o %t 2>&1 | \
24+
# RUN: FileCheck %s --check-prefix=ERROR6
25+
# ERROR6: maximum object size (4294967295) exceeded when writing data for section 2 (.data)
26+
27+
--- !XCOFF
28+
FileHeader:
29+
MagicNumber: 0x1DF
30+
NumberOfSections: 2
31+
OffsetToSymbolTable: [[SYMTABOFFSET=0x7A]]
32+
EntriesInSymbolTable: 6
33+
AuxiliaryHeaderSize: 0
34+
Flags: 0x0
35+
Sections:
36+
- Name: .text
37+
Address: 0x0
38+
Size: 0x8
39+
FileOffsetToData: [[TEXTRAWDATAOFFSET=0x64]]
40+
FileOffsetToRelocations: [[RELOCOFFSET=0x70]]
41+
NumberOfRelocations: 0x1
42+
Flags: [ STYP_TEXT ]
43+
SectionData: 386000004BFFFFFC
44+
Relocations:
45+
- Address: 0x4
46+
Symbol: 0x2
47+
Info: 0x99
48+
Type: 0x1A
49+
- Name: .data
50+
Address: 0x0
51+
Size: 0x4
52+
FileOffsetToData: [[DATARAWDATAOFFSET=0x6C]]
53+
FileOffsetToRelocations: 0
54+
Flags: [ STYP_DATA ]
55+
SectionData: 3210ABCD
56+
Symbols:
57+
- Name: .bar
58+
Value: 0x0
59+
Section: N_UNDEF
60+
Type: 0x0
61+
StorageClass: C_EXT
62+
NumberOfAuxEntries: 1
63+
AuxEntries:
64+
- Type: AUX_CSECT
65+
SymbolAlignmentAndType: 0
66+
StorageMappingClass: XMC_PR
67+
SectionOrLength: 0
68+
- Name: ''
69+
Value: 0x0
70+
Section: .text
71+
Type: 0x0
72+
StorageClass: C_HIDEXT
73+
NumberOfAuxEntries: 1
74+
AuxEntries:
75+
- Type: AUX_CSECT
76+
SymbolAlignmentAndType: 17
77+
StorageMappingClass: XMC_PR
78+
SectionOrLength: 8
79+
- Name: .foo
80+
Value: 0x0
81+
Section: .text
82+
Type: 0x0
83+
StorageClass: C_EXT
84+
NumberOfAuxEntries: 1
85+
AuxEntries:
86+
- Type: AUX_CSECT
87+
SymbolAlignmentAndType: 2
88+
StorageMappingClass: XMC_PR
89+
SectionOrLength: 2
90+
StringTable: {}
91+
...

0 commit comments

Comments
 (0)