Files
revng-revng/translate
T
Alessandro Di Federico 83ea2caacd Isolate ELF code and remove architecture parameter
This commit removes all the ELF-specific code from the `CodeGenerator`
class by creating a new class, `BinaryFile` which contains all the
information about the program that might be needed in an image format
independent way. However, `BinaryFile` has some fields which are
specific to ELF, we might want to address this when additional file
formats are supported.

A key benefit of isolating this code is that we can anticipate the
parsing of the input file, so that we have its architecture available
earlier than when `CodeGenerator` is instantiated, therefore we can drop
the `--architecture` parameter.
2016-12-03 08:46:12 +01:00

125 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
#
# This file is distributed under the MIT License. See LICENSE.md for details.
#
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INPUT=""
OPTIMIZE=0
SKIP=0
set -e
while [[ $# > 0 ]]
do
key="$1"
case $key in
-O0)
OPTIMIZE="0"
shift
;;
-O1)
OPTIMIZE="1"
shift # past argument
;;
-O2)
OPTIMIZE="2"
shift # past argument
;;
-s)
SKIP="1"
shift # past argument
;;
--)
shift
break
;;
*)
# unknown option
if [ -z "$INPUT" ]; then
INPUT="$key"
else
break;
fi
shift
;;
esac
done
# Output file names
LL="$INPUT.ll"
REVAMB_LOG="$LL.log"
LL_OPT="$INPUT.opt.ll"
CSV="$LL.li.csv"
OBJ="$LL.o"
# Required programs
export PATH="$SCRIPT_PATH:$PATH"
CC="${CC:-cc}"
LLC="llc"
OPT="opt"
REVAMB="revamb"
TOOPT="li-csv-to-ld-options"
SUPPORTC="$SCRIPT_PATH/../share/revamb/support.c"
if [ '!' -e "$SUPPORTC" ]; then
SUPPORTC="$SCRIPT_PATH/support.c"
if [ '!' -e "$SUPPORTC" ]; then
echo "Can't find support.c"
exit 1
fi
fi
# Read endianess and architecture bytes
ARCHID=$(python -c '
from binascii import hexlify
import sys
file = open(sys.argv[1], "rb")
file.seek(5)
result = hexlify(file.read(1))
file.seek(17)
result += hexlify(file.read(2))
file.close()
print(result.decode("utf-8"))' "$INPUT")
case "$ARCHID" in
010028)
ARCH=arm;
;;
020200)
ARCH=mips;
;;
01003e)
ARCH=x86_64;
;;
*)
echo "Unknown architecture: $ARCHID"
exit 1
;;
esac
if [ "$SKIP" -eq 0 ]; then
"$REVAMB" -g ll --debug jtcount,osrjts --use-sections "$INPUT" "$LL" "$@" |& tee "$REVAMB_LOG"
fi
OUTPUT="$INPUT.translated"
if [ "$OPTIMIZE" -eq 0 ]; then
"$LLC" -O0 -filetype=obj "$LL" -o "$OBJ"
elif [ "$OPTIMIZE" -eq 1 ]; then
"$LLC" -O2 -filetype=obj "$LL" -o "$OBJ" -regalloc=fast
elif [ "$OPTIMIZE" -eq 2 ]; then
"$OPT" -O2 -S -o "$LL_OPT" "$LL"
LL="$LL_OPT"
"$LLC" -O2 -filetype=obj "$LL" -o "$OBJ" -regalloc=fast
fi
"$CC" $("$TOOPT" "$CSV") \
"$OBJ" \
"$SUPPORTC" \
-DTARGET_"$ARCH" \
-lz -lm -lrt -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -g \
-o "$OUTPUT" \
-fno-pie