| 1 | |
| 2 | |
| 3 | |
| 4 |
|
| 5 | obj=$1
|
| 6 |
|
| 7 | file ${obj} | grep -q ELF || (echo "${obj} is not an ELF file." 1>&2 ; exit 0)
|
| 8 |
|
| 9 | |
| 10 | objdump -hj __ex_table ${obj} 2> /dev/null > /dev/null
|
| 11 | [ $? -ne 0 ] && exit 0
|
| 12 |
|
| 13 | white_list=.text,.fixup
|
| 14 |
|
| 15 | suspicious_relocs=$(objdump -rj __ex_table ${obj} | tail -n +6 |
|
| 16 | grep -v $(eval echo -e{${white_list}}) | awk '{print $3}')
|
| 17 |
|
| 18 | |
| 19 | [ -z "${suspicious_relocs}" ] && exit 0
|
| 20 |
|
| 21 |
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 |
|
| 31 | function find_section_offset_from_symbol()
|
| 32 | {
|
| 33 | eval $(objdump -t ${obj} | grep ${1} | sed 's/\([0-9a-f]\+\) .\{7\} \([^ \t]\+\).*/section="\2"; section_offset="0x\1" /')
|
| 34 |
|
| 35 | |
| 36 | section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) )
|
| 37 | }
|
| 38 |
|
| 39 | function find_symbol_and_offset_from_reloc()
|
| 40 | {
|
| 41 | |
| 42 | eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/')
|
| 43 |
|
| 44 | |
| 45 | |
| 46 | if [ -z "${symbol_offset}" ]; then
|
| 47 | symbol_offset=0x0
|
| 48 | fi
|
| 49 | }
|
| 50 |
|
| 51 | function find_alt_replacement_target()
|
| 52 | {
|
| 53 | |
| 54 | |
| 55 | eval $(objdump -rj .altinstructions ${obj} | grep -B1 "${section}+${section_offset}" | head -n1 | awk '{print $3}' |
|
| 56 | sed 's/\([^+]\+\)+\(0x[0-9a-f]\+\)/alt_target_section="\1"; alt_target_offset="\2"/')
|
| 57 | }
|
| 58 |
|
| 59 | function handle_alt_replacement_reloc()
|
| 60 | {
|
| 61 | |
| 62 | find_alt_replacement_target ${section} ${section_offset}
|
| 63 |
|
| 64 | echo "Error: found a reference to .altinstr_replacement in __ex_table:"
|
| 65 | addr2line -fip -j ${alt_target_section} -e ${obj} ${alt_target_offset} | awk '{print "\t" $0}'
|
| 66 |
|
| 67 | error=true
|
| 68 | }
|
| 69 |
|
| 70 | function is_executable_section()
|
| 71 | {
|
| 72 | objdump -hwj ${section} ${obj} | grep -q CODE
|
| 73 | return $?
|
| 74 | }
|
| 75 |
|
| 76 | function handle_suspicious_generic_reloc()
|
| 77 | {
|
| 78 | if is_executable_section ${section}; then
|
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | echo "Warning: found a reference to section \"${section}\" in __ex_table:"
|
| 84 | addr2line -fip -j ${section} -e ${obj} ${section_offset} | awk '{print "\t" $0}'
|
| 85 | else
|
| 86 | |
| 87 | |
| 88 | |
| 89 | echo "Error: found a reference to non-executable section \"${section}\" in __ex_table at offset ${section_offset}"
|
| 90 | error=true
|
| 91 | fi
|
| 92 | }
|
| 93 |
|
| 94 | function handle_suspicious_reloc()
|
| 95 | {
|
| 96 | case "${section}" in
|
| 97 | ".altinstr_replacement")
|
| 98 | handle_alt_replacement_reloc ${section} ${section_offset}
|
| 99 | ;;
|
| 100 | *)
|
| 101 | handle_suspicious_generic_reloc ${section} ${section_offset}
|
| 102 | ;;
|
| 103 | esac
|
| 104 | }
|
| 105 |
|
| 106 | function diagnose()
|
| 107 | {
|
| 108 |
|
| 109 | for reloc in ${suspicious_relocs}; do
|
| 110 | |
| 111 | |
| 112 | find_symbol_and_offset_from_reloc ${reloc}
|
| 113 |
|
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | find_section_offset_from_symbol ${symbol} ${symbol_offset}
|
| 119 |
|
| 120 | |
| 121 | |
| 122 | |
| 123 | if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then
|
| 124 | continue;
|
| 125 | fi
|
| 126 |
|
| 127 | |
| 128 | |
| 129 | handle_suspicious_reloc
|
| 130 | done
|
| 131 | }
|
| 132 |
|
| 133 | function check_debug_info() {
|
| 134 | objdump -hj .debug_info ${obj} 2> /dev/null > /dev/null ||
|
| 135 | echo -e "${obj} does not contain debug information, the addr2line output will be limited.\n" \
|
| 136 | "Recompile ${obj} with CONFIG_DEBUG_INFO to get a more useful output."
|
| 137 | }
|
| 138 |
|
| 139 | check_debug_info
|
| 140 |
|
| 141 | diagnose
|
| 142 |
|
| 143 | if [ "${error}" ]; then
|
| 144 | exit 1
|
| 145 | fi
|
| 146 |
|
| 147 | exit 0
|