162306a36Sopenharmony_ci#! /bin/bash 262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 362306a36Sopenharmony_ci# (c) 2015, Quentin Casasnovas <quentin.casasnovas@oracle.com> 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciobj=$1 662306a36Sopenharmony_ci 762306a36Sopenharmony_cifile ${obj} | grep -q ELF || (echo "${obj} is not an ELF file." 1>&2 ; exit 0) 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci# Bail out early if there isn't an __ex_table section in this object file. 1062306a36Sopenharmony_ciobjdump -hj __ex_table ${obj} 2> /dev/null > /dev/null 1162306a36Sopenharmony_ci[ $? -ne 0 ] && exit 0 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciwhite_list=.text,.fixup 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_cisuspicious_relocs=$(objdump -rj __ex_table ${obj} | tail -n +6 | 1662306a36Sopenharmony_ci grep -v $(eval echo -e{${white_list}}) | awk '{print $3}') 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci# No suspicious relocs in __ex_table, jobs a good'un 1962306a36Sopenharmony_ci[ -z "${suspicious_relocs}" ] && exit 0 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci# After this point, something is seriously wrong since we just found out we 2362306a36Sopenharmony_ci# have some relocations in __ex_table which point to sections which aren't 2462306a36Sopenharmony_ci# white listed. If you're adding a new section in the Linux kernel, and 2562306a36Sopenharmony_ci# you're expecting this section to contain code which can fault (i.e. the 2662306a36Sopenharmony_ci# __ex_table relocation to your new section is expected), simply add your 2762306a36Sopenharmony_ci# new section to the white_list variable above. If not, you're probably 2862306a36Sopenharmony_ci# doing something wrong and the rest of this code is just trying to print 2962306a36Sopenharmony_ci# you more information about it. 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_cifunction find_section_offset_from_symbol() 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci eval $(objdump -t ${obj} | grep ${1} | sed 's/\([0-9a-f]\+\) .\{7\} \([^ \t]\+\).*/section="\2"; section_offset="0x\1" /') 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci # addr2line takes addresses in hexadecimal... 3662306a36Sopenharmony_ci section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) ) 3762306a36Sopenharmony_ci} 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_cifunction find_symbol_and_offset_from_reloc() 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci # Extract symbol and offset from the objdump output 4262306a36Sopenharmony_ci eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/') 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci # When the relocation points to the begining of a symbol or section, it 4562306a36Sopenharmony_ci # won't print the offset since it is zero. 4662306a36Sopenharmony_ci if [ -z "${symbol_offset}" ]; then 4762306a36Sopenharmony_ci symbol_offset=0x0 4862306a36Sopenharmony_ci fi 4962306a36Sopenharmony_ci} 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_cifunction find_alt_replacement_target() 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci # The target of the .altinstr_replacement is the relocation just before 5462306a36Sopenharmony_ci # the .altinstr_replacement one. 5562306a36Sopenharmony_ci eval $(objdump -rj .altinstructions ${obj} | grep -B1 "${section}+${section_offset}" | head -n1 | awk '{print $3}' | 5662306a36Sopenharmony_ci sed 's/\([^+]\+\)+\(0x[0-9a-f]\+\)/alt_target_section="\1"; alt_target_offset="\2"/') 5762306a36Sopenharmony_ci} 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cifunction handle_alt_replacement_reloc() 6062306a36Sopenharmony_ci{ 6162306a36Sopenharmony_ci # This will define alt_target_section and alt_target_section_offset 6262306a36Sopenharmony_ci find_alt_replacement_target ${section} ${section_offset} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci echo "Error: found a reference to .altinstr_replacement in __ex_table:" 6562306a36Sopenharmony_ci addr2line -fip -j ${alt_target_section} -e ${obj} ${alt_target_offset} | awk '{print "\t" $0}' 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci error=true 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cifunction is_executable_section() 7162306a36Sopenharmony_ci{ 7262306a36Sopenharmony_ci objdump -hwj ${section} ${obj} | grep -q CODE 7362306a36Sopenharmony_ci return $? 7462306a36Sopenharmony_ci} 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_cifunction handle_suspicious_generic_reloc() 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci if is_executable_section ${section}; then 7962306a36Sopenharmony_ci # We've got a relocation to a non white listed _executable_ 8062306a36Sopenharmony_ci # section, print a warning so the developper adds the section to 8162306a36Sopenharmony_ci # the white list or fix his code. We try to pretty-print the file 8262306a36Sopenharmony_ci # and line number where that relocation was added. 8362306a36Sopenharmony_ci echo "Warning: found a reference to section \"${section}\" in __ex_table:" 8462306a36Sopenharmony_ci addr2line -fip -j ${section} -e ${obj} ${section_offset} | awk '{print "\t" $0}' 8562306a36Sopenharmony_ci else 8662306a36Sopenharmony_ci # Something is definitively wrong here since we've got a relocation 8762306a36Sopenharmony_ci # to a non-executable section, there's no way this would ever be 8862306a36Sopenharmony_ci # running in the kernel. 8962306a36Sopenharmony_ci echo "Error: found a reference to non-executable section \"${section}\" in __ex_table at offset ${section_offset}" 9062306a36Sopenharmony_ci error=true 9162306a36Sopenharmony_ci fi 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_cifunction handle_suspicious_reloc() 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci case "${section}" in 9762306a36Sopenharmony_ci ".altinstr_replacement") 9862306a36Sopenharmony_ci handle_alt_replacement_reloc ${section} ${section_offset} 9962306a36Sopenharmony_ci ;; 10062306a36Sopenharmony_ci *) 10162306a36Sopenharmony_ci handle_suspicious_generic_reloc ${section} ${section_offset} 10262306a36Sopenharmony_ci ;; 10362306a36Sopenharmony_ci esac 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_cifunction diagnose() 10762306a36Sopenharmony_ci{ 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci for reloc in ${suspicious_relocs}; do 11062306a36Sopenharmony_ci # Let's find out where the target of the relocation in __ex_table 11162306a36Sopenharmony_ci # is, this will define ${symbol} and ${symbol_offset} 11262306a36Sopenharmony_ci find_symbol_and_offset_from_reloc ${reloc} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci # When there's a global symbol at the place of the relocation, 11562306a36Sopenharmony_ci # objdump will use it instead of giving us a section+offset, so 11662306a36Sopenharmony_ci # let's find out which section is this symbol in and the total 11762306a36Sopenharmony_ci # offset withing that section. 11862306a36Sopenharmony_ci find_section_offset_from_symbol ${symbol} ${symbol_offset} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci # In this case objdump was presenting us with a reloc to a symbol 12162306a36Sopenharmony_ci # rather than a section. Now that we've got the actual section, 12262306a36Sopenharmony_ci # we can skip it if it's in the white_list. 12362306a36Sopenharmony_ci if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then 12462306a36Sopenharmony_ci continue; 12562306a36Sopenharmony_ci fi 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci # Will either print a warning if the relocation happens to be in a 12862306a36Sopenharmony_ci # section we do not know but has executable bit set, or error out. 12962306a36Sopenharmony_ci handle_suspicious_reloc 13062306a36Sopenharmony_ci done 13162306a36Sopenharmony_ci} 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cifunction check_debug_info() { 13462306a36Sopenharmony_ci objdump -hj .debug_info ${obj} 2> /dev/null > /dev/null || 13562306a36Sopenharmony_ci echo -e "${obj} does not contain debug information, the addr2line output will be limited.\n" \ 13662306a36Sopenharmony_ci "Recompile ${obj} with CONFIG_DEBUG_INFO to get a more useful output." 13762306a36Sopenharmony_ci} 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_cicheck_debug_info 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_cidiagnose 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ciif [ "${error}" ]; then 14462306a36Sopenharmony_ci exit 1 14562306a36Sopenharmony_cifi 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ciexit 0 148