xref: /linux/arch/powerpc/tools/unrel_branch_check.sh (revision 3d97abbc9f6fe90973551f3e3eef47ffef863114)
1d9de6b0dSStephen Rothwell#!/bin/bash
24ea80652SNicholas Piggin# Copyright © 2016 IBM Corporation
34ea80652SNicholas Piggin#
44ea80652SNicholas Piggin# This program is free software; you can redistribute it and/or
54ea80652SNicholas Piggin# modify it under the terms of the GNU General Public License
64ea80652SNicholas Piggin# as published by the Free Software Foundation; either version
74ea80652SNicholas Piggin# 2 of the License, or (at your option) any later version.
84ea80652SNicholas Piggin#
94ea80652SNicholas Piggin# This script checks the relocations of a vmlinux for "suspicious"
104ea80652SNicholas Piggin# branches from unrelocated code (head_64.S code).
114ea80652SNicholas Piggin
124ea80652SNicholas Piggin# Turn this on if you want more debug output:
134ea80652SNicholas Piggin# set -x
144ea80652SNicholas Piggin
154ea80652SNicholas Piggin# Have Kbuild supply the path to objdump so we handle cross compilation.
164ea80652SNicholas Pigginobjdump="$1"
174ea80652SNicholas Pigginvmlinux="$2"
184ea80652SNicholas Piggin
194ea80652SNicholas Piggin#__end_interrupts should be located within the first 64K
2020ff8ec1SStephen Rothwellkstart=0xc000000000000000
2120ff8ec1SStephen Rothwellprintf -v kend '0x%x' $(( kstart + 0x10000 ))
224ea80652SNicholas Piggin
234ea80652SNicholas Pigginend_intr=0x$(
2420ff8ec1SStephen Rothwell$objdump -R -d --start-address="$kstart" --stop-address="$kend" "$vmlinux" |
2520ff8ec1SStephen Rothwellawk '$2 == "<__end_interrupts>:" { print $1 }'
264ea80652SNicholas Piggin)
274ea80652SNicholas Piggin
284ea80652SNicholas PigginBRANCHES=$(
294e71106cSStephen Rothwell$objdump -R -D --no-show-raw-insn --start-address="$kstart" --stop-address="$end_intr" "$vmlinux" |
30*3d97abbcSStephen Rothwellsed -E -n '
31*3d97abbcSStephen Rothwell# match lines that start with a kernel address
32*3d97abbcSStephen Rothwell/^c[0-9a-f]*:\s*b/ {
33*3d97abbcSStephen Rothwell	# drop a target that we do not care about
34*3d97abbcSStephen Rothwell	/\<__start_initialization_multiplatform>/d
35*3d97abbcSStephen Rothwell	# drop branches via ctr or lr
36*3d97abbcSStephen Rothwell	/\<b.?.?(ct|l)r/d
37*3d97abbcSStephen Rothwell	# cope with some differences between Clang and GNU objdumps
38*3d97abbcSStephen Rothwell	s/\<bt.?\s*[[:digit:]]+,/beq/
39*3d97abbcSStephen Rothwell	s/\<bf.?\s*[[:digit:]]+,/bne/
40*3d97abbcSStephen Rothwell	# tidy up
41*3d97abbcSStephen Rothwell	s/\s0x/ /
42*3d97abbcSStephen Rothwell	s/://
43*3d97abbcSStephen Rothwell	# format for the loop below
44*3d97abbcSStephen Rothwell	s/^(\S+)\s+(\S+)\s+(\S+)\s*(\S*).*$/\1:\2:0x\3:\4/
45*3d97abbcSStephen Rothwell	# strip out condition registers
46*3d97abbcSStephen Rothwell	s/:0xcr[0-7],/:0x/
47*3d97abbcSStephen Rothwell	p
48*3d97abbcSStephen Rothwell}'
494ea80652SNicholas Piggin)
504ea80652SNicholas Piggin
5120ff8ec1SStephen Rothwellfor tuple in $BRANCHES; do
52d9de6b0dSStephen Rothwell	from=$(echo "$tuple" | cut -d':' -f1)
53d9de6b0dSStephen Rothwell	branch=$(echo "$tuple" | cut -d':' -f2)
54*3d97abbcSStephen Rothwell	to=$(echo "$tuple" | cut -d':' -f3)
55d9de6b0dSStephen Rothwell	sym=$(echo "$tuple" | cut -d':' -f4)
564ea80652SNicholas Piggin
5720ff8ec1SStephen Rothwell	if (( to > end_intr )); then
584ea80652SNicholas Piggin		if [ -z "$bad_branches" ]; then
594ea80652SNicholas Piggin			echo "WARNING: Unrelocated relative branches"
604ea80652SNicholas Piggin			bad_branches="yes"
614ea80652SNicholas Piggin		fi
624ea80652SNicholas Piggin		echo "$from $branch-> $to $sym"
634ea80652SNicholas Piggin	fi
644ea80652SNicholas Piggindone
654ea80652SNicholas Piggin
664ea80652SNicholas Pigginif [ -z "$bad_branches" ]; then
674ea80652SNicholas Piggin	exit 0
684ea80652SNicholas Pigginfi
69