From 8fd5479f325da6cca057542b7e891206d86b6e58 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Tue, 4 Oct 2016 19:58:04 -0700 Subject: [PATCH] Templates for ::pack4; i386 now packs, needs work before running. modified: p_mach.cpp modified: p_mach.h new file: stub/i386-darwin.macho-upxmain.exe new file: stub/i386-darwin.macho-upxmain.h new file: stub/src/i386-darwin.macho-upxmain.c --- src/p_mach.cpp | 120 +--- src/p_mach.h | 113 +++- src/stub/i386-darwin.macho-upxmain.exe | Bin 0 -> 4936 bytes src/stub/i386-darwin.macho-upxmain.h | 348 +++++++++++ src/stub/src/i386-darwin.macho-upxmain.c | 751 +++++++++++++++++++++++ 5 files changed, 1229 insertions(+), 103 deletions(-) create mode 100755 src/stub/i386-darwin.macho-upxmain.exe create mode 100644 src/stub/i386-darwin.macho-upxmain.h create mode 100644 src/stub/src/i386-darwin.macho-upxmain.c diff --git a/src/p_mach.cpp b/src/p_mach.cpp index 5ea06c57..58440561 100644 --- a/src/p_mach.cpp +++ b/src/p_mach.cpp @@ -39,6 +39,8 @@ static const static const #include "stub/i386-darwin.macho-fold.h" static const +#include "stub/i386-darwin.macho-upxmain.h" +static const #include "stub/i386-darwin.dylib-entry.h" static const @@ -46,9 +48,9 @@ static const static const #include "stub/amd64-darwin.macho-fold.h" static const -#include "stub/amd64-darwin.dylib-entry.h" -static const #include "stub/amd64-darwin.macho-upxmain.h" +static const +#include "stub/amd64-darwin.dylib-entry.h" static const #include "stub/arm.v5a-darwin.macho-entry.h" @@ -601,52 +603,9 @@ void PackMachPPC64LE::pack4(OutputFile *fo, Filter &ft) // append PackHeader #undef PAGE_SIZE64 #define PAGE_MASK64 (~(upx_uint64_t)0<<12) #define PAGE_SIZE64 ((upx_uint64_t)0-PAGE_MASK64) -void PackMachI386::pack4(OutputFile *fo, Filter &ft) // append PackHeader -{ - // offset of p_info in compressed file - overlay_offset = sizeof(mhdro) + sizeof(segZERO) - + sizeof(segXHDR) + sizeof(secXHDR) - + sizeof(segTEXT) + sizeof(secTEXT) - + sizeof(cmdUUID) - + sizeof(segLINK) + sizeof(threado) + sizeof(linfo); - if (my_filetype==Mach_header::MH_EXECUTE) { - overlay_offset += sizeof(linkitem); - } - super::pack4(fo, ft); - unsigned const t = fo->getBytesWritten(); - segTEXT.filesize = t; - segTEXT.vmsize += t; // utilize GAP + NO_LAP + sz_unc - sz_cpr - secTEXT.offset = overlay_offset - sizeof(linfo); - secTEXT.addr = segTEXT.vmaddr + secTEXT.offset; - secTEXT.size = segTEXT.filesize - secTEXT.offset; - secXHDR.offset = overlay_offset - sizeof(linfo); - if (my_filetype==Mach_header::MH_EXECUTE) { - secXHDR.offset -= sizeof(linkitem); - } - secXHDR.addr += secXHDR.offset; - unsigned foff1 = (PAGE_MASK & (~PAGE_MASK + segTEXT.filesize)); - if (foff1 < segTEXT.vmsize) - foff1 += PAGE_SIZE; // codesign disallows overhang - segLINK.fileoff = foff1; - segLINK.vmaddr = segTEXT.vmaddr + foff1; - fo->seek(foff1 - 1, SEEK_SET); fo->write("", 1); - fo->seek(sizeof(mhdro), SEEK_SET); - fo->rewrite(&segZERO, sizeof(segZERO)); - fo->rewrite(&segXHDR, sizeof(segXHDR)); - fo->rewrite(&secXHDR, sizeof(secXHDR)); - fo->rewrite(&segTEXT, sizeof(segTEXT)); - fo->rewrite(&secTEXT, sizeof(secTEXT)); - fo->rewrite(&cmdUUID, sizeof(cmdUUID)); - fo->rewrite(&segLINK, sizeof(segLINK)); - fo->rewrite(&threado, sizeof(threado)); - if (my_filetype==Mach_header::MH_EXECUTE) { - fo->rewrite(&linkitem, sizeof(linkitem)); - } - fo->rewrite(&linfo, sizeof(linfo)); -} - -void PackMachAMD64::pack4(OutputFile *fo, Filter &ft) // append PackHeader +template +void PackMachBase::pack4(OutputFile *fo, Filter &ft) // append PackHeader { N_Mach::Mach_main_command cmdMAIN; // offset of p_info in compressed file @@ -693,7 +652,7 @@ void PackMachAMD64::pack4(OutputFile *fo, Filter &ft) // append PackHeader fo->rewrite(&secTEXT, sizeof(secTEXT)); fo->rewrite(&cmdUUID, sizeof(cmdUUID)); fo->rewrite(&segLINK, sizeof(segLINK)); - fo->rewrite(&threado, sizeof(threado)); + threado_rewrite(fo); if (my_filetype==Mach_header::MH_EXECUTE) { fo->rewrite(&linkitem, sizeof(linkitem)); } @@ -719,6 +678,7 @@ void PackMachAMD64::pack4(OutputFile *fo, Filter &ft) // append PackHeader lcp_next = (Mach_command *)(sz_cmd + (char *)lcp); switch (lcp->cmd) { + case Mach_segment_command::LC_SEGMENT: // fall through case Mach_segment_command::LC_SEGMENT_64: { Mach_segment_command *const segptr = (Mach_segment_command *)lcp; if (!strcmp("__TEXT", segptr->segname)) { @@ -812,15 +772,11 @@ void PackMachAMD64::pack4(OutputFile *fo, Filter &ft) // append PackHeader skip = 1; } break; case Mach_segment_command::LC_MAIN: { - // Change to LC_UNIX_THREAD; known to be contiguous with last + // Replace later with LC_UNIX_THREAD. // LC_MAIN requires libSystem.B.dylib to provide the environment for main(), and CALLs the entryoff. // LC_UNIXTHREAD does not need libSystem.B.dylib, and JMPs to the .rip with %rsp/argc and argv= 8+%rsp - threado.cmd = Mach_segment_command::LC_UNIXTHREAD; - threado.cmdsize = sizeof(threado); - threado.flavor = my_thread_flavor; - threado.count = my_thread_state_word_count; - memset(&threado.state, 0, sizeof(threado.state)); - threado.state.rip = ((N_Mach::Mach_main_command const *)lcp)->entryoff + segTEXT.vmaddr; + threado_setPC(segTEXT.vmaddr + + (((N_Mach::Mach_main_command const *)lcp)->entryoff - segTEXT.fileoff)); skip = 1; } break; case Mach_segment_command::LC_LOAD_DYLIB: { @@ -836,9 +792,15 @@ void PackMachAMD64::pack4(OutputFile *fo, Filter &ft) // append PackHeader } break; case Mach_segment_command::LC_SOURCE_VERSION: { // copy from saved original memcpy(lcp, &cmdSRCVER, sizeof(cmdSRCVER)); + if (Mach_segment_command::LC_SOURCE_VERSION != cmdSRCVER.cmd) { + skip = 1; // was not seen + } } break; case Mach_segment_command::LC_VERSION_MIN_MACOSX: { // copy from saved original memcpy(lcp, &cmdVERMIN, sizeof(cmdVERMIN)); + if (Mach_segment_command::LC_VERSION_MIN_MACOSX != cmdVERMIN.cmd) { + skip = 1; // was not seen + } } break; } // end switch @@ -857,12 +819,13 @@ next: } // end for // Add LC_UNIX_THREAD + unsigned const sz_threado = threado_size(); mhp->ncmds += 1; - mhp->sizeofcmds += sizeof(threado); + mhp->sizeofcmds += sz_threado; fo->seek(0, SEEK_SET); fo->rewrite(mhp, tail - (char *)mhp); - fo->rewrite(&threado, sizeof(threado)); - tail += sizeof(threado); + threado_rewrite(fo); + tail += sz_threado; // // Zero any remaining tail. if (tail < lcp_end) { @@ -1205,11 +1168,20 @@ void PackMachI386::pack3(OutputFile *fo, Filter &ft) // append loader unsigned len = fo->getBytesWritten(); fo->write(&zero, 3& (0u-len)); len += (3& (0u-len)); + disp = len - sz_mach_headers; fo->write(&disp, sizeof(disp)); + len += sizeof(disp); - threado.state.eip = len + sizeof(disp) + segTEXT.vmaddr; /* entry address */ + char page[~PAGE_MASK]; memset(page, 0, sizeof(page)); + fo->write(page, ~PAGE_MASK & -len); + len += ~PAGE_MASK & -len; + segLINK.fileoff = len; + + threado.state.eip = len + segTEXT.vmaddr; /* entry address */ super::pack3(fo, ft); + len = fo->getBytesWritten(); + fo->write(&zero, 7& (0u-len)); } void PackMachAMD64::pack3(OutputFile *fo, Filter &ft) // append loader @@ -1397,12 +1369,6 @@ unsigned PackMachBase::find_SEGMENT_gap( return lo - hi; } -template -void PackMachBase::pack4(OutputFile *fo, Filter &ft) -{ - PackUnix::pack4(fo, ft); // FIXME super() does not work? -} - template void PackMachBase::pack3(OutputFile *fo, Filter &ft) { @@ -1821,24 +1787,6 @@ void PackMachBase::unpack(OutputFile *fo) delete [] mhdr; } -template -upx_uint64_t PackMachBase::getEntryVMA(Mach_command const *ptr) -{ - return ptr->cmd; // FIXME must be specialized -} - -upx_uint64_t PackMachI386::getEntryVMA(Mach_command const *ptr) -{ - Mach_thread_command const *tc = (Mach_thread_command const *)ptr; - return tc->state.eip; -} - -upx_uint64_t PackMachAMD64::getEntryVMA(Mach_command const *ptr) -{ - Mach_thread_command const *tc = (Mach_thread_command const *)ptr; - return tc->state.rip; -} - // The prize is the value of overlay_offset: the offset of compressed data template int PackMachBase::canUnpack() @@ -1883,7 +1831,7 @@ int PackMachBase::canUnpack() } } else if (Mach_segment_command::LC_UNIXTHREAD==ptr->cmd) { - rip = getEntryVMA(ptr); + rip = entryVMA; } } if (0 == style || 0 == offLINK) { @@ -2113,10 +2061,10 @@ bool PackMachBase::canPack() {CPU_TYPE_I386, MH_EXECUTE, sizeof(stub_i386_darwin_macho_entry), sizeof(stub_i386_darwin_macho_fold), - 0, + sizeof(stub_i386_darwin_macho_upxmain_exe), stub_i386_darwin_macho_entry, stub_i386_darwin_macho_fold, - 0 + stub_i386_darwin_macho_upxmain_exe }, {CPU_TYPE_I386, MH_DYLIB, sizeof(stub_i386_darwin_dylib_entry), 0, 0, diff --git a/src/p_mach.h b/src/p_mach.h index 8030c076..91b093cb 100644 --- a/src/p_mach.h +++ b/src/p_mach.h @@ -293,6 +293,19 @@ __packed_struct(Mach_uuid_command) unsigned char uuid[16]; __packed_struct_end() +template +__packed_struct(Mach_thread_command) + typedef typename TMachITypes::Word Word; + + Word cmd; /* LC_THREAD or LC_UNIXTHREAD */ + Word cmdsize; /* total size of this command */ + Word flavor; + Word count; /* sizeof(following_thread_state)/4 */ + TMachThreadState state; +#define WANT_MACH_THREAD_ENUM 1 +#include "p_mach_enum.h" +__packed_struct_end() + typedef struct { upx_uint32_t cmd; // LC_MAIN; MH_EXECUTE only upx_uint32_t cmdsize; // 24 @@ -361,6 +374,10 @@ typedef struct { upx_uint32_t datasize; } Mach_data_in_code_command; +} // namespace N_Mach + +namespace N_Mach32 { + template __packed_struct(Mach_ppc_thread_state) typedef typename TMachITypes::Addr Addr; @@ -393,10 +410,6 @@ __packed_struct(Mach_i386_thread_state) Word ds, es, fs, gs; __packed_struct_end() -} // namespace N_Mach - -namespace N_Mach32 { - template __packed_struct(Mach_ARM_thread_state) typedef typename TMachITypes::Word Word; @@ -508,8 +521,8 @@ struct MachClass_32 typedef N_Mach::Mach_linkedit_data_command Mach_linkedit_data_command; typedef N_Mach::Mach_uuid_command Mach_uuid_command; - typedef N_Mach::Mach_ppc_thread_state Mach_ppc_thread_state; - typedef N_Mach::Mach_i386_thread_state Mach_i386_thread_state; + typedef N_Mach32::Mach_ppc_thread_state Mach_ppc_thread_state; + typedef N_Mach32::Mach_i386_thread_state Mach_i386_thread_state; typedef N_Mach32::Mach_ARM_thread_state Mach_ARM_thread_state; static void compileTimeAssertions() { @@ -667,10 +680,14 @@ public: virtual void pack1(OutputFile *, Filter &); // generate executable header virtual int pack2(OutputFile *, Filter &); // append compressed data virtual void pack3(OutputFile *, Filter &) = 0; // append loader - virtual void pack4(OutputFile *, Filter &) = 0; // append PackHeader + virtual void pack4(OutputFile *, Filter &) /*= 0*/; // append PackHeader virtual void pack4dylib(OutputFile *, Filter &, Addr init_address); + virtual int threado_size() const = 0; + virtual void threado_setPC(upx_uint64_t pc) = 0; + virtual void threado_rewrite(OutputFile *) = 0; + virtual void threado_write(OutputFile *) = 0; virtual void pack1_setup_threado(OutputFile *const fo) = 0; virtual void unpack(OutputFile *fo); @@ -691,10 +708,10 @@ protected: Filter const *ft ); virtual void defineSymbols(Filter const *); virtual void addStubEntrySections(Filter const *); - virtual upx_uint64_t getEntryVMA(Mach_command const *); static int __acc_cdecl_qsort compare_segment_command(void const *aa, void const *bb); + upx_uint64_t entryVMA; unsigned my_cputype; unsigned my_filetype; unsigned my_thread_flavor; @@ -795,6 +812,17 @@ protected: __packed_struct_end() Mach_thread_command threado; + int threado_size() const { return sizeof(threado); } + void threado_setPC(upx_uint64_t pc) { + memset(&threado, 0, sizeof(threado)); + threado.cmd = Mach_segment_command::LC_UNIXTHREAD; + threado.cmdsize = sizeof(threado); + threado.flavor = my_thread_flavor; + threado.count = my_thread_state_word_count; + threado.state.srr0 = pc; + } + void threado_rewrite(OutputFile *fo) { fo->rewrite(&threado, sizeof(threado)); } + void threado_write(OutputFile *fo) { fo->write(&threado, sizeof(threado)); } }; class PackMachPPC64LE : public PackMachBase @@ -827,6 +855,17 @@ protected: __packed_struct_end() Mach_thread_command threado; + int threado_size() const { return sizeof(threado); } + void threado_setPC(upx_uint64_t pc) { + memset(&threado, 0, sizeof(threado)); + threado.cmd = Mach_segment_command::LC_UNIXTHREAD; + threado.cmdsize = sizeof(threado); + threado.flavor = my_thread_flavor; + threado.count = my_thread_state_word_count; + threado.state64.srr0 = pc; + } + void threado_rewrite(OutputFile *fo) { fo->rewrite(&threado, sizeof(threado)); } + void threado_write(OutputFile *fo) { fo->write(&threado, sizeof(threado)); } }; class PackDylibPPC32 : public PackMachPPC32 @@ -874,10 +913,8 @@ protected: virtual void pack1_setup_threado(OutputFile *const fo); virtual void pack3(OutputFile *, Filter &); // append loader - virtual void pack4(OutputFile *, Filter &); // append PackHeader virtual Linker* newLinker() const; virtual void addStubEntrySections(Filter const *); - virtual upx_uint64_t getEntryVMA(Mach_command const *); __packed_struct(Mach_thread_command) LE32 cmd; /* LC_THREAD or LC_UNIXTHREAD */ @@ -890,6 +927,17 @@ protected: __packed_struct_end() Mach_thread_command threado; + int threado_size() const { return sizeof(threado); } + void threado_setPC(upx_uint64_t pc) { + memset(&threado, 0, sizeof(threado)); + threado.cmd = Mach_segment_command::LC_UNIXTHREAD; + threado.cmdsize = sizeof(threado); + threado.flavor = my_thread_flavor; + threado.count = my_thread_state_word_count; + threado.state.eip = pc; + } + void threado_rewrite(OutputFile *fo) { fo->rewrite(&threado, sizeof(threado)); } + void threado_write(OutputFile *fo) { fo->write(&threado, sizeof(threado)); } }; class PackDylibI386 : public PackMachI386 @@ -922,23 +970,32 @@ protected: virtual void pack1_setup_threado(OutputFile *const fo); virtual void pack3(OutputFile *, Filter &); // append loader - virtual void pack4(OutputFile *, Filter &); // append PackHeader virtual Linker* newLinker() const; virtual void addStubEntrySections(Filter const *); - virtual upx_uint64_t getEntryVMA(Mach_command const *ptr); - __packed_struct(Mach_thread_command) - LE32 cmd; /* LC_THREAD or LC_UNIXTHREAD */ - LE32 cmdsize; /* total size of this command */ - LE32 flavor; - LE32 count; /* sizeof(following_thread_state)/4 */ + typedef typename MachITypes::Word Word; + Word cmd; /* LC_THREAD or LC_UNIXTHREAD */ + Word cmdsize; /* total size of this command */ + Word flavor; + Word count; /* sizeof(following_thread_state)/4 */ Mach_AMD64_thread_state state; #define WANT_MACH_THREAD_ENUM 1 #include "p_mach_enum.h" __packed_struct_end() Mach_thread_command threado; + int threado_size() const { return sizeof(threado); } + void threado_setPC(upx_uint64_t pc) { + memset(&threado, 0, sizeof(threado)); + threado.cmd = Mach_segment_command::LC_UNIXTHREAD; + threado.cmdsize = sizeof(threado); + threado.flavor = my_thread_flavor; + threado.count = my_thread_state_word_count; + threado.state.rip = pc; + } + void threado_rewrite(OutputFile *fo) { fo->rewrite(&threado, sizeof(threado)); } + void threado_write(OutputFile *fo) { fo->write(&threado, sizeof(threado)); } }; class PackDylibAMD64 : public PackMachAMD64 @@ -987,6 +1044,17 @@ protected: __packed_struct_end() Mach_thread_command threado; + int threado_size() const { return sizeof(threado); } + void threado_setPC(upx_uint64_t pc) { + memset(&threado, 0, sizeof(threado)); + threado.cmd = Mach_segment_command::LC_UNIXTHREAD; + threado.cmdsize = sizeof(threado); + threado.flavor = my_thread_flavor; + threado.count = my_thread_state_word_count; + threado.state.pc = pc; + } + void threado_rewrite(OutputFile *fo) { fo->rewrite(&threado, sizeof(threado)); } + void threado_write(OutputFile *fo) { return fo->write(&threado, sizeof(threado)); } }; class PackMachARM64EL : public PackMachBase @@ -1020,6 +1088,17 @@ protected: __packed_struct_end() Mach_thread_command threado; + int threado_size() const { return sizeof(threado); } + void threado_setPC(upx_uint64_t pc) { + memset(&threado, 0, sizeof(threado)); + threado.cmd = Mach_segment_command::LC_UNIXTHREAD; + threado.cmdsize = sizeof(threado); + threado.flavor = my_thread_flavor; + threado.count = my_thread_state_word_count; + threado.state.pc = pc; + } + void threado_rewrite(OutputFile *fo) { fo->rewrite(&threado, sizeof(threado)); } + void threado_write(OutputFile *fo) { fo->write(&threado, sizeof(threado)); } }; class PackMachFat : public Packer diff --git a/src/stub/i386-darwin.macho-upxmain.exe b/src/stub/i386-darwin.macho-upxmain.exe new file mode 100755 index 0000000000000000000000000000000000000000..e300c5df4427d53879a016a382c008363eab18a1 GIT binary patch literal 4936 zcmeHLeQXrR6`%EOVq*^WLP_hS2AkIXaO(z6iYi|ysv)ewgbQbzvkk~uVK|Bn{)lfk zHgRb%>q|Nvt*S~y6t(>$Rivt|RHdpQh6Z}ZrZy?%OOE3TlwK;gTGwuCOht^LY=3WW z4@@If{i}a@b#LZ3Z|2SWn74B?dvW$}_p%vdK2R2D5$O8>3C1J|xD=#mk+mDt?P}E) zG%`;h13q4pToR;dTh+R)bNWdpBZr#wJ^~`W^k|wM>(tXY-F{ep#S@GjeUz~k3m%Md zk<1QdIGSd(bTqc?)*4&(w1WAcHuM%Swh{bZ<$>OeY^HwT_xiP4*Jik=3q9=lkcnGn z!~?m_70*AXuCK`WPI8{_8xd%dqp_LiGmITtm1+F98gr!3Pw~nFrO;l6`fE!V^JBt= zz!4BVw2nk@ljVcv>O=E?dKP08$@)Pnjd=UY{f!MPcOTfl8|@+x)T1-kr7vaL#KZGN z_(V23WW=0@=6V`Led%={k;%II^Fv4Vfw&%PernxQ(AC($STV+uF7kK&nUBDH1m+_! zAA$J@{9hsv<$tcOtuZG{gb@(+c|y$*VLuN~SsU|scv6I?%s!ikZElQ-vOKP)rj>wd zPb-h9qvm8z(6Pd{6;0Jv+mGWP*c=FeX{n>ZtNeyzu^pOORaVJX6a&m(FR|c9JFy{l)hdH3F_Gsg4EiM835e8`d$ zIhY+5Mk@Iwdv$tdu|W==Pb5Y3vYuzYJd>^Gt~Jjt4fVB8PvnSag4Ys`aRj=WUa`NL z79h8id^Qq^2xGV=Qf(`)=Wq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l`n0>4ekV z1k>(krvU)Dbmij*;@@ax)!VAm g`U$vv0_h0}xq1ntZ6)NH + + John F. Reiser + + */ + + +#define STUB_I386_DARWIN_MACHO_UPXMAIN_EXE_SIZE 4936 +#define STUB_I386_DARWIN_MACHO_UPXMAIN_EXE_ADLER32 0x8952e687 +#define STUB_I386_DARWIN_MACHO_UPXMAIN_EXE_CRC32 0x20b8b848 + +unsigned char stub_i386_darwin_macho_upxmain_exe[4936] = { +/* 0x0000 */ 206,250,237,254, 7, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, +/* 0x0010 */ 13, 0, 0, 0,152, 2, 0, 0,133, 0, 0, 1, 1, 0, 0, 0, +/* 0x0020 */ 56, 0, 0, 0, 95, 95, 80, 65, 71, 69, 90, 69, 82, 79, 0, 0, +/* 0x0030 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, +/* 0x0040 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0050 */ 0, 0, 0, 0, 1, 0, 0, 0,192, 0, 0, 0, 95, 95, 84, 69, +/* 0x0060 */ 88, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, +/* 0x0070 */ 0, 16, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 7, 0, 0, 0, +/* 0x0080 */ 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 95, 95,116,101, +/* 0x0090 */ 120,116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 95, 84, 69, +/* 0x00a0 */ 88, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 25, 0, 0, +/* 0x00b0 */ 151, 6, 0, 0, 33, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x00c0 */ 0, 0, 0, 0, 0, 4, 0,128, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x00d0 */ 95, 95,117,110,119,105,110,100, 95,105,110,102,111, 0, 0, 0, +/* 0x00e0 */ 95, 95, 84, 69, 88, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x00f0 */ 184, 31, 0, 0, 72, 0, 0, 0,184, 15, 0, 0, 2, 0, 0, 0, +/* 0x0100 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0110 */ 0, 0, 0, 0, 1, 0, 0, 0, 56, 0, 0, 0, 95, 95, 68, 65, +/* 0x0120 */ 84, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, +/* 0x0130 */ 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, +/* 0x0140 */ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, +/* 0x0150 */ 56, 0, 0, 0, 95, 95, 76, 73, 78, 75, 69, 68, 73, 84, 0, 0, +/* 0x0160 */ 0, 0, 0, 0, 0, 32, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, +/* 0x0170 */ 72, 3, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, +/* 0x0180 */ 0, 0, 0, 0, 34, 0, 0,128, 48, 0, 0, 0, 0, 0, 0, 0, +/* 0x0190 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x01a0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, +/* 0x01b0 */ 224, 0, 0, 0, 2, 0, 0, 0, 24, 0, 0, 0,224, 16, 0, 0, +/* 0x01c0 */ 29, 0, 0, 0, 60, 18, 0, 0, 12, 1, 0, 0, 11, 0, 0, 0, +/* 0x01d0 */ 80, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 13, 0, 0, 0, +/* 0x01e0 */ 15, 0, 0, 0, 28, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, +/* 0x01f0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0200 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0210 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, +/* 0x0220 */ 28, 0, 0, 0, 12, 0, 0, 0, 47,117,115,114, 47,108,105, 98, +/* 0x0230 */ 47,100,121,108,100, 0, 0, 0, 36, 0, 0, 0, 16, 0, 0, 0, +/* 0x0240 */ 0, 12, 10, 0, 0, 12, 10, 0, 42, 0, 0, 0, 16, 0, 0, 0, +/* 0x0250 */ 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0,128, 24, 0, 0, 0, +/* 0x0260 */ 255, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0270 */ 12, 0, 0, 0, 52, 0, 0, 0, 24, 0, 0, 0, 2, 0, 0, 0, +/* 0x0280 */ 0, 0,214, 4, 0, 0, 1, 0, 47,117,115,114, 47,108,105, 98, +/* 0x0290 */ 47,108,105, 98, 83,121,115,116,101,109, 46, 66, 46,100,121,108, +/* 0x02a0 */ 105, 98, 0, 0, 41, 0, 0, 0, 16, 0, 0, 0,224, 16, 0, 0, +/* 0x02b0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x02c0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x02d0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x02e0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x02f0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0300 */ 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, +/* 0x0870 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0880 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0890 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x08a0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x08b0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x08c0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x08d0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x08e0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x08f0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0900 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0910 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x0920 */ 0, 85,137,229, 83, 87, 86,131,236, 44,139,117, 16,139, 93, 24, +/* 0x0930 */ 139, 69, 8,139, 77, 12,137, 77,240,141, 72, 24,137, 77,236,139, +/* 0x0940 */ 77,240,131,193,232,137, 77,232,137,117,228,139, 64, 24,137, 69, +/* 0x0950 */ 224,242, 15, 16, 69,232,242, 15, 17, 69,216,131,236, 8, 49,255, +/* 0x0960 */ 141, 77,232,141, 85,224, 87, 83,232, 21, 1, 0, 0,131,196, 16, +/* 0x0970 */ 131,236, 8,141, 69,216, 49,210,137,241,255,117, 28, 83,255,117, +/* 0x0980 */ 32,106,255, 80, 87,232,236, 1, 0, 0,137,242,131,196, 32,137, +/* 0x0990 */ 195,139, 74, 16,133,201, 15,132,220, 0, 0, 0,139,117, 20,141, +/* 0x09a0 */ 66, 28,131, 56, 14,116, 13, 71, 3, 64, 4, 57,207,114,243,233, +/* 0x09b0 */ 196, 0, 0, 0, 3, 64, 8,131,236, 4, 49,219, 83, 83, 80,232, +/* 0x09c0 */ 213, 5, 0, 0,131,196, 16,137, 69,212,133,192,120,119,139, 69, +/* 0x09d0 */ 16,141, 64, 16,137, 69,208, 49,255,131,236, 12, 83, 87, 86,255, +/* 0x09e0 */ 117, 16,255,117,212,232,167, 5, 0, 0,131,196, 32, 57,240,117, +/* 0x09f0 */ 84,139, 77, 16,139, 1, 61,190,186,254,202,116, 7, 61,202,254, +/* 0x0a00 */ 186,190,117, 78, 15,182, 65, 7,141, 4,128,141, 4,133, 8, 0, +/* 0x0a10 */ 0, 0,131,236, 8, 80, 81,137,207,232, 59, 5, 0, 0,131,196, +/* 0x0a20 */ 16,139, 71, 4, 49,201,133,192,139, 85,208,116, 24,131,122,248, +/* 0x0a30 */ 7,116, 10, 65,131,194, 20, 57,193,114,242,235, 8,139, 58, 49, +/* 0x0a40 */ 219,133,255,117,148,131,236, 12,106,127,232, 54, 5, 0, 0,131, +/* 0x0a50 */ 196, 16,131,236, 8, 49,192,137,250, 80, 80, 80,139,117,212, 86, +/* 0x0a60 */ 80, 83,232, 15, 1, 0, 0,131,196, 32,137,195,131,236, 12, 86, +/* 0x0a70 */ 232, 32, 5, 0, 0,131,196, 16,137,216,131,196, 44, 94, 95, 91, +/* 0x0a80 */ 93,195, 85,137,229, 83, 87, 86,131,236, 28,137,215,137, 77,224, +/* 0x0a90 */ 131, 63, 0, 15,132,200, 0, 0, 0,131,236, 12,139, 77,224,141, +/* 0x0aa0 */ 85,232,106, 12,232,102, 4, 0, 0,131,196, 16,139, 93,232,139, +/* 0x0ab0 */ 117,236,133,219, 15,132,151, 0, 0, 0,141, 70,255, 57,216, 15, +/* 0x0ac0 */ 131,164, 0, 0, 0, 59, 31, 15,135,156, 0, 0, 0, 57,222,115, +/* 0x0ad0 */ 93,137, 93,228,139, 69,240,137, 69,220, 15,182,192,131,236, 12, +/* 0x0ae0 */ 80,141, 69,228, 80,255,119, 4, 86,139, 69,224,255,112, 4,255, +/* 0x0af0 */ 85, 8,131,196, 32,133,192,117,112, 57, 93,228,117,107,131,125, +/* 0x0b00 */ 12, 0,139, 77,220,116, 29, 15,182,197,102,133,192,116, 21,193, +/* 0x0b10 */ 233, 16, 15,183,192, 15,182,201, 80, 81, 83,255,119, 4,255, 85, +/* 0x0b20 */ 12,131,196, 16,139, 69,224, 1,112, 4, 41, 48,235, 18,139, 87, +/* 0x0b30 */ 4,131,236, 12,139, 77,224, 86,232,210, 3, 0, 0,131,196, 16, +/* 0x0b40 */ 1, 95, 4,139, 7, 41,216,137, 7, 15,133, 74,255,255,255,235, +/* 0x0b50 */ 16,129,254, 85, 80, 88, 33,117, 16,139, 69,224,131, 56, 0,117, +/* 0x0b60 */ 8,131,196, 28, 94, 95, 91, 93,195,131,236, 12,106,127,232, 18, +/* 0x0b70 */ 4, 0, 0,131,196, 16, 85,137,229, 83, 87, 86,131,236, 76,137, +/* 0x0b80 */ 85,188,137, 77,200,139, 73, 16,137, 77,224, 49,192,133,201, 15, +/* 0x0b90 */ 132, 85, 2, 0, 0,131,125, 12, 0, 15,149,193, 15,182,201,141, +/* 0x0ba0 */ 12, 73,137, 77,184,139, 77,200,141, 89, 28, 49,210, 49,192, 49, +/* 0x0bb0 */ 201, 49,246,137,117,228,137,215,137,206,139, 11,141, 81,252,131, +/* 0x0bc0 */ 250, 2, 15,130,213, 0, 0, 0,129,249, 40, 0, 0,128, 15,132, +/* 0x0bd0 */ 234, 0, 0, 0,131,249, 1,137,250, 15,133, 10, 1, 0, 0,139, +/* 0x0be0 */ 123, 28,133,255,137,241, 15,132,238, 1, 0, 0,137,125,208,137, +/* 0x0bf0 */ 85,216,137, 69,204,139, 67, 36,137, 69,232,139,123, 24, 1,207, +/* 0x0c00 */ 137,125,212,137,125,236,137,254,129,230,255, 15, 0, 0,137,242, +/* 0x0c10 */ 41,215, 1,194, 15,132,214, 0, 0, 0,137, 77,220,139, 77,184, +/* 0x0c20 */ 141, 12, 10,137, 77,196,133,255,137, 85,224,186, 2, 0, 0, 0, +/* 0x0c30 */ 185, 18, 0, 0, 0, 15, 69,209,133,192, 15,148,193, 15,182,201, +/* 0x0c40 */ 193,225, 12,131,125, 12, 0,190, 0, 16, 0, 0, 15, 69,206, 9, +/* 0x0c50 */ 209,131,248, 1, 25,192, 11, 69, 16,137, 69,192,139, 83, 32, 3, +/* 0x0c60 */ 85,188,139, 69, 8,190, 0, 0, 0, 0, 17,240,131,236, 4, 80, +/* 0x0c70 */ 82,255,117,192, 81,106, 3,255,117,196, 87,232, 29, 3, 0, 0, +/* 0x0c80 */ 139, 85,224,131,196, 32,133,255,139, 77,220, 15, 68,200,137, 77, +/* 0x0c90 */ 220,131,248,255,137,199,117, 91,233, 85, 1, 0, 0,131,123, 8, +/* 0x0ca0 */ 1,137,241,117, 61,131,123, 12, 16,137,250,139,117,228, 15,133, +/* 0x0cb0 */ 41, 1, 0, 0,139, 67, 56, 1,200,233, 31, 1, 0, 0,139, 67, +/* 0x0cc0 */ 8,137,250, 57, 66, 32,137,241, 15,135, 12, 1, 0, 0, 59, 66, +/* 0x0cd0 */ 36,139,117,228, 15,131, 3, 1, 0, 0, 3, 66, 24,233,251, 0, +/* 0x0ce0 */ 0, 0,137,250,233,241, 0, 0, 0,137,241,233,234, 0, 0, 0, +/* 0x0cf0 */ 137, 77,220,131,125, 12, 0,116, 47,131,123, 36, 0,116, 41,137, +/* 0x0d00 */ 214,131,123, 32, 0,117, 8,139, 69, 20,137, 56,137, 93,216,131, +/* 0x0d10 */ 236, 8,139, 77, 12,141, 85,232,255,117, 28,255,117, 24,232, 95, +/* 0x0d20 */ 253,255,255,131,196, 16,137,242,137,214,247,222,129,230,255, 15, +/* 0x0d30 */ 0, 0,116, 22,141, 4, 23,131,236, 8, 86, 80,137, 85,224,232, +/* 0x0d40 */ 44, 2, 0, 0,139, 85,224,131,196, 16,133,210,116, 30,131,236, +/* 0x0d50 */ 4,255,115, 44, 82, 87,137, 85,224,232, 43, 2, 0, 0,139, 85, +/* 0x0d60 */ 224,131,196, 16,133,192, 15,133,134, 0, 0, 0,139, 69,212, 3, +/* 0x0d70 */ 69,208, 1,214, 1,247, 57,199,115, 41,133,255,116, 74,137,193, +/* 0x0d80 */ 41,249,131,236, 4, 49,192, 80, 80,106,255,104, 18, 16, 0, 0, +/* 0x0d90 */ 255,115, 44, 81, 87,232, 3, 2, 0, 0,131,196, 32, 57,199,116, +/* 0x0da0 */ 39,235, 79,131,125, 12, 0,116, 31,131,194, 3,129,226,255, 15, +/* 0x0db0 */ 0, 0,184, 3, 0, 0, 0, 57,194,119, 13,131,236, 8, 82, 87, +/* 0x0dc0 */ 232,200, 1, 0, 0,131,196, 16,139, 69,200,139, 64, 16,137, 69, +/* 0x0dd0 */ 224,139, 69,204,139, 77,220,139, 85,216,139,117,228, 70, 3, 91, +/* 0x0de0 */ 4, 59,117,224, 15,130,201,253,255,255,131,196, 76, 94, 95, 91, +/* 0x0df0 */ 93,195,131,236, 12,106,127,232,137, 1, 0, 0,131,196, 16, 85, +/* 0x0e00 */ 137,229, 83, 87, 86,129,236, 28, 64, 0, 0,232, 0, 0, 0, 0, +/* 0x0e10 */ 88,139,117, 4,141, 77, 8,141,128,239,255,255,255, 37, 0,240, +/* 0x0e20 */ 255,255,139, 80, 16,133,210,116, 68,137,181,228,191,255,255,137, +/* 0x0e30 */ 141,232,191,255,255,131,200, 28, 49,246,185, 76, 73, 78, 75,131, +/* 0x0e40 */ 56, 1,117, 19,139,120, 14,187, 69, 68, 73, 84, 49,223,139, 88, +/* 0x0e50 */ 10, 49,203, 9,251,116, 48, 70, 3, 64, 4, 57,214,137,189,240, +/* 0x0e60 */ 191,255,255,137,189,236,191,255,255,114,212,235, 61,137,181,228, +/* 0x0e70 */ 191,255,255,137,141,232,191,255,255,137,133,240,191,255,255,137, +/* 0x0e80 */ 133,236,191,255,255,235, 35,139, 88, 24, 15,183, 3, 1,216,141, +/* 0x0e90 */ 75, 2,139,123,252,131,195,252,133,255,116,246, 41,251,137,133, +/* 0x0ea0 */ 236,191,255,255,137,141,240,191,255,255,139,181,232,191,255,255, +/* 0x0eb0 */ 137,240,131,192,248,131,236, 4,141,141,244,191,255,255, 80,255, +/* 0x0ec0 */ 181,240,191,255,255,255,181,236,191,255,255,104, 0, 64, 0, 0, +/* 0x0ed0 */ 81, 87, 83,232, 73,250,255,255,131,196, 32,137,133,240,191,255, +/* 0x0ee0 */ 255,131,236, 8, 87, 83,232,162, 0, 0, 0,131,196, 16,139,133, +/* 0x0ef0 */ 228,191,255,255,137, 70,252,139,133,240,191,255,255,141,102,248, +/* 0x0f00 */ 255,224, 49,192,129,196, 28, 64, 0, 0, 94, 95, 91, 93,195, 85, +/* 0x0f10 */ 137,229, 83, 87, 86,131,236, 12,139, 69, 8,139, 57, 57,199,114, +/* 0x0f20 */ 44,139,113, 4,133,192,116, 20,137,199, 79,138, 30,141,118, 1, +/* 0x0f30 */ 136, 26,141, 82, 1,117,243,139, 57,139,113, 4, 1,198,137,113, +/* 0x0f40 */ 4, 41,199,137, 57,131,196, 12, 94, 95, 91, 93,195,199, 4, 36, +/* 0x0f50 */ 127, 0, 0, 0,232, 44, 0, 0, 0,139, 84, 36, 4,139, 76, 36, +/* 0x0f60 */ 8,139, 2, 15,200,137, 2,131,233, 4,141, 82, 4,119,242,195, +/* 0x0f70 */ 90, 88, 89, 81, 80, 82, 87,151, 49,192,243,170, 95,195, 90, 15, +/* 0x0f80 */ 52,176, 4,235, 2,176, 1,235, 2,176, 74,235, 2,176, 73,235, +/* 0x0f90 */ 2,176,153,235, 2,176, 6,235, 2,176, 5,235, 2,176,197,235, +/* 0x0fa0 */ 2,176, 3, 15,182,192,137,225, 13, 0, 0, 12, 0,232,204,255, +/* 0x0fb0 */ 255,255,115, 3,131,200,255,195, 1, 0, 0, 0, 28, 0, 0, 0, +/* 0x0fc0 */ 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, +/* 0x0fd0 */ 2, 0, 0, 0, 33, 9, 0, 0, 52, 0, 0, 0, 52, 0, 0, 0, +/* 0x0fe0 */ 185, 15, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 3, 0, 0, 0, +/* 0x0ff0 */ 12, 0, 1, 0, 16, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, +/* 0x1000 */ 0, 1, 95, 0, 5, 0, 10, 95, 0, 72,117,112,120, 95,109, 97, +/* 0x1010 */ 105,110, 0,106,109, 0,111, 98, 0,148, 1,119,114,105,116,101, +/* 0x1020 */ 0,179, 1,101,120,105,116, 0,184, 1,112,114,101, 97,100, 0, +/* 0x1030 */ 199, 1, 99,108,111,115,101, 0,204, 1,111,112,101,110, 0,209, +/* 0x1040 */ 1,114,101, 97,100, 0,219, 1, 0, 2,109,104, 95,101,120,101, +/* 0x1050 */ 99,117,116,101, 95,104,101, 97,100,101,114, 0,102, 95, 98,122, +/* 0x1060 */ 101,114,111, 0,169, 1, 2, 0, 0, 0, 3, 0,161, 18, 0, 0, +/* 0x1070 */ 4, 97,105,110, 0,143, 1,112,114,111,116,101, 99,116, 0,189, +/* 0x1080 */ 1,117,110,109, 97,112, 0,194, 1,109, 97,112, 0,214, 1, 3, +/* 0x1090 */ 0,255, 27, 0, 0, 2,115,119, 97,112, 0,164, 1,122,101,114, +/* 0x10a0 */ 111, 0,174, 1, 3, 0,217, 30, 0, 3, 0,240, 30, 0, 3, 0, +/* 0x10b0 */ 240, 30, 0, 3, 0,129, 31, 0, 3, 0,133, 31, 0, 3, 0,137, +/* 0x10c0 */ 31, 0, 3, 0,141, 31, 0, 3, 0,145, 31, 0, 3, 0,149, 31, +/* 0x10d0 */ 0, 3, 0,153, 31, 0, 3, 0,157, 31, 0, 3, 0,161, 31, 0, +/* 0x10e0 */ 141, 0, 0, 0, 14, 1, 0, 0,130, 26, 0, 0,155, 0, 0, 0, +/* 0x10f0 */ 14, 1, 0, 0,118, 27, 0, 0,164, 0, 0, 0, 14, 1, 0, 0, +/* 0x1100 */ 15, 31, 0, 0,171, 0, 0, 0, 14, 1, 0, 0,126, 31, 0, 0, +/* 0x1110 */ 177, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0,186, 0, 0, 0, +/* 0x1120 */ 2, 0, 0, 0, 3, 0, 0, 0,195, 0, 0, 0, 2, 0, 0, 0, +/* 0x1130 */ 4, 0, 0, 0,205, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, +/* 0x1140 */ 214, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0,224, 0, 0, 0, +/* 0x1150 */ 2, 0, 0, 0, 73, 0, 0, 0,235, 0, 0, 0, 2, 0, 0, 0, +/* 0x1160 */ 74, 0, 0, 0,248, 0, 0, 0, 2, 0, 0, 0,153, 0, 0, 0, +/* 0x1170 */ 2, 1, 0, 0, 2, 0, 0, 0,197, 0, 0, 0, 2, 0, 0, 0, +/* 0x1180 */ 15, 1, 0, 0,112, 31, 0, 0, 11, 0, 0, 0, 3, 1, 16, 0, +/* 0x1190 */ 0, 16, 0, 0, 31, 0, 0, 0, 15, 1, 0, 0, 89, 31, 0, 0, +/* 0x11a0 */ 38, 0, 0, 0, 15, 1, 0, 0,112, 31, 0, 0, 45, 0, 0, 0, +/* 0x11b0 */ 15, 1, 0, 0,149, 31, 0, 0, 52, 0, 0, 0, 15, 1, 0, 0, +/* 0x11c0 */ 133, 31, 0, 0, 58, 0, 0, 0, 15, 1, 0, 0,255, 29, 0, 0, +/* 0x11d0 */ 64, 0, 0, 0, 15, 1, 0, 0,157, 31, 0, 0, 70, 0, 0, 0, +/* 0x11e0 */ 15, 1, 0, 0,137, 31, 0, 0, 80, 0, 0, 0, 15, 1, 0, 0, +/* 0x11f0 */ 141, 31, 0, 0, 88, 0, 0, 0, 15, 1, 0, 0,153, 31, 0, 0, +/* 0x1200 */ 94, 0, 0, 0, 15, 1, 0, 0,145, 31, 0, 0,101, 0, 0, 0, +/* 0x1210 */ 15, 1, 0, 0,161, 31, 0, 0,107, 0, 0, 0, 15, 1, 0, 0, +/* 0x1220 */ 33, 25, 0, 0,117, 0, 0, 0, 15, 1, 0, 0,129, 31, 0, 0, +/* 0x1230 */ 124, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 32, 0, 95, 95, +/* 0x1240 */ 95, 98,122,101,114,111, 0, 95, 95,109,104, 95,101,120,101, 99, +/* 0x1250 */ 117,116,101, 95,104,101, 97,100,101,114, 0, 95, 98,115,119, 97, +/* 0x1260 */ 112, 0, 95, 98,122,101,114,111, 0, 95, 99,108,111,115,101, 0, +/* 0x1270 */ 95,101,120,105,116, 0, 95,109, 97,105,110, 0, 95,109,109, 97, +/* 0x1280 */ 112, 0, 95,109,112,114,111,116,101, 99,116, 0, 95,109,117,110, +/* 0x1290 */ 109, 97,112, 0, 95,111,112,101,110, 0, 95,112,114,101, 97,100, +/* 0x12a0 */ 0, 95,114,101, 97,100, 0, 95,117,112,120, 95,109, 97,105,110, +/* 0x12b0 */ 0, 95,119,114,105,116,101, 0,100,121,108,100, 95,115,116,117, +/* 0x12c0 */ 98, 95, 98,105,110,100,101,114, 0, 95,117,110,112, 97, 99,107, +/* 0x12d0 */ 69,120,116,101,110,116, 0, 95,100,111, 95,120,109, 97,112, 0, +/* 0x12e0 */ 95,120,114,101, 97,100, 0,115,121,115,103,111, 0, 83, 89, 83, +/* 0x12f0 */ 95,101,120,105,116, 0, 83, 89, 83, 95,114,101, 97,100, 0, 83, +/* 0x1300 */ 89, 83, 95,119,114,105,116,101, 0, 83, 89, 83, 95,111,112,101, +/* 0x1310 */ 110, 0, 83, 89, 83, 95, 99,108,111,115,101, 0, 83, 89, 83, 95, +/* 0x1320 */ 109,117,110,109, 97,112, 0, 83, 89, 83, 95,109,112,114,111,116, +/* 0x1330 */ 101, 99,116, 0, 83, 89, 83, 95,112,114,101, 97,100, 0, 83, 89, +/* 0x1340 */ 83, 95,109,109, 97,112, 0, 0 +}; diff --git a/src/stub/src/i386-darwin.macho-upxmain.c b/src/stub/src/i386-darwin.macho-upxmain.c new file mode 100644 index 00000000..98ed7674 --- /dev/null +++ b/src/stub/src/i386-darwin.macho-upxmain.c @@ -0,0 +1,751 @@ +/* i386-darwin.macho-upxmain.c -- loader hack for Mach-o i386 + + This file is part of the UPX executable compressor. + + Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer + Copyright (C) 1996-2016 Laszlo Molnar + Copyright (C) 2000-2016 John F. Reiser + All Rights Reserved. + + UPX and the UCL library are free software; you can redistribute them + and/or modify them under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. + If not, write to the Free Software Foundation, Inc., + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Markus F.X.J. Oberhumer Laszlo Molnar + + + John F. Reiser + + */ + +#include +#include +#include "include/darwin.h" + +#ifndef DEBUG /*{*/ +#define DEBUG 0 +#endif /*}*/ + +/************************************************************************* +// configuration section +**************************************************************************/ + +// In order to make it much easier to move this code at runtime and execute +// 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) my_printf a + +static int +my_printf(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 +**************************************************************************/ + +typedef struct { + size_t size; // must be first to match size[0] uncompressed size + 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) { + *q = *p; + } + x->buf += count; + x->size -= count; +} + + +/************************************************************************* +// util +**************************************************************************/ + +#if 1 //{ save space +#define ERR_LAB error: exit(127); +#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); +} +#endif //} + + +/************************************************************************* +// UPX & NRV stuff +**************************************************************************/ + +struct l_info { // 12-byte trailer for loader (after macho headers) + unsigned l_checksum; + unsigned l_magic; // UPX_MAGIC_LE32 + unsigned short l_lsize; + unsigned char l_version; + unsigned char l_format; +}; +struct p_info { // 12-byte packed program header + unsigned p_progid; + unsigned p_filesize; + unsigned p_blocksize; +}; + +struct b_info { // 12-byte header before each compressed block + unsigned sz_unc; // uncompressed_size + unsigned sz_cpr; // compressed_size + unsigned char b_method; // compression algorithm + unsigned char b_ftid; // filter id + unsigned char b_cto8; // filter parameter + unsigned char b_unused; +}; + +typedef void f_unfilter( + nrv_byte *, // also addvalue + nrv_uint, + unsigned cto8, // junk in high 24 bits + unsigned ftid +); +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 + Extent *const xo, // output + f_expand *const f_decompress, + 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 + // compressible and is stored in its uncompressed form. + + // Read and check block sizes. + xread(xi, (unsigned char *)&h, sizeof(h)); + if (h.sz_unc == 0) { // uncompressed size 0 -> EOF + if (h.sz_cpr != UPX_MAGIC_LE32) // h.sz_cpr must be h->magic + err_exit(2); + if (xi->size != 0) // all bytes must be written + err_exit(3); + break; + } + if (h.sz_cpr <= 0) { + err_exit(4); +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: + // assert(h.sz_cpr <= h.sz_unc); + // assert(h.sz_unc > 0 && h.sz_unc <= blocksize); + // assert(h.sz_cpr > 0 && h.sz_cpr <= blocksize); + + if (h.sz_cpr < h.sz_unc) { // Decompress block + nrv_uint out_len = h.sz_unc; // EOF for lzma + int const j = (*f_decompress)(xi->buf, h.sz_cpr, + xo->buf, &out_len, h.b_method); + if (j != 0 || out_len != (nrv_uint)h.sz_unc) + err_exit(7); + if (h.b_ftid!=0 && f_unf) { // have filter + (*f_unf)(xo->buf, out_len, h.b_cto8, h.b_ftid); + } + xi->buf += h.sz_cpr; + xi->size -= h.sz_cpr; + } + else { // copy literal block + xread(xi, xo->buf, h.sz_cpr); + } + xo->buf += h.sz_unc; + xo->size -= h.sz_unc; + } +} + +static void +upx_bzero(unsigned char *p, size_t len) +{ + if (len) do { + *p++= 0; + } while (--len); +} +#define bzero upx_bzero + + +// The PF_* and PROT_* bits are {1,2,4}; the conversion table fits in 32 bits. +#define REP8(x) \ + ((x)|((x)<<4)|((x)<<8)|((x)<<12)|((x)<<16)|((x)<<20)|((x)<<24)|((x)<<28)) +#define EXP8(y) \ + ((1&(y)) ? 0xf0f0f0f0 : (2&(y)) ? 0xff00ff00 : (4&(y)) ? 0xffff0000 : 0) +#define PF_TO_PROT(pf) \ + ((PROT_READ|PROT_WRITE|PROT_EXEC) & ( \ + ( (REP8(PROT_EXEC ) & EXP8(PF_X)) \ + |(REP8(PROT_READ ) & EXP8(PF_R)) \ + |(REP8(PROT_WRITE) & EXP8(PF_W)) \ + ) >> ((pf & (PF_R|PF_W|PF_X))<<2) )) + +typedef struct { + unsigned magic; + unsigned nfat_arch; +} Fat_header; +typedef struct { + unsigned cputype; + unsigned cpusubtype; + unsigned offset; + unsigned size; + unsigned align; /* shift count (log base 2) */ +} Fat_arch; + enum e8 { + FAT_MAGIC = 0xcafebabe, + FAT_CIGAM = 0xbebafeca + }; + enum e9 { + CPU_TYPE_I386 = 7, + CPU_TYPE_AMD64 = 0x01000007, + CPU_TYPE_ARM = 12, + CPU_TYPE_POWERPC = 0x00000012, + CPU_TYPE_POWERPC64 = 0x01000012 + }; + +typedef struct { + unsigned magic; + unsigned cputype; + unsigned cpysubtype; + unsigned filetype; + unsigned ncmds; + unsigned sizeofcmds; + unsigned flags; +} Mach_header; + enum e0 { + MH_MAGIC = 0xfeedface, + MH_MAGIC64 = 1+0xfeedface + }; + enum e2 { + MH_EXECUTE = 2 + }; + enum e3 { + MH_NOUNDEFS = 1 + }; + +typedef struct { + unsigned cmd; + unsigned cmdsize; +} Mach_load_command; + enum e4 { + LC_REQ_DYLD = 0x80000000, // OR'ed ==> must not ignore + LC_SEGMENT = 0x1, + LC_SEGMENT_64 = 0x19, + LC_THREAD = 0x4, + LC_UNIXTHREAD = 0x5, + LC_LOAD_DYLINKER = 0xe, + LC_MAIN = (0x28|LC_REQ_DYLD) + }; + +typedef struct { + unsigned cmd; + unsigned cmdsize; + char segname[16]; + uint32_t vmaddr; + uint32_t vmsize; + uint32_t fileoff; + uint32_t filesize; + unsigned maxprot; + unsigned initprot; + unsigned nsects; + unsigned flags; +} Mach_segment_command; + enum e5 { + VM_PROT_READ = 1, + VM_PROT_WRITE = 2, + VM_PROT_EXECUTE = 4 + }; + +typedef struct { + char sectname[16]; + char segname[16]; + uint32_t addr; /* memory address */ + uint32_t size; /* size in bytes */ + unsigned offset; /* file offset */ + unsigned align; /* power of 2 */ + unsigned reloff; /* file offset of relocation entries */ + unsigned nreloc; /* number of relocation entries */ + unsigned flags; /* section type and attributes */ + unsigned reserved1; /* for offset or index */ + unsigned reserved2; /* for count or sizeof */ +} Mach_section_command; + +typedef struct { + uint32_t cmd; // LC_MAIN; MH_EXECUTE only + uint32_t cmdsize; // 24 + uint64_t entryoff; // file offset of main() [expected in __TEXT] + uint64_t stacksize; // non-default initial stack size +} Mach_main_command; + +typedef struct { + uint32_t eax, ebx, ecx, edx; + uint32_t edi, esi, ebp; + uint32_t esp, ss; + uint32_t eflags; + uint32_t eip, cs; + uint32_t ds,es,fs,gs; +} Mach_i386_thread_state; + +typedef struct { + unsigned cmd; /* LC_THREAD or LC_UNIXTHREAD */ + unsigned cmdsize; /* total size of this command */ + unsigned flavor; + unsigned count; /* sizeof(following_thread_state)/4 */ + Mach_i386_thread_state state; +} Mach_thread_command; + enum e6 { + x86_THREAD_STATE32 = 1 + }; + enum e7 { + i386_THREAD_STATE_COUNT = sizeof(Mach_i386_thread_state)/4 + }; + +typedef union { + unsigned offset; /* from start of load command to string */ +} Mach_lc_str; + +#define MAP_FIXED 0x10 +#define MAP_PRIVATE 0x02 +#define MAP_ANON 0x1000 +//#define MAP_ANON 0x20 // x86 DEBUG ONLY +#define PROT_READ 1 +#define PROT_WRITE 2 +#define PROT_EXEC 4 +#define MAP_ANON_FD -1 +#define MAP_FAILED ((void *) -1) + +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 uint32_t // entry address +do_xmap( + Mach_header const *const mhdr, + off_t const fat_offset, + Extent *const xi, + int const fdi, + Mach_header **mhdrpp, + f_expand *const f_decompress, + f_unfilter *const f_unf +) +{ + Mach_segment_command const *sc = (Mach_segment_command const *)(1+ mhdr); + Mach_segment_command const *segTEXT = 0; + uint32_t entry = 0; + unsigned long base = 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==sc->cmd && sc->vmsize!=0) { + Extent xo; + size_t mlen = xo.size = sc->filesize; + unsigned char *addr = xo.buf = base + (unsigned char *)sc->vmaddr; + unsigned char *haddr = sc->vmsize + addr; + size_t frag = (int)(uint32_t)addr &~ PAGE_MASK; + addr -= frag; + mlen += frag; + + if (0!=mlen) { // In particular, omitted for __PAGEZERO + // Decompressor can overrun the destination by 3 bytes. [x86 only] + size_t const mlen3 = mlen + (xi ? 3 : 0); + unsigned const prot = VM_PROT_READ | VM_PROT_WRITE; + unsigned const flags = (addr ? MAP_FIXED : 0) | 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, mlen3, prot, flags, fdm, offset)); + unsigned char *mapa = mmap(addr, mlen3, prot, flags, fdm, offset); + if (MAP_FAILED == mapa) { + err_exit(8); + } + if (0 == addr) { // dyld auto-relocate + base = (unsigned long)mapa; // relocation constant + } + addr = mapa; + } + if (xi && 0!=sc->filesize) { + if (0==sc->fileoff /*&& 0!=mhdrpp*/) { + segTEXT = sc; + *mhdrpp = (Mach_header *)(void *)addr; + } + unpackExtent(xi, &xo, f_decompress, f_unf); + } + /*bzero(addr, frag);*/ // fragment at lo end + frag = (-mlen) &~ PAGE_MASK; // distance to next page boundary + bzero(mlen+addr, frag); // fragment at hi end + if (0!=mlen && 0!=mprotect(addr, mlen, sc->initprot)) { + err_exit(10); +ERR_LAB + } + addr += mlen + frag; /* page boundary on hi end */ + if ( +#if defined(SIMULATE_ON_DEBIAN_EABI4) /*{*/ + 0!=addr && +#endif /*}*/ + addr < haddr) { // need pages for .bss + if (0!=addr && addr != mmap(addr, haddr - addr, sc->initprot, + MAP_FIXED | MAP_PRIVATE | MAP_ANON, MAP_ANON_FD, 0 ) ) { + err_exit(9); + } + } + else if (xi) { // cleanup if decompressor overrun crosses page boundary + mlen = ~PAGE_MASK & (3+ mlen); + if (mlen<=3) { // page fragment was overrun buffer only + munmap((char *)addr, mlen); + } + } + } + else if (LC_UNIXTHREAD==sc->cmd || LC_THREAD==sc->cmd) { + Mach_thread_command const *const thrc = (Mach_thread_command const *)sc; + if (x86_THREAD_STATE32 ==thrc->flavor + && i386_THREAD_STATE_COUNT ==thrc->count ) { + entry = thrc->state.eip + base; // JMP + } + } + else if (LC_MAIN==sc->cmd) { + entry = ((Mach_main_command const *)sc)->entryoff; + if (segTEXT->fileoff <= entry && entry < segTEXT->filesize) { + entry += segTEXT->vmaddr; // CALL + } + // XXX FIXME TODO: if entry not in segTEXT? + // XXX FIXME TODO: LC_MAIN is a CALL; LC_*THREAD is a JMP + } + return entry; +} + +static off_t +fat_find(Fat_header *fh) // *fh suffers bswap() +{ + Fat_arch *fa = (Fat_arch *)(1+ fh); + bswap(fh, sizeof(*fh) + (fh->nfat_arch>>24)*sizeof(*fa)); + unsigned j; + for (j= 0; j < fh->nfat_arch; ++j, ++fa) { + if (CPU_TYPE_I386==fa->cputype) { + return fa->offset; // should not be 0 because of header + } + } + return 0; +} + +/************************************************************************* +// upx_main - called by our entry code +// +**************************************************************************/ + +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") + +uint32_t // entry address +upx_main( + struct l_info const *const li, + size_t volatile sz_compressed, // total length + Mach_header *const mhdr, // temp char[sz_mhdr] for decompressing + size_t const sz_mhdr, + f_expand *const f_decompress, + f_unfilter *const f_unf, + Mach_header **const mhdrpp // Out: *mhdrpp= &real Mach_header +) +{ + uint32_t entry; + off_t fat_offset = 0; + Extent xi, xo, xi0; + xi.buf = CONST_CAST(unsigned char *, 1+ (struct p_info const *)(1+ li)); // &b_info + xi.size = sz_compressed - (sizeof(struct l_info) + sizeof(struct p_info)); + xo.buf = (unsigned char *)mhdr; + 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? + + entry = do_xmap(mhdr, fat_offset, &xi0, MAP_ANON_FD, mhdrpp, f_decompress, f_unf); + + { // Map dyld dynamic loader + Mach_load_command const *lc = (Mach_load_command const *)(1+ mhdr); + unsigned j; + + for (j=0; j < mhdr->ncmds; ++j, + (lc = (Mach_load_command const *)(lc->cmdsize + (void const *)lc)) + ) if (LC_LOAD_DYLINKER==lc->cmd) { + char const *const dyld_name = ((Mach_lc_str const *)(1+ lc))->offset + + (char const *)lc; + int const fdi = open(dyld_name, O_RDONLY, 0); + if (0 > fdi) { + err_exit(18); + } + for (;;) { // possibly 2 times for 'fat' binary + if (sz_mhdr!=pread(fdi, (void *)mhdr, sz_mhdr, fat_offset)) { +ERR_LAB + err_exit(19); + } + switch (mhdr->magic) { + case MH_MAGIC: break; // i686 on x86_64 ? + case MH_MAGIC64: break; + + case FAT_CIGAM: + case FAT_MAGIC: { + // stupid Apple: waste code and a page fault on EVERY execve + fat_offset = fat_find((Fat_header *)mhdr); + if (fat_offset) { + continue; // the 'for' loop + } + err_exit(20); // no other choice + } break; + } // switch + break; + } + entry = do_xmap(mhdr, fat_offset, 0, fdi, 0, 0, 0); + close(fdi); + break; + } + } + + return entry; +} + +typedef struct { + uint32_t cmd; + uint32_t cmdsize; + uint32_t data[2]; // because cmdsize >= 16 +} Mach_command; // generic prefix + +// +// Build on Mac OS X: (where gcc is really clang) +// gcc -o i386-darwin.macho-upxmain.exe \ +// -Os -fPIC -fno-stack-protector \ +// i386-darwin.macho-upxmain.c \ +// i386-darwin.macho-upxsubr.S \ +// -Wl,-pagezero_size,0x1000 \ +// -Wl,-no_pie \ +// -Wl,-no_uuid \ +// -Wl,-no_function_starts \ +// -Wl,-bind_at_load \ +// -Wl,-headerpad,0x400 \ +// +//# -Wl,-unexported_symbols_list unexport-upxload.txt \ +//# strip -u -r i386-darwin.macho-upxmain.exe + +int +main(int argc, char *argv[]) +{ + // Entry via JMP (with no parameters) instead of CALL + asm("movl 1*4(%%ebp),%0; lea 2*4(%%ebp),%1" : "=r" (argc), "=r" (argv) : ); + + Mach_header const *mhdr0 = (Mach_header const *)((~0ul<<12) & (unsigned long)&main); + Mach_command const *ptr = (Mach_command const *)(1+ mhdr0); + f_unfilter *f_unf; + f_expand *f_exp; + char *payload; + size_t paysize; + + unsigned j; + for (j=0; j < mhdr0->ncmds; ++j, + ptr = (Mach_command const *)(ptr->cmdsize + (char const *)ptr)) + if (LC_SEGMENT==ptr->cmd) { + Mach_segment_command const *const seg = (Mach_segment_command const *)ptr; + // Compare 8 characters + if (*(uint64_t const *)(&"__LINKEDIT"[2]) == *(uint64_t const *)(&seg->segname[2])) { + f_unf = (f_unfilter *)(sizeof(unsigned short) + seg->vmaddr); + f_exp = (f_expand *)(*(unsigned short const *)seg->vmaddr + seg->vmaddr); + unsigned const *q = (unsigned const *)seg->vmaddr; + while (!(paysize = *--q)) /*empty*/ ; + payload = (char *)(-paysize + (char const *)q); + break; + } + } + char mhdr[16384]; + uint32_t entry = upx_main((struct l_info const *)payload, paysize, + (Mach_header *)mhdr, sizeof(mhdr), + f_exp, f_unf, (Mach_header **)&argv[-2]); + + munmap(payload, paysize); // leaving __LINKEDIT + argv[-1] = (char *)(long)argc; + asm("lea -2*4(%1),%%esp; jmp *%0" : : "r" (entry), "r" (argv)); + return 0; +} + +/* vim:set ts=4 sw=4 et: */