Skip to content

Commit 263bc0c

Browse files
Iulian Barbudianpopa
authored andcommitted
devtool: added strip command
Added `devtool strip` command. This command can strip debug symbols from Firecracker release binaries. The command can be parameterized by `--target-libc (musl|gnu)`, which sets the libc used by a specific toolchain (e.g. x86_64-uknown-linux-${libc_flavor}). Signed-off-by: Iulian Barbu <[email protected]>
1 parent 8d4f4ef commit 263bc0c

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- Added experimental JSON response format support for MMDS guest applications
1818
requests.
1919
- Added metrics for the vsock device.
20+
- Added devtool strip command which removes debug symbols from the release
21+
binaries.
2022

2123
### Fixed
2224

tests/host_tools/cargo_build.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def get_firecracker_binaries():
6969
fc_bin_path = "{}/{}".format(out_dir, FC_BINARY_NAME)
7070
jailer_bin_path = "{}/{}".format(out_dir, JAILER_BINARY_NAME)
7171

72+
utils.run_cmd(
73+
"strip --strip-debug {} {}"
74+
.format(fc_bin_path, jailer_bin_path)
75+
)
76+
7277
return fc_bin_path, jailer_bin_path
7378

7479

tests/integration_tests/build/test_binary_size.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,31 @@
1111
MACHINE = platform.machine()
1212
""" Platform definition used to select the correct size target"""
1313

14-
FC_BINARY_SIZE_TARGET = 2751144 if MACHINE == "x86_64" else 3132008
14+
SIZES_DICT = {
15+
"x86_64": {
16+
"FC_BINARY_SIZE_TARGET": 1648616,
17+
"JAILER_BINARY_SIZE_TARGET": 1439512,
18+
"FC_BINARY_SIZE_LIMIT": 2052927,
19+
"JAILER_BINARY_SIZE_LIMIT": 1511488,
20+
},
21+
"aarch64": {
22+
"FC_BINARY_SIZE_TARGET": 1619432,
23+
"JAILER_BINARY_SIZE_TARGET": 1338312,
24+
"FC_BINARY_SIZE_LIMIT": 2024290,
25+
"JAILER_BINARY_SIZE_LIMIT": 1511488,
26+
}
27+
}
28+
29+
FC_BINARY_SIZE_TARGET = SIZES_DICT[MACHINE]["FC_BINARY_SIZE_TARGET"]
1530
"""Firecracker target binary size in bytes"""
1631

17-
FC_BINARY_SIZE_LIMIT = 3100000 if MACHINE == "x86_64" else 3500000
32+
FC_BINARY_SIZE_LIMIT = SIZES_DICT[MACHINE]["FC_BINARY_SIZE_LIMIT"]
1833
"""Firecracker maximum binary size in bytes"""
1934

20-
JAILER_BINARY_SIZE_TARGET = 2483592 if MACHINE == "x86_64" else 2714528
35+
JAILER_BINARY_SIZE_TARGET = SIZES_DICT[MACHINE]["JAILER_BINARY_SIZE_TARGET"]
2136
"""Jailer target binary size in bytes"""
2237

23-
JAILER_BINARY_SIZE_LIMIT = 3000000
38+
JAILER_BINARY_SIZE_LIMIT = SIZES_DICT[MACHINE]["JAILER_BINARY_SIZE_LIMIT"]
2439
"""Jailer maximum binary size in bytes"""
2540

2641
BINARY_SIZE_TOLERANCE = 0.05

