[llvm] 68195b1 - [yaml2obj] - Allow empty SectionHeaderTable definitions.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 27 23:57:16 PST 2021


Author: Georgii Rymar
Date: 2021-01-28T10:51:52+03:00
New Revision: 68195b15a36c338751b7abe28819345617dd70fb

URL: https://github.com/llvm/llvm-project/commit/68195b15a36c338751b7abe28819345617dd70fb
DIFF: https://github.com/llvm/llvm-project/commit/68195b15a36c338751b7abe28819345617dd70fb.diff

LOG: [yaml2obj] - Allow empty SectionHeaderTable definitions.

Currently we don't allow the following definition:

```
Sections:
  - Type: SectionHeaderTable
  - Name: .foo
    Type: SHT_PROGBITS
```

We report an error: "SectionHeaderTable can't be empty. Use 'NoHeaders' key to drop the section header table".

It was implemented in this way earlier, when `SectionHeaderTable`
was a dedicated key outside of the `Sections` list. And we did not
allow to select where the table is written.

Currently it makes sense to allow it, because a user might
want to place the default section header table at an arbitrary position,
e.g. before other sections. In this case it is not convenient and error prone
to require specifying all sections:

```
Sections:
  - Type: SectionHeaderTable
    Sections:
      - Name: .foo
      - Name: .strtab
      - Name: .shstrtab
  - Name: .foo
    Type: SHT_PROGBITS
```

This patch allows empty SectionHeaderTable definitions.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h
index 4f3c76bbd82c..d9a9f61f27b5 100644
--- a/llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -296,13 +296,15 @@ struct SectionHeaderTable : Chunk {
   Optional<bool> NoHeaders;
 
   size_t getNumHeaders(size_t SectionsNum) const {
-    if (IsImplicit)
+    if (IsImplicit || isDefault())
       return SectionsNum;
     if (NoHeaders)
       return (*NoHeaders) ? 0 : SectionsNum;
     return (Sections ? Sections->size() : 0) + /*Null section*/ 1;
   }
 
+  bool isDefault() const { return !Sections && !Excluded && !NoHeaders; }
+
   static constexpr StringRef TypeStr = "SectionHeaderTable";
 };
 

diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index e477a1b2b8f2..089ef8e876bc 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -555,7 +555,8 @@ unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec,
   const ELFYAML::SectionHeaderTable &SectionHeaders =
       Doc.getSectionHeaderTable();
   if (SectionHeaders.IsImplicit ||
-      (SectionHeaders.NoHeaders && !SectionHeaders.NoHeaders.getValue()))
+      (SectionHeaders.NoHeaders && !SectionHeaders.NoHeaders.getValue()) ||
+      SectionHeaders.isDefault())
     return Index;
 
   assert(!SectionHeaders.NoHeaders.getValueOr(false) ||
@@ -1744,7 +1745,8 @@ template <class ELFT>
 DenseMap<StringRef, size_t> ELFState<ELFT>::buildSectionHeaderReorderMap() {
   const ELFYAML::SectionHeaderTable &SectionHeaders =
       Doc.getSectionHeaderTable();
-  if (SectionHeaders.IsImplicit || SectionHeaders.NoHeaders)
+  if (SectionHeaders.IsImplicit || SectionHeaders.NoHeaders ||
+      SectionHeaders.isDefault())
     return DenseMap<StringRef, size_t>();
 
   DenseMap<StringRef, size_t> Ret;

diff  --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index 05d30577812b..21ff9b8afd17 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1474,9 +1474,6 @@ std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
   if (const auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) {
     if (SHT->NoHeaders && (SHT->Sections || SHT->Excluded || SHT->Offset))
       return "NoHeaders can't be used together with Offset/Sections/Excluded";
-    if (!SHT->NoHeaders && !SHT->Sections && !SHT->Excluded)
-      return "SectionHeaderTable can't be empty. Use 'NoHeaders' key to drop "
-             "the section header table";
     return "";
   }
 

diff  --git a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
index 5fe029f42e75..3d09290a6a5a 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-headers.yaml
@@ -151,10 +151,18 @@ Sections:
     Sections:  []
     NoHeaders: [[NOHEADERS]]
 
-## Check that we do not allow an empty SectionHeaderTable tag and suggest to use an explicit syntax instead.
-# RUN: not yaml2obj %s --docnum=5 -o /dev/null 2>&1 | FileCheck %s --check-prefix=NO-VALUE
+## Check that we allow using an empty SectionHeaderTable definition.
+## It can be used to emit the default section header table at an arbitrary position.
 
-# NO-VALUE: SectionHeaderTable can't be empty. Use 'NoHeaders' key to drop the section header table
+# RUN: yaml2obj %s --docnum=5 -o %t5.novalues
+# RUN: llvm-readelf --sections %t5.novalues | \
+# RUN:   FileCheck %s --check-prefix=NO-VALUES
+
+## Check we placed the section header table before the .foo section.
+
+# NO-VALUES: There are 4 section headers, starting at offset 0x40:
+# NO-VALUES: [Nr] Name Type     Address          Off    Size
+# NO-VALUES: [ 1] .foo PROGBITS 0000000000000000 000140 000000
 
 --- !ELF
 FileHeader:
@@ -162,9 +170,9 @@ FileHeader:
   Data:  ELFDATA2LSB
   Type:  ET_REL
 Sections:
+  - Type: SectionHeaderTable
   - Name: .foo
     Type: SHT_PROGBITS
-  - Type: SectionHeaderTable
 
 ## Test that we are still able to override e_shoff, e_shnum and e_shstrndx
 ## fields even when we do not produce section headers.


        


More information about the llvm-commits mailing list