Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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