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

James Henderson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Oct 13 02:08:32 PDT 2026


================
@@ -33,10 +36,87 @@ using namespace llvm::objcopy::elf;
 using namespace llvm::object;
 using namespace llvm::support;
 
-template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
-  uint8_t *B = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
-               Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
-  Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
+namespace {
+
+template <class T> void writeObject(raw_ostream &Out, const T &Value) {
+  Out.write(reinterpret_cast<const char *>(&Value), sizeof(Value));
----------------
jh7370 wrote:

Nit: I know it's semantically going to be identical, but as we are dealing with raw bytes rather than characters, I'd prefer to use `uint8_t` when writing to the stream.

More generally, throughout this change, prefer `uint8_t` over `char`, please, where it is possible (I expect it will be possible in most cases). This also reduces the number of things that are different to the previous code.

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


More information about the llvm-branch-commits mailing list