blob: 4d74e5ddd41ac894a3e798c92368f9715329c749 [file] [log] [blame]
#!/bin/bash
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# A tool that can be passed in passed in as the strip command to the
# Linux kernel which will create split debug files and stash them away
# where portage expects them.
die() {
if [[ "$#" -ge 1 ]]; then
echo "$@" >&2
fi
exit 1
}
main() {
local magic="$1"
local obj_file_abspath="$2"
local obj_file_relpath="${obj_file_abspath#${INSTALL_MOD_PATH}}"
local debug_file_relpath="usr/lib/debug/${obj_file_relpath}.debug"
local debug_file_abspath="${INSTALL_MOD_PATH}/${debug_file_relpath}"
# We explicitly tell the kernel to pass us the argument "magic" so
# that we can predictably know that the object file to strip is in $2.
# Though right now the kernel's default flags still would have had the
# object file in $2 (the kernel by default passes in --strip-debug),
# it seems better not to rely upon that.
if [[ "${magic}" != "magic" ]]; then
die "Unexpected call to $0"
fi
# If the kernel is calling strip on something that's not in the
# module directory then we don't do our magic. Detect this by seeing
# that we couldn't strip INSTALL_MOD_PATH from the start of the path.
#
# NOTE: we don't actually see this in practice since we only
# specify this script when building modules, but better safe
# than sorry.
if [[ "${obj_file_abspath}" == "${obj_file_relpath}" ]]; then
exec ${CROSS_COMPILE}strip --strip-debug "${obj_file_abspath}"
fi
# Follow the recipe from the objcopy man page to split the debug.
mkdir -p "${debug_file_abspath%/*}"
${CROSS_COMPILE}objcopy --only-keep-debug \
"${obj_file_abspath}" "${debug_file_abspath}" || die
${CROSS_COMPILE}strip --strip-debug "${obj_file_abspath}" || die
${CROSS_COMPILE}objcopy --add-gnu-debuglink=${debug_file_abspath} \
"${obj_file_abspath}" || die
}
main "$@"