#!/usr/bin/env bash BINARY_PATH=$1 BINARY_NAME=$(basename $BINARY_PATH) DESTINATION_STRIPPED_DIR=$2 OBJCOPY_PATH=${3:objcopy} READELF_PATH=${4:readelf} BUILD_ID=$($READELF_PATH -n $1 | sed -n '/Build ID/ { s/.*: //p; q; }') BUILD_ID_PREFIX=${BUILD_ID:0:2} BUILD_ID_SUFFIX=${BUILD_ID:2} TEMP_BINARY_PATH="${BINARY_PATH}_temp" DESTINATION_DEBUG_INFO_DIR="$DESTINATION_STRIPPED_DIR/lib/debug/.build-id" DESTINATION_STRIP_BINARY_DIR="$DESTINATION_STRIPPED_DIR/bin" mkdir -p "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX" mkdir -p "$DESTINATION_STRIP_BINARY_DIR" $OBJCOPY_PATH --only-keep-debug "$BINARY_PATH" "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" touch "$TEMP_BINARY_PATH" $OBJCOPY_PATH --add-gnu-debuglink "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" "$BINARY_PATH" "$TEMP_BINARY_PATH" $OBJCOPY_PATH --strip-all "$TEMP_BINARY_PATH" "$DESTINATION_STRIP_BINARY_DIR/$BINARY_NAME" rm -f "$TEMP_BINARY_PATH"