-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenbin
executable file
·116 lines (101 loc) · 2.92 KB
/
genbin
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# writes to stdout the hex bytecodes of an assmebled assembly file given from stdin with 64 bit architecture
# if no file is provided, stdin is read
# awk filter to cut the bytecodes from objdump output
# not used anymore
awk_filter='{
for(i=1; i <=7; i++)
if($i ~ /^[0-9a-f]+$/)
printf "%s", $i
}'
# Define a function to display the usage/help message
display_help() {
echo "genbin writes to stdout the hex bytecodes of an assembled assembly file given from stdin with 64 but architecture"
echo "if no file is provided, stdin is read."
echo "Usage: $(basename "$0") [OPTIONS] [FILE]"
echo "Options:"
echo " -h, --help Display this help message"
echo " -bx, --backslash_x Bytes will be prepended with '\x'"
echo " -nb, --nbytes Prints the total number of bytes of the binary"
}
file=""
bx=false
nb=false
# Parsing options and arguments
while [ "$#" -gt 0 ] && [[ "$1" == -* ]]; do
case "$1" in
-h | --help)
display_help
exit 0
;;
-bx | --backslash_x)
bx=true
;;
-nb | --nbytes)
nb=true
;;
*)
echo "Invalid option: $1" >&2
display_help
exit 1
;;
esac
shift
done
#read from file if provided otherwise read from stdin
if [ $# -eq 0 ]; then
file="/dev/stdin"
elif [ -n "$1" -a -e "$1" ]; then
file="$1"
else
echo "assembly file not found"
exit 1
fi
# getting the code
asm=$(cat "$file")
# nasm is not like as
# It will complain if you give it in input a not well formatted assembly file
# So we have to prepend this initial stub
asm_completed="section .text
global _start
_start:
${asm}"
echo "$asm_completed" > a.temp
# assembling for 64 bit arch just to get to the object file
# this makes disassembling easier and more quality
nasm -f elf64 -g -F dwarf -o a.temp.o a.temp
# old version of the program was using objdump to disassemble file
# output was then filtered out with awk
#objdump -d --no-addresses -M intel a.temp.o > a.temp.dump
#awk "$awk_filter" a.temp.dump > a.temp.filtered
# now we use objcopy, similar to objdump
# converts object file into its binary representation
objcopy -O binary a.temp.o a.temp.o2
# od converts the representation of a file
# -An stripps the address offset column from the input
# -v outputs all input data, including consecutive identical lines
# -t x1 specifies the output format as hexadecimal with 1 byte per line
# it is then used tr to delete all new lines characters obtaining a single long line of hex
od -An -v -t x1 a.temp.o2 | tr -d ' \n' > a.temp.filtered
# creating different versions of the output
# normal
# with '\x' appended to each hex byte
# and calculating the total number of resulting bytes
filtered=$(cat a.temp.filtered)
filtered_slashx=$(echo "$filtered" | sed 's/../\\x&/g')
total_bytes=$((${#filtered} / 2))
# output
if [ "$bx" = true ]; then
echo "$filtered_slashx"
else
echo "$filtered"
fi
if [ "$nb" = true ]; then
echo "$total_bytes"
fi
# cleaning
rm a.temp
rm a.temp.o
#rm a.temp.dump
rm a.temp.o2
rm a.temp.filtered