Skip to content

Commit 2bfaf19

Browse files
author
Georgii Rymar
committed
[yaml2obj] - Make Section::Link field to be Optional<>.
`Link` is not an optional field currently. Because of this it is not convenient to write macros. This makes it optional and fixes corresponding test cases. Differential revision: https://reviews.llvm.org/D90390
1 parent 13bfd89 commit 2bfaf19

File tree

10 files changed

+29
-30
lines changed

10 files changed

+29
-30
lines changed

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ struct Section : public Chunk {
174174
ELF_SHT Type;
175175
Optional<ELF_SHF> Flags;
176176
Optional<llvm::yaml::Hex64> Address;
177-
StringRef Link;
177+
Optional<StringRef> Link;
178178
llvm::yaml::Hex64 AddressAlign;
179179
Optional<llvm::yaml::Hex64> EntSize;
180180

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
694694

695695
assignSectionAddress(SHeader, Sec);
696696

697-
if (!Sec->Link.empty())
698-
SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name);
697+
if (Sec->Link)
698+
SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
699699

700700
if (IsFirstUndefSection) {
701701
if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
@@ -866,10 +866,10 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
866866
else
867867
SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
868868

869-
if (RawSec && !RawSec->Link.empty()) {
869+
if (RawSec && RawSec->Link) {
870870
// If the Link field is explicitly defined in the document,
871871
// we should use it.
872-
SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name);
872+
SHeader.sh_link = toSectionIndex(*RawSec->Link, RawSec->Name);
873873
} else {
874874
// When we describe the .dynsym section in the document explicitly, it is
875875
// allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not
@@ -1023,8 +1023,8 @@ void ELFState<ELFT>::initDWARFSectionHeader(Elf_Shdr &SHeader, StringRef Name,
10231023
else if (Name == ".debug_str")
10241024
SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
10251025

1026-
if (YAMLSec && !YAMLSec->Link.empty())
1027-
SHeader.sh_link = toSectionIndex(YAMLSec->Link, Name);
1026+
if (YAMLSec && YAMLSec->Link)
1027+
SHeader.sh_link = toSectionIndex(*YAMLSec->Link, Name);
10281028

10291029
assignSectionAddress(SHeader, YAMLSec);
10301030
}
@@ -1180,7 +1180,7 @@ void ELFState<ELFT>::writeSectionContent(
11801180

11811181
// For relocation section set link to .symtab by default.
11821182
unsigned Link = 0;
1183-
if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
1183+
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
11841184
SN2I.lookup(".symtab", Link))
11851185
SHeader.sh_link = Link;
11861186

@@ -1191,9 +1191,9 @@ void ELFState<ELFT>::writeSectionContent(
11911191
return;
11921192

11931193
for (const ELFYAML::Relocation &Rel : *Section.Relocations) {
1194-
unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name,
1195-
Section.Link == ".dynsym")
1196-
: 0;
1194+
const bool IsDynamic = Section.Link && (*Section.Link == ".dynsym");
1195+
unsigned SymIdx =
1196+
Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, IsDynamic) : 0;
11971197
if (IsRela) {
11981198
Elf_Rela REntry;
11991199
zero(REntry);
@@ -1261,7 +1261,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
12611261
"Section type is not SHT_GROUP");
12621262

12631263
unsigned Link = 0;
1264-
if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
1264+
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
12651265
SN2I.lookup(".symtab", Link))
12661266
SHeader.sh_link = Link;
12671267

@@ -1379,7 +1379,7 @@ void ELFState<ELFT>::writeSectionContent(
13791379
SHeader.sh_entsize = 16;
13801380

13811381
unsigned Link = 0;
1382-
if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
1382+
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
13831383
SN2I.lookup(".symtab", Link))
13841384
SHeader.sh_link = Link;
13851385

@@ -1402,7 +1402,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
14021402
const ELFYAML::HashSection &Section,
14031403
ContiguousBlobAccumulator &CBA) {
14041404
unsigned Link = 0;
1405-
if (Section.Link.empty() && !ExcludedSectionHeaders.count(".dynsym") &&
1405+
if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
14061406
SN2I.lookup(".dynsym", Link))
14071407
SHeader.sh_link = Link;
14081408

@@ -1592,7 +1592,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
15921592
const ELFYAML::AddrsigSection &Section,
15931593
ContiguousBlobAccumulator &CBA) {
15941594
unsigned Link = 0;
1595-
if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
1595+
if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
15961596
SN2I.lookup(".symtab", Link))
15971597
SHeader.sh_link = Link;
15981598

@@ -1653,7 +1653,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
16531653
const ELFYAML::GnuHashSection &Section,
16541654
ContiguousBlobAccumulator &CBA) {
16551655
unsigned Link = 0;
1656-
if (Section.Link.empty() && !ExcludedSectionHeaders.count(".dynsym") &&
1656+
if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
16571657
SN2I.lookup(".dynsym", Link))
16581658
SHeader.sh_link = Link;
16591659

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
11011101
IO.mapRequired("Type", Section.Type);
11021102
IO.mapOptional("Flags", Section.Flags);
11031103
IO.mapOptional("Address", Section.Address);
1104-
IO.mapOptional("Link", Section.Link, StringRef());
1104+
IO.mapOptional("Link", Section.Link);
11051105
IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
11061106
IO.mapOptional("EntSize", Section.EntSize);
11071107
IO.mapOptional("Offset", Section.Offset);

llvm/test/tools/llvm-readobj/ELF/relr-relocs.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Sections:
151151
0x000F0501, 0x50400009 ]
152152
EntSize: [[ENTSIZE=<none>]]
153153
ShType: [[SHTYPE=<none>]]
154-
Link: [[LINK=""]]
154+
Link: [[LINK=<none>]]
155155

156156
## Check we report a warning when we are unable to dump relocations
157157
## for a SHT_RELR/SHT_ANDROID_RELR section.

llvm/test/tools/obj2yaml/ELF/DWARF/debug-addr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Sections:
165165
- Name: .debug_addr
166166
Type: [[TYPE=SHT_PROGBITS]]
167167
Flags: [[FLAGS=<none>]]
168-
Link: [[LINK='']]
168+
Link: [[LINK=<none>]]
169169
EntSize: [[ENTSIZE=<none>]]
170170
Info: [[INFO=<none>]]
171171
AddressAlign: [[ADDRALIGN=0]]

llvm/test/tools/obj2yaml/ELF/DWARF/debug-aranges.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Sections:
105105
- Name: .debug_aranges
106106
Type: [[TYPE=SHT_PROGBITS]]
107107
Flags: [[FLAGS=<none>]]
108-
Link: [[LINK='']]
108+
Link: [[LINK=<none>]]
109109
EntSize: [[ENTSIZE=<none>]]
110110
Info: [[INFO=<none>]]
111111
AddressAlign: [[ADDRALIGN=0]]

llvm/test/tools/obj2yaml/ELF/DWARF/debug-ranges.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Sections:
216216
- Name: .debug_ranges
217217
Type: [[TYPE=SHT_PROGBITS]]
218218
Flags: [[FLAGS=<none>]]
219-
Link: [[LINK='']]
219+
Link: [[LINK=<none>]]
220220
EntSize: [[ENTSIZE=<none>]]
221221
Info: [[INFO=<none>]]
222222
AddressAlign: [[ADDRALIGN=0]]

llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Sections:
5656
- Name: .debug_str
5757
Type: SHT_[[TYPE=PROGBITS]]
5858
Flags: [[FLAGS=<none>]]
59-
Link: [[LINK='']]
59+
Link: [[LINK=<none>]]
6060
EntSize: [[ENTSIZE=1]]
6161
Info: [[INFO=<none>]]
6262
AddressAlign: [[ADDRALIGN=1]]

llvm/test/tools/yaml2obj/ELF/dynsym-section.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Sections:
3434

3535
## Check we are able to set Link = 0 for the .dynsym section explicitly.
3636

37-
# RUN: yaml2obj %s --docnum=2 -DLINK="Link: 0" -o %t2
37+
# RUN: yaml2obj %s --docnum=2 -DLINK=0 -o %t2
3838
# RUN: llvm-readelf --section-headers %t2 | FileCheck %s --check-prefix=LINK-NULL
3939

4040
# LINK-NULL: [Nr] Name {{.*}} Flg Lk Inf
@@ -48,7 +48,7 @@ FileHeader:
4848
Sections:
4949
- Name: .dynsym
5050
Type: SHT_DYNSYM
51-
[[LINK]]
51+
Link: [[LINK=<none>]]
5252
- Name: .dynstr
5353
Type: SHT_STRTAB
5454
- Name: .foo
@@ -57,7 +57,7 @@ Sections:
5757
## Check that by default the .dynsym section will be linked to the .dynstr section,
5858
## when the latter one exists.
5959

60-
# RUN: yaml2obj %s --docnum=2 -DLINK="" -o %t3
60+
# RUN: yaml2obj %s --docnum=2 -o %t3
6161
# RUN: llvm-readelf --section-headers %t3 | FileCheck %s --check-prefix=LINK-DEFAULT
6262

6363
# LINK-DEFAULT: [Nr] Name {{.*}} Flg Lk Inf
@@ -67,7 +67,7 @@ Sections:
6767
## Even when the .dynstr section exists, we can explicitly link the .dynsym section
6868
## to another section.
6969

70-
# RUN: yaml2obj %s --docnum=2 -DLINK="Link: 3" -o %t4
70+
# RUN: yaml2obj %s --docnum=2 -DLINK=3 -o %t4
7171
# RUN: llvm-readelf --section-headers %t4 | FileCheck %s --check-prefix=LINK-FOO
7272

7373
# LINK-FOO: [Nr] Name {{.*}} Flg Lk Inf
@@ -76,5 +76,5 @@ Sections:
7676

7777
## Check we can use a section name as a Link value for .dynsym.
7878

79-
# RUN: yaml2obj %s --docnum=2 -DLINK="Link: .foo" -o %t5
79+
# RUN: yaml2obj %s --docnum=2 -DLINK=.foo -o %t5
8080
# RUN: llvm-readelf --section-headers %t5 | FileCheck %s --check-prefix=LINK-FOO

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@ bool ELFDumper<ELFT>::shouldPrintSection(const ELFYAML::Section &S,
203203
if (DWARF && DWARF->getNonEmptySectionNames().count(SecName)) {
204204
if (const ELFYAML::RawContentSection *RawSec =
205205
dyn_cast<const ELFYAML::RawContentSection>(&S)) {
206-
if (RawSec->Type != ELF::SHT_PROGBITS || !RawSec->Link.empty() ||
207-
RawSec->Info || RawSec->AddressAlign != 1 || RawSec->Address ||
208-
RawSec->EntSize)
206+
if (RawSec->Type != ELF::SHT_PROGBITS || RawSec->Link || RawSec->Info ||
207+
RawSec->AddressAlign != 1 || RawSec->Address || RawSec->EntSize)
209208
return true;
210209

211210
ELFYAML::ELF_SHF ShFlags = RawSec->Flags.getValueOr(ELFYAML::ELF_SHF(0));

0 commit comments

Comments
 (0)