src: some xspan cleanups

This commit is contained in:
Markus F.X.J. Oberhumer 2023-01-30 11:03:11 +01:00
parent 43b6ff29e3
commit 0653bb5a72
9 changed files with 43 additions and 41 deletions

View File

@ -358,8 +358,8 @@ jobs:
- { zig_target: x86_64-macos.13-none }
- { zig_target: x86_64-windows-gnu }
env:
# 2023-01-26
ZIG_DIST_VERSION: 0.11.0-dev.1460+e675af069
# 2023-01-30
ZIG_DIST_VERSION: 0.11.0-dev.1491+2b27bc2c6
# for zig-cc wrapper scripts (see below):
ZIG_CPPFLAGS: -DUPX_DOCTEST_CONFIG_MULTITHREADING
ZIG_FLAGS: ${{ matrix.zig_flags }}

View File

@ -538,8 +538,9 @@ void PackExe::pack(OutputFile *fo) {
memcpy(loader, getLoader(), lsize);
patchPackHeader(loader, e_len);
// fprintf(stderr,"\ne_len=%x d_len=%x c_len=%x oo=%x ulen=%x destp=%x copys=%x
// images=%x",e_len,d_len,packedsize,ph.overlap_overhead,ph.u_len,destpara,copysize,ih_imagesize);
NO_fprintf(stderr, "\ne_len=%x d_len=%x c_len=%x oo=%x ulen=%x destp=%x copys=%x images=%x",
e_len, d_len, packedsize, ph.overlap_overhead, ph.u_len, /*destpara*/ 0, copysize,
ih_imagesize);
// write header + write loader + compressed file
#if TESTING
@ -614,12 +615,14 @@ void PackExe::unpack(OutputFile *fo) {
unsigned relocn = 0;
SPAN_S_VAR(upx_byte, relocs, obuf + ph.u_len, obuf);
MemBuffer wrkmem;
MemBuffer mb_wrkmem;
SPAN_0_VAR(upx_byte, wrkmem, nullptr);
if (!(flag & NORELOC)) {
relocs -= get_le16(obuf + ph.u_len - 2);
relocs -= get_le16(obuf + (ph.u_len - 2));
ph.u_len -= 2;
wrkmem.alloc(4 * MAXRELOCS);
mb_wrkmem.alloc(4 * MAXRELOCS);
wrkmem = mb_wrkmem; // => now a SPAN_S
unsigned es = 0, ones = get_le16(relocs);
const unsigned seghi = get_le16(relocs + 2);
SPAN_S_VAR(const upx_byte, p, relocs + 4);
@ -630,17 +633,17 @@ void PackExe::unpack(OutputFile *fo) {
bool dorel = true;
for (p += 4; ones && di < 0x10000; p++) {
if (dorel) {
set_le16(wrkmem + 4 * relocn, di);
set_le16(wrkmem + 2 + 4 * relocn++, es);
// printf ("%x\n",es*16+di);
set_le16(wrkmem + (4 * relocn), di);
set_le16(wrkmem + (2 + 4 * relocn++), es);
NO_printf("%x\n", es * 16 + di);
}
dorel = true;
if (*p == 0) {
SPAN_S_VAR(const upx_byte, q, obuf);
for (q = obuf + es * 16 + di; !(*q == 0x9a && get_le16(q + 3) <= seghi); q++)
;
di = ptr_diff_bytes(q, obuf + es * 16) + 3;
for (q = obuf + (es * 16 + di); !(*q == 0x9a && get_le16(q + 3) <= seghi);
q++) {
}
di = ptr_diff_bytes(q, obuf + (es * 16)) + 3;
} else if (*p == 1) {
di += 254;
if (di < 0x10000)
@ -659,7 +662,7 @@ void PackExe::unpack(OutputFile *fo) {
if (relocn) {
oh.relocs = relocn;
while (relocn & 3)
set_le32(wrkmem + 4 * relocn++, 0);
set_le32(wrkmem + (4 * relocn++), 0);
}
unsigned outputlen = ptr_udiff_bytes(relocs, obuf) + sizeof(oh) + relocn * 4;
@ -702,7 +705,7 @@ void PackExe::unpack(OutputFile *fo) {
fo->write(&oh, sizeof(oh));
if (relocn)
fo->write(wrkmem, relocn * 4);
fo->write(obuf, ptr_diff_bytes(relocs, obuf));
fo->write(obuf, ptr_udiff_bytes(relocs, obuf));
// copy the overlay
copyOverlay(fo, ih_overlay, obuf);

View File

@ -282,9 +282,9 @@ void PackTmt::unpack(OutputFile *fo) {
decompress(ibuf, obuf);
// decode relocations
const unsigned osize = ph.u_len - get_le32(obuf + ph.u_len - 4);
const unsigned osize = ph.u_len - get_le32(obuf + (ph.u_len - 4));
SPAN_P_VAR(upx_byte, relocs, obuf + osize);
const unsigned origstart = get_le32(obuf + ph.u_len - 8);
const unsigned origstart = get_le32(obuf + (ph.u_len - 8));
// unfilter
if (ph.filter) {
@ -292,7 +292,7 @@ void PackTmt::unpack(OutputFile *fo) {
ft.init(ph.filter, 0);
ft.cto = (unsigned char) ph.filter_cto;
if (ph.version < 11)
ft.cto = (unsigned char) (get_le32(obuf + ph.u_len - 12) >> 24);
ft.cto = (unsigned char) (get_le32(obuf + (ph.u_len - 12)) >> 24);
ft.unfilter(obuf, ptr_udiff_bytes(relocs, obuf));
}

View File

@ -274,7 +274,7 @@ void PackVmlinuxBase<T>::pack(OutputFile *fo)
fi->readx(ibuf + ((unsigned) phdri[j].p_paddr - paddr_min), phdri[j].p_filesz);
}
}
checkAlreadyPacked(ibuf + ph.u_len - 1024, 1024);
checkAlreadyPacked(ibuf + (ph.u_len - 1024), 1024);
// prepare filter
ph.filter = 0;

View File

@ -37,9 +37,8 @@
**************************************************************************/
Packer::Packer(InputFile *f)
: bele(nullptr), fi(f), file_size(-1), ph_format(-1), ph_version(-1), ibufgood(0), uip(nullptr),
: bele(nullptr), fi(f), file_size(0), ph_format(-1), ph_version(-1), ibufgood(0), uip(nullptr),
linker(nullptr), last_patch(nullptr), last_patch_len(0), last_patch_off(0) {
file_size = 0;
if (fi != nullptr)
file_size = fi->st_size();
mem_size_assert(1, file_size_u);
@ -653,7 +652,7 @@ int Packer::patchPackHeader(void *b, int blen) {
bool Packer::getPackHeader(const void *b, int blen, bool allow_incompressible) {
auto bb = (const upx_byte *) b;
if (!ph.fillPackHeader(SPAN_S_MAKE(const upx_byte, bb, blen), blen))
if (!ph.decodePackHeaderFromBuf(SPAN_S_MAKE(const upx_byte, bb, blen), blen))
return false;
if (ph.version > getVersion())
@ -705,7 +704,7 @@ void Packer::checkAlreadyPacked(const void *b, int blen) {
// is a real PackHeader, e.g.
//
// PackHeader tmp;
// if (!tmp.fillPackHeader((unsigned char *)b + boff, blen - boff))
// if (!tmp.decodePackHeaderFromBuf((unsigned char *)b + boff, blen - boff))
// return;
//
// This also would require that the buffer in 'b' holds

View File

@ -50,7 +50,7 @@ class PackHeader final {
PackHeader();
void putPackHeader(SPAN_S(upx_byte) p);
bool fillPackHeader(SPAN_S(const upx_byte) b, int blen);
bool decodePackHeaderFromBuf(SPAN_S(const upx_byte) b, int blen);
public:
int getPackHeaderSize() const;
@ -76,7 +76,7 @@ public:
unsigned saved_u_adler;
unsigned saved_c_adler;
// info fields set by fillPackHeader()
// info fields set by decodePackHeaderFromBuf()
unsigned buf_offset;
// info fields set by Packer::compress()

View File

@ -170,12 +170,12 @@ void PackHeader::putPackHeader(SPAN_S(upx_byte) p) {
//
**************************************************************************/
bool PackHeader::fillPackHeader(SPAN_S(const upx_byte) buf, int blen) {
bool PackHeader::decodePackHeaderFromBuf(SPAN_S(const upx_byte) buf, int blen) {
int boff = find_le32(raw_bytes(buf, blen), blen, UPX_MAGIC_LE32);
if (boff < 0)
return false;
SPAN_S_VAR(const upx_byte, p, buf + boff);
SPAN_S_VAR(const upx_byte, const p, buf + boff);
unsigned const headway = blen - boff; // bytes remaining in buf
if (headway < (1 + 7))
@ -187,8 +187,8 @@ bool PackHeader::fillPackHeader(SPAN_S(const upx_byte) buf, int blen) {
filter_cto = 0;
if (opt->debug.debug_level) {
fprintf(stderr, " fillPackHeader version=%d format=%d method=%d level=%d\n", version,
format, method, level);
fprintf(stderr, " decodePackHeaderFromBuf version=%d format=%d method=%d level=%d\n",
version, format, method, level);
}
if (0 == format || 128 == format || (format < 128 && format > UPX_F_LINUX_ELF64_ARM) ||
(format > 128 && format > UPX_F_DYLIB_PPC64)) {
@ -272,16 +272,15 @@ bool PackHeader::fillPackHeader(SPAN_S(const upx_byte) buf, int blen) {
if (version == 0xff)
throwCantUnpack("cannot unpack UPX ;-)");
// check header_checksum
if (version > 9) {
unsigned const size = getPackHeaderSize(); // expected; based on format and version
if (headway < size || p[size - 1] != get_packheader_checksum(p, size - 1))
throwCantUnpack("header corrupted 3");
}
if (c_len < 2 || u_len < 2 || !mem_size_valid_bytes(c_len) || !mem_size_valid_bytes(u_len))
throwCantUnpack("header corrupted 4");
//
// success
//

View File

@ -178,7 +178,7 @@ int PeFile::readFileHeader() {
: (h.p512 * 512 + h.m512 - h.m512 ? 512 : h.nexepos);
if ((pe_offset + delta) < delta // wrap-around
|| (pe_offset + delta) > (unsigned) file_size) {
|| (pe_offset + delta) > file_size_u) {
char buf[64];
snprintf(buf, sizeof(buf), "bad PE delta %#x at offset %#x", delta, pe_offset);
throwCantPack(buf);
@ -365,7 +365,7 @@ void PeFile32::processRelocs() // pass1
ih.objects = tryremove(IDADDR(PEDIR_RELOC), ih.objects);
}
mb_orelocs.alloc(1);
orelocs = mb_orelocs;
orelocs = mb_orelocs; // => orelocs now is a SPAN_S
sorelocs = 0;
return;
}
@ -412,7 +412,7 @@ void PeFile32::processRelocs() // pass1
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
mb_orelocs.alloc(mem_size(4, rnum, 1024)); // 1024 - safety
orelocs = mb_orelocs;
orelocs = mb_orelocs; // => orelocs now is a SPAN_S
sorelocs = optimizeReloc32((upx_byte *) fix[3], xcounts[3], orelocs, ibuf + rvamin,
ibufgood - rvamin, true, &big_relocs);
delete[] fix[3];
@ -461,7 +461,7 @@ void PeFile64::processRelocs() // pass1
ih.objects = tryremove(IDADDR(PEDIR_RELOC), ih.objects);
}
mb_orelocs.alloc(1);
orelocs = mb_orelocs;
orelocs = mb_orelocs; // => orelocs now is a SPAN_S
sorelocs = 0;
return;
}
@ -511,7 +511,7 @@ void PeFile64::processRelocs() // pass1
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
mb_orelocs.alloc(mem_size(4, rnum, 1024)); // 1024 - safety
orelocs = mb_orelocs;
orelocs = mb_orelocs; // => orelocs now is a SPAN_S
sorelocs = optimizeReloc64((upx_byte *) fix[10], xcounts[10], orelocs, ibuf + rvamin,
ibufgood - rvamin, true, &big_relocs);
@ -2159,8 +2159,8 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh, unsigned subsystem_mask,
} else
handleStub(fi, fo, pe_offset);
unsigned overlaystart = readSections(objs, ih.imagesize, ih.filealign, ih.datasize);
unsigned overlay = file_size - stripDebug(overlaystart);
if (overlay >= (unsigned) file_size)
unsigned overlay = file_size_u - stripDebug(overlaystart);
if (overlay >= file_size_u)
overlay = 0;
checkOverlay(overlay);
@ -2776,7 +2776,7 @@ void PeFile::unpack0(OutputFile *fo, const ht &ih, ht &oh, ord_mask_t ord_mask,
const unsigned iobjs = ih.objects;
const unsigned overlay =
file_size -
file_size_u -
ALIGN_UP(isection[iobjs - 1].rawdataptr + isection[iobjs - 1].size, ih.filealign);
checkOverlay(overlay);
@ -2787,7 +2787,7 @@ void PeFile::unpack0(OutputFile *fo, const ht &ih, ht &oh, ord_mask_t ord_mask,
// decompress
decompress(ibuf, obuf);
unsigned skip = get_le32(obuf + ph.u_len - 4);
unsigned skip = get_le32(obuf + (ph.u_len - 4));
unsigned take = sizeof(oh);
SPAN_S_VAR(upx_byte, extrainfo, obuf);
extrainfo = obuf.subref("bad extrainfo offset %#x", skip, take);

View File

@ -47,6 +47,7 @@
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_MEM == UPX_RSIZE_MAX)
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX_STR <= UPX_RSIZE_MAX / 256)
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * UPX_RSIZE_MAX * 9 / 8 + 16 * 1024 * 1024 < INT_MAX)
ACC_COMPILE_TIME_ASSERT_HEADER(UPX_RSIZE_MAX >= 65536 * 8192)
upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1,
upx_uint64_t extra2) {