PE: Remove file alignment restriction
This commit is contained in:
committed by
Markus F.X.J. Oberhumer
parent
cc100be26e
commit
222471744f
@@ -78,10 +78,12 @@ Linker* PackW32Pe::newLinker() const
|
|||||||
|
|
||||||
int PackW32Pe::readFileHeader()
|
int PackW32Pe::readFileHeader()
|
||||||
{
|
{
|
||||||
|
if (fi->st_size() >= 0x206) {
|
||||||
char buf[6];
|
char buf[6];
|
||||||
fi->seek(0x200, SEEK_SET);
|
fi->seek(0x200, SEEK_SET);
|
||||||
fi->readx(buf, 6);
|
fi->readx(buf, 6);
|
||||||
isrtm = memcmp(buf, "32STUB" ,6) == 0;
|
isrtm = memcmp(buf, "32STUB" ,6) == 0;
|
||||||
|
}
|
||||||
return super::readFileHeader();
|
return super::readFileHeader();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-11
@@ -191,8 +191,6 @@ int PeFile::readFileHeader()
|
|||||||
return 0;
|
return 0;
|
||||||
fi->seek(pe_offset,SEEK_SET);
|
fi->seek(pe_offset,SEEK_SET);
|
||||||
readPeHeader();
|
readPeHeader();
|
||||||
fi->seek(0x200,SEEK_SET);
|
|
||||||
fi->readx(&h,6);
|
|
||||||
return getFormat();
|
return getFormat();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2172,8 +2170,11 @@ void PeFile::checkHeaderValues(unsigned subsystem, unsigned mask,
|
|||||||
if (ih_entry && ih_entry < rvamin)
|
if (ih_entry && ih_entry < rvamin)
|
||||||
throwCantPack("run a virus scanner on this file!");
|
throwCantPack("run a virus scanner on this file!");
|
||||||
|
|
||||||
if (ih_filealign < 0x200)
|
const unsigned fam1 = ih_filealign - 1;
|
||||||
throwCantPack("filealign < 0x200 is not yet supported");
|
if (!(1+ fam1) || (1+ fam1) & fam1) { // ih_filealign is not a power of 2
|
||||||
|
char buf[32]; snprintf(buf, sizeof(buf), "bad file alignment %#x", 1+ fam1);
|
||||||
|
throwCantPack(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned PeFile::handleStripRelocs(upx_uint64_t ih_imagebase,
|
unsigned PeFile::handleStripRelocs(upx_uint64_t ih_imagebase,
|
||||||
@@ -2370,8 +2371,8 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
|
|||||||
allow_filter = false;
|
allow_filter = false;
|
||||||
|
|
||||||
const unsigned oam1 = ih.objectalign - 1;
|
const unsigned oam1 = ih.objectalign - 1;
|
||||||
if ((1+ oam1) & oam1) { // ih.objectalign is not a power of 2
|
if (!(1+ oam1) || (1+ oam1) & oam1) { // ih.objectalign is not a power of 2
|
||||||
char buf[32]; snprintf(buf, sizeof(buf), "bad alignment %#x", 1+ oam1);
|
char buf[32]; snprintf(buf, sizeof(buf), "bad object alignment %#x", 1+ oam1);
|
||||||
throwCantPack(buf);
|
throwCantPack(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2448,6 +2449,9 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
|
|||||||
if (tlsindex && ((newvsize - ph.c_len - 1024 + oam1) &~ oam1) > tlsindex + 4)
|
if (tlsindex && ((newvsize - ph.c_len - 1024 + oam1) &~ oam1) > tlsindex + 4)
|
||||||
tlsindex = 0;
|
tlsindex = 0;
|
||||||
|
|
||||||
|
const int oh_filealign = UPX_MIN(ih.filealign, 0x200);
|
||||||
|
const unsigned fam1 = oh_filealign - 1;
|
||||||
|
|
||||||
int identsize = 0;
|
int identsize = 0;
|
||||||
const unsigned codesize = getLoaderSection("IDENTSTR",&identsize);
|
const unsigned codesize = getLoaderSection("IDENTSTR",&identsize);
|
||||||
assert(identsize > 0);
|
assert(identsize > 0);
|
||||||
@@ -2471,12 +2475,12 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
|
|||||||
// identsplit - number of ident + (upx header) bytes to put into the PE header
|
// identsplit - number of ident + (upx header) bytes to put into the PE header
|
||||||
const unsigned sizeof_osection = sizeof(osection[0]) * oobjs;
|
const unsigned sizeof_osection = sizeof(osection[0]) * oobjs;
|
||||||
int identsplit = pe_offset + sizeof_osection + sizeof(ht);
|
int identsplit = pe_offset + sizeof_osection + sizeof(ht);
|
||||||
if ((identsplit & 0x1ff) == 0)
|
if ((identsplit & fam1) == 0)
|
||||||
identsplit = 0;
|
identsplit = 0;
|
||||||
else if (((identsplit + identsize) ^ identsplit) < 0x200)
|
else if (((identsplit + identsize) ^ identsplit) < oh_filealign)
|
||||||
identsplit = identsize;
|
identsplit = identsize;
|
||||||
else
|
else
|
||||||
identsplit = ALIGN_GAP(identsplit, 0x200);
|
identsplit = ALIGN_GAP(identsplit, oh_filealign);
|
||||||
ic = identsize - identsplit;
|
ic = identsize - identsplit;
|
||||||
|
|
||||||
const unsigned c_len = ((ph.c_len + ic) & 15) == 0 ? ph.c_len : ph.c_len + 16 - ((ph.c_len + ic) & 15);
|
const unsigned c_len = ((ph.c_len + ic) & 15) == 0 ? ph.c_len : ph.c_len + 16 - ((ph.c_len + ic) & 15);
|
||||||
@@ -2493,7 +2497,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
|
|||||||
|
|
||||||
// new PE header
|
// new PE header
|
||||||
memcpy(&oh,&ih,sizeof(oh));
|
memcpy(&oh,&ih,sizeof(oh));
|
||||||
oh.filealign = 0x200; // identsplit depends on this
|
oh.filealign = oh_filealign; // identsplit depends on this
|
||||||
memset(osection,0,sizeof(osection));
|
memset(osection,0,sizeof(osection));
|
||||||
|
|
||||||
oh.entry = upxsection;
|
oh.entry = upxsection;
|
||||||
@@ -2566,7 +2570,6 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
|
|||||||
|
|
||||||
const unsigned ncsize = soxrelocs + soimpdlls + soexport
|
const unsigned ncsize = soxrelocs + soimpdlls + soexport
|
||||||
+ (!last_section_rsrc_only ? soresources : 0);
|
+ (!last_section_rsrc_only ? soresources : 0);
|
||||||
const unsigned fam1 = oh.filealign - 1;
|
|
||||||
|
|
||||||
// this one is tricky: it seems windoze touches 4 bytes after
|
// this one is tricky: it seems windoze touches 4 bytes after
|
||||||
// the end of the relocation data - so we have to increase
|
// the end of the relocation data - so we have to increase
|
||||||
|
|||||||
Reference in New Issue
Block a user