Skip to content

Commit cb9b55d

Browse files
mettiJessica Yu
authored andcommitted
modpost: add support for symbol namespaces
Add support for symbols that are exported into namespaces. For that, extract any namespace suffix from the symbol name. In addition, emit a warning whenever a module refers to an exported symbol without explicitly importing the namespace that it is defined in. This patch consistently adds the namespace suffix to symbol names exported into Module.symvers. Example warning emitted by modpost in case of the above violation: WARNING: module ums-usbat uses symbol usb_stor_resume from namespace USB_STORAGE, but does not import it. Co-developed-by: Martijn Coenen <maco@android.com> Signed-off-by: Martijn Coenen <maco@android.com> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Matthias Maennich <maennich@google.com> Signed-off-by: Jessica Yu <jeyu@kernel.org>
1 parent 8651ec0 commit cb9b55d

4 files changed

Lines changed: 101 additions & 19 deletions

File tree

‎Documentation/kbuild/modules.rst‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,12 @@ build.
470470

471471
The syntax of the Module.symvers file is::
472472

473-
<CRC> <Symbol> <module>
473+
<CRC> <Symbol> <Namespace> <Module> <Export Type>
474474

475-
0x2d036834 scsi_remove_host drivers/scsi/scsi_mod
475+
0xe1cc2a05 usb_stor_suspend USB_STORAGE drivers/usb/storage/usb-storage EXPORT_SYMBOL_GPL
476+
477+
The fields are separated by tabs and values may be empty (e.g.
478+
if no namespace is defined for an exported symbol).
476479

477480
For a kernel build without CONFIG_MODVERSIONS enabled, the CRC
478481
would read 0x00000000.

‎scripts/export_report.pl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ sub collectcfiles {
9494
#
9595
while ( <$module_symvers> ) {
9696
chomp;
97-
my (undef, $symbol, $module, $gpl) = split;
97+
my (undef, $symbol, $namespace, $module, $gpl) = split('\t');
9898
$SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl];
9999
}
100100
close($module_symvers);

‎scripts/mod/modpost.c‎

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ struct symbol {
164164
struct module *module;
165165
unsigned int crc;
166166
int crc_valid;
167+
const char *namespace;
167168
unsigned int weak:1;
168169
unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
169170
unsigned int kernel:1; /* 1 if symbol is from kernel
@@ -233,6 +234,37 @@ static struct symbol *find_symbol(const char *name)
233234
return NULL;
234235
}
235236

237+
static bool contains_namespace(struct namespace_list *list,
238+
const char *namespace)
239+
{
240+
struct namespace_list *ns_entry;
241+
242+
for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
243+
if (strcmp(ns_entry->namespace, namespace) == 0)
244+
return true;
245+
246+
return false;
247+
}
248+
249+
static void add_namespace(struct namespace_list **list, const char *namespace)
250+
{
251+
struct namespace_list *ns_entry;
252+
253+
if (!contains_namespace(*list, namespace)) {
254+
ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
255+
strlen(namespace) + 1));
256+
strcpy(ns_entry->namespace, namespace);
257+
ns_entry->next = *list;
258+
*list = ns_entry;
259+
}
260+
}
261+
262+
static bool module_imports_namespace(struct module *module,
263+
const char *namespace)
264+
{
265+
return contains_namespace(module->imported_namespaces, namespace);
266+
}
267+
236268
static const struct {
237269
const char *str;
238270
enum export export;
@@ -312,23 +344,39 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
312344
return export_unknown;
313345
}
314346

347+
static const char *sym_extract_namespace(const char **symname)
348+
{
349+
size_t n;
350+
char *dupsymname;
351+
352+
n = strcspn(*symname, ".");
353+
if (n < strlen(*symname) - 1) {
354+
dupsymname = NOFAIL(strdup(*symname));
355+
dupsymname[n] = '\0';
356+
*symname = dupsymname;
357+
return dupsymname + n + 1;
358+
}
359+
360+
return NULL;
361+
}
362+
315363
/**
316364
* Add an exported symbol - it may have already been added without a
317365
* CRC, in this case just update the CRC
318366
**/
319-
static struct symbol *sym_add_exported(const char *name, struct module *mod,
320-
enum export export)
367+
static struct symbol *sym_add_exported(const char *name, const char *namespace,
368+
struct module *mod, enum export export)
321369
{
322370
struct symbol *s = find_symbol(name);
323371

324372
if (!s) {
325373
s = new_symbol(name, mod, export);
374+
s->namespace = namespace;
326375
} else {
327376
if (!s->preloaded) {
328-
warn("%s: '%s' exported twice. Previous export "
329-
"was in %s%s\n", mod->name, name,
330-
s->module->name,
331-
is_vmlinux(s->module->name) ?"":".ko");
377+
warn("%s: '%s' exported twice. Previous export was in %s%s\n",
378+
mod->name, name, s->module->name,
379+
is_vmlinux(s->module->name) ? "" : ".ko");
332380
} else {
333381
/* In case Module.symvers was out of date */
334382
s->module = mod;
@@ -620,6 +668,7 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
620668
unsigned int crc;
621669
enum export export;
622670
bool is_crc = false;
671+
const char *name, *namespace;
623672

624673
if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
625674
strstarts(symname, "__ksymtab"))
@@ -691,8 +740,9 @@ static void handle_modversions(struct module *mod, struct elf_info *info,
691740
default:
692741
/* All exported symbols */
693742
if (strstarts(symname, "__ksymtab_")) {
694-
sym_add_exported(symname + strlen("__ksymtab_"), mod,
695-
export);
743+
name = symname + strlen("__ksymtab_");
744+
namespace = sym_extract_namespace(&name);
745+
sym_add_exported(name, namespace, mod, export);
696746
}
697747
if (strcmp(symname, "init_module") == 0)
698748
mod->has_init = 1;
@@ -1943,6 +1993,7 @@ static void read_symbols(const char *modname)
19431993
const char *symname;
19441994
char *version;
19451995
char *license;
1996+
char *namespace;
19461997
struct module *mod;
19471998
struct elf_info info = { };
19481999
Elf_Sym *sym;
@@ -1974,6 +2025,12 @@ static void read_symbols(const char *modname)
19742025
license = get_next_modinfo(&info, "license", license);
19752026
}
19762027

2028+
namespace = get_modinfo(&info, "import_ns");
2029+
while (namespace) {
2030+
add_namespace(&mod->imported_namespaces, namespace);
2031+
namespace = get_next_modinfo(&info, "import_ns", namespace);
2032+
}
2033+
19772034
for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
19782035
symname = remove_dot(info.strtab + sym->st_name);
19792036

@@ -2118,6 +2175,13 @@ static int check_exports(struct module *mod)
21182175
basename++;
21192176
else
21202177
basename = mod->name;
2178+
2179+
if (exp->namespace &&
2180+
!module_imports_namespace(mod, exp->namespace)) {
2181+
warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2182+
basename, exp->name, exp->namespace);
2183+
}
2184+
21212185
if (!mod->gpl_compatible)
21222186
check_for_gpl_usage(exp->export, basename, exp->name);
21232187
check_for_unused(exp->export, basename, exp->name);
@@ -2341,15 +2405,18 @@ static void read_dump(const char *fname, unsigned int kernel)
23412405
return;
23422406

23432407
while ((line = get_next_line(&pos, file, size))) {
2344-
char *symname, *modname, *d, *export, *end;
2408+
char *symname, *namespace, *modname, *d, *export, *end;
23452409
unsigned int crc;
23462410
struct module *mod;
23472411
struct symbol *s;
23482412

23492413
if (!(symname = strchr(line, '\t')))
23502414
goto fail;
23512415
*symname++ = '\0';
2352-
if (!(modname = strchr(symname, '\t')))
2416+
if (!(namespace = strchr(symname, '\t')))
2417+
goto fail;
2418+
*namespace++ = '\0';
2419+
if (!(modname = strchr(namespace, '\t')))
23532420
goto fail;
23542421
*modname++ = '\0';
23552422
if ((export = strchr(modname, '\t')) != NULL)
@@ -2366,7 +2433,8 @@ static void read_dump(const char *fname, unsigned int kernel)
23662433
mod = new_module(modname);
23672434
mod->skip = 1;
23682435
}
2369-
s = sym_add_exported(symname, mod, export_no(export));
2436+
s = sym_add_exported(symname, namespace, mod,
2437+
export_no(export));
23702438
s->kernel = kernel;
23712439
s->preloaded = 1;
23722440
sym_update_crc(symname, mod, crc, export_no(export));
@@ -2395,16 +2463,20 @@ static void write_dump(const char *fname)
23952463
{
23962464
struct buffer buf = { };
23972465
struct symbol *symbol;
2466+
const char *namespace;
23982467
int n;
23992468

24002469
for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
24012470
symbol = symbolhash[n];
24022471
while (symbol) {
2403-
if (dump_sym(symbol))
2404-
buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
2405-
symbol->crc, symbol->name,
2406-
symbol->module->name,
2407-
export_str(symbol->export));
2472+
if (dump_sym(symbol)) {
2473+
namespace = symbol->namespace;
2474+
buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2475+
symbol->crc, symbol->name,
2476+
namespace ? namespace : "",
2477+
symbol->module->name,
2478+
export_str(symbol->export));
2479+
}
24082480
symbol = symbol->next;
24092481
}
24102482
}

‎scripts/mod/modpost.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ buf_printf(struct buffer *buf, const char *fmt, ...);
109109
void
110110
buf_write(struct buffer *buf, const char *s, int len);
111111

112+
struct namespace_list {
113+
struct namespace_list *next;
114+
char namespace[0];
115+
};
116+
112117
struct module {
113118
struct module *next;
114119
const char *name;
@@ -121,6 +126,8 @@ struct module {
121126
struct buffer dev_table_buf;
122127
char srcversion[25];
123128
int is_dot_o;
129+
// Actual imported namespaces
130+
struct namespace_list *imported_namespaces;
124131
};
125132

126133
struct elf_info {

0 commit comments

Comments
 (0)