Skip to content

Commit 1fdd743

Browse files
yonghong-songmasahir0y
authored andcommitted
kbuild: add an elfnote for whether vmlinux is built with lto
Currently, clang LTO built vmlinux won't work with pahole. LTO introduced cross-cu dwarf tag references and broke current pahole model which handles one cu as a time. The solution is to merge all cu's as one pahole cu as in [1]. We would like to do this merging only if cross-cu dwarf references happens. The LTO build mode is a pretty good indication for that. In earlier version of this patch ([2]), clang flag -grecord-gcc-switches is proposed to add to compilation flags so pahole could detect "-flto" and then merging cu's. This will increate the binary size of 1% without LTO though. Arnaldo suggested to use a note to indicate the vmlinux is built with LTO. Such a cheap way to get whether the vmlinux is built with LTO or not helps pahole but is also useful for tracing as LTO may inline/delete/demote global functions, promote static functions, etc. So this patch added an elfnote with a new type LINUX_ELFNOTE_LTO_INFO. The owner of the note is "Linux". With gcc 8.4.1 and clang trunk, without LTO, I got $ readelf -n vmlinux Displaying notes found in: .notes Owner Data size Description ... Linux 0x00000004 func description data: 00 00 00 00 ... With "readelf -x ".notes" vmlinux", I can verify the above "func" with type code 0x101. With clang thin-LTO, I got the same as above except the following: description data: 01 00 00 00 which indicates the vmlinux is built with LTO. [1] https://lore.kernel.org/bpf/[email protected]/ [2] https://lore.kernel.org/bpf/[email protected]/ Suggested-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Tested-by: Sedat Dilek <[email protected]> # LLVM/Clang v12.0.0-rc4 (x86-64) Tested-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 6e74bc4 commit 1fdd743

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

include/linux/elfnote-lto.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef __ELFNOTE_LTO_H
2+
#define __ELFNOTE_LTO_H
3+
4+
#include <linux/elfnote.h>
5+
6+
#define LINUX_ELFNOTE_LTO_INFO 0x101
7+
8+
#ifdef CONFIG_LTO
9+
#define BUILD_LTO_INFO ELFNOTE32("Linux", LINUX_ELFNOTE_LTO_INFO, 1)
10+
#else
11+
#define BUILD_LTO_INFO ELFNOTE32("Linux", LINUX_ELFNOTE_LTO_INFO, 0)
12+
#endif
13+
14+
#endif /* __ELFNOTE_LTO_H */

init/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <generated/compile.h>
1111
#include <linux/build-salt.h>
12+
#include <linux/elfnote-lto.h>
1213
#include <linux/export.h>
1314
#include <linux/uts.h>
1415
#include <linux/utsname.h>
@@ -45,3 +46,4 @@ const char linux_proc_banner[] =
4546
" (" LINUX_COMPILER ") %s\n";
4647

4748
BUILD_SALT;
49+
BUILD_LTO_INFO;

scripts/mod/modpost.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,10 +2193,12 @@ static void add_header(struct buffer *b, struct module *mod)
21932193
*/
21942194
buf_printf(b, "#define INCLUDE_VERMAGIC\n");
21952195
buf_printf(b, "#include <linux/build-salt.h>\n");
2196+
buf_printf(b, "#include <linux/elfnote-lto.h>\n");
21962197
buf_printf(b, "#include <linux/vermagic.h>\n");
21972198
buf_printf(b, "#include <linux/compiler.h>\n");
21982199
buf_printf(b, "\n");
21992200
buf_printf(b, "BUILD_SALT;\n");
2201+
buf_printf(b, "BUILD_LTO_INFO;\n");
22002202
buf_printf(b, "\n");
22012203
buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
22022204
buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");

0 commit comments

Comments
 (0)