[llvm-branch-commits] [llvm] 7ac0644 - [yaml2obj, obj2yaml] - Make Symbol::Section field optional.

Georgii Rymar via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Dec 4 02:57:01 PST 2020


Author: Georgii Rymar
Date: 2020-12-04T13:45:47+03:00
New Revision: 7ac06444b8baead56301562fbfecbcedbdf30ecd

URL: https://github.com/llvm/llvm-project/commit/7ac06444b8baead56301562fbfecbcedbdf30ecd
DIFF: https://github.com/llvm/llvm-project/commit/7ac06444b8baead56301562fbfecbcedbdf30ecd.diff

LOG: [yaml2obj,obj2yaml] - Make Symbol::Section field optional.

This is similar to what we did earlier for fields of the Section class.

When a field is optional we can use the =<none> syntax in macros.

This was splitted from D92478.

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

Added: 
    

Modified: 
    llvm/include/llvm/ObjectYAML/ELFYAML.h
    llvm/lib/ObjectYAML/ELFEmitter.cpp
    llvm/lib/ObjectYAML/ELFYAML.cpp
    llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h
index 9015fb680b60..8de2e1730378 100644
--- a/llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -103,7 +103,7 @@ struct SectionHeaderTable {
 struct Symbol {
   StringRef Name;
   ELF_STT Type;
-  StringRef Section;
+  Optional<StringRef> Section;
   Optional<ELF_SHN> Index;
   ELF_STB Binding;
   llvm::yaml::Hex64 Value;

diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index b8386bd46be2..ee6149fe02bd 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -832,8 +832,8 @@ ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
       Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name));
 
     Symbol.setBindingAndType(Sym.Binding, Sym.Type);
-    if (!Sym.Section.empty())
-      Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name);
+    if (Sym.Section)
+      Symbol.st_shndx = toSectionIndex(*Sym.Section, "", Sym.Name);
     else if (Sym.Index)
       Symbol.st_shndx = *Sym.Index;
 

diff  --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 92b9c284e064..d7de5462ed25 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1086,7 +1086,7 @@ void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
   IO.mapOptional("Name", Symbol.Name, StringRef());
   IO.mapOptional("StName", Symbol.StName);
   IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
-  IO.mapOptional("Section", Symbol.Section, StringRef());
+  IO.mapOptional("Section", Symbol.Section);
   IO.mapOptional("Index", Symbol.Index);
   IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
   IO.mapOptional("Value", Symbol.Value, Hex64(0));
@@ -1104,7 +1104,7 @@ void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
 
 std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
                                                      ELFYAML::Symbol &Symbol) {
-  if (Symbol.Index && Symbol.Section.data())
+  if (Symbol.Index && Symbol.Section)
     return "Index and Section cannot both be specified for Symbol";
   return "";
 }

diff  --git a/llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml b/llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml
index 89b420104330..ca27db4afe03 100644
--- a/llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/dynamic-symbols.yaml
@@ -42,7 +42,7 @@ DynamicSymbols:
 
 ## Check we can use numeric values to refer to sections.
 
-# RUN: yaml2obj --docnum=2 %s -o %t2
+# RUN: yaml2obj --docnum=2 -DSEC=0xff %s -o %t2
 # RUN: llvm-readobj --dyn-symbols %t2 2>&1 | FileCheck -DFILE=%t2 %s --check-prefix=NUM
 
 # NUM:      Name: foo
@@ -53,11 +53,15 @@ DynamicSymbols:
 # NUM:      Section:
 # NUM-SAME: .dynsym (0x2)
 
-# NUM:      Name: zed
+# NUM:      Name: zed1
 # NUM: warning: '[[FILE]]': invalid section index: 255
 # NUM:      Section:
 # NUM-SAME: <?> (0xFF)
 
+# NUM:      Name: zed2
+# NUM:      Section:
+# NUM-SAME: Undefined (0x0)
+
 --- !ELF
 FileHeader:
   Class: ELFCLASS64
@@ -67,12 +71,27 @@ Sections:
   - Name: .data
     Type: SHT_PROGBITS
 DynamicSymbols:
-  - Name: foo
+  - Name:    foo
     Section: 1
-  - Name: bar
+  - Name:    bar
     Section: 2
-  - Name: zed
-    Section: 0xff
+  - Name:    zed1
+    Section: [[SEC=<none>]]
+## The case when no "Section" key is set.
+  - Name: zed2
+
+## Check that by default no section is set.
+
+# RUN: yaml2obj --docnum=2 %s -o %t2.none
+# RUN: llvm-readobj --dyn-symbols %t2.none 2>&1 | \
+# RUN:   FileCheck -DFILE=%t2.none %s --check-prefix=NONE
+
+# NONE:      Name: zed1
+# NONE:      Section:
+# NONE-SAME: Undefined (0x0)
+# NONE:      Name: zed2
+# NONE:      Section:
+# NONE-SAME: Undefined (0x0)
 
 ## Check we report errors when unknown sections are referenced by dynamic symbols.
 
@@ -80,6 +99,7 @@ DynamicSymbols:
 
 # ERR: error: unknown section referenced: '.sec1' by YAML symbol 'foo'
 # ERR: error: unknown section referenced: '.sec2' by YAML symbol 'bar'
+# ERR: error: unknown section referenced: '' by YAML symbol 'zed'
 
 --- !ELF
 FileHeader:
@@ -90,7 +110,9 @@ Sections:
   - Name: .data
     Type: SHT_PROGBITS
 DynamicSymbols:
-  - Name: foo
+  - Name:    foo
     Section: .sec1
-  - Name: bar
+  - Name:    bar
     Section: .sec2
+  - Name:    zed
+    Section: ''


        


More information about the llvm-branch-commits mailing list