File: rarc2-tool

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (91 lines) | stat: -rwxr-xr-x 1,876 bytes parent folder | download | duplicates (3)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
# radare2 -- 2010-2011 -- pancake / nopcode.org
# TODO: execute in this way? rasc -s $bytes -X

[ -z "${ARCH}" ] && ARCH=`rarc2 -A`
[ -z "${CC}" ] && CC=gcc
RF="-s"

compile() {
	spp -h >/dev/null 2>&1
	if [ $? = 0 ]; then
		spp -Darch=${ARCH} $@ | rarc2 $RF -a${ARCH} > .a.S || exit $?
	else
		rarc2 $RF -a${ARCH} $@ > .a.S || exit $?
	fi
}

help() {
	cat << EOF
Usage: rarc2-tool [-flag] [file]
   -b       dump bytes
   -n       use nasm instead of gas
   -x       execute
   -c [elf,mach0,pe] - select output format (-r is implicit)
   -r       use intel syntax and rasm2 to display hexpairs
   -c       compile against libc
   -S       only generate .S file
ARCH: environ to set architecture: arm, x86, x64
EOF
	exit 1
}

while getopts rnbSxc: o ; do
	[ "$o" = "?" ] && help
	case "$o" in
	c) r=1; c="$OPTARG" ;;
	*) eval $o=1 ;;
	esac
done

shift $((${OPTIND}-1))

if [ -n "`echo $@`" ]; then
	if [ -n "$r" ]; then
		RF=""
		ARCH=x86
		n=1
	elif [ -n "$n" ]; then
		RF=""
	fi
	compile $@
	if [ -n "$c" ]; then
		# rasm2 and rabin2
		rasm2 -a x86.olly -f .a.S > .a.x
		rabin2 -a x86_32 -c $c:$(cat .a.x) $1.out
	elif [ -n "$r" ]; then
		rasm2 -a x86.olly -f .a.S
	elif [ -n "$n" ]; then
		# nasm
		cat .a.S | grep -v '^#' | sed -e 's, ptr , ,g' | \
			grep -v 'intel_syntax' | \
			sed -e 's|^\.||' | \
			awk '{if (/^equ /){gsub(","," ");/(.*) (.*) (.*)/;print $2" equ "$3;}else {print}}' | \
			sed -e 's|lea eax, dword|lea eax, |g' > .a.S2
		#echo 'section .text' > .a.S
		mv .a.S2 .a.S
		nasm -f elf .a.S
		ld -e main .a.o -o .a.out
	else
		# gcc (default)
		if [ -n "$c" ]; then
			${CC} .a.S -o .a.out
		else
			${CC} -nostdlib .a.S -o .a.out
		fi
	fi
	if [ -e .a.out ]; then
		if [ -n "$b" ]; then
			rabin2 -O d/S/.text .a.out
		else
			if [ -n "$x" ]; then
				./.a.out
			else
				cp .a.out $1.out
			fi
		fi
	fi
	rm -f .a.S .a.x .a.out
else
	help
fi