-
Notifications
You must be signed in to change notification settings - Fork 2
/
livepatch-build
executable file
·286 lines (248 loc) · 8.41 KB
/
livepatch-build
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
#!/bin/bash
#
# livepatch build script
#
# Copyright (C) 2014 Seth Jennings <[email protected]>
# Copyright (C) 2013,2014 Josh Poimboeuf <[email protected]>
# Copyright (C) 2015 Ross Lagerwall <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script takes a Xen tree, and a patch and outputs an livepatch
# module intended to patch Xen at runtime.
# Large amounts of this script are taken from kpatch's kpatch-build
# script.
SCRIPTDIR="$(readlink -f $(dirname $(type -p $0)))"
CPUS="$(getconf _NPROCESSORS_ONLN)"
DEBUG=n
XEN_DEBUG=n
SKIP=
DEPENDS=
PRELINK=
XENSYMS=xen-syms
warn() {
echo "ERROR: $1" >&2
}
die() {
if [[ -z $1 ]]; then
msg="LivePatch build failed"
else
msg="$1"
fi
warn "$msg."
exit 1
}
function make_patch_name()
{
PATCHNAME=$(basename "$1")
if [[ "$PATCHNAME" =~ \.patch ]] || [[ "$PATCHNAME" =~ \.diff ]]; then
PATCHNAME="${PATCHNAME%.*}"
fi
# Only allow alphanumerics and '_' and '-' in the patch name. Everything
# else is replaced with '-'. Truncate to 48 chars.
echo ${PATCHNAME//[^a-zA-Z0-9_-]/-} |cut -c 1-48
}
# Do a full normal build
function build_full()
{
cd "${SRCDIR}/xen" || die
make "-j$CPUS" clean &> "${OUTPUT}/build_full_clean.log" || die
make "-j$CPUS" debug="$XEN_DEBUG" &> "${OUTPUT}/build_full_compile.log" || die
cp xen-syms "$OUTPUT"
}
# Build with special GCC flags
function build_special()
{
name=$1
cd "${SRCDIR}" || die
# Capture .o files from the patched build
export CROSS_COMPILE="${SCRIPTDIR}/livepatch-gcc "
export LIVEPATCH_BUILD_DIR="$(pwd)/"
export LIVEPATCH_CAPTURE_DIR="$OUTPUT/${name}"
mkdir -p "$LIVEPATCH_CAPTURE_DIR"
# Build with special GCC flags
cd "${SRCDIR}/xen" || die
sed -i 's/CFLAGS += -nostdinc/CFLAGS += -nostdinc -ffunction-sections -fdata-sections/' Rules.mk
make "-j$CPUS" debug="$XEN_DEBUG" &> "${OUTPUT}/build_${name}_compile.log" || die
sed -i 's/CFLAGS += -nostdinc -ffunction-sections -fdata-sections/CFLAGS += -nostdinc/' Rules.mk
unset LIVEPATCH_BUILD_DIR
unset LIVEPATCH_CAPTURE_DIR
}
function create_patch()
{
echo "Extracting new and modified ELF sections..."
[[ -e "${OUTPUT}/original/changed_objs" ]] || die "no changed objects found"
[[ -e "${OUTPUT}/patched/changed_objs" ]] || die "no changed objects found"
cd "${OUTPUT}/original" || die
FILES="$(find xen -type f -name "*.o")"
cd "${OUTPUT}" || die
CHANGED=0
ERROR=0
debugopt=
[[ $DEBUG -eq 1 ]] && debugopt=-d
for i in $FILES; do
mkdir -p "output/$(dirname $i)" || die
echo "Processing ${i}"
echo "Run create-diff-object on $i" >> "${OUTPUT}/create-diff-object.log"
"${SCRIPTDIR}"/create-diff-object $debugopt $PRELINK "original/$i" "patched/$i" "$XENSYMS" "output/$i" &>> "${OUTPUT}/create-diff-object.log"
rc="${PIPESTATUS[0]}"
if [[ $rc = 139 ]]; then
warn "create-diff-object SIGSEGV"
if ls core* &> /dev/null; then
cp core* /tmp
die "core file at /tmp/$(ls core*)"
fi
die "no core file found, run 'ulimit -c unlimited' and try to recreate"
fi
# create-diff-object returns 3 if no functional change is found
[[ $rc -eq 0 ]] || [[ $rc -eq 3 ]] || ERROR=$(expr $ERROR "+" 1)
if [[ $rc -eq 0 ]]; then
CHANGED=1
fi
done
if [[ $ERROR -ne 0 ]]; then
die "$ERROR error(s) encountered"
fi
if [[ $CHANGED -eq 0 ]]; then
die "no functional changes found"
fi
# Create a dependency section
perl -e "print pack 'VVVZ*H*', 4, 20, 3, 'GNU', '${DEPENDS}'" > depends.bin
echo "Creating patch module..."
if [ -z "$PRELINK" ]; then
ld -r -o "${PATCHNAME}.livepatch" --build-id=sha1 $(find output -type f -name "*.o") || die
chmod +x "${PATCHNAME}.livepatch"
else
ld -r -o output.o --build-id=sha1 $(find output -type f -name "*.o") || die
"${SCRIPTDIR}"/prelink $debugopt output.o "${PATCHNAME}.livepatch" "$XENSYMS" &>> "${OUTPUT}/prelink.log" || die
fi
objcopy --add-section .livepatch.depends=depends.bin "${PATCHNAME}.livepatch"
objcopy --set-section-flags .livepatch.depends=alloc,readonly "${PATCHNAME}.livepatch"
}
usage() {
echo "usage: $(basename $0) [options]" >&2
echo " -h, --help Show this help message" >&2
echo " -s, --srcdir Xen source directory" >&2
echo " -p, --patch Patch file" >&2
echo " -o, --output Output directory" >&2
echo " -j, --cpus Number of CPUs to use" >&2
echo " -k, --skip Skip build or diff phase" >&2
echo " -d, --debug Enable debug logging" >&2
echo " --xen-debug Build debug Xen" >&2
echo " --xen-syms Build against a xen-syms" >&2
echo " --depends Required build-id" >&2
echo " --prelink Prelink" >&2
}
options=$(getopt -o hs:p:o:j:k:d -l "help,srcdir:patch:output:cpus:,skip:,debug,xen-debug,xen-syms:,depends:,prelink" -- "$@") || die "getopt failed"
eval set -- "$options"
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-j|--cpus)
shift
CPUS="$1"
shift
;;
-k|--skip)
shift
SKIP="$1"
shift
;;
-d|--debug)
DEBUG=1
shift
;;
--xen-debug)
XEN_DEBUG=y
shift
;;
-s|--srcdir)
shift
srcarg="$1"
shift
;;
-p|--patch)
shift
patcharg="$1"
shift
;;
-o|--output)
shift
outputarg="$1"
shift
;;
--xen-syms)
shift
XENSYMS="$(readlink -m -- "$1")"
[ -f "$XENSYMS" ] || die "xen-syms file does not exist"
shift
;;
--depends)
shift
DEPENDS="$1"
shift
;;
--prelink)
PRELINK=--resolve
shift
;;
--)
shift
break
;;
esac
done
[ -z "$srcarg" ] && die "Xen directory not given"
[ -z "$patcharg" ] && die "Patchfile not given"
[ -z "$outputarg" ] && die "Output directory not given"
[ -z "$DEPENDS" ] && die "Build-id dependency not given"
SRCDIR="$(readlink -m -- "$srcarg")"
PATCHFILE="$(readlink -m -- "$patcharg")"
OUTPUT="$(readlink -m -- "$outputarg")"
[ -d "${SRCDIR}" ] || die "Xen directory does not exist"
[ -f "${PATCHFILE}" ] || die "Patchfile does not exist"
PATCHNAME=$(make_patch_name "${PATCHFILE}")
echo "Building LivePatch patch: ${PATCHNAME}"
echo
echo "Xen directory: ${SRCDIR}"
echo "Patch file: ${PATCHFILE}"
echo "Output directory: ${OUTPUT}"
echo "================================================"
echo
if [ "${SKIP}" != "build" ]; then
[ -e "${OUTPUT}" ] && die "Output directory exists"
mkdir -p "${OUTPUT}" || die
echo "Testing patch file..."
cd "$SRCDIR" || die
patch -s -N -p1 --dry-run < "$PATCHFILE" || die "source patch file failed to apply"
echo "Perform full initial build with ${CPUS} CPU(s)..."
build_full
echo "Apply patch and build with ${CPUS} CPU(s)..."
cd "$SRCDIR" || die
patch -s -N -p1 < "$PATCHFILE" || die
build_special patched
echo "Unapply patch and build with ${CPUS} CPU(s)..."
cd "$SRCDIR" || die
patch -s -R -p1 < "$PATCHFILE" || die
build_special original
fi
if [ "${SKIP}" != "diff" ]; then
[ -d "${OUTPUT}" ] || die "Output directory does not exist"
cd "${OUTPUT}" || die
create_patch
echo "${PATCHNAME}.livepatch created successfully"
fi