[llvm] [llvm-objcopy] Add --gap-fill and --pad-to options (PR #65815)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 13 01:52:55 PDT 2023


================
@@ -2635,9 +2635,36 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
 }
 
 Error BinaryWriter::write() {
-  for (const SectionBase &Sec : Obj.allocSections())
+  SmallVector<const SectionBase *, 30> LoadableSections;
+  for (const SectionBase &Sec : Obj.allocSections()) {
+    if ((Sec.Flags & SectionFlag::SecLoad) && Sec.Type != SHT_NOBITS)
+      LoadableSections.push_back(&Sec);
+  }
+
+  if (LoadableSections.empty())
+    return Error::success();
+
+  llvm::stable_sort(LoadableSections,
+                    [](const SectionBase *LHS, const SectionBase *RHS) {
+                      return LHS->Offset < RHS->Offset;
+                    });
+
+  assert(LoadableSections.front()->Offset == 0);
+
+  for (unsigned i = 0; i != LoadableSections.size(); ++i) {
+    const SectionBase &Sec = *LoadableSections[i];
     if (Error Err = Sec.accept(*SecWriter))
       return Err;
+    if (GapFill == 0)
+      continue;
+    uint64_t PadOffset = (i < LoadableSections.size() - 1)
+                             ? LoadableSections[i + 1]->Offset
+                             : Buf->getBufferSize();
+    assert(PadOffset <= Buf->getBufferSize());
+    if (Sec.Offset + Sec.Size < PadOffset)
----------------
jh7370 wrote:

This if check feels a bit redundant?

https://github.com/llvm/llvm-project/pull/65815


More information about the llvm-commits mailing list