Path: blob/master/arch/powerpc/tools/unrel_branch_check.sh
26424 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.0+2# Copyright © 2016,2020 IBM Corporation3#4# This script checks the unrelocated code of a vmlinux for "suspicious"5# branches to relocated code (head_64.S code).67# Have Kbuild supply the path to objdump and nm so we handle cross compilation.8objdump="$1"9nm="$2"10vmlinux="$3"1112kstart=0xc0000000000000001314end_intr=0x$($nm -p "$vmlinux" |15sed -E -n '/\s+[[:alpha:]]\s+__end_interrupts\s*$/{s///p;q}')16if [ "$end_intr" = "0x" ]; then17exit 018fi1920# we know that there is a correct branch to21# __start_initialization_multiplatform, so find its address22# so we can exclude it.23sim=0x$($nm -p "$vmlinux" |24sed -E -n '/\s+[[:alpha:]]\s+__start_initialization_multiplatform\s*$/{s///p;q}')2526$objdump -D --no-show-raw-insn --start-address="$kstart" --stop-address="$end_intr" "$vmlinux" |27sed -E -n '28# match lines that start with a kernel address29/^c[0-9a-f]*:\s*b/ {30# drop branches via ctr or lr31/\<b.?.?(ct|l)r/d32# cope with some differences between Clang and GNU objdumps33s/\<bt.?\s*[[:digit:]]+,/beq/34s/\<bf.?\s*[[:digit:]]+,/bne/35# tidy up36s/\s0x/ /37s/://38# format for the loop below39s/^(\S+)\s+(\S+)\s+(\S+)\s*(\S*).*$/\1:\2:\3:\4/40# strip out condition registers41s/:cr[0-7],/:/42p43}' | {4445all_good=true46while IFS=: read -r from branch to sym; do47case "$to" in48c*) to="0x$to"49;;50.+*)51to=${to#.+}52if [ "$branch" = 'b' ]; then53if (( to >= 0x2000000 )); then54to=$(( to - 0x4000000 ))55fi56elif (( to >= 0x8000 )); then57to=$(( to - 0x10000 ))58fi59printf -v to '0x%x' $(( "0x$from" + to ))60;;61*) printf 'Unkown branch format\n'62;;63esac64if [ "$to" = "$sim" ]; then65continue66fi67if (( to > end_intr )); then68if $all_good; then69printf '%s\n' 'WARNING: Unrelocated relative branches'70all_good=false71fi72printf '%s %s-> %s %s\n' "$from" "$branch" "$to" "$sym"73fi74done7576$all_good7778}798081