Skip to contentSkip to conversation

kbuild: add CONFIG_VMLINUX_MAP expert option

PATCH Superseded Rasmus Villemoes 2 participants 5 messages Feb 24, 2021 – Mar 5, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: add CONFIG_VMLINUX_MAP expert option

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.

---
 .gitignore              | 1 +
 Makefile                | 3 ++-
 lib/Kconfig.debug       | 9 +++++++++
 scripts/link-vmlinux.sh | 9 +++++++++
 4 files changed, 21 insertions(+), 1 deletion(-)
.gitignore +1 -0
index 3af66272d6f1..d3038aff4485 100644
@@ -58,6 +58,7 @@ modules.order
 /TAGS
 /linux
 /vmlinux
+/vmlinux.map
 /vmlinux.32
 /vmlinux.symvers
 /vmlinux-gdb.py
Makefile +2 -1
index b18dbc634690..be6fbd99a214 100644
@@ -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          \
lib/Kconfig.debug +9 -0
index 5ea0c1773b0a..d6af084c11ae 100644
@@ -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
scripts/link-vmlinux.sh +9 -0
index 3b261b0f74f0..bba58839db40 100755
@@ -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
 }
 
Signature · 1 line
2.29.2
#1 lore raw
Quoted 9 lines · Rasmus Villemoes

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.

ping

#2 lore raw
In reply to Rasmus Villemoes
Quoted 24 lines · Rasmus Villemoes

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.

---
 .gitignore              | 1 +
 Makefile                | 3 ++-
 lib/Kconfig.debug       | 9 +++++++++
 scripts/link-vmlinux.sh | 9 +++++++++
 4 files changed, 21 insertions(+), 1 deletion(-)
.gitignore +0 -0
index 3af66272d6f1..d3038aff4485 100644

.gitignore modules.order · line 61

Quoted 4 lines
.gitignore +1 -0
@@ -58,6 +58,7 @@ modules.order
 /TAGS
 /linux
 /vmlinux
+/vmlinux.map

Could you move this one line below
so that vmlinux.map is placed between
vmlinux.32 and vmlinux.symvers ?

I know this list is not sorted...

.gitignore modules.order · line 64

Quoted 3 lines
.gitignore +0 -0
 /vmlinux.32
 /vmlinux.symvers
 /vmlinux-gdb.py

Please add this to Documentation/dontdiff as well.

Makefile endif # CONFIG_MODULES · line 1505

Quoted 11 lines
Makefile +2 -1
index b18dbc634690..be6fbd99a214 100644
@@ -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

Do you need this ?

You already added this to cleanup() of scripts/link-vmlinux.sh, which is invoked from 'make clean'.

lib/Kconfig.debug config VMLINUX_VALIDATION · line 416

Quoted 12 lines
Makefile +0 -0
 # Directories & files removed with 'make mrproper'
 MRPROPER_FILES += include/config include/generated          \
lib/Kconfig.debug +2 -0
index 5ea0c1773b0a..d6af084c11ae 100644
@@ -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

Please use depends on EXPERT
because VMLINUX_MAP is not select or implied by anyone.

Quoted 15 lines
lib/Kconfig.debug +7 -0
+       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
scripts/link-vmlinux.sh +0 -0
index 3b261b0f74f0..bba58839db40 100755

scripts/link-vmlinux.sh vmlinux_link() · line 174

Quoted 9 lines
scripts/link-vmlinux.sh +6 -0
@@ -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
+

For consistency, please do like strip_debug, that is, define this as a local variable, and
set the value of the 'if' case.

Quoted 30 lines
scripts/link-vmlinux.sh +3 -1
        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

Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw

PATCH v2 1/1 kbuild: add CONFIG_VMLINUX_MAP expert option

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 make it depend on CONFIG_EXPERT.

---
 .gitignore              |  1 +
 Documentation/dontdiff  |  1 +
 lib/Kconfig.debug       | 10 ++++++++++
 scripts/link-vmlinux.sh |  8 ++++++++
 4 files changed, 20 insertions(+)
.gitignore +1 -0
index 3af66272d6f1..3adea59847ce 100644
@@ -59,6 +59,7 @@ modules.order
 /linux
 /vmlinux
 /vmlinux.32
+/vmlinux.map
 /vmlinux.symvers
 /vmlinux-gdb.py
 /vmlinuz
Documentation/dontdiff +1 -0
index e361fc95ca29..ac42ad8d430d 100644
@@ -252,6 +252,7 @@ vmlinux-*
 vmlinux.aout
 vmlinux.bin.all
 vmlinux.lds
+vmlinux.map
 vmlinux.symvers
 vmlinuz
 voffset.h
