check_extable.sh 4.9 KB

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