python-pathvalidate: bump version to 0.14.1
[buildroot-gz.git] / support / scripts / check-host-rpath
blob6ce547cb042388d82718e4cc6c5efa211c60f507
1 #!/usr/bin/env bash
3 # This script scans $(HOST_DIR)/{bin,sbin} for all ELF files, and checks
4 # they have an RPATH to $(HOST_DIR)/usr/lib if they need libraries from
5 # there.
7 # Override the user's locale so we are sure we can parse the output of
8 # readelf(1) and file(1)
9 export LC_ALL=C
11 main() {
12 local pkg="${1}"
13 local hostdir="${2}"
14 local file ret
16 # Remove duplicate and trailing '/' for proper match
17 hostdir="$( sed -r -e 's:/+:/:g; s:/$::;' <<<"${hostdir}" )"
19 ret=0
20 while read file; do
21 elf_needs_rpath "${file}" "${hostdir}" || continue
22 check_elf_has_rpath "${file}" "${hostdir}" && continue
23 if [ ${ret} -eq 0 ]; then
24 ret=1
25 printf "***\n"
26 printf "*** ERROR: package %s installs executables without proper RPATH:\n" "${pkg}"
28 printf "*** %s\n" "${file}"
29 done < <( find "${hostdir}"/{,usr/}{bin,sbin} -type f -exec file {} + 2>/dev/null \
30 |sed -r -e '/^([^:]+):.*\<ELF\>.*\<executable\>.*/!d' \
31 -e 's//\1/' \
34 return ${ret}
37 elf_needs_rpath() {
38 local file="${1}"
39 local hostdir="${2}"
40 local lib
42 while read lib; do
43 [ -e "${hostdir}/usr/lib/${lib}" ] && return 0
44 done < <( readelf -d "${file}" \
45 |sed -r -e '/^.* \(NEEDED\) .*Shared library: \[(.+)\]$/!d;' \
46 -e 's//\1/;' \
49 return 1
52 check_elf_has_rpath() {
53 local file="${1}"
54 local hostdir="${2}"
55 local rpath dir
57 while read rpath; do
58 for dir in ${rpath//:/ }; do
59 # Remove duplicate and trailing '/' for proper match
60 dir="$( sed -r -e 's:/+:/:g; s:/$::;' <<<"${dir}" )"
61 [ "${dir}" = "${hostdir}/usr/lib" ] && return 0
62 done
63 done < <( readelf -d "${file}" \
64 |sed -r -e '/.* \(R(UN)?PATH\) +Library r(un)?path: \[(.+)\]$/!d' \
65 -e 's//\3/;' \
68 return 1
71 main "${@}"