18c2ecf20Sopenharmony_ci#!/bin/sh
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci# Copyright © 2015 IBM Corporation
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci# This script checks the relocations of a vmlinux for "suspicious"
88c2ecf20Sopenharmony_ci# relocations.
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci# based on relocs_check.pl
118c2ecf20Sopenharmony_ci# Copyright © 2009 IBM Corporation
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciif [ $# -lt 3 ]; then
148c2ecf20Sopenharmony_ci	echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2
158c2ecf20Sopenharmony_ci	exit 1
168c2ecf20Sopenharmony_cifi
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci# Have Kbuild supply the path to objdump and nm so we handle cross compilation.
198c2ecf20Sopenharmony_ciobjdump="$1"
208c2ecf20Sopenharmony_cinm="$2"
218c2ecf20Sopenharmony_civmlinux="$3"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci# Remove from the bad relocations those that match an undefined weak symbol
248c2ecf20Sopenharmony_ci# which will result in an absolute relocation to 0.
258c2ecf20Sopenharmony_ci# Weak unresolved symbols are of that form in nm output:
268c2ecf20Sopenharmony_ci# "                  w _binary__btf_vmlinux_bin_end"
278c2ecf20Sopenharmony_ciundef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }')
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cibad_relocs=$(
308c2ecf20Sopenharmony_ci$objdump -R "$vmlinux" |
318c2ecf20Sopenharmony_ci	# Only look at relocation lines.
328c2ecf20Sopenharmony_ci	grep -E '\<R_' |
338c2ecf20Sopenharmony_ci	# These relocations are okay
348c2ecf20Sopenharmony_ci	# On PPC64:
358c2ecf20Sopenharmony_ci	#	R_PPC64_RELATIVE, R_PPC64_NONE
368c2ecf20Sopenharmony_ci	# On PPC:
378c2ecf20Sopenharmony_ci	#	R_PPC_RELATIVE, R_PPC_ADDR16_HI,
388c2ecf20Sopenharmony_ci	#	R_PPC_ADDR16_HA,R_PPC_ADDR16_LO,
398c2ecf20Sopenharmony_ci	#	R_PPC_NONE
408c2ecf20Sopenharmony_ci	grep -F -w -v 'R_PPC64_RELATIVE
418c2ecf20Sopenharmony_ciR_PPC64_NONE
428c2ecf20Sopenharmony_ciR_PPC_ADDR16_LO
438c2ecf20Sopenharmony_ciR_PPC_ADDR16_HI
448c2ecf20Sopenharmony_ciR_PPC_ADDR16_HA
458c2ecf20Sopenharmony_ciR_PPC_RELATIVE
468c2ecf20Sopenharmony_ciR_PPC_NONE' |
478c2ecf20Sopenharmony_ci	([ "$undef_weak_symbols" ] && grep -F -w -v "$undef_weak_symbols" || cat)
488c2ecf20Sopenharmony_ci)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciif [ -z "$bad_relocs" ]; then
518c2ecf20Sopenharmony_ci	exit 0
528c2ecf20Sopenharmony_cifi
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cinum_bad=$(echo "$bad_relocs" | wc -l)
558c2ecf20Sopenharmony_ciecho "WARNING: $num_bad bad relocations"
568c2ecf20Sopenharmony_ciecho "$bad_relocs"
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci# If we see this type of relocation it's an idication that
598c2ecf20Sopenharmony_ci# we /may/ be using an old version of binutils.
608c2ecf20Sopenharmony_ciif echo "$bad_relocs" | grep -q -F -w R_PPC64_UADDR64; then
618c2ecf20Sopenharmony_ci	echo "WARNING: You need at least binutils >= 2.19 to build a CONFIG_RELOCATABLE kernel"
628c2ecf20Sopenharmony_cifi
63