#!/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} 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" cp "$BINARY_PATH" "$DESTINATION_STRIP_BINARY_DIR/$BINARY_NAME" $OBJCOPY_PATH --only-keep-debug --compress-debug-sections "$DESTINATION_STRIP_BINARY_DIR/$BINARY_NAME" "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" chmod 0644 "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" chown 0:0 "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" strip --remove-section=.comment --remove-section=.note "$DESTINATION_STRIP_BINARY_DIR/$BINARY_NAME" $OBJCOPY_PATH --add-gnu-debuglink "$DESTINATION_DEBUG_INFO_DIR/$BUILD_ID_PREFIX/$BUILD_ID_SUFFIX.debug" "$DESTINATION_STRIP_BINARY_DIR/$BINARY_NAME"