Back to home page

OSCL-LXR

 
 

    


0001 #! /bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 # (c) 2015, Quentin Casasnovas <quentin.casasnovas@oracle.com>
0004 
0005 obj=$1
0006 
0007 file ${obj} | grep -q ELF || (echo "${obj} is not an ELF file." 1>&2 ; exit 0)
0008 
0009 # Bail out early if there isn't an __ex_table section in this object file.
0010 objdump -hj __ex_table ${obj} 2> /dev/null > /dev/null
0011 [ $? -ne 0 ] && exit 0
0012 
0013 white_list=.text,.fixup
0014 
0015 suspicious_relocs=$(objdump -rj __ex_table ${obj}  | tail -n +6 |
0016                         grep -v $(eval echo -e{${white_list}}) | awk '{print $3}')
0017 
0018 # No suspicious relocs in __ex_table, jobs a good'un
0019 [ -z "${suspicious_relocs}" ] && exit 0
0020 
0021 
0022 # After this point, something is seriously wrong since we just found out we
0023 # have some relocations in __ex_table which point to sections which aren't
0024 # white listed.  If you're adding a new section in the Linux kernel, and
0025 # you're expecting this section to contain code which can fault (i.e. the
0026 # __ex_table relocation to your new section is expected), simply add your
0027 # new section to the white_list variable above.  If not, you're probably
0028 # doing something wrong and the rest of this code is just trying to print
0029 # you more information about it.
0030 
0031 function find_section_offset_from_symbol()
0032 {
0033     eval $(objdump -t ${obj} | grep ${1} | sed 's/\([0-9a-f]\+\) .\{7\} \([^ \t]\+\).*/section="\2"; section_offset="0x\1" /')
0034 
0035     # addr2line takes addresses in hexadecimal...
0036     section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) )
0037 }
0038 
0039 function find_symbol_and_offset_from_reloc()
0040 {
0041     # Extract symbol and offset from the objdump output
0042     eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/')
0043 
0044     # When the relocation points to the begining of a symbol or section, it
0045     # won't print the offset since it is zero.
0046     if [ -z "${symbol_offset}" ]; then
0047         symbol_offset=0x0
0048     fi
0049 }
0050 
0051 function find_alt_replacement_target()
0052 {
0053     # The target of the .altinstr_replacement is the relocation just before
0054     # the .altinstr_replacement one.
0055     eval $(objdump -rj .altinstructions ${obj} | grep -B1 "${section}+${section_offset}" | head -n1 | awk '{print $3}' |
0056            sed 's/\([^+]\+\)+\(0x[0-9a-f]\+\)/alt_target_section="\1"; alt_target_offset="\2"/')
0057 }
0058 
0059 function handle_alt_replacement_reloc()
0060 {
0061     # This will define alt_target_section and alt_target_section_offset
0062     find_alt_replacement_target ${section} ${section_offset}
0063 
0064     echo "Error: found a reference to .altinstr_replacement in __ex_table:"
0065     addr2line -fip -j ${alt_target_section} -e ${obj} ${alt_target_offset} | awk '{print "\t" $0}'
0066 
0067     error=true
0068 }
0069 
0070 function is_executable_section()
0071 {
0072     objdump -hwj ${section} ${obj} | grep -q CODE
0073     return $?
0074 }
0075 
0076 function handle_suspicious_generic_reloc()
0077 {
0078     if is_executable_section ${section}; then
0079         # We've got a relocation to a non white listed _executable_
0080         # section, print a warning so the developper adds the section to
0081         # the white list or fix his code.  We try to pretty-print the file
0082         # and line number where that relocation was added.
0083         echo "Warning: found a reference to section \"${section}\" in __ex_table:"
0084         addr2line -fip -j ${section} -e ${obj} ${section_offset} | awk '{print "\t" $0}'
0085     else
0086         # Something is definitively wrong here since we've got a relocation
0087         # to a non-executable section, there's no way this would ever be
0088         # running in the kernel.
0089         echo "Error: found a reference to non-executable section \"${section}\" in __ex_table at offset ${section_offset}"
0090         error=true
0091     fi
0092 }
0093 
0094 function handle_suspicious_reloc()
0095 {
0096     case "${section}" in
0097         ".altinstr_replacement")
0098             handle_alt_replacement_reloc ${section} ${section_offset}
0099             ;;
0100         *)
0101             handle_suspicious_generic_reloc ${section} ${section_offset}
0102             ;;
0103     esac
0104 }
0105 
0106 function diagnose()
0107 {
0108 
0109     for reloc in ${suspicious_relocs}; do
0110         # Let's find out where the target of the relocation in __ex_table
0111         # is, this will define ${symbol} and ${symbol_offset}
0112         find_symbol_and_offset_from_reloc ${reloc}
0113 
0114         # When there's a global symbol at the place of the relocation,
0115         # objdump will use it instead of giving us a section+offset, so
0116         # let's find out which section is this symbol in and the total
0117         # offset withing that section.
0118         find_section_offset_from_symbol ${symbol} ${symbol_offset}
0119 
0120         # In this case objdump was presenting us with a reloc to a symbol
0121         # rather than a section. Now that we've got the actual section,
0122         # we can skip it if it's in the white_list.
0123         if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then
0124             continue;
0125         fi
0126 
0127         # Will either print a warning if the relocation happens to be in a
0128         # section we do not know but has executable bit set, or error out.
0129         handle_suspicious_reloc
0130     done
0131 }
0132 
0133 function check_debug_info() {
0134     objdump -hj .debug_info ${obj} 2> /dev/null > /dev/null ||
0135         echo -e "${obj} does not contain debug information, the addr2line output will be limited.\n" \
0136              "Recompile ${obj} with CONFIG_DEBUG_INFO to get a more useful output."
0137 }
0138 
0139 check_debug_info
0140 
0141 diagnose
0142 
0143 if [ "${error}" ]; then
0144     exit 1
0145 fi
0146 
0147 exit 0