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