Re: [PATCH v3 3/6] modpost: Make mod_device_table aliases more unique

From: Masahiro Yamada
Date: Mon Jun 02 2025 - 03:46:22 EST


On Tue, May 27, 2025 at 6:08 PM Alexey Gladkov <legion@xxxxxxxxxx> wrote:
>
> In order to avoid symbol conflicts if they appear in the same binary, a
> more unique alias identifier can be generated.

Why must this be unique?

What problem would happen if the same symbol names
appear in MODULE_DEVICE_TABLE()?


>
> Signed-off-by: Alexey Gladkov <legion@xxxxxxxxxx>
> Reviewed-by: Petr Pavlu <petr.pavlu@xxxxxxxx>
> ---
> include/linux/module.h | 14 ++++++++++++--
> scripts/mod/file2alias.c | 18 ++++++++++++++----
> 2 files changed, 26 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/module.h b/include/linux/module.h
> index 88048561360f..e7506684069d 100644
> --- a/include/linux/module.h
> +++ b/include/linux/module.h
> @@ -249,10 +249,20 @@ struct module_kobject *lookup_or_create_module_kobject(const char *name);
> /* What your module does. */
> #define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
>
> +/* Format: __mod_device_table__<counter>__kmod_<modname>__<type>__<name> */
> +#define __mod_device_table(type, name) \
> + __PASTE(__mod_device_table__, \
> + __PASTE(__COUNTER__, \
> + __PASTE(__, \
> + __PASTE(__KBUILD_MODNAME, \
> + __PASTE(__, \
> + __PASTE(type, \
> + __PASTE(__, name)))))))
> +
> #ifdef MODULE
> /* Creates an alias so file2alias.c can find device table. */
> -#define MODULE_DEVICE_TABLE(type, name) \
> -extern typeof(name) __mod_device_table__##type##__##name \
> +#define MODULE_DEVICE_TABLE(type, name) \
> +extern typeof(name) __mod_device_table(type, name) \
> __attribute__ ((unused, alias(__stringify(name))))
> #else /* !MODULE */
> #define MODULE_DEVICE_TABLE(type, name)
> diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
> index 00586119a25b..dff1799a4c79 100644
> --- a/scripts/mod/file2alias.c
> +++ b/scripts/mod/file2alias.c
> @@ -1476,8 +1476,8 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
> {
> void *symval;
> char *zeros = NULL;
> - const char *type, *name;
> - size_t typelen;
> + const char *type, *name, *modname;
> + size_t typelen, modnamelen;
> static const char *prefix = "__mod_device_table__";
>
> /* We're looking for a section relative symbol */
> @@ -1488,10 +1488,20 @@ void handle_moddevtable(struct module *mod, struct elf_info *info,
> if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
> return;
>
> - /* All our symbols are of form __mod_device_table__<type>__<name>. */
> + /* All our symbols are of form __mod_device_table__<counter>__kmod_<modname>__<type>__<name>. */
> if (!strstarts(symname, prefix))
> return;
> - type = symname + strlen(prefix);
> +
> + modname = strstr(symname, "__kmod_");
> + if (!modname)
> + return;
> + modname += strlen("__kmod_");
> +
> + type = strstr(modname, "__");
> + if (!type)
> + return;
> + modnamelen = type - modname;
> + type += strlen("__");
>
> name = strstr(type, "__");
> if (!name)
> --
> 2.49.0
>


--
Best Regards
Masahiro Yamada