tools/devtool

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ say_noln() {
139139
#
140140
say_err() {
141141
[ -t 2 ] && [ -n "$TERM" ] \
142-
&& echo "$(tput setaf 1)[$MY_NAME] $*$(tput sgr0)" 1>&2 \
143-
|| echo "[$MY_NAME] $*" 1>&2
142+
&& echo -e "$(tput setaf 1)[$MY_NAME] $*$(tput sgr0)" 1>&2 \
143+
|| echo -e "[$MY_NAME] $*" 1>&2
144144
}
145145

146146
# Send a warning-highlighted text to stdout
@@ -234,6 +234,19 @@ ensure_build_dir() {
234234
done
235235
}
236236

237+
# Makes sure that Firecracker release binaries were built.
238+
# This is relevant in the context of stripping the already built release binaries.
239+
ensure_release_binaries_exist() {
240+
target=$1
241+
profile=$2
242+
firecracker_bin_path="$CARGO_TARGET_DIR/$target/$profile/firecracker"
243+
jailer_bin_path="$CARGO_TARGET_DIR/$target/$profile/jailer"
244+
245+
# Both binaries must exist for the stripping to be succesgitsful.
246+
[ -f $firecracker_bin_path ] && [ -f $jailer_bin_path ] || \
247+
die "Missing release binaries. Needed files:\n* $firecracker_bin_path\n* $jailer_bin_path."
248+
}
249+
237250
# Fix build/ dir permissions after a privileged container run.
238251
# Since the privileged cotainer runs as root, any files it creates will be
239252
# owned by root. This fixes that by recursively changing the ownership of build/
@@ -385,6 +398,15 @@ cmd_help() {
385398
echo " -l, --libc musl|gnu Choose the libc flavor against which Firecracker will"
386399
echo " be linked. Default is musl."
387400
echo ""
401+
echo " strip [--target-libc musl|gnu]"
402+
echo " Strip debug symbols from the Firecracker release binaries."
403+
echo ""
404+
echo " --target-libc musl|gnu Choose the target libc flavor which determines the"
405+
echo " toolchain used to build Firecracker release binaries."
406+
echo " Stripping will occur only for the Firecracker release"
407+
echo " binaries corresponding to the inferred toolchain. Default"
408+
echo " is musl."
409+
echo ""
388410
echo " fmt"
389411
echo " Auto-format all Rust source files, to match the Firecracker requirements."
390412
echo " This should be used as the last step in every commit, to ensure that the"
@@ -504,6 +526,55 @@ cmd_build() {
504526
return $ret
505527
}
506528

529+
cmd_strip() {
530+
profile="release"
531+
libc="musl"
532+
target="$(uname -m)-unknown-linux-${libc}"
533+
534+
# Parse any command line args.
535+
while [ $# -gt 0 ]; do
536+
case "$1" in
537+
"-h"|"--help") { cmd_help; exit 1; } ;;
538+
"--target-libc")
539+
shift
540+
[[ "$1" =~ ^(musl|gnu)$ ]] || \
541+
die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"."
542+
libc="$1"
543+
target="$(uname -m)-unknown-linux-${libc}"
544+
;;
545+
*)
546+
die "Unknown argument: $1. Please use --help for help."
547+
;;
548+
esac
549+
shift
550+
done
551+
552+
# Check prerequisites
553+
ensure_devctr
554+
ensure_build_dir
555+
ensure_release_binaries_exist $target $profile
556+
557+
say "Starting stripping the debug symbols for $profile binaries built against $target target."
558+
strip_flags="--strip-debug"
559+
say "Strip flags: $strip_flags."
560+
561+
run_devctr \
562+
--user "$(id -u):$(id -g)" \
563+
-- \
564+
strip $strip_flags\
565+
"$CTR_CARGO_TARGET_DIR/$target/$profile/firecracker" \
566+
"$CTR_CARGO_TARGET_DIR/$target/$profile/jailer"
567+
ret=$?
568+
569+
[ $ret -eq 0 ] && {
570+
cargo_bin_dir="$CARGO_TARGET_DIR/$target/$profile"
571+
say "Stripping was successful."
572+
say "Stripped binaries placed under $cargo_bin_dir."
573+
}
574+
575+
return $ret
576+
}
577+
507578
# `$0 test` - run integration tests
508579
# Please see `$0 help` for more information.
509580
#

0 commit comments

Comments
 (0)