aboutsummaryrefslogtreecommitdiff
path: root/batch/old/genShellcode.sh
blob: bf0327a740f608033357111b4fd193225a864203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash

set -e

OBJDUMP="$(dirname $0)/../deps/sysroot/bin/i686-w64-mingw32-objdump"
OBJDUMP_ARGS="-z -D"
TMPFILE="$(mktemp)"


if [ ! -x ${OBJDUMP} ]; then
	echo "$0: ${OBJDUMP} not found!"
	false
fi

if [ "x$1" != "x" -a "x$2" != "x" -a "x$3" != "x" -a "x$4" != "x" ]; then
	echo "$0: create tmpfile ${TMPFILE}"
	OBJECTFILE="${1}"
	OUTPUT="${2}"
	DEFINE="${3}"
        OBJDUMP_ARGS="${OBJDUMP_ARGS} -j ${4}"

	DO_APPEND=0
	if [ "x$5" != "x" ]; then
		echo "$5" | egrep -qi 'append.*=.*true' && DO_APPEND=1 || true
	fi

	if [ ! -r ${OBJECTFILE} ]; then
		echo "$0: ${OBJECTFILE} not found or not readable"
		false
	fi

	echo "$0: objdump command: \`${OBJDUMP} ${OBJDUMP_ARGS} ${OBJECTFILE}\`"
	export SIZE=0
	if [ ${DO_APPEND} -eq 1 ]; then
		echo "$0: APPENDING to ${OUTPUT}"
		cp ${OUTPUT} ${TMPFILE}
		echo >> ${TMPFILE}
		echo '#undef '"${DEFINE}" >> ${TMPFILE}
	else
		echo '#undef '"${DEFINE}" > ${TMPFILE}
	fi
	echo -n '#define '"${DEFINE}"' "' >> ${TMPFILE}
        # TODO: use objdump -s to show everything (-d shows only valid opcodes)
	for i in $(${OBJDUMP} ${OBJDUMP_ARGS} ${OBJECTFILE} |grep "^ " |cut -f2); do
		echo -n '\x'$i >>${TMPFILE}
		SIZE=$(expr $SIZE + 1)
	done
	if [ $SIZE -eq 0 ]; then
		echo "$0: Whoops! Something went wrong (SIZE=0)."
		echo "$0: Check output manually with: \`${OBJDUMP} ${OBJDUMP_ARGS} ${OBJECTFILE}\`"
		false
	fi
	echo '"' >>${TMPFILE}
	echo '#undef '"${DEFINE}"'_SIZE' >> ${TMPFILE}
	echo '#define '"${DEFINE}"'_SIZE '"${SIZE}" >> ${TMPFILE}
	mv ${TMPFILE} ${OUTPUT}
	echo "$0: moved ${TMPFILE} to ${OUTPUT}"
else
	echo "usage: $0 [OBJECT-FILE or STATIC-LIB] [OUTPUT-HEADER] [OUTPUT-DEFINE] [LOADER-SECTION] [DO-APPEND=[TRUE|FALSE]]"
	exit 1
fi