aboutsummaryrefslogtreecommitdiff
path: root/tools/build/check-links.sh
blob: 06aa8d31d28e420371b7b2ccb78bc3083f5eeb40 (plain) (blame)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/sh
# $FreeBSD$

libkey() {
	libkey="lib_symbols_$1"
	patterns=[.+,/-]
	replacement=_
	while :; do
		case " ${libkey} " in
			*${patterns}*)
				libkey="${libkey%%${patterns}*}${replacement}${libkey#*${patterns}}"
				;;
			*)
				break
				;;
		esac
	done
	return 0
}

usage() {
	cat <<-EOF
	usage: $0 [-Uv] [-L LD_LIBRARY_PATH] file
	       -L:       Specify an alternative LD_LIBRARY_PATH for the library resolution.
	       -U:       Skip looking for unresolved symbols.
	       -v:       Show which library each symbol is resolved to.
	EOF
	exit 0
}

ret=0
CHECK_UNRESOLVED=1
VERBOSE_RESOLVED=0
while getopts "L:Uv" flag; do
	case "${flag}" in
		L) LIB_PATH="${OPTARG}" ;;
		U) CHECK_UNRESOLVED=0 ;;
		v) VERBOSE_RESOLVED=1 ;;
		*) usage ;;
	esac
done
shift $((OPTIND-1))

if ! [ -f "$1" ]; then
	echo "No such file or directory: $1" >&2
	exit 1
fi

mime=$(file -L --mime-type $1)
isbin=0
case $mime in
*application/x-executable) isbin=1 ;;
*application/x-sharedlib);;
*) echo "Not an elf file" >&2 ; exit 1;;
esac

# Gather all symbols from the target
unresolved_symbols=$(nm -u -D --format=posix "$1" | awk '$2 == "U" {print $1}' | tr '\n' ' ')
[ ${isbin} -eq 1 ] && bss_symbols=$(nm -D --format=posix "$1" | awk '$2 == "B" && $4 != "" {print $1}' | tr '\n' ' ')
if [ -n "${LIB_PATH}" ]; then
	for libc in /lib/libc.so.*; do
		LDD_ENV="LD_PRELOAD=${libc}"
	done
	LDD_ENV="${LDD_ENV} LD_LIBRARY_PATH=${LIB_PATH}"
fi

ldd_libs=$(env ${LDD_ENV} ldd $(realpath $1) | awk '{print $1 ":" $3}')

# Check for useful libs
list_libs=
resolved_symbols=
for lib in $(readelf -d $1 | awk '$2 ~ /\(?NEEDED\)?/ { sub(/\[/,"",$NF); sub(/\]/,"",$NF); print $NF }'); do
	echo -n "checking if $lib is needed: "
	if [ -n "${lib##/*}" ]; then
		for libpair in ${ldd_libs}; do
			case "${libpair}" in
				${lib}:*) libpath="${libpair#*:}" && break ;;
			esac
		done
	else
		libpath="${lib}"
	fi
	list_libs="$list_libs $lib"
	foundone=
	lib_symbols="$(nm -D --defined-only --format=posix "${libpath}" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
	if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
		# Save the global symbols for this lib
		libkey "${lib}"
		setvar "${libkey}" "${lib_symbols}"
	fi
	for fct in ${lib_symbols}; do
		case " ${unresolved_symbols} ${bss_symbols} " in
			*\ ${fct}\ *) foundone="${fct}" && break ;;
		esac
	done
	if [ -n "${foundone}" ]; then
		echo "yes... ${foundone}"
	else
		echo "no"
		ret=1
	fi
done

if [ ${CHECK_UNRESOLVED} -eq 1 ]; then
	# Add in crt1 symbols
	list_libs="${list_libs} crt1.o"
	lib_symbols="$(nm --defined-only --format=posix "/usr/lib/crt1.o" | awk '$2 ~ /C|R|D|T|W|B|V/ {print $1}' | tr '\n' ' ')"
	# Save the global symbols for this lib
	libkey "crt1.o"
	setvar "${libkey}" "${lib_symbols}"

	# Now search libs for all symbols and report missing ones.
	for sym in ${unresolved_symbols}; do
		found=0
		for lib in ${list_libs}; do
			libkey "${lib}"
			eval "lib_symbols=\"\${${libkey}}\""
			# lib_symbols now contains symbols for the lib.
			case " ${lib_symbols} " in
				*\ ${sym}\ *)
					[ ${VERBOSE_RESOLVED} -eq 1 ] &&
					    echo "Resolved symbol ${sym} from ${lib}"
					found=1
					break
					;;
			esac
		done
		if [ $found -eq 0 ]; then
			echo "Unresolved symbol $sym"
			ret=1
		fi
	done
fi

exit ${ret}