summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMasahiro Yamada <yamada.masahiro@socionext.com>2018-11-29 20:05:26 -0500
committerMasahiro Yamada <yamada.masahiro@socionext.com>2018-12-01 09:13:14 -0500
commitbbda5ec671d3fe62faefa1cab7270aa586042a4b (patch)
treea1d375f74ad25efd2797bffaadfb0dc5fc1f65fe
parentee3e46b7efd2954479f87030d31fda3c22bbc763 (diff)
kbuild: simplify dependency generation for CONFIG_TRIM_UNUSED_KSYMS
My main motivation of this commit is to clean up scripts/Kbuild.include and scripts/Makefile.build. Currently, CONFIG_TRIM_UNUSED_KSYMS works with a tricky gimmick; possibly exported symbols are detected by letting $(CPP) replace EXPORT_SYMBOL* with a special string '=== __KSYM_*===', which is post-processed by sed, and passed to fixdep. The extra preprocessing is costly, and hacking cmd_and_fixdep is ugly. I came up with a new way to find exported symbols; insert a dummy symbol __ksym_marker_* to each potentially exported symbol. Those dummy symbols are picked up by $(NM), post-processed by sed, then appended to .*.cmd files. I collected the post-process part to a new shell script scripts/gen_ksymdeps.sh for readability. The dummy symbols are put into the .discard.* section so that the linker script rips them off the final vmlinux or modules. A nice side-effect is building with CONFIG_TRIM_UNUSED_KSYMS will be much faster. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Reviewed-by: Nicolas Pitre <nico@linaro.org>
-rw-r--r--include/asm-generic/export.h13
-rw-r--r--include/linux/export.h18
-rw-r--r--scripts/Kbuild.include28
-rw-r--r--scripts/Makefile.build7
-rw-r--r--scripts/basic/fixdep.c31
-rwxr-xr-xscripts/gen_ksymdeps.sh25
6 files changed, 53 insertions, 69 deletions
diff --git a/include/asm-generic/export.h b/include/asm-generic/export.h
index 4d73e6e3c66c..294d6ae785d4 100644
--- a/include/asm-generic/export.h
+++ b/include/asm-generic/export.h
@@ -59,16 +59,19 @@ __kcrctab_\name:
59.endm 59.endm
60#undef __put 60#undef __put
61 61
62#if defined(__KSYM_DEPS__) 62#if defined(CONFIG_TRIM_UNUSED_KSYMS)
63
64#define __EXPORT_SYMBOL(sym, val, sec) === __KSYM_##sym ===
65
66#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
67 63
68#include <linux/kconfig.h> 64#include <linux/kconfig.h>
69#include <generated/autoksyms.h> 65#include <generated/autoksyms.h>
70 66
67.macro __ksym_marker sym
68 .section ".discard.ksym","a"
69__ksym_marker_\sym:
70 .previous
71.endm
72
71#define __EXPORT_SYMBOL(sym, val, sec) \ 73#define __EXPORT_SYMBOL(sym, val, sec) \
74 __ksym_marker sym; \
72 __cond_export_sym(sym, val, sec, __is_defined(__KSYM_##sym)) 75 __cond_export_sym(sym, val, sec, __is_defined(__KSYM_##sym))
73#define __cond_export_sym(sym, val, sec, conf) \ 76#define __cond_export_sym(sym, val, sec, conf) \
74 ___cond_export_sym(sym, val, sec, conf) 77 ___cond_export_sym(sym, val, sec, conf)
diff --git a/include/linux/export.h b/include/linux/export.h
index ce764a5d2ee4..fd8711ed9ac4 100644
--- a/include/linux/export.h
+++ b/include/linux/export.h
@@ -92,22 +92,22 @@ struct kernel_symbol {
92 */ 92 */
93#define __EXPORT_SYMBOL(sym, sec) 93#define __EXPORT_SYMBOL(sym, sec)
94 94
95#elif defined(__KSYM_DEPS__) 95#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
96
97#include <generated/autoksyms.h>
96 98
97/* 99/*
98 * For fine grained build dependencies, we want to tell the build system 100 * For fine grained build dependencies, we want to tell the build system
99 * about each possible exported symbol even if they're not actually exported. 101 * about each possible exported symbol even if they're not actually exported.
100 * We use a string pattern that is unlikely to be valid code that the build 102 * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
101 * system filters out from the preprocessor output (see ksym_dep_filter 103 * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
102 * in scripts/Kbuild.include). 104 * discarded in the final link stage.
103 */ 105 */
104#define __EXPORT_SYMBOL(sym, sec) === __KSYM_##sym === 106#define __ksym_marker(sym) \
105 107 static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
106#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
107
108#include <generated/autoksyms.h>
109 108
110#define __EXPORT_SYMBOL(sym, sec) \ 109#define __EXPORT_SYMBOL(sym, sec) \
110 __ksym_marker(sym); \
111 __cond_export_sym(sym, sec, __is_defined(__KSYM_##sym)) 111 __cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
112#define __cond_export_sym(sym, sec, conf) \ 112#define __cond_export_sym(sym, sec, conf) \
113 ___cond_export_sym(sym, sec, conf) 113 ___cond_export_sym(sym, sec, conf)
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 6cf6a8b83b97..4b943f4d2226 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -260,39 +260,11 @@ if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
260 @set -e; \ 260 @set -e; \
261 $(cmd_and_fixdep), @:) 261 $(cmd_and_fixdep), @:)
262 262
263ifndef CONFIG_TRIM_UNUSED_KSYMS
264
265cmd_and_fixdep = \ 263cmd_and_fixdep = \
266 $(echo-cmd) $(cmd_$(1)); \ 264 $(echo-cmd) $(cmd_$(1)); \
267 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\ 265 scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
268 rm -f $(depfile); 266 rm -f $(depfile);
269 267
270else
271
272# Filter out exported kernel symbol names from the preprocessor output.
273# See also __KSYM_DEPS__ in include/linux/export.h.
274# We disable the depfile generation here, so as not to overwrite the existing
275# depfile while fixdep is parsing it.
276flags_nodeps = $(filter-out -Wp$(comma)-M%, $($(1)))
277ksym_dep_filter = \
278 case "$(1)" in \
279 cc_*_c|cpp_i_c) \
280 $(CPP) $(call flags_nodeps,c_flags) -D__KSYM_DEPS__ $< ;; \
281 as_*_S|cpp_s_S) \
282 $(CPP) $(call flags_nodeps,a_flags) -D__KSYM_DEPS__ $< ;; \
283 boot*|build*|cpp_its_S|*cpp_lds_S|dtc|host*|vdso*) : ;; \
284 *) echo "Don't know how to preprocess $(1)" >&2; false ;; \
285 esac | tr ";" "\n" | sed -n 's/^.*=== __KSYM_\(.*\) ===.*$$/_\1/p'
286
287cmd_and_fixdep = \
288 $(echo-cmd) $(cmd_$(1)); \
289 $(ksym_dep_filter) | \
290 scripts/basic/fixdep -e $(depfile) $@ '$(make-cmd)' \
291 > $(dot-target).cmd; \
292 rm -f $(depfile);
293
294endif
295
296# Usage: $(call if_changed_rule,foo) 268# Usage: $(call if_changed_rule,foo)
297# Will check if $(cmd_foo) or any of the prerequisites changed, 269# Will check if $(cmd_foo) or any of the prerequisites changed,
298# and if so will execute $(rule_foo). 270# and if so will execute $(rule_foo).
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index cdb25d163b42..23ebf2508234 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -254,9 +254,15 @@ objtool_dep = $(objtool_obj) \
254 $(wildcard include/config/orc/unwinder.h \ 254 $(wildcard include/config/orc/unwinder.h \
255 include/config/stack/validation.h) 255 include/config/stack/validation.h)
256 256
257ifdef CONFIG_TRIM_UNUSED_KSYMS
258cmd_gen_ksymdeps = \
259 $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd;
260endif
261
257define rule_cc_o_c 262define rule_cc_o_c
258 $(call echo-cmd,checksrc) $(cmd_checksrc) \ 263 $(call echo-cmd,checksrc) $(cmd_checksrc) \
259 $(call cmd_and_fixdep,cc_o_c) \ 264 $(call cmd_and_fixdep,cc_o_c) \
265 $(cmd_gen_ksymdeps) \
260 $(cmd_checkdoc) \ 266 $(cmd_checkdoc) \
261 $(call echo-cmd,objtool) $(cmd_objtool) \ 267 $(call echo-cmd,objtool) $(cmd_objtool) \
262 $(cmd_modversions_c) \ 268 $(cmd_modversions_c) \
@@ -265,6 +271,7 @@ endef
265 271
266define rule_as_o_S 272define rule_as_o_S
267 $(call cmd_and_fixdep,as_o_S) \ 273 $(call cmd_and_fixdep,as_o_S) \
274 $(cmd_gen_ksymdeps) \
268 $(call echo-cmd,objtool) $(cmd_objtool) \ 275 $(call echo-cmd,objtool) $(cmd_objtool) \
269 $(cmd_modversions_S) 276 $(cmd_modversions_S)
270endef 277endef
diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c
index 850966f3d602..facbd603adf6 100644
--- a/scripts/basic/fixdep.c
+++ b/scripts/basic/fixdep.c
@@ -105,8 +105,7 @@
105 105
106static void usage(void) 106static void usage(void)
107{ 107{
108 fprintf(stderr, "Usage: fixdep [-e] <depfile> <target> <cmdline>\n"); 108 fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
109 fprintf(stderr, " -e insert extra dependencies given on stdin\n");
110 exit(1); 109 exit(1);
111} 110}
112 111
@@ -131,21 +130,6 @@ static void print_dep(const char *m, int slen, const char *dir)
131 printf(".h) \\\n"); 130 printf(".h) \\\n");
132} 131}
133 132
134static void do_extra_deps(void)
135{
136 char buf[80];
137
138 while (fgets(buf, sizeof(buf), stdin)) {
139 int len = strlen(buf);
140
141 if (len < 2 || buf[len - 1] != '\n') {
142 fprintf(stderr, "fixdep: bad data on stdin\n");
143 exit(1);
144 }
145 print_dep(buf, len - 1, "include/ksym");
146 }
147}
148
149struct item { 133struct item {
150 struct item *next; 134 struct item *next;
151 unsigned int len; 135 unsigned int len;
@@ -293,7 +277,7 @@ static int is_ignored_file(const char *s, int len)
293 * assignments are parsed not only by make, but also by the rather simple 277 * assignments are parsed not only by make, but also by the rather simple
294 * parser in scripts/mod/sumversion.c. 278 * parser in scripts/mod/sumversion.c.
295 */ 279 */
296static void parse_dep_file(char *m, const char *target, int insert_extra_deps) 280static void parse_dep_file(char *m, const char *target)
297{ 281{
298 char *p; 282 char *p;
299 int is_last, is_target; 283 int is_last, is_target;
@@ -369,9 +353,6 @@ static void parse_dep_file(char *m, const char *target, int insert_extra_deps)
369 exit(1); 353 exit(1);
370 } 354 }
371 355
372 if (insert_extra_deps)
373 do_extra_deps();
374
375 printf("\n%s: $(deps_%s)\n\n", target, target); 356 printf("\n%s: $(deps_%s)\n\n", target, target);
376 printf("$(deps_%s):\n", target); 357 printf("$(deps_%s):\n", target);
377} 358}
@@ -379,13 +360,9 @@ static void parse_dep_file(char *m, const char *target, int insert_extra_deps)
379int main(int argc, char *argv[]) 360int main(int argc, char *argv[])
380{ 361{
381 const char *depfile, *target, *cmdline; 362 const char *depfile, *target, *cmdline;
382 int insert_extra_deps = 0;
383 void *buf; 363 void *buf;
384 364
385 if (argc == 5 && !strcmp(argv[1], "-e")) { 365 if (argc != 4)
386 insert_extra_deps = 1;
387 argv++;
388 } else if (argc != 4)
389 usage(); 366 usage();
390 367
391 depfile = argv[1]; 368 depfile = argv[1];
@@ -395,7 +372,7 @@ int main(int argc, char *argv[])
395 printf("cmd_%s := %s\n\n", target, cmdline); 372 printf("cmd_%s := %s\n\n", target, cmdline);
396 373
397 buf = read_file(depfile); 374 buf = read_file(depfile);
398 parse_dep_file(buf, target, insert_extra_deps); 375 parse_dep_file(buf, target);
399 free(buf); 376 free(buf);
400 377
401 return 0; 378 return 0;
diff --git a/scripts/gen_ksymdeps.sh b/scripts/gen_ksymdeps.sh
new file mode 100755
index 000000000000..1324986e1362
--- /dev/null
+++ b/scripts/gen_ksymdeps.sh
@@ -0,0 +1,25 @@
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4set -e
5
6# List of exported symbols
7ksyms=$($NM $1 | sed -n 's/.*__ksym_marker_\(.*\)/\1/p' | tr A-Z a-z)
8
9if [ -z "$ksyms" ]; then
10 exit 0
11fi
12
13echo
14echo "ksymdeps_$1 := \\"
15
16for s in $ksyms
17do
18 echo $s | sed -e 's:^_*: $(wildcard include/ksym/:' \
19 -e 's:__*:/:g' -e 's/$/.h) \\/'
20done
21
22echo
23echo "$1: \$(ksymdeps_$1)"
24echo
25echo "\$(ksymdeps_$1):"