check_extable.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #! /bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # (c) 2015, Quentin Casasnovas <quentin.casasnovas@oracle.com>
  4. obj=$1
  5. file ${obj} | grep -q ELF || (echo "${obj} is not and ELF file." 1>&2 ; exit 0)
  6. # Bail out early if there isn't an __ex_table section in this object file.
  7. objdump -hj __ex_table ${obj} 2> /dev/null > /dev/null
  8. [ $? -ne 0 ] && exit 0
  9. white_list=.text,.fixup
  10. suspicious_relocs=$(objdump -rj __ex_table ${obj} | tail -n +6 |
  11. grep -v $(eval echo -e{${white_list}}) | awk '{print $3}')
  12. # No suspicious relocs in __ex_table, jobs a good'un
  13. [ -z "${suspicious_relocs}" ] && exit 0
  14. # After this point, something is seriously wrong since we just found out we
  15. # have some relocations in __ex_table which point to sections which aren't
  16. # white listed. If you're adding a new section in the Linux kernel, and
  17. # you're expecting this section to contain code which can fault (i.e. the
  18. # __ex_table relocation to your new section is expected), simply add your
  19. # new section to the white_list variable above. If not, you're probably
  20. # doing something wrong and the rest of this code is just trying to print
  21. # you more information about it.
  22. function find_section_offset_from_symbol()
  23. {
  24. eval $(objdump -t ${obj} | grep ${1} | sed 's/\([0-9a-f]\+\) .\{7\} \([^ \t]\+\).*/section="\2"; section_offset="0x\1" /')
  25. # addr2line takes addresses in hexadecimal...
  26. section_offset=$(printf "0x%016x" $(( ${section_offset} + $2 )) )
  27. }
  28. function find_symbol_and_offset_from_reloc()
  29. {
  30. # Extract symbol and offset from the objdump output
  31. eval $(echo $reloc | sed 's/\([^+]\+\)+\?\(0x[0-9a-f]\+\)\?/symbol="\1"; symbol_offset="\2"/')
  32. # When the relocation points to the begining of a symbol or section, it
  33. # won't print the offset since it is zero.
  34. if [ -z "${symbol_offset}" ]; then
  35. symbol_offset=0x0
  36. fi
  37. }
  38. function find_alt_replacement_target()
  39. {
  40. # The target of the .altinstr_replacement is the relocation just before
  41. # the .altinstr_replacement one.
  42. eval $(objdump -rj .altinstructions ${obj} | grep -B1 "${section}+${section_offset}" | head -n1 | awk '{print $3}' |
  43. sed 's/\([^+]\+\)+\(0x[0-9a-f]\+\)/alt_target_section="\1"; alt_target_offset="\2"/')
  44. }
  45. function handle_alt_replacement_reloc()
  46. {
  47. # This will define alt_target_section and alt_target_section_offset
  48. find_alt_replacement_target ${section} ${section_offset}
  49. echo "Error: found a reference to .altinstr_replacement in __ex_table:"
  50. addr2line -fip -j ${alt_target_section} -e ${obj} ${alt_target_offset} | awk '{print "\t" $0}'
  51. error=true
  52. }
  53. function is_executable_section()
  54. {
  55. objdump -hwj ${section} ${obj} | grep -q CODE
  56. return $?
  57. }
  58. function handle_suspicious_generic_reloc()
  59. {
  60. if is_executable_section ${section}; then
  61. # We've got a relocation to a non white listed _executable_
  62. # section, print a warning so the developper adds the section to
  63. # the white list or fix his code. We try to pretty-print the file
  64. # and line number where that relocation was added.
  65. echo "Warning: found a reference to section \"${section}\" in __ex_table:"
  66. addr2line -fip -j ${section} -e ${obj} ${section_offset} | awk '{print "\t" $0}'
  67. else
  68. # Something is definitively wrong here since we've got a relocation
  69. # to a non-executable section, there's no way this would ever be
  70. # running in the kernel.
  71. echo "Error: found a reference to non-executable section \"${section}\" in __ex_table at offset ${section_offset}"
  72. error=true
  73. fi
  74. }
  75. function handle_suspicious_reloc()
  76. {
  77. case "${section}" in
  78. ".altinstr_replacement")
  79. handle_alt_replacement_reloc ${section} ${section_offset}
  80. ;;
  81. *)
  82. handle_suspicious_generic_reloc ${section} ${section_offset}
  83. ;;
  84. esac
  85. }
  86. function diagnose()
  87. {
  88. for reloc in ${suspicious_relocs}; do
  89. # Let's find out where the target of the relocation in __ex_table
  90. # is, this will define ${symbol} and ${symbol_offset}
  91. find_symbol_and_offset_from_reloc ${reloc}
  92. # When there's a global symbol at the place of the relocation,
  93. # objdump will use it instead of giving us a section+offset, so
  94. # let's find out which section is this symbol in and the total
  95. # offset withing that section.
  96. find_section_offset_from_symbol ${symbol} ${symbol_offset}
  97. # In this case objdump was presenting us with a reloc to a symbol
  98. # rather than a section. Now that we've got the actual section,
  99. # we can skip it if it's in the white_list.
  100. if [ -z "$( echo $section | grep -v $(eval echo -e{${white_list}}))" ]; then
  101. continue;
  102. fi
  103. # Will either print a warning if the relocation happens to be in a
  104. # section we do not know but has executable bit set, or error out.
  105. handle_suspicious_reloc
  106. done
  107. }
  108. function check_debug_info() {
  109. objdump -hj .debug_info ${obj} 2> /dev/null > /dev/null ||
  110. echo -e "${obj} does not contain debug information, the addr2line output will be limited.\n" \
  111. "Recompile ${obj} with CONFIG_DEBUG_INFO to get a more useful output."
  112. }
  113. check_debug_info
  114. diagnose
  115. if [ "${error}" ]; then
  116. exit 1
  117. fi
  118. exit 0