Skip to content

Commit 31cb50b

Browse files
committed
kbuild: check static EXPORT_SYMBOL* by script instead of modpost
The 'static' specifier and EXPORT_SYMBOL() are an odd combination. Commit 15bfc23 ("modpost: check for static EXPORT_SYMBOL* functions") tried to detect it, but this check has false negatives. Here is the sample code. Makefile: obj-y += foo1.o foo2.o foo1.c: #include <linux/export.h> static void foo(void) {} EXPORT_SYMBOL(foo); foo2.c: void foo(void) {} foo1.c exports the static symbol 'foo', but modpost cannot catch it because it is fooled by foo2.c, which has a global symbol with the same name. s->is_static is cleared if a global symbol with the same name is found somewhere, but EXPORT_SYMBOL() and the global symbol do not necessarily belong to the same compilation unit. This check should be done per compilation unit, but I do not know how to do it in modpost. modpost runs against vmlinux.o or modules, which merges multiple objects, then forgets their origin. modpost cannot parse individual objects because they may not be ELF but LLVM IR when CONFIG_LTO_CLANG=y. Add a simple bash script to parse the output from ${NM}. This works for CONFIG_LTO_CLANG=y because llvm-nm can dump symbols of LLVM IR files. Revert 15bfc23. Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Tested-by: Nathan Chancellor <[email protected]> Tested-by: Sedat Dilek <[email protected]> # LLVM-14 (x86-64)
1 parent 534671e commit 31cb50b

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

scripts/Makefile.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,12 @@ cmd_gen_ksymdeps = \
249249
$(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd
250250
endif
251251

252+
cmd_check_local_export = $(srctree)/scripts/check-local-export $@
253+
252254
define rule_cc_o_c
253255
$(call cmd_and_fixdep,cc_o_c)
254256
$(call cmd,gen_ksymdeps)
257+
$(call cmd,check_local_export)
255258
$(call cmd,checksrc)
256259
$(call cmd,checkdoc)
257260
$(call cmd,gen_objtooldep)
@@ -262,6 +265,7 @@ endef
262265
define rule_as_o_S
263266
$(call cmd_and_fixdep,as_o_S)
264267
$(call cmd,gen_ksymdeps)
268+
$(call cmd,check_local_export)
265269
$(call cmd,gen_objtooldep)
266270
$(call cmd,gen_symversions_S)
267271
endef

scripts/check-local-export

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0-only
3+
#
4+
# Copyright (C) 2022 Masahiro Yamada <[email protected]>
5+
#
6+
# Exit with error if a local exported symbol is found.
7+
# EXPORT_SYMBOL should be used for global symbols.
8+
9+
set -e
10+
11+
declare -A symbol_types
12+
declare -a export_symbols
13+
14+
exit_code=0
15+
16+
while read value type name
17+
do
18+
# Skip the line if the number of fields is less than 3.
19+
#
20+
# case 1)
21+
# For undefined symbols, the first field (value) is empty.
22+
# The outout looks like this:
23+
# " U _printk"
24+
# It is unneeded to record undefined symbols.
25+
#
26+
# case 2)
27+
# For Clang LTO, llvm-nm outputs a line with type 't' but empty name:
28+
# "---------------- t"
29+
if [[ -z ${name} ]]; then
30+
continue
31+
fi
32+
33+
# save (name, type) in the associative array
34+
symbol_types[${name}]=${type}
35+
36+
# append the exported symbol to the array
37+
if [[ ${name} == __ksymtab_* ]]; then
38+
export_symbols+=(${name#__ksymtab_})
39+
fi
40+
41+
# If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm)
42+
# shows 'no symbols' diagnostic (but exits with 0). It is harmless and
43+
# hidden by '2>/dev/null'. However, it suppresses real error messages
44+
# as well. Add a hand-crafted error message here.
45+
#
46+
# Use --quiet instead of 2>/dev/null when we upgrade the minimum version
47+
# of binutils to 2.37, llvm to 13.0.0.
48+
#
49+
# Then, the following line will be really simple:
50+
# done < <(${NM} --quiet ${1})
51+
done < <(${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } )
52+
53+
# Catch error in the process substitution
54+
wait $!
55+
56+
for name in "${export_symbols[@]}"
57+
do
58+
# nm(3) says "If lowercase, the symbol is usually local"
59+
if [[ ${symbol_types[$name]} =~ [a-z] ]]; then
60+
echo "$@: error: local symbol '${name}' was exported" >&2
61+
exit_code=1
62+
fi
63+
done
64+
65+
exit ${exit_code}

scripts/mod/modpost.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ struct symbol {
212212
unsigned int crc;
213213
bool crc_valid;
214214
bool weak;
215-
bool is_static; /* true if symbol is not global */
216215
bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */
217216
char name[];
218217
};
@@ -242,7 +241,7 @@ static struct symbol *alloc_symbol(const char *name)
242241

243242
memset(s, 0, sizeof(*s));
244243
strcpy(s->name, name);
245-
s->is_static = true;
244+
246245
return s;
247246
}
248247

@@ -2064,20 +2063,6 @@ static void read_symbols(const char *modname)
20642063
sym_get_data(&info, sym));
20652064
}
20662065

2067-
// check for static EXPORT_SYMBOL_* functions && global vars
2068-
for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2069-
unsigned char bind = ELF_ST_BIND(sym->st_info);
2070-
2071-
if (bind == STB_GLOBAL || bind == STB_WEAK) {
2072-
struct symbol *s =
2073-
find_symbol(remove_dot(info.strtab +
2074-
sym->st_name));
2075-
2076-
if (s)
2077-
s->is_static = false;
2078-
}
2079-
}
2080-
20812066
check_sec_ref(modname, &info);
20822067

20832068
if (!mod->is_vmlinux) {
@@ -2507,7 +2492,6 @@ static void read_dump(const char *fname)
25072492
mod->from_dump = true;
25082493
}
25092494
s = sym_add_exported(symname, mod, gpl_only);
2510-
s->is_static = false;
25112495
sym_set_crc(s, crc);
25122496
sym_update_namespace(symname, namespace);
25132497
}
@@ -2572,7 +2556,6 @@ int main(int argc, char **argv)
25722556
char *missing_namespace_deps = NULL;
25732557
char *dump_write = NULL, *files_source = NULL;
25742558
int opt;
2575-
int n;
25762559
LIST_HEAD(dump_lists);
25772560
struct dump_list *dl, *dl2;
25782561

@@ -2648,15 +2631,6 @@ int main(int argc, char **argv)
26482631
if (sec_mismatch_count && !sec_mismatch_warn_only)
26492632
error("Section mismatches detected.\n"
26502633
"Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2651-
for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2652-
struct symbol *s;
2653-
2654-
for (s = symbolhash[n]; s; s = s->next) {
2655-
if (s->is_static)
2656-
error("\"%s\" [%s] is a static EXPORT_SYMBOL\n",
2657-
s->name, s->module->name);
2658-
}
2659-
}
26602634

26612635
if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
26622636
warn("suppressed %u unresolved symbol warnings because there were too many)\n",

0 commit comments

Comments
 (0)