diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index 48f8797a..19be9d3d 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -401,10 +401,7 @@ void PackLinuxI386elf::pack(OutputFile *fo) ph.c_len = total_out; // write packheader - const int hsize = ph.getPackHeaderSize(); - set_le32(obuf, UPX_MAGIC_LE32); // note: always le32 - patchPackHeader(obuf, hsize); - fo->write(obuf, hsize); + writePackHeader(fo); // write overlay offset (needed for decompression) set_native32(obuf, lsize); diff --git a/src/p_unix.cpp b/src/p_unix.cpp index 207f03df..5e3c4d1c 100644 --- a/src/p_unix.cpp +++ b/src/p_unix.cpp @@ -75,6 +75,23 @@ bool PackUnix::canPack() } +void PackUnix::writePackHeader(OutputFile *fo) +{ + unsigned char buf[32]; + memset(buf, 0, sizeof(buf)); + + const int hsize = ph.getPackHeaderSize(); + assert((unsigned)hsize <= sizeof(buf)); + + // note: magic constants are always le32 + set_le32(buf+0, UPX_MAGIC_LE32); + set_le32(buf+4, UPX_MAGIC2_LE32); + + patchPackHeader(buf, hsize); + fo->write(buf, hsize); +} + + /************************************************************************* // Generic Unix pack(). Subclasses must provide patchLoader(). // @@ -186,10 +203,7 @@ void PackUnix::pack(OutputFile *fo) ph.c_len = total_out; // write packheader - const int hsize = ph.getPackHeaderSize(); - set_le32(obuf, UPX_MAGIC_LE32); // note: always le32 - patchPackHeader(obuf, hsize); - fo->write(obuf, hsize); + writePackHeader(fo); // write overlay offset (needed for decompression) set_native32(obuf, lsize); diff --git a/src/p_unix.h b/src/p_unix.h index 5a9d987b..af3c6034 100644 --- a/src/p_unix.h +++ b/src/p_unix.h @@ -56,6 +56,8 @@ protected: virtual void patchLoaderChecksum() {} virtual void updateLoader(OutputFile *) = 0; + virtual void writePackHeader(OutputFile *fo); + // in order too share as much code as possible we introduce // an endian abstraction here virtual unsigned get_native32(const void *, int off=0) = 0;