Skip to content

Commit 65217a4

Browse files
committedJul 24, 2019
Revert "[yaml2obj] - Allow custom fields for the SHT_UNDEF sections."
It fails on macOS with the following error: https://reviews.llvm.org/D65140#1599522 llvm-svn: 366937
1 parent 63e5fb7 commit 65217a4

File tree

2 files changed

+55
-89
lines changed

2 files changed

+55
-89
lines changed
 

‎llvm/test/tools/yaml2obj/elf-custom-null-section.yaml

+35-71
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ Sections:
4242
Info: 0
4343
Address: 0x0
4444

45-
## Check we can redefine fields of the first SHT_NULL section.
45+
## Check we are still able to describe other sections too.
4646

4747
# RUN: yaml2obj --docnum=3 %s -o %t3
48-
# RUN: llvm-readelf --sections %t3 | FileCheck %s --check-prefix=REDEFINE
48+
# RUN: llvm-readelf --sections %t3 | FileCheck %s --check-prefix=OTHER-SECTION
4949

50-
# REDEFINE: Section Headers:
51-
# REDEFINE-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
52-
# REDEFINE-NEXT: [ 0] .foo NULL 0000000000000006 000000 000002 03 A 4 5 1
50+
# OTHER-SECTION: Section Headers:
51+
# OTHER-SECTION-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
52+
# OTHER-SECTION-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0
53+
# OTHER-SECTION-NEXT: [ 1] foo PROGBITS 0000000000000000 000180 000000 00 0 0 0
54+
# OTHER-SECTION-NEXT: [ 2] .symtab SYMTAB 0000000000000000 000180 000018 18 3 1 8
55+
# OTHER-SECTION-NEXT: [ 3] .strtab STRTAB 0000000000000000 000198 000001 00 0 0 1
56+
# OTHER-SECTION-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 000199 00001f 00 0 0 1
5357

5458
--- !ELF
5559
FileHeader:
@@ -59,25 +63,23 @@ FileHeader:
5963
Machine: EM_X86_64
6064
Sections:
6165
- Type: SHT_NULL
62-
Name: .foo
63-
Flags: [ SHF_ALLOC ]
64-
AddressAlign: 0x1
65-
Size: 0x2
66-
EntSize: 0x3
67-
Link: 4
68-
Info: 5
69-
Address: 0x6
70-
71-
## Check that file size does not change if we redefine the Size
72-
## of the first SHT_NULL section.
66+
Name: ''
67+
Flags: [ ]
68+
AddressAlign: 0x0
69+
Size: 0x0
70+
EntSize: 0x0
71+
Link: 0
72+
- Type: SHT_PROGBITS
73+
Name: 'foo'
74+
75+
## Check we can redefine sh_size and sh_link fields of the SHT_NULL section.
7376

7477
# RUN: yaml2obj --docnum=4 %s -o %t4
75-
# RUN: stat %t3 > %t.txt
76-
# RUN: stat %t4 >> %t.txt
77-
# RUN: FileCheck %s --input-file=%t.txt --check-prefix=SIZE
78+
# RUN: llvm-readelf --sections %t4 | FileCheck %s --check-prefix=REDEFINE
7879

79-
# SIZE: Size: [[FILESIZE:.*]]
80-
# SIZE: Size: [[FILESIZE]]
80+
# REDEFINE: Section Headers:
81+
# REDEFINE-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
82+
# REDEFINE-NEXT: [ 0] NULL 0000000000000000 000000 000123 00 1 0 0
8183

8284
--- !ELF
8385
FileHeader:
@@ -86,28 +88,16 @@ FileHeader:
8688
Type: ET_REL
8789
Machine: EM_X86_64
8890
Sections:
89-
- Type: SHT_NULL
90-
Name: .foo
91-
Flags: [ SHF_ALLOC ]
92-
AddressAlign: 0x1
93-
Size: 0xFFFF
94-
EntSize: 0x3
95-
Link: 4
96-
Info: 5
97-
Address: 0x6
91+
- Type: SHT_NULL
92+
Link: .foo
93+
Size: 0x123
94+
- Type: SHT_PROGBITS
95+
Name: .foo
9896

99-
## Check we are still able to describe other sections too.
97+
## The same as above, but using a number as a Link value.
10098

10199
# RUN: yaml2obj --docnum=5 %s -o %t5
102-
# RUN: llvm-readelf --sections %t5 | FileCheck %s --check-prefix=OTHER-SECTION
103-
104-
# OTHER-SECTION: Section Headers:
105-
# OTHER-SECTION-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
106-
# OTHER-SECTION-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0
107-
# OTHER-SECTION-NEXT: [ 1] foo PROGBITS 0000000000000000 000180 000000 00 0 0 0
108-
# OTHER-SECTION-NEXT: [ 2] .symtab SYMTAB 0000000000000000 000180 000018 18 3 1 8
109-
# OTHER-SECTION-NEXT: [ 3] .strtab STRTAB 0000000000000000 000198 000001 00 0 0 1
110-
# OTHER-SECTION-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 000199 00001f 00 0 0 1
100+
# RUN: llvm-readelf --sections %t5 | FileCheck %s --check-prefix=REDEFINE
111101

