18c2ecf20Sopenharmony_ci#! /bin/bash 28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 38c2ecf20Sopenharmony_ci# (c) 2015, Quentin Casasnovas <quentin.casasnovas@oracle.com> 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ciobj=$1 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_cifile ${obj} | grep -q ELF || (echo "${obj} is not and ELF file." 1>&2 ; exit 0) 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci# Bail out early if there isn't an __ex_table section in this object file. 108c2ecf20Sopenharmony_ciobjdump -hj __ex_table ${obj} 2> /dev/null > /dev/null 118c2ecf20Sopenharmony_ci[ $? -ne 0 ] && exit 0 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciwhite_list=.text,.fixup 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cisuspicious_relocs=$(objdump -rj __ex_table ${obj} | tail -n +6 | 168c2ecf20Sopenharmony_ci grep -v $(eval echo -e{${white_list}}) | awk '{print $3}') 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci# No suspicious relocs in __ex_table, jobs a good'un 198c2ecf20Sopenharmony_ci[ -z "${suspicious_relocs}" ] && exit 0 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci# After this point, something is seriously wrong since we just found out we 238c2ecf20Sopenharmony_ci# have some relocations in __ex_table which point to sections which aren't 248c2ecf20Sopenharmony_ci# white listed. If you're adding a new section in the Linux kernel, and 258c2ecf20Sopenharmony_ci# you're expecting this section to contain code which can fault (i.e. the 268c2ecf20Sopenharmony_ci# __ex_table relocation to your new section is expected), simply add your 278c2ecf20Sopenharmony_ci# new section to the white_list variable above. If not, you're probably 288c2ecf20Sopenharmony_ci# doing something wrong and the rest of this code is just trying to print 298c2ecf20Sopenharmony_ci# you more information about it. 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cifunction find_section_offset_from_symbol() 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci eval $(objdump -t ${obj} | grep ${1} | sed 's/\([0-9a-f]\+\) .\{7\} \([^ \t]\+\).*/section="\2"; section_offset="0x\1" /') 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci # addr2line takes addresses in hexadecimal... 368c2ecf20Sopenharmony_ci section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) ) 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cifunction find_symbol_and_offset_from_reloc() 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci # Extract symbol and offset from the objdump output 428c2ecf20Sopenharmony_ci eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/') 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci # When the relocation points to the begining of a symbol or section, it 458c2ecf20Sopenharmony_ci # won't print the offset since it is zero. 468c2ecf20Sopenharmony_ci if [ -z "${symbol_offset}" ]; then 478c2ecf20Sopenharmony_ci symbol_offset=0x0 488c2ecf20Sopenharmony_ci fi 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cifunction find_alt_replacement_target() 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci # The target of the .altinstr_replacement is the relocation just before 548c2ecf20Sopenharmony_ci # the .altinstr_replacement one. 558c2ecf20Sopenharmony_ci eval $(objdump -rj .altinstructions ${obj} | grep -B1 "${section}+${section_offset}" | head -n1 | awk '{print $3}' | 568c2ecf20Sopenharmony_ci sed 's/\([^+]\+\)+\(0x[0-9a-f]\+\)/alt_target_section="\1"; alt_target_offset="\2"/') 578c2ecf20Sopenharmony_ci} 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cifunction handle_alt_replacement_reloc() 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci # This will define alt_target_section and alt_target_section_offset 628c2ecf20Sopenharmony_ci find_alt_replacement_target ${section} ${section_offset} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci echo "Error: found a reference to .altinstr_replacement in __ex_table:" 658c2ecf20Sopenharmony_ci addr2line -fip -j ${alt_target_section} -e ${obj} ${alt_target_offset} | awk '{print "\t" $0}' 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci error=true 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cifunction is_executable_section() 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci objdump -hwj ${section} ${obj} | grep -q CODE 738c2ecf20Sopenharmony_ci return $? 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cifunction handle_suspicious_generic_reloc() 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci if is_executable_section ${section}; then 798c2ecf20Sopenharmony_ci # We've got a relocation to a non white listed _executable_ 808c2ecf20Sopenharmony_ci # section, print a warning so the developper adds the section to 818c2ecf20Sopenharmony_ci # the white list or fix his code. We try to pretty-print the file 828c2ecf20Sopenharmony_ci # and line number where that relocation was added. 838c2ecf20Sopenharmony_ci echo "Warning: found a reference to section \"${section}\" in __ex_table:" 848c2ecf20Sopenharmony_ci addr2line -fip -j ${section} -e ${obj} ${section_offset} | awk '{print "\t" $0}' 858c2ecf20Sopenharmony_ci else 868c2ecf20Sopenharmony_ci # Something is definitively wrong here since we've got a relocation 878c2ecf20Sopenharmony_ci # to a non-executable section, there's no way this would ever be 888c2ecf20Sopenharmony_ci # running in the kernel. 898c2ecf20Sopenharmony_ci echo "Error: found a reference to non-executable section \"${section}\" in __ex_table at offset ${section_offset}" 908c2ecf20Sopenharmony_ci error=true 918c2ecf20Sopenharmony_ci fi 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cifunction handle_suspicious_reloc() 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci case "${section}" in 978c2ecf20Sopenharmony_ci ".altinstr_replacement") 988c2ecf20Sopenharmony_ci handle_alt_replacement_reloc ${section} ${section_offset} 998c2ecf20Sopenharmony_ci ;; 1008c2ecf20Sopenharmony_ci *) 1018c2ecf20Sopenharmony_ci handle_suspicious_generic_reloc ${section} ${section_offset} 1028c2ecf20Sopenharmony_ci ;; 1038c2ecf20Sopenharmony_ci esac 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cifunction diagnose() 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci for reloc in ${suspicious_relocs}; do 1108c2ecf20Sopenharmony_ci # Let's find out where the target of the relocation in __ex_table 1118c2ecf20Sopenharmony_ci # is, this will define ${symbol} and ${symbol_offset} 1128c2ecf20Sopenharmony_ci find_symbol_and_offset_from_reloc ${reloc} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci # When there's a global symbol at the place of the relocation, 1158c2ecf20Sopenharmony_ci # objdump will use it instead of giving us a section+offset, so 1168c2ecf20Sopenharmony_ci # let's find out which section is this symbol in and the total 1178c2ecf20Sopenharmony_ci # offset withing that section. 1188c2ecf20Sopenharmony_ci find_section_offset_from_symbol ${symbol} ${symbol_offset} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci # In this case objdump was presenting us with a reloc to a symbol 1218c2ecf20Sopenharmony_ci # rather than a section. Now that we've got the actual section, 1228c2ecf20Sopenharmony_ci # we can skip it if it's in the white_list. 1238c2ecf20Sopenharmony_ci if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then 1248c2ecf20Sopenharmony_ci continue; 1258c2ecf20Sopenharmony_ci fi 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci # Will either print a warning if the relocation happens to be in a 1288c2ecf20Sopenharmony_ci # section we do not know but has executable bit set, or error out. 1298c2ecf20Sopenharmony_ci handle_suspicious_reloc 1308c2ecf20Sopenharmony_ci done 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cifunction check_debug_info() { 1348c2ecf20Sopenharmony_ci objdump -hj .debug_info ${obj} 2> /dev/null > /dev/null || 1358c2ecf20Sopenharmony_ci echo -e "${obj} does not contain debug information, the addr2line output will be limited.\n" \ 1368c2ecf20Sopenharmony_ci "Recompile ${obj} with CONFIG_DEBUG_INFO to get a more useful output." 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cicheck_debug_info 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cidiagnose 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ciif [ "${error}" ]; then 1448c2ecf20Sopenharmony_ci exit 1 1458c2ecf20Sopenharmony_cifi 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ciexit 0 148