skip 0==.vmsize; also DEBUG, DPRINTF

This commit is contained in:
John Reiser 2012-08-22 20:43:00 -07:00
parent 2d29a82250
commit c89702ae84
4 changed files with 264 additions and 90 deletions

View File

@ -296,7 +296,7 @@ tmp/amd64-darwin.macho-fold.o : $(srcdir)/src/$$T.S
$(call tc,f-objstrip,$@)
tmp/amd64-darwin.macho-main.o : $(srcdir)/src/$$T.c
$(call tc,gcc) -c -Os $< -o $@
$(call tc,gcc) -c -Os -fPIC $< -o $@
$(call tc,f-objstrip,$@)
$(call tc,objdump) -dr $(tc_objdump_disasm_options) $@ | $(RTRIM) > $@.disasm

View File

@ -1,5 +1,5 @@
/* amd64-darwin.macho-fold.h
created from amd64-darwin.macho-fold.bin, 1354 (0x54a) bytes
created from amd64-darwin.macho-fold.bin, 1372 (0x55c) bytes
This file is part of the UPX executable compressor.
@ -31,11 +31,11 @@
*/
#define STUB_AMD64_DARWIN_MACHO_FOLD_SIZE 1354
#define STUB_AMD64_DARWIN_MACHO_FOLD_ADLER32 0x9f6f399d
#define STUB_AMD64_DARWIN_MACHO_FOLD_CRC32 0x7c7ef068
#define STUB_AMD64_DARWIN_MACHO_FOLD_SIZE 1372
#define STUB_AMD64_DARWIN_MACHO_FOLD_ADLER32 0x35053e52
#define STUB_AMD64_DARWIN_MACHO_FOLD_CRC32 0x488007f4
unsigned char stub_amd64_darwin_macho_fold[1354] = {
unsigned char stub_amd64_darwin_macho_fold[1372] = {
/* 0x0000 */ 232, 80, 0, 0, 0,131,249, 73,117, 74, 72,137,241, 72,137,254,
/* 0x0010 */ 235, 44,138, 7, 72,131,199, 1, 60,128,114, 10, 60,143,119, 6,
/* 0x0020 */ 128,127,254, 15,116, 6, 44,232, 60, 1,119, 35, 56, 23,117, 31,
@ -43,82 +43,83 @@ unsigned char stub_amd64_darwin_macho_fold[1354] = {
/* 0x0040 */ 233, 4,138, 7, 72,131,199, 1, 72,255,201,117,217,235, 5, 72,
/* 0x0050 */ 255,201,117,190,195, 65, 89, 72,137,223,139, 51, 72, 41,247,106,
/* 0x0060 */ 0,184, 0, 8, 0, 0,139, 79, 24, 57,193, 15, 66,200, 73,137,
/* 0x0070 */ 232, 73,137,228, 72, 41,204, 72,137,226, 65, 84,232, 97, 3, 0,
/* 0x0070 */ 232, 73,137,228, 72, 41,204, 72,137,226, 65, 84,232,115, 3, 0,
/* 0x0080 */ 0, 76,137,228,255,160,128, 0, 0, 0,139, 7, 15,200,137, 7,
/* 0x0090 */ 131,238, 4, 72,141,127, 4,119,241,195,176, 1,235, 2,176, 74,
/* 0x00a0 */ 235, 2,176, 73,235, 2,176,153,235, 2,176, 6,235, 2,176, 5,
/* 0x00b0 */ 235, 2,176,197,235, 2,176, 3, 15,182,192, 13, 0, 0, 0, 2,
/* 0x00c0 */ 73,137,202, 15, 5,115, 4, 72,131,200,255,195, 81, 72, 57, 23,
/* 0x00d0 */ 76,139, 71, 8, 72,141, 74,255,115, 10,191,127, 0, 0, 0,232,
/* 0x00e0 */ 182,255,255,255, 72,131,249,255,116, 17, 65, 15,182, 0, 72,255,
/* 0x00f0 */ 201, 73,255,192,136, 6, 72,255,198,235,233, 72, 1, 87, 8, 72,
/* 0x0100 */ 41, 23, 88,195, 65, 85, 73,137,213, 65, 84, 73,137,204, 85, 72,
/* 0x0110 */ 137,253, 83, 72,137,243, 72,131,236, 40, 72,131, 62, 0, 15,132,
/* 0x0120 */ 212, 0, 0, 0, 72,141,116, 36, 16,186, 12, 0, 0, 0, 72,137,
/* 0x0130 */ 239,232,150,255,255,255,139, 84, 36, 16,139,116, 36, 20,133,210,
/* 0x0140 */ 117, 21,129,254, 85, 80, 88, 33,117, 17, 72,131,125, 0, 0, 15,
/* 0x0150 */ 132,163, 0, 0, 0,235, 4,133,246,117, 10,191,127, 0, 0, 0,
/* 0x0160 */ 232, 53,255,255,255, 57,214,119,242,137,208, 72, 59, 3,119,235,
/* 0x0170 */ 57,214, 72,139, 67, 8,115, 90, 68, 15,182, 68, 36, 24,137, 84,
/* 0x0180 */ 36, 12, 72,141, 76, 36, 12, 72,139,125, 8, 72,137,194, 65,255,
/* 0x0190 */ 213,133,192,117,198,139,116, 36, 12, 59,116, 36, 16,117,188, 15,
/* 0x01a0 */ 182, 76, 36, 25,132,201, 15,149,194, 49,192, 77,133,228, 15,149,
/* 0x01b0 */ 192,133,194,116, 15, 15,182, 84, 36, 26, 15,182,201, 72,139,123,
/* 0x01c0 */ 8, 65,255,212,139, 68, 36, 20, 72, 1, 69, 8, 72, 41, 69, 0,
/* 0x01d0 */ 235, 13,137,242, 72,137,239, 72,137,198,232,237,254,255,255,139,
/* 0x01e0 */ 84, 36, 16, 72,139, 3, 72, 1, 83, 8, 72, 41,208, 72,133,192,
/* 0x01f0 */ 72,137, 3,233, 38,255,255,255, 72,131,196, 40, 91, 93, 65, 92,
/* 0x0200 */ 65, 93,195, 65, 87, 73,137,215, 65, 86, 65, 85, 65, 84, 73,137,
/* 0x0210 */ 252, 73,131,196, 32, 85, 83, 72,131,236, 88, 72,139,132, 36,144,
/* 0x0220 */ 0, 0, 0, 72,137,124, 36, 56,137,116, 36, 52,137, 76, 36, 48,
/* 0x0230 */ 76,137, 68, 36, 40, 76,137, 76, 36, 32, 72,137, 68, 36, 24, 49,
/* 0x0240 */ 192, 59, 71, 16, 72,199, 68, 36, 16, 0, 0, 0, 0,199, 68, 36,
/* 0x0250 */ 12, 0, 0, 0, 0, 15,131,115, 1, 0, 0, 65,139, 4, 36,131,
/* 0x0260 */ 248, 25, 15,133, 34, 1, 0, 0, 73,139, 68, 36, 24, 73,139, 84,
/* 0x0270 */ 36, 48, 72,137,195, 72,137,197, 73,137,213,129,227,255, 15, 0,
/* 0x0280 */ 0, 73,137,198, 77, 3,116, 36, 32, 72, 41,221, 73, 1,221, 72,
/* 0x0290 */ 137, 84, 36, 64, 72,137, 68, 36, 72,116, 65,131,200,255, 68,139,
/* 0x02a0 */ 76, 36, 52, 69, 3, 76, 36, 40, 72,133,210, 15, 69, 68, 36, 48,
/* 0x02b0 */ 77,133,255,117, 10, 72,133,210,185, 18, 0, 0, 0,117, 5,185,
/* 0x02c0 */ 18, 16, 0, 0, 65,137,192,186, 3, 0, 0, 0, 76,137,238, 72,
/* 0x02d0 */ 137,239,232,219,253,255,255, 72, 57,197,117,113, 77,133,255,116,
/* 0x02e0 */ 47, 73,131,124, 36, 48, 0,116, 39, 73,131,124, 36, 40, 0,117,
/* 0x02f0 */ 8, 72,139, 84, 36, 40, 72,137, 42, 72,139, 76, 36, 24, 72,139,
/* 0x0300 */ 84, 36, 32, 72,141,116, 36, 64, 76,137,255,232,244,253,255,255,
/* 0x0310 */ 76,137,235, 74,141, 84, 45, 0, 72,247,219,129,227,255, 15, 0,
/* 0x0320 */ 0, 72,137,216, 72,133,219,116, 11,198, 2, 0, 72,255,194, 72,
/* 0x0330 */ 255,200,235,243, 77,133,237,116, 30, 65,139, 84, 36, 60, 76,137,
/* 0x0340 */ 238, 72,137,239,232, 85,253,255,255,133,192,116, 10,191,127, 0,
/* 0x0350 */ 0, 0,232, 67,253,255,255, 73,141, 68, 29, 0, 72, 1,197, 76,
/* 0x0360 */ 57,245,115, 77, 65,139, 84, 36, 60, 73, 41,238, 69, 49,201, 65,
/* 0x0370 */ 131,200,255,185, 18, 16, 0, 0, 76,137,246, 72,137,239,232, 47,
/* 0x0380 */ 253,255,255, 72, 57,197,116, 41,235,195,131,232, 4,131,248, 1,
/* 0x0390 */ 119, 31, 72,184, 4, 0, 0, 0, 42, 0, 0, 0, 73, 57, 68, 36,
/* 0x03a0 */ 8, 73,141, 84, 36, 16, 72, 15, 69, 84, 36, 16, 72,137, 84, 36,
/* 0x03b0 */ 16, 65,139, 68, 36, 4,255, 68, 36, 12,139, 84, 36, 12, 73, 1,
/* 0x03c0 */ 196, 72,139, 68, 36, 56, 59, 80, 16,233,135,254,255,255, 72,139,
/* 0x03d0 */ 68, 36, 16, 72,131,196, 88, 91, 93, 65, 92, 65, 93, 65, 94, 65,
/* 0x03e0 */ 95,195, 65, 86, 73,137,206, 49,201, 65, 85, 69, 49,237, 65, 84,
/* 0x03f0 */ 77,137,204, 85, 72,137,213, 72,141, 87, 24, 83, 76,137,195, 72,
/* 0x0400 */ 131,236, 64,139,127, 24, 72,137,116, 36, 56, 72,139, 68, 36, 56,
/* 0x0410 */ 72,141,116, 36, 16, 72,137, 84, 36, 40, 72,137, 84, 36, 8, 76,
/* 0x0420 */ 137,194, 72,137,108, 36, 24, 72,137,124, 36, 16, 72,141,124, 36,
/* 0x0430 */ 32, 72,131,232, 24, 72,137, 68, 36, 32, 72,137, 4, 36,232,193,
/* 0x0440 */ 252,255,255, 65, 83, 73,137,217,131,201,255, 49,246, 72,137,239,
/* 0x0450 */ 72,141, 84, 36, 8, 65, 84, 76,139,132, 36,128, 0, 0, 0,232,
/* 0x0460 */ 159,253,255,255, 65, 89, 65, 90,139,117, 16, 72,141, 85, 32, 72,
/* 0x0470 */ 137,195, 49,201, 57,241, 15,131,190, 0, 0, 0,131, 58, 14, 15,
/* 0x0480 */ 133,168, 0, 0, 0,139,122, 8, 49,246, 72,141, 60, 58, 49,210,
/* 0x0490 */ 232, 25,252,255,255,133,192, 65,137,196,120, 22, 68,137,233, 76,
/* 0x04a0 */ 137,242, 72,137,238, 68,137,231,232,249,251,255,255, 73, 57,198,
/* 0x04b0 */ 116, 16,191,127, 0, 0, 0,232,222,251,255,255, 68,139,107, 8,
/* 0x04c0 */ 235,218,139, 69, 0, 61,202,254,186,190,116, 7, 61,190,186,254,
/* 0x04d0 */ 202,117, 49, 72, 15,182,117, 7, 72,137,239, 72,141, 93, 8, 72,
/* 0x04e0 */ 107,246, 20,131,198, 8,232,159,251,255,255,139, 69, 4, 49,201,
/* 0x04f0 */ 57,193,115, 16,129, 59, 7, 0, 0, 1,116,192,255,193, 72,131,
/* 0x0500 */ 195, 20,235,236, 65, 80, 68,137,238, 69, 49,201, 69, 49,192, 68,
/* 0x0510 */ 137,225,106, 0, 49,210, 72,137,239,232,229,252,255,255, 68,137,
/* 0x0520 */ 231, 72,137,195,232,129,251,255,255, 94, 95,235, 13,139, 66, 4,
/* 0x0530 */ 255,193, 72, 1,194,233, 58,255,255,255, 72,131,196, 64, 72,137,
/* 0x0540 */ 216, 91, 93, 65, 92, 65, 93, 65, 94,195
/* 0x0090 */ 131,238, 4, 72,141,127, 4,119,241,195,176, 4,235, 2,176, 1,
/* 0x00a0 */ 235, 2,176, 74,235, 2,176, 73,235, 2,176,153,235, 2,176, 6,
/* 0x00b0 */ 235, 2,176, 5,235, 2,176,197,235, 2,176, 3, 15,182,192, 13,
/* 0x00c0 */ 0, 0, 0, 2, 73,137,202, 15, 5,115, 4, 72,131,200,255,195,
/* 0x00d0 */ 81, 72, 57, 23, 76,139, 71, 8, 72,141, 74,255,115, 10,191,127,
/* 0x00e0 */ 0, 0, 0,232,182,255,255,255, 72,131,249,255,116, 17, 65, 15,
/* 0x00f0 */ 182, 0, 72,255,201, 73,255,192,136, 6, 72,255,198,235,233, 72,
/* 0x0100 */ 1, 87, 8, 72, 41, 23, 88,195, 65, 85, 73,137,213, 65, 84, 73,
/* 0x0110 */ 137,204, 85, 72,137,253, 83, 72,137,243, 72,131,236, 40, 72,131,
/* 0x0120 */ 62, 0, 15,132,212, 0, 0, 0, 72,141,116, 36, 16,186, 12, 0,
/* 0x0130 */ 0, 0, 72,137,239,232,150,255,255,255,139, 84, 36, 16,139,116,
/* 0x0140 */ 36, 20,133,210,117, 21,129,254, 85, 80, 88, 33,117, 17, 72,131,
/* 0x0150 */ 125, 0, 0, 15,132,163, 0, 0, 0,235, 4,133,246,117, 10,191,
/* 0x0160 */ 127, 0, 0, 0,232, 53,255,255,255, 57,214,119,242,137,208, 72,
/* 0x0170 */ 59, 3,119,235, 57,214, 72,139, 67, 8,115, 90, 68, 15,182, 68,
/* 0x0180 */ 36, 24,137, 84, 36, 12, 72,141, 76, 36, 12, 72,139,125, 8, 72,
/* 0x0190 */ 137,194, 65,255,213,133,192,117,198,139,116, 36, 12, 59,116, 36,
/* 0x01a0 */ 16,117,188, 15,182, 76, 36, 25,132,201, 15,149,194, 49,192, 77,
/* 0x01b0 */ 133,228, 15,149,192,133,194,116, 15, 15,182, 84, 36, 26, 15,182,
/* 0x01c0 */ 201, 72,139,123, 8, 65,255,212,139, 68, 36, 20, 72, 1, 69, 8,
/* 0x01d0 */ 72, 41, 69, 0,235, 13,137,242, 72,137,239, 72,137,198,232,237,
/* 0x01e0 */ 254,255,255,139, 84, 36, 16, 72,139, 3, 72, 1, 83, 8, 72, 41,
/* 0x01f0 */ 208, 72,133,192, 72,137, 3,233, 38,255,255,255, 72,131,196, 40,
/* 0x0200 */ 91, 93, 65, 92, 65, 93,195, 65, 87, 73,137,215, 65, 86, 65, 85,
/* 0x0210 */ 65, 84, 73,137,252, 73,131,196, 32, 85, 83, 72,131,236, 88, 72,
/* 0x0220 */ 139,132, 36,144, 0, 0, 0, 72,137,124, 36, 56,137,116, 36, 52,
/* 0x0230 */ 137, 76, 36, 48, 76,137, 68, 36, 40, 76,137, 76, 36, 32, 72,137,
/* 0x0240 */ 68, 36, 24, 49,192, 59, 71, 16, 72,199, 68, 36, 16, 0, 0, 0,
/* 0x0250 */ 0,199, 68, 36, 12, 0, 0, 0, 0, 15,131,129, 1, 0, 0, 65,
/* 0x0260 */ 139, 4, 36,131,248, 25, 15,133, 48, 1, 0, 0, 73,139, 76, 36,
/* 0x0270 */ 32, 72,133,201, 15,132, 34, 1, 0, 0, 73,139, 68, 36, 24, 73,
/* 0x0280 */ 139, 84, 36, 48, 72,137,195, 72,137,197, 73,137,213,129,227,255,
/* 0x0290 */ 15, 0, 0, 72,137, 84, 36, 64, 72,137, 68, 36, 72, 72, 41,221,
/* 0x02a0 */ 73, 1,221, 76,141, 52, 8,116, 64, 77,133,255,117, 10, 72,133,
/* 0x02b0 */ 210,185, 18, 0, 0, 0,117, 5,185, 18, 16, 0, 0, 65,131,200,
/* 0x02c0 */ 255, 68,139, 76, 36, 52, 72,133,210, 68, 15, 69, 68, 36, 48, 69,
/* 0x02d0 */ 3, 76, 36, 40,186, 3, 0, 0, 0, 76,137,238, 72,137,239,232,
/* 0x02e0 */ 210,253,255,255, 72, 57,197,117,113, 77,133,255,116, 47, 73,131,
/* 0x02f0 */ 124, 36, 48, 0,116, 39, 73,131,124, 36, 40, 0,117, 8, 72,139,
/* 0x0300 */ 84, 36, 40, 72,137, 42, 72,139, 76, 36, 24, 72,139, 84, 36, 32,
/* 0x0310 */ 72,141,116, 36, 64, 76,137,255,232,235,253,255,255, 76,137,235,
/* 0x0320 */ 74,141, 84, 45, 0, 72,247,219,129,227,255, 15, 0, 0, 72,137,
/* 0x0330 */ 216, 72,133,219,116, 11,198, 2, 0, 72,255,194, 72,255,200,235,
/* 0x0340 */ 243, 77,133,237,116, 30, 65,139, 84, 36, 60, 76,137,238, 72,137,
/* 0x0350 */ 239,232, 76,253,255,255,133,192,116, 10,191,127, 0, 0, 0,232,
/* 0x0360 */ 58,253,255,255, 73,141, 68, 29, 0, 72, 1,197, 76, 57,245,115,
/* 0x0370 */ 82, 72,133,237,116, 77, 65,139, 84, 36, 60, 73, 41,238, 69, 49,
/* 0x0380 */ 201, 65,131,200,255,185, 18, 16, 0, 0, 76,137,246, 72,137,239,
/* 0x0390 */ 232, 33,253,255,255, 72, 57,197,116, 41,235,190,131,232, 4,131,
/* 0x03a0 */ 248, 1,119, 31, 72,184, 4, 0, 0, 0, 42, 0, 0, 0, 73, 57,
/* 0x03b0 */ 68, 36, 8, 73,141, 84, 36, 16, 72, 15, 69, 84, 36, 16, 72,137,
/* 0x03c0 */ 84, 36, 16, 65,139, 68, 36, 4,255, 68, 36, 12,139, 84, 36, 12,
/* 0x03d0 */ 73, 1,196, 72,139, 68, 36, 56, 59, 80, 16,233,121,254,255,255,
/* 0x03e0 */ 72,139, 68, 36, 16, 72,131,196, 88, 91, 93, 65, 92, 65, 93, 65,
/* 0x03f0 */ 94, 65, 95,195, 65, 86, 73,137,206, 49,201, 65, 85, 69, 49,237,
/* 0x0400 */ 65, 84, 77,137,204, 85, 72,137,213, 72,141, 87, 24, 83, 76,137,
/* 0x0410 */ 195, 72,131,236, 64,139,127, 24, 72,137,116, 36, 56, 72,139, 68,
/* 0x0420 */ 36, 56, 72,141,116, 36, 16, 72,137, 84, 36, 40, 72,137, 84, 36,
/* 0x0430 */ 8, 76,137,194, 72,137,108, 36, 24, 72,137,124, 36, 16, 72,141,
/* 0x0440 */ 124, 36, 32, 72,131,232, 24, 72,137, 68, 36, 32, 72,137, 4, 36,
/* 0x0450 */ 232,179,252,255,255, 65, 83, 73,137,217,131,201,255, 49,246, 72,
/* 0x0460 */ 137,239, 72,141, 84, 36, 8, 65, 84, 76,139,132, 36,128, 0, 0,
/* 0x0470 */ 0,232,145,253,255,255, 65, 89, 65, 90,139,117, 16, 72,141, 85,
/* 0x0480 */ 32, 72,137,195, 49,201, 57,241, 15,131,190, 0, 0, 0,131, 58,
/* 0x0490 */ 14, 15,133,168, 0, 0, 0,139,122, 8, 49,246, 72,141, 60, 58,
/* 0x04a0 */ 49,210,232, 11,252,255,255,133,192, 65,137,196,120, 22, 68,137,
/* 0x04b0 */ 233, 76,137,242, 72,137,238, 68,137,231,232,235,251,255,255, 73,
/* 0x04c0 */ 57,198,116, 16,191,127, 0, 0, 0,232,208,251,255,255, 68,139,
/* 0x04d0 */ 107, 8,235,218,139, 69, 0, 61,202,254,186,190,116, 7, 61,190,
/* 0x04e0 */ 186,254,202,117, 49, 72, 15,182,117, 7, 72,137,239, 72,141, 93,
/* 0x04f0 */ 8, 72,107,246, 20,131,198, 8,232,141,251,255,255,139, 69, 4,
/* 0x0500 */ 49,201, 57,193,115, 16,129, 59, 7, 0, 0, 1,116,192,255,193,
/* 0x0510 */ 72,131,195, 20,235,236, 65, 80, 68,137,238, 69, 49,201, 69, 49,
/* 0x0520 */ 192, 68,137,225,106, 0, 49,210, 72,137,239,232,215,252,255,255,
/* 0x0530 */ 68,137,231, 72,137,195,232,115,251,255,255, 94, 95,235, 13,139,
/* 0x0540 */ 66, 4,255,193, 72, 1,194,233, 58,255,255,255, 72,131,196, 64,
/* 0x0550 */ 72,137,216, 91, 93, 65, 92, 65, 93, 65, 94,195
};

View File

@ -86,6 +86,8 @@ SYS_munmap =0x49
SYS_mprotect =0x4a
// lazy jmps enable compression of this code
write: .globl write
mov $SYS_write,%al; jmp 2+ 0f; 0:
exit: .globl exit
mov $SYS_exit,%al; jmp 2+ 0f; 0:
mprotect: .globl mprotect

View File

@ -32,6 +32,9 @@
#include "include/darwin.h"
#ifndef DEBUG /*{*/
#define DEBUG 0
#endif /*}*/
/*************************************************************************
// configuration section
@ -41,6 +44,138 @@
// it at an address different from it load address: there must be no
// static data, and no string constants.
#if !DEBUG /*{*/
#define DPRINTF(a) /* empty: no debug drivel */
#define DEBUG_STRCON(name, value) /* empty */
#else /*}{ DEBUG */
extern int write(int, void const *, size_t);
#if 0
#include "stdarg.h"
#else
#define va_arg __builtin_va_arg
#define va_end __builtin_va_end
#define va_list __builtin_va_list
#define va_start __builtin_va_start
#endif
#if defined(__i386__) || defined(__x86_64__) /*{*/
#define PIC_STRING(value, var) \
__asm__ __volatile__ ( \
"call 0f; .asciz \"" value "\"; \
0: pop %0;" : "=r"(var) : \
)
#elif defined(__arm__) /*}{*/
#define PIC_STRING(value, var) \
__asm__ __volatile__ ( \
"mov %0,pc; b 0f; \
.asciz \"" value "\"; .balign 4; \
0: " : "=r"(var) \
)
#elif defined(__mips__) /*}{*/
#define PIC_STRING(value, var) \
__asm__ __volatile__ ( \
".set noreorder; bal 0f; move %0,$31; .set reorder; \
.asciz \"" value "\"; .balign 4; \
0: " \
: "=r"(var) : : "ra" \
)
#endif /*}*/
#define DEBUG_STRCON(name, strcon) \
static char const *name(void) { \
register char const *rv; PIC_STRING(strcon, rv); \
return rv; \
}
#ifdef __arm__ /*{*/
extern unsigned div10(unsigned);
#else /*}{*/
static unsigned
div10(unsigned x)
{
return x / 10u;
}
#endif /*}*/
static int
unsimal(unsigned x, char *ptr, int n)
{
if (10<=x) {
unsigned const q = div10(x);
x -= 10 * q;
n = unsimal(q, ptr, n);
}
ptr[n] = '0' + x;
return 1+ n;
}
static int
decimal(int x, char *ptr, int n)
{
if (x < 0) {
x = -x;
*ptr++ = '-'; ++n;
}
return unsimal(x, ptr, n);
}
DEBUG_STRCON(STR_hex, "0123456789abcdef");
static int
heximal(unsigned long x, char *ptr, int n)
{
if (16<=x) {
n = heximal(x>>4, ptr, n);
x &= 0xf;
}
ptr[n] = STR_hex()[x];
return 1+ n;
}
#define DPRINTF(a) dprintf a
static int
dprintf(char const *fmt, ...)
{
char c;
int n= 0;
char *ptr;
char buf[20];
va_list va; va_start(va, fmt);
ptr= &buf[0];
while (0!=(c= *fmt++)) if ('%'!=c) goto literal;
else switch (c= *fmt++) {
default: {
literal:
n+= write(2, fmt-1, 1);
} break;
case 0: goto done; /* early */
case 'u': {
n+= write(2, buf, unsimal(va_arg(va, unsigned), buf, 0));
} break;
case 'd': {
n+= write(2, buf, decimal(va_arg(va, int), buf, 0));
} break;
case 'p': {
buf[0] = '0';
buf[1] = 'x';
n+= write(2, buf, heximal((unsigned long)va_arg(va, void *), buf, 2));
} break;
case 'x': {
buf[0] = '0';
buf[1] = 'x';
n+= write(2, buf, heximal(va_arg(va, int), buf, 2));
} break;
}
done:
va_end(va);
return n;
}
#endif /*}*/
/*************************************************************************
// "file" util
@ -51,13 +186,17 @@ typedef struct {
void *buf;
} Extent;
DEBUG_STRCON(STR_xread, "xread %%p(%%x %%p) %%p %%x\\n")
DEBUG_STRCON(STR_xreadfail, "xreadfail %%p(%%x %%p) %%p %%x\\n")
static void
xread(Extent *x, void *buf, size_t count)
{
unsigned char *p=x->buf, *q=buf;
size_t j;
DPRINTF((STR_xread(), x, x->size, x->buf, buf, count));
if (x->size < count) {
DPRINTF((STR_xreadfail(), x, x->size, x->buf, buf, count));
exit(127);
}
for (j = count; 0!=j--; ++p, ++q) {
@ -77,9 +216,12 @@ xread(Extent *x, void *buf, size_t count)
#define err_exit(a) goto error
#else //}{ save debugging time
#define ERR_LAB /*empty*/
DEBUG_STRCON(STR_exit, "err_exit %%x\\n");
static void
err_exit(int a)
{
DPRINTF((STR_exit(), a));
(void)a; // debugging convenience
exit(127);
}
@ -122,6 +264,10 @@ typedef int f_expand(
const nrv_byte *, nrv_uint,
nrv_byte *, nrv_uint *, unsigned );
DEBUG_STRCON(STR_unpackExtent,
"unpackExtent in=%%p(%%x %%p) out=%%p(%%x %%p) %%p %%p\\n");
DEBUG_STRCON(STR_err5, "sz_cpr=%%x sz_unc=%%x xo->size=%%x\\n");
static void
unpackExtent(
Extent *const xi, // input
@ -130,6 +276,8 @@ unpackExtent(
f_unfilter *f_unf
)
{
DPRINTF((STR_unpackExtent(),
xi, xi->size, xi->buf, xo, xo->size, xo->buf, f_decompress, f_unf));
while (xo->size) {
struct b_info h;
// Note: if h.sz_unc == h.sz_cpr then the block was not
@ -150,6 +298,7 @@ ERR_LAB
}
if (h.sz_cpr > h.sz_unc
|| h.sz_unc > xo->size ) {
DPRINTF((STR_err5(), h.sz_cpr, h.sz_unc, xo->size));
err_exit(5);
}
// Now we have:
@ -314,6 +463,11 @@ extern void *mmap(void *, size_t, unsigned, unsigned, int, off_t);
ssize_t pread(int, void *, size_t, off_t);
extern void bswap(void *, unsigned);
DEBUG_STRCON(STR_mmap,
"mmap addr=%%p len=%%p prot=%%x flags=%%x fd=%%d off=%%p\\n");
DEBUG_STRCON(STR_do_xmap,
"do_xmap fdi=%%x mhdr=%%p xi=%%p(%%x %%p) f_unf=%%p\\n")
static Mach_AMD64_thread_state const *
do_xmap(
Mach_header64 const *const mhdr,
@ -329,9 +483,12 @@ do_xmap(
Mach_AMD64_thread_state const *entry = 0;
unsigned j;
DPRINTF((STR_do_xmap(),
fdi, mhdr, xi, (xi? xi->size: 0), (xi? xi->buf: 0), f_unf));
for ( j=0; j < mhdr->ncmds; ++j,
(sc = (Mach_segment_command const *)(sc->cmdsize + (void const *)sc))
) if (LC_SEGMENT_64==sc->cmd) {
) if (LC_SEGMENT_64==sc->cmd && sc->vmsize!=0) {
Extent xo;
size_t mlen = xo.size = sc->filesize;
unsigned char *addr = xo.buf = (unsigned char *)sc->vmaddr;
@ -340,11 +497,17 @@ do_xmap(
addr -= frag;
mlen += frag;
if (0!=mlen && addr != mmap(addr, mlen, VM_PROT_READ | VM_PROT_WRITE,
MAP_FIXED | MAP_PRIVATE |
((xi || 0==sc->filesize) ? MAP_ANON : 0),
((0==sc->filesize) ? MAP_ANON_FD : fdi), sc->fileoff + fat_offset) ) {
err_exit(8);
if (0!=mlen) {
unsigned const prot = VM_PROT_READ | VM_PROT_WRITE;
unsigned const flags = MAP_FIXED | MAP_PRIVATE |
((xi || 0==sc->filesize) ? MAP_ANON : 0);
int const fdm = ((0==sc->filesize) ? MAP_ANON_FD : fdi);
off_t const offset = sc->fileoff + fat_offset;
DPRINTF((STR_mmap(), addr, mlen, prot, flags, fdm, offset));
if (addr != mmap(addr, mlen, prot, flags, fdm, offset)) {
err_exit(8);
}
}
if (xi && 0!=sc->filesize) {
if (0==sc->fileoff /*&& 0!=mhdrpp*/) {
@ -365,7 +528,7 @@ ERR_LAB
0!=addr &&
#endif /*}*/
addr < haddr) { // need pages for .bss
if (addr != mmap(addr, haddr - addr, sc->initprot,
if (0!=addr && addr != mmap(addr, haddr - addr, sc->initprot,
MAP_FIXED | MAP_PRIVATE | MAP_ANON, MAP_ANON_FD, 0 ) ) {
err_exit(9);
}
@ -389,6 +552,10 @@ extern void spin(void *, ...);
//
**************************************************************************/
DEBUG_STRCON(STR_upx_main,
"upx_main szc=%%x f_dec=%%p f_unf=%%p "
" xo=%%p(%%x %%p) xi=%%p(%%x %%p) mhdrpp=%%p\\n")
Mach_AMD64_thread_state const *
upx_main(
struct l_info const *const li,
@ -409,6 +576,10 @@ upx_main(
xo.size = ((struct b_info const *)(void const *)xi.buf)->sz_unc;
xi0 = xi;
DPRINTF((STR_upx_main(),
sz_compressed, f_decompress, f_unf, &xo, xo.size, xo.buf,
&xi, xi.size, xi.buf, mhdrpp));
// Uncompress Macho headers
unpackExtent(&xi, &xo, f_decompress, 0); // never filtered?