[yaml2obj][ELF] Add an optional `Size` field to the YAML section declaration.
authorSimon Atanasyan <simon@atanasyan.com>
Fri, 16 May 2014 16:01:00 +0000 (16:01 +0000)
committerSimon Atanasyan <simon@atanasyan.com>
Fri, 16 May 2014 16:01:00 +0000 (16:01 +0000)
Now the only method to configure ELF section's content and size is to assign
a hexadecimal string to the `Content` field. Unfortunately this way is
completely useless when you need to declare a really large section.

To solve this problem this patch adds one more optional field `Size`
to the `RawContentSection` structure. When yaml2obj generates an ELF file
it uses the following algorithm:
1. If both `Content` and `Size` fields are missed create an empty section.
2. If only `Content` field is missed take section length from the `Size`
   field and fill the section by zero.
3. If only `Size` field is missed create a section using data from
   the `Content` field.
4. If both `Content` and `Size` fields are provided validate that the `Size`
   value is not less than size of `Content` data. Than take section length
   from the `Size`, fill beginning of the section by `Content` and the rest
   by zero.

Examples
--------
* Create a section 0x10000 bytes long filled by zero
  Name: .data
  Type: SHT_PROGBITS
  Flags: [ SHF_ALLOC ]
  Size: 0x10000

* Create a section 0x10000 bytes long starting from 'CA' 'FE' 'BA' 'BE'
  Name: .data
  Type: SHT_PROGBITS
  Flags: [ SHF_ALLOC ]
  Content: CAFEBABE
  Size: 0x10000

The patch reviewed by Michael Spencer.

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

include/llvm/Object/ELFYAML.h
lib/Object/ELFYAML.cpp
test/Object/yaml2obj-elf-section-basic.yaml
test/Object/yaml2obj-elf-section-invalid-size.yaml [new file with mode: 0644]
tools/obj2yaml/elf2yaml.cpp
tools/yaml2obj/yaml2elf.cpp

index 5be7534557e000b3a46eef903d250a973df0af84..524e55b07e1cca951074feaed3a9fe6827e9fb45 100644 (file)
@@ -83,6 +83,7 @@ struct Section {
 };
 struct RawContentSection : Section {
   object::yaml::BinaryRef Content;
+  llvm::yaml::Hex64 Size;
   RawContentSection() : Section(SectionKind::RawContent) {}
   static bool classof(const Section *S) {
     return S->Kind == SectionKind::RawContent;
@@ -193,6 +194,7 @@ template <> struct MappingTraits<ELFYAML::Relocation> {
 template <>
 struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
   static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
+  static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
 };
 
 template <>
index c396f66ee65bb8df1a8225f4df944f3c3e6c881a..547720ddc328a97d58c3f2de0f755cf91cef2901 100644 (file)
@@ -658,6 +658,7 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
   commonSectionMapping(IO, Section);
   IO.mapOptional("Content", Section.Content);
+  IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
 }
 
 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
@@ -687,6 +688,14 @@ void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
   }
 }
 
+StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
+    IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
+  const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
+  if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
+    return StringRef();
+  return "Section size must be greater or equal to the content size";
+}
+
 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
                                                  ELFYAML::Relocation &Rel) {
   IO.mapRequired("Offset", Rel.Offset);
index 7316b7f5bc73dc60e6d3ca8e83a5aacac0d6b0f7..56a3fd6e5f18c6d1000589d2889b0e01a3936484 100644 (file)
@@ -17,6 +17,14 @@ Sections:
     Content: EBFE
     AddressAlign: 2
 
+  - Name: .data
+    Type: SHT_PROGBITS
+    Flags: [ SHF_ALLOC ]
+    Address: 0xCAFECAFE
+    Content: FEBF
+    Size: 8
+    AddressAlign: 2
+
 # CHECK:        Section {
 # CHECK:          Index: 0
 # CHECK:          Type: SHT_NULL (0x0)
@@ -38,6 +46,23 @@ Sections:
 # CHECK-NEXT:     )
 #
 # CHECK:        Section {
+# CHECK:          Name: .data
+# CHECK-NEXT:     Type: SHT_PROGBITS (0x1)
+# CHECK-NEXT:     Flags [ (0x2)
+# CHECK-NEXT:       SHF_ALLOC (0x2)
+# CHECK-NEXT:     ]
+# CHECK-NEXT:     Address: 0xCAFECAFE
+# CHECK-NEXT:     Offset: 0x1D0
+# CHECK-NEXT:     Size: 8
+# CHECK-NEXT:     Link: 0
+# CHECK-NEXT:     Info: 0
+# CHECK-NEXT:     AddressAlignment: 2
+# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:     SectionData (
+# CHECK-NEXT:       0000: FEBF0000 00000000 |........|
+# CHECK-NEXT:     )
+#
+# CHECK:        Section {
 # CHECK:          Name: .symtab (25)
 # CHECK:          Type: SHT_SYMTAB (0x2)
 # CHECK:        }
diff --git a/test/Object/yaml2obj-elf-section-invalid-size.yaml b/test/Object/yaml2obj-elf-section-invalid-size.yaml
new file mode 100644 (file)
index 0000000..d0cb370
--- /dev/null
@@ -0,0 +1,26 @@
+# RUN: not yaml2obj -format=elf -o %t %s 2>&1 | FileCheck %s
+
+!ELF
+FileHeader:
+  Class: ELFCLASS64
+  Data: ELFDATA2LSB
+  Type: ET_REL
+  Machine: EM_X86_64
+
+Sections:
+  - Name: .text
+    Type: SHT_PROGBITS
+    Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+    Content: EBFE
+    AddressAlign: 2
+
+  - Name: .data
+    Type: SHT_PROGBITS
+    Flags: [ SHF_ALLOC ]
+    Content: 0000000000000000
+    Size: 2
+
+# CHECK: YAML:17:5: error: Section size must be greater or equal to the content size
+# CHECK-NEXT: - Name: .data
+# CHECK-NEXT:   ^
+# CHECK-NEXT: yaml2obj: Failed to parse YAML file!
index 38afa138d97d973a561990597884f0ab952e301b..7642921b4855d65983748794c9ca432ab35d161e 100644 (file)
@@ -254,6 +254,7 @@ ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
   if (error_code EC = ContentOrErr.getError())
     return EC;
   S->Content = object::yaml::BinaryRef(ContentOrErr.get());
+  S->Size = S->Content.binary_size();
 
   return S.release();
 }
index 3190b27d3246cdefc10c2bc854d7f69eec037894..bb52cda7c1e9b3eec98f9f13699203f21e6eb43f 100644 (file)
@@ -314,9 +314,14 @@ void
 ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
                                     const ELFYAML::RawContentSection &Section,
                                     ContiguousBlobAccumulator &CBA) {
-  Section.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
+  assert(Section.Size >= Section.Content.binary_size() &&
+         "Section size and section content are inconsistent");
+  raw_ostream &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
+  Section.Content.writeAsBinary(OS);
+  for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
+    OS.write(0);
   SHeader.sh_entsize = 0;
-  SHeader.sh_size = Section.Content.binary_size();
+  SHeader.sh_size = Section.Size;
 }
 
 template <class ELFT>