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