diff options
author | Chih-Hung Hsieh <chh@google.com> | 2018-06-04 10:37:43 -0700 |
---|---|---|
committer | Chih-Hung Hsieh <chh@google.com> | 2018-06-07 16:04:14 -0700 |
commit | 30485c920c64880856c58bc33e2b45109a13c004 (patch) | |
tree | c200398533fb07fe194d3beeda4e9915f9e69dbf /scripts | |
parent | 38462a68811eb4e427ff47d7cc62de1ac5835135 (diff) | |
download | build_soong-30485c920c64880856c58bc33e2b45109a13c004.tar.gz build_soong-30485c920c64880856c58bc33e2b45109a13c004.tar.bz2 build_soong-30485c920c64880856c58bc33e2b45109a13c004.zip |
Use llvm-{objcopy,strip} when clang lld is used.
* Pass LLVM_{OBJCOPY,STRIP} to makefile rules.
* strip.sh is used only in soong-only mode.
It will use llvm-strip and and llvm-objcopy
when --use-llvm-strip is given.
Keep flags of strip.sh in alphabetic order.
* Tested build/soong/scripts/build-ndk-prebuilts.sh
by adding "UseClangLld": true, to soong.variables file.
Bug: 80093681
Test: build/soong/scripts/build-ndk-prebuilts.sh
Change-Id: I612267304eea434c7a33cc086b27b577d5f64094
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/strip.sh | 70 |
1 files changed, 57 insertions, 13 deletions
diff --git a/scripts/strip.sh b/scripts/strip.sh index 318c7ad5..432582fc 100755 --- a/scripts/strip.sh +++ b/scripts/strip.sh @@ -17,15 +17,17 @@ # Script to handle the various ways soong may need to strip binaries # Inputs: # Environment: +# CLANG_BIN: path to the clang bin directory # CROSS_COMPILE: prefix added to readelf, objcopy tools # XZ: path to the xz binary # Arguments: # -i ${file}: input file (required) # -o ${file}: output file (required) # -d ${file}: deps file (required) -# --keep-symbols -# --keep-mini-debug-info # --add-gnu-debuglink +# --keep-mini-debug-info +# --keep-symbols +# --use-llvm-strip OPTSTRING=d:i:o:-: @@ -33,25 +35,52 @@ usage() { cat <<EOF Usage: strip.sh [options] -i in-file -o out-file -d deps-file Options: - --keep-symbols Keep symbols in out-file - --keep-mini-debug-info Keep compressed debug info in out-file --add-gnu-debuglink Add a gnu-debuglink section to out-file + --keep-mini-debug-info Keep compressed debug info in out-file + --keep-symbols Keep symbols in out-file + --use-llvm-strip Use llvm-{strip,objcopy} instead of strip/objcopy EOF exit 1 } +# With --use-llvm-strip, GNU strip is replaced with llvm-strip to work around +# old GNU strip bug on lld output files, b/80093681. +# Similary, calls to objcopy are replaced with llvm-objcopy, +# with some exceptions. + do_strip() { - "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp" + # ${CROSS_COMPILE}strip --strip-all does not strip .ARM.attributes, + # so we tell llvm-strip to keep it too. + if [ ! -z "${use_llvm_strip}" ]; then + "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes "${infile}" "${outfile}.tmp" + else + "${CROSS_COMPILE}strip" --strip-all "${infile}" -o "${outfile}.tmp" + fi } do_strip_keep_symbols() { - "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" \ - `"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "-R " $2}' | xargs` + # Maybe we should replace this objcopy with llvm-objcopy, but + # we have not found a use case that is broken by objcopy yet. + REMOVE_SECTIONS=`"${CROSS_COMPILE}readelf" -S "${infile}" | awk '/.debug_/ {print "--remove-section " $2}' | xargs` + if [ ! -z "${use_llvm_strip}" ]; then + "${CROSS_COMPILE}objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} + else + "${CLANG_BIN}/llvm-objcopy" "${infile}" "${outfile}.tmp" ${REMOVE_SECTIONS} + fi } do_strip_keep_mini_debug_info() { rm -f "${outfile}.dynsyms" "${outfile}.funcsyms" "${outfile}.keep_symbols" "${outfile}.debug" "${outfile}.mini_debuginfo" "${outfile}.mini_debuginfo.xz" - if "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp"; then + if [ ! -z "${use_llvm_strip}" ]; then + "${CLANG_BIN}/llvm-strip" --strip-all -keep=.ARM.attributes -remove-section=.comment "${infile}" "${outfile}.tmp" + else + "${CROSS_COMPILE}strip" --strip-all -R .comment "${infile}" -o "${outfile}.tmp" + fi + if [ "$?" == "0" ]; then + # Current prebult llvm-objcopy does not support the following flags: + # --only-keep-debug --rename-section --keep-symbols + # For the following use cases, ${CROSS_COMPILE}objcopy does fine with lld linked files, + # except the --add-section flag. "${CROSS_COMPILE}objcopy" --only-keep-debug "${infile}" "${outfile}.debug" "${CROSS_COMPILE}nm" -D "${infile}" --format=posix --defined-only | awk '{ print $$1 }' | sort >"${outfile}.dynsyms" "${CROSS_COMPILE}nm" "${infile}" --format=posix --defined-only | awk '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > "${outfile}.funcsyms" @@ -61,14 +90,22 @@ do_strip_keep_mini_debug_info() { "${CROSS_COMPILE}objcopy" -S --remove-section .gdb_index --remove-section .comment --keep-symbols="${outfile}.keep_symbols" "${outfile}.mini_debuginfo" "${CROSS_COMPILE}objcopy" --rename-section saved_debug_frame=.debug_frame "${outfile}.mini_debuginfo" "${XZ}" "${outfile}.mini_debuginfo" - "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" + if [ ! -z "${use_llvm_strip}" ]; then + "${CLANG_BIN}/llvm-objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" + else + "${CROSS_COMPILE}objcopy" --add-section .gnu_debugdata="${outfile}.mini_debuginfo.xz" "${outfile}.tmp" + fi else cp -f "${infile}" "${outfile}.tmp" fi } do_add_gnu_debuglink() { - "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" + if [ ! -z "${use_llvm_strip}" ]; then + "${CLANG_BIN}/llvm-objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" + else + "${CROSS_COMPILE}objcopy" --add-gnu-debuglink="${infile}" "${outfile}.tmp" + fi } while getopts $OPTSTRING opt; do @@ -78,9 +115,10 @@ while getopts $OPTSTRING opt; do o) outfile="${OPTARG}" ;; -) case "${OPTARG}" in - keep-symbols) keep_symbols=true ;; - keep-mini-debug-info) keep_mini_debug_info=true ;; add-gnu-debuglink) add_gnu_debuglink=true ;; + keep-mini-debug-info) keep_mini_debug_info=true ;; + keep-symbols) keep_symbols=true ;; + use-llvm-strip) use_llvm_strip=true ;; *) echo "Unknown option --${OPTARG}"; usage ;; esac;; ?) usage ;; @@ -130,12 +168,18 @@ fi rm -f "${outfile}" mv "${outfile}.tmp" "${outfile}" +if [ ! -z "${use_llvm_strip}" ]; then + USED_STRIP_OBJCOPY="${CLANG_BIN}/llvm-strip ${CLANG_BIN}/llvm-objcopy" +else + USED_STRIP_OBJCOPY="${CROSS_COMPILE}strip" +fi + cat <<EOF > "${depsfile}" ${outfile}: \ ${infile} \ ${CROSS_COMPILE}nm \ ${CROSS_COMPILE}objcopy \ ${CROSS_COMPILE}readelf \ - ${CROSS_COMPILE}strip + ${USED_STRIP_OBJCOPY} EOF |