#!/bin/sh

set -e

BINARY_FILE=$1
DEBUG_FILE="${BINARY_FILE}.debug"

# Sequence made according to dh_strip but using binutils-mingw-w64-x86-64 tools
# https://salsa.debian.org/debian/debhelper/-/blob/main/dh_strip

# Note: --compress-debug-sections is not supported by winedbg and will cause a crash
x86_64-w64-mingw32-objcopy --only-keep-debug "$BINARY_FILE" "$DEBUG_FILE"
chmod 0644 "$DEBUG_FILE"
x86_64-w64-mingw32-strip --remove-section=.comment --remove-section=.note --strip-unneeded "$BINARY_FILE"
x86_64-w64-mingw32-objcopy --add-gnu-debuglink "$DEBUG_FILE" "$BINARY_FILE"


# To debug a program, you can do:
# $ winedbg --gdb --no-start --port 2345 program.exe
# [...]
#
# $ gdb program.exe
# [...]
# (gdb) target remote localhost:2345

# gdb must be used instead of x86_64-w64-mingw32-gdb (else winedbg will have this error:
# 0108:err:winedbg:packet_query Unhandled query "GetTIBAddr:110"
