]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/build/check-links.sh
Integrate tools/regression/execve into the FreeBSD test suite as
[FreeBSD/FreeBSD.git] / tools / build / check-links.sh
1 #!/bin/sh
2 # $FreeBSD$
3
4 libkey() {
5         libkey="lib_symbols_$1"
6         patterns=[.+,/-]
7         replacement=_
8         while :; do
9                 case " ${libkey} " in
10                         *${patterns}*)
11                                 libkey="${libkey%%${patterns}*}${replacement}${libkey#*${patterns}}"
12                                 ;;
13                         *)
14                                 break
15                                 ;;
16                 esac
17         done
18         return 0
19 }
20
21 ret=0
22 CHECK_UNRESOLVED=1
23 while getopts "U" flag; do
24         case "${flag}" in
25                 U) CHECK_UNRESOLVED=0 ;;
26         esac
27 done
28 shift $((OPTIND-1))
29
30 mime=$(file -L --mime-type $1)
31 isbin=0
32 case $mime in
33 *application/x-executable) isbin=1 ;;
34 *application/x-sharedlib);;
35 *) echo "Not an elf file" >&2 ; exit 1;;
36 esac
37
38 # Gather all symbols from the target
39 unresolved_symbols=$(nm -u -D --format=posix "$1" | awk '$2 == "U" {print $1}' | tr '\n' ' ')
40 [ ${isbin} -eq 1 ] && bss_symbols=$(nm -D --format=posix "$1" | awk '$2 == "B" && $4 != "" {print $1}' | tr '\n' ' ')
41 ldd_libs=$(ldd $(realpath $1) | awk '{print $1 ":" $3}')
42
43 # Check for useful libs
44 list_libs=
45 resolved_symbols=
46 for lib in $(readelf -d $1 | awk '$2 ~ /\(?NEEDED\)?/ { sub(/\[/,"",$NF); sub(/\]/,"",$NF); print $NF }'); do
47         echo -n "checking if $lib is needed: "
48         if [ -n "${lib##/*}" ]; then
49                 for libpair in ${ldd_libs}; do
50                         case "${libpair}" in
51                                 ${lib}:*) libpath="${libpair#*:}" && break ;;
52                         esac
53                 done
54         else
55                 libpath="${lib}"
56         fi
57         list_libs="$list_libs $lib"
58         foundone=
59         lib_symbols="$(nm -D --defined-only --format=posix "${libpath}" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
60         if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
61                 # Save the global symbols for this lib
62                 libkey "${lib}"
63                 setvar "${libkey}" "${lib_symbols}"
64         fi
65         for fct in ${lib_symbols}; do
66                 case " ${unresolved_symbols} ${bss_symbols} " in
67                         *\ ${fct}\ *) foundone="${fct}" && break ;;
68                 esac
69         done
70         if [ -n "${foundone}" ]; then
71                 echo "yes... ${foundone}"
72         else
73                 echo "no"
74                 ret=1
75         fi
76 done
77
78 if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
79         # Add in crt1 symbols
80         list_libs="${list_libs} crt1.o"
81         lib_symbols="$(nm --defined-only --format=posix "/usr/lib/crt1.o" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
82         # Save the global symbols for this lib
83         libkey "crt1.o"
84         setvar "${libkey}" "${lib_symbols}"
85
86         # No search libs for all symbols and report missing ones.
87         for sym in ${unresolved_symbols}; do
88                 found=0
89                 for lib in ${list_libs}; do
90                         libkey "${lib}"
91                         eval "lib_symbols=\"\${${libkey}}\""
92                         # lib_symbols now contains symbols for the lib.
93                         case " ${lib_symbols} " in
94                                 *\ ${sym}\ *) found=1 && break ;;
95                         esac
96                 done
97                 if [ $found -eq 0 ]; then
98                         echo "Unresolved symbol $sym"
99                         ret=1
100                 fi
101         done
102 fi
103
104 exit ${ret}