[llvm] [llvm-objcopy] Stream ELF output to reduce peak memory usage (PR #217706)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 01:11:41 PDT 2026


================
@@ -2689,13 +2792,38 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
   }
 
   size_t TotalSize = totalSize();
-  Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
-  if (!Buf)
-    return createStringError(errc::not_enough_memory,
-                             "failed to allocate memory buffer of " +
-                                 Twine::utohexstr(TotalSize) + " bytes");
+  if (auto *Stream = dyn_cast<raw_fd_stream>(&Out);
+      Stream && Stream->supportsSeeking() && Stream->isRegularFile()) {
----------------
jh7370 wrote:

I don't really like this (and that's ignoring the ugly "modern" C++ syntax involving declarations and checks inside the if). Specifically, I'm talking about the different Stream kinds being handled differently. To me, this indicates the output stream kinds would be better off being wrapped in a set of classes with a common interface that just does the right thing for the given underlying stream type (because the right class was picked in the first place).

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


More information about the llvm-commits mailing list