summaryrefslogtreecommitdiffstats
path: root/scripts/check-glibc.sh
blob: b02cd75a08eb87903270e0fa12bec26264573152 (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
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
# Gate a release on the glibc the target actually has.
#
# The dev host's 32-bit glibc is newer than Debian 12's, so a dependency can
# silently raise the required symbol version. Without this check the failure
# only shows up as "version `GLIBC_2.38' not found" on the target machine.
#
# Weak undefined symbols are ignored: they resolve to NULL on older libc and the
# code paths using them fall back. Rust's std does this for glibc 2.39's
# pidfd_spawnp/pidfd_getpid, for example.
set -euo pipefail

binary=${1:?usage: check-glibc.sh <binary> [ceiling, default 2.36]}
ceiling=${2:-2.36}

if [ ! -f "$binary" ]; then
    echo "check-glibc: no such file: $binary" >&2
    exit 2
fi

required=$(readelf --dyn-syms --wide "$binary" 2>/dev/null \
    | awk '$5 != "WEAK" { print }' \
    | grep -oE 'GLIBC_[0-9]+\.[0-9]+' \
    | sed 's/^GLIBC_//' \
    | sort -Vu \
    | tail -1 || true)
weak=$(readelf --dyn-syms --wide "$binary" 2>/dev/null \
    | awk '$5 == "WEAK" { print }' \
    | grep -oE 'GLIBC_[0-9]+\.[0-9]+' \
    | sed 's/^GLIBC_//' \
    | sort -Vu \
    | tail -1 || true)

if [ -z "${required:-}" ]; then
    echo "check-glibc: no versioned symbols (statically linked?) — nothing to check"
    exit 0
fi

echo "check-glibc: required GLIBC_$required, ceiling GLIBC_$ceiling"
if [ -n "${weak:-}" ] && [ "$weak" != "$required" ]; then
    echo "check-glibc: weak GLIBC_$weak (optional, resolves to NULL on older libc)"
fi

if [ "$(printf '%s\n%s\n' "$ceiling" "$required" | sort -V | tail -1)" != "$ceiling" ]; then
    echo "check-glibc: FAIL — binary needs GLIBC_$required, target has GLIBC_$ceiling" >&2
    exit 1
fi

echo "check-glibc: ok"