Commit bf70b050 authored by Masahiro Yamada's avatar Masahiro Yamada Committed by Jessica Yu

module: swap the order of symbol.namespace

Currently, EXPORT_SYMBOL_NS(_GPL) constructs the kernel symbol as
follows:

  __ksymtab_SYMBOL.NAMESPACE

The sym_extract_namespace() in modpost allocates memory for the part
SYMBOL.NAMESPACE when '.' is contained. One problem is that the pointer
returned by strdup() is lost because the symbol name will be copied to
malloc'ed memory by alloc_symbol(). No one will keep track of the
pointer of strdup'ed memory.

sym->namespace still points to the NAMESPACE part. So, you can free it
with complicated code like this:

   free(sym->namespace - strlen(sym->name) - 1);

It complicates memory free.

To fix it elegantly, I swapped the order of the symbol and the
namespace as follows:

  __ksymtab_NAMESPACE.SYMBOL

then, simplified sym_extract_namespace() so that it allocates memory
only for the NAMESPACE part.

I prefer this order because it is intuitive and also matches to major
languages. For example, NAMESPACE::NAME in C++, MODULE.NAME in Python.
Reviewed-by: default avatarMatthias Maennich <maennich@google.com>
Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
Signed-off-by: default avatarJessica Yu <jeyu@kernel.org>
parent c7c4e29f
...@@ -52,7 +52,7 @@ extern struct module __this_module; ...@@ -52,7 +52,7 @@ extern struct module __this_module;
__ADDRESSABLE(sym) \ __ADDRESSABLE(sym) \
asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
" .balign 4 \n" \ " .balign 4 \n" \
"__ksymtab_" #sym NS_SEPARATOR #ns ": \n" \ "__ksymtab_" #ns NS_SEPARATOR #sym ": \n" \
" .long " #sym "- . \n" \ " .long " #sym "- . \n" \
" .long __kstrtab_" #sym "- . \n" \ " .long __kstrtab_" #sym "- . \n" \
" .long __kstrtab_ns_" #sym "- . \n" \ " .long __kstrtab_ns_" #sym "- . \n" \
...@@ -76,7 +76,7 @@ struct kernel_symbol { ...@@ -76,7 +76,7 @@ struct kernel_symbol {
#else #else
#define __KSYMTAB_ENTRY_NS(sym, sec, ns) \ #define __KSYMTAB_ENTRY_NS(sym, sec, ns) \
static const struct kernel_symbol __ksymtab_##sym##__##ns \ static const struct kernel_symbol __ksymtab_##sym##__##ns \
asm("__ksymtab_" #sym NS_SEPARATOR #ns) \ asm("__ksymtab_" #ns NS_SEPARATOR #sym) \
__attribute__((section("___ksymtab" sec "+" #sym), used)) \ __attribute__((section("___ksymtab" sec "+" #sym), used)) \
__aligned(sizeof(void *)) \ __aligned(sizeof(void *)) \
= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym } = { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym }
......
...@@ -350,18 +350,16 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec) ...@@ -350,18 +350,16 @@ static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
static const char *sym_extract_namespace(const char **symname) static const char *sym_extract_namespace(const char **symname)
{ {
size_t n; char *namespace = NULL;
char *dupsymname; char *ns_separator;
n = strcspn(*symname, "."); ns_separator = strchr(*symname, '.');
if (n < strlen(*symname) - 1) { if (ns_separator) {
dupsymname = NOFAIL(strdup(*symname)); namespace = NOFAIL(strndup(*symname, ns_separator - *symname));
dupsymname[n] = '\0'; *symname = ns_separator + 1;
*symname = dupsymname;
return dupsymname + n + 1;
} }
return NULL; return namespace;
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment