162306a36Sopenharmony_ci# Copyright © 2016 IBM Corporation
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci# This program is free software; you can redistribute it and/or
462306a36Sopenharmony_ci# modify it under the terms of the GNU General Public License
562306a36Sopenharmony_ci# as published by the Free Software Foundation; either version
662306a36Sopenharmony_ci# 2 of the License, or (at your option) any later version.
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci# This script checks the head of a vmlinux for linker stubs that
962306a36Sopenharmony_ci# break our placement of fixed-location code for 64-bit.
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci# based on relocs_check.pl
1262306a36Sopenharmony_ci# Copyright © 2009 IBM Corporation
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci# NOTE!
1562306a36Sopenharmony_ci#
1662306a36Sopenharmony_ci# If the build dies here, it's likely code in head_64.S/exception-64*.S or
1762306a36Sopenharmony_ci# nearby, is branching to labels it can't reach directly, which results in the
1862306a36Sopenharmony_ci# linker inserting branch stubs. This can move code around in ways that break
1962306a36Sopenharmony_ci# the fixed section calculations (head-64.h). To debug this, disassemble the
2062306a36Sopenharmony_ci# vmlinux and look for branch stubs (long_branch, plt_branch, etc.) in the
2162306a36Sopenharmony_ci# fixed section region (0 - 0x8000ish). Check what code is calling those stubs,
2262306a36Sopenharmony_ci# and perhaps change so a direct branch can reach.
2362306a36Sopenharmony_ci#
2462306a36Sopenharmony_ci# A ".linker_stub_catch" section is used to catch some stubs generated by
2562306a36Sopenharmony_ci# early .text code, which tend to get placed at the start of the section.
2662306a36Sopenharmony_ci# If there are too many such stubs, they can overflow this section. Expanding
2762306a36Sopenharmony_ci# it may help (or reducing the number of stub branches).
2862306a36Sopenharmony_ci#
2962306a36Sopenharmony_ci# Linker stubs use the TOC pointer, so even if fixed section code could
3062306a36Sopenharmony_ci# tolerate them being inserted into head code, they can't be allowed in low
3162306a36Sopenharmony_ci# level entry code (boot, interrupt vectors, etc) until r2 is set up. This
3262306a36Sopenharmony_ci# could cause the kernel to die in early boot.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci# Allow for verbose output
3562306a36Sopenharmony_ciif [ "$V" = "1" ]; then
3662306a36Sopenharmony_ci	set -x
3762306a36Sopenharmony_cifi
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciif [ $# -lt 2 ]; then
4062306a36Sopenharmony_ci	echo "$0 [path to nm] [path to vmlinux]" 1>&2
4162306a36Sopenharmony_ci	exit 1
4262306a36Sopenharmony_cifi
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci# Have Kbuild supply the path to nm so we handle cross compilation.
4562306a36Sopenharmony_cinm="$1"
4662306a36Sopenharmony_civmlinux="$2"
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci# gcc-4.6-era toolchain make _stext an A (absolute) symbol rather than T
4962306a36Sopenharmony_ci$nm "$vmlinux" | grep -e " [TA] _stext$" -e " t start_first_256B$" -e " a text_start$" -e " t start_text$" > .tmp_symbols.txt
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_civma=$(grep -e " [TA] _stext$" .tmp_symbols.txt | cut -d' ' -f1)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciexpected_start_head_addr="$vma"
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistart_head_addr=$(grep " t start_first_256B$" .tmp_symbols.txt | cut -d' ' -f1)
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciif [ "$start_head_addr" != "$expected_start_head_addr" ]; then
5962306a36Sopenharmony_ci	echo "ERROR: head code starts at $start_head_addr, should be $expected_start_head_addr" 1>&2
6062306a36Sopenharmony_ci	echo "ERROR: try to enable LD_HEAD_STUB_CATCH config option" 1>&2
6162306a36Sopenharmony_ci	echo "ERROR: see comments in arch/powerpc/tools/head_check.sh" 1>&2
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	exit 1
6462306a36Sopenharmony_cifi
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_citop_vma=$(echo "$vma" | cut -d'0' -f1)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciexpected_start_text_addr=$(grep " a text_start$" .tmp_symbols.txt | cut -d' ' -f1 | sed "s/^0/$top_vma/")
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistart_text_addr=$(grep " t start_text$" .tmp_symbols.txt | cut -d' ' -f1)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciif [ "$start_text_addr" != "$expected_start_text_addr" ]; then
7362306a36Sopenharmony_ci	echo "ERROR: start_text address is $start_text_addr, should be $expected_start_text_addr" 1>&2
7462306a36Sopenharmony_ci	echo "ERROR: try to enable LD_HEAD_STUB_CATCH config option" 1>&2
7562306a36Sopenharmony_ci	echo "ERROR: see comments in arch/powerpc/tools/head_check.sh" 1>&2
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	exit 1
7862306a36Sopenharmony_cifi
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cirm -f .tmp_symbols.txt
81