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

Improve xstrip.py.

This commit is contained in:
Markus F.X.J. Oberhumer 2006-11-25 15:36:56 +01:00
parent 92ec1b0598
commit cbb5902066
2 changed files with 66 additions and 9 deletions

View File

@ -192,7 +192,7 @@ define tc.default.f-embed_objinfo
$(call tc,objcopy) -R .comment -R .note -R .note.GNU-stack -R .reginfo $1
$(call tc,objdump) -Dr $(tc_objdump_disasm_options) $1 | $(RTRIM) > $1.disasm
$(call tc,objdump) -htr -w $1 | $(BLSQUEEZE) > $1.dump
$(call tc,xstrip) $1
$(call tc,xstrip) --dump=$1.dump $1
cat $1.dump >> $1
endef
define tc.default.f-objstrip
@ -764,7 +764,9 @@ tmp/powerpc-linux.elf-main.o : $(srcdir)/src/$$T.c
ifneq ($(strip $(STUBS)),)
# FIXME: we want a dependency-only prerequisite here
$(STUBS): tmp/.tmp-stamp $(MAKEFILE_LIST) $(top_srcdir)/src/stub/scripts/bin2h.py
$(STUBS): tmp/.tmp-stamp $(MAKEFILE_LIST)
$(STUBS): $(top_srcdir)/src/stub/scripts/bin2h.py
$(STUBS): $(top_srcdir)/src/stub/scripts/xstrip.py
endif
-include tmp/*.d

View File

@ -28,14 +28,38 @@
#
import getopt, os, re, sys
import getopt, os, re, string, struct, sys
class opts:
dry_run = 0
dump = None
verbose = 0
# /***********************************************************************
# //
# ************************************************************************/
def strip_with_dump(eh, idata):
new_len = 0
lines = open(opts.dump, "rb").readlines()
for l in lines:
l = re.sub(r"\s+", " ", l.strip())
f = l.split(" ")
if len(f) >= 8:
if f[7].startswith("CONTENTS"):
sh_offset = int("0x" + f[5], 16)
sh_size = int("0x" + f[2], 16)
if sh_offset + sh_size > new_len:
new_len = sh_offset + sh_size
##print sh_offset, sh_size, f
if new_len > len(eh):
##print opts.dump, new_len
return eh, idata[:new_len-len(eh)]
return eh, idata
# /***********************************************************************
# //
# ************************************************************************/
@ -51,18 +75,48 @@ def do_file(fn):
fp.seek(0, 0)
if idata[:4] != "\x7f\x45\x4c\x46":
raise Exception, "%s is not %s" % (fn, "ELF")
pos = idata.find("\0.symtab\0.strtab\0.shstrtab\0")
if pos >= 0:
odata = idata[:pos]
if odata and not opts.dry_run:
if idata[4:7] == "\x01\x01\x01":
# ELF32 LE
eh, idata = idata[:52], idata[52:]
e_shnum, e_shstrndx = struct.unpack("<HH", eh[48:52])
assert e_shstrndx + 3 == e_shnum
##eh = eh[:48] + struct.pack("<HH", e_shnum - 3, e_shstrndx)
elif idata[4:7] == "\x01\x02\x01":
# ELF32 BE
eh, idata = idata[:52], idata[52:]
e_shnum, e_shstrndx = struct.unpack(">HH", eh[48:52])
assert e_shstrndx + 3 == e_shnum
elif idata[4:7] == "\x02\x01\x01":
# ELF64 LE
eh, idata = idata[:64], idata[64:]
e_shnum, e_shstrndx = struct.unpack("<HH", eh[60:64])
assert e_shstrndx + 3 == e_shnum
elif idata[4:7] == "\x02\x02\x01":
# ELF64 BE
eh, idata = idata[:64], idata[64:]
e_shnum, e_shstrndx = struct.unpack(">HH", eh[60:64])
assert e_shstrndx + 3 == e_shnum
else:
raise Exception, "%s is not %s" % (fn, "ELF")
odata = None
if opts.dump:
eh, odata = strip_with_dump(eh, idata)
else:
pos = idata.find("\0.symtab\0.strtab\0.shstrtab\0")
if pos >= 0:
odata = idata[:pos]
if eh and odata and not opts.dry_run:
fp.write(eh)
fp.write(odata)
fp.truncate(len(odata))
fp.truncate()
fp.close()
def main(argv):
shortopts, longopts = "qv", [
"dry-run", "quiet", "verbose"
"dry-run", "dump=", "quiet", "verbose"
]
xopts, args = getopt.gnu_getopt(argv[1:], shortopts, longopts)
for opt, optarg in xopts:
@ -70,6 +124,7 @@ def main(argv):
elif opt in ["-q", "--quiet"]: opts.verbose = opts.verbose - 1
elif opt in ["-v", "--verbose"]: opts.verbose = opts.verbose + 1
elif opt in ["--dry-run"]: opts.dry_run = opts.dry_run + 1
elif opt in ["--dump"]: opts.dump = optarg
else: assert 0, ("getopt problem:", opt, optarg, xopts, args)
# process arguments
if not args: