about summary refs log tree commit diff
path: root/pkgs/build-support/setup-hooks/separate-debug-info.sh
blob: 636918992090688a3ff93c0f8b77a56da98b164d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export NIX_LDFLAGS+=" --build-id"
export NIX_CFLAGS_COMPILE+=" -ggdb"
dontStrip=1

fixupOutputHooks+=(_separateDebugInfo)

_separateDebugInfo() {
    local dst="${debug:-$out}"
    if [ "$prefix" = "$dst" ]; then return; fi

    dst="$dst/lib/debug/.build-id"

    # Find executables and dynamic libraries.
    local -a files=($(find "$prefix" -type f -a \( -perm /0100 -o -name "*.so" -o -name "*.so.*" \)))

    local i magic
    for i in "${files[@]}"; do
        # Skip non-ELF files.
        exec 10< "$i"
        read -n 4 -u 10 magic
        if [[ "$magic" =~ ELF ]]; then echo FOO; fi
        exec 10<&-

        # Extract the Build ID. FIXME: there's probably a cleaner way.
        local id="$(readelf -n "$i" | sed 's/.*Build ID: \([0-9a-f]*\).*/\1/; t; d')"
        if [ "${#id}" != 40 ]; then
            echo "could not find build ID of $i, skipping" >&2
            continue
        fi

        # Extract the debug info.
        header "separating debug info from $i (build ID $id)"
        mkdir -p "$dst/${id:0:2}"
        objcopy --only-keep-debug "$i" "$dst/${id:0:2}/${id:2}.debug"
        strip --strip-debug "$i"
    done
}