This commit is contained in:
John Reiser
2016-09-20 10:56:00 -07:00
21 changed files with 395 additions and 776 deletions
+4
View File
@@ -0,0 +1,4 @@
[submodule "src/lzma-sdk"]
path = src/lzma-sdk
url = https://github.com/upx/upx-lzma-sdk.git
branch = master
+4 -11
View File
@@ -66,16 +66,15 @@ Prerequisites
Tools needed to build/modify the UPX sources Tools needed to build/modify the UPX sources
-------------------------------------------- --------------------------------------------
- A C++ compiler supporting inner classes, templates, exceptions - A C++ compiler: gcc >= 3.3 or clang.
and RTTI.
- GNU make 3.81 or better - GNU make 3.81 or better
- zlib and zlib-devel [or zlib-dev] software packages for your - zlib and zlib-devel [or zlib-dev] software packages for your
host environment, or via http://www.cygwin.org/setup.exe host environment
- lzma SDK, such as from https://sourceforge.net/projects/sevenzip/ - LZMA SDK from https://github.com/upx/upx-lzma-sdk
files/LZMA%20SDK/4.43/ When using git run 'git submodule update --init --recursive'.
To compile the packer sources To compile the packer sources
@@ -87,12 +86,6 @@ To compile the packer sources
set UPX_UCLDIR=c:\src\ucl-1.03 (DOS / Windows) set UPX_UCLDIR=c:\src\ucl-1.03 (DOS / Windows)
export UPX_UCLDIR=$HOME/local/src/ucl-1.03 (Unix) export UPX_UCLDIR=$HOME/local/src/ucl-1.03 (Unix)
Set the environment variable UPX_LZMADIR to point to your
to your unpacked lzma443.tar.bz2 source directory, e.g.
export UPX_LZMADIR=$HOME/local/src/lzma-4.43 (Unix)
export UPX_LZMA_VERSION=0x443 # perhaps 0x459,0x458,0x457,0x449,0x442
Finally type Finally type
make make
+30 -39
View File
@@ -1,7 +1,7 @@
# #
# UPX Makefile - needs GNU make 3.81 or better # UPX Makefile - needs GNU make 3.81 or better
# #
# Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer # Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
# #
# In GNU Make 4.0 this "MAKEFLAGS += -rR" is not executed immediately. # In GNU Make 4.0 this "MAKEFLAGS += -rR" is not executed immediately.
@@ -9,7 +9,18 @@
# with respect to builtin variables. So if we want to have no # with respect to builtin variables. So if we want to have no
# dependencies on the default values of builtin variables, # dependencies on the default values of builtin variables,
# then we cannot use builtin variables at all. # then we cannot use builtin variables at all.
# See: http://lists.gnu.org/archive/html/help-make/2016-08/msg00011.html
MAKEFLAGS += -rR MAKEFLAGS += -rR
ifneq ($(upx_CXX),)
CXX = $(upx_CXX)
else
CXX = g++
endif
ifneq ($(upx_CXXLD),)
CXXLD = $(upx_CXXLD)
else
CXXLD = $(CXX)
endif
.SUFFIXES: .SUFFIXES:
export SHELL = /bin/sh export SHELL = /bin/sh
@@ -32,30 +43,21 @@ ifneq ($(srcdir),.)
VPATH := . $(srcdir) VPATH := . $(srcdir)
endif endif
ifeq ($(upx_CXX),)
upx_CXX = g++
endif
ifneq ($(findstring $(firstword $(upx_CXX)),g++),)
USE_GNUC ?= 1
endif
ifeq ($(USE_GNUC),1)
ifeq ($(DEBUG),1) ifeq ($(DEBUG),1)
upx_CXXFLAGS += -O0 -g CXXFLAGS += -O0 -g
else else
upx_CXXFLAGS += -O2 CXXFLAGS += -O2
LDFLAGS += -s LDFLAGS += -s
endif endif
# some gcc versions suffer over-agressive aliasing and wrapv optimization features/bugs # some gcc versions suffer over-agressive aliasing and wrapv optimization features/bugs
upx_CXXFLAGS_OPTIMIZE_STRICT_ALIASING ?= -fno-strict-aliasing CXXFLAGS_OPTIMIZE_STRICT_ALIASING ?= -fno-strict-aliasing
upx_CXXFLAGS_OPTIMIZE_WRAPV ?= -fwrapv CXXFLAGS_OPTIMIZE_WRAPV ?= -fwrapv
upx_CXXFLAGS += $(upx_CXXFLAGS_OPTIMIZE_STRICT_ALIASING) CXXFLAGS += $(CXXFLAGS_OPTIMIZE_STRICT_ALIASING)
upx_CXXFLAGS += $(upx_CXXFLAGS_OPTIMIZE_WRAPV) CXXFLAGS += $(CXXFLAGS_OPTIMIZE_WRAPV)
upx_CXXFLAGS += -Wall -W -Wcast-align -Wcast-qual -Wpointer-arith -Wshadow -Wwrite-strings CXXFLAGS += -Wall -W -Wcast-align -Wcast-qual -Wpointer-arith -Wshadow -Wwrite-strings
upx_CXXFLAGS_WERROR ?= -Werror CXXFLAGS_WERROR ?= -Werror
upx_CXXFLAGS += $(upx_CXXFLAGS_WERROR) CXXFLAGS += $(CXXFLAGS_WERROR)
endif CPPFLAGS += $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES)
upx_CPPFLAGS += $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES)
upx_CXXLD ?= $(upx_CXX)
exeext ?= .out exeext ?= .out
libext ?= .a libext ?= .a
@@ -70,13 +72,10 @@ INCLUDES += -I$(UPX_UCLDIR)/include
LIBS += $(addprefix -L,$(dir $(wildcard $(UPX_UCLDIR)/libucl$(libext) $(UPX_UCLDIR)/src/.libs/libucl$(libext)))) LIBS += $(addprefix -L,$(dir $(wildcard $(UPX_UCLDIR)/libucl$(libext) $(UPX_UCLDIR)/src/.libs/libucl$(libext))))
endif endif
LIBS += -lucl -lz LIBS += -lucl -lz
# you should set envvar UPX_LZMADIR to point to your unpacked LZMA SDK # LZMA from https://github.com/upx/upx-lzma-sdk
include $(top_srcdir)/src/stub/src/c/Makevars.lzma include $(top_srcdir)/src/stub/src/c/Makevars.lzma
$(info UPX_LZMA_VERSION=$(UPX_LZMA_VERSION))
ifneq ($(UPX_LZMA_VERSION),)
DEFS += -DWITH_LZMA=$(UPX_LZMA_VERSION) DEFS += -DWITH_LZMA=$(UPX_LZMA_VERSION)
INCLUDES += -I$(UPX_LZMADIR) INCLUDES += -I$(UPX_LZMADIR)
endif
all: upx$(exeext) | .depend all: upx$(exeext) | .depend
@@ -84,33 +83,25 @@ all: upx$(exeext) | .depend
upx$(exeext): $(upx_OBJECTS) $(upx_DEPENDENCIES) upx$(exeext): $(upx_OBJECTS) $(upx_DEPENDENCIES)
$($(notdir $@).PRE_LINK_STEP) $($(notdir $@).PRE_LINK_STEP)
$(strip $(upx_CXXLD) $(call e,upx_CPPFLAGS) $(call e,upx_CXXFLAGS) $(call e,LDFLAGS) -o $@ $(upx_OBJECTS) $(call e,LDADD) $(call e,LIBS)) $(strip $(CXXLD) $(call e,CPPFLAGS) $(call e,CXXFLAGS) $(call e,LDFLAGS) -o $@ $(upx_OBJECTS) $(call e,LDADD) $(call e,LIBS))
$($(notdir $@).POST_LINK_STEP) $($(notdir $@).POST_LINK_STEP)
%.o : %.cpp | .depend %.o : %.cpp | .depend
$(strip $(upx_CXX) $(call e,upx_CPPFLAGS) $(call e,upx_CXXFLAGS) -o $@ -c $<) $(strip $(CXX) $(call e,CPPFLAGS) $(call e,CXXFLAGS) -o $@ -c $<)
.depend: $(sort $(wildcard $(srcdir)/*.cpp $(srcdir)/*.h)) $(MAKEFILE_LIST) .depend: $(sort $(wildcard $(srcdir)/*.cpp $(srcdir)/*.h)) $(MAKEFILE_LIST)
@rm -f $@ @rm -f $@
ifeq ($(USE_GNUC),1) ifeq (1,1)
@echo "Updating $@" @echo "Updating $@"
@$(strip $(upx_CXX) $(call e,upx_CPPFLAGS) -MM) $(filter %.cpp,$^) > $@ @$(strip $(CXX) $(call e,CPPFLAGS) -MM) $(filter %.cpp,$^) > $@
else else
touch $@ touch $@
endif endif
ifeq ($(USE_GNUC),1) ##compress_lzma$(objext) : CXXFLAGS += -O3 -fomit-frame-pointer
##compress_lzma$(objext) : upx_CXXFLAGS += -O3 -fomit-frame-pointer compress_lzma$(objext) : CXXFLAGS += -Wno-shadow
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-cast-qual p_mach$(objext) : CXXFLAGS += -Wno-cast-align
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-shadow
ifeq ($(UPX_LZMA_VERSION),0x443)
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-non-virtual-dtor
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-unused
endif
# needed for gcc-4.3:
compress_lzma$(objext) : upx_CXXFLAGS += -Wno-error
endif
mostlyclean clean distclean maintainer-clean: mostlyclean clean distclean maintainer-clean:
+17 -5
View File
@@ -77,19 +77,31 @@ public:
private: private:
void checkNULL() const { void checkNULL() const {
if (!ptr_) if __acc_very_unlikely(!ptr_)
throwCantUnpack("unexpected NULL pointer; take care!"); throwCantUnpack("unexpected NULL pointer; take care!");
} }
void checkRange(size_t extra=0) const { void checkRange() const {
size_t off = (const char *) ptr_ - (const char *) base_; size_t off = (const char *) ptr_ - (const char *) base_;
if (off > size_ || off + extra > size_) if __acc_very_unlikely(off > size_)
throwCantUnpack("pointer out of range; take care!"); throwCantUnpack("pointer out of range; take care!");
} }
void checkStrict(size_t extra=0) const { void checkRange(size_t extra) const {
size_t off = (const char *) ptr_ - (const char *) base_;
if __acc_very_unlikely(off > size_ || off + extra > size_)
throwCantUnpack("pointer out of range; take care!");
}
void checkStrict() const {
checkNULL();
checkRange();
}
void checkStrict(size_t extra) const {
checkNULL(); checkNULL();
checkRange(extra); checkRange(extra);
} }
void check(size_t extra=0) const { void check() const {
if (ptr_) checkRange();
}
void check(size_t extra) const {
if (ptr_) checkRange(extra); if (ptr_) checkRange(extra);
} }
+11 -145
View File
@@ -36,25 +36,11 @@
// ACC // ACC
**************************************************************************/ **************************************************************************/
#if (defined(_WIN32) || defined(_WIN64)) && defined(_MSC_VER) #include "miniacc.h"
#ifndef _CRT_NONSTDC_NO_DEPRECATE #if !(ACC_CC_CLANG || ACC_CC_GNUC)
#define _CRT_NONSTDC_NO_DEPRECATE 1 # error "only clang and gcc are officially supported"
#endif
#ifndef _CRT_NONSTDC_NO_WARNINGS
#define _CRT_NONSTDC_NO_WARNINGS 1
#endif
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#endif #endif
#include "miniacc.h"
#if ((ACC_OS_WIN32 || ACC_OS_WIN64) && ACC_CC_MWERKS) && defined(__MSL__)
# undef HAVE_UTIME_H /* this pulls in <windows.h> */
#endif
// FIXME - quick hack for arm-wince-gcc-3.4 (Debian pocketpc-*.deb packages) // FIXME - quick hack for arm-wince-gcc-3.4 (Debian pocketpc-*.deb packages)
#if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(_WIN32) #if 1 && (ACC_ARCH_ARM) && defined(__pe__) && !defined(_WIN32)
# undef HAVE_CHMOD # undef HAVE_CHMOD
@@ -64,80 +50,6 @@
#endif #endif
// pragmas
#if (ACC_CC_BORLANDC)
# if (__BORLANDC__ < 0x0500)
# error "need Borland C++ 5.0 or newer"
# endif
# pragma warn -aus // 8004: 'x' is assigned a value that is never used
# pragma warn -inl // 8026+8027: Function not expanded inline
// Borland compilers typically produce a number of bogus warnings, and
// the actual diagnostics vary from version to version...
# if (__BORLANDC__ < 0x0530)
# pragma warn -csu // 8012: Comparing signed and unsigned values
# endif
# if (__BORLANDC__ >= 0x0530 && __BORLANDC__ < 0x0560)
# pragma warn -osh // 8055: Possible overflow in shift operation
# endif
# if (__BORLANDC__ >= 0x0560)
# pragma warn -use // 8080: 'x' is declared but never used
# endif
#elif (ACC_CC_DMC)
# if (__DMC__ < 0x829)
# error "need Digital Mars C++ 8.29 or newer"
# endif
#elif (ACC_CC_INTELC)
# if (__INTEL_COMPILER < 450)
# error "need Intel C++ 4.5 or newer"
# endif
# if (ACC_OS_WIN32 || ACC_OS_WIN64)
# elif defined(__linux__)
# pragma warning(error: 424) // #424: extra ";" ignored
//# pragma warning(disable: 128) // #128: loop is not reachable from preceding code
//# pragma warning(disable: 181) // #181: argument is incompatible with corresponding format string conversion
# pragma warning(disable: 193) // #193: zero used for undefined preprocessing identifier
# pragma warning(disable: 810) // #810: conversion from "A" to "B" may lose significant bits
# pragma warning(disable: 981) // #981: operands are evaluated in unspecified order
# pragma warning(disable: 1418) // #1418: external function definition with no prior declaration
//# pragma warning(disable: 1419) // #1419: external declaration in primary source file
# else
# error "untested platform"
# endif
#elif (ACC_CC_MSC)
# if (_MSC_VER < 1100)
# error "need Visual C++ 5.0 or newer"
# endif
# pragma warning(error: 4096) // W2: '__cdecl' must be used with '...'
# pragma warning(error: 4138) // Wx: '*/' found outside of comment
# pragma warning(disable: 4097) // W3: typedef-name 'A' used as synonym for class-name 'B'
# pragma warning(disable: 4511) // W3: 'class': copy constructor could not be generated
# pragma warning(disable: 4512) // W4: 'class': assignment operator could not be generated
# pragma warning(disable: 4514) // W4: 'function': unreferenced inline function has been removed
# pragma warning(disable: 4710) // W4: 'function': function not inlined
# if (_MSC_VER >= 1300)
# pragma warning(disable: 4625) // W4: 'class' : copy constructor could not be generated because a base class copy constructor is inaccessible
# pragma warning(disable: 4626) // W4: 'class' : assignment operator could not be generated because a base class assignment operator is inaccessible
# pragma warning(disable: 4711) // W4: 'function' selected for automatic inline expansion
# pragma warning(disable: 4820) // W4: 'struct' : 'x' bytes padding added after member 'member'
# endif
# if (_MSC_VER >= 1400)
# pragma warning(disable: 4996) // W1: 'function': was declared deprecated
# endif
#elif (ACC_CC_SUNPROC)
//# pragma error_messages(off,"badargtype2w") // FIXME
#elif (ACC_CC_WATCOMC)
# if (__WATCOMC__ < 1280)
# error "need Open Watcom C++ 1.8 or newer" // because earlier versions do not support nested classes
# endif
# if defined(__cplusplus)
# pragma warning 367 9 // w3: conditional expression in if statement is always true
# pragma warning 368 9 // w3: conditional expression in if statement is always false
# pragma warning 389 9 // w3: integral value may be truncated
# pragma warning 656 9 // w5: define this function inside its class definition (may improve code quality)
# endif
#endif
#define ACC_WANT_ACC_INCD_H 1 #define ACC_WANT_ACC_INCD_H 1
#define ACC_WANT_ACC_INCE_H 1 #define ACC_WANT_ACC_INCE_H 1
#define ACC_WANT_ACC_LIB_H 1 #define ACC_WANT_ACC_LIB_H 1
@@ -184,12 +96,6 @@ typedef acc_uintptr_t upx_uintptr_t;
#undef linux #undef linux
#undef small #undef small
#undef tos #undef tos
#if (ACC_CC_DMC)
# undef tell
#endif
#if !(ACC_CC_PGI)
# undef unix
#endif
#if defined(__DJGPP__) #if defined(__DJGPP__)
# undef sopen # undef sopen
# undef __unix__ # undef __unix__
@@ -199,11 +105,11 @@ typedef acc_uintptr_t upx_uintptr_t;
#if !defined(WITH_UCL) #if !defined(WITH_UCL)
# define WITH_UCL 1 # define WITH_UCL 1
#endif #endif
#if 0 && !defined(WITH_LZMA) #if !defined(WITH_LZMA) || (WITH_LZMA+0 == 0)
# define WITH_LZMA 1 # error "WITH_LZMA is missing"
#endif #endif
#if 1 && (ACC_CC_WATCOMC) #if (WITH_LZMA+0 != 0x443)
# undef WITH_LZMA # error "invalud WITH_LZMA"
#endif #endif
#if defined(UPX_OFFICIAL_BUILD) #if defined(UPX_OFFICIAL_BUILD)
# if !(WITH_LZMA) || !(WITH_NRV) || !(WITH_UCL) # if !(WITH_LZMA) || !(WITH_NRV) || !(WITH_UCL)
@@ -375,47 +281,28 @@ typedef acc_uintptr_t upx_uintptr_t;
#define TABLESIZE(table) ((sizeof(table)/sizeof((table)[0]))) #define TABLESIZE(table) ((sizeof(table)/sizeof((table)[0])))
#if 0
#define ALIGN_DOWN(a,b) (((a) / (b)) * (b))
#define ALIGN_UP(a,b) ALIGN_DOWN((a) + ((b) - 1), b)
#define ALIGN_GAP(a,b) (ALIGN_UP(a,b) - (a))
#elif 1
template <class T> template <class T>
inline T ALIGN_DOWN(const T& a, const T& b) { T r; r = (a / b) * b; return r; } inline T ALIGN_DOWN(const T& a, const T& b) { T r; r = (a / b) * b; return r; }
template <class T> template <class T>
inline T ALIGN_UP (const T& a, const T& b) { T r; r = ((a + b - 1) / b) * b; return r; } inline T ALIGN_UP (const T& a, const T& b) { T r; r = ((a + b - 1) / b) * b; return r; }
template <class T> template <class T>
inline T ALIGN_GAP (const T& a, const T& b) { T r; r = ALIGN_UP(a, b) - a; return r; } inline T ALIGN_GAP (const T& a, const T& b) { T r; r = ALIGN_UP(a, b) - a; return r; }
#else
inline unsigned ALIGN_DOWN(unsigned a, unsigned b) { return (a / b) * b; }
inline unsigned ALIGN_UP (unsigned a, unsigned b) { return ((a + b - 1) / b) * b; }
inline unsigned ALIGN_GAP (unsigned a, unsigned b) { return ALIGN_UP(a, b) - a; }
#endif
#if 0
#define UPX_MAX(a,b) ((a) >= (b) ? (a) : (b))
#define UPX_MIN(a,b) ((a) <= (b) ? (a) : (b))
#elif 1
template <class T> template <class T>
inline const T& UPX_MAX(const T& a, const T& b) { if (a < b) return b; return a; } inline const T& UPX_MAX(const T& a, const T& b) { if (a < b) return b; return a; }
template <class T> template <class T>
inline const T& UPX_MIN(const T& a, const T& b) { if (a < b) return a; return b; } inline const T& UPX_MIN(const T& a, const T& b) { if (a < b) return a; return b; }
#else
inline unsigned UPX_MAX(unsigned a, unsigned b) { return a < b ? b : a; }
inline unsigned UPX_MIN(unsigned a, unsigned b) { return a < b ? a : b; }
#endif
// An Array allocates memory on the heap, but automatically // An Array allocates memory on the heap, but automatically
// gets destructed when leaving scope or on exceptions. // gets destructed when leaving scope or on exceptions.
#define Array(type, var, size) \ #define Array(type, var, size) \
assert((int)(size) > 0); \ MemBuffer var ## _membuf(mem_size(sizeof(type), size)); \
MemBuffer var ## _membuf((size)*(sizeof(type))); \
type * const var = ((type *) var ## _membuf.getVoidPtr()) type * const var = ((type *) var ## _membuf.getVoidPtr())
#define ByteArray(var, size) Array(unsigned char, var, size) #define ByteArray(var, size) Array(unsigned char, var, size)
struct noncopyable class noncopyable
{ {
protected: protected:
inline noncopyable() {} inline noncopyable() {}
@@ -567,13 +454,7 @@ private:
**************************************************************************/ **************************************************************************/
struct upx_callback_t; struct upx_callback_t;
#define upx_callback_p upx_callback_t * typedef upx_callback_t *upx_callback_p;
#if 0
typedef void* (__acc_cdecl *upx_alloc_func_t)
(upx_callback_p self, unsigned items, unsigned size);
typedef void (__acc_cdecl *upx_free_func_t)
(upx_callback_p self, void* ptr);
#endif
typedef void (__acc_cdecl *upx_progress_func_t) typedef void (__acc_cdecl *upx_progress_func_t)
(upx_callback_p, unsigned, unsigned); (upx_callback_p, unsigned, unsigned);
@@ -609,15 +490,6 @@ struct OptVar
assertValue(); assertValue();
return *this; return *this;
} }
#if 0
#error
// there is too much implicit magic in this copy operator;
// better introduce an explicit "oassign" function.
OptVar& operator= (const OptVar& other) {
if (other.is_set) { v = other.v; is_set += 1; }
assertValue(); return *this;
}
#endif
void reset() { v = default_value; is_set = 0; } void reset() { v = default_value; is_set = 0; }
operator T () const { return v; } operator T () const { return v; }
@@ -631,15 +503,9 @@ struct OptVar
template <class T> inline void oassign(T& self, const T& other) { template <class T> inline void oassign(T& self, const T& other) {
if (other.is_set) { self.v = other.v; self.is_set += 1; } if (other.is_set) { self.v = other.v; self.is_set += 1; }
} }
#if 0
template <class V, class T> inline void oassign(V& v, const T& other) {
if (other.is_set) { v = other.v; }
}
#else
template <class T> inline void oassign(unsigned& v, const T& other) { template <class T> inline void oassign(unsigned& v, const T& other) {
if (other.is_set) { v = other.v; } if (other.is_set) { v = other.v; }
} }
#endif
struct lzma_compress_config_t struct lzma_compress_config_t
@@ -770,7 +636,7 @@ typedef ElfLinker Linker;
// main.cpp // main.cpp
extern const char *progname; extern const char *progname;
bool set_ec(int ec); bool set_exit_code(int ec);
#if (ACC_CC_CLANG || ACC_CC_GNUC || ACC_CC_LLVM || ACC_CC_PATHSCALE) #if (ACC_CC_CLANG || ACC_CC_GNUC || ACC_CC_LLVM || ACC_CC_PATHSCALE)
void e_exit(int ec) __attribute__((__noreturn__)); void e_exit(int ec) __attribute__((__noreturn__));
#else #else
+2 -2
View File
@@ -62,7 +62,7 @@ f80_jcc2(Filter const *f)
return f80_jmp1(f); return f80_jmp1(f);
} }
static int const N_MRU = 32; // does not have to be a power of 2 #define N_MRU 32 // does not have to be a power of 2
// Adaptively remember recent destinations. // Adaptively remember recent destinations.
static void static void
@@ -340,7 +340,7 @@ static int U(Filter *f)
} }
#endif #endif
#undef N_MRU
#undef F #undef F
#undef U #undef U
Submodule
+1
Submodule src/lzma-sdk added at ef24b001e9
+2 -18
View File
@@ -158,7 +158,7 @@ static bool set_eec(int ec, int *eec)
return 0; return 0;
} }
bool set_ec(int ec) bool set_exit_code(int ec)
{ {
return set_eec(ec,&exit_code); return set_eec(ec,&exit_code);
} }
@@ -166,7 +166,7 @@ bool set_ec(int ec)
void e_exit(int ec) void e_exit(int ec)
{ {
(void) set_ec(ec); (void) set_exit_code(ec);
do_exit(); do_exit();
} }
@@ -469,7 +469,6 @@ done:
return r; return r;
} }
#if 1 && (ACC_CC_GNUC >= 0x030300)
template <class T, T default_value, T min_value, T max_value> template <class T, T default_value, T min_value, T max_value>
int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_fatal) int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_fatal)
{ {
@@ -479,17 +478,6 @@ int getoptvar(OptVar<T,default_value,min_value,max_value> *var, const char *arg_
*var = v; *var = v;
return r; return r;
} }
#else
template <class T>
int getoptvar(T *var, const char *arg_fatal)
{
typename T::Type v = T::default_value_c;
int r = getoptvar(&v, T::min_value_c, T::max_value_c, arg_fatal);
if (r == 0)
*var = v;
return r;
}
#endif
static int do_option(int optc, const char *arg) static int do_option(int optc, const char *arg)
@@ -1289,13 +1277,9 @@ static bool test(void)
COMPILE_TIME_ASSERT_ALIGNED1(test2_t) COMPILE_TIME_ASSERT_ALIGNED1(test2_t)
COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21*sizeof(T)) COMPILE_TIME_ASSERT(sizeof(t2) == 7 + 21*sizeof(T))
#if defined(__acc_alignof) #if defined(__acc_alignof)
#if (ACC_CC_CLANG && ACC_ARCH_AMD64)
// CBUG: clang-2.8 bug in 64-bit mode
#else
COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1) COMPILE_TIME_ASSERT(__acc_alignof(t1) == 1)
COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1) COMPILE_TIME_ASSERT(__acc_alignof(t2) == 1)
#endif #endif
#endif
#if 1 && (ACC_CC_WATCOMC) #if 1 && (ACC_CC_WATCOMC)
test1_t t11; COMPILE_TIME_ASSERT(sizeof(t11.a) <= sizeof(t11.b)) test1_t t11; COMPILE_TIME_ASSERT(sizeof(t11.a) <= sizeof(t11.b))
test2_t t22; COMPILE_TIME_ASSERT(sizeof(t22.a) <= sizeof(t22.b)) test2_t t22; COMPILE_TIME_ASSERT(sizeof(t22.a) <= sizeof(t22.b))
+45 -14
View File
@@ -30,6 +30,43 @@
#include "mem.h" #include "mem.h"
/*************************************************************************
//
**************************************************************************/
// DO NOT CHANGE
#define MAX_SIZE (768 * 1024 * 1024)
ACC_COMPILE_TIME_ASSERT_HEADER(2ull * MAX_SIZE * 9 / 8 + 16*1024*1024 < INT_MAX)
size_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
{
assert(element_size > 0);
if (element_size > MAX_SIZE) throwCantPack("mem_size 1; take care");
if (n > MAX_SIZE) throwCantPack("mem_size 2; take care");
if (extra > MAX_SIZE) throwCantPack("mem_size 3; take care");
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
if (bytes > MAX_SIZE) throwCantPack("mem_size 4; take care");
return ACC_ICONV(size_t, bytes);
}
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n)
{
(void) mem_size(element_size, n); // check
return ACC_ICONV(size_t, n); // return n
}
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra)
{
assert(element_size > 0);
if (element_size > MAX_SIZE) return false;
if (n > MAX_SIZE) return false;
if (extra > MAX_SIZE) return false;
upx_uint64_t bytes = element_size * n + extra; // cannot overflow
if (bytes > MAX_SIZE) return false;
return true;
}
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
@@ -103,23 +140,19 @@ void MemBuffer::dealloc()
unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra) unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra)
{ {
assert((int)uncompressed_size > 0); size_t bytes = mem_size(1, uncompressed_size, extra);
assert((int)extra >= 0); bytes += uncompressed_size/8 + 256;
unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra; return ACC_ICONV(unsigned, bytes);
return size;
} }
unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra) unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra)
{ {
assert((int)uncompressed_size > 0); size_t bytes = mem_size(1, uncompressed_size, extra);
assert((int)extra >= 0);
unsigned size = uncompressed_size + extra;
// size += 512; // 512 safety bytes
// INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors // INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors
#if (ACC_ARCH_I386) #if (ACC_ARCH_I386)
size += 3; bytes += 3;
#endif #endif
return size; return ACC_ICONV(unsigned, bytes);
} }
@@ -187,10 +220,8 @@ void MemBuffer::alloc(unsigned size)
assert(b == NULL); assert(b == NULL);
assert(b_size == 0); assert(b_size == 0);
// //
assert((int)size > 0); size_t bytes = mem_size(1, size, use_mcheck ? 32 : 0);
unsigned total = use_mcheck ? size + 32 : size; unsigned char *p = (unsigned char *) malloc(bytes);
assert((int)total > 0);
unsigned char *p = (unsigned char *) malloc(total);
if (!p) if (!p)
throwOutOfMemoryException(); throwOutOfMemoryException();
b_size = size; b_size = size;
+7
View File
@@ -34,6 +34,13 @@
// //
**************************************************************************/ **************************************************************************/
size_t mem_size (upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0);
size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n);
bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0);
#define New(type,n) new type [ mem_size_get_n(sizeof(type),n) ]
class MemBuffer class MemBuffer
{ {
public: public:
+2 -2
View File
@@ -88,9 +88,9 @@ Linker* PackArmPe::newLinker() const
// import handling // import handling
**************************************************************************/ **************************************************************************/
void PackArmPe::processImports(unsigned myimport, unsigned iat_off) // pass 2 void PackArmPe::processImports2(unsigned myimport, unsigned iat_off) // pass 2
{ {
PeFile::processImports(myimport, iat_off); PeFile::processImports2(myimport, iat_off);
__packed_struct(import_desc) __packed_struct(import_desc)
LE32 oft; // orig first thunk LE32 oft; // orig first thunk
+1 -1
View File
@@ -67,7 +67,7 @@ protected:
virtual Linker* newLinker() const; virtual Linker* newLinker() const;
virtual const char *kernelDll() const { return "coredll.dll"; } virtual const char *kernelDll() const { return "coredll.dll"; }
virtual void processImports(unsigned, unsigned); virtual void processImports2(unsigned, unsigned);
virtual void addStubImports(); virtual void addStubImports();
virtual void processTls(Interval *); virtual void processTls(Interval *);
+20 -20
View File
@@ -230,14 +230,14 @@ PackLinuxElf32::PackLinuxElf32help1(InputFile *f)
if (f && Elf32_Ehdr::ET_DYN!=e_type) { if (f && Elf32_Ehdr::ET_DYN!=e_type) {
unsigned const len = sz_phdrs + e_phoff; unsigned const len = sz_phdrs + e_phoff;
file_image = new char[len]; file_image = New(char, len);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, len); f->readx(file_image, len);
phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
} }
if (f && Elf32_Ehdr::ET_DYN==e_type) { if (f && Elf32_Ehdr::ET_DYN==e_type) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, file_size); f->readx(file_image, file_size);
phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !!
@@ -600,14 +600,14 @@ PackLinuxElf64::PackLinuxElf64help1(InputFile *f)
if (f && Elf64_Ehdr::ET_DYN!=e_type) { if (f && Elf64_Ehdr::ET_DYN!=e_type) {
unsigned const len = sz_phdrs + e_phoff; unsigned const len = sz_phdrs + e_phoff;
file_image = new char[len]; file_image = New(char, len);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, len); f->readx(file_image, len);
phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !!
} }
if (f && Elf64_Ehdr::ET_DYN==e_type) { if (f && Elf64_Ehdr::ET_DYN==e_type) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
f->seek(0, SEEK_SET); f->seek(0, SEEK_SET);
f->readx(file_image, file_size); f->readx(file_image, file_size);
phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !! phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !!
@@ -911,7 +911,7 @@ PackLinuxElf32::buildLinuxLoader(
unsigned char const *const uncLoader = fold_hdrlen + fold; unsigned char const *const uncLoader = fold_hdrlen + fold;
h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc)); h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc));
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr]; unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr);
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr, int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
@@ -919,7 +919,7 @@ PackLinuxElf32::buildLinuxLoader(
#if 0 //{ debugging only #if 0 //{ debugging only
if (M_IS_LZMA(ph.method)) { if (M_IS_LZMA(ph.method)) {
ucl_uint tmp_len = h.sz_unc; // LZMA uses this as EOF ucl_uint tmp_len = h.sz_unc; // LZMA uses this as EOF
unsigned char *tmp = new unsigned char[tmp_len]; unsigned char *tmp = New(unsigned char, tmp_len);
memset(tmp, 0, tmp_len); memset(tmp, 0, tmp_len);
r = upx_decompress(sizeof(h) + cprLoader, h.sz_cpr, tmp, &tmp_len, h.b_method, NULL); r = upx_decompress(sizeof(h) + cprLoader, h.sz_cpr, tmp, &tmp_len, h.b_method, NULL);
if (r == UPX_E_OUT_OF_MEMORY) if (r == UPX_E_OUT_OF_MEMORY)
@@ -976,7 +976,7 @@ PackLinuxElf64::buildLinuxLoader(
unsigned char const *const uncLoader = fold_hdrlen + fold; unsigned char const *const uncLoader = fold_hdrlen + fold;
h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc)); h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc));
unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr]; unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr);
int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr, int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr,
NULL, ph.method, 10, NULL, NULL ); NULL, ph.method, 10, NULL, NULL );
if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc)
@@ -1662,7 +1662,7 @@ PackLinuxElf64ppcle::canPack()
if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) { if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size); fi->readx(file_image, file_size);
memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr)); memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr));
@@ -1837,7 +1837,7 @@ PackLinuxElf64amd::canPack()
if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) { if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) {
// The DT_STRTAB has no designated length. Read the whole file. // The DT_STRTAB has no designated length. Read the whole file.
file_image = new char[file_size]; file_image = New(char, file_size);
fi->seek(0, SEEK_SET); fi->seek(0, SEEK_SET);
fi->readx(file_image, file_size); fi->readx(file_image, file_size);
memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr)); memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr));
@@ -2302,7 +2302,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
} }
} }
if (note_size) { if (note_size) {
note_body = new unsigned char[note_size]; note_body = New(unsigned char, note_size);
note_size = 0; note_size = 0;
} }
phdr = phdri; phdr = phdri;
@@ -2342,7 +2342,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Shdr const *tmp = shdri; Elf32_Shdr const *tmp = shdri;
if (! shdri) { if (! shdri) {
shdr = new Elf32_Shdr[e_shnum]; shdr = New(Elf32_Shdr, e_shnum);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(ehdri.e_shoff,SEEK_SET); fi->seek(ehdri.e_shoff,SEEK_SET);
@@ -2355,7 +2355,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
//set the shstrtab //set the shstrtab
sec_strndx = &shdr[ehdri.e_shstrndx]; sec_strndx = &shdr[ehdri.e_shstrndx];
char *strtab = new char[(unsigned) sec_strndx->sh_size]; char *strtab = New(char, sec_strndx->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(sec_strndx->sh_offset,SEEK_SET); fi->seek(sec_strndx->sh_offset,SEEK_SET);
fi->readx(strtab,sec_strndx->sh_size); fi->readx(strtab,sec_strndx->sh_size);
@@ -2364,7 +2364,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/)
Elf32_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id"); Elf32_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id");
if (buildid) { if (buildid) {
unsigned char *data = new unsigned char[(unsigned) buildid->sh_size]; unsigned char *data = New(unsigned char, buildid->sh_size);
memset(data,0,buildid->sh_size); memset(data,0,buildid->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(buildid->sh_offset,SEEK_SET); fi->seek(buildid->sh_offset,SEEK_SET);
@@ -2495,7 +2495,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
} }
} }
if (note_size) { if (note_size) {
note_body = new unsigned char[note_size]; note_body = New(unsigned char, note_size);
note_size = 0; note_size = 0;
} }
phdr = phdri; phdr = phdri;
@@ -2540,7 +2540,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Shdr *shdr = NULL; Elf64_Shdr *shdr = NULL;
if (! shdri) { if (! shdri) {
shdr = new Elf64_Shdr[e_shnum]; shdr = New(Elf64_Shdr, e_shnum);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(ehdri.e_shoff,SEEK_SET); fi->seek(ehdri.e_shoff,SEEK_SET);
@@ -2553,7 +2553,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
//set the shstrtab //set the shstrtab
sec_strndx = &shdri[ehdri.e_shstrndx]; sec_strndx = &shdri[ehdri.e_shstrndx];
char *strtab = new char[(unsigned) sec_strndx->sh_size]; char *strtab = New(char, sec_strndx->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(sec_strndx->sh_offset,SEEK_SET); fi->seek(sec_strndx->sh_offset,SEEK_SET);
fi->readx(strtab,sec_strndx->sh_size); fi->readx(strtab,sec_strndx->sh_size);
@@ -2562,7 +2562,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/)
Elf64_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id"); Elf64_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id");
if (buildid) { if (buildid) {
unsigned char *data = new unsigned char[(unsigned) buildid->sh_size]; unsigned char *data = New(unsigned char, buildid->sh_size);
memset(data,0,buildid->sh_size); memset(data,0,buildid->sh_size);
fi->seek(0,SEEK_SET); fi->seek(0,SEEK_SET);
fi->seek(buildid->sh_offset,SEEK_SET); fi->seek(buildid->sh_offset,SEEK_SET);
@@ -3255,7 +3255,7 @@ void PackLinuxElf64::unpack(OutputFile *fo)
unsigned orig_file_size = get_te32(&hbuf.p_filesize); unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize); blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size if (file_size > (off_t)orig_file_size || blocksize > orig_file_size
|| blocksize > 1024*1024*1024) || !mem_size_valid(1, blocksize, OVERHEAD))
throwCantUnpack("p_info corrupted"); throwCantUnpack("p_info corrupted");
ibuf.alloc(blocksize + OVERHEAD); ibuf.alloc(blocksize + OVERHEAD);
@@ -3712,7 +3712,7 @@ Elf64_Sym const *PackLinuxElf64::elf_lookup(char const *name) const
unsigned const symbias = get_te32(&gashtab[1]); unsigned const symbias = get_te32(&gashtab[1]);
unsigned const n_bitmask = get_te32(&gashtab[2]); unsigned const n_bitmask = get_te32(&gashtab[2]);
unsigned const gnu_shift = get_te32(&gashtab[3]); unsigned const gnu_shift = get_te32(&gashtab[3]);
upx_uint64_t const *const bitmask = (upx_uint64_t const *)&gashtab[4]; upx_uint64_t const *const bitmask = (upx_uint64_t const *)(void const *)&gashtab[4];
unsigned const *const buckets = (unsigned const *)&bitmask[n_bitmask]; unsigned const *const buckets = (unsigned const *)&bitmask[n_bitmask];
unsigned const h = gnu_hash(name); unsigned const h = gnu_hash(name);
@@ -3784,7 +3784,7 @@ void PackLinuxElf32::unpack(OutputFile *fo)
unsigned orig_file_size = get_te32(&hbuf.p_filesize); unsigned orig_file_size = get_te32(&hbuf.p_filesize);
blocksize = get_te32(&hbuf.p_blocksize); blocksize = get_te32(&hbuf.p_blocksize);
if (file_size > (off_t)orig_file_size || blocksize > orig_file_size if (file_size > (off_t)orig_file_size || blocksize > orig_file_size
|| blocksize > 1024*1024*1024) || !mem_size_valid(1, blocksize, OVERHEAD))
throwCantUnpack("p_info corrupted"); throwCantUnpack("p_info corrupted");
ibuf.alloc(blocksize + OVERHEAD); ibuf.alloc(blocksize + OVERHEAD);
+43 -43
View File
@@ -48,9 +48,9 @@ __packed_struct(Mach_fat_arch)
__packed_struct_end() __packed_struct_end()
typedef struct { typedef struct {
uint32_t cmd; upx_uint32_t cmd;
uint32_t cmdsize; upx_uint32_t cmdsize;
uint32_t data[2]; // because cmdsize >= 16 upx_uint32_t data[2]; // because cmdsize >= 16
} Mach_command; // generic prefix } Mach_command; // generic prefix
/************************************************************************* /*************************************************************************
@@ -294,71 +294,71 @@ __packed_struct(Mach_uuid_command)
__packed_struct_end() __packed_struct_end()
typedef struct { typedef struct {
uint32_t cmd; // LC_MAIN; MH_EXECUTE only upx_uint32_t cmd; // LC_MAIN; MH_EXECUTE only
uint32_t cmdsize; // 24 upx_uint32_t cmdsize; // 24
uint64_t entryoff; // file offset of main() [expected in __TEXT] upx_uint64_t entryoff; // file offset of main() [expected in __TEXT]
uint64_t stacksize; // non-default initial stack size upx_uint64_t stacksize; // non-default initial stack size
} Mach_main_command; } Mach_main_command;
typedef struct { typedef struct {
uint32_t cmd; // LC_SOURCE_VERSION upx_uint32_t cmd; // LC_SOURCE_VERSION
uint32_t cmdsize; // 16 upx_uint32_t cmdsize; // 16
uint32_t long version; upx_uint32_t version;
} Mach_source_version_command; } Mach_source_version_command;
typedef struct { typedef struct {
uint32_t cmd; // LC_VERSION_MIN_MACOSX upx_uint32_t cmd; // LC_VERSION_MIN_MACOSX
uint32_t cmdsize; // 16 upx_uint32_t cmdsize; // 16
uint32_t version; // X.Y.Z ==> xxxx.yy.zz upx_uint32_t version; // X.Y.Z ==> xxxx.yy.zz
uint32_t sdk; // X.Y.Z ==> xxxx.yy.zz upx_uint32_t sdk; // X.Y.Z ==> xxxx.yy.zz
} Mach_version_min_command; } Mach_version_min_command;
typedef struct { typedef struct {
uint32_t cmd; // LC_DYLD_INFO_ONLY upx_uint32_t cmd; // LC_DYLD_INFO_ONLY
uint32_t cmdsize; // 48 upx_uint32_t cmdsize; // 48
uint32_t rebase_off; upx_uint32_t rebase_off;
uint32_t rebase_size; upx_uint32_t rebase_size;
uint32_t bind_off; upx_uint32_t bind_off;
uint32_t bind_size; upx_uint32_t bind_size;
uint32_t weak_bind_off; upx_uint32_t weak_bind_off;
uint32_t weak_bind_size; upx_uint32_t weak_bind_size;
uint32_t lazy_bind_off; upx_uint32_t lazy_bind_off;
uint32_t lazy_bind_size; upx_uint32_t lazy_bind_size;
uint32_t export_off; upx_uint32_t export_off;
uint32_t export_size; upx_uint32_t export_size;
} Mach_dyld_info_only_command; } Mach_dyld_info_only_command;
typedef struct { typedef struct {
uint32_t cmd; upx_uint32_t cmd;
uint32_t cmdsize; upx_uint32_t cmdsize;
uint32_t name; upx_uint32_t name;
} Mach_load_dylinker_command; } Mach_load_dylinker_command;
typedef struct { typedef struct {
uint32_t name; /* library's path name */ upx_uint32_t name; /* library's path name */
uint32_t timestamp; /* library's build time stamp */ upx_uint32_t timestamp; /* library's build time stamp */
uint32_t current_version; /* library's current version number */ upx_uint32_t current_version; /* library's current version number */
uint32_t compatibility_version; /* library's compatibility vers number*/ upx_uint32_t compatibility_version; /* library's compatibility vers number*/
} Mach_dylib; } Mach_dylib;
typedef struct { typedef struct {
uint32_t cmd; upx_uint32_t cmd;
uint32_t cmdsize; upx_uint32_t cmdsize;
Mach_dylib dylib; Mach_dylib dylib;
} Mach_load_dylib_command; } Mach_load_dylib_command;
typedef struct { typedef struct {
uint32_t cmd; upx_uint32_t cmd;
uint32_t cmdsize; upx_uint32_t cmdsize;
uint32_t dataoff; upx_uint32_t dataoff;
uint32_t datasize; upx_uint32_t datasize;
} Mach_function_starts_command; } Mach_function_starts_command;
typedef struct { typedef struct {
uint32_t cmd; upx_uint32_t cmd;
uint32_t cmdsize; upx_uint32_t cmdsize;
uint32_t dataoff; upx_uint32_t dataoff;
uint32_t datasize; upx_uint32_t datasize;
} Mach_data_in_code_command; } Mach_data_in_code_command;
template <class TMachITypes> template <class TMachITypes>
+1 -221
View File
@@ -98,7 +98,7 @@ PackVmlinuxBase<T>::compare_Phdr(void const *aa, void const *bb)
if (xa > xb) return 1; if (xa > xb) return 1;
if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr
if (a->p_paddr > b->p_paddr) return 1; if (a->p_paddr > b->p_paddr) return 1;
return 0; return 0;
} }
template <class T> template <class T>
@@ -1282,226 +1282,6 @@ bool PackVmlinuxAMD64::has_valid_vmlinux_head()
return true; return true;
} }
//
// Example usage within build system of Linux kernel-2.6.18:
//
//----- arch/i386/boot/compressed/Makefile
//#
//# linux/arch/i386/boot/compressed/Makefile
//#
//# create a compressed vmlinux image from the original vmlinux
//#
//
//targets := vmlinux upx-piggy.o
//
//LDFLAGS_vmlinux := -Ttext $(IMAGE_OFFSET) -e startup_32
//
//$(obj)/vmlinux: $(obj)/upx-piggy.o FORCE
// $(call if_changed,ld)
// @:
//
//$(obj)/upx-piggy.o: vmlinux FORCE
// upx --lzma -f -o $@ $<; touch $@
//
//#
//# The ORIGINAL build sequence using gzip is:
//# vmlinux Elf executable at top level in tree
//# (in same directory as MAINTAINERS)
//# In arch/i386:
//# boot/compressed/vmlinux.bin by objcopy -O binary
//# boot/compressed/vmlinux.bin.gz by gzip
//# boot/compressed/piggy.o by ld --format binary --oformat elf32-i386
//#
//# The 3 steps above create a linkable
//# compressed blob.
//# In arch/i386:
//# boot/compressed/vmlinux by ld head.o misc.o piggy.o
//# boot/vmlinux.bin by objcopy
//# boot/bzImage by arch/i386/boot/tools/build with
//# bootsect and setup
//#
//#
//# The MODIFIED build sequence using upx is:
//# vmlinux Elf executable at top level in tree
//# (in same directory as MAINTAINERS)
//# In arch/i386:
//# boot/compressed/upx-piggy.o by upx format vmlinux/386
//#
//# In arch/i386/boot:
//# boot/compressed/vmlinux by ld upx-piggy.o
//# boot/vmlinux.bin by objcopy
//# boot/bzImage by arch/i386/boot/tools/build with
//# bootsect and setup
//#
//-----
#if 0 /*{*/
// For Debian nslu2-linux (2.6.19), only this Makefile changes:
--- ./debian/build/build-arm-none-ixp4xx/arch/arm/boot/compressed/Makefile.orig 2006-11-29 13:57:37.000000000 -0800
+++ ./debian/build/build-arm-none-ixp4xx/arch/arm/boot/compressed/Makefile 2006-12-16 02:39:38.000000000 -0800
@@ -5,7 +5,7 @@
#
HEAD = head.o
-OBJS = misc.o
+OBJS =
FONTC = drivers/video/console/font_acorn_8x8.c
FONT = $(addprefix ../../../../drivers/video/console/, font_acorn_8x8.o)
@@ -73,8 +73,8 @@
SEDFLAGS = s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/
-targets := vmlinux vmlinux.lds piggy.gz piggy.o $(FONT) \
- head.o misc.o $(OBJS)
+targets := vmlinux vmlinux.lds upx-piggy.o $(FONT) \
+ head.o $(OBJS)
EXTRA_CFLAGS := -fpic
EXTRA_AFLAGS :=
@@ -95,20 +95,16 @@
# would otherwise mess up our GOT table
CFLAGS_misc.o := -Dstatic=
-$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.o \
- $(addprefix $(obj)/, $(OBJS)) FORCE
+$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) \
+ $(addprefix $(obj)/, $(OBJS)) $(obj)/upx-piggy.o FORCE
$(call if_changed,ld)
@:
-$(obj)/piggy.gz: $(obj)/../Image FORCE
- $(call if_changed,gzip)
-
-$(obj)/piggy.o: $(obj)/piggy.gz FORCE
+$(obj)/upx-piggy.o: vmlinux FORCE
+ upx --lzma -f -o $@ $<; touch $@
CFLAGS_font_acorn_8x8.o := -Dstatic=
$(obj)/vmlinux.lds: $(obj)/vmlinux.lds.in arch/arm/boot/Makefile .config
@sed "$(SEDFLAGS)" < $< > $@
-$(obj)/misc.o: $(obj)/misc.c include/asm/arch/uncompress.h lib/inflate.c
-
#endif /*}*/
// Approximate translation for Linux 2.4.x:
// - - -
// arch/i386/Makefile: LD_FLAGS=-e startup_32
//----- arch/i386/boot/compressed/Makefile
//# linux/arch/i386/boot/compressed/Makefile
//#
//# create a compressed vmlinux image from the original vmlinux
//#
//
//HEAD =
//SYSTEM = $(TOPDIR)/vmlinux
//
//OBJECTS = $(HEAD)
//
//ZLDFLAGS = -e startup_32
//
//#
//# ZIMAGE_OFFSET is the load offset of the compression loader
//# BZIMAGE_OFFSET is the load offset of the high loaded compression loader
//#
//ZIMAGE_OFFSET = 0x1000
//BZIMAGE_OFFSET = 0x100000
//
//ZLINKFLAGS = -Ttext $(ZIMAGE_OFFSET) $(ZLDFLAGS)
//BZLINKFLAGS = -Ttext $(BZIMAGE_OFFSET) $(ZLDFLAGS)
//
//all: vmlinux
//
//vmlinux: upx-piggy.o $(OBJECTS)
// $(LD) $(ZLINKFLAGS) -o vmlinux $(OBJECTS) upx-piggy.o
//
//bvmlinux: upx-piggy.o $(OBJECTS)
// $(LD) $(BZLINKFLAGS) -o bvmlinux $(OBJECTS) upx-piggy.o
//
//upx-piggy.o: $(SYSTEM)
// $(RM) -f $@
// upx --best -o $@ $<
// touch $@
//
//clean:
// rm -f vmlinux bvmlinux _tmp_*
//-----
//
// Example test jig:
// $ gcc -m32 -o test-piggy -nostartfiles -nostdlib test-piggy.o piggy.o
// $ gdb test-piggy
// (gdb) run >dumped
// (gdb) /* Execute [single step, etc.; the decompressor+unfilter moves!]
// * until reaching the 'lret' at the end of unfilter.
// */
// (gdb) set $pc= &dump
// (gdb) stepi
// (gdb) set $edx=<actual_uncompressed_length>
// (gdb) continue
// (gdb) q
// $ # Compare file 'dumped' with the portion of vmlinux that made piggy.o.
// $ dd if=vmlinux bs=<leader_size> skip=1 | cmp - dumped
// cmp: EOF on dumped
// $
//----- test-piggy.S
//#include <asm/mman.h>
//#include <asm/unistd.h>
//
//dump:
// movl $0x456789,%edx # length MODIFY THIS VALUE TO SUIT YOUR CASE
// movl $0x100000,%ecx # base
// movl $1,%ebx # stdout
// movl $ __NR_write,%eax
// int $0x80
// nop
// hlt
//mmap:
// pushl %ebx
// leal 2*4(%esp),%ebx
// pushl $ __NR_mmap; popl %eax
// int $0x80
// popl %ebx
// ret $6*4
//
//_start: .globl _start
// nop
// int3 # enter debugger!
// pushl $0
// pushl $0
// pushl $ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED
// pushl $ PROT_EXEC | PROT_WRITE | PROT_READ
// pushl $0x600000 # 6 MiB length
// pushl $0x100000 # 1 MiB address
// call mmap
// leal -0x9000(%esp),%esi # expect "lea 0x9000(%esi),%esp" later
///* Fall into .text of upx-compressed vmlinux. */
//-----
// Example test jig for ARM:
//-----main.c
//unsigned work[(1<<16)/sizeof(unsigned)];
//unsigned kernel[(3<<20)/sizeof(unsigned)];
//
///* In order to avoid complaints such as
// /usr/bin/ld: ERROR: libgcc_s.so uses hardware FP, whereas main uses software FP
// when building this test program, then you must change the .e_flags
// in the header of the compressed, relocatble output from upx, from
// Flags: 0x202, has entry point, GNU EABI, software FP
// to
// Flags: 0x0
//*/
//
//main()
//{
// char *const end = decompress_kernel(kernel, work, (1<<(16-2))+work, 0x1234);
// write(1, kernel, (char *)end - (char *)kernel);
// return 0;
//}
//-----
/************************************************************************* /*************************************************************************
// //
+7 -7
View File
@@ -207,7 +207,7 @@ void PackWcle::encodeObjectTable()
{ {
unsigned ic,jc; unsigned ic,jc;
oobject_table = new le_object_table_entry_t[soobject_table = 2]; oobject_table = New(le_object_table_entry_t, soobject_table = 2);
memset(oobject_table,0,soobject_table * sizeof(*oobject_table)); memset(oobject_table,0,soobject_table * sizeof(*oobject_table));
// object #1: // object #1:
@@ -242,7 +242,7 @@ void PackWcle::encodeObjectTable()
void PackWcle::encodePageMap() void PackWcle::encodePageMap()
{ {
opm_entries = new le_pagemap_entry_t[sopm_entries = opages]; opm_entries = New(le_pagemap_entry_t, sopm_entries = opages);
for (unsigned ic = 0; ic < sopm_entries; ic++) for (unsigned ic = 0; ic < sopm_entries; ic++)
{ {
opm_entries[ic].l = (unsigned char) (ic+1); opm_entries[ic].l = (unsigned char) (ic+1);
@@ -256,7 +256,7 @@ void PackWcle::encodePageMap()
void PackWcle::encodeFixupPageTable() void PackWcle::encodeFixupPageTable()
{ {
unsigned ic; unsigned ic;
ofpage_table = new unsigned[sofpage_table = 1 + opages]; ofpage_table = New(unsigned, sofpage_table = 1 + opages);
for (ofpage_table[0] = ic = 0; ic < opages; ic++) for (ofpage_table[0] = ic = 0; ic < opages; ic++)
set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA); set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA);
} }
@@ -264,7 +264,7 @@ void PackWcle::encodeFixupPageTable()
void PackWcle::encodeFixups() void PackWcle::encodeFixups()
{ {
ofixups = new upx_byte[sofixups = 1*7 + FIXUP_EXTRA]; ofixups = New(upx_byte, sofixups = 1*7 + FIXUP_EXTRA);
memset(ofixups,0,sofixups); memset(ofixups,0,sofixups);
ofixups[0] = 7; ofixups[0] = 7;
set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1)); set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1));
@@ -608,7 +608,7 @@ void PackWcle::decodeFixups()
selfrel_fixups++; selfrel_fixups++;
unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9; unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9;
ofixups = new upx_byte[fixupn*9+1000+selectlen*5]; ofixups = New(upx_byte, fixupn*9+1000+selectlen*5);
upx_bytep fp = ofixups; upx_bytep fp = ofixups;
for (ic = 1, jc = 0; ic <= opages; ic++) for (ic = 1, jc = 0; ic <= opages; ic++)
@@ -682,7 +682,7 @@ void PackWcle::decodeFixups()
void PackWcle::decodeFixupPageTable() void PackWcle::decodeFixupPageTable()
{ {
ofpage_table = new unsigned[sofpage_table = 1 + opages]; ofpage_table = New(unsigned, sofpage_table = 1 + opages);
set_le32(ofpage_table,0); set_le32(ofpage_table,0);
// the rest of ofpage_table is filled by decodeFixups() // the rest of ofpage_table is filled by decodeFixups()
} }
@@ -691,7 +691,7 @@ void PackWcle::decodeFixupPageTable()
void PackWcle::decodeObjectTable() void PackWcle::decodeObjectTable()
{ {
soobject_table = oimage[ph.u_len - 1]; soobject_table = oimage[ph.u_len - 1];
oobject_table = new le_object_table_entry_t[soobject_table]; oobject_table = New(le_object_table_entry_t, soobject_table);
unsigned jc, ic = soobject_table * sizeof(*oobject_table); unsigned jc, ic = soobject_table * sizeof(*oobject_table);
const unsigned extradata = ph.version == 10 ? 17 : 13; const unsigned extradata = ph.version == 10 ? 17 : 13;
+25 -36
View File
@@ -58,7 +58,6 @@ static unsigned my_strlen(const unsigned char *s)
#define strlen my_strlen #define strlen my_strlen
#endif #endif
#if (__ACC_CXX_HAVE_PLACEMENT_DELETE) || defined(__DJGPP__)
#include "bptr.h" #include "bptr.h"
#define IPTR(type, var) BoundedPtr<type> var(ibuf, ibuf.getSize()) #define IPTR(type, var) BoundedPtr<type> var(ibuf, ibuf.getSize())
#define OPTR(type, var) BoundedPtr<type> var(obuf, obuf.getSize()) #define OPTR(type, var) BoundedPtr<type> var(obuf, obuf.getSize())
@@ -68,16 +67,6 @@ static unsigned my_strlen(const unsigned char *s)
#define OPTR_I(type, var, v) BoundedPtr<type> var(obuf, obuf.getSize(), v) #define OPTR_I(type, var, v) BoundedPtr<type> var(obuf, obuf.getSize(), v)
#define IPTR_C(type, var, v) const BoundedPtr<type> var(ibuf, ibuf.getSize(), v) #define IPTR_C(type, var, v) const BoundedPtr<type> var(ibuf, ibuf.getSize(), v)
#define OPTR_C(type, var, v) const BoundedPtr<type> var(obuf, obuf.getSize(), v) #define OPTR_C(type, var, v) const BoundedPtr<type> var(obuf, obuf.getSize(), v)
#else
#define IPTR(type, var) type* var = 0
#define OPTR(type, var) type* var = 0
#define IPTR_I_D(type, var, disp) \
type* var = (v) + (disp)
#define IPTR_I(type, var, v) type* var = (v)
#define OPTR_I(type, var, v) type* var = (v)
#define IPTR_C(type, var, v) type* const var = (v)
#define OPTR_C(type, var, v) type* const var = (v)
#endif
static void xcheck(const void *p) static void xcheck(const void *p)
{ {
@@ -321,7 +310,7 @@ PeFile::Reloc::Reloc(upx_byte *s,unsigned si) :
PeFile::Reloc::Reloc(unsigned rnum) : PeFile::Reloc::Reloc(unsigned rnum) :
start(NULL), size(0), rel(NULL), rel1(NULL) start(NULL), size(0), rel(NULL), rel1(NULL)
{ {
start = new upx_byte[rnum * 4 + 8192]; start = new upx_byte[mem_size(4, rnum, 8192)];
counts[0] = 0; counts[0] = 0;
} }
@@ -404,7 +393,7 @@ void PeFile32::processRelocs() // pass1
LE32 *fix[4]; LE32 *fix[4];
for (; ic; ic--) for (; ic; ic--)
fix[ic] = new LE32 [counts[ic]]; fix[ic] = New(LE32, counts[ic]);
unsigned xcounts[4]; unsigned xcounts[4];
memset(xcounts, 0, sizeof(xcounts)); memset(xcounts, 0, sizeof(xcounts));
@@ -441,7 +430,7 @@ void PeFile32::processRelocs() // pass1
} }
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3], sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3],
orelocs, ibuf + rvamin,1, &big_relocs), orelocs, ibuf + rvamin,1, &big_relocs),
orelocs); orelocs);
@@ -499,7 +488,7 @@ void PeFile64::processRelocs() // pass1
LE32 *fix[16]; LE32 *fix[16];
for (ic = 15; ic; ic--) for (ic = 15; ic; ic--)
fix[ic] = new LE32 [counts[ic]]; fix[ic] = New(LE32, counts[ic]);
unsigned xcounts[16]; unsigned xcounts[16];
memset(xcounts, 0, sizeof(xcounts)); memset(xcounts, 0, sizeof(xcounts));
@@ -539,7 +528,7 @@ void PeFile64::processRelocs() // pass1
} }
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10], sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10],
orelocs, ibuf + rvamin,1, &big_relocs), orelocs, ibuf + rvamin,1, &big_relocs),
orelocs); orelocs);
@@ -628,7 +617,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64
unsigned l = strlen(dll); unsigned l = strlen(dll);
assert(l > 0); assert(l > 0);
char *name = new char[3 * l + 2]; char *name = New(char, 3 * l + 2);
assert(name); assert(name);
name[0] = first_char; name[0] = first_char;
char *n = name + 1 + 2 * l; char *n = name + 1 + 2 * l;
@@ -643,7 +632,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64
{ {
unsigned len = 1 + 2 * strlen(dll) + 1 + 2 * strlen(proc) + 1 + 1; unsigned len = 1 + 2 * strlen(dll) + 1 + 2 * strlen(proc) + 1 + 1;
tstr dlln(name_for_dll(dll, first_char)); tstr dlln(name_for_dll(dll, first_char));
char *procn = new char[len]; char *procn = New(char, len);
upx_snprintf(procn, len - 1, "%s%c", (const char*) dlln, separator); upx_snprintf(procn, len - 1, "%s%c", (const char*) dlln, separator);
encode_name(proc, procn + strlen(procn)); encode_name(proc, procn + strlen(procn));
return procn; return procn;
@@ -772,7 +761,7 @@ public:
int osize = 4 + 2 * nsections; // upper limit for alignments int osize = 4 + 2 * nsections; // upper limit for alignments
for (unsigned ic = 0; ic < nsections; ic++) for (unsigned ic = 0; ic < nsections; ic++)
osize += sections[ic]->size; osize += sections[ic]->size;
output = new upx_byte[osize]; output = New(upx_byte, osize);
outputlen = 0; outputlen = 0;
// sort the sections by name before adding them all // sort the sections by name before adding them all
@@ -787,7 +776,7 @@ public:
return outputlen; return outputlen;
} }
void relocate(unsigned myimport) void relocate_import(unsigned myimport)
{ {
assert(nsections > 0); assert(nsections > 0);
assert(output); assert(output);
@@ -856,11 +845,11 @@ void PeFile::addStubImports()
addKernelImport("VirtualProtect"); addKernelImport("VirtualProtect");
} }
void PeFile::processImports(unsigned myimport, unsigned) // pass 2 void PeFile::processImports2(unsigned myimport, unsigned) // pass 2
{ {
COMPILE_TIME_ASSERT(sizeof(import_desc) == 20); COMPILE_TIME_ASSERT(sizeof(import_desc) == 20);
ilinker->relocate(myimport); ilinker->relocate_import(myimport);
int len; int len;
oimpdlls = ilinker->getLoader(&len); oimpdlls = ilinker->getLoader(&len);
assert(len == (int) soimpdlls); assert(len == (int) soimpdlls);
@@ -947,7 +936,7 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1
soimport++; // separator soimport++; // separator
} }
} }
oimport = new upx_byte[soimport]; oimport = New(upx_byte, soimport);
memset(oimport,0,soimport); memset(oimport,0,soimport);
qsort(idlls,dllnum,sizeof (udll*),udll::compare); qsort(idlls,dllnum,sizeof (udll*),udll::compare);
@@ -1123,13 +1112,13 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize)
iv.add(edir.name,len); iv.add(edir.name,len);
len = 4 * edir.functions; len = 4 * edir.functions;
functionptrs = new char[len + 1]; functionptrs = New(char, len + 1);
memcpy(functionptrs,base + edir.addrtable,len); memcpy(functionptrs,base + edir.addrtable,len);
size += len; size += len;
iv.add(edir.addrtable,len); iv.add(edir.addrtable,len);
unsigned ic; unsigned ic;
names = new char* [edir.names + edir.functions + 1]; names = New(char *, edir.names + edir.functions + 1);
for (ic = 0; ic < edir.names; ic++) for (ic = 0; ic < edir.names; ic++)
{ {
char *n = base + get_le32(base + edir.nameptrtable + ic * 4); char *n = base + get_le32(base + edir.nameptrtable + ic * 4);
@@ -1156,7 +1145,7 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize)
names[ic + edir.names] = NULL; names[ic + edir.names] = NULL;
len = 2 * edir.names; len = 2 * edir.names;
ordinals = new char[len + 1]; ordinals = New(char, len + 1);
memcpy(ordinals,base + edir.ordinaltable,len); memcpy(ordinals,base + edir.ordinaltable,len);
size += len; size += len;
iv.add(edir.ordinaltable,len); iv.add(edir.ordinaltable,len);
@@ -1218,7 +1207,7 @@ void PeFile::processExports(Export *xport) // pass1
} }
xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT)); xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT));
soexport = ALIGN_UP(xport->getsize(), 4u); soexport = ALIGN_UP(xport->getsize(), 4u);
oexport = new upx_byte[soexport]; oexport = New(upx_byte, soexport);
memset(oexport, 0, soexport); memset(oexport, 0, soexport);
} }
@@ -1338,7 +1327,7 @@ void PeFile::processTls1(Interval *iv,
sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size; sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size;
// the PE loader wants this stuff uncompressed // the PE loader wants this stuff uncompressed
otls = new upx_byte[sotls]; otls = New(upx_byte, sotls);
memset(otls,0,sotls); memset(otls,0,sotls);
memcpy(otls,ibuf + IDADDR(PEDIR_TLS),sizeof(tls)); memcpy(otls,ibuf + IDADDR(PEDIR_TLS),sizeof(tls));
// WARNING: this can acces data in BSS // WARNING: this can acces data in BSS
@@ -1430,7 +1419,7 @@ void PeFile::processLoadConf(Interval *iv) // pass 1
// printf("loadconf reloc detected: %x\n", pos); // printf("loadconf reloc detected: %x\n", pos);
} }
oloadconf = new upx_byte[soloadconf]; oloadconf = New(upx_byte, soloadconf);
memcpy(oloadconf, loadconf, soloadconf); memcpy(oloadconf, loadconf, soloadconf);
} }
@@ -1654,7 +1643,7 @@ PeFile::Resource::upx_rnode *PeFile::Resource::convert(const void *rnode,
ibufcheck(p, 2); ibufcheck(p, 2);
const unsigned len = 2 + 2 * get_le16(p); const unsigned len = 2 + 2 * get_le16(p);
ibufcheck(p, len); ibufcheck(p, len);
child->name = new upx_byte[len]; child->name = New(upx_byte, len);
memcpy(child->name,p,len); // copy unicode string memcpy(child->name,p,len); // copy unicode string
ssize += len; // size of unicode strings ssize += len; // size of unicode strings
} }
@@ -1709,7 +1698,7 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos,
upx_byte *PeFile::Resource::build() upx_byte *PeFile::Resource::build()
{ {
newstart = new upx_byte [dirsize()]; newstart = New(upx_byte, dirsize());
unsigned bpos = 0,spos = dsize; unsigned bpos = 0,spos = dsize;
build(root,bpos,spos,0); build(root,bpos,spos,0);
@@ -1875,7 +1864,7 @@ void PeFile::processResources(Resource *res)
for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size()) for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size())
; ;
oresources = new upx_byte[soresources]; oresources = New(upx_byte, soresources);
upx_byte *ores = oresources + res->dirsize(); upx_byte *ores = oresources + res->dirsize();
char *keep_icons = NULL; // icon ids in the first icon group char *keep_icons = NULL; // icon ids in the first icon group
@@ -1885,7 +1874,7 @@ void PeFile::processResources(Resource *res)
if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0) if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0)
{ {
iconsin1stdir = get_le16(ibuf + res->offs() + 4); iconsin1stdir = get_le16(ibuf + res->offs() + 4);
keep_icons = new char[1 + iconsin1stdir * 9]; keep_icons = New(char, 1 + iconsin1stdir * 9);
*keep_icons = 0; *keep_icons = 0;
for (unsigned ic = 0; ic < iconsin1stdir; ic++) for (unsigned ic = 0; ic < iconsin1stdir; ic++)
upx_snprintf(keep_icons + strlen(keep_icons), 9, "3/%u,", upx_snprintf(keep_icons + strlen(keep_icons), 9, "3/%u,",
@@ -2041,7 +2030,7 @@ unsigned PeFile::stripDebug(unsigned overlaystart)
void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih) void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih)
{ {
isection = new pe_section_t[objs]; isection = New(pe_section_t, objs);
fi->seek(pe_offset+sizeof_ih,SEEK_SET); fi->seek(pe_offset+sizeof_ih,SEEK_SET);
fi->readx(isection,sizeof(pe_section_t)*objs); fi->readx(isection,sizeof(pe_section_t)*objs);
rvamin = isection[0].vaddr; rvamin = isection[0].vaddr;
@@ -2370,7 +2359,7 @@ void PeFile::pack0(OutputFile *fo, ht &ih, ht &oh,
if (rel_at_sections_start) if (rel_at_sections_start)
callProcessRelocs(rel, ic); callProcessRelocs(rel, ic);
processImports(ic, getProcessImportParam(upxsection)); processImports2(ic, getProcessImportParam(upxsection));
ODADDR(PEDIR_IMPORT) = ic; ODADDR(PEDIR_IMPORT) = ic;
ODSIZE(PEDIR_IMPORT) = soimpdlls; ODSIZE(PEDIR_IMPORT) = soimpdlls;
ic += soimpdlls; ic += soimpdlls;
@@ -2931,7 +2920,7 @@ int PeFile::canUnpack0(unsigned max_sections, LE16 &ih_objects,
return false; return false;
unsigned objs = ih_objects; unsigned objs = ih_objects;
isection = new pe_section_t[objs]; isection = New(pe_section_t, objs);
fi->seek(pe_offset + ihsize, SEEK_SET); fi->seek(pe_offset + ihsize, SEEK_SET);
fi->readx(isection,sizeof(pe_section_t)*objs); fi->readx(isection,sizeof(pe_section_t)*objs);
if (ih_objects < 3) if (ih_objects < 3)
+1 -1
View File
@@ -101,7 +101,7 @@ protected:
void rebuildImports(upx_byte *& extrainfo, void rebuildImports(upx_byte *& extrainfo,
ord_mask_t ord_mask, bool set_oft); ord_mask_t ord_mask, bool set_oft);
virtual unsigned processImports() = 0; virtual unsigned processImports() = 0;
virtual void processImports(unsigned, unsigned); virtual void processImports2(unsigned, unsigned);
upx_byte *oimport; upx_byte *oimport;
unsigned soimport; unsigned soimport;
upx_byte *oimpdlls; upx_byte *oimpdlls;
+159 -184
View File
@@ -58,7 +58,6 @@
* *
**************************************************************/ **************************************************************/
#if 1 #if 1
#include "conf.h" #include "conf.h"
#else #else
@@ -73,26 +72,26 @@
#undef LLONG #undef LLONG
#undef ULLONG #undef ULLONG
#if 1 #if 1
# define LLONG upx_int64_t #define LLONG upx_int64_t
# define ULLONG upx_uint64_t #define ULLONG upx_uint64_t
#else #else
# define LLONG long int #define LLONG long int
# define ULLONG unsigned long int #define ULLONG unsigned long int
#endif #endif
#undef NO_FLOAT #undef NO_FLOAT
#undef LDOUBLE #undef LDOUBLE
#if 1 #if 1
# define NO_FLOAT 1 #define NO_FLOAT 1
# if (ACC_CC_SUNPROC) #if (ACC_CC_SUNPROC)
# else
# define float error no_float
# define double error no_float
# endif
#elif 0 || (HAVE_LONG_DOUBLE)
# define LDOUBLE long double
#else #else
# define LDOUBLE double #define float error no_float
#define double error no_float
#endif
#elif 0 || (HAVE_LONG_DOUBLE)
#define LDOUBLE long double
#else
#define LDOUBLE double
#endif #endif
/* /*
@@ -101,50 +100,46 @@
/* format read states */ /* format read states */
#define DP_S_DEFAULT 0 #define DP_S_DEFAULT 0
#define DP_S_FLAGS 1 #define DP_S_FLAGS 1
#define DP_S_MIN 2 #define DP_S_MIN 2
#define DP_S_DOT 3 #define DP_S_DOT 3
#define DP_S_MAX 4 #define DP_S_MAX 4
#define DP_S_MOD 5 #define DP_S_MOD 5
#define DP_S_CONV 6 #define DP_S_CONV 6
#define DP_S_DONE 7 #define DP_S_DONE 7
/* format flags - Bits */ /* format flags - Bits */
#define DP_F_MINUS (1 << 0) #define DP_F_MINUS (1 << 0)
#define DP_F_PLUS (1 << 1) #define DP_F_PLUS (1 << 1)
#define DP_F_SPACE (1 << 2) #define DP_F_SPACE (1 << 2)
#define DP_F_NUM (1 << 3) #define DP_F_NUM (1 << 3)
#define DP_F_ZERO (1 << 4) #define DP_F_ZERO (1 << 4)
#define DP_F_UP (1 << 5) #define DP_F_UP (1 << 5)
#define DP_F_UNSIGNED (1 << 6) #define DP_F_UNSIGNED (1 << 6)
/* Conversion Flags */ /* Conversion Flags */
#define DP_C_SHORT 1 #define DP_C_SHORT 1
#define DP_C_LONG 2 #define DP_C_LONG 2
#define DP_C_LDOUBLE 3 #define DP_C_LDOUBLE 3
#define DP_C_LLONG 4 #define DP_C_LLONG 4
#define char_to_int(p) ((p)- '0') #define char_to_int(p) ((p) - '0')
#undef MAX #undef MAX
#define MAX(p,q) (((p) >= (q)) ? (p) : (q)) #define MAX(p, q) (((p) >= (q)) ? (p) : (q))
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c) static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c) {
{
if (*currlen < maxlen) if (*currlen < maxlen)
buffer[*currlen] = (char) c; buffer[*currlen] = (char) c;
*currlen += 1; *currlen += 1;
} }
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, const char *value, int flags,
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen, int min, int max) {
const char *value, int flags, int min, int max) int padlen, strln; /* amount to pad */
{
int padlen, strln; /* amount to pad */
int cnt = 0; int cnt = 0;
#ifdef DEBUG_SNPRINTF #ifdef DEBUG_SNPRINTF
@@ -153,7 +148,7 @@ static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
if (value == NULL) if (value == NULL)
value = "<NULL>"; value = "<NULL>";
for (strln = 0; value[strln]; ) /* strlen */ for (strln = 0; value[strln];) /* strlen */
++strln; ++strln;
padlen = min - strln; padlen = min - strln;
if (padlen < 0) if (padlen < 0)
@@ -162,29 +157,27 @@ static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
padlen = -padlen; /* Left Justify */ padlen = -padlen; /* Left Justify */
while ((padlen > 0) && (cnt < max)) { while ((padlen > 0) && (cnt < max)) {
dopr_outch (buffer, currlen, maxlen, ' '); dopr_outch(buffer, currlen, maxlen, ' ');
--padlen; --padlen;
++cnt; ++cnt;
} }
while (*value && (cnt < max)) { while (*value && (cnt < max)) {
dopr_outch (buffer, currlen, maxlen, *value++); dopr_outch(buffer, currlen, maxlen, *value++);
++cnt; ++cnt;
} }
while ((padlen < 0) && (cnt < max)) { while ((padlen < 0) && (cnt < max)) {
dopr_outch (buffer, currlen, maxlen, ' '); dopr_outch(buffer, currlen, maxlen, ' ');
++padlen; ++padlen;
++cnt; ++cnt;
} }
} }
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
static void fmtint(char *buffer, size_t *currlen, size_t maxlen, static void fmtint(char *buffer, size_t *currlen, size_t maxlen, LLONG value, unsigned base,
LLONG value, unsigned base, int min, int max, int flags) int min, int max, int flags) {
{
int signvalue = 0; int signvalue = 0;
ULLONG uvalue; ULLONG uvalue;
char convert[64+1]; char convert[64 + 1];
int place = 0; int place = 0;
int spadlen = 0; /* amount to space pad */ int spadlen = 0; /* amount to space pad */
int zpadlen = 0; /* amount to zero pad */ int zpadlen = 0; /* amount to zero pad */
@@ -197,11 +190,11 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
uvalue = value; uvalue = value;
if (!(flags & DP_F_UNSIGNED)) { if (!(flags & DP_F_UNSIGNED)) {
if( value < 0 ) { if (value < 0) {
signvalue = '-'; signvalue = '-';
uvalue = -value; uvalue = -value;
} else { } else {
if (flags & DP_F_PLUS) /* Do a sign (+/i) */ if (flags & DP_F_PLUS) /* Do a sign (+/i) */
signvalue = '+'; signvalue = '+';
else if (flags & DP_F_SPACE) else if (flags & DP_F_SPACE)
signvalue = ' '; signvalue = ' ';
@@ -212,13 +205,15 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
do { do {
convert[place] = digits[(unsigned) (uvalue % base)]; convert[place] = digits[(unsigned) (uvalue % base)];
uvalue /= base; uvalue /= base;
} while (++place < (int)sizeof(convert) - 1 && uvalue); } while (++place < (int) sizeof(convert) - 1 && uvalue);
convert[place] = 0; convert[place] = 0;
zpadlen = max - place; zpadlen = max - place;
spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
if (zpadlen < 0) zpadlen = 0; if (zpadlen < 0)
if (spadlen < 0) spadlen = 0; zpadlen = 0;
if (spadlen < 0)
spadlen = 0;
if (flags & DP_F_ZERO) { if (flags & DP_F_ZERO) {
zpadlen = MAX(zpadlen, spadlen); zpadlen = MAX(zpadlen, spadlen);
spadlen = 0; spadlen = 0;
@@ -227,46 +222,43 @@ static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
spadlen = -spadlen; /* Left Justifty */ spadlen = -spadlen; /* Left Justifty */
#ifdef DEBUG_SNPRINTF #ifdef DEBUG_SNPRINTF
printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", zpadlen, spadlen, min, max, place);
zpadlen, spadlen, min, max, place);
#endif #endif
/* Spaces */ /* Spaces */
while (spadlen > 0) { while (spadlen > 0) {
dopr_outch (buffer, currlen, maxlen, ' '); dopr_outch(buffer, currlen, maxlen, ' ');
--spadlen; --spadlen;
} }
/* Sign */ /* Sign */
if (signvalue) if (signvalue)
dopr_outch (buffer, currlen, maxlen, signvalue); dopr_outch(buffer, currlen, maxlen, signvalue);
/* Zeros */ /* Zeros */
while (zpadlen > 0) { while (zpadlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0'); dopr_outch(buffer, currlen, maxlen, '0');
--zpadlen; --zpadlen;
} }
/* Digits */ /* Digits */
while (place > 0) while (place > 0)
dopr_outch (buffer, currlen, maxlen, convert[--place]); dopr_outch(buffer, currlen, maxlen, convert[--place]);
/* Left Justified spaces */ /* Left Justified spaces */
while (spadlen < 0) { while (spadlen < 0) {
dopr_outch (buffer, currlen, maxlen, ' '); dopr_outch(buffer, currlen, maxlen, ' ');
++spadlen; ++spadlen;
} }
} }
/************************************************************************* /*************************************************************************
// floating format support // floating format support
**************************************************************************/ **************************************************************************/
#if !(NO_FLOAT) #if !(NO_FLOAT)
static LDOUBLE abs_val(LDOUBLE value) static LDOUBLE abs_val(LDOUBLE value) {
{
LDOUBLE result = value; LDOUBLE result = value;
if (value < 0) if (value < 0)
@@ -275,8 +267,7 @@ static LDOUBLE abs_val(LDOUBLE value)
return result; return result;
} }
static LDOUBLE POW10(int exp) static LDOUBLE POW10(int exp) {
{
LDOUBLE result = 1; LDOUBLE result = 1;
while (exp) { while (exp) {
@@ -287,29 +278,29 @@ static LDOUBLE POW10(int exp)
return result; return result;
} }
static LLONG ROUND(LDOUBLE value) static LLONG ROUND(LDOUBLE value) {
{
LLONG intpart; LLONG intpart;
intpart = (LLONG)value; intpart = (LLONG) value;
value = value - intpart; value = value - intpart;
if (value >= 0.5) intpart++; if (value >= 0.5)
intpart++;
return intpart; return intpart;
} }
/* a replacement for modf that doesn't need the math library. Should /* a replacement for modf that doesn't need the math library. Should
be portable, but slow */ be portable, but slow */
static double my_modf(double x0, double *iptr) static double my_modf(double x0, double *iptr) {
{
int i; int i;
long l; long l;
double x = x0; double x = x0;
double f = 1.0; double f = 1.0;
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
l = (long)x; l = (long) x;
if (l <= (x+1) && l >= (x-1)) break; if (l <= (x + 1) && l >= (x - 1))
break;
x *= 0.1; x *= 0.1;
f *= 10.0; f *= 10.0;
} }
@@ -323,8 +314,8 @@ static double my_modf(double x0, double *iptr)
if (i != 0) { if (i != 0) {
double i2, ret; double i2, ret;
ret = my_modf(x0-l*f, &i2); ret = my_modf(x0 - l * f, &i2);
*iptr = l*f + i2; *iptr = l * f + i2;
return ret; return ret;
} }
@@ -332,17 +323,15 @@ static double my_modf(double x0, double *iptr)
return x - *iptr; return x - *iptr;
} }
static void fmtfp(char *buffer, size_t *currlen, size_t maxlen, LDOUBLE fvalue, int min, int max,
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, int flags) {
LDOUBLE fvalue, int min, int max, int flags) /* avoid warnings with 'gcc -Wshadow' */
{
/* avoid warnings with 'gcc -Wshadow' */
#undef index #undef index
#define index iindex #define index iindex
int signvalue = 0; int signvalue = 0;
double ufvalue; double ufvalue;
char iconvert[311+1]; char iconvert[311 + 1];
char fconvert[311+1]; char fconvert[311 + 1];
int iplace = 0; int iplace = 0;
int fplace = 0; int fplace = 0;
int padlen = 0; /* amount to pad */ int padlen = 0; /* amount to pad */
@@ -362,7 +351,7 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
if (max < 0) if (max < 0)
max = 6; max = 6;
ufvalue = abs_val (fvalue); ufvalue = abs_val(fvalue);
if (fvalue < 0) { if (fvalue < 0) {
signvalue = '-'; signvalue = '-';
@@ -408,34 +397,34 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
/* Convert integer part */ /* Convert integer part */
do { do {
temp = intpart; temp = intpart;
my_modf(intpart*0.1, &intpart); my_modf(intpart * 0.1, &intpart);
temp = temp*0.1; temp = temp * 0.1;
index = (int) ((temp - intpart + 0.05) * 10.0); index = (int) ((temp - intpart + 0.05) * 10.0);
/* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */ /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
/* printf ("%llf, %f, %x\n", temp, intpart, index); */ /* printf ("%llf, %f, %x\n", temp, intpart, index); */
iconvert[iplace] = digits[index]; iconvert[iplace] = digits[index];
} while (++iplace < (int)sizeof(iconvert) - 1 && intpart); } while (++iplace < (int) sizeof(iconvert) - 1 && intpart);
iconvert[iplace] = 0; iconvert[iplace] = 0;
/* Convert fractional part */ /* Convert fractional part */
if (fracpart) if (fracpart) {
{
do { do {
temp = fracpart; temp = fracpart;
my_modf(fracpart*0.1, &fracpart); my_modf(fracpart * 0.1, &fracpart);
temp = temp*0.1; temp = temp * 0.1;
index = (int) ((temp - fracpart + 0.05) * 10.0); index = (int) ((temp - fracpart + 0.05) * 10.0);
/* index = (int) ((((temp/10) -fracpart) +0.05) *10); */ /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
/* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */ /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
fconvert[fplace] = digits[index]; fconvert[fplace] = digits[index];
} while (++fplace < (int)sizeof(fconvert) - 1 && fracpart); } while (++fplace < (int) sizeof(fconvert) - 1 && fracpart);
} }
fconvert[fplace] = 0; fconvert[fplace] = 0;
/* -1 for decimal point, another -1 if we are printing a sign */ /* -1 for decimal point, another -1 if we are printing a sign */
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
zpadlen = max - fplace; zpadlen = max - fplace;
if (zpadlen < 0) zpadlen = 0; if (zpadlen < 0)
zpadlen = 0;
if (padlen < 0) if (padlen < 0)
padlen = 0; padlen = 0;
if (flags & DP_F_MINUS) if (flags & DP_F_MINUS)
@@ -443,24 +432,24 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
if ((flags & DP_F_ZERO) && (padlen > 0)) { if ((flags & DP_F_ZERO) && (padlen > 0)) {
if (signvalue) { if (signvalue) {
dopr_outch (buffer, currlen, maxlen, signvalue); dopr_outch(buffer, currlen, maxlen, signvalue);
--padlen; --padlen;
signvalue = 0; signvalue = 0;
} }
while (padlen > 0) { while (padlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0'); dopr_outch(buffer, currlen, maxlen, '0');
--padlen; --padlen;
} }
} }
while (padlen > 0) { while (padlen > 0) {
dopr_outch (buffer, currlen, maxlen, ' '); dopr_outch(buffer, currlen, maxlen, ' ');
--padlen; --padlen;
} }
if (signvalue) if (signvalue)
dopr_outch (buffer, currlen, maxlen, signvalue); dopr_outch(buffer, currlen, maxlen, signvalue);
while (iplace > 0) while (iplace > 0)
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
#ifdef DEBUG_SNPRINTF #ifdef DEBUG_SNPRINTF
printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen); printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
@@ -471,34 +460,31 @@ static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
* char to print out. * char to print out.
*/ */
if (max > 0) { if (max > 0) {
dopr_outch (buffer, currlen, maxlen, '.'); dopr_outch(buffer, currlen, maxlen, '.');
while (fplace > 0) while (fplace > 0)
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
} }
while (zpadlen > 0) { while (zpadlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0'); dopr_outch(buffer, currlen, maxlen, '0');
--zpadlen; --zpadlen;
} }
while (padlen < 0) { while (padlen < 0) {
dopr_outch (buffer, currlen, maxlen, ' '); dopr_outch(buffer, currlen, maxlen, ' ');
++padlen; ++padlen;
} }
#undef index #undef index
} }
#endif /* !(NO_FLOAT) */ #endif /* !(NO_FLOAT) */
/************************************************************************* /*************************************************************************
// dopr() // dopr()
**************************************************************************/ **************************************************************************/
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args) static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args) {
{
char ch; char ch;
LLONG value; LLONG value;
#if !(NO_FLOAT) #if !(NO_FLOAT)
@@ -521,12 +507,12 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
if (ch == '\0') if (ch == '\0')
state = DP_S_DONE; state = DP_S_DONE;
switch(state) { switch (state) {
case DP_S_DEFAULT: case DP_S_DEFAULT:
if (ch == '%') if (ch == '%')
state = DP_S_FLAGS; state = DP_S_FLAGS;
else else
dopr_outch (buffer, &currlen, maxlen, ch); dopr_outch(buffer, &currlen, maxlen, ch);
ch = *format++; ch = *format++;
break; break;
case DP_S_FLAGS: case DP_S_FLAGS:
@@ -557,11 +543,11 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
} }
break; break;
case DP_S_MIN: case DP_S_MIN:
if (isdigit((unsigned char)ch)) { if (isdigit((unsigned char) ch)) {
min = 10*min + char_to_int(ch); min = 10 * min + char_to_int(ch);
ch = *format++; ch = *format++;
} else if (ch == '*') { } else if (ch == '*') {
min = va_arg (args, int); min = va_arg(args, int);
ch = *format++; ch = *format++;
state = DP_S_DOT; state = DP_S_DOT;
} else { } else {
@@ -577,13 +563,13 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
} }
break; break;
case DP_S_MAX: case DP_S_MAX:
if (isdigit((unsigned char)ch)) { if (isdigit((unsigned char) ch)) {
if (max < 0) if (max < 0)
max = 0; max = 0;
max = 10*max + char_to_int(ch); max = 10 * max + char_to_int(ch);
ch = *format++; ch = *format++;
} else if (ch == '*') { } else if (ch == '*') {
max = va_arg (args, int); max = va_arg(args, int);
ch = *format++; ch = *format++;
state = DP_S_MOD; state = DP_S_MOD;
} else { } else {
@@ -599,7 +585,7 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
case 'l': case 'l':
cflags = DP_C_LONG; cflags = DP_C_LONG;
ch = *format++; ch = *format++;
if (ch == 'l') { /* It's a long long */ if (ch == 'l') { /* It's a long long */
cflags = DP_C_LLONG; cflags = DP_C_LLONG;
ch = *format++; ch = *format++;
} }
@@ -618,77 +604,77 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
case 'd': case 'd':
case 'i': case 'i':
if (cflags == DP_C_SHORT) if (cflags == DP_C_SHORT)
value = va_arg (args, int); value = va_arg(args, int);
else if (cflags == DP_C_LONG) else if (cflags == DP_C_LONG)
value = va_arg (args, long int); value = va_arg(args, long int);
else if (cflags == DP_C_LLONG) else if (cflags == DP_C_LLONG)
value = va_arg (args, LLONG); value = va_arg(args, LLONG);
else else
value = va_arg (args, int); value = va_arg(args, int);
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
break; break;
case 'o': case 'o':
flags |= DP_F_UNSIGNED; flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT) if (cflags == DP_C_SHORT)
value = va_arg (args, unsigned int); value = va_arg(args, unsigned int);
else if (cflags == DP_C_LONG) else if (cflags == DP_C_LONG)
value = (long)va_arg (args, unsigned long int); value = (long) va_arg(args, unsigned long int);
else if (cflags == DP_C_LLONG) else if (cflags == DP_C_LLONG)
value = (long)va_arg (args, ULLONG); value = (long) va_arg(args, ULLONG);
else else
value = (long)va_arg (args, unsigned int); value = (long) va_arg(args, unsigned int);
fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
break; break;
case 'u': case 'u':
flags |= DP_F_UNSIGNED; flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT) if (cflags == DP_C_SHORT)
value = va_arg (args, unsigned int); value = va_arg(args, unsigned int);
else if (cflags == DP_C_LONG) else if (cflags == DP_C_LONG)
value = (long)va_arg (args, unsigned long int); value = (long) va_arg(args, unsigned long int);
else if (cflags == DP_C_LLONG) else if (cflags == DP_C_LLONG)
value = (LLONG)va_arg (args, ULLONG); value = (LLONG) va_arg(args, ULLONG);
else else
value = (long)va_arg (args, unsigned int); value = (long) va_arg(args, unsigned int);
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
break; break;
case 'X': case 'X':
flags |= DP_F_UP; flags |= DP_F_UP;
case 'x': case 'x':
flags |= DP_F_UNSIGNED; flags |= DP_F_UNSIGNED;
if (cflags == DP_C_SHORT) if (cflags == DP_C_SHORT)
value = va_arg (args, unsigned int); value = va_arg(args, unsigned int);
else if (cflags == DP_C_LONG) else if (cflags == DP_C_LONG)
value = (long)va_arg (args, unsigned long int); value = (long) va_arg(args, unsigned long int);
else if (cflags == DP_C_LLONG) else if (cflags == DP_C_LLONG)
value = (LLONG)va_arg (args, ULLONG); value = (LLONG) va_arg(args, ULLONG);
else else
value = (long)va_arg (args, unsigned int); value = (long) va_arg(args, unsigned int);
fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
break; break;
#if !(NO_FLOAT) #if !(NO_FLOAT)
case 'f': case 'f':
if (cflags == DP_C_LDOUBLE) if (cflags == DP_C_LDOUBLE)
fvalue = va_arg (args, LDOUBLE); fvalue = va_arg(args, LDOUBLE);
else else
fvalue = va_arg (args, double); fvalue = va_arg(args, double);
/* um, floating point? */ /* um, floating point? */
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
break; break;
case 'E': case 'E':
flags |= DP_F_UP; flags |= DP_F_UP;
case 'e': case 'e':
if (cflags == DP_C_LDOUBLE) if (cflags == DP_C_LDOUBLE)
fvalue = va_arg (args, LDOUBLE); fvalue = va_arg(args, LDOUBLE);
else else
fvalue = va_arg (args, double); fvalue = va_arg(args, double);
break; break;
case 'G': case 'G':
flags |= DP_F_UP; flags |= DP_F_UP;
case 'g': case 'g':
if (cflags == DP_C_LDOUBLE) if (cflags == DP_C_LDOUBLE)
fvalue = va_arg (args, LDOUBLE); fvalue = va_arg(args, LDOUBLE);
else else
fvalue = va_arg (args, double); fvalue = va_arg(args, double);
break; break;
#else #else
case 'f': case 'f':
@@ -700,42 +686,45 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
exit(255); exit(255);
#endif /* !(NO_FLOAT) */ #endif /* !(NO_FLOAT) */
case 'c': case 'c':
dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
break; break;
case 's': case 's':
strvalue = va_arg (args, const char *); strvalue = va_arg(args, const char *);
if (!strvalue) strvalue = "(NULL)"; if (!strvalue)
strvalue = "(NULL)";
if (max == -1) { if (max == -1) {
max = (int) strlen(strvalue); max = (int) strlen(strvalue);
} }
if (min > 0 && max >= 0 && min > max) max = min; if (min > 0 && max >= 0 && min > max)
fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); max = min;
fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
break; break;
case 'p': case 'p':
strvalue = (const char *) va_arg (args, const void *); strvalue = (const char *) va_arg(args, const void *);
fmtint (buffer, &currlen, maxlen, (LLONG) (upx_uintptr_t) strvalue, 16, min, max, flags); fmtint(buffer, &currlen, maxlen, (LLONG)(upx_uintptr_t) strvalue, 16, min, max,
flags);
break; break;
case 'n': case 'n':
if (cflags == DP_C_SHORT) { if (cflags == DP_C_SHORT) {
short int *num; short int *num;
num = va_arg (args, short int *); num = va_arg(args, short int *);
*num = (short int)currlen; *num = (short int) currlen;
} else if (cflags == DP_C_LONG) { } else if (cflags == DP_C_LONG) {
long int *num; long int *num;
num = va_arg (args, long int *); num = va_arg(args, long int *);
*num = (long int)currlen; *num = (long int) currlen;
} else if (cflags == DP_C_LLONG) { } else if (cflags == DP_C_LLONG) {
LLONG *num; LLONG *num;
num = va_arg (args, LLONG *); num = va_arg(args, LLONG *);
*num = (LLONG)currlen; *num = (LLONG) currlen;
} else { } else {
int *num; int *num;
num = va_arg (args, int *); num = va_arg(args, int *);
*num = (int) currlen; *num = (int) currlen;
} }
break; break;
case '%': case '%':
dopr_outch (buffer, &currlen, maxlen, ch); dopr_outch(buffer, &currlen, maxlen, ch);
break; break;
case 'w': case 'w':
/* not supported yet, treat as next char */ /* not supported yet, treat as next char */
@@ -767,29 +756,26 @@ static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args
return currlen; return currlen;
} }
/************************************************************************* /*************************************************************************
// public entries // public entries
**************************************************************************/ **************************************************************************/
// UPX version with assertions // UPX version with assertions
static int xdopr(char *buffer, size_t maxlen, const char *format, va_list args) static int xdopr(char *buffer, size_t maxlen, const char *format, va_list args) {
{
size_t ret; size_t ret;
// preconditions // preconditions
assert(maxlen < INT_MAX); assert(maxlen < INT_MAX);
if (buffer != NULL) if (buffer != NULL)
assert((int)maxlen > 0); assert((int) maxlen > 0);
else else
assert(maxlen == 0); assert(maxlen == 0);
ret = dopr(buffer, maxlen, format, args); ret = dopr(buffer, maxlen, format, args);
// postconditions // postconditions
if (buffer != NULL) if (buffer != NULL) {
{ assert((int) ret >= 0);
assert((int)ret >= 0);
assert(ret < maxlen); assert(ret < maxlen);
assert(buffer[ret] == '\0'); assert(buffer[ret] == '\0');
} }
@@ -797,15 +783,11 @@ static int xdopr(char *buffer, size_t maxlen, const char *format, va_list args)
return (int) ret; return (int) ret;
} }
int upx_vsnprintf(char *str, size_t count, const char *format, va_list ap) {
int upx_vsnprintf(char *str, size_t count, const char *format, va_list ap)
{
return xdopr(str, count, format, ap); return xdopr(str, count, format, ap);
} }
int __acc_cdecl_va upx_snprintf(char *str, size_t count, const char *format, ...) {
int __acc_cdecl_va upx_snprintf(char *str, size_t count, const char *format,...)
{
va_list ap; va_list ap;
int ret; int ret;
@@ -815,28 +797,23 @@ int __acc_cdecl_va upx_snprintf(char *str, size_t count, const char *format,...)
return ret; return ret;
} }
int upx_vasprintf(char **ptr, const char *format, va_list ap) {
int upx_vasprintf(char **ptr, const char *format, va_list ap)
{
int ret; int ret;
assert(ptr != NULL); assert(ptr != NULL);
*ptr = NULL; *ptr = NULL;
ret = xdopr(NULL, 0, format, ap); ret = xdopr(NULL, 0, format, ap);
if (ret > 0) if (ret > 0) {
{
*ptr = (char *) malloc(ret + 1); *ptr = (char *) malloc(ret + 1);
assert(*ptr != NULL); assert(*ptr != NULL);
if (*ptr == NULL) if (*ptr == NULL)
return -1; return -1;
ret = xdopr(*ptr, ret+1, format, ap); ret = xdopr(*ptr, ret + 1, format, ap);
} }
return ret; return ret;
} }
int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...) {
int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...)
{
va_list ap; va_list ap;
int ret; int ret;
@@ -846,7 +823,6 @@ int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...)
return ret; return ret;
} }
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
@@ -859,7 +835,7 @@ int __acc_cdecl_va upx_asprintf(char **ptr, const char *format, ...)
#include <math.h> #include <math.h>
#undef snprintf #undef snprintf
#define snprintf upx_snprintf #define snprintf upx_snprintf
//int sprintf(char *str,const char *fmt,...); //int sprintf(char *str,const char *fmt,...);
int main(void) int main(void)
@@ -998,4 +974,3 @@ int main(void)
/* /*
vi:ts=4:et vi:ts=4:et
*/ */
+11 -25
View File
@@ -1,28 +1,14 @@
# you should set envvar UPX_LZMADIR to point to your unpacked LZMA SDK lzma443.tar.bz2 # UPX unconditionally uses its own version in src/lzma-sdk because
# NOTE: only version 4.43 is currently sufficiently tested ! # that version works fine since 2006 and that is the only version
# that is actually sufficiently tested!!!
ifndef UPX_LZMADIR override UPX_LZMADIR := $(top_srcdir)/src/lzma-sdk
ifdef top_srcdir override UPX_LZMA_VERSION := 0x443
ifneq ($(wildcard $(top_srcdir)/src/stub/src/c/lzma-sdk/C/.),)
UPX_LZMADIR := $(top_srcdir)/src/stub/src/c/lzma-sdk
else ifneq ($(wildcard $(top_srcdir)/src/lzma-sdk/C/.),)
UPX_LZMADIR := $(top_srcdir)/src/lzma-sdk
endif
endif
endif
ifneq ($(wildcard $(UPX_LZMADIR)/C/7zVersion.h),) ifeq ($(wildcard $(UPX_LZMADIR)/C/7zip/.),)
UPX_LZMA_VERSION := $(shell perl -ne 'print "0x$$1$$2" if /^\s*\#\s*define\s+MY_VERSION\s+"(\d*)\.(\d\d)[\s\w]*"/;' $(UPX_LZMADIR)/C/7zVersion.h) ifneq ($(wildcard $(top_srcdir)/.git/.),)
else ifneq ($(wildcard $(UPX_LZMADIR)/CPP/Windows/Time.cpp),) $(error ERROR: missing git submodule; run 'git submodule update --init --recursive')
UPX_LZMA_VERSION = 0x459 else
else ifneq ($(wildcard $(UPX_LZMADIR)/C/7zBuf.h),) $(error ERROR: missing directory src/lzma-sdk/; visit https://github.com/upx/upx-lzma-sdk )
UPX_LZMA_VERSION = 0x458 endif
else ifneq ($(wildcard $(UPX_LZMADIR)/C/CpuArch.h),)
UPX_LZMA_VERSION = 0x457
else ifneq ($(wildcard $(UPX_LZMADIR)/CPP/7zip/.),)
UPX_LZMA_VERSION = 0x449
else ifneq ($(wildcard $(UPX_LZMADIR)/C/7zip/Compress/LZ/Patricia/.),)
UPX_LZMA_VERSION = 0x442
else ifneq ($(wildcard $(UPX_LZMADIR)/C/7zip/.),)
UPX_LZMA_VERSION = 0x443
endif endif
+2 -2
View File
@@ -83,7 +83,7 @@ void do_one_file(const char *iname, char *oname)
throwIOException("empty file -- skipped"); throwIOException("empty file -- skipped");
if (st.st_size < 512) if (st.st_size < 512)
throwIOException("file is too small -- skipped"); throwIOException("file is too small -- skipped");
if (st.st_size >= 1024*1024*1024) if (!mem_size_valid(1, st.st_size))
throwIOException("file is too large -- skipped"); throwIOException("file is too large -- skipped");
if ((st.st_mode & S_IWUSR) == 0) if ((st.st_mode & S_IWUSR) == 0)
{ {
@@ -298,7 +298,7 @@ void do_files(int i, int argc, char *argv[])
unlink_ofile(oname); unlink_ofile(oname);
if (opt->verbose >= 1 || (opt->verbose >= 0 && !e.isWarning())) if (opt->verbose >= 1 || (opt->verbose >= 0 && !e.isWarning()))
printErr(iname,&e); printErr(iname,&e);
set_ec(e.isWarning() ? EXIT_WARN : EXIT_ERROR); set_exit_code(e.isWarning() ? EXIT_WARN : EXIT_ERROR);
} catch (const Error &e) { } catch (const Error &e) {
unlink_ofile(oname); unlink_ofile(oname);
printErr(iname,&e); printErr(iname,&e);