check-linux-compat.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env bash
  2. # Checks that a Linux LYNX binary runs on the oldest supported distribution (default: Ubuntu 22.04, glibc 2.35):
  3. # no glibc symbol version above MAX_GLIBC, and no dependency on the system's C++ runtime (libstdc++/libgcc_s are
  4. # linked statically, see CMakeLists.txt). The AppImage catalog tests on the oldest Ubuntu LTS still supported.
  5. #
  6. # usage: scripts/check-linux-compat.sh <binary> [max glibc, default 2.35]
  7. set -euo pipefail
  8. BIN="$1"; MAX="${2:-2.35}"
  9. need="$(objdump -T "$BIN" | grep -o 'GLIBC_[0-9][0-9.]*' | sed 's/GLIBC_//' | sort -uV)"
  10. top="$(tail -1 <<< "$need")"
  11. echo "glibc symbol versions: $(tr '\n' ' ' <<< "$need")"
  12. echo "highest: GLIBC_$top (allowed: GLIBC_$MAX)"
  13. fail=0
  14. if [[ "$(printf '%s\n%s\n' "$top" "$MAX" | sort -V | tail -1)" != "$MAX" ]]; then
  15. echo "ERROR: needs GLIBC_$top, newer than GLIBC_$MAX; these symbols:"
  16. objdump -T "$BIN" | grep -o 'GLIBC_[0-9][0-9.]*.*' | tr -d ')' | while read -r v s; do
  17. [[ "$(printf '%s\n%s\n' "${v#GLIBC_}" "$MAX" | sort -V | tail -1)" != "$MAX" ]] && echo " $s $v" || true
  18. done
  19. fail=1
  20. fi
  21. deps="$(objdump -p "$BIN" | awk '/NEEDED/ {print $2}')"
  22. echo "shared libraries: $(tr '\n' ' ' <<< "$deps")"
  23. if grep -qE '^lib(stdc\+\+|gcc_s)\.so' <<< "$deps"; then
  24. echo "ERROR: links the system's C++ runtime (libstdc++/libgcc_s); link it statically"; fail=1
  25. fi
  26. if objdump -T "$BIN" | grep -qE '(GLIBCXX|CXXABI)_[0-9]'; then
  27. echo "ERROR: imports GLIBCXX/CXXABI symbols from the system's libstdc++"; fail=1
  28. fi
  29. # A library (a plugin a host loads with dlopen; not the executable, which has an interpreter) must not ask for static
  30. # TLS: glibc keeps only a small reserve for modules loaded later, and the host then fails to load it with "cannot
  31. # allocate memory in static TLS block" (the 0.1.27 Linux plugins on glibc 2.35, round 190fp). One initial-exec
  32. # thread_local makes the module's whole TLS block static.
  33. if ! readelf -lW "$BIN" | grep -q 'INTERP' && readelf -dW "$BIN" | grep -E '\((FLAGS)\)' | grep -q 'STATIC_TLS'; then
  34. echo "ERROR: a library that asks for static TLS (DF_STATIC_TLS: an initial-exec thread_local); hosts can fail to load it"; fail=1
  35. fi
  36. [[ $fail == 0 ]] && echo "OK: runs with glibc $MAX and no system C++ runtime"
  37. exit $fail