[llvm-branch-commits] [llvm] 8c2cf89 - [yaml2obj/obj2yaml] - Make Value/Size fields of Symbol optional.

Georgii Rymar via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Dec 16 03:05:05 PST 2020


Author: Georgii Rymar
Date: 2020-12-16T13:49:57+03:00
New Revision: 8c2cf89834c3496be343525b9f9c6aecc9182117

URL: https://github.com/llvm/llvm-project/commit/8c2cf89834c3496be343525b9f9c6aecc9182117
DIFF: https://github.com/llvm/llvm-project/commit/8c2cf89834c3496be343525b9f9c6aecc9182117.diff

LOG: [yaml2obj/obj2yaml] - Make Value/Size fields of Symbol optional.

When a field is optional we can use the `=<none>` syntax in macros.
This patch makes `Value`/`Size` fields of `Symbol` optional
and adds test cases for them.

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

Added: 
    llvm/test/tools/obj2yaml/ELF/symbol.yaml
    llvm/test/tools/yaml2obj/ELF/symbol-size.yaml
    llvm/test/tools/yaml2obj/ELF/symbol-value.yaml

Modified: 
    llvm/include/llvm/ObjectYAML/ELFYAML.h
    llvm/lib/ObjectYAML/ELFEmitter.cpp
    llvm/lib/ObjectYAML/ELFYAML.cpp
    llvm/tools/obj2yaml/elf2yaml.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h
index 8de2e17303782..a838b69273ceb 100644
--- a/llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -106,8 +106,8 @@ struct Symbol {
   Optional<StringRef> Section;
   Optional<ELF_SHN> Index;
   ELF_STB Binding;
-  llvm::yaml::Hex64 Value;
-  llvm::yaml::Hex64 Size;
+  Optional<llvm::yaml::Hex64> Value;
+  Optional<llvm::yaml::Hex64> Size;
   Optional<uint8_t> Other;
 
   Optional<uint32_t> StName;

diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index ee6149fe02bd8..51efc00f52d4d 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -837,9 +837,9 @@ ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
     else if (Sym.Index)
       Symbol.st_shndx = *Sym.Index;
 
-    Symbol.st_value = Sym.Value;
+    Symbol.st_value = Sym.Value.getValueOr(yaml::Hex64(0));
     Symbol.st_other = Sym.Other ? *Sym.Other : 0;
-    Symbol.st_size = Sym.Size;
+    Symbol.st_size = Sym.Size.getValueOr(yaml::Hex64(0));
   }
 
   return Ret;

diff  --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index d7de5462ed254..52a4a3a2d80b0 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1089,8 +1089,8 @@ void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
   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));
-  IO.mapOptional("Size", Symbol.Size, Hex64(0));
+  IO.mapOptional("Value", Symbol.Value);
+  IO.mapOptional("Size", Symbol.Size);
 
   // Symbol's Other field is a bit special. It is usually a field that
   // represents st_other and holds the symbol visibility. However, on some

diff  --git a/llvm/test/tools/obj2yaml/ELF/symbol.yaml b/llvm/test/tools/obj2yaml/ELF/symbol.yaml
new file mode 100644
index 0000000000000..3684d4f70c6cd
--- /dev/null
+++ b/llvm/test/tools/obj2yaml/ELF/symbol.yaml
@@ -0,0 +1,27 @@
+## This is a test case to check how obj2yaml dumps symbols.
+
+## Check that we only dump Size and Value keys when
+## their values are not 0.
+
+# RUN: yaml2obj %s -o %t1
+# RUN: obj2yaml %t1 | FileCheck %s --check-prefix=FIELDS-DEF
+
+# FIELDS-DEF:      Symbols:
+# FIELDS-DEF-NEXT:   - Name:  foo
+# FIELDS-DEF-NEXT:   - Name:  bar
+# FIELDS-DEF-NEXT:     Value: 0x1
+# FIELDS-DEF-NEXT:     Size:  0x1
+# FIELDS-DEF-NEXT: ...
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Symbols:
+  - Name:  foo
+    Size:  0x0
+    Value: 0x0
+  - Name:  bar
+    Size:  0x1
+    Value: 0x1