lib/Kconfig.debug +10 -0
index 5ea0c1773b0a..663c1cd5018c 100644
@@ -412,6 +412,16 @@ config VMLINUX_VALIDATION
 	depends on STACK_VALIDATION && DEBUG_ENTRY && !PARAVIRT
 	default y
 
+config VMLINUX_MAP
+	bool "Generate vmlinux.map file when linking"
+	depends on 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
scripts/link-vmlinux.sh +8 -0
index 3b261b0f74f0..855fd4e6f03e 100755
@@ -155,6 +155,7 @@ vmlinux_link()
 	local output=${1}
 	local objects
 	local strip_debug
+	local map_option
 
 	info LD ${output}
 
@@ -166,6 +167,10 @@ vmlinux_link()
 		strip_debug=-Wl,--strip-debug
 	fi
 
+	if [ -n "${CONFIG_VMLINUX_MAP}" ]; then
+		map_option="-Map=${output}.map"
+	fi
+
 	if [ "${SRCARCH}" != "um" ]; then
 		if [ -n "${CONFIG_LTO_CLANG}" ]; then
 			# Use vmlinux.o instead of performing the slow LTO
@@ -187,6 +192,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 +206,7 @@ vmlinux_link()
 		${CC} ${CFLAGS_vmlinux}				\
 			${strip_debug}				\
 			-o ${output}				\
+			${map_option:+-Wl,${map_option}}	\
 			-Wl,-T,${lds}				\
 			${objects}				\
 			-lutil -lrt -lpthread
@@ -303,6 +310,7 @@ cleanup()
 	rm -f .tmp_vmlinux*
 	rm -f System.map
 	rm -f vmlinux
+	rm -f vmlinux.map
 	rm -f vmlinux.o
 }
 
Signature · 1 line
2.29.2
#4 lore raw
Quoted 6 lines · Rasmus Villemoes

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.

Quoted 7 lines

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 make it depend on CONFIG_EXPERT.

---

Applied to linux-kbuild.
Thanks.

Quoted 99 lines
 .gitignore              |  1 +
 Documentation/dontdiff  |  1 +
 lib/Kconfig.debug       | 10 ++++++++++
 scripts/link-vmlinux.sh |  8 ++++++++
 4 files changed, 20 insertions(+)
.gitignore +1 -0
index 3af66272d6f1..3adea59847ce 100644
@@ -59,6 +59,7 @@ modules.order
 /linux
 /vmlinux
 /vmlinux.32
+/vmlinux.map
 /vmlinux.symvers
 /vmlinux-gdb.py
 /vmlinuz
Documentation/dontdiff +1 -0
index e361fc95ca29..ac42ad8d430d 100644
@@ -252,6 +252,7 @@ vmlinux-*
 vmlinux.aout
 vmlinux.bin.all
 vmlinux.lds
+vmlinux.map
 vmlinux.symvers
 vmlinuz
 voffset.h
lib/Kconfig.debug +10 -0
index 5ea0c1773b0a..663c1cd5018c 100644
@@ -412,6 +412,16 @@ config VMLINUX_VALIDATION
        depends on STACK_VALIDATION && DEBUG_ENTRY && !PARAVIRT
        default y
 
+config VMLINUX_MAP
+       bool "Generate vmlinux.map file when linking"
+       depends on 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
scripts/link-vmlinux.sh +8 -1
index 3b261b0f74f0..855fd4e6f03e 100755
@@ -155,6 +155,7 @@ vmlinux_link()
        local output=${1}
        local objects
        local strip_debug
+       local map_option
 
        info LD ${output}
 
@@ -166,6 +167,10 @@ vmlinux_link()
                strip_debug=-Wl,--strip-debug
        fi
 
+       if [ -n "${CONFIG_VMLINUX_MAP}" ]; then
+               map_option="-Map=${output}.map"
+       fi
+
        if [ "${SRCARCH}" != "um" ]; then
                if [ -n "${CONFIG_LTO_CLANG}" ]; then
                        # Use vmlinux.o instead of performing the slow LTO
@@ -187,6 +192,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 +206,7 @@ vmlinux_link()
                ${CC} ${CFLAGS_vmlinux}                         \
                        ${strip_debug}                          \
                        -o ${output}                            \
+                       ${map_option:+-Wl,${map_option}}        \
                        -Wl,-T,${lds}                           \
                        ${objects}                              \
                        -lutil -lrt -lpthread
@@ -303,6 +310,7 @@ cleanup()
        rm -f .tmp_vmlinux*
        rm -f System.map
        rm -f vmlinux
+       rm -f vmlinux.map
        rm -f vmlinux.o
 }
 
--
Quoted 2 lines

2.29.2

--
Best Regards
Masahiro Yamada