Skip to content

Commit 2a66c31

Browse files
committed
modpost: change the license of EXPORT_SYMBOL to bool type
There were more EXPORT_SYMBOL types in the past. The following commits removed unused ones. - f1c3d73 ("module: remove EXPORT_SYMBOL_GPL_FUTURE") - 3679482 ("module: remove EXPORT_UNUSED_SYMBOL*") There are 3 remaining in enum export, but export_unknown does not make any sense because we never expect such a situation like "we do not know how it was exported". If the symbol name starts with "__ksymtab_", but the section name does not start with "___ksymtab+" or "___ksymtab_gpl+", it is not an exported symbol. It occurs when a variable starting with "__ksymtab_" is directly defined: int __ksymtab_foo; Presumably, there is no practical issue for using such a weird variable name (but there is no good reason for doing so, either). Anyway, that is not an exported symbol. Setting export_unknown is not the right thing to do. Do not call sym_add_exported() in this case. With pointless export_unknown removed, the export type finally becomes boolean (either EXPORT_SYMBOL or EXPORT_SYMBOL_GPL). I renamed the field name to is_gpl_only. EXPORT_SYMBOL_GPL sets it true. Only GPL-compatible modules can use it. I removed the orphan comment, "How a symbol is exported", which is unrelated to sec_mismatch_count. It is about enum export. See commit bd5cbce ("kbuild: export-type enhancement to modpost.c") Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nicolas Schier <[email protected]> Tested-by: Nathan Chancellor <[email protected]>
1 parent ce79c40 commit 2a66c31

File tree

1 file changed

+30
-78
lines changed

1 file changed

+30
-78
lines changed

scripts/mod/modpost.c

Lines changed: 30 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static bool all_versions;
3030
static bool external_module;
3131
/* Only warn about unresolved symbols */
3232
static bool warn_unresolved;
33-
/* How a symbol is exported */
33+
3434
static int sec_mismatch_count;
3535
static bool sec_mismatch_warn_only = true;
3636
/* ignore missing files */
@@ -47,12 +47,6 @@ static bool error_occurred;
4747
#define MAX_UNRESOLVED_REPORTS 10
4848
static unsigned int nr_unresolved;
4949

50-
enum export {
51-
export_plain,
52-
export_gpl,
53-
export_unknown
54-
};
55-
5650
/* In kernel, this size is defined in linux/module.h;
5751
* here we use Elf_Addr instead of long for covering cross-compile
5852
*/
@@ -219,7 +213,7 @@ struct symbol {
219213
bool crc_valid;
220214
bool weak;
221215
bool is_static; /* true if symbol is not global */
222-
enum export export; /* Type of export */
216+
bool is_gpl_only; /* exported by EXPORT_SYMBOL_GPL */
223217
char name[];
224218
};
225219

@@ -316,34 +310,6 @@ static void add_namespace(struct list_head *head, const char *namespace)
316310
}
317311
}
318312

