0001
0002
0003
0004
0005 obj=$1
0006
0007 file ${obj} | grep -q ELF || (echo "${obj} is not an ELF file." 1>&2 ; exit 0)
0008
0009
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
0019 [ -z "${suspicious_relocs}" ] && exit 0
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
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
0036 section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) )
0037 }
0038
0039 function find_symbol_and_offset_from_reloc()
0040 {
0041
0042 eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/')
0043
0044
0045
0046 if [ -z "${symbol_offset}" ]; then
0047 symbol_offset=0x0
0048 fi
0049 }
0050
0051 function find_alt_replacement_target()
0052 {
0053
0054
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
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
0080
0081
0082
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
0087
0088
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
0111
0112 find_symbol_and_offset_from_reloc ${reloc}
0113
0114
0115
0116
0117
0118 find_section_offset_from_symbol ${symbol} ${symbol_offset}
0119
0120
0121
0122
0123 if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then
0124 continue;
0125 fi
0126
0127
0128
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