[yaml2obj] - Add a Size tag support for SHT_LLVM_ADDRSIG sections.

It allows using "Size" with or without "Content" in YAML descriptions of
SHT_LLVM_ADDRSIG sections.

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

llvm-svn: 373610
diff --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h
index 592b502..ef2b4fb 100644
--- a/llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -268,6 +268,7 @@
 
 struct AddrsigSection : Section {
   Optional<yaml::BinaryRef> Content;
+  Optional<llvm::yaml::Hex64> Size;
   Optional<std::vector<AddrsigSymbol>> Symbols;
 
   AddrsigSection() : Section(SectionKind::Addrsig) {}
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 3f3b27c..f9c31f3 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -1007,8 +1007,8 @@
   if (Section.Link.empty() && SN2I.lookup(".symtab", Link))
     SHeader.sh_link = Link;
 
-  if (Section.Content) {
-    SHeader.sh_size = writeContent(OS, Section.Content, None);
+  if (Section.Content || Section.Size) {
+    SHeader.sh_size = writeContent(OS, Section.Content, Section.Size);
     return;
   }
 
diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index e295a00..29585ab 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1074,6 +1074,7 @@
 static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) {
   commonSectionMapping(IO, Section);
   IO.mapOptional("Content", Section.Content);
+  IO.mapOptional("Size", Section.Size);
   IO.mapOptional("Symbols", Section.Symbols);
 }
 
@@ -1245,12 +1246,17 @@
   }
 
   if (const auto *Sec = dyn_cast<ELFYAML::AddrsigSection>(Section.get())) {
-    if (!Sec->Symbols && !Sec->Content)
-      return "one of \"Symbols\" or \"Content\" must be specified";
+    if (!Sec->Symbols && !Sec->Content && !Sec->Size)
+      return "one of \"Content\", \"Size\" or \"Symbols\" must be specified";
 
-    if (Sec->Content) {
+    if (Sec->Content || Sec->Size) {
+      if (Sec->Size && Sec->Content &&
+          (uint64_t)*Sec->Size < Sec->Content->binary_size())
+        return "\"Size\" must be greater than or equal to the content "
+               "size";
+
       if (Sec->Symbols)
-        return "\"Content\" and \"Symbols\" cannot be used together";
+        return "\"Symbols\" cannot be used with \"Content\" or \"Size\"";
       return {};
     }
 
diff --git a/llvm/test/tools/yaml2obj/elf-llvm-addrsig-section.yaml b/llvm/test/tools/yaml2obj/elf-llvm-addrsig-section.yaml
index 98496d30..1433d6d 100644
--- a/llvm/test/tools/yaml2obj/elf-llvm-addrsig-section.yaml
+++ b/llvm/test/tools/yaml2obj/elf-llvm-addrsig-section.yaml
@@ -161,7 +161,7 @@
 
 # RUN: not yaml2obj --docnum=7 %s 2>&1 | FileCheck %s --check-prefix=NO-TAGS
 
-# NO-TAGS: error: one of "Symbols" or "Content" must be specified
+# NO-TAGS: error: one of "Content", "Size" or "Symbols" must be specified
 
 --- !ELF
 FileHeader:
@@ -177,7 +177,7 @@
 
 # RUN: not yaml2obj --docnum=8 %s 2>&1 | FileCheck %s --check-prefix=CONTENT-SYMBOLS
 
-# CONTENT-SYMBOLS: error: "Content" and "Symbols" cannot be used together
+# CONTENT-SYMBOLS: "Symbols" cannot be used with "Content" or "Size"
 
 --- !ELF
 FileHeader:
@@ -211,3 +211,97 @@
     Type: SHT_LLVM_ADDRSIG
     Link: 123
     Content: ""
+
+## Check we can use only "Size" to create a SHT_LLVM_ADDRSIG section.
+
+# RUN: yaml2obj --docnum=10 %s -o %t10
+# RUN: llvm-readobj --sections --section-data %t10 | FileCheck %s --check-prefix=SIZE
+
+# SIZE:      Name: .llvm_addrsig
+# SIZE:      Size:
+# SIZE-SAME: 17
+# SIZE:      SectionData (
+# SIZE-NEXT:  0000: 00000000 00000000 00000000 00000000  |
+# SIZE-NEXT:  0010: 00                                   |
+# SIZE-NEXT: )
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name: .llvm_addrsig
+    Type: SHT_LLVM_ADDRSIG
+    Size: 0x11
+
+## Check we can use "Size" and "Content" together to create a SHT_LLVM_ADDRSIG section.
+
+# RUN: yaml2obj --docnum=11 %s -o %t11
+# RUN: llvm-readobj --sections --section-data %t11 | FileCheck %s --check-prefix=SIZE-CONTENT
+
+# SIZE-CONTENT:      Name: .llvm_addrsig_sizegr
+# SIZE-CONTENT:      Size:
+# SIZE-CONTENT-SAME: 5
+# SIZE-CONTENT:      SectionData (
+# SIZE-CONTENT-NEXT:  0000: 11223300 00 |
+# SIZE-CONTENT-NEXT: )
+
+# SIZE-CONTENT:      Name: .llvm_addrsig_sizeeq
+# SIZE-CONTENT:      Size:
+# SIZE-CONTENT-SAME: 3
+# SIZE-CONTENT:      SectionData (
+# SIZE-CONTENT-NEXT:  0000: 112233 |
+# SIZE-CONTENT-NEXT: )
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name: .llvm_addrsig_sizegr
+    Type: SHT_LLVM_ADDRSIG
+    Size: 0x5
+    Content: "112233"
+  - Name: .llvm_addrsig_sizeeq
+    Type: SHT_LLVM_ADDRSIG
+    Size: 0x3
+    Content: "112233"
+
+## Check that when "Size" and "Content" are used together, the size
+## must be greater than or equal to the content size.
+
+# RUN: not yaml2obj --docnum=12 %s 2>&1 | FileCheck %s --check-prefix=SIZE-CONTENT-ERR
+
+# SIZE-CONTENT-ERR: error: "Size" must be greater than or equal to the content size
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name: .llvm_addrsig
+    Type: SHT_LLVM_ADDRSIG
+    Size: 0x1
+    Content: "1122"
+
+## Check we can't use "Size" and "Symbols" tags together.
+
+# RUN: not yaml2obj --docnum=13 %s 2>&1 | FileCheck %s --check-prefix=CONTENT-SYMBOLS
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name: .llvm_addrsig
+    Type: SHT_LLVM_ADDRSIG
+    Size: 0x1
+    Symbols: [ ]