[PATCH v2] Improve diagnosis when "Inconsistent kallsyms data" happens

From: Domenico Andreoli
Date: Fri May 24 2019 - 03:58:44 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
SYSMAP .tmp_System.map.relative
SYSMAP System.map.relative
Inconsistent kallsyms data
Try "make KALLSYMS_EXTRA_PASS=1" as a workaround
----- .tmp_System.map.relative 2019-05-24 09:43:45.249819728 +0200
+++++ System.map.relative 2019-05-24 09:43:42.196968172 +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
...


Changes in v2:
- *.relative files are generated by a separate function and only when
needed, this saves time and does not leave unused files around in
case of successful build


Signed-off-by: Domenico Andreoli <domenico.andreoli@xxxxxxxxx>
---
scripts/link-vmlinux.sh | 33 ++++++++++++++++++++++++++++++---
1 file changed, 30 insertions(+), 3 deletions(-)

Index: b/scripts/link-vmlinux.sh
===================================================================
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -126,6 +126,28 @@ mksysmap()
${CONFIG_SHELL} "${srctree}/scripts/mksysmap" ${1} ${2}
}

+# 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
+mksysmap_relative()
+{
+ local addr
+ local addr_size
+ local addr_prev
+ local remainder
+
+ info SYSMAP ${2}
+ while read addr remainder; do
+ if [ -z "${addr_prev}" ]; then
+ addr_size=`echo ${addr} | wc -c`
+ addr_prev=${addr}
+ fi
+ printf "%0${addr_size}x " $(( 0x${addr} - 0x${addr_prev} ))
+ echo ${remainder}
+ addr_prev=${addr}
+ done <${1} >${2}
+}
+
sortextable()
{
${objtree}/scripts/sortextable ${1}
@@ -134,10 +156,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
}
@@ -261,8 +283,13 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then
mksysmap ${kallsyms_vmlinux} .tmp_System.map

if ! cmp -s System.map .tmp_System.map; then
+ mksysmap_relative .tmp_System.map .tmp_System.map.relative
+ mksysmap_relative System.map System.map.relative
+
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 >&2 -u .tmp_System.map.relative System.map.relative
+
exit 1
fi
fi