From f40a274ef847b8825134163fc1c3cd31434aeb1b Mon Sep 17 00:00:00 2001 From: Eugene Syromiatnikov Date: Wed, 17 May 2023 14:01:55 +0200 Subject: [PATCH] find-provides.ksyms, find-requires.ksyms: rewrite section data matching Currently, it just matches the 2nd to 5th fields with awk, which leads to occasional capturing of some garbage at the end of the section data and minor issues when supplying it to the symbol extracting awk script: awk: cmd. line:1: (FILENAME=- FNR=19) fatal: argument index with `$' must be > 0 awk: warning: escape sequence `\.' treated as plain `.' awk: cmd. line:1: (FILENAME=- FNR=9) fatal: not enough arguments to satisfy format string `78b624dbc125415351be965ex.$..%ASQ..^' ^ ran out for this one While at it, also eliminate grep and sed calls (with the pair of awk and sed being the most offending). * find-provides.ksyms: Generate part of the awk script that performs the byte reversal based on the endianness in the readelf -h output, inject it into the awk script; use substr for matching parts of the section data instead of fields. * find-requires.ksyms (all_provides): Likewise. Resolves: #2115811 Resolves: #2178935 Signed-off-by: Eugene Syromiatnikov --- find-provides.ksyms | 11 +++++------ find-requires.ksyms | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/find-provides.ksyms b/find-provides.ksyms index c7f593c..89d5dbb 100755 --- a/find-provides.ksyms +++ b/find-provides.ksyms @@ -51,12 +51,11 @@ for module in $(grep -E '/lib/modules/.+\.ko(\.gz|\.bz2|\.xz|\.zst)?$') "$@"; do | while read -r sectname; do [ -n "$sectname" ] || continue - ELFSECTDATA=$(readelf -R "$sectname" "$module" | awk '/0x/{printf $2$3$4$5}') - if [[ -n $(readelf -h $module | grep "little endian") ]]; then - SECTDATA=$(echo $ELFSECTDATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g') - else - SECTDATA=$ELFSECTDATA - fi + revbytes="" + kmod_elf_hdr="$(readelf -h "$module")" + [ "x$kmod_elf_hdr" = "x${kmod_elf_hdr%Data:*little endian*}" ] || + revbytes='a = gensub(/(..)(..)(..)(..)/, "\\4\\3\\2\\1", "g", a);' + SECTDATA=$(readelf -R "$sectname" "$module" | awk '/^ 0x[0-9a-f]{8}/{ a = substr($0, 14, 8) substr($0, 23, 8) substr($0, 32, 8) substr($0, 41, 8); '"$revbytes"' printf("%s", a); }') objdump -t "$module" \ | awk \ diff --git a/find-requires.ksyms b/find-requires.ksyms index 3b5e186..8f38d09 100644 --- a/find-requires.ksyms +++ b/find-requires.ksyms @@ -53,12 +53,11 @@ all_provides() { | while read -r sectname; do [ -n "$sectname" ] || continue - ELFSECTDATA=$(readelf -R "$sectname" "$module" | awk '/0x/{printf $2$3$4$5}') - if [[ -n $(readelf -h "$module" | grep "little endian") ]]; then - SECTDATA=$(echo $ELFSECTDATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g') - else - SECTDATA=$ELFSECTDATA - fi + revbytes="" + kmod_elf_hdr="$(readelf -h "$module")" + [ "x$kmod_elf_hdr" = "x${kmod_elf_hdr%Data:*little endian*}" ] || + revbytes='a = gensub(/(..)(..)(..)(..)/, "\\4\\3\\2\\1", "g", a);' + SECTDATA=$(readelf -R "$sectname" "$module" | awk '/^ 0x[0-9a-f]{8}/{ a = substr($0, 14, 8) substr($0, 23, 8) substr($0, 32, 8) substr($0, 41, 8); '"$revbytes"' printf("%s", a); }') objdump -t "$module" \ | awk \