[PATCH] kbuild: add CONFIG_VMLINUX_MAP expert option

From: Rasmus Villemoes
Date: Wed Feb 24 2021 - 05:53:57 EST


It can be quite useful to have ld emit a link map file, in order to
debug or verify that special sections end up where they are supposed
to, and to see what LD_DEAD_CODE_DATA_ELIMINATION manages to get rid
of.

The only reason I'm not just adding this unconditionally is that the
.map file can be rather large (several MB), and that's a waste of
space when one isn't interested in these things. Also hide the prompt
behind CONFIG_EXPERT.

Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
.gitignore | 1 +
Makefile | 3 ++-
lib/Kconfig.debug | 9 +++++++++
scripts/link-vmlinux.sh | 9 +++++++++
4 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 3af66272d6f1..d3038aff4485 100644
--- a/.gitignore
+++ b/.gitignore
@@ -58,6 +58,7 @@ modules.order
/TAGS
/linux
/vmlinux
+/vmlinux.map
/vmlinux.32
/vmlinux.symvers
/vmlinux-gdb.py
diff --git a/Makefile b/Makefile
index b18dbc634690..be6fbd99a214 100644
--- a/Makefile
+++ b/Makefile
@@ -1501,7 +1501,8 @@ endif # CONFIG_MODULES
# Directories & files removed with 'make clean'
CLEAN_FILES += include/ksym vmlinux.symvers \
modules.builtin modules.builtin.modinfo modules.nsdeps \
- compile_commands.json
+ compile_commands.json \
+ vmlinux.map

# Directories & files removed with 'make mrproper'
MRPROPER_FILES += include/config include/generated \
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 5ea0c1773b0a..d6af084c11ae 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -412,6 +412,15 @@ config VMLINUX_VALIDATION
depends on STACK_VALIDATION && DEBUG_ENTRY && !PARAVIRT
default y

+config VMLINUX_MAP
+ bool "Generate vmlinux.map file when linking" if EXPERT
+ help
+ Selecting this option will pass "-Map=vmlinux.map" to ld
+ when linking vmlinux. That file can be useful for verifying
+ and debugging magic section games, and for seeing which
+ pieces of code get eliminated with
+ CONFIG_LD_DEAD_CODE_DATA_ELIMINATION.
+
config DEBUG_FORCE_WEAK_PER_CPU
bool "Force weak per-cpu definitions"
depends on DEBUG_KERNEL
diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
index 3b261b0f74f0..bba58839db40 100755
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -166,6 +166,12 @@ vmlinux_link()
strip_debug=-Wl,--strip-debug
fi

+ if [ -n "${CONFIG_VMLINUX_MAP}" ]; then
+ map_option="-Map=${output}.map"
+ else
+ map_option=""
+ fi
+
if [ "${SRCARCH}" != "um" ]; then
if [ -n "${CONFIG_LTO_CLANG}" ]; then
# Use vmlinux.o instead of performing the slow LTO
@@ -187,6 +193,7 @@ vmlinux_link()
${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
${strip_debug#-Wl,} \
-o ${output} \
+ ${map_option} \
-T ${lds} ${objects}
else
objects="-Wl,--whole-archive \
@@ -200,6 +207,7 @@ vmlinux_link()
${CC} ${CFLAGS_vmlinux} \
${strip_debug} \
-o ${output} \
+ ${map_option:+-Wl,${map_option}} \
-Wl,-T,${lds} \
${objects} \
-lutil -lrt -lpthread
@@ -303,6 +311,7 @@ cleanup()
rm -f .tmp_vmlinux*
rm -f System.map
rm -f vmlinux
+ rm -f vmlinux.map
rm -f vmlinux.o
}

--
2.29.2