1## Check how we can use the "Excluded" key of the "SectionHeaderTable" tag to exclude
2## entries from the section header table.
3
4## Check we can use the "Excluded" key to omit a section from the section header table.
5## Check we do not include the name of the excluded section in the string table.
6# RUN: yaml2obj %s -DINCLUDED=.foo -DEXCLUDED=.bar --docnum=1 -o %t1
7# RUN: llvm-readelf --section-headers -p .shstrtab %t1 | \
8# RUN:   FileCheck %s -DSEC=.foo --check-prefixes=INCLUDE-SEC,INCLUDE-FOO
9# RUN: yaml2obj %s -DINCLUDED=.bar -DEXCLUDED=.foo --docnum=1 -o %t2
10# RUN: llvm-readelf --section-headers -p .shstrtab %t2 | \
11# RUN:   FileCheck %s -DSEC=.bar --check-prefixes=INCLUDE-SEC,INCLUDE-BAR
12
13# INCLUDE-SEC:      [Nr] Name
14# INCLUDE-SEC:      [ 1] [[SEC]]
15# INCLUDE-SEC-NEXT: [ 2] .strtab
16# INCLUDE-SEC-NEXT: [ 3] .shstrtab
17
18# INCLUDE-SEC:      String dump of section '.shstrtab':
19# INCLUDE-FOO-NEXT: [     1] .foo
20# INCLUDE-BAR-NEXT: [     1] .bar
21# INCLUDE-SEC-NEXT: [     6] .shstrtab
22# INCLUDE-SEC-NEXT: [    10] .strtab
23# INCLUDE-SEC-NOT:  {{.}}
24
25--- !ELF
26FileHeader:
27  Class: ELFCLASS64
28  Data:  ELFDATA2LSB
29  Type:  ET_REL
30Sections:
31  - Name: .foo
32    Type: SHT_PROGBITS
33  - Name: .bar
34    Type: SHT_PROGBITS
35SectionHeaderTable:
36  Sections:
37    - Name: [[INCLUDED]]
38    - Name: .strtab
39    - Name: .shstrtab
40  Excluded:
41    - Name: [[EXCLUDED]]
42
43## Check we report an error when a section is in both the "Sections" and "Excluded" lists at the same time.
44## Also check that we report an error if a section is missing from the lists.
45# RUN: not yaml2obj %s -DINCLUDED=.bar -DEXCLUDED=.strtab --docnum=1 -o /dev/null 2>&1 | \
46# RUN:   FileCheck %s --check-prefix=EXCLUDE-INCLUDED
47
48# EXCLUDE-INCLUDED: error: repeated section name: '.strtab' in the section header description
49# EXCLUDE-INCLUDED: error: section '.foo' should be present in the 'Sections' or 'Excluded' lists
50
51## Check we report an error when the `Excluded` key mentions an unknown section.
52# RUN: not yaml2obj %s -DINCLUDED=.bar -DEXCLUDED=.unknown --docnum=1 -o /dev/null 2>&1 | \
53# RUN:   FileCheck %s --check-prefix=EXCLUDE-UNKNOWN
54
55# EXCLUDE-UNKNOWN: error: section '.foo' should be present in the 'Sections' or 'Excluded' lists
56# EXCLUDE-UNKNOWN: error: section header contains undefined section '.unknown'
57
58## Check we report an error when the `Excluded` key mentions a section more than once.
59# RUN: not yaml2obj %s --docnum=2 -o /dev/null 2>&1 | FileCheck %s --check-prefix=EXCLUDE-TWICE
60
61# EXCLUDE-TWICE: error: repeated section name: '.strtab' in the section header description
62# EXCLUDE-TWICE: error: repeated section name: '.strtab' in the section header description
63
64--- !ELF
65FileHeader:
66  Class: ELFCLASS64
67  Data:  ELFDATA2LSB
68  Type:  ET_REL
69SectionHeaderTable:
70  Sections:
71    - Name: .strtab
72    - Name: .shstrtab
73  Excluded:
74    - Name: .strtab
75    - Name: .strtab
76
77## Check that we are able to exclude all sections, except the implicit
78## null section, with the use of the "Excluded" key.
79
80## Case A: the "Sections" key is present, but empty.
81# RUN: yaml2obj %s --docnum=3 -o %t3
82# RUN: llvm-readelf --section-headers %t3 | FileCheck %s --check-prefix=NO-SECTIONS
83
84# NO-SECTIONS:      There are 1 section headers, starting at offset 0x48:
85# NO-SECTIONS:      Section Headers:
86# NO-SECTIONS-NEXT:  [Nr] Name         Type Address          Off    Size   ES Flg Lk Inf Al
87# NO-SECTIONS-NEXT:  [ 0] <no-strings> NULL 0000000000000000 000000 000000 00      0   0  0
88
89--- !ELF
90FileHeader:
91  Class: ELFCLASS64
92  Data:  ELFDATA2LSB
93  Type:  ET_REL
94SectionHeaderTable:
95  Sections: []
96  Excluded:
97    - Name: .strtab
98    - Name: .shstrtab
99
100## Case B: the "Sections" key is not present.
101# RUN: yaml2obj %s --docnum=4 -o %t4
102# RUN: llvm-readelf --section-headers %t4 | FileCheck %s --check-prefix=NO-SECTIONS
103
104--- !ELF
105FileHeader:
106  Class: ELFCLASS64
107  Data:  ELFDATA2LSB
108  Type:  ET_REL
109SectionHeaderTable:
110  Excluded:
111    - Name: .strtab
112    - Name: .shstrtab
113
114## Check how we handle cases when a section is excluded, but its section index is needed.
115## The general rule is: when a section is explicitly linked with another section, which is
116## excluded, then we report an error. In the case when it is linked implicitly with an excluded
117## section, we use 0 as index value.
118
119## Case A: check we report an error when a regular section has a Link field which
120##         points to an excluded section.
121# RUN: not yaml2obj %s --docnum=5 -o /dev/null 2>&1 | \
122# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.foo -DTARGET=.bar
123
124# LINK: error: unable to link '[[SEC]]' to excluded section '[[TARGET]]'
125
126--- !ELF
127FileHeader:
128  Class: ELFCLASS64
129  Data:  ELFDATA2LSB
130  Type:  ET_REL
131Sections:
132  - Name: .foo
133    Type: SHT_PROGBITS
134    Link: .bar
135  - Name: .bar
136    Type: SHT_PROGBITS
137SectionHeaderTable:
138  Sections:
139    - Name: .foo
140    - Name: .strtab
141    - Name: .shstrtab
142  Excluded:
143    - Name: .bar
144
145## Case B.1: check we report an error when a symbol table section has a Link field which
146##           points to an excluded section.
147# RUN: not yaml2obj %s --docnum=6 -DNAME=.symtab -DTYPE=SHT_SYMTAB -o /dev/null 2>&1 | \
148# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.symtab -DTARGET=.foo
149# RUN: not yaml2obj %s --docnum=6 -DNAME=.dynsym -DTYPE=SHT_DYNSYM -o /dev/null 2>&1 | \
150# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.dynsym -DTARGET=.foo
151
152--- !ELF
153FileHeader:
154  Class: ELFCLASS64
155  Data:  ELFDATA2LSB
156  Type:  ET_DYN
157Sections:
158  - Name:  [[NAME]]
159    Type:  [[TYPE]]
160    Link:  .foo
161  - Name:  .foo
162    Type:  SHT_PROGBITS
163SectionHeaderTable:
164  Sections:
165    - Name: [[NAME]]
166    - Name: .strtab
167    - Name: .shstrtab
168  Excluded:
169    - Name: .foo
170
171## Case B.2: check we do not link .dynsym with .dynstr implicitly when the latter is excluded.
172# RUN: yaml2obj %s --docnum=7 -o %t5
173# RUN: llvm-readelf %t5 --section-headers | FileCheck %s --check-prefix=LINK-DYNSYM
174
175# LINK-DYNSYM: [Nr] Name    Type   Address          Off    Size   ES Flg Lk
176# LINK-DYNSYM: [ 1] .dynsym DYNSYM 0000000000000000 000040 000018 18   A  0
177
178--- !ELF
179FileHeader:
180  Class: ELFCLASS64
181  Data:  ELFDATA2LSB
182  Type:  ET_DYN
183Sections:
184  - Name:  .dynsym
185    Type:  SHT_DYNSYM
186  - Name:  .dynstr
187    Type:  SHT_PROGBITS
188SectionHeaderTable:
189  Sections:
190    - Name: .dynsym
191    - Name: .strtab
192    - Name: .shstrtab
193  Excluded:
194    - Name: .dynstr
195
196## Case B.3: check we do not link .symtab with .strtab implicitly when the latter is excluded.
197# RUN: yaml2obj %s --docnum=8 -o %t6
198# RUN: llvm-readelf %t6 --section-headers | FileCheck %s --check-prefix=LINK-SYMTAB
199
200# LINK-SYMTAB: [Nr] Name    Type   Address          Off    Size   ES Flg Lk Inf Al
201# LINK-SYMTAB: [ 1] .symtab SYMTAB 0000000000000000 000040 000018 18      0   1  0
202
203--- !ELF
204FileHeader:
205  Class: ELFCLASS64
206  Data:  ELFDATA2LSB
207  Type:  ET_DYN
208Sections:
209  - Name:  .symtab
210    Type:  SHT_SYMTAB
211  - Name:  .strtab
212    Type:  SHT_PROGBITS
213SectionHeaderTable:
214  Sections:
215    - Name: .symtab
216    - Name: .shstrtab
217  Excluded:
218    - Name: .strtab
219
220## Case C: check we report an error when a debug section has a Link field which
221##         points to an excluded section.
222# RUN: not yaml2obj %s --docnum=9 -o /dev/null 2>&1 | \
223# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.debug_unknown -DTARGET=.strtab
224
225--- !ELF
226FileHeader:
227  Class: ELFCLASS64
228  Data:  ELFDATA2LSB
229  Type:  ET_EXEC
230Sections:
231  - Name:  .debug_unknown
232    Type:  SHT_PROGBITS
233    Link:  .strtab
234SectionHeaderTable:
235  Sections:
236    - Name: .debug_unknown
237    - Name: .shstrtab
238  Excluded:
239    - Name: .strtab
240
241## Case D.1: check we report an error when a relocatable section has an Info field which
242##           points to an excluded section.
243# RUN: not yaml2obj %s --docnum=10 -o /dev/null 2>&1 | \
244# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.rela -DTARGET=.strtab
245
246--- !ELF
247FileHeader:
248  Class: ELFCLASS64
249  Data:  ELFDATA2LSB
250  Type:  ET_DYN
251Sections:
252  - Name: .rela
253    Type: SHT_RELA
254    Info: .strtab
255    Relocations: []
256SectionHeaderTable:
257  Sections:
258    - Name: .rela
259    - Name: .shstrtab
260  Excluded:
261    - Name: .strtab
262
263## Case D.2: check we report an error when the SHT_REL[A] section is linked
264##           with an excluded section explicitly.
265# RUN: not yaml2obj %s --docnum=11 -o /dev/null 2>&1 | \
266# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.rela -DTARGET=.symtab
267
268--- !ELF
269FileHeader:
270  Class: ELFCLASS64
271  Data:  ELFDATA2LSB
272  Type:  ET_DYN
273Sections:
274  - Name: .rela
275    Type: SHT_RELA
276    Link: .symtab
277    Relocations: []
278  - Name: .symtab
279    Type: SHT_PROGBITS
280SectionHeaderTable:
281  Sections:
282    - Name: .rela
283    - Name: .strtab
284    - Name: .shstrtab
285  Excluded:
286    - Name: .symtab
287
288## Case E: check we report an error when a symbol references an excluded section.
289# RUN: not yaml2obj %s --docnum=12 -o /dev/null 2>&1 | \
290# RUN:   FileCheck %s --check-prefix=SYMBOL-SECTION
291
292# SYMBOL-SECTION: error: excluded section referenced: '.foo' by symbol 'foo'
293
294--- !ELF
295FileHeader:
296  Class: ELFCLASS64
297  Data:  ELFDATA2LSB
298  Type:  ET_DYN
299Sections:
300  - Name: .foo
301    Type: SHT_PROGBITS
302Symbols:
303  - Name:    foo
304    Type:    STT_OBJECT
305    Section: .foo
306SectionHeaderTable:
307  Sections:
308    - Name: .symtab
309    - Name: .strtab
310    - Name: .shstrtab
311  Excluded:
312    - Name: .foo
313
314## Case F.1: check we report an error when a group section
315##           contains an excluded section member.
316# RUN: not yaml2obj %s --docnum=13 -o /dev/null 2>&1 | \
317# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.group -DTARGET=.strtab
318
319--- !ELF
320FileHeader:
321  Class: ELFCLASS64
322  Data:  ELFDATA2LSB
323  Type:  ET_REL
324Sections:
325  - Name: .group
326    Type: SHT_GROUP
327    Members:
328      - SectionOrType: .strtab
329SectionHeaderTable:
330  Sections:
331    - Name: .group
332    - Name: .shstrtab
333  Excluded:
334    - Name: .strtab
335
336## Case F.2: check we report an error when the group section is linked
337##           to an excluded section explicitly.
338# RUN: not yaml2obj %s --docnum=14 -o /dev/null 2>&1 | \
339# RUN:   FileCheck %s --check-prefix=LINK -DSEC=.group -DTARGET=.symtab
340
341--- !ELF
342FileHeader:
343  Class: ELFCLASS64
344  Data:  ELFDATA2LSB
345  Type:  ET_REL
346Sections:
347  - Name: .group
348    Type: SHT_GROUP
349    Link: .symtab
350    Members: []
351  - Name: .symtab
352    Type: SHT_SYMTAB
353SectionHeaderTable:
354  Sections:
355    - Name: .group
356    - Name: .strtab
357    - Name: .shstrtab
358  Excluded:
359    - Name: .symtab
360
361## Case G: check we do not link SHT_LLVM_CALL_GRAPH_PROFILE/SHT_LLVM_ADDRSIG/SHT_GROUP/SHT_REL[A] sections
362##         with .symtab implicitly when the latter is excluded.
363# RUN: yaml2obj %s --docnum=15 -o %t7
364# RUN: llvm-readelf %t7 --section-headers | FileCheck %s --check-prefix=LINK-IMPLICIT
365
366# LINK-IMPLICIT:      [Nr] Name          Type                    Address          Off    Size   ES Flg Lk Inf Al
367# LINK-IMPLICIT:      [ 1] .cgp          LLVM_CALL_GRAPH_PROFILE 0000000000000000 000040 000000 10      0   0  0
368# LINK-IMPLICIT-NEXT: [ 2] .llvm_addrsig LLVM_ADDRSIG            0000000000000000 000040 000000 00      0   0  0
369# LINK-IMPLICIT-NEXT: [ 3] .group        GROUP                   0000000000000000 000040 000000 04      0   0  0
370# LINK-IMPLICIT-NEXT: [ 4] .rela         RELA                    0000000000000000 000040 000000 18      0   0  0
371
372--- !ELF
373FileHeader:
374  Class: ELFCLASS64
375  Data:  ELFDATA2LSB
376  Type:  ET_DYN
377Sections:
378  - Name:    .cgp
379    Type:    SHT_LLVM_CALL_GRAPH_PROFILE
380    Content: ""
381  - Name:    .llvm_addrsig
382    Type:    SHT_LLVM_ADDRSIG
383    Content: ""
384  - Name: .group
385    Type: SHT_GROUP
386    Members: []
387  - Name: .rela
388    Type: SHT_RELA
389    Relocations: []
390  - Name: .symtab
391    Type: SHT_SYMTAB
392SectionHeaderTable:
393  Sections:
394    - Name: .cgp
395    - Name: .llvm_addrsig
396    - Name: .group
397    - Name: .rela
398    - Name: .strtab
399    - Name: .shstrtab
400  Excluded:
401    - Name: .symtab
402
403## Case H: check we do not link SHT_HASH/SHT_GNU_HASH sections with .dynsym
404##         implicitly when the latter is excluded.
405# RUN: yaml2obj %s --docnum=16 -o %t8
406# RUN: llvm-readelf %t8 --section-headers | FileCheck %s --check-prefix=LINK-HASH
407
408# LINK-HASH:      [Nr] Name      Type     Address          Off    Size   ES Flg Lk Inf Al
409# LINK-HASH:      [ 1] .hash     HASH     0000000000000000 000040 000000 04      0   0  0
410# LINK-HASH-NEXT: [ 2] .gnu_hash GNU_HASH 0000000000000000 000040 000000 00      0   0  0
411
412--- !ELF
413FileHeader:
414  Class: ELFCLASS64
415  Data:  ELFDATA2LSB
416  Type:  ET_DYN
417Sections:
418  - Name:    .hash
419    Type:    SHT_HASH
420    Content: ""
421  - Name:    .gnu_hash
422    Type:    SHT_GNU_HASH
423    Content: ""
424  - Name: .dynsym
425    Type: SHT_DYNSYM
426SectionHeaderTable:
427  Sections:
428    - Name: .hash
429    - Name: .gnu_hash
430    - Name: .strtab
431    - Name: .shstrtab
432  Excluded:
433    - Name: .dynsym
434
435## Case I: document the case when an excluded section is explicitly linked to another excluded section.
436##         We report an error in this case, because:
437##         1) It is a reasonable behavior, as it is perhaps usually a result of a mistake
438##            in a YAML description.
439##         2) Helps to keep the code simpler.
440# RUN: not yaml2obj %s --docnum=17 -o /dev/null 2>&1 | FileCheck %s --check-prefix=CROSS-LINK
441
442# CROSS-LINK:      error: unable to link '.foo' to excluded section '.bar'
443# CROSS-LINK-NEXT: error: unable to link '.bar' to excluded section '.foo'
444
445--- !ELF
446FileHeader:
447  Class: ELFCLASS64
448  Data:  ELFDATA2LSB
449  Type:  ET_REL
450Sections:
451  - Name: .foo
452    Type: SHT_PROGBITS
453    Link: .bar
454  - Name: .bar
455    Type: SHT_PROGBITS
456    Link: .foo
457SectionHeaderTable:
458  Sections:
459    - Name: .strtab
460    - Name: .shstrtab
461  Excluded:
462    - Name: .foo
463    - Name: .bar
464
465## Check we set e_shstrndx field to 0 when the section header string table is excluded.
466## Check that the e_shnum field is adjusted properly when a section is removed.
467# RUN: yaml2obj --docnum=18 %s -o %t9
468# RUN: llvm-readelf --file-headers %t9 | FileCheck %s --check-prefix=SHSTRTAB
469
470# SHSTRTAB: Number of section headers:         2
471# SHSTRTAB: Section header string table index: 0
472
473--- !ELF
474FileHeader:
475  Class: ELFCLASS64
476  Data:  ELFDATA2LSB
477  Type:  ET_REL
478SectionHeaderTable:
479  Sections:
480    - Name: .strtab
481  Excluded:
482    - Name: .shstrtab
483
484## Check we do not allow using "Excluded" together with "NoHeaders".
485# RUN: not yaml2obj %s --docnum=19 -DNOHEADERS=true -o /dev/null 2>&1 | FileCheck %s --check-prefix=NOHEADERS
486# RUN: not yaml2obj %s --docnum=19 -DNOHEADERS=false -o /dev/null 2>&1 | FileCheck %s --check-prefix=NOHEADERS
487# NOHEADERS: NoHeaders can't be used together with Sections/Excluded
488
489--- !ELF
490FileHeader:
491  Class: ELFCLASS64
492  Data:  ELFDATA2LSB
493  Type:  ET_REL
494SectionHeaderTable:
495  NoHeaders: [[NOHEADERS]]
496  Excluded:  []
497