kbuild: fix UNUSED_KSYMS_WHITELIST for Clang LTO

Commit fbe078d397b4 ("kbuild: lto: add a default list of used symbols")
does not work as expected if the .config file has already specified
CONFIG_UNUSED_KSYMS_WHITELIST="my/own/white/list" before enabling
CONFIG_LTO_CLANG.

So, the user-supplied whitelist and LTO-specific white list must be
independent of each other.

I refactored the shell script so CONFIG_MODVERSIONS and CONFIG_CLANG_LTO
handle whitelists in the same way.

Fixes: fbe078d397b4 ("kbuild: lto: add a default list of used symbols")
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>

+26 -16
-1
init/Kconfig
··· 2283 2283 config UNUSED_KSYMS_WHITELIST 2284 2284 string "Whitelist of symbols to keep in ksymtab" 2285 2285 depends on TRIM_UNUSED_KSYMS 2286 - default "scripts/lto-used-symbollist.txt" if LTO_CLANG 2287 2286 help 2288 2287 By default, all unused exported symbols will be un-exported from the 2289 2288 build when TRIM_UNUSED_KSYMS is selected.
+26 -9
scripts/gen_autoksyms.sh
··· 19 19 # We need access to CONFIG_ symbols 20 20 . include/config/auto.conf 21 21 22 - ksym_wl=/dev/null 22 + needed_symbols= 23 + 24 + # Special case for modversions (see modpost.c) 25 + if [ -n "$CONFIG_MODVERSIONS" ]; then 26 + needed_symbols="$needed_symbols module_layout" 27 + fi 28 + 29 + # With CONFIG_LTO_CLANG, LLVM bitcode has not yet been compiled into a binary 30 + # when the .mod files are generated, which means they don't yet contain 31 + # references to certain symbols that will be present in the final binaries. 32 + if [ -n "$CONFIG_LTO_CLANG" ]; then 33 + # intrinsic functions 34 + needed_symbols="$needed_symbols memcpy memmove memset" 35 + # ftrace 36 + needed_symbols="$needed_symbols _mcount" 37 + # stack protector symbols 38 + needed_symbols="$needed_symbols __stack_chk_fail __stack_chk_guard" 39 + fi 40 + 41 + ksym_wl= 23 42 if [ -n "$CONFIG_UNUSED_KSYMS_WHITELIST" ]; then 24 43 # Use 'eval' to expand the whitelist path and check if it is relative 25 44 eval ksym_wl="$CONFIG_UNUSED_KSYMS_WHITELIST" ··· 59 40 EOT 60 41 61 42 [ -f modules.order ] && modlist=modules.order || modlist=/dev/null 62 - sed 's/ko$/mod/' $modlist | 63 - xargs -n1 sed -n -e '2{s/ /\n/g;/^$/!p;}' -- | 64 - cat - "$ksym_wl" | 43 + 44 + { 45 + sed 's/ko$/mod/' $modlist | xargs -n1 sed -n -e '2p' 46 + echo "$needed_symbols" 47 + [ -n "$ksym_wl" ] && cat "$ksym_wl" 48 + } | sed -e 's/ /\n/g' | sed -n -e '/^$/!p' | 65 49 # Remove the dot prefix for ppc64; symbol names with a dot (.) hold entry 66 50 # point addresses. 67 51 sed -e 's/^\.//' | 68 52 sort -u | 69 53 sed -e 's/\(.*\)/#define __KSYM_\1 1/' >> "$output_file" 70 - 71 - # Special case for modversions (see modpost.c) 72 - if [ -n "$CONFIG_MODVERSIONS" ]; then 73 - echo "#define __KSYM_module_layout 1" >> "$output_file" 74 - fi
-6
scripts/lto-used-symbollist.txt
··· 1 - memcpy 2 - memmove 3 - memset 4 - _mcount 5 - __stack_chk_fail 6 - __stack_chk_guard