[yaml2obj] - Add a Size tag support for SHT_LLVM_ADDRSIG sections.
authorGeorge Rimar <grimar@accesssoftek.com>
Thu, 3 Oct 2019 15:02:18 +0000 (15:02 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Thu, 3 Oct 2019 15:02:18 +0000 (15:02 +0000)
It allows using "Size" with or without "Content" in YAML descriptions of
SHT_LLVM_ADDRSIG sections.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373610 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ObjectYAML/ELFYAML.h
lib/ObjectYAML/ELFEmitter.cpp
lib/ObjectYAML/ELFYAML.cpp
test/tools/yaml2obj/elf-llvm-addrsig-section.yaml

index 592b5021bd693716955a5057793b016c36efc9b0..ef2b4fba031d12afbab803bc5ad6fe12d2d4df20 100644 (file)
@@ -268,6 +268,7 @@ struct AddrsigSymbol {
 
 struct AddrsigSection : Section {
   Optional<yaml::BinaryRef> Content;
+  Optional<llvm::yaml::Hex64> Size;
   Optional<std::vector<AddrsigSymbol>> Symbols;
 
   AddrsigSection() : Section(SectionKind::Addrsig) {}
index 3f3b27c5bfad143c6c48f048cf267dc8c3077f37..f9c31f335f112a1a761e6103ce93f4f487056d36 100644 (file)
@@ -1007,8 +1007,8 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
   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;
   }
 
index e295a000ef8a1cac8e16e3193a668fbfac89536a..29585abe6e80bb8be7f1d9f9ccec4d94d25fb2c0 100644 (file)
@@ -1074,6 +1074,7 @@ static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) {
 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 @@ StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
   }
 
   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 || 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->Content) {
       if (Sec->Symbols)
-        return "\"Content\" and \"Symbols\" cannot be used together";
+        return "\"Symbols\" cannot be used with \"Content\" or \"Size\"";
       return {};
     }
 
index 98496d30fa84deeae18c9f8cbeb94558fbe3dbe5..1433d6dbc13e7e9174bfc900a85c645c96eab634 100644 (file)
@@ -161,7 +161,7 @@ Sections:
 
 # 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 @@ Sections:
 
 # 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 @@ Sections:
     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: [ ]