Add support for UPX_VERSION_GITREV. Some cleanups.

This commit is contained in:
Markus F.X.J. Oberhumer 2016-09-25 09:41:26 +02:00
parent 3c786acf2b
commit daafc29d2b
8 changed files with 62 additions and 54 deletions

View File

@ -46,6 +46,13 @@ objext ?= .o
upx_SOURCES := $(sort $(wildcard $(srcdir)/*.cpp)) upx_SOURCES := $(sort $(wildcard $(srcdir)/*.cpp))
upx_OBJECTS := $(notdir $(upx_SOURCES:.cpp=$(objext))) upx_OBJECTS := $(notdir $(upx_SOURCES:.cpp=$(objext)))
ifneq ($(wildcard $(top_srcdir)/.git/.),)
UPX_VERSION_GITREV := $(strip $(shell cd '$(top_srcdir)' && git rev-parse --short=6 HEAD || echo ""))
ifneq ($(UPX_VERSION_GITREV),)
DEFS += '-DUPX_VERSION_GITREV="$(UPX_VERSION_GITREV)"'
endif
endif
# we need UCL and zlib - you can set envvar UPX_UCLDIR # we need UCL and zlib - you can set envvar UPX_UCLDIR
ifneq ($(wildcard $(UPX_UCLDIR)/include/ucl/ucl.h),) ifneq ($(wildcard $(UPX_UCLDIR)/include/ucl/ucl.h),)
INCLUDES += -I$(UPX_UCLDIR)/include INCLUDES += -I$(UPX_UCLDIR)/include
@ -126,7 +133,7 @@ endif
CLANG_FORMAT_FILES += packhead.cpp CLANG_FORMAT_FILES += packhead.cpp
CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h
CLANG_FORMAT_FILES += snprintf.cpp CLANG_FORMAT_FILES += snprintf.cpp
CLANG_FORMAT_FILES += stdcxx.h stdcxx.cpp CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h
CLANG_FORMAT_FILES += ui.cpp ui.h util.h CLANG_FORMAT_FILES += ui.cpp ui.h util.h
clang-format: clang-format:
$(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES)) $(top_srcdir)/src/stub/scripts/upx-clang-format -i $(addprefix $(top_srcdir)/src/,$(CLANG_FORMAT_FILES))

View File

@ -285,6 +285,10 @@ int upx_ucl_init(void)
{ {
if (ucl_init() != UCL_E_OK) if (ucl_init() != UCL_E_OK)
return -1; return -1;
#if defined(UPX_OFFICIAL_BUILD)
if (UCL_VERSION != ucl_version())
return -2;
#endif
return 0; return 0;
} }

View File

@ -51,15 +51,14 @@ void show_head(void)
fg = con_fg(f,FG_GREEN); fg = con_fg(f,FG_GREEN);
con_fprintf(f, con_fprintf(f,
" Ultimate Packer for eXecutables\n" " Ultimate Packer for eXecutables\n"
" Copyright (C) 1996 - %s\n" " Copyright (C) 1996 - " UPX_VERSION_YEAR "\n"
"UPX %-10s Markus Oberhumer, Laszlo Molnar & John Reiser %14s\n\n", "UPX %-11s Markus Oberhumer, Laszlo Molnar & John Reiser %14s\n\n",
UPX_VERSION_YEAR, #if defined(UPX_VERSION_GITREV)
#if (ACC_OS_DOS16 || ACC_OS_DOS32) "git-" UPX_VERSION_GITREV,
#elif (ACC_OS_DOS16 || ACC_OS_DOS32)
V("d"), V("d"),
#elif (ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64) #elif (ACC_OS_WIN16 || ACC_OS_WIN32 || ACC_OS_WIN64)
V("w"), V("w"),
#elif 0 && defined(__linux__)
V("l"),
#else #else
UPX_VERSION_STRING, UPX_VERSION_STRING,
#endif #endif
@ -136,7 +135,7 @@ static void show_all_packers(FILE *f, int verbose)
{ {
const char *fn = pn.names[i].fname; const char *fn = pn.names[i].fname;
const char *sn = pn.names[i].sname; const char *sn = pn.names[i].sname;
if (verbose) if (verbose > 0)
{ {
con_fprintf(f, " %-36s %s\n", fn, sn); con_fprintf(f, " %-36s %s\n", fn, sn);
} }
@ -155,7 +154,7 @@ static void show_all_packers(FILE *f, int verbose)
} }
} }
} }
if (!verbose && pn.names_count) if (verbose <= 0 && pn.names_count)
con_fprintf(f, "\n"); con_fprintf(f, "\n");
} }

View File

@ -1285,10 +1285,6 @@ static bool test(void)
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
#if 1 && (ACC_CC_WATCOMC)
test1_t t11; COMPILE_TIME_ASSERT(sizeof(t11.a) <= sizeof(t11.b))
test2_t t22; COMPILE_TIME_ASSERT(sizeof(t22.a) <= sizeof(t22.b))
#endif
#if 1 && !defined(UPX_OFFICIAL_BUILD) #if 1 && !defined(UPX_OFFICIAL_BUILD)
T allbits; allbits = 0; allbits -= 1; T allbits; allbits = 0; allbits -= 1;
//++allbits; allbits++; --allbits; allbits--; //++allbits; allbits++; --allbits; allbits--;
@ -1356,6 +1352,10 @@ __acc_static_noinline void upx_sanity_check(void)
assert(strlen(UPX_VERSION_YEAR) == 4); assert(strlen(UPX_VERSION_YEAR) == 4);
assert(memcmp(UPX_VERSION_DATE_ISO, UPX_VERSION_YEAR, 4) == 0); assert(memcmp(UPX_VERSION_DATE_ISO, UPX_VERSION_YEAR, 4) == 0);
assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE)-1 - 4], UPX_VERSION_YEAR, 4) == 0); assert(memcmp(&UPX_VERSION_DATE[sizeof(UPX_VERSION_DATE)-1 - 4], UPX_VERSION_YEAR, 4) == 0);
#if defined(UPX_VERSION_GITREV)
assert(strlen(UPX_VERSION_GITREV) >= 5);
assert(strlen(UPX_VERSION_GITREV) <= 7);
#endif
#if 1 #if 1
assert(TestBELE<LE16>::test()); assert(TestBELE<LE16>::test());
@ -1552,7 +1552,7 @@ int __acc_cdecl_main main(int argc, char *argv[])
set_term(stdout); set_term(stdout);
do_files(i,argc,argv); do_files(i,argc,argv);
#if 0 && (UPX_VERSION_HEX < 0x030000) #if 1 && defined(UPX_VERSION_GITREV)
{ {
FILE *f = stdout; FILE *f = stdout;
int fg = con_fg(f,FG_RED); int fg = con_fg(f,FG_RED);

View File

@ -90,40 +90,37 @@ int ptr_diff(const char *p1, const char *p2)
/************************************************************************* /*************************************************************************
// // bool use_mcheck()
**************************************************************************/ **************************************************************************/
static int use_mcheck = -1; #if defined(__SANITIZE_ADDRESS__)
__acc_static_forceinline bool use_mcheck() { return false; }
static int mcheck_init() #elif (WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND)
{ static int use_mcheck_flag = -1;
if (use_mcheck < 0) __acc_static_noinline void use_mcheck_init()
{
use_mcheck = 1;
#if (WITH_VALGRIND) && defined(RUNNING_ON_VALGRIND)
if (RUNNING_ON_VALGRIND)
{ {
use_mcheck_flag = 1;
if (RUNNING_ON_VALGRIND) {
use_mcheck_flag = 0;
//fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n"); //fprintf(stderr, "upx: detected RUNNING_ON_VALGRIND\n");
use_mcheck = 0;
} }
}
__acc_static_forceinline bool use_mcheck()
{
if __acc_unlikely(use_mcheck_flag < 0)
use_mcheck_init();
return (bool) use_mcheck_flag;
}
#else
__acc_static_forceinline bool use_mcheck() { return true; }
#endif #endif
}
return use_mcheck;
}
/************************************************************************* /*************************************************************************
// //
**************************************************************************/ **************************************************************************/
MemBuffer::MemBuffer() : MemBuffer::MemBuffer(upx_uint64_t size) :
b(NULL), b_size(0)
{
if (use_mcheck < 0)
mcheck_init();
}
MemBuffer::MemBuffer(unsigned size) :
b(NULL), b_size(0) b(NULL), b_size(0)
{ {
alloc(size); alloc(size);
@ -137,10 +134,10 @@ MemBuffer::~MemBuffer()
void MemBuffer::dealloc() void MemBuffer::dealloc()
{ {
if (b) if (b != NULL)
{ {
checkState(); checkState();
if (use_mcheck) if (use_mcheck())
{ {
// remove magic constants // remove magic constants
set_be32(b - 8, 0); set_be32(b - 8, 0);
@ -220,7 +217,7 @@ void MemBuffer::checkState() const
{ {
if (!b) if (!b)
throwInternalError("block not allocated"); throwInternalError("block not allocated");
if (use_mcheck) if (use_mcheck())
{ {
if (get_be32(b - 4) != MAGIC1(b)) if (get_be32(b - 4) != MAGIC1(b))
throwInternalError("memory clobbered before allocated block 1"); throwInternalError("memory clobbered before allocated block 1");
@ -233,22 +230,19 @@ void MemBuffer::checkState() const
} }
void MemBuffer::alloc(unsigned size) void MemBuffer::alloc(upx_uint64_t size)
{ {
if (use_mcheck < 0)
mcheck_init();
// NOTE: we don't automatically free a used buffer // NOTE: we don't automatically free a used buffer
assert(b == NULL); assert(b == NULL);
assert(b_size == 0); assert(b_size == 0);
// //
assert(size > 0); assert(size > 0);
size_t bytes = mem_size(1, size, use_mcheck ? 32 : 0); size_t bytes = mem_size(1, size, use_mcheck() ? 32 : 0);
unsigned char *p = (unsigned char *) malloc(bytes); unsigned char *p = (unsigned char *) malloc(bytes);
if (!p) if (!p)
throwOutOfMemoryException(); throwOutOfMemoryException();
b_size = size; b_size = ACC_ICONV(unsigned, size);
if (use_mcheck) if (use_mcheck())
{ {
b = p + 16; b = p + 16;
// store magic constants to detect buffer overruns // store magic constants to detect buffer overruns

View File

@ -37,14 +37,14 @@
class MemBuffer class MemBuffer
{ {
public: public:
MemBuffer(); MemBuffer() : b(NULL), b_size(0) { }
explicit MemBuffer(unsigned size); explicit MemBuffer(upx_uint64_t size);
~MemBuffer(); ~MemBuffer();
static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra=0); static unsigned getSizeForCompression(unsigned uncompressed_size, unsigned extra=0);
static unsigned getSizeForUncompression(unsigned uncompressed_size, unsigned extra=0); static unsigned getSizeForUncompression(unsigned uncompressed_size, unsigned extra=0);
void alloc(unsigned size); void alloc(upx_uint64_t size);
void allocForCompression(unsigned uncompressed_size, unsigned extra=0); void allocForCompression(unsigned uncompressed_size, unsigned extra=0);
void allocForUncompression(unsigned uncompressed_size, unsigned extra=0); void allocForUncompression(unsigned uncompressed_size, unsigned extra=0);

View File

@ -461,8 +461,9 @@ class PackBSDElf32x86 : public PackLinuxElf32x86
public: public:
PackBSDElf32x86(InputFile *f); PackBSDElf32x86(InputFile *f);
virtual ~PackBSDElf32x86(); virtual ~PackBSDElf32x86();
virtual int getFormat() const { return UPX_F_BSD_ELF_i386; } virtual int getFormat() const = 0;
virtual const char *getName() const { return "bsd/i386"; } virtual const char *getName() const = 0;
virtual const char *getFullName(const options_t *) const = 0;
protected: protected:
virtual void pack1(OutputFile *, Filter &); // generate executable header virtual void pack1(OutputFile *, Filter &); // generate executable header
@ -476,6 +477,8 @@ class PackFreeBSDElf32x86 : public PackBSDElf32x86
public: public:
PackFreeBSDElf32x86(InputFile *f); PackFreeBSDElf32x86(InputFile *f);
virtual ~PackFreeBSDElf32x86(); virtual ~PackFreeBSDElf32x86();
virtual int getFormat() const { return UPX_F_BSD_ELF_i386; }
virtual const char *getName() const { return "freebsd/i386"; }
virtual const char *getFullName(const options_t *) const { return "i386-freebsd.elf"; } virtual const char *getFullName(const options_t *) const { return "i386-freebsd.elf"; }
}; };
@ -503,6 +506,7 @@ class PackOpenBSDElf32x86 : public PackBSDElf32x86
public: public:
PackOpenBSDElf32x86(InputFile *f); PackOpenBSDElf32x86(InputFile *f);
virtual ~PackOpenBSDElf32x86(); virtual ~PackOpenBSDElf32x86();
virtual int getFormat() const { return UPX_F_BSD_ELF_i386; }
virtual const char *getName() const { return "openbsd/i386"; } virtual const char *getName() const { return "openbsd/i386"; }
virtual const char *getFullName(const options_t *) const { return "i386-openbsd.elf"; } virtual const char *getFullName(const options_t *) const { return "i386-openbsd.elf"; }

View File

@ -2007,7 +2007,7 @@ int PackMachBase<T>::canUnpack()
fi->seek(offLINK - bufsize, SEEK_SET); fi->seek(offLINK - bufsize, SEEK_SET);
} else } else
if (392 == style) { // PackHeader follows loader at __LINKEDIT if (392 == style) { // PackHeader follows loader at __LINKEDIT
if (bufsize > (fi->st_size() - offLINK)) { if ((off_t)bufsize > (fi->st_size() - offLINK)) {
bufsize = fi->st_size() - offLINK; bufsize = fi->st_size() - offLINK;
} }
fi->seek(offLINK, SEEK_SET); fi->seek(offLINK, SEEK_SET);