112102
--- !ELF
113103
FileHeader:
@@ -116,15 +106,11 @@ FileHeader:
116106
Type: ET_REL
117107
Machine: EM_X86_64
118108
Sections:
119-
- Type: SHT_NULL
120-
Name: ''
121-
Flags: [ ]
122-
AddressAlign: 0x0
123-
Size: 0x0
124-
EntSize: 0x0
125-
Link: 0
109+
- Type: SHT_NULL
110+
Link: 1
111+
Size: 0x123
126112
- Type: SHT_PROGBITS
127-
Name: 'foo'
113+
Name: .foo
128114

129115
## Check we report an error if null section sh_link field refers to an unknown section.
130116

@@ -164,7 +150,7 @@ Sections:
164150
# MULTIPLE: Section Headers:
165151
# MULTIPLE-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
166152
# MULTIPLE-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0
167-
# MULTIPLE-NEXT: [ 1] .foo NULL 0000000000000123 000180 000020 10 A 1 2 0
153+
# MULTIPLE-NEXT: [ 1] NULL 0000000000000123 000180 000020 10 A 1 2 0
168154

169155
--- !ELF
170156
FileHeader:
@@ -175,31 +161,9 @@ FileHeader:
175161
Sections:
176162
- Type: SHT_NULL
177163
- Type: SHT_NULL
178-
Name: .foo
179164
Flags: [ SHF_ALLOC ]
180165
Size: 0x20
181166
EntSize: 0x10
182167
Link: 1
183168
Info: 2
184169
Address: 0x123
185-
186-
## Check we can override the sh_offset/sh_size fields of the first SHT_NULL section if requested.
187-
188-
# RUN: yaml2obj --docnum=9 %s -o %t9
189-
# RUN: llvm-readelf --sections %t9 | FileCheck %s --check-prefix=OVERRIDE
190-
191-
# OVERRIDE: Section Headers:
192-
# OVERRIDE-NEXT: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
193-
# OVERRIDE-NEXT: [ 0] NULL 0000000000000000 000007 000008 00 0 0 0
194-
195-
--- !ELF
196-
FileHeader:
197-
Class: ELFCLASS64
198-
Data: ELFDATA2LSB
199-
Type: ET_REL
200-
Machine: EM_X86_64
201-
Sections:
202-
- Type: SHT_NULL
203-
Size: 0x2
204-
ShOffset: 0x7
205-
ShSize: 0x8

‎llvm/tools/yaml2obj/yaml2elf.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
247247
? (typename ELFT::uint)(*Doc.Header.SHOffset)
248248
: sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
249249
Header.e_shnum =
250-
Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size();
250+
Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : SN2I.size() + 1;
251251
Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
252252
: SN2I.get(".shstrtab");
253253
}
@@ -327,15 +327,30 @@ bool ELFState<ELFT>::initSectionHeaders(ELFState<ELFT> &State,
327327
SHeaders.resize(Doc.Sections.size());
328328

329329
for (size_t I = 0; I < Doc.Sections.size(); ++I) {
330+
Elf_Shdr &SHeader = SHeaders[I];
330331
ELFYAML::Section *Sec = Doc.Sections[I].get();
331-
if (I == 0 && Sec->IsImplicit)
332+
333+
if (I == 0) {
334+
if (Sec->IsImplicit)
335+
continue;
336+
337+
if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec))
338+
if (S->Size)
339+
SHeader.sh_size = *S->Size;
340+
341+
if (!Sec->Link.empty()) {
342+
unsigned Index;
343+
if (!convertSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
344+
return false;
345+
SHeader.sh_link = Index;
346+
}
332347
continue;
348+
}
333349

334350
// We have a few sections like string or symbol tables that are usually
335351
// added implicitly to the end. However, if they are explicitly specified
336352
// in the YAML, we need to write them here. This ensures the file offset
337353
// remains correct.
338-
Elf_Shdr &SHeader = SHeaders[I];
339354
if (initImplicitHeader(State, CBA, SHeader, Sec->Name,
340355
Sec->IsImplicit ? nullptr : Sec))
341356
continue;
@@ -357,17 +372,7 @@ bool ELFState<ELFT>::initSectionHeaders(ELFState<ELFT> &State,
357372
SHeader.sh_link = Index;
358373
}
359374

360-
if (I == 0) {
361-
if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
362-
// We do not write any content for special SHN_UNDEF section.
363-
if (RawSec->Size)
364-
SHeader.sh_size = *RawSec->Size;
365-
if (RawSec->Info)
366-
SHeader.sh_info = *RawSec->Info;
367-
}
368-
if (Sec->EntSize)
369-
SHeader.sh_entsize = *Sec->EntSize;
370-
} else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
375+
if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
371376
if (!writeSectionContent(SHeader, *S, CBA))
372377
return false;
373378
} else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
@@ -961,11 +966,8 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
961966
}
962967

963968
template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
964-
for (unsigned I = 0, E = Doc.Sections.size(); I != E; ++I) {
969+
for (unsigned I = 1, E = Doc.Sections.size(); I != E; ++I) {
965970
StringRef Name = Doc.Sections[I]->Name;
966-
if (Name.empty())
967-
continue;
968-
969971
DotShStrtab.add(dropUniqueSuffix(Name));
970972
if (!SN2I.addName(Name, I)) {
971973
WithColor::error() << "Repeated section name: '" << Name

0 commit comments

Comments
 (0)
Please sign in to comment.