summaryrefslogtreecommitdiff
path: root/.github/scripts/armhf-static-ldd.sh
blob: 50cecc613bbafb5c049078634375c09a0ae4158c (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
set -euo pipefail

# Resolve DT_NEEDED entries from the clean ARMHF runtime without executing the
# target, so linuxdeploy sees the runtime prefix first and a libquadmath
# dependency fails; the AppImage startup smoke test proves runtime behavior.

if [ "$#" -ne 1 ]; then
  echo "Usage: armhf-static-ldd.sh ELF" >&2
  exit 2
fi

target=$1
runtime_prefix=${WSJT_ARMHF_RUNTIME_PREFIX:-/opt/wsjtx/armhf-runtime}
search_path=${ARMHF_STATIC_LDD_LIBRARY_PATH:-}

base_search_directories=("$runtime_prefix")
if [ -n "$search_path" ]; then
  IFS=: read -r -a configured_directories <<< "$search_path"
  base_search_directories+=("${configured_directories[@]}")
fi

status=0
declare -A resolved_libraries=()

resolve_dependencies() {
  local elf=$1 dynamic origin line directory library resolved metadata
  local -a needed=() encoded_directories=() search_directories=()

  dynamic="$(readelf -dW -- "$elf")" || exit
  origin="$(dirname -- "$elf")"
  while IFS= read -r line; do
    if [[ "$line" =~ \(NEEDED\).*Shared[[:space:]]library:[[:space:]]\[([^]]+)\] ]]; then
      needed+=("${BASH_REMATCH[1]}")
    elif [[ "$line" =~ \((RUNPATH|RPATH)\).*Library[[:space:]](runpath|rpath):[[:space:]]\[([^]]+)\] ]]; then
      IFS=: read -r -a encoded <<< "${BASH_REMATCH[3]}"
      for directory in "${encoded[@]}"; do
        # shellcheck disable=SC2016 # expand the loader's literal ORIGIN token
        directory=${directory//'${ORIGIN}'/$origin}
        # shellcheck disable=SC2016 # expand the loader's literal ORIGIN token
        directory=${directory//'$ORIGIN'/$origin}
        encoded_directories+=("$directory")
      done
    fi
  done <<< "$dynamic"
  search_directories=(
    "${base_search_directories[@]}"
    "${encoded_directories[@]}"
    /lib/arm-linux-gnueabihf
    /usr/lib/arm-linux-gnueabihf
    /lib
    /usr/lib
  )

  for library in "${needed[@]}"; do
    if [[ "$library" == libquadmath.so* ]]; then
      echo "$elf unexpectedly depends on unavailable ARMHF runtime $library" >&2
      exit 1
    fi
    if [ -n "${resolved_libraries[$library]:-}" ]; then
      continue
    fi

    resolved=
    if [[ "$library" == */* ]] && [ -f "$library" ]; then
      resolved=$library
    else
      for directory in "${search_directories[@]}"; do
        if [ -f "$directory/$library" ]; then
          resolved=$directory/$library
          break
        fi
      done
    fi

    if [ -z "$resolved" ]; then
      printf '%s => not found (0x00000000)\n' "$library"
      status=1
      continue
    fi

    metadata="$(file -L -- "$resolved")" || exit
    case "$metadata" in
      *"ELF 32-bit"*"ARM, EABI5"*) ;;
      *)
        echo "$elf resolves $library to a non-ARMHF ELF: $metadata" >&2
        exit 1
        ;;
    esac
    resolved_libraries[$library]=$resolved
    printf '%s => %s (0x00000000)\n' "$library" "$resolved"
    resolve_dependencies "$resolved"
  done
}

resolve_dependencies "$target"

exit "$status"