^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) # Copyright © 2016 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # modify it under the terms of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # as published by the Free Software Foundation; either version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # 2 of the License, or (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # This script checks the head of a vmlinux for linker stubs that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # break our placement of fixed-location code for 64-bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # based on relocs_check.pl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # Copyright © 2009 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # NOTE!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) # If the build dies here, it's likely code in head_64.S/exception-64*.S or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) # nearby, is branching to labels it can't reach directly, which results in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) # linker inserting branch stubs. This can move code around in ways that break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # the fixed section calculations (head-64.h). To debug this, disassemble the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # vmlinux and look for branch stubs (long_branch, plt_branch, etc.) in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) # fixed section region (0 - 0x8000ish). Check what code is calling those stubs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # and perhaps change so a direct branch can reach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # A ".linker_stub_catch" section is used to catch some stubs generated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) # early .text code, which tend to get placed at the start of the section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) # If there are too many such stubs, they can overflow this section. Expanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # it may help (or reducing the number of stub branches).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) # Linker stubs use the TOC pointer, so even if fixed section code could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) # tolerate them being inserted into head code, they can't be allowed in low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) # level entry code (boot, interrupt vectors, etc) until r2 is set up. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) # could cause the kernel to die in early boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) # Allow for verbose output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if [ "$V" = "1" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) set -x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if [ $# -lt 2 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) echo "$0 [path to nm] [path to vmlinux]" 1>&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # Have Kbuild supply the path to nm so we handle cross compilation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) nm="$1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) vmlinux="$2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) # gcc-4.6-era toolchain make _stext an A (absolute) symbol rather than T
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) $nm "$vmlinux" | grep -e " [TA] _stext$" -e " t start_first_256B$" -e " a text_start$" -e " t start_text$" > .tmp_symbols.txt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) vma=$(cat .tmp_symbols.txt | grep -e " [TA] _stext$" | cut -d' ' -f1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) expected_start_head_addr=$vma
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) start_head_addr=$(cat .tmp_symbols.txt | grep " t start_first_256B$" | cut -d' ' -f1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if [ "$start_head_addr" != "$expected_start_head_addr" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) echo "ERROR: head code starts at $start_head_addr, should be $expected_start_head_addr"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) echo "ERROR: try to enable LD_HEAD_STUB_CATCH config option"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) echo "ERROR: see comments in arch/powerpc/tools/head_check.sh"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) top_vma=$(echo $vma | cut -d'0' -f1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) expected_start_text_addr=$(cat .tmp_symbols.txt | grep " a text_start$" | cut -d' ' -f1 | sed "s/^0/$top_vma/")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) start_text_addr=$(cat .tmp_symbols.txt | grep " t start_text$" | cut -d' ' -f1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if [ "$start_text_addr" != "$expected_start_text_addr" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) echo "ERROR: start_text address is $start_text_addr, should be $expected_start_text_addr"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) echo "ERROR: try to enable LD_HEAD_STUB_CATCH config option"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) echo "ERROR: see comments in arch/powerpc/tools/head_check.sh"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) rm -f .tmp_symbols.txt