1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

branch merge

This commit is contained in:
John Reiser 2009-02-12 12:42:57 -08:00
commit 7974ef80d7
16 changed files with 139 additions and 186 deletions

View File

@ -45,7 +45,7 @@ stub/**.*-stamp
stub/tmp*/* stub/tmp*/*
stub/**/tmp*/* stub/**/tmp*/*
stub/tools/armpe/armpe_tester stub/tools/armpe/armpe_tester{,.exe,.out}
stub/tools/sstrip/amd64-linux-sstrip stub/tools/sstrip/amd64-linux-sstrip
stub/tools/sstrip/i386-linux-sstrip stub/tools/sstrip/i386-linux-sstrip

View File

@ -307,22 +307,15 @@
#endif #endif
#undef __attribute_packed
#if (ACC_CC_INTELC && (__INTEL_COMPILER < 800)) #if (ACC_CC_INTELC && (__INTEL_COMPILER < 800))
#elif (0 && (ACC_ARCH_AMD64 || ACC_ARCH_I386)) #elif (0 && (ACC_ARCH_AMD64 || ACC_ARCH_I386))
#elif (ACC_CC_GNUC || ACC_CC_INTELC || ACC_CC_PATHSCALE) #elif (ACC_CC_GNUC || ACC_CC_INTELC || ACC_CC_PATHSCALE)
# define __attribute_packed __attribute__((__packed__,__aligned__(1)))
# define __packed_struct(s) struct s { # define __packed_struct(s) struct s {
# define __packed_struct_end() } __attribute_packed; # define __packed_struct_end() } __attribute__((__packed__,__aligned__(1)));
#elif (ACC_CC_WATCOMC) #elif (ACC_CC_WATCOMC)
# define __attribute_packed
# define __packed_struct(s) _Packed struct s { # define __packed_struct(s) _Packed struct s {
# define __packed_struct_end() }; # define __packed_struct_end() };
#endif #endif
#if !defined(__attribute_packed)
# define __attribute_packed
#endif
#if !defined(__packed_struct) #if !defined(__packed_struct)
# define __packed_struct(s) struct s { # define __packed_struct(s) struct s {
# define __packed_struct_end() }; # define __packed_struct_end() };

View File

@ -49,8 +49,8 @@ public:
protected: protected:
enum { FIXUP_EXTRA = 3 }; enum { FIXUP_EXTRA = 3 };
struct le_header_t __packed_struct(le_header_t)
{ // 0x00 // 0x00
char _[2]; // signature: 'LE' || 'LX' char _[2]; // signature: 'LE' || 'LX'
char byte_order; // 0 little endian char byte_order; // 0 little endian
char word_order; // 0 little endian char word_order; // 0 little endian
@ -117,28 +117,23 @@ protected:
LE16 device_id; LE16 device_id;
LE16 ddk_version; LE16 ddk_version;
#endif #endif
} __packed_struct_end()
__attribute_packed;
struct le_object_table_entry_t __packed_struct(le_object_table_entry_t)
{
LE32 virtual_size; LE32 virtual_size;
LE32 base_address; LE32 base_address;
LE32 flags; LE32 flags;
LE32 pagemap_index; LE32 pagemap_index;
LE32 npages; LE32 npages;
LE32 reserved; LE32 reserved;
} __packed_struct_end()
__attribute_packed;
struct le_pagemap_entry_t __packed_struct(le_pagemap_entry_t)
{
unsigned char h; unsigned char h;
unsigned char m; unsigned char m;
unsigned char l; unsigned char l;
unsigned char type; // 0x00-legal;0x40-iterated;0x80-invalid;0xC0-zeroed unsigned char type; // 0x00-legal;0x40-iterated;0x80-invalid;0xC0-zeroed
} __packed_struct_end()
__attribute_packed;
virtual void readObjectTable(); virtual void readObjectTable();
virtual void writeObjectTable(); virtual void writeObjectTable();

View File

@ -148,14 +148,12 @@ Linker* PackArmPe::newLinker() const
// import handling // import handling
**************************************************************************/ **************************************************************************/
struct import_desc __packed_struct(import_desc)
{
LE32 oft; // orig first thunk LE32 oft; // orig first thunk
char _[8]; char _[8];
LE32 dllname; LE32 dllname;
LE32 iat; // import address table LE32 iat; // import address table
} __packed_struct_end()
__attribute_packed;
void PackArmPe::processImports(unsigned myimport, unsigned iat_off) // pass 2 void PackArmPe::processImports(unsigned myimport, unsigned iat_off) // pass 2
{ {

View File

@ -66,19 +66,16 @@ protected:
long coff_offset; long coff_offset;
struct external_scnhdr_t __packed_struct(external_scnhdr_t)
{
char _[12]; // name, paddr char _[12]; // name, paddr
LE32 vaddr; LE32 vaddr;
LE32 size; LE32 size;
LE32 scnptr; LE32 scnptr;
char misc[12]; // relptr, lnnoptr, nreloc, nlnno char misc[12]; // relptr, lnnoptr, nreloc, nlnno
char __[4]; // flags char __[4]; // flags
} __packed_struct_end()
__attribute_packed;
struct coff_header_t __packed_struct(coff_header_t)
{
// ext_file_hdr // ext_file_hdr
LE16 f_magic; LE16 f_magic;
LE16 f_nscns; LE16 f_nscns;
@ -99,8 +96,7 @@ protected:
// section headers // section headers
external_scnhdr_t sh[3]; external_scnhdr_t sh[3];
} __packed_struct_end()
__attribute_packed;
coff_header_t coff_hdr; coff_header_t coff_hdr;
external_scnhdr_t *text,*data,*bss; external_scnhdr_t *text,*data,*bss;

View File

@ -145,26 +145,23 @@ protected:
Elf32_Shdr const *sec_dynsym; Elf32_Shdr const *sec_dynsym;
Elf32_Shdr const *sec_dynstr; Elf32_Shdr const *sec_dynstr;
struct cprElfHdr1 { __packed_struct(cprElfHdr1)
Elf32_Ehdr ehdr; Elf32_Ehdr ehdr;
Elf32_Phdr phdr[1]; Elf32_Phdr phdr[1];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
struct cprElfHdr2 { __packed_struct(cprElfHdr2)
Elf32_Ehdr ehdr; Elf32_Ehdr ehdr;
Elf32_Phdr phdr[2]; Elf32_Phdr phdr[2];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
struct cprElfHdr3 { __packed_struct(cprElfHdr3)
Elf32_Ehdr ehdr; Elf32_Ehdr ehdr;
Elf32_Phdr phdr[3]; Elf32_Phdr phdr[3];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
cprElfHdr3 elfout; cprElfHdr3 elfout;
@ -227,26 +224,23 @@ protected:
Elf64_Phdr *phdri; // for input file Elf64_Phdr *phdri; // for input file
acc_uint64l_t page_mask; // AND clears the offset-within-page acc_uint64l_t page_mask; // AND clears the offset-within-page
struct cprElfHdr1 { __packed_struct(cprElfHdr1)
Elf64_Ehdr ehdr; Elf64_Ehdr ehdr;
Elf64_Phdr phdr[1]; Elf64_Phdr phdr[1];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
struct cprElfHdr2 { __packed_struct(cprElfHdr2)
Elf64_Ehdr ehdr; Elf64_Ehdr ehdr;
Elf64_Phdr phdr[2]; Elf64_Phdr phdr[2];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
struct cprElfHdr3 { __packed_struct(cprElfHdr3)
Elf64_Ehdr ehdr; Elf64_Ehdr ehdr;
Elf64_Phdr phdr[3]; Elf64_Phdr phdr[3];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
cprElfHdr3 elfout; cprElfHdr3 elfout;

View File

@ -87,26 +87,23 @@ protected:
unsigned n_mru; unsigned n_mru;
struct cprElfHdr1 { __packed_struct(cprElfHdr1)
Elf_LE32_Ehdr ehdr; Elf_LE32_Ehdr ehdr;
Elf_LE32_Phdr phdr[1]; Elf_LE32_Phdr phdr[1];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
struct cprElfHdr2 { __packed_struct(cprElfHdr2)
Elf_LE32_Ehdr ehdr; Elf_LE32_Ehdr ehdr;
Elf_LE32_Phdr phdr[2]; Elf_LE32_Phdr phdr[2];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
struct cprElfHdr3 { __packed_struct(cprElfHdr3)
Elf_LE32_Ehdr ehdr; Elf_LE32_Ehdr ehdr;
Elf_LE32_Phdr phdr[3]; Elf_LE32_Phdr phdr[3];
l_info linfo; l_info linfo;
} __packed_struct_end()
__attribute_packed;
cprElfHdr3 elfout; cprElfHdr3 elfout;

View File

@ -67,8 +67,7 @@ protected:
virtual int readFileHeader(); virtual int readFileHeader();
virtual bool checkFileHeader(); virtual bool checkFileHeader();
struct ps1_exe_t __packed_struct(ps1_exe_t)
{
// ident string // ident string
char id[8]; char id[8];
// is NULL // is NULL
@ -91,33 +90,26 @@ protected:
char origin[60]; char origin[60];
// backup of the original header (epc - is_len) // backup of the original header (epc - is_len)
// id & the upx header ... // id & the upx header ...
} __packed_struct_end()
__attribute_packed;
// for unpack // for unpack
struct ps1_exe_hb_t __packed_struct(ps1_exe_hb_t)
{
LE32 ih_bkup[10]; LE32 ih_bkup[10];
// plus checksum for the backup // plus checksum for the backup
LE32 ih_csum; LE32 ih_csum;
} __packed_struct_end()
__attribute_packed;
struct ps1_exe_chb_t __packed_struct(ps1_exe_chb_t)
{
unsigned char id; unsigned char id;
unsigned char len; unsigned char len;
LE16 ih_csum; LE16 ih_csum;
unsigned char ih_bkup; unsigned char ih_bkup;
} __packed_struct_end()
__attribute_packed;
struct bss_nfo __packed_struct(bss_nfo)
{
LE16 hi1, op1, lo1, op2; LE16 hi1, op1, lo1, op2;
LE16 hi2, op3, lo2, op4; LE16 hi2, op3, lo2, op4;
} __packed_struct_end()
__attribute_packed;
ps1_exe_t ih, oh; ps1_exe_t ih, oh;
ps1_exe_hb_t bh; ps1_exe_hb_t bh;

View File

@ -62,8 +62,7 @@ protected:
virtual int readFileHeader(); virtual int readFileHeader();
virtual bool checkFileHeader(); virtual bool checkFileHeader();
struct tos_header_t __packed_struct(tos_header_t)
{
BE16 fh_magic; BE16 fh_magic;
BE32 fh_text; BE32 fh_text;
BE32 fh_data; BE32 fh_data;
@ -72,8 +71,7 @@ protected:
BE32 fh_reserved; BE32 fh_reserved;
BE32 fh_flag; BE32 fh_flag;
BE16 fh_reloc; BE16 fh_reloc;
} __packed_struct_end()
__attribute_packed;
tos_header_t ih, oh; tos_header_t ih, oh;

View File

@ -92,30 +92,28 @@ protected:
unsigned b_len; // total length of b_info blocks unsigned b_len; // total length of b_info blocks
// must agree with stub/linux.hh // must agree with stub/linux.hh
struct b_info { // 12-byte header before each compressed block __packed_struct(b_info) // 12-byte header before each compressed block
unsigned sz_unc; // uncompressed_size unsigned sz_unc; // uncompressed_size
unsigned sz_cpr; // compressed_size unsigned sz_cpr; // compressed_size
unsigned char b_method; // compression algorithm unsigned char b_method; // compression algorithm
unsigned char b_ftid; // filter id unsigned char b_ftid; // filter id
unsigned char b_cto8; // filter parameter unsigned char b_cto8; // filter parameter
unsigned char b_unused; unsigned char b_unused;
} __packed_struct_end()
__attribute_packed;
struct l_info { // 12-byte trailer in header for loader __packed_struct(l_info) // 12-byte trailer in header for loader
LE32 l_checksum; LE32 l_checksum;
LE32 l_magic; LE32 l_magic;
LE16 l_lsize; LE16 l_lsize;
unsigned char l_version; unsigned char l_version;
unsigned char l_format; unsigned char l_format;
} __packed_struct_end()
__attribute_packed;
struct p_info { // 12-byte packed program header __packed_struct(p_info) // 12-byte packed program header
unsigned p_progid; unsigned p_progid;
unsigned p_filesize; unsigned p_filesize;
unsigned p_blocksize; unsigned p_blocksize;
} __packed_struct_end()
__attribute_packed;
struct l_info linfo; struct l_info linfo;
@ -135,31 +133,28 @@ protected:
PackUnixBe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; } PackUnixBe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; }
// must agree with stub/linux.hh // must agree with stub/linux.hh
struct b_info { // 12-byte header before each compressed block __packed_struct(b_info) // 12-byte header before each compressed block
BE32 sz_unc; // uncompressed_size BE32 sz_unc; // uncompressed_size
BE32 sz_cpr; // compressed_size BE32 sz_cpr; // compressed_size
unsigned char b_method; // compression algorithm unsigned char b_method; // compression algorithm
unsigned char b_ftid; // filter id unsigned char b_ftid; // filter id
unsigned char b_cto8; // filter parameter unsigned char b_cto8; // filter parameter
unsigned char b_unused; unsigned char b_unused;
} __packed_struct_end()
__attribute_packed;
struct l_info { // 12-byte trailer in header for loader __packed_struct(l_info) // 12-byte trailer in header for loader
BE32 l_checksum; BE32 l_checksum;
BE32 l_magic; BE32 l_magic;
BE16 l_lsize; BE16 l_lsize;
unsigned char l_version; unsigned char l_version;
unsigned char l_format; unsigned char l_format;
} __packed_struct_end()
__attribute_packed;
struct p_info { // 12-byte packed program header __packed_struct(p_info) // 12-byte packed program header
BE32 p_progid; BE32 p_progid;
BE32 p_filesize; BE32 p_filesize;
BE32 p_blocksize; BE32 p_blocksize;
} __packed_struct_end()
__attribute_packed;
}; };
@ -170,31 +165,28 @@ protected:
PackUnixLe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; } PackUnixLe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; }
// must agree with stub/linux.hh // must agree with stub/linux.hh
struct b_info { // 12-byte header before each compressed block __packed_struct(b_info) // 12-byte header before each compressed block
LE32 sz_unc; // uncompressed_size LE32 sz_unc; // uncompressed_size
LE32 sz_cpr; // compressed_size LE32 sz_cpr; // compressed_size
unsigned char b_method; // compression algorithm unsigned char b_method; // compression algorithm
unsigned char b_ftid; // filter id unsigned char b_ftid; // filter id
unsigned char b_cto8; // filter parameter unsigned char b_cto8; // filter parameter
unsigned char b_unused; unsigned char b_unused;
} __packed_struct_end()
__attribute_packed;
struct l_info { // 12-byte trailer in header for loader __packed_struct(l_info) // 12-byte trailer in header for loader
LE32 l_checksum; LE32 l_checksum;
LE32 l_magic; LE32 l_magic;
LE16 l_lsize; LE16 l_lsize;
unsigned char l_version; unsigned char l_version;
unsigned char l_format; unsigned char l_format;
} __packed_struct_end()
__attribute_packed;
struct p_info { // 12-byte packed program header __packed_struct(p_info) // 12-byte packed program header
LE32 p_progid; LE32 p_progid;
LE32 p_filesize; LE32 p_filesize;
LE32 p_blocksize; LE32 p_blocksize;
} __packed_struct_end()
__attribute_packed;
}; };

View File

@ -301,7 +301,7 @@ void PackVmlinuxBase<T>::pack(OutputFile *fo)
if (UPX_E_OK!=r || len_unc<=len_cpr) // FIXME: allow no compression if (UPX_E_OK!=r || len_unc<=len_cpr) // FIXME: allow no compression
throwInternalError("Ehdr compression failed"); throwInternalError("Ehdr compression failed");
struct b_info { // 12-byte header before each compressed block __packed_struct(b_info) // 12-byte header before each compressed block
unsigned sz_unc; // uncompressed_size unsigned sz_unc; // uncompressed_size
unsigned sz_cpr; // compressed_size unsigned sz_cpr; // compressed_size
unsigned char b_method; // compression algorithm unsigned char b_method; // compression algorithm
@ -309,8 +309,7 @@ void PackVmlinuxBase<T>::pack(OutputFile *fo)
unsigned char b_cto8; // filter parameter unsigned char b_cto8; // filter parameter
unsigned char b_unused; // FIXME: !=0 for partial-block unfilter unsigned char b_unused; // FIXME: !=0 for partial-block unfilter
// unsigned f_offset, f_len; // only if partial-block unfilter // unsigned f_offset, f_len; // only if partial-block unfilter
} __packed_struct_end()
__attribute_packed;
struct b_info hdr_info; struct b_info hdr_info;
set_be32(&hdr_info.sz_unc, len_unc); set_be32(&hdr_info.sz_unc, len_unc);

View File

@ -64,8 +64,7 @@ protected:
// virtual upx_byte *getLoader() const; // virtual upx_byte *getLoader() const;
// virtual int getLoaderSize() const; // virtual int getLoaderSize() const;
struct boot_sect_t __packed_struct(boot_sect_t)
{
char _[0x1f1]; char _[0x1f1];
unsigned char setup_sects; unsigned char setup_sects;
char __[2]; char __[2];
@ -86,11 +85,11 @@ protected:
char _8[0x248 - (0x234 + 1)]; char _8[0x248 - (0x234 + 1)];
LE32 payload_offset; LE32 payload_offset;
LE32 payload_length; LE32 payload_length;
// some more uninteresting fields here ... // some more uninteresting fields here ...
// see /usr/src/linux/Documentation/i386/boot.txt // see /usr/src/linux/Documentation/i386/boot.txt
} __packed_struct_end()
__attribute_packed h;
boot_sect_t h;
MemBuffer setup_buf; MemBuffer setup_buf;
int setup_size; int setup_size;

View File

@ -173,14 +173,12 @@ int PackW32Pe::readFileHeader()
// import handling // import handling
**************************************************************************/ **************************************************************************/
struct import_desc __packed_struct(import_desc)
{
LE32 oft; // orig first thunk LE32 oft; // orig first thunk
char _[8]; char _[8];
LE32 dllname; LE32 dllname;
LE32 iat; // import address table LE32 iat; // import address table
} __packed_struct_end()
__attribute_packed;
void PackW32Pe::processImports(unsigned myimport, unsigned) // pass 2 void PackW32Pe::processImports(unsigned myimport, unsigned) // pass 2
{ {

View File

@ -106,8 +106,7 @@ protected:
unsigned crelocs; // rva of preprocessed fixups unsigned crelocs; // rva of preprocessed fixups
int big_relocs; int big_relocs;
struct pe_header_t __packed_struct(pe_header_t)
{
// 0x0 // 0x0
char _[4]; // pemagic char _[4]; // pemagic
LE16 cpu; LE16 cpu;
@ -142,19 +141,15 @@ protected:
// 0x74 // 0x74
LE32 ddirsentries; // usually 16 LE32 ddirsentries; // usually 16
struct ddirs_t __packed_struct(ddirs_t)
{
LE32 vaddr; LE32 vaddr;
LE32 size; LE32 size;
} __packed_struct_end()
__attribute_packed;
struct ddirs_t ddirs[16]; ddirs_t ddirs[16];
} __packed_struct_end()
__attribute_packed;
struct pe_section_t __packed_struct(pe_section_t)
{
char name[8]; char name[8];
LE32 vsize; LE32 vsize;
LE32 vaddr; LE32 vaddr;
@ -162,8 +157,7 @@ protected:
LE32 rawdataptr; LE32 rawdataptr;
char _[12]; char _[12];
LE32 flags; LE32 flags;
} __packed_struct_end()
__attribute_packed;
pe_header_t ih, oh; pe_header_t ih, oh;
pe_section_t *isection; pe_section_t *isection;
@ -333,8 +327,7 @@ protected:
class Export : private noncopyable class Export : private noncopyable
{ {
struct export_dir_t __packed_struct(export_dir_t)
{
char _[12]; // flags, timedate, version char _[12]; // flags, timedate, version
LE32 name; LE32 name;
char __[4]; // ordinal base char __[4]; // ordinal base
@ -343,8 +336,7 @@ protected:
LE32 addrtable; LE32 addrtable;
LE32 nameptrtable; LE32 nameptrtable;
LE32 ordinaltable; LE32 ordinaltable;
} __packed_struct_end()
__attribute_packed;
export_dir_t edir; export_dir_t edir;
char *ename; char *ename;

View File

@ -1,29 +1,35 @@
# Makefile for armpe_tester # Makefile for armpe_tester
MAKEFLAGS += -rR MAKEFLAGS += -rR
SHELL = /bin/sh .SUFFIXES:
.SECONDEXPANSION:
.NOTPARALLEL:
export LC_ALL = C
export SHELL = /bin/sh
# update path for our special stub build tools # update $PATH for our special stub build tools
ifneq ($(wildcard $(HOME)/local/bin/bin-upx),) ifneq ($(wildcard $(HOME)/local/bin/bin-upx/.),)
export PATH := $(HOME)/local/bin/bin-upx:$(PATH) export PATH := $(HOME)/local/bin/bin-upx:$(PATH)
endif endif
ifneq ($(wildcard $(HOME)/bin/bin-upx/.),)
ifneq ($(wildcard $(HOME)/bin/bin-upx),)
export PATH := $(HOME)/bin/bin-upx:$(PATH) export PATH := $(HOME)/bin/bin-upx:$(PATH)
endif endif
CFLAGS_W = -Wall -W -Wcast-align -Wcast-qual -Wwrite-strings
all: armpe_tester wtest.exe
armpe_tester: armpe_tester.c all: armpe_tester.out
arm-9tdmi-linux-gnu-gcc -Wl,--section-start,.interp=0x1000 -g -Wall -W -o $@ $< #all: armpe_tester.exe
wtest.exe: armpe_tester.c armpe_tester.out: armpe_tester.c $(MAKEFILE_LIST)
arm-wince-pe-gcc -Wl,--image-base,0x400000 -s -Wall -W -o $@ $< arm-linux-gcc-4.1.0 -Wl,--section-start,.interp=0x1000 -g $(CFLAGS_W) -o $@ $<
armpe_tester.exe: armpe_tester.c $(MAKEFILE_LIST)
arm-wince-pe-gcc -Wl,--image-base,0x400000 -s $(CFLAGS_W) -o $@ $<
mostlyclean clean distclean maintainer-clean: mostlyclean clean distclean maintainer-clean:
rm -f *.d *.o *.obj rm -f *.d *.o *.obj
rm -f armpe_tester wtest.exe rm -f armpe_tester.exe armpe_tester.out
.PHONY: all mostlyclean clean distclean maintainer-clean .PHONY: all mostlyclean clean distclean maintainer-clean

View File

@ -32,38 +32,46 @@
// arm-9tdmi-linux-gnu-gcc -Wl,--section-start,.interp=0x1000 // arm-9tdmi-linux-gnu-gcc -Wl,--section-start,.interp=0x1000
// arm-wince-pe-gcc -Wl,--image-base,0x400000 // arm-wince-pe-gcc -Wl,--image-base,0x400000
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <stdarg.h>
#ifdef i386 #ifdef __i386__
# define UPX_MMAP_ADDRESS 0x20000000 # define UPX_MMAP_ADDRESS 0x20000000
#else #else
# define UPX_MMAP_ADDRESS 0x10000 # define UPX_MMAP_ADDRESS 0x10000
#endif #endif
#ifdef linux #ifdef __linux__
# include <sys/mman.h> # include <sys/mman.h>
#else #else
void *VirtualAlloc(void *address, unsigned size, unsigned type, unsigned protect); void *VirtualAlloc(void *address, unsigned size, unsigned type, unsigned protect);
# define MEM_COMMIT 0x1000 # define MEM_COMMIT 0x1000
# define PAGE_EXECUTE_READWRITE 0x0040 # define PAGE_EXECUTE_READWRITE 0x0040
#endif #endif
typedef unsigned short LE16; typedef size_t acc_uintptr_t;
typedef unsigned long LE32; typedef unsigned short LE16;
#define __attribute_packed typedef unsigned int LE32;
#define get_le32(p) (* (const unsigned *) (p))
#define set_le32(p,v) (* (unsigned *) (p) = (v))
#define get_le16(p) (* (const unsigned short *) (p))
struct ddirs_t #if !defined(__packed_struct)
{ # define __packed_struct(s) struct s {
# define __packed_struct_end() };
#endif
__packed_struct(ddirs_t)
LE32 vaddr; LE32 vaddr;
LE32 size; LE32 size;
} __packed_struct_end()
__attribute_packed;
struct pe_header_t
{ __packed_struct(pe_header_t)
// 0x0 // 0x0
char _[4]; char _[4];
LE16 cpu; LE16 cpu;
@ -97,13 +105,12 @@ struct pe_header_t
char _____[20]; char _____[20];
// 0x74 // 0x74
LE32 ddirsentries; LE32 ddirsentries;
//
struct ddirs_t ddirs[16]; struct ddirs_t ddirs[16];
} __packed_struct_end()
__attribute_packed;
struct pe_section_t
{ __packed_struct(pe_section_t)
char name[8]; char name[8];
LE32 vsize; LE32 vsize;
LE32 vaddr; LE32 vaddr;
@ -111,12 +118,10 @@ struct pe_section_t
LE32 rawdataptr; LE32 rawdataptr;
char _[12]; char _[12];
LE32 flags; LE32 flags;
} __packed_struct_end()
__attribute_packed;
struct exe_header_t __packed_struct(exe_header_t)
{
LE16 mz; LE16 mz;
LE16 m512; LE16 m512;
LE16 p512; LE16 p512;
@ -124,8 +129,8 @@ struct exe_header_t
LE16 relocoffs; LE16 relocoffs;
char __[34]; char __[34];
LE32 nexepos; LE32 nexepos;
} __packed_struct_end()
__attribute_packed;
enum { enum {
PEDIR_EXPORT = 0, PEDIR_EXPORT = 0,
@ -145,9 +150,6 @@ enum {
PEDIR_COMRT = 14 PEDIR_COMRT = 14
}; };
#define get_le32(p) (*(unsigned *) (p))
#define set_le32(p, v) (*(unsigned *) (p) = (v))
#define get_le16(p) (*(unsigned short *) (p))
static struct pe_header_t ih; static struct pe_header_t ih;
static struct pe_section_t isections[4]; static struct pe_section_t isections[4];
@ -213,7 +215,7 @@ static int load(const char *file)
static int read(void) static int read(void)
{ {
unsigned ic; unsigned ic;
#ifdef linux #ifdef __linux__
vaddr = mmap((void *) UPX_MMAP_ADDRESS, ih.imagesize, vaddr = mmap((void *) UPX_MMAP_ADDRESS, ih.imagesize,
PROT_WRITE | PROT_READ | PROT_EXEC, PROT_WRITE | PROT_READ | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
@ -237,7 +239,7 @@ static int read(void)
static void dump(char n) static void dump(char n)
{ {
char buf[100]; char buf[100];
#ifdef linux #ifdef __linux__
snprintf(buf, sizeof(buf), "/tmp/a.dump%c", n); snprintf(buf, sizeof(buf), "/tmp/a.dump%c", n);
#else #else
snprintf(buf, sizeof(buf), "/a.dump%c", n); snprintf(buf, sizeof(buf), "/a.dump%c", n);
@ -247,7 +249,7 @@ static void dump(char n)
fclose(f2); fclose(f2);
} }
static int loadlibraryw(unsigned short *name) static int loadlibraryw(const unsigned short *name)
{ {
return name[0] + name[1] * 0x100 + name[2] * 0x10000; return name[0] + name[1] * 0x100 + name[2] * 0x10000;
} }
@ -315,7 +317,7 @@ static void dump2(int c)
static void call(void) static void call(void)
{ {
#ifndef i386 #ifndef __i386__
void (*entry)(void (*)(int), unsigned) = vaddr + ih.entry; void (*entry)(void (*)(int), unsigned) = vaddr + ih.entry;
entry(dump2, 1); entry(dump2, 1);
dump('z'); dump('z');
@ -346,10 +348,12 @@ static int main2(int argc, char **argv)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
out = stdout; out = stdout;
#ifndef linux #ifndef __linux__
out = fopen("/wtest.log", "wt"); out = fopen("/wtest.log", "wt");
#endif #endif
int ret = main2(argc, argv); int ret = main2(argc, argv);
fclose(out); fclose(out);
return ret; return ret;
} }
/* vim:set ts=4 sw=4 et: */