forked from eurecom-s3/symcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_sym
executable file
·123 lines (100 loc) · 2.87 KB
/
make_sym
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
#!/bin/bash
set -eux
SYMCC_BASE="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
SYMCC_RUNTIME_DIR="$(readlink -f "${SYMCC_BASE}/SymRuntime-prefix/src/SymRuntime-build")"
SYMCC_LIBCXX_DIR="$(readlink -f "${SYMCC_BASE}/libcxx_symcc-prefix/src/libcxx_symcc-build/lib")"
LIBMODE=false
while [ $# -gt 1 ]; do
case $1 in
-l | --lib)
LIBMODE=true
;;
*)
echo "Unknown argument $1, ignoring"
;;
esac
shift 1
done
input_file=$1
bitcode="${input_file}.bc"
bitcode_sym="${input_file}.sym.bc"
output_sym="${input_file}.sym"
if ! [ -f "${bitcode}" ]; then
extract-bc -o "${bitcode}" "${input_file}"
if ! [ -f "${bitcode}" ]; then
echo >&2 "Cannot extract bitcode from ${input_file}"
exit 1
fi
fi
# ================
# Generate abilists
# ================
extra_runtime=""
extra_link=""
extra_link_sym=""
for lib in $(ldd ${input_file} | sed -n -E 's/^.*=>\s*(\S+)\s.*$/\1/;T;p'); do
libname=$(basename "${lib}")
echo "Checking dependency ${libname} (${lib})"
if ([ -f "${lib}.sym" ] ) || ${SYMCC_BASE}/make_sym -l "${lib}"; then
extra_link_sym="${extra_link_sym} -l:${libname}.sym"
else
extra_link="${extra_link} -l:${libname}"
fi
done
# ================
# Instrumentation
# ================
base_id=$(md5sum "${bitcode}" | sed -n 's/^\([0-9a-fA-F]\{16\}\).*$/0x\1/;T;p')
opt -load=${SYMCC_BASE}/libSymbolize.so \
-symbolize \
-o "${bitcode_sym}" "${bitcode}"
# ================
# Compilation/Linking
# ================
compiler=$(which clang)
# Step1: find link-paths from original
extra_linkpath=$(readelf -d "${input_file}" | sed -n 's/^.*(\(RUNPATH\|RPATH\)).*\[\(.*\)]$/\2/g;T;p' | sed 's/:/\n/g' | sed 's/^/-L/g' | xargs)
extra_rpath=$(readelf -d "${input_file}" | sed -n 's/^.*(\(RUNPATH\|RPATH\)).*\[\(.*\)]$/\2/g;T;p' | sed 's/:/\n/g' | sed 's/^/-Wl,-rpath=/g' | xargs)
# Step2: Adjust extra_link and set compiler
if [[ "${extra_link}" == *"c++"* ]]; then
extra_link="-L\"${SYMCC_LIBCXX_DIR}\" -Wl,-rpath,\"${SYMCC_LIBCXX_DIR}\" -stdlib=libc++ -Wl,--start-group -lstdc++ -lc++ -lc++abi -Wl,--end-group ${extra_link}"
compiler=$(which clang++)
else
extra_link="${extra_link}"
fi
extra_link="${extra_linkpath} ${extra_link} ${extra_rpath}"
if [ ${LIBMODE} = false ]; then
${compiler} -o "${output_sym}" "${bitcode_sym}" \
-pie \
-fpic \
-Qunused-arguments \
-g \
-O3 \
-L"${SYMCC_RUNTIME_DIR}" \
-lSymRuntime \
-Wl,-rpath,"${SYMCC_RUNTIME_DIR}" \
-ldl \
-lrt \
-lpthread \
-lm \
${extra_link} \
${extra_link_sym} \
-Wl,-export-dynamic
else
${compiler} -o "${output_sym}" "${bitcode_sym}" \
-shared \
-fpic \
-Qunused-arguments \
-g \
-O3 \
-L"${SYMCC_RUNTIME_DIR}" \
-lSymRuntime \
-Wl,-rpath,"${SYMCC_RUNTIME_DIR}" \
-ldl \
-lrt \
-lpthread \
-lm \
${extra_link} \
${extra_link_sym} \
-Wl,-export-dynamic
fi