diff  --git a/llvm/test/tools/yaml2obj/ELF/symbol-size.yaml b/llvm/test/tools/yaml2obj/ELF/symbol-size.yaml
new file mode 100644
index 0000000000000..ba3b40928f357
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/ELF/symbol-size.yaml
@@ -0,0 +1,33 @@
+## Check we can set 
diff erent sizes for symbols.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-readelf --symbols %t | FileCheck %s
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Symbols:
+## No "Size" key was set. Check the behaviour is the
+## same as if it was set to 0.
+  - Name:  aaa
+# CHECK:    Num: Value Size {{.*}} Name
+# CHECK:      1: [[#]] 0    {{.*}} aaa
+## The "Size" key is explicitly set to 0x0.
+  - Name:  bbb
+    Size:  0x0
+## Check we can use the "=<none>" syntax.
+# CHECK-NEXT: 2: [[#]] 0    {{.*}} bbb
+  - Name:  ccc
+    Size:  [[ESIZE=<none>]]
+# CHECK-NEXT: 3: [[#]] 0    {{.*}} ccc
+## "Size" is explicitly set to an arbitrary value.
+## Here we use UINT64_MAX to check this boundary case.
+  - Name:  ddd
+    Size:  0xffffffffffffffff
+# CHECK-NEXT: 4: [[#]] -1   {{.*}} ddd
+## The same as the previous case, but using decimal values.
+  - Name:  eee
+    Size:  18446744073709551615
+# CHECK-NEXT: 5: [[#]] -1   {{.*}} eee

diff  --git a/llvm/test/tools/yaml2obj/ELF/symbol-value.yaml b/llvm/test/tools/yaml2obj/ELF/symbol-value.yaml
new file mode 100644
index 0000000000000..41872bfc1af65
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/ELF/symbol-value.yaml
@@ -0,0 +1,33 @@
+## Check we can set 
diff erent values for symbols.
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-readelf --symbols %t | FileCheck %s
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data:  ELFDATA2LSB
+  Type:  ET_REL
+Symbols:
+## No "Value" key was set. Check the behaviour is the
+## same as if it was set to 0.
+  - Name:  aaa
+# CHECK:    Num: Value            {{.*}} Name
+# CHECK:      1: 0000000000000000 {{.*}} aaa
+## The "Value" key is explicitly set to 0x0.
+  - Name:  bbb
+    Value: 0x0
+## Check we can use the "=<none>" syntax.
+# CHECK-NEXT: 2: 0000000000000000 {{.*}} bbb
+  - Name:  ccc
+    Value: [[EVAL=<none>]]
+# CHECK-NEXT: 3: 0000000000000000 {{.*}} ccc
+## The "Value" key is explicitly set to an arbitrary value.
+## Here we use UINT64_MAX to check this boundary case.
+  - Name:  ddd
+    Value: 0xffffffffffffffff
+# CHECK-NEXT: 4: ffffffffffffffff {{.*}} ddd
+## The same as the previous case, but using decimal values.
+  - Name:  eee
+    Value: 18446744073709551615
+# CHECK-NEXT: 5: ffffffffffffffff {{.*}} eee

diff  --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp
index 0885e92ee28a3..1f9ff88f042a5 100644
--- a/llvm/tools/obj2yaml/elf2yaml.cpp
+++ b/llvm/tools/obj2yaml/elf2yaml.cpp
@@ -661,8 +661,10 @@ template <class ELFT>
 Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
                                   StringRef StrTable, ELFYAML::Symbol &S) {
   S.Type = Sym->getType();
-  S.Value = Sym->st_value;
-  S.Size = Sym->st_size;
+  if (Sym->st_value)
+    S.Value = (yaml::Hex64)Sym->st_value;
+  if (Sym->st_size)
+    S.Size = (yaml::Hex64)Sym->st_size;
   S.Other = Sym->st_other;
   S.Binding = Sym->getBinding();
 


        


More information about the llvm-branch-commits mailing list