Skip to content

Commit 1480229

Browse files
author
George Rimar
committed
Recommit "rL366894: [yaml2obj] - Allow custom fields for the SHT_UNDEF sections."
With fix: do not use `stat` tool. Original commit message: This is a follow-up refactoring patch for recently introduced functionality which which reduces the code duplication and also makes possible to redefine all possible fields of the first SHT_NULL section (previously it was only possible to set sh_link and sh_size). Differential revision: https://reviews.llvm.org/D65140 llvm-svn: 367003
1 parent 2e95941 commit 1480229

File tree

2 files changed

+89
-55
lines changed

2 files changed

+89
-55
lines changed

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

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

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

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

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
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
5753

5854
--- !ELF
5955
FileHeader:
@@ -63,23 +59,25 @@ FileHeader:
6359
Machine: EM_X86_64
6460
Sections:
6561
- Type: SHT_NULL
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.
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.
7673

7774
# RUN: yaml2obj --docnum=4 %s -o %t4
78-
# RUN: llvm-readelf --sections %t4 | FileCheck %s --check-prefix=REDEFINE
75+
# RUN: ls -l %t3 | tr -s ' ' | cut -d ' ' -f 5 > %t.txt
76+
# RUN: ls -l %t4 | tr -s ' ' | cut -d ' ' -f 5 >> %t.txt
77+
# RUN: FileCheck %s --input-file=%t.txt --check-prefix=SIZE
7978

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
79+
# SIZE: [[FILESIZE:.*]]
80+
# SIZE: [[FILESIZE]]
8381

8482
--- !ELF
8583
FileHeader:
@@ -88,16 +86,28 @@ FileHeader:
8886
Type: ET_REL
8987
Machine: EM_X86_64
9088
Sections:
91-
- Type: SHT_NULL
92-
Link: .foo
93-
Size: 0x123
94-
- Type: SHT_PROGBITS
95-
Name: .foo
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
9698

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

99101
# RUN: yaml2obj --docnum=5 %s -o %t5
100-
# RUN: llvm-readelf --sections %t5 | FileCheck %s --check-prefix=REDEFINE
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
101111

102112
--- !ELF
103113
FileHeader:
@@ -106,11 +116,15 @@ FileHeader:
106116
Type: ET_REL
107117
Machine: EM_X86_64
108118
Sections:
109-
- Type: SHT_NULL
110-
Link: 1
111-
Size: 0x123
119+
- Type: SHT_NULL
120+
Name: ''
121+
Flags: [ ]
122+
AddressAlign: 0x0
123+
Size: 0x0
124+
EntSize: 0x0
125+
Link: 0
112126
- Type: SHT_PROGBITS
113-
Name: .foo
127+
Name: 'foo'
114128

115129
## Check we report an error if null section sh_link field refers to an unknown section.
116130

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

155169
--- !ELF
156170
FileHeader:
@@ -161,9 +175,31 @@ FileHeader:
161175
Sections:
162176
- Type: SHT_NULL
163177
- Type: SHT_NULL
178+
Name: .foo
164179
Flags: [ SHF_ALLOC ]
165180
Size: 0x20
166181
EntSize: 0x10
167182
Link: 1
168183
Info: 2
169184
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

Lines changed: 18 additions & 20 deletions
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 : SN2I.size() + 1;
250+
Doc.Header.SHNum ? (uint16_t)*Doc.Header.SHNum : Doc.Sections.size();
251251
Header.e_shstrndx = Doc.Header.SHStrNdx ? (uint16_t)*Doc.Header.SHStrNdx
252252
: SN2I.get(".shstrtab");
253253
}
@@ -327,30 +327,15 @@ 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];
331330
ELFYAML::Section *Sec = Doc.Sections[I].get();
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-
}
331+
if (I == 0 && Sec->IsImplicit)
347332
continue;
348-
}
349333

350334
// We have a few sections like string or symbol tables that are usually
351335
// added implicitly to the end. However, if they are explicitly specified
352336
// in the YAML, we need to write them here. This ensures the file offset
353337
// remains correct.
338+
Elf_Shdr &SHeader = SHeaders[I];
354339
if (initImplicitHeader(State, CBA, SHeader, Sec->Name,
355340
Sec->IsImplicit ? nullptr : Sec))
356341
continue;
@@ -372,7 +357,17 @@ bool ELFState<ELFT>::initSectionHeaders(ELFState<ELFT> &State,
372357
SHeader.sh_link = Index;
373358
}
374359

375-
if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
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)) {
376371
if (!writeSectionContent(SHeader, *S, CBA))
377372
return false;
378373
} else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) {
@@ -966,8 +961,11 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
966961
}
967962

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

0 commit comments

Comments
 (0)