[yaml2obj] - Improve handling of the SHT_GROUP section.
authorGeorgii Rymar <grimar@accesssoftek.com>
Mon, 28 Oct 2019 10:30:05 +0000 (13:30 +0300)
committerGeorgii Rymar <grimar@accesssoftek.com>
Tue, 29 Oct 2019 08:09:12 +0000 (11:09 +0300)
Currently, when we do not specify "Info" field in a YAML description
for SHT_GROUP section, yaml2obj reports an error:
"error: unknown symbol referenced: '' by YAML section '.group1'"

Also, we do not link it with a symbol table by default,
though it is what we do for AddrsigSection, HashSection, RelocationSection.
(http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_link)

The patch fixes missings mentioned.

Differential revision: https://reviews.llvm.org/D69299

llvm/include/llvm/ObjectYAML/ELFYAML.h
llvm/lib/ObjectYAML/ELFEmitter.cpp
llvm/lib/ObjectYAML/ELFYAML.cpp
llvm/test/tools/yaml2obj/duplicate-section-names.test
llvm/test/tools/yaml2obj/elf-comdat-broken-info.yaml
llvm/test/tools/yaml2obj/elf-comdat-broken-members.yaml
llvm/test/tools/yaml2obj/elf-comdat-broken.yaml
llvm/test/tools/yaml2obj/elf-override-shname.yaml
llvm/test/tools/yaml2obj/elf-override-shoffset.yaml
llvm/test/tools/yaml2obj/elf-override-shsize.yaml
llvm/test/tools/yaml2obj/section-link.yaml

index 57f802c..8cd12b3 100644 (file)
@@ -326,7 +326,7 @@ struct Group : Section {
   // Members of a group contain a flag and a list of section indices
   // that are part of the group.
   std::vector<SectionOrType> Members;
-  StringRef Signature; /* Info */
+  Optional<StringRef> Signature; /* Info */
 
   Group() : Section(SectionKind::Group) {}
 
index d5cdb85..6a4e00c 100644 (file)
@@ -791,10 +791,16 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
   assert(Section.Type == llvm::ELF::SHT_GROUP &&
          "Section type is not SHT_GROUP");
 
+  unsigned Link = 0;
+  if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
+    SHeader.sh_link = Link;
+
   SHeader.sh_entsize = 4;
   SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
-  SHeader.sh_info =
-      toSymbolIndex(Section.Signature, Section.Name, /*IsDynamic=*/false);
+
+  if (Section.Signature)
+    SHeader.sh_info =
+        toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false);
 
   raw_ostream &OS =
       CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
index ad316be..f362b9c 100644 (file)
@@ -1069,7 +1069,7 @@ static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
 
 static void groupSectionMapping(IO &IO, ELFYAML::Group &Group) {
   commonSectionMapping(IO, Group);
-  IO.mapOptional("Info", Group.Signature, StringRef());
+  IO.mapOptional("Info", Group.Signature);
   IO.mapRequired("Members", Group.Members);
 }
 
index 65d3de4..4765179 100644 (file)
@@ -150,7 +150,6 @@ FileHeader:
 Sections:
   - Name: .group
     Type: SHT_GROUP
-    Link: .symtab
     Info: foo
     Members:
       - SectionOrType: GRP_COMDAT
@@ -159,7 +158,6 @@ Sections:
     Type: SHT_PROGBITS
   - Name: '.group [1]'
     Type: SHT_GROUP
-    Link: .symtab
     Info: 'foo [1]'
     Members:         
       - SectionOrType: GRP_COMDAT
index 6f2f7ac..d79b250 100644 (file)
@@ -42,13 +42,11 @@ FileHeader:
 Sections:
   - Name: .group1
     Type: SHT_GROUP
-    Link: .symtab
     Info: foo
     Members:
       - SectionOrType: GRP_COMDAT
   - Name: .group2
     Type: SHT_GROUP
-    Link: .symtab
     Info: bar
     Members:
       - SectionOrType: GRP_COMDAT
index 63cb27e..9ebcf02 100644 (file)
@@ -14,8 +14,6 @@ FileHeader:
 Sections:
   - Name: .group
     Type: SHT_GROUP
-    Link: .symtab
-    Info: 0
     Members:
       - SectionOrType: GRP_COMDAT
       - SectionOrType: .foo
index 87ff6fe..88ed3c3 100644 (file)
@@ -10,7 +10,6 @@ FileHeader:
 Sections:
   - Name:              .group
     Type:              SHT_GROUP
-    Link:              .symtab
     Info:              foo
     Members:
       - SectionOrType: 0xFF
index 7c4cb61..4698bc0 100644 (file)
@@ -63,7 +63,6 @@ Sections:
     ShName: 0x000000005
   - Name: .group
     Type: SHT_GROUP
-    Info: 0
     ShName: 0x000000006
     Members:
   - Name: .gnu.version
index 26ef286..9fe5ef0 100644 (file)
@@ -43,7 +43,6 @@ Sections:
     ShOffset: 0x000000005
   - Name: .group
     Type: SHT_GROUP
-    Info: 0
     ShOffset: 0x000000006
     Members:
   - Name: .gnu.version
index c62e91a..4470ae2 100644 (file)
@@ -43,7 +43,6 @@ Sections:
     ShSize: 0x000000005
   - Name: .group
     Type: SHT_GROUP
-    Info: 0
     ShSize: 0x000000006
     Members:
   - Name: .gnu.version
index 88595ea..efe4d3f 100644 (file)
@@ -44,3 +44,53 @@ Sections:
   - Name: .bar
     Type: SHT_PROGBITS
     Link: .unknown2
+
+## Check we link SHT_GROUP to a symbol table by default if it exists.
+## Also, check we can set an arbitrary value for sh_link.
+
+# RUN: yaml2obj --docnum=3 %s -o %t3
+# RUN: llvm-readobj --sections %t3 | FileCheck %s --check-prefix=GROUP-LINK
+
+# GROUP-LINK:      Name: .group1
+# GROUP-LINK:      Link:
+# GROUP-LINK-SAME:       3
+
+# GROUP-LINK:      Name: .group2
+# GROUP-LINK:      Link:
+# GROUP-LINK-SAME:       255
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: .group1
+    Type: SHT_GROUP
+    Members:
+  - Name: .group2
+    Type: SHT_GROUP
+    Link: 0xFF
+    Members:
+Symbols: []
+
+## Check we set SHT_GROUP's link value to 0 when there is no symbol table.
+
+# RUN: yaml2obj --docnum=4 %s -o %t4
+# RUN: llvm-readobj --sections %t4 | FileCheck %s --check-prefix=GROUP-LINK-NOSYMTAB
+
+# GROUP-LINK-NOSYMTAB:      Name: .group
+# GROUP-LINK-NOSYMTAB:      Link:
+# GROUP-LINK-NOSYMTAB-SAME:       0
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: .group
+    Type: SHT_GROUP
+    Members: