forked from SoftSec-KAIST/BinKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_compile_utils.sh
executable file
·427 lines (360 loc) · 12.7 KB
/
do_compile_utils.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash
# ========= NOTES ===================
# if not use lld
# -> flto cannot be compiled
# if use lld
# -> pie link error to readonly section for mips_32, mipseb_32
# TODO: add VER to the final binary name after PACKAGE_NAME
# ------- get arguments ------------------------
PACKAGE_NAME=${1}
TAR_NAME=${2}
VER=${3}
WORK_DIR=${4}
OUTDIR=${5}
LOGDIR=${6}
OPTI_LEVEL=${7}
ARCH=${8}
COMPILER=${9}
SUFFIX=${10}
ECHOON=${11}
mkdir -p $LOGDIR
COMPILETYPE="${COMPILER}_${ARCH}_${OPTI_LEVEL}"
CONFIGSUB=$(<patches/config.sub)
## ------ check already exists ----------------
#files=`find "${OUTDIR}" -executable -type f -name "${PACKAGE_NAME}*_${COMPILETYPE}_*"`
#if [ ! -z "${files// }" ]; then
# exit
#fi
# ------- check default ----------------------
cd $WORK_DIR
if [ -f $TAR_NAME ] ; then
case $TAR_NAME in
*.tar.bz2) TAR_OPT="jf" ;;
*.tar.gz) TAR_OPT="zf" ;;
*.tar.xz) TAR_OPT="f" ;;
*.txz) TAR_OPT="f" ;;
*.tar) TAR_OPT="f" ;;
*.tbz2) TAR_OPT="jf" ;;
*.tgz) TAR_OPT="zf" ;;
*) echo "'$TAR_NAME' cannot be extracted via extract()"; exit ;;
esac
else
echo "'$TAR_NAME' is not a valid file"
exit
fi
# set working directory
TAR_ROOT=`tar t${TAR_OPT} "${TAR_NAME}" | sed -e 's@/.*@@' | uniq`
if [ -z $TAR_ROOT ]; then
echo "${PACKAGE_NAME} got something wrong"
exit
fi
if [[ $TAR_ROOT == "." ]]; then
# if no root directory
TAR_ROOT=$PACKAGE_NAME
fi
if [[ ! -d $TAR_ROOT ]]; then
echo "${PACKAGE_NAME} has no root ..."
exit
fi
function do_compile()
{
local CCTARGET=$1
local AUTOCONF=$2
LOGPREFIX="${PACKAGE_NAME}-${VER}_${COMPILETYPE}${SUFFIX}"
if [ ! -z "$CCTARGET" ] || [ ! -z "$AUTOCONF" ]; then
if [ ! $CNT -eq "0" ]; then
return
else
LOGPREFIX="${PACKAGE_NAME}-${VER}_${COMPILETYPE}${SUFFIX}_${CCTARGET}_${AUTOCONF}"
fi
fi
# ------------------- compile with CC="clang --target=" -----------------
# clang needs to compile with this ...
if [[ $CCTARGET == "CCTARGET" ]] && [[ ! $COMPILER =~ "clang" ]]; then
return
fi
NEW_WORK_DIR="${TAR_ROOT}_${COMPILETYPE}${SUFFIX}"
# remove directory and copy again
rm -rf "${NEW_WORK_DIR}"
if [ ! -d "$NEW_WORK_DIR" ]; then
cp -R --preserve=all $TAR_ROOT $NEW_WORK_DIR
fi
cd $NEW_WORK_DIR
if [ ! -f "configure" ]; then
cd ..
rm -rf $NEW_WORK_DIR
exit
fi
# need to add aarch64 to config.sub
#if [[ $ARCH =~ "arm_64" ]]; then
subfile=`find . -name "config.sub" 2>/dev/null`
mapfile -t subfiles <<< "${subfile}"
for sub in "${subfiles[@]}"; do
if [[ -f "${sub}" ]]; then
chmod 755 ${sub}
echo "${CONFIGSUB}" > ${sub}
fi
done
#fi
# ------- check architecture ------------------
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"
local OPTIONS=""
local EXTRA_CFLAGS=""
local EXTRA_LDFLAGS=""
# for debugging information
# -fno-var-tracking due to gcc-4.9.4 O1 dwarf2out
OPTIONS="${OPTIONS} -g -fno-var-tracking"
# ------- check inline, pie, lto options -----
if [[ $SUFFIX =~ "noinline" ]]; then
OPTIONS="${OPTIONS} -fno-inline"
fi
if [[ $SUFFIX =~ "nopie" ]]; then
OPTIONS="${OPTIONS} -no-pie -fno-PIE"
# we will not consider giving nostartfiles option since this corrupt the
# section and yields corrupted elf binary.
# if [[ $PACKAGE_NAME =~ "coreutils" ]]; then
# EXTRA_LDFLAGS="-nostartfiles"
# fi
fi
if [[ $SUFFIX =~ "lto" ]]; then
OPTIONS="${OPTIONS} -flto"
fi
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-2" ]]; then
EXTRA_CFLAGS="${EXTRA_CFLAGS} -mllvm -fla -mllvm -split -mllvm -split_num=2 -mllvm -sub -mllvm -sub_loop=2 -mllvm -bcf -mllvm -bcf_loop=2"
elif [[ $OBFUS =~ "all" ]]; then
EXTRA_CFLAGS="${EXTRA_CFLAGS} -mllvm -fla -mllvm -sub -mllvm -bcf"
else
EXTRA_CFLAGS="${EXTRA_CFLAGS} -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 =~ "eb_" ]]; then
OPTIONS="${OPTIONS} -EB"
fi
# ========= x86 =============
if [ $ARCH == "x86_32" ]; then
ARCH_PREFIX=$ARCH_X86
OPTIONS="${OPTIONS} -m32"
ELFTYPE="ELF 32-bit LSB"
ARCHTYPE="Intel 80386"
elif [ $ARCH == "x86_64" ]; then
ARCH_PREFIX=$ARCH_X8664
ELFTYPE="ELF 64-bit LSB"
ARCHTYPE="x86-64"
# ========= arm =============
elif [ $ARCH == "arm_32" ]; then
ARCH_PREFIX=$ARCH_ARM
ELFTYPE="ELF 32-bit LSB"
ARCHTYPE="ARM, EABI5"
elif [ $ARCH == "arm_64" ]; then
ARCH_PREFIX=$ARCH_ARM64
ELFTYPE="ELF 64-bit LSB"
ARCHTYPE="ARM aarch64"
# ========= mips =============
elif [ $ARCH == "mips_32" ]; then
ARCH_PREFIX=$ARCH_MIPS
OPTIONS="${OPTIONS} -mips32r2"
ELFTYPE="ELF 32-bit LSB"
#ARCHTYPE="MIPS, MIPS32 rel2"
ARCHTYPE="MIPS, MIPS32"
elif [ $ARCH == "mips_64" ]; then
ARCH_PREFIX=$ARCH_MIPS64
OPTIONS="${OPTIONS} -mips64r2"
ELFTYPE="ELF 64-bit LSB"
#ARCHTYPE="MIPS, MIPS64 rel2"
ARCHTYPE="MIPS, MIPS64"
# ========= mipseb =============
elif [ $ARCH == "mipseb_32" ]; then
ARCH_PREFIX=$ARCH_MIPSEB
OPTIONS="${OPTIONS} -mips32r2"
ELFTYPE="ELF 32-bit MSB"
#ARCHTYPE="MIPS, MIPS32 rel2"
ARCHTYPE="MIPS, MIPS32"
elif [ $ARCH == "mipseb_64" ]; then
ARCH_PREFIX=$ARCH_MIPSEB64
OPTIONS="${OPTIONS} -mips64r2"
ELFTYPE="ELF 64-bit MSB"
#ARCHTYPE="MIPS, MIPS64 rel2"
ARCHTYPE="MIPS, MIPS64"
# ========= powerpc =============
elif [[ $ARCH == "ppc_32" ]]; then
ARCH_PREFIX=$ARCH_POWERPC
ELFTYPE="ELF 32-bit MSB"
ARCHTYPE="PowerPC or cisco 4500"
elif [[ $ARCH == "ppc_64" ]]; then
ARCH_PREFIX=$ARCH_POWERPC64
ELFTYPE="ELF 64-bit MSB"
ARCHTYPE="64-bit PowerPC or cisco 7500"
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"
EXTRA_CFLAGS="${EXTRA_CFLAGS} -fcommon"
if [ $ARCH == "x86_64" ]; then
local EXTRA_LDFLAGS="-L${EXTRA_DEP_PATH}/install_x86_64/lib -luuid"
fi
OPTIONS="${OPTIONS} -${OPTI_LEVEL}"
if [[ $COMPILER =~ "gcc" ]]; then
CMD=""
CMD="--host=\"${ARCH_PREFIX}\""
CMD="${CMD} CFLAGS=\""
CMD="${CMD} -isysroot ${SYSROOT} -isystem ${SYSTEM} -I${SYSTEM}"
CMD="${CMD} ${OPTIONS} ${EXTRA_CFLAGS}\""
CMD="${CMD} LDFLAGS=\"${OPTIONS} ${EXTRA_LDFLAGS}\""
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="--host=\"${ARCH_PREFIX}\""
# ------------------- compile with CC="clang --target=" -----------------
# clang needs to compile with this ...
if [[ $CCTARGET == "CCTARGET" ]]; then
CMD="${CMD} CC=\"clang --target=${ARCH_PREFIX}"
CMD="${CMD} --gcc-toolchain=${TOOL_PATH}/${ARCH_PREFIX}-${COMPVER} \""
CMD="${CMD} CFLAGS=\" "
else
CMD="${CMD} CC=\"clang\""
CMD="${CMD} CFLAGS=\" --target=${ARCH_PREFIX}"
CMD="${CMD} --gcc-toolchain=${TOOL_PATH}/${ARCH_PREFIX}-${COMPVER} "
fi
CMD="${CMD} -isysroot ${SYSROOT} -isystem ${SYSTEM} -I${SYSTEM}"
CMD="${CMD} ${OPTIONS} ${EXTRA_CFLAGS}\""
CMD="${CMD} LDFLAGS=\"${OPTIONS} ${EXTRA_LDFLAGS}\""
CMD="${CMD} AR=\"llvm-ar\""
CMD="${CMD} RANLIB=\"llvm-ranlib\""
CMD="${CMD} NM=\"llvm-nm\""
fi
# coreutils time_t force to 32-bit
if [[ $ARCH =~ "_32" ]]; then
if [[ $PACKAGE_NAME =~ (coreutils|gzip) ]]; then
CMD="${CMD} TIME_T_32_BIT_OK=yes"
fi
fi
AUTO="autoconf"
#CONF="./configure --build=x86_64-linux-gnu ${CMD}"
# install output binaries in a temporary directory (--prefix)
CONF="./configure --prefix=\"${PWD}/gogo\" --build=x86_64-linux-gnu ${CMD}"
MAKE="make -j ${NUM_JOBS} -l ${MAX_JOBS}"
# make install to filter out unnecessary elf files
INS="make install"
if [[ $ECHOON == "False" ]]; then
AUTO="${AUTO} >${LOGDIR}/${LOGPREFIX}_autoconf.log"
AUTO="${AUTO} 2>${LOGDIR}/${LOGPREFIX}_autoconf_error.log"
CONF="${CONF} >${LOGDIR}/${LOGPREFIX}_configure.log"
CONF="${CONF} 2>${LOGDIR}/${LOGPREFIX}_configure_error.log"
MAKE="${MAKE} >${LOGDIR}/${LOGPREFIX}_make.log"
MAKE="${MAKE} 2>${LOGDIR}/${LOGPREFIX}_make_error.log"
INS="${INS} >${LOGDIR}/${LOGPREFIX}_install.log"
INS="${INS} 2>${LOGDIR}/${LOGPREFIX}_install_error.log"
fi
# -------- now start compiling! -------------
#echo "[+] running $VER $COMPILER $ARCH $OPT ----"
if [[ $AUTOCONF == "AUTOCONF" ]]; then
# autoconf
eval $AUTO
fi
# DELETE DEFAULT OPTIMAZATION LEVEL
sed -i "s/-O[s0-9]*//g" "configure"
# configure
eval $CONF
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
# make
eval $MAKE
# make install
eval $INS
# -------- file check -----------------
CNT=0
# for debug
#tmp_list=`find . -type f -executable -exec file {} \; 2>/dev/null \
# check output binaries in the temporary directory (previously, --prefix)
tmp_list=`find "${PWD}/gogo" -type f -executable -exec file {} \; 2>/dev/null \
| grep -v "ERROR" \
| grep "${ELFTYPE}" | grep "${ARCHTYPE}" \
| cut -d ":" -f 1 | grep -v "\\.o$"`
mapfile -t bin_list <<< "${tmp_list}"
BINS=""
for b in "${bin_list[@]}"
do
# filter known directories that do not belong to output binaries
if [[ ! -z "${b// }" ]] \
&& [[ ! "${b}" =~ (/extension/|/gettext-tools/|/contrib/|/test/) ]] \
&& [[ ! "${b}" =~ (/testsuite/|/modules/|/builtins/|/support/) ]] \
&& [[ ! "${b}" =~ (/tests/|/examples/|/doc/|/po/) ]]; then
#&& [[ ! "${b}" =~ test ]]; then
mkdir -p "${OUTDIR}"
cp "${b}" "${OUTDIR}/${PACKAGE_NAME}-${VER}_${COMPILETYPE}_${b##*/}"
CNT=$((CNT + 1))
BINS="${BINS}${b}\n"
fi
done
if [ "$CNT" -gt "0" ]; then
OUTSTR="${LOGPREFIX}: COMPILE SUCCESS!!"
touch "${LOGDIR}/${LOGPREFIX}_success"
else
OUTSTR="${LOGPREFIX}: COMPILE FAIL."
touch "${LOGDIR}/${LOGPREFIX}_fail"
if [ -f config.log ]; then
cp config.log "${LOGDIR}/${LOGPREFIX}_config.log"
fi
fi
if [[ $ECHOON == "True" ]]; then
echo -e "${OUTSTR}";
fi
cd ..
rm -rf "${NEW_WORK_DIR}"
}
# TODO: move timeout script here
#function check_timeout()
#{
# eval "timeout $1 $2 $3 $4"
# exit_status=$?
# if [[ "$exit_status" -eq "124" ]]; then
# touch "${LOGDIR}/${LOGPREFIX}_timeout"
# fi
#}
CNT=0
# Hope one of below would work. If there exists a compiled binary after one, we
# do not proceed more. The CNT variable reprsents the number of compiled
# binaries.
# For clang, it is better to build with CCTARGET to fully build the package.
# clang without CCTARGET often builds fewer binaries.
if [[ $COMPILER =~ "gcc" ]]; then
do_compile "" ""
do_compile "" "AUTO"
do_compile "CCTARGET" ""
do_compile "CCTARGET" "AUTO"
elif [[ $COMPILER =~ "clang" ]]; then
do_compile "CCTARGET" ""
do_compile "CCTARGET" "AUTO"
do_compile "" ""
do_compile "" "AUTO"
fi