319-
static const struct {
320-
const char *str;
321-
enum export export;
322-
} export_list[] = {
323-
{ .str = "EXPORT_SYMBOL", .export = export_plain },
324-
{ .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
325-
{ .str = "(unknown)", .export = export_unknown },
326-
};
327-
328-
329-
static const char *export_str(enum export ex)
330-
{
331-
return export_list[ex].str;
332-
}
333-
334-
static enum export export_no(const char *s)
335-
{
336-
int i;
337-
338-
if (!s)
339-
return export_unknown;
340-
for (i = 0; export_list[i].export != export_unknown; i++) {
341-
if (strcmp(export_list[i].str, s) == 0)
342-
return export_list[i].export;
343-
}
344-
return export_unknown;
345-
}
346-
347313
static void *sym_get_data_by_offset(const struct elf_info *info,
348314
unsigned int secindex, unsigned long offset)
349315
{
@@ -374,18 +340,6 @@ static const char *sec_name(const struct elf_info *info, int secindex)
374340

375341
#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
376342

377-
static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
378-
{
379-
const char *secname = sec_name(elf, sec);
380-
381-
if (strstarts(secname, "___ksymtab+"))
382-
return export_plain;
383-
else if (strstarts(secname, "___ksymtab_gpl+"))
384-
return export_gpl;
385-
else
386-
return export_unknown;
387-
}
388-
389343
static void sym_update_namespace(const char *symname, const char *namespace)
390344
{
391345
struct symbol *s = find_symbol(symname);
@@ -405,7 +359,7 @@ static void sym_update_namespace(const char *symname, const char *namespace)
405359
}
406360

407361
static struct symbol *sym_add_exported(const char *name, struct module *mod,
408-
enum export export)
362+
bool gpl_only)
409363
{
410364
struct symbol *s = find_symbol(name);
411365

@@ -417,7 +371,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod,
417371

418372
s = alloc_symbol(name);
419373
s->module = mod;
420-
s->export = export;
374+
s->is_gpl_only = gpl_only;
421375
list_add_tail(&s->list, &mod->exported_symbols);
422376
hash_add_symbol(s);
423377

@@ -689,8 +643,6 @@ static void handle_modversion(const struct module *mod,
689643
static void handle_symbol(struct module *mod, struct elf_info *info,
690644
const Elf_Sym *sym, const char *symname)
691645
{
692-
const char *name;
693-
694646
switch (sym->st_shndx) {
695647
case SHN_COMMON:
696648
if (strstarts(symname, "__gnu_lto_")) {
@@ -724,12 +676,15 @@ static void handle_symbol(struct module *mod, struct elf_info *info,
724676
default:
725677
/* All exported symbols */
726678
if (strstarts(symname, "__ksymtab_")) {
727-
enum export export;
679+
const char *name, *secname;
728680

729681
name = symname + strlen("__ksymtab_");
730-
export = export_from_secname(info,
731-
get_secindex(info, sym));
732-
sym_add_exported(name, mod, export);
682+
secname = sec_name(info, get_secindex(info, sym));
683+
684+
if (strstarts(secname, "___ksymtab_gpl+"))
685+
sym_add_exported(name, mod, true);
686+
else if (strstarts(secname, "___ksymtab+"))
687+
sym_add_exported(name, mod, false);
733688
}
734689
if (strcmp(symname, "init_module") == 0)
735690
mod->has_init = true;
@@ -2140,20 +2095,6 @@ void buf_write(struct buffer *buf, const char *s, int len)
21402095
buf->pos += len;
21412096
}
21422097

2143-
static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2144-
{
2145-
switch (exp) {
2146-
case export_gpl:
2147-
error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2148-
m, s);
2149-
break;
2150-
case export_plain:
2151-
case export_unknown:
2152-
/* ignore */
2153-
break;
2154-
}
2155-
}
2156-
21572098
static void check_exports(struct module *mod)
21582099
{
21592100
struct symbol *s, *exp;
@@ -2192,8 +2133,9 @@ static void check_exports(struct module *mod)
21922133
add_namespace(&mod->missing_namespaces, exp->namespace);
21932134
}
21942135

2195-
if (!mod->is_gpl_compatible)
2196-
check_for_gpl_usage(exp->export, basename, exp->name);
2136+
if (!mod->is_gpl_compatible && exp->is_gpl_only)
2137+
error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2138+
basename, exp->name);
21972139
}
21982140
}
21992141

@@ -2437,6 +2379,7 @@ static void read_dump(const char *fname)
24372379
unsigned int crc;
24382380
struct module *mod;
24392381
struct symbol *s;
2382+
bool gpl_only;
24402383

24412384
if (!(symname = strchr(line, '\t')))
24422385
goto fail;
@@ -2454,12 +2397,22 @@ static void read_dump(const char *fname)
24542397
crc = strtoul(line, &d, 16);
24552398
if (*symname == '\0' || *modname == '\0' || *d != '\0')
24562399
goto fail;
2400+
2401+
if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
2402+
gpl_only = true;
2403+
} else if (!strcmp(export, "EXPORT_SYMBOL")) {
2404+
gpl_only = false;
2405+
} else {
2406+
error("%s: unknown license %s. skip", symname, export);
2407+
continue;
2408+
}
2409+
24572410
mod = find_module(modname);
24582411
if (!mod) {
24592412
mod = new_module(modname);
24602413
mod->from_dump = true;
24612414
}
2462-
s = sym_add_exported(symname, mod, export_no(export));
2415+
s = sym_add_exported(symname, mod, gpl_only);
24632416
s->is_static = false;
24642417
sym_set_crc(symname, crc);
24652418
sym_update_namespace(symname, namespace);
@@ -2481,9 +2434,9 @@ static void write_dump(const char *fname)
24812434
if (mod->from_dump)
24822435
continue;
24832436
list_for_each_entry(sym, &mod->exported_symbols, list) {
2484-
buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2437+
buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
24852438
sym->crc, sym->name, mod->name,
2486-
export_str(sym->export),
2439+
sym->is_gpl_only ? "_GPL" : "",
24872440
sym->namespace ?: "");
24882441
}
24892442
}
@@ -2604,9 +2557,8 @@ int main(int argc, char **argv)
26042557

26052558
for (s = symbolhash[n]; s; s = s->next) {
26062559
if (s->is_static)
2607-
error("\"%s\" [%s] is a static %s\n",
2608-
s->name, s->module->name,
2609-
export_str(s->export));
2560+
error("\"%s\" [%s] is a static EXPORT_SYMBOL\n",
2561+
s->name, s->module->name);
26102562
}
26112563
}
26122564

0 commit comments

Comments
 (0)