forked from SoftSec-KAIST/BinKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_compile_busybox.sh
executable file
·319 lines (264 loc) · 8.03 KB
/
do_compile_busybox.sh
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
SOURCE_GIT_PATH="https://git.busybox.net/busybox"
WORK_DIR="$BASEDIR/sources/busybox"
OUTDIR="$BASEDIR/output/busybox"
LOGDIR="$BASEDIR/logs/busybox"
PACKAGE_NAME="busybox"
function join_by { local IFS="$1"; shift; echo "$*"; }
ARGS=$@
ARGS_STR=$(join_by _ $ARGS)
ECHO=false
OPTIONS=""
SUFFIX=""
# -fno-var-tracking due to gcc-4.9.4 O1
OPTIONS="${OPTIONS} -g -fno-var-tracking"
SUFFIX="${SUFFIX}_debug"
echo "COMPILING: ${PACKAGE_NAME}-${SUFFIX}"
echo "OUTDIR: ${OUTDIR}"
echo "OPTIONS: ${OPTIONS}"
declare -a unused_arch_list=(
"ppc_32"
"ppc_64"
)
declare -a arch_list=(
"x86_32"
"x86_64"
"arm_32"
"arm_64"
"mips_32"
"mips_64"
"mipseb_32"
"mipseb_64"
)
declare -a opti_list=(
"O0"
"O1"
"O2"
"O3"
)
declare -a compiler_list=(
"gcc-4.9.4"
"gcc-5.5.0"
"gcc-6.4.0"
"gcc-7.3.0"
"gcc-8.2.0"
"clang-4.0"
"clang-5.0"
"clang-6.0"
"clang-7.0"
)
LOGDIR="${LOGDIR}${SUFFIX}"
CONFIGDIR="${LOGDIR}/configure/"
MAKEDIR="${LOGDIR}/make/"
mkdir -p $LOGDIR $CONFIGDIR $MAKEDIR
#declare -a versions=("master")
#declare -a versions=("1_26_stable")
declare -a versions=("1_21_stable")
PATCH=$(<patches/busybox_121_mips_dwarf2out.patch)
function doit()
{
local VER=$1
local COMPILER=$2
local ARCH=$3
local OPT=$4
local OPTIONS="${OPTIONS}"
local EXTRA="${EXTRA}"
make clean >/dev/null 2>/dev/null
make defconfig > /dev/null 2>/dev/null
# nested loop is not supported by clang
sed -i 's/CONFIG_FIND=y/# CONFIG_FIND is not set/' ".config"
# z stream error for clang
sed -i 's/# CONFIG_FEATURE_SEAMLESS_Z is not set/CONFIG_FEATURE_SEAMLESS_Z=y/' ".config"
# debug option
sed -i 's/# CONFIG_DEBUG is not set/CONFIG_DEBUG=y/' ".config"
if [[ $ARCH =~ "eb_" ]]; then
OPTIONS="${OPTIONS} -EB"
fi
ARCH_X86="i686-ubuntu-linux-gnu"
ARCH_X8664="x86_64-ubuntu-linux-gnu"
ARCH_ARM="arm-ubuntu-linux-gnueabi"
ARCH_ARM64="aarch64-ubuntu-linux-gnu"
ARCH_MIPS="mipsel-ubuntu-linux-gnu"
ARCH_MIPS64="mips64el-ubuntu-linux-gnu"
ARCH_MIPSEB="mips-ubuntu-linux-gnu"
ARCH_MIPSEB64="mips64-ubuntu-linux-gnu"
ARCH_POWERPC="powerpc-ubuntu-linux-gnu"
ARCH_POWERPC64="powerpc64-ubuntu-linux-gnu"
if [[ $COMPILER =~ "gcc" ]]; then
COMPVER=${COMPILER#"gcc-"}
elif [[ $COMPILER =~ "clang" ]]; then
# using LLVM-obfuscator
if [[ $COMPILER =~ "obfus" ]]; then
OBFUS=${COMPILER#clang-obfus-}
if [[ $OBFUS =~ "all" ]]; then
EXTRA="${EXTRA} -mllvm -fla -mllvm -sub -mllvm -bcf"
else
EXTRA="${EXTRA} -mllvm -${OBFUS}"
fi
fi
# fix compiler version for clang
COMPVER="8.2.0"
export PATH="${TOOL_PATH}/clang/${COMPILER}/bin:${PATH}"
# clang lto is only supported by lld
if [[ $SUFFIX =~ "lto" ]]; then
OPTIONS="${OPTIONS} -fuse-ld=lld"
fi
else
echo "DO NOT SUPPORT THIS COMPILER: $COMPILER"
exit
fi
if [[ $ARCH == "arm_32" ]]; then
ARCH_PREFIX=$ARCH_ARM
elif [[ $ARCH == "arm_64" ]]; then
ARCH_PREFIX=$ARCH_ARM64
elif [[ $ARCH == "mips_32" ]]; then
ARCH_PREFIX=$ARCH_MIPS
OPTIONS="${OPTIONS} -mips32r2"
elif [[ $ARCH == "mips_64" ]]; then
ARCH_PREFIX=$ARCH_MIPS64
OPTIONS="${OPTIONS} -mips64r2"
elif [[ $ARCH == "mipseb_32" ]]; then
ARCH_PREFIX=$ARCH_MIPSEB
OPTIONS="${OPTIONS} -mips32r2"
elif [[ $ARCH == "mipseb_64" ]]; then
ARCH_PREFIX=$ARCH_MIPSEB64
OPTIONS="${OPTIONS} -mips64r2"
elif [[ $ARCH == "x86_32" ]]; then
ARCH_PREFIX=$ARCH_X86
OPTIONS="${OPTIONS} -m32"
elif [[ $ARCH == "x86_64" ]]; then
ARCH_PREFIX=$ARCH_X8664
elif [[ $ARCH == "ppc_32" ]]; then
ARCH_PREFIX=$ARCH_POWERPC
elif [[ $ARCH == "ppc_64" ]]; then
ARCH_PREFIX=$ARCH_POWERPC64
fi
export PATH="${TOOL_PATH}/${ARCH_PREFIX}-${COMPVER}/bin:${PATH}"
SYSROOT="${TOOL_PATH}/${ARCH_PREFIX}-${COMPVER}/${ARCH_PREFIX}/sysroot"
SYSTEM="${TOOL_PATH}/${ARCH_PREFIX}-${COMPVER}/${ARCH_PREFIX}/sysroot/usr/include"
OPTIONS="${OPTIONS} -${OPT}"
if [[ $COMPILER =~ "gcc" ]]; then
CMD=""
CMD="CROSS_COMPILE=\"${ARCH_PREFIX}-\""
CMD="${CMD} CFLAGS=\" ${OPTIONS}"
CMD="${CMD} -isysroot ${SYSROOT} -isystem ${SYSTEM} -I${SYSTEM}\""
CMD="${CMD} LDFLAGS=\"${OPTIONS}\""
CMD="${CMD} AR=\"${ARCH_PREFIX}-gcc-ar\""
CMD="${CMD} RANLIB=\"${ARCH_PREFIX}-gcc-ranlib\""
CMD="${CMD} NM=\"${ARCH_PREFIX}-gcc-nm\""
elif [[ $COMPILER =~ "clang" ]]; then
CMD="CC=\"clang --target=${ARCH_PREFIX}\""
CMD="${CMD} CROSS_COMPILE=\"${ARCH_PREFIX}-\""
CMD="${CMD} CFLAGS=\" ${OPTIONS} ${EXTRA} --target=${ARCH_PREFIX}"
CMD="${CMD} --gcc-toolchain=${TOOL_PATH}/${ARCH_PREFIX}-${COMPVER}"
CMD="${CMD} -isysroot ${SYSROOT} -isystem ${SYSTEM} -I${SYSTEM}\""
CMD="${CMD} LDFLAGS=\"${OPTIONS}\""
CMD="${CMD} AR=\"llvm-ar\""
CMD="${CMD} RANLIB=\"llvm-ranlib\""
CMD="${CMD} NM=\"llvm-nm\""
fi
CMD="make -j ${NUM_JOBS} -l ${MAX_JOBS} ${CMD}"
if ! $ECHO; then
COMPILETYPE="${COMPILER}_${ARCH}_${OPT}"
MAKEDIR="${LOGDIR}/make/"
CMD="${CMD} >${MAKEDIR}/${PACKAGE_NAME}-${VER}_${COMPILETYPE}${SUFFIX}_make_error.log 2>&1"
fi
if [ -f Makefile ]; then
# one should wrap variables with "" ...
sed -i 's/ CC=\$\${CC:-\$(CC)}/ CC="\$\${CC:-\$(CC)}"/' Makefile
sed -i 's/ CC=\$(CC)/ CC="$(CC)"/' Makefile
fi
eval "${CMD}"
cnt=0
for b in "${bin_list[@]}"
do
if [ -f $b ]; then
cp "${b}" "${NEW_OUTDIR}/${PACKAGE_NAME}-${VER}_${COMPILER}_${ARCH}_${OPT}_${b##*/}"
cnt=$(( $cnt + 1 ))
else
# echo "COMPILE ${b} FAILED"
:
fi
done
if [ $cnt -eq ${#bin_list[@]} ]; then
:
# echo "$cnt COMPILE SUCCESS!!"
else
echo "COMPILE $VER $COMPILER $ARCH $OPT FAILED"
fi
}
function helper()
{
local ver=$1
local comp=$2
local arch=$3
local opti=$4
if $ECHO; then
echo "[+] running $ver $comp $arch $opti ----"
fi
declare -a bin_list=(
"./busybox_unstripped"
)
COMPILETYPE="${comp}_${arch}_${opti}"
NEW_WORK_DIR="${WORK_DIR}-${ver}_${COMPILETYPE}${SUFFIX}"
NEW_OUTDIR="${OUTDIR}/${ver}"
mkdir -p $NEW_OUTDIR
GO=true
for b in "${bin_list[@]}"
do
if [ ! -f "${NEW_OUTDIR}/${PACKAGE_NAME}-${ver}_${comp}_${arch}_${opti}_${b##*/}" ]; then
GO=false
fi
done
if $GO; then
#echo "${ver}_${comp}_${arch}_${opti} already done."
cd ..
rm -rf $NEW_WORK_DIR
return
fi
rm -rf $NEW_WORK_DIR
cp -R --preserve=all $WORK_DIR $NEW_WORK_DIR
cd $NEW_WORK_DIR
eval "git checkout -- . >/dev/null 2>&1"
eval "git checkout $ver >/dev/null 2>&1"
# DELETE DEFAULT OPTIMAZATION LEVEL
#sed -i "s/-O[0-9]*//g" "configure"
# if [[ $ver == "1_21_stable" ]]; then
# echo "$PATCH" | patch -p0 -N > /dev/null
# fi
doit "$ver" "$comp" "$arch" "$opti" "$NEW_OUTDIR"
cd ..
rm -rf $NEW_WORK_DIR
}
#echo $WORK_DIR
if [ ! -d "$WORK_DIR" ]; then
git clone $SOURCE_GIT_PATH $WORK_DIR
fi
mkdir -p $OUTDIR
declare -a cmds
declare -i cmd_idx=0
for ver in "${versions[@]}"; do
for comp in "${compiler_list[@]}"; do
for arch in "${arch_list[@]}"; do
for opti in "${opti_list[@]}"; do
cmds[$cmd_idx]="helper ${ver} ${comp} ${arch} ${opti}"
# echo "${cmds[$cmd_idx]}"
# eval "${cmds[$cmd_idx]}"
# exit
let cmd_idx++
done
done
done
done
export -f helper
export -f doit
export WORK_DIR
export OUTDIR
export LOGDIR
export SUFFIX
export OPTIONS
export ECHO
export PATCH
export PACKAGE_NAME
echo "${#cmds[@]} options to be processed ..."
parallel -j "$NUM_JOBS" ::: "${cmds[@]}"