1*4882a593Smuzhiyun#!/bin/sh 2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-or-later 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun# Copyright © 2015 IBM Corporation 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun# This script checks the relocations of a vmlinux for "suspicious" 8*4882a593Smuzhiyun# relocations. 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun# based on relocs_check.pl 11*4882a593Smuzhiyun# Copyright © 2009 IBM Corporation 12*4882a593Smuzhiyun 13*4882a593Smuzhiyunif [ $# -lt 3 ]; then 14*4882a593Smuzhiyun echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2 15*4882a593Smuzhiyun exit 1 16*4882a593Smuzhiyunfi 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun# Have Kbuild supply the path to objdump and nm so we handle cross compilation. 19*4882a593Smuzhiyunobjdump="$1" 20*4882a593Smuzhiyunnm="$2" 21*4882a593Smuzhiyunvmlinux="$3" 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun# Remove from the bad relocations those that match an undefined weak symbol 24*4882a593Smuzhiyun# which will result in an absolute relocation to 0. 25*4882a593Smuzhiyun# Weak unresolved symbols are of that form in nm output: 26*4882a593Smuzhiyun# " w _binary__btf_vmlinux_bin_end" 27*4882a593Smuzhiyunundef_weak_symbols=$($nm "$vmlinux" | awk '$1 ~ /w/ { print $2 }') 28*4882a593Smuzhiyun 29*4882a593Smuzhiyunbad_relocs=$( 30*4882a593Smuzhiyun$objdump -R "$vmlinux" | 31*4882a593Smuzhiyun # Only look at relocation lines. 32*4882a593Smuzhiyun grep -E '\<R_' | 33*4882a593Smuzhiyun # These relocations are okay 34*4882a593Smuzhiyun # On PPC64: 35*4882a593Smuzhiyun # R_PPC64_RELATIVE, R_PPC64_NONE 36*4882a593Smuzhiyun # On PPC: 37*4882a593Smuzhiyun # R_PPC_RELATIVE, R_PPC_ADDR16_HI, 38*4882a593Smuzhiyun # R_PPC_ADDR16_HA,R_PPC_ADDR16_LO, 39*4882a593Smuzhiyun # R_PPC_NONE 40*4882a593Smuzhiyun grep -F -w -v 'R_PPC64_RELATIVE 41*4882a593SmuzhiyunR_PPC64_NONE 42*4882a593SmuzhiyunR_PPC_ADDR16_LO 43*4882a593SmuzhiyunR_PPC_ADDR16_HI 44*4882a593SmuzhiyunR_PPC_ADDR16_HA 45*4882a593SmuzhiyunR_PPC_RELATIVE 46*4882a593SmuzhiyunR_PPC_NONE' | 47*4882a593Smuzhiyun ([ "$undef_weak_symbols" ] && grep -F -w -v "$undef_weak_symbols" || cat) 48*4882a593Smuzhiyun) 49*4882a593Smuzhiyun 50*4882a593Smuzhiyunif [ -z "$bad_relocs" ]; then 51*4882a593Smuzhiyun exit 0 52*4882a593Smuzhiyunfi 53*4882a593Smuzhiyun 54*4882a593Smuzhiyunnum_bad=$(echo "$bad_relocs" | wc -l) 55*4882a593Smuzhiyunecho "WARNING: $num_bad bad relocations" 56*4882a593Smuzhiyunecho "$bad_relocs" 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun# If we see this type of relocation it's an idication that 59*4882a593Smuzhiyun# we /may/ be using an old version of binutils. 60*4882a593Smuzhiyunif echo "$bad_relocs" | grep -q -F -w R_PPC64_UADDR64; then 61*4882a593Smuzhiyun echo "WARNING: You need at least binutils >= 2.19 to build a CONFIG_RELOCATABLE kernel" 62*4882a593Smuzhiyunfi 63