Merge tag 'linux-kselftest-kunit-fixes-5.11-rc3' of git://git.kernel.org/pub/scm...
[linux/fpc-iii.git] / arch / powerpc / tools / unrel_branch_check.sh
blob8301efee1e6c98d3eccd3913907070c61bd00d05
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright © 2016,2020 IBM Corporation
5 # This script checks the unrelocated code of a vmlinux for "suspicious"
6 # branches to relocated code (head_64.S code).
8 # Have Kbuild supply the path to objdump and nm so we handle cross compilation.
9 objdump="$1"
10 nm="$2"
11 vmlinux="$3"
13 kstart=0xc000000000000000
15 end_intr=0x$($nm -p "$vmlinux" |
16 sed -E -n '/\s+[[:alpha:]]\s+__end_interrupts\s*$/{s///p;q}')
17 if [ "$end_intr" = "0x" ]; then
18 exit 0
21 # we know that there is a correct branch to
22 # __start_initialization_multiplatform, so find its address
23 # so we can exclude it.
24 sim=0x$($nm -p "$vmlinux" |
25 sed -E -n '/\s+[[:alpha:]]\s+__start_initialization_multiplatform\s*$/{s///p;q}')
27 $objdump -D --no-show-raw-insn --start-address="$kstart" --stop-address="$end_intr" "$vmlinux" |
28 sed -E -n '
29 # match lines that start with a kernel address
30 /^c[0-9a-f]*:\s*b/ {
31 # drop branches via ctr or lr
32 /\<b.?.?(ct|l)r/d
33 # cope with some differences between Clang and GNU objdumps
34 s/\<bt.?\s*[[:digit:]]+,/beq/
35 s/\<bf.?\s*[[:digit:]]+,/bne/
36 # tidy up
37 s/\s0x/ /
38 s/://
39 # format for the loop below
40 s/^(\S+)\s+(\S+)\s+(\S+)\s*(\S*).*$/\1:\2:\3:\4/
41 # strip out condition registers
42 s/:cr[0-7],/:/
44 }' | {
46 all_good=true
47 while IFS=: read -r from branch to sym; do
48 case "$to" in
49 c*) to="0x$to"
51 .+*)
52 to=${to#.+}
53 if [ "$branch" = 'b' ]; then
54 if (( to >= 0x2000000 )); then
55 to=$(( to - 0x4000000 ))
57 elif (( to >= 0x8000 )); then
58 to=$(( to - 0x10000 ))
60 printf -v to '0x%x' $(( "0x$from" + to ))
62 *) printf 'Unkown branch format\n'
64 esac
65 if [ "$to" = "$sim" ]; then
66 continue
68 if (( to > end_intr )); then
69 if $all_good; then
70 printf '%s\n' 'WARNING: Unrelocated relative branches'
71 all_good=false
73 printf '%s %s-> %s %s\n' "$from" "$branch" "$to" "$sym"
75 done
77 $all_good