Skip to content

Commit c96fca2

Browse files
author
Sam Ravnborg
committed
kbuild: warn when a moduled uses a symbol marked UNUSED
We now have infrastructure in place to mark an EXPORTed symbol as unused. So the natural next step is to warn during buildtime when a module uses a symbol marked UNUSED. Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
1 parent 534b89a commit c96fca2

2 files changed

Lines changed: 64 additions & 23 deletions

File tree

‎scripts/mod/modpost.c‎

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ static int all_versions = 0;
2424
/* If we are modposting external module set to 1 */
2525
static int external_module = 0;
2626
/* How a symbol is exported */
27-
enum export {export_plain, export_gpl, export_gpl_future, export_unknown};
27+
enum export {
28+
export_plain, export_unused, export_gpl,
29+
export_unused_gpl, export_gpl_future, export_unknown
30+
};
2831

2932
void fatal(const char *fmt, ...)
3033
{
@@ -191,7 +194,9 @@ static struct {
191194
enum export export;
192195
} export_list[] = {
193196
{ .str = "EXPORT_SYMBOL", .export = export_plain },
197+
{ .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
194198
{ .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
199+
{ .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
195200
{ .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
196201
{ .str = "(unknown)", .export = export_unknown },
197202
};
@@ -218,8 +223,12 @@ static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
218223
{
219224
if (sec == elf->export_sec)
220225
return export_plain;
226+
else if (sec == elf->export_unused_sec)
227+
return export_unused;
221228
else if (sec == elf->export_gpl_sec)
222229
return export_gpl;
230+
else if (sec == elf->export_unused_gpl_sec)
231+
return export_unused_gpl;
223232
else if (sec == elf->export_gpl_future_sec)
224233
return export_gpl_future;
225234
else
@@ -368,8 +377,12 @@ static void parse_elf(struct elf_info *info, const char *filename)
368377
info->modinfo_len = sechdrs[i].sh_size;
369378
} else if (strcmp(secname, "__ksymtab") == 0)
370379
info->export_sec = i;
380+
else if (strcmp(secname, "__ksymtab_unused") == 0)
381+
info->export_unused_sec = i;
371382
else if (strcmp(secname, "__ksymtab_gpl") == 0)
372383
info->export_gpl_sec = i;
384+
else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
385+
info->export_unused_gpl_sec = i;
373386
else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
374387
info->export_gpl_future_sec = i;
375388

@@ -1087,38 +1100,64 @@ void buf_write(struct buffer *buf, const char *s, int len)
10871100
buf->pos += len;
10881101
}
10891102

1090-
void check_license(struct module *mod)
1103+
static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1104+
{
1105+
const char *e = is_vmlinux(m) ?"":".ko";
1106+
1107+
switch (exp) {
1108+
case export_gpl:
1109+
fatal("modpost: GPL-incompatible module %s%s "
1110+
"uses GPL-only symbol '%s'\n", m, e, s);
1111+
break;
1112+
case export_unused_gpl:
1113+
fatal("modpost: GPL-incompatible module %s%s "
1114+
"uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1115+
break;
1116+
case export_gpl_future:
1117+
warn("modpost: GPL-incompatible module %s%s "
1118+
"uses future GPL-only symbol '%s'\n", m, e, s);
1119+
break;
1120+
case export_plain:
1121+
case export_unused:
1122+
case export_unknown:
1123+
/* ignore */
1124+
break;
1125+
}
1126+
}
1127+
1128+
static void check_for_unused(enum export exp, const char* m, const char* s)
1129+
{
1130+
const char *e = is_vmlinux(m) ?"":".ko";
1131+
1132+
switch (exp) {
1133+
case export_unused:
1134+
case export_unused_gpl:
1135+
warn("modpost: module %s%s "
1136+
"uses symbol '%s' marked UNUSED\n", m, e, s);
1137+
break;
1138+
default:
1139+
/* ignore */
1140+
break;
1141+
}
1142+
}
1143+
1144+
static void check_exports(struct module *mod)
10911145
{
10921146
struct symbol *s, *exp;
10931147

10941148
for (s = mod->unres; s; s = s->next) {
10951149
const char *basename;
1096-
if (mod->gpl_compatible == 1) {
1097-
/* GPL-compatible modules may use all symbols */
1098-
continue;
1099-
}
11001150
exp = find_symbol(s->name);
11011151
if (!exp || exp->module == mod)
11021152
continue;
11031153
basename = strrchr(mod->name, '/');
11041154
if (basename)
11051155
basename++;
1106-
switch (exp->export) {
1107-
case export_gpl:
1108-
fatal("modpost: GPL-incompatible module %s "
1109-
"uses GPL-only symbol '%s'\n",
1110-
basename ? basename : mod->name,
1111-
exp->name);
1112-
break;
1113-
case export_gpl_future:
1114-
warn("modpost: GPL-incompatible module %s "
1115-
"uses future GPL-only symbol '%s'\n",
1116-
basename ? basename : mod->name,
1117-
exp->name);
1118-
break;
1119-
case export_plain: /* ignore */ break;
1120-
case export_unknown: /* ignore */ break;
1121-
}
1156+
else
1157+
basename = mod->name;
1158+
if (!mod->gpl_compatible)
1159+
check_for_gpl_usage(exp->export, basename, exp->name);
1160+
check_for_unused(exp->export, basename, exp->name);
11221161
}
11231162
}
11241163

@@ -1399,7 +1438,7 @@ int main(int argc, char **argv)
13991438
for (mod = modules; mod; mod = mod->next) {
14001439
if (mod->skip)
14011440
continue;
1402-
check_license(mod);
1441+
check_exports(mod);
14031442
}
14041443

14051444
for (mod = modules; mod; mod = mod->next) {

‎scripts/mod/modpost.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ struct elf_info {
117117
Elf_Sym *symtab_start;
118118
Elf_Sym *symtab_stop;
119119
Elf_Section export_sec;
120+
Elf_Section export_unused_sec;
120121
Elf_Section export_gpl_sec;
122+
Elf_Section export_unused_gpl_sec;
121123
Elf_Section export_gpl_future_sec;
122124
const char *strtab;
123125
char *modinfo;

0 commit comments

Comments
 (0)