[PATCH] Improve diagnostic on "Inconsistent kallsyms data"

From: Domenico Andreoli
Date: Fri May 17 2019 - 20:36:48 EST


Hi,

linking kallsyms into the kernel occasionally triggers edge conditions
of the linker heuristic and results in inconsistent System.map generation.

This patch adds a first-aid analysis of such inconsistency in form of
unified diff between the two generated symbol maps, where every symbol's
address is replaced by the offset from the previous symbol.

Becomes then trivial to spot symbols reordering without incurring in
the noise of other symbols shifting, as shown below.

$ make
...
LD vmlinux.o
MODPOST vmlinux.o
KSYM .tmp_kallsyms1.o
KSYM .tmp_kallsyms2.o
KSYM .tmp_kallsyms3.o
LD vmlinux
SORTEX vmlinux
SYSMAP System.map
Inconsistent kallsyms data
Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
---- .tmp_System.map.relative 2019-05-18 02:13:19.197282498 +0200
++++ System.map.relative 2019-05-18 02:13:16.817219696 +0200
@@ -7,10 +7,10 @@
0000000000013cf78 A __rela_size
0000000000012da88 A __pecoff_data_rawsize
0000000000004e600 A __pecoff_data_size
-000000000004b7000 A __efistub_stext_offset
+000000000004a7000 A __efistub_stext_offset
000000000001719d8 A __rela_offset
0000000000026b628 A _kernel_size_le_lo32
-0ffff00000f4b3000 t __efistub__text
+0ffff00000f4c3000 t __efistub__text
00000000000000000 t _head
00000000000000000 T _text
00000000000000040 t pe_header
@@ -28619,10 +28619,10 @@
00000000000000008 r bus_spec.64271
00000000000000008 r str_spec.64272
00000000000000008 R kallsyms_offsets
-00000000000019bd0 R kallsyms_relative_base
+00000000000019bc8 R kallsyms_relative_base
00000000000000008 R kallsyms_num_syms
00000000000000008 R kallsyms_names
-0000000000004c468 R kallsyms_markers
+0000000000004c460 R kallsyms_markers
000000000000001a0 R kallsyms_token_table
00000000000000368 R kallsyms_token_index
000000000000838c8 R __start_ro_after_init
@@ -43349,9 +43349,9 @@
00000000000000000 R __start___modver
00000000000000000 R __stop___param
00000000000000008 r __modver_attr
-00000000000000008 R __stop___modver
-00000000000000ff0 R __end_rodata
+00000000000000008 R __end_rodata
00000000000000000 R __start___ex_table
+00000000000000000 R __stop___modver
00000000000001c78 R __start_notes
00000000000000000 R __stop___ex_table
00000000000000024 r _note_54
@@ -43359,11 +43359,11 @@
0000000000000034c T idmap_pg_dir
00000000000003000 T tramp_pg_dir
00000000000001000 T swapper_pg_dir
-00000000000001000 T swapper_pg_end
-0000000000000f000 T __init_begin
+00000000000001000 T __init_begin
00000000000000000 T __inittext_begin
00000000000000000 T _sinittext
00000000000000000 T stext
+00000000000000000 T swapper_pg_end
00000000000000020 t preserve_boot_args
00000000000000020 t __create_page_tables
000000000000002e4 t __primary_switched
...

Signed-off-by: Domenico Andreoli <domenico.andreoli@xxxxxxxxx>

---
scripts/link-vmlinux.sh | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)

Index: b/scripts/link-vmlinux.sh
===================================================================
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -124,6 +124,25 @@ kallsyms()
mksysmap()
{
${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
+
+ local KSYM_ADDR
+ local KSYM_ADDR_SIZE
+ local KSYM_ADDR_PREV
+ local KSYM_REMAINDER
+
+ # Replace the symbol's address with the offset from the previous
+ # so that in case of "Inconsistent kallsyms data" it's easier
+ # to spot symbols moving around
+ while read KSYM_ADDR KSYM_REMAINDER; do
+ if [ -z "${KSYM_ADDR_PREV}" ]; then
+ KSYM_ADDR_SIZE=`echo ${KSYM_ADDR} | wc -c`
+ KSYM_ADDR_PREV=${KSYM_ADDR}
+ fi
+ printf "%0${KSYM_ADDR_SIZE}x " \
+ $(( 0x${KSYM_ADDR} - 0x${KSYM_ADDR_PREV} ))
+ echo ${KSYM_REMAINDER}
+ KSYM_ADDR_PREV=${KSYM_ADDR}
+ done <${2} >${2}.relative
}

sortextable()
@@ -134,10 +153,10 @@ sortextable()
# Delete output files in case of error
cleanup()
{
- rm -f .tmp_System.map
+ rm -f .tmp_System.map*
rm -f .tmp_kallsyms*
rm -f .tmp_vmlinux*
- rm -f System.map
+ rm -f System.map*
rm -f vmlinux
rm -f vmlinux.o
}
@@ -240,7 +259,6 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then
kallsyms_vmlinux=.tmp_vmlinux3

vmlinux_link .tmp_kallsyms2.o .tmp_vmlinux3
-
kallsyms .tmp_vmlinux3 .tmp_kallsyms3.o
fi
fi
@@ -262,7 +280,8 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then

if ! cmp -s System.map .tmp_System.map; then
echo >&2 Inconsistent kallsyms data
- echo >&2 Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
+ echo >&2 Try \"make KALLSYMS_EXTRA_PASS=1\" as a workaround
+ diff -u .tmp_System.map.relative System.map.relative
exit 1
fi
fi