[yaml2obj] - Make `Section::Link` field to be `Optional<>`.
authorGeorgii Rymar <grimar@accesssoftek.com>
Thu, 29 Oct 2020 13:04:45 +0000 (16:04 +0300)
committerGeorgii Rymar <grimar@accesssoftek.com>
Fri, 30 Oct 2020 13:18:53 +0000 (16:18 +0300)
`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

llvm/include/llvm/ObjectYAML/ELFYAML.h
llvm/lib/ObjectYAML/ELFEmitter.cpp
llvm/lib/ObjectYAML/ELFYAML.cpp
llvm/test/tools/llvm-readobj/ELF/relr-relocs.test
llvm/test/tools/obj2yaml/ELF/DWARF/debug-addr.yaml
llvm/test/tools/obj2yaml/ELF/DWARF/debug-aranges.yaml
llvm/test/tools/obj2yaml/ELF/DWARF/debug-ranges.yaml
llvm/test/tools/obj2yaml/ELF/DWARF/debug-str.yaml
llvm/test/tools/yaml2obj/ELF/dynsym-section.yaml
llvm/tools/obj2yaml/elf2yaml.cpp

index f3805a9..f7b02f5 100644 (file)
@@ -174,7 +174,7 @@ struct Section : public Chunk {
   ELF_SHT Type;
   Optional<ELF_SHF> Flags;
   Optional<llvm::yaml::Hex64> Address;
-  StringRef Link;
+  Optional<StringRef> Link;
   llvm::yaml::Hex64 AddressAlign;
   Optional<llvm::yaml::Hex64> EntSize;
 
index a1acd2a..124af97 100644 (file)
@@ -694,8 +694,8 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
 
     assignSectionAddress(SHeader, Sec);
 
-    if (!Sec->Link.empty())
-      SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name);
+    if (Sec->Link)
+      SHeader.sh_link = toSectionIndex(*Sec->Link, Sec->Name);
 
     if (IsFirstUndefSection) {
       if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) {
@@ -866,10 +866,10 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
   else
     SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
 
-  if (RawSec && !RawSec->Link.empty()) {
+  if (RawSec && RawSec->Link) {
     // If the Link field is explicitly defined in the document,
     // we should use it.
-    SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name);
+    SHeader.sh_link = toSectionIndex(*RawSec->Link, RawSec->Name);
   } else {
     // When we describe the .dynsym section in the document explicitly, it is
     // 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,
   else if (Name == ".debug_str")
     SHeader.sh_flags = ELF::SHF_MERGE | ELF::SHF_STRINGS;
 
-  if (YAMLSec && !YAMLSec->Link.empty())
-    SHeader.sh_link = toSectionIndex(YAMLSec->Link, Name);
+  if (YAMLSec && YAMLSec->Link)
+    SHeader.sh_link = toSectionIndex(*YAMLSec->Link, Name);
 
   assignSectionAddress(SHeader, YAMLSec);
 }
@@ -1180,7 +1180,7 @@ void ELFState<ELFT>::writeSectionContent(
 
   // For relocation section set link to .symtab by default.
   unsigned Link = 0;
-  if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+  if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
       SN2I.lookup(".symtab", Link))
     SHeader.sh_link = Link;
 
@@ -1191,9 +1191,9 @@ void ELFState<ELFT>::writeSectionContent(
     return;
 
   for (const ELFYAML::Relocation &Rel : *Section.Relocations) {
-    unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name,
-                                                 Section.Link == ".dynsym")
-                                 : 0;
+    const bool IsDynamic = Section.Link && (*Section.Link == ".dynsym");
+    unsigned SymIdx =
+        Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, IsDynamic) : 0;
     if (IsRela) {
       Elf_Rela REntry;
       zero(REntry);
@@ -1261,7 +1261,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
          "Section type is not SHT_GROUP");
 
   unsigned Link = 0;
-  if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+  if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
       SN2I.lookup(".symtab", Link))
     SHeader.sh_link = Link;
 
@@ -1379,7 +1379,7 @@ void ELFState<ELFT>::writeSectionContent(
     SHeader.sh_entsize = 16;
 
   unsigned Link = 0;
-  if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+  if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
       SN2I.lookup(".symtab", Link))
     SHeader.sh_link = Link;
 
@@ -1402,7 +1402,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
                                          const ELFYAML::HashSection &Section,
                                          ContiguousBlobAccumulator &CBA) {
   unsigned Link = 0;
-  if (Section.Link.empty() && !ExcludedSectionHeaders.count(".dynsym") &&
+  if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
       SN2I.lookup(".dynsym", Link))
     SHeader.sh_link = Link;
 
@@ -1592,7 +1592,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
                                          const ELFYAML::AddrsigSection &Section,
                                          ContiguousBlobAccumulator &CBA) {
   unsigned Link = 0;
-  if (Section.Link.empty() && !ExcludedSectionHeaders.count(".symtab") &&
+  if (!Section.Link && !ExcludedSectionHeaders.count(".symtab") &&
       SN2I.lookup(".symtab", Link))
     SHeader.sh_link = Link;
 
@@ -1653,7 +1653,7 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
                                          const ELFYAML::GnuHashSection &Section,
                                          ContiguousBlobAccumulator &CBA) {
   unsigned Link = 0;
-  if (Section.Link.empty() && !ExcludedSectionHeaders.count(".dynsym") &&
+  if (!Section.Link && !ExcludedSectionHeaders.count(".dynsym") &&
       SN2I.lookup(".dynsym", Link))
     SHeader.sh_link = Link;
 
index cd4fd33..f9ee4ce 100644 (file)
@@ -1101,7 +1101,7 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
   IO.mapRequired("Type", Section.Type);
   IO.mapOptional("Flags", Section.Flags);
   IO.mapOptional("Address", Section.Address);
-  IO.mapOptional("Link", Section.Link, StringRef());
+  IO.mapOptional("Link", Section.Link);
   IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
   IO.mapOptional("EntSize", Section.EntSize);
   IO.mapOptional("Offset", Section.Offset);
index 22a56c1..7de0f1b 100644 (file)
@@ -151,7 +151,7 @@ Sections:
                0x000F0501, 0x50400009 ]
     EntSize: [[ENTSIZE=<none>]]
     ShType:  [[SHTYPE=<none>]]
-    Link:    [[LINK=""]]
+    Link:    [[LINK=<none>]]
 
 ## Check we report a warning when we are unable to dump relocations
 ## for a SHT_RELR/SHT_ANDROID_RELR section.
index b294adf..2bdb1bf 100644 (file)
@@ -165,7 +165,7 @@ Sections:
   - Name:         .debug_addr
     Type:         [[TYPE=SHT_PROGBITS]]
     Flags:        [[FLAGS=<none>]]
-    Link:         [[LINK='']]
+    Link:         [[LINK=<none>]]
     EntSize:      [[ENTSIZE=<none>]]
     Info:         [[INFO=<none>]]
     AddressAlign: [[ADDRALIGN=0]]
index cc3c239..5f296bb 100644 (file)
@@ -105,7 +105,7 @@ Sections:
   - Name:         .debug_aranges
     Type:         [[TYPE=SHT_PROGBITS]]
     Flags:        [[FLAGS=<none>]]
-    Link:         [[LINK='']]
+    Link:         [[LINK=<none>]]
     EntSize:      [[ENTSIZE=<none>]]
     Info:         [[INFO=<none>]]
     AddressAlign: [[ADDRALIGN=0]]
index 0e3fbae..c4a4c41 100644 (file)
@@ -216,7 +216,7 @@ Sections:
   - Name:         .debug_ranges
     Type:         [[TYPE=SHT_PROGBITS]]
     Flags:        [[FLAGS=<none>]]
-    Link:         [[LINK='']]
+    Link:         [[LINK=<none>]]
     EntSize:      [[ENTSIZE=<none>]]
     Info:         [[INFO=<none>]]
     AddressAlign: [[ADDRALIGN=0]]
index 76c1c5c..dbe022e 100644 (file)
@@ -56,7 +56,7 @@ Sections:
   - Name:         .debug_str
     Type:         SHT_[[TYPE=PROGBITS]]
     Flags:        [[FLAGS=<none>]]
-    Link:         [[LINK='']]
+    Link:         [[LINK=<none>]]
     EntSize:      [[ENTSIZE=1]]
     Info:         [[INFO=<none>]]
     AddressAlign: [[ADDRALIGN=1]]
index 5c46113..294b91e 100644 (file)
@@ -34,7 +34,7 @@ Sections:
 
 ## Check we are able to set Link = 0 for the .dynsym section explicitly.
 
-# RUN: yaml2obj %s --docnum=2 -DLINK="Link: 0" -o %t2
+# RUN: yaml2obj %s --docnum=2 -DLINK=0 -o %t2
 # RUN: llvm-readelf --section-headers %t2 | FileCheck %s --check-prefix=LINK-NULL
 
 # LINK-NULL: [Nr] Name    {{.*}} Flg Lk Inf
@@ -48,7 +48,7 @@ FileHeader:
 Sections:
   - Name: .dynsym
     Type: SHT_DYNSYM
-    [[LINK]]
+    Link: [[LINK=<none>]]
   - Name: .dynstr
     Type: SHT_STRTAB
   - Name: .foo
@@ -57,7 +57,7 @@ Sections:
 ## Check that by default the .dynsym section will be linked to the .dynstr section,
 ## when the latter one exists.
 
-# RUN: yaml2obj %s --docnum=2 -DLINK="" -o %t3
+# RUN: yaml2obj %s --docnum=2 -o %t3
 # RUN: llvm-readelf --section-headers %t3 | FileCheck %s --check-prefix=LINK-DEFAULT
 
 # LINK-DEFAULT: [Nr] Name    {{.*}} Flg Lk Inf
@@ -67,7 +67,7 @@ Sections:
 ## Even when the .dynstr section exists, we can explicitly link the .dynsym section
 ## to another section.
 
-# RUN: yaml2obj %s --docnum=2 -DLINK="Link: 3" -o %t4
+# RUN: yaml2obj %s --docnum=2 -DLINK=3 -o %t4
 # RUN: llvm-readelf --section-headers %t4 | FileCheck %s --check-prefix=LINK-FOO
 
 # LINK-FOO: [Nr] Name     {{.*}} Flg Lk Inf
@@ -76,5 +76,5 @@ Sections:
 
 ## Check we can use a section name as a Link value for .dynsym.
 
-# RUN: yaml2obj %s --docnum=2 -DLINK="Link: .foo" -o %t5
+# RUN: yaml2obj %s --docnum=2 -DLINK=.foo -o %t5
 # RUN: llvm-readelf --section-headers %t5 | FileCheck %s --check-prefix=LINK-FOO
index fb62f47..edb2aa3 100644 (file)
@@ -203,9 +203,8 @@ bool ELFDumper<ELFT>::shouldPrintSection(const ELFYAML::Section &S,
   if (DWARF && DWARF->getNonEmptySectionNames().count(SecName)) {
     if (const ELFYAML::RawContentSection *RawSec =
             dyn_cast<const ELFYAML::RawContentSection>(&S)) {
-      if (RawSec->Type != ELF::SHT_PROGBITS || !RawSec->Link.empty() ||
-          RawSec->Info || RawSec->AddressAlign != 1 || RawSec->Address ||
-          RawSec->EntSize)
+      if (RawSec->Type != ELF::SHT_PROGBITS || RawSec->Link || RawSec->Info ||
+          RawSec->AddressAlign != 1 || RawSec->Address || RawSec->EntSize)
         return true;
 
       ELFYAML::ELF_SHF ShFlags = RawSec->Flags.getValueOr(ELFYAML::ELF_SHF(0));