[PATCH] D128883: [yaml2obj] Add optional ProgramHeader.Content

Namhyung Kim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 30 00:12:29 PDT 2022


namhyung created this revision.
namhyung added reviewers: MaskRay, jhenderson, irogers.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
namhyung requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

For ELF core files, it needs a way to add raw contents from the program
header directly since core files don't have sections.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128883

Files:
  llvm/include/llvm/ObjectYAML/ELFYAML.h
  llvm/lib/ObjectYAML/ELFEmitter.cpp
  llvm/lib/ObjectYAML/ELFYAML.cpp


Index: llvm/lib/ObjectYAML/ELFYAML.cpp
===================================================================
--- llvm/lib/ObjectYAML/ELFYAML.cpp
+++ llvm/lib/ObjectYAML/ELFYAML.cpp
@@ -1096,6 +1096,7 @@
   IO.mapOptional("FileSize", Phdr.FileSize);
   IO.mapOptional("MemSize", Phdr.MemSize);
   IO.mapOptional("Offset", Phdr.Offset);
+  IO.mapOptional("Content", Phdr.Content);
 }
 
 std::string MappingTraits<ELFYAML::ProgramHeader>::validate(
@@ -1104,6 +1105,8 @@
     return "the \"LastSec\" key can't be used without the \"FirstSec\" key";
   if (FileHdr.FirstSec && !FileHdr.LastSec)
     return "the \"FirstSec\" key can't be used without the \"LastSec\" key";
+  if (FileHdr.FirstSec && FileHdr.Content)
+    return "the \"FirstSec\" key can't be used with \"Content\" key";
   return "";
 }
 
Index: llvm/lib/ObjectYAML/ELFEmitter.cpp
===================================================================
--- llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -232,7 +232,8 @@
                               ContiguousBlobAccumulator &CBA,
                               ELFYAML::Section *YAMLSec);
   void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
-                              std::vector<Elf_Shdr> &SHeaders);
+                              std::vector<Elf_Shdr> &SHeaders,
+                              ContiguousBlobAccumulator &CBA);
 
   std::vector<Fragment>
   getPhdrFragments(const ELFYAML::ProgramHeader &Phdr,
@@ -1162,7 +1163,8 @@
 
 template <class ELFT>
 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
-                                            std::vector<Elf_Shdr> &SHeaders) {
+                                            std::vector<Elf_Shdr> &SHeaders,
+                                            ContiguousBlobAccumulator &CBA) {
   uint32_t PhdrIdx = 0;
   for (auto &YamlPhdr : Doc.ProgramHeaders) {
     Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
@@ -1215,6 +1217,12 @@
       for (const Fragment &F : Fragments)
         PHeader.p_align = std::max((uint64_t)PHeader.p_align, F.AddrAlign);
     }
+
+    if (YamlPhdr.Content) {
+      alignToOffset(CBA, PHeader.p_align,
+                    yaml::Hex64(PHeader.p_paddr + PHeader.p_offset));
+      CBA.writeAsBinary(*YamlPhdr.Content);
+    }
   }
 }
 
@@ -1938,7 +1946,7 @@
   State.initSectionHeaders(SHeaders, CBA);
 
   // Now we can decide segment offsets.
-  State.setProgramHeaderLayout(PHeaders, SHeaders);
+  State.setProgramHeaderLayout(PHeaders, SHeaders, CBA);
 
   bool ReachedLimit = CBA.getOffset() > MaxSize;
   if (Error E = CBA.takeLimitError()) {
Index: llvm/include/llvm/ObjectYAML/ELFYAML.h
===================================================================
--- llvm/include/llvm/ObjectYAML/ELFYAML.h
+++ llvm/include/llvm/ObjectYAML/ELFYAML.h
@@ -687,6 +687,10 @@
 
   // This vector contains all chunks from [FirstSec, LastSec].
   std::vector<Chunk *> Chunks;
+
+  // This contains optional content when no sections are used.
+  // It should NOT be used with Chunks or other section contents.
+  Optional<yaml::BinaryRef> Content;
 };
 
 struct Object {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128883.441280.patch
Type: text/x-patch
Size: 3134 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220630/8bbc7cfd/attachment.bin>


More information about the llvm-commits mailing list