[llvm] baf3225 - [yaml2obj] - Implement the "Offset" property for the Fill Chunk.

Georgii Rymar via llvm-commits llvm-commits at lists.llvm.org
Wed May 20 03:39:09 PDT 2020


Author: Georgii Rymar
Date: 2020-05-20T13:38:48+03:00
New Revision: baf32259872190feac080ea83381aef545157949

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

LOG: [yaml2obj] - Implement the "Offset" property for the Fill Chunk.

Similar to a regular section chunk, a Fill should have this property.
This patch implements it.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h
index 15224349a6d6..2fd18fcd2957 100644
--- a/llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -161,6 +161,7 @@ struct Chunk {
 
   ChunkKind Kind;
   StringRef Name;
+  Optional<llvm::yaml::Hex64> Offset;
 
   Chunk(ChunkKind K) : Kind(K) {}
   virtual ~Chunk();
@@ -173,7 +174,6 @@ struct Section : public Chunk {
   StringRef Link;
   llvm::yaml::Hex64 AddressAlign;
   Optional<llvm::yaml::Hex64> EntSize;
-  Optional<llvm::yaml::Hex64> Offset;
 
   // Usually sections are not created implicitly, but loaded from YAML.
   // When they are, this flag is used to signal about that.
@@ -213,11 +213,6 @@ struct Fill : Chunk {
   Optional<yaml::BinaryRef> Pattern;
   llvm::yaml::Hex64 Size;
 
-  // We have to remember the offset of the fill, because it does not have
-  // a corresponding section header, unlike a section. We might need this
-  // information when writing the output.
-  uint64_t ShOffset;
-
   Fill() : Chunk(ChunkKind::Fill) {}
 
   static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Fill; }

diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 2b1de2fff6f5..2a3839509faf 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -428,8 +428,8 @@ void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
 
   size_t SecNdx = -1;
   for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) {
-    if (auto S = dyn_cast<ELFYAML::Fill>(D.get())) {
-      S->ShOffset = alignToOffset(CBA, /*Align=*/1, /*Offset=*/None);
+    if (ELFYAML::Fill *S = dyn_cast<ELFYAML::Fill>(D.get())) {
+      S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset);
       writeFill(*S, CBA);
       LocationCounter += S->Size;
       continue;
@@ -755,7 +755,7 @@ ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
     }
 
     if (ELFYAML::Fill *Fill = NameToFill.lookup(SecName.Section)) {
-      Ret.push_back({Fill->ShOffset, Fill->Size, llvm::ELF::SHT_PROGBITS,
+      Ret.push_back({*Fill->Offset, Fill->Size, llvm::ELF::SHT_PROGBITS,
                      /*ShAddrAlign=*/1});
       continue;
     }

diff  --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp
index a3a0f56c66d8..445fcbc412ba 100644
--- a/llvm/lib/ObjectYAML/ELFYAML.cpp
+++ b/llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1174,6 +1174,7 @@ static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) {
 static void fillMapping(IO &IO, ELFYAML::Fill &Fill) {
   IO.mapOptional("Name", Fill.Name, StringRef());
   IO.mapOptional("Pattern", Fill.Pattern);
+  IO.mapOptional("Offset", Fill.Offset);
   IO.mapRequired("Size", Fill.Size);
 }
 

diff  --git a/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml b/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
index 4d7b91d6727a..11574521079b 100644
--- a/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/custom-fill.yaml
@@ -294,3 +294,49 @@ ProgramHeaders:
   - Type: PT_LOAD
     Sections:
       - Section: fill
+
+## Show that we can use the "Offset" key to set an arbitrary offset for a Fill.
+
+## 0x41 is the minimal possible valid offset for Fill,
+## because the .foo section of size 0x1 is placed at 0x40.
+# RUN: yaml2obj --docnum=11 -DOFFSET=0x41 -o %t11 %s
+# RUN: llvm-readelf --section-headers %t11 | FileCheck %s --check-prefix=OFFSET-MIN
+
+## 0x123 is an arbitrary offset.
+# RUN: yaml2obj --docnum=11 -DOFFSET=0x123 -o %t12 %s
+# RUN: llvm-readelf --section-headers %t12 | FileCheck %s --check-prefix=OFFSET
+
+# OFFSET-MIN:      Section Headers:
+# OFFSET-MIN-NEXT:  [Nr] Name Type     Address          Off    Size
+# OFFSET-MIN-NEXT:  [ 0]      NULL     0000000000000000 000000 000000
+# OFFSET-MIN-NEXT:  [ 1] .foo PROGBITS 0000000000000000 000040 000001
+# OFFSET-MIN-NEXT:  [ 2] .bar PROGBITS 0000000000000000 000042 000001
+
+# OFFSET:      Section Headers:
+# OFFSET-NEXT:  [Nr] Name Type     Address          Off    Size
+# OFFSET-NEXT:  [ 0]      NULL     0000000000000000 000000 000000
+# OFFSET-NEXT:  [ 1] .foo PROGBITS 0000000000000000 000040 000001
+# OFFSET-NEXT:  [ 2] .bar PROGBITS 0000000000000000 000124 000001
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_DYN
+  Machine: EM_X86_64
+Sections:
+  - Name: .foo
+    Type: SHT_PROGBITS
+    Size: 1
+  - Type:    Fill
+    Pattern: "AA"
+    Size:    0x1
+    Offset:  [[OFFSET]]
+  - Name: .bar
+    Type: SHT_PROGBITS
+    Size: 1
+
+## Show that the "Offset" value can't go backward.
+# RUN: not yaml2obj --docnum=11 -DOFFSET=0x40 2>&1 %s | FileCheck %s --check-prefix=OFFSET-ERR
+
+# OFFSET-ERR: error: the 'Offset' value (0x40) goes backward


        


More information about the llvm-commits mailing list