1b71c9ffbSNicholas Piggin#!/bin/sh
22874c5fdSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-or-later
3b71c9ffbSNicholas Piggin
4b71c9ffbSNicholas Piggin# Copyright © 2015 IBM Corporation
5b71c9ffbSNicholas Piggin
6b71c9ffbSNicholas Piggin
7b71c9ffbSNicholas Piggin# This script checks the relocations of a vmlinux for "suspicious"
8b71c9ffbSNicholas Piggin# relocations.
9b71c9ffbSNicholas Piggin
10b71c9ffbSNicholas Piggin# based on relocs_check.pl
11b71c9ffbSNicholas Piggin# Copyright © 2009 IBM Corporation
12b71c9ffbSNicholas Piggin
1343e76cd3SAlexandre Ghitiif [ $# -lt 3 ]; then
1443e76cd3SAlexandre Ghiti	echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2
15b71c9ffbSNicholas Piggin	exit 1
16b71c9ffbSNicholas Pigginfi
17b71c9ffbSNicholas Piggin
1843e76cd3SAlexandre Ghiti# Have Kbuild supply the path to objdump and nm so we handle cross compilation.
19b71c9ffbSNicholas Pigginobjdump="$1"
2043e76cd3SAlexandre Ghitinm="$2"
2143e76cd3SAlexandre Ghitivmlinux="$3"
2243e76cd3SAlexandre Ghiti
2343e76cd3SAlexandre Ghiti# Remove from the bad relocations those that match an undefined weak symbol
2443e76cd3SAlexandre Ghiti# which will result in an absolute relocation to 0.
2543e76cd3SAlexandre Ghiti# Weak unresolved symbols are of that form in nm output:
2643e76cd3SAlexandre Ghiti# "                  w _binary__btf_vmlinux_bin_end"
2743e76cd3SAlexandre Ghitiundef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }')
28b71c9ffbSNicholas Piggin
29b71c9ffbSNicholas Pigginbad_relocs=$(
30e44ff9eaSMichael Ellerman$objdump -R "$vmlinux" |
31b71c9ffbSNicholas Piggin	# Only look at relocation lines.
32b71c9ffbSNicholas Piggin	grep -E '\<R_' |
33b71c9ffbSNicholas Piggin	# These relocations are okay
34b71c9ffbSNicholas Piggin	# On PPC64:
35b71c9ffbSNicholas Piggin	#	R_PPC64_RELATIVE, R_PPC64_NONE
36b71c9ffbSNicholas Piggin	# On PPC:
37b71c9ffbSNicholas Piggin	#	R_PPC_RELATIVE, R_PPC_ADDR16_HI,
38b71c9ffbSNicholas Piggin	#	R_PPC_ADDR16_HA,R_PPC_ADDR16_LO,
39b71c9ffbSNicholas Piggin	#	R_PPC_NONE
40b71c9ffbSNicholas Piggin	grep -F -w -v 'R_PPC64_RELATIVE
41b71c9ffbSNicholas PigginR_PPC64_NONE
42*d7997691SAlexey KardashevskiyR_PPC64_UADDR64
43b71c9ffbSNicholas PigginR_PPC_ADDR16_LO
44b71c9ffbSNicholas PigginR_PPC_ADDR16_HI
45b71c9ffbSNicholas PigginR_PPC_ADDR16_HA
46b71c9ffbSNicholas PigginR_PPC_RELATIVE
47b71c9ffbSNicholas PigginR_PPC_NONE' |
4843e76cd3SAlexandre Ghiti	([ "$undef_weak_symbols" ] && grep -F -w -v "$undef_weak_symbols" || cat)
49b71c9ffbSNicholas Piggin)
50b71c9ffbSNicholas Piggin
51b71c9ffbSNicholas Pigginif [ -z "$bad_relocs" ]; then
52b71c9ffbSNicholas Piggin	exit 0
53b71c9ffbSNicholas Pigginfi
54b71c9ffbSNicholas Piggin
55b71c9ffbSNicholas Pigginnum_bad=$(echo "$bad_relocs" | wc -l)
56b71c9ffbSNicholas Pigginecho "WARNING: $num_bad bad relocations"
57b71c9ffbSNicholas Pigginecho "$bad_relocs"
58