xref: /llvm-project/llvm/test/tools/llvm-readobj/ELF/malformed-pt-dynamic.test (revision 9c89dcf80736a7c0710dc4c237ec35f0687e1efd)
1## If the offset and/or size fields of the PT_DYNAMIC field become corrupted,
2## we should report a sensible message.
3
4## Case A: Test case where the size of the PT_DYNAMIC header is too large to fit in the file,
5##         but the start is within the file.
6
7## Case A.1: the section header table is present in the object. Check that we report a warning about the
8##           broken PT_DYNAMIC header, check we dump the dynamic table.
9# RUN: yaml2obj %s -DFILESIZE=0x131 -o %t1
10# RUN: llvm-readobj %t1 --dynamic-table 2>&1 | FileCheck -DFILE=%t1 %s --check-prefixes=WARN1,WARN1-LLVM
11# RUN: llvm-readelf %t1 --dynamic-table 2>&1 | FileCheck -DFILE=%t1 %s --check-prefixes=WARN1,WARN1-GNU
12
13# WARN1: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1000) + file size (0x131) exceeds the size of the file (0x1130)
14
15# WARN1-LLVM:      DynamicSection [ (1 entries)
16# WARN1-LLVM-NEXT:   Tag                Type Name/Value
17# WARN1-LLVM-NEXT:   0x0000000000000000 NULL 0x0
18# WARN1-LLVM-NEXT: ]
19
20# WARN1-GNU:       Dynamic section at offset 0x1000 contains 1 entries:
21# WARN1-GNU-NEXT:   Tag                Type   Name/Value
22# WARN1-GNU-NEXT:   0x0000000000000000 (NULL) 0x0
23
24## Case A.2: in this case we drop section headers. The dynamic table is not dumped.
25# RUN: yaml2obj %s -DFILESIZE=0x12 -DNOHEADERS=true -o %t1.noheaders
26# RUN: llvm-readobj %t1.noheaders --dynamic-table 2>&1 | FileCheck -DFILE=%t1.noheaders %s \
27# RUN:   --check-prefix=WARN1-NOHEADERS --implicit-check-not="DynamicSection ["
28# RUN: llvm-readelf %t1.noheaders --dynamic-table 2>&1 | FileCheck -DFILE=%t1.noheaders %s \
29# RUN:   --check-prefix=WARN1-NOHEADERS --implicit-check-not="Dynamic section"
30
31# WARN1-NOHEADERS: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1000) + file size (0x12) exceeds the size of the file (0x1011)
32
33## Case B: Test case where the offset of the PT_DYNAMIC header is too large to be in the file.
34
35## Case B.1: the section header table is present in the object. Check that we report a warning about the
36##           broken PT_DYNAMIC header. Check we also report a warning about broken fields of the SHT_DYNAMIC section.
37# RUN: yaml2obj %s -DOFFSET=0x1131 -o %t2
38# RUN: llvm-readobj %t2 --dynamic-table 2>&1 | FileCheck -DFILE=%t2 %s \
39# RUN:   --check-prefix=WARN2 --implicit-check-not=warning:
40# RUN: llvm-readelf %t2 --dynamic-table 2>&1 | FileCheck -DFILE=%t2 %s \
41# RUN:   --check-prefix=WARN2 --implicit-check-not=warning:
42
43# WARN2: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1131) + file size (0x10) exceeds the size of the file (0x1130)
44# WARN2: warning: '[[FILE]]': unable to read the dynamic table from SHT_DYNAMIC section with index 1: offset (0x1131) + size (0x10) is greater than the file size (0x1130)
45# WARN2: warning: '[[FILE]]': no valid dynamic table was found
46
47## Case B.2: in this case we drop section headers. The dynamic table is not dumped.
48# RUN: yaml2obj %s -DOFFSET=0x1112 -DNOHEADERS=true -o %t2.noheaders
49# RUN: llvm-readobj %t2.noheaders --dynamic-table 2>&1 | FileCheck -DFILE=%t2.noheaders %s \
50# RUN:   --check-prefix=WARN2-NOHEADERS --implicit-check-not="DynamicSection ["
51# RUN: llvm-readelf %t2.noheaders --dynamic-table 2>&1 | FileCheck -DFILE=%t2.noheaders %s \
52# RUN:   --check-prefix=WARN2-NOHEADERS --implicit-check-not="Dynamic section"
53
54# WARN2-NOHEADERS: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1112) + file size (0x10) exceeds the size of the file (0x1011)
55
56## Case C: test we report a warning when the offset + the file size of the PT_DYNAMIC is so large a
57##         value that it overflows the platform address size type. Check we also report a warning about
58##         broken fields of the SHT_DYNAMIC section.
59
60# RUN: yaml2obj %s -DOFFSET=0xffffffffffffffff -o %t3
61# RUN: llvm-readobj %t3 --dynamic-table 2>&1 | FileCheck -DFILE=%t3 %s \
62# RUN:   --check-prefix=WARN3 --implicit-check-not=warning:
63# RUN: llvm-readelf %t3 --dynamic-table 2>&1 | FileCheck -DFILE=%t3 %s \
64# RUN:   --check-prefix=WARN3 --implicit-check-not=warning:
65
66# WARN3: warning: '[[FILE]]': PT_DYNAMIC segment offset (0xffffffffffffffff) + file size (0x10) exceeds the size of the file (0x1130)
67# WARN3: warning: '[[FILE]]': unable to read the dynamic table from SHT_DYNAMIC section with index 1: offset (0xffffffffffffffff) + size (0x10) is greater than the file size (0x1130)
68# WARN3: warning: '[[FILE]]': no valid dynamic table was found
69
70# RUN: yaml2obj %s -DNOHEADERS=true -DOFFSET=0xffffffffffffffff -o %t3.noheaders
71# RUN: llvm-readobj %t3.noheaders --dynamic-table 2>&1 | \
72# RUN:   FileCheck -DFILE=%t3.noheaders %s --check-prefix=WARN3-NOHEADERS
73# RUN: llvm-readelf %t3.noheaders --dynamic-table 2>&1 | \
74# RUN:   FileCheck -DFILE=%t3.noheaders %s --check-prefix=WARN3-NOHEADERS
75
76# WARN3-NOHEADERS: warning: '[[FILE]]': PT_DYNAMIC segment offset (0xffffffffffffffff) + file size (0x10) exceeds the size of the file (0x1011)
77
78# RUN: yaml2obj %s -DFILESIZE=0xffffffffffffffff -o %t4
79# RUN: llvm-readobj %t4 --dynamic-table 2>&1 | FileCheck -DFILE=%t4 %s --check-prefix=WARN4
80# RUN: llvm-readelf %t4 --dynamic-table 2>&1 | FileCheck -DFILE=%t4 %s --check-prefix=WARN4
81
82# WARN4: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1000) + file size (0xffffffffffffffff) exceeds the size of the file (0x1130)
83
84# RUN: yaml2obj %s -DNOHEADERS=true -DFILESIZE=0xffffffffffffffff -o %t4.noheaders
85# RUN: llvm-readobj %t4.noheaders --dynamic-table 2>&1 | \
86# RUN:   FileCheck -DFILE=%t4.noheaders %s --check-prefix=WARN4-NOHEADERS
87# RUN: llvm-readelf %t4.noheaders --dynamic-table 2>&1 | \
88# RUN:   FileCheck -DFILE=%t4.noheaders %s --check-prefix=WARN4-NOHEADERS
89
90# WARN4-NOHEADERS: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1000) + file size (0xffffffffffffffff) exceeds the size of the file (0x1011)
91
92## Case D: the same as "Case C", but for a 32-bit object.
93
94# RUN: yaml2obj %s -DBITS=32 -DOFFSET=0xffffffff -o %t5
95# RUN: llvm-readobj %t5 --dynamic-table 2>&1 | FileCheck -DFILE=%t5 %s \
96# RUN:   --check-prefix=WARN5 --implicit-check-not=warning:
97# RUN: llvm-readelf %t5 --dynamic-table 2>&1 | FileCheck -DFILE=%t5 %s \
98# RUN:   --check-prefix=WARN5 --implicit-check-not=warning:
99
100# WARN5: warning: '[[FILE]]': PT_DYNAMIC segment offset (0xffffffff) + file size (0x8) exceeds the size of the file (0x10c8)
101# WARN5: warning: '[[FILE]]': unable to read the dynamic table from SHT_DYNAMIC section with index 1: offset (0xffffffff) + size (0x8) is greater than the file size (0x10c8)
102# WARN5: warning: '[[FILE]]': no valid dynamic table was found
103
104# RUN: yaml2obj %s -DNOHEADERS=true -DBITS=32 -DOFFSET=0xffffffff -o %t5.noheaders
105# RUN: llvm-readobj %t5.noheaders --dynamic-table 2>&1 | \
106# RUN:   FileCheck -DFILE=%t5.noheaders %s --check-prefix=WARN5-NOHEADERS
107# RUN: llvm-readelf %t5.noheaders --dynamic-table 2>&1 | \
108# RUN:   FileCheck -DFILE=%t5.noheaders %s --check-prefix=WARN5-NOHEADERS
109
110# WARN5-NOHEADERS: warning: '[[FILE]]': PT_DYNAMIC segment offset (0xffffffff) + file size (0x8) exceeds the size of the file (0x1009)
111
112# RUN: yaml2obj %s -DBITS=32 -DFILESIZE=0xffffffff -o %t6
113# RUN: llvm-readobj %t6 --dynamic-table 2>&1 | FileCheck -DFILE=%t6 %s --check-prefix=WARN6
114# RUN: llvm-readelf %t6 --dynamic-table 2>&1 | FileCheck -DFILE=%t6 %s --check-prefix=WARN6
115
116# WARN6: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1000) + file size (0xffffffff) exceeds the size of the file (0x10c8)
117
118# RUN: yaml2obj %s -DNOHEADERS=true -DBITS=32 -DFILESIZE=0xffffffff -o %t6.noheaders
119# RUN: llvm-readobj %t6.noheaders --dynamic-table 2>&1 | \
120# RUN:   FileCheck -DFILE=%t6.noheaders %s --check-prefix=WARN6-NOHEADERS
121# RUN: llvm-readelf %t6.noheaders --dynamic-table 2>&1 | \
122# RUN:   FileCheck -DFILE=%t6.noheaders %s --check-prefix=WARN6-NOHEADERS
123
124# WARN6-NOHEADERS: warning: '[[FILE]]': PT_DYNAMIC segment offset (0x1000) + file size (0xffffffff) exceeds the size of the file (0x1009)
125
126--- !ELF
127FileHeader:
128  Class: ELFCLASS[[BITS=64]]
129  Data:  ELFDATA2LSB
130  Type:  ET_EXEC
131Sections:
132  - Name:     .dynamic
133    Type:     SHT_DYNAMIC
134    Address:  0x1000
135    Offset:   0x1000
136    ShOffset: [[OFFSET=<none>]]
137    Entries:
138      - Tag:   DT_NULL
139        Value: 0
140  - Type: SectionHeaderTable
141    NoHeaders: [[NOHEADERS=false]]
142ProgramHeaders:
143  - Type:     PT_DYNAMIC
144    FileSize: [[FILESIZE=<none>]]
145    FirstSec: .dynamic
146    LastSec:  .dynamic
147