[PATCH 4/7] module: Parse and stash build ID on insertion

From: Stephen Boyd
Date: Mon Mar 01 2021 - 18:05:13 EST


Parse the build ID of a module when it is inserted and stash it away in
'struct module' in case we print module symbols. This will be used in a
future patch to print the module build ID when module functions are in a
stack trace.

Note: we only do this if CONFIG_KALLSYMS is enabled, because if it isn't
enabled then module names aren't printed for symbolic pointers and thus
build IDs aren't printed either.

Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
Cc: Jessica Yu <jeyu@xxxxxxxxxx>
Cc: Evan Green <evgreen@xxxxxxxxxxxx>
Cc: Hsin-Yi Wang <hsinyi@xxxxxxxxxxxx>
Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
---
include/linux/module.h | 4 ++++
kernel/module.c | 20 ++++++++++++++++++++
2 files changed, 24 insertions(+)

diff --git a/include/linux/module.h b/include/linux/module.h
index 59f094fa6f74..9d1f6a5240c1 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -11,6 +11,7 @@

#include <linux/list.h>
#include <linux/stat.h>
+#include <linux/buildid.h>
#include <linux/compiler.h>
#include <linux/cache.h>
#include <linux/kmod.h>
@@ -432,6 +433,9 @@ struct module {

/* Notes attributes */
struct module_notes_attrs *notes_attrs;
+
+ /* Module build-id */
+ char build_id[BUILD_ID_STR_SIZE_MAX];
#endif

/* The command line arguments (may be mangled). People like
diff --git a/kernel/module.c b/kernel/module.c
index 30479355ab85..a7559a0de9d8 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -13,6 +13,7 @@
#include <linux/trace_events.h>
#include <linux/init.h>
#include <linux/kallsyms.h>
+#include <linux/buildid.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/sysfs.h>
@@ -2770,6 +2771,20 @@ static void add_kallsyms(struct module *mod, const struct load_info *info)
}
mod->core_kallsyms.num_symtab = ndst;
}
+
+static void module_init_build_id(struct module *mod, const struct load_info *info)
+{
+ const Elf_Shdr *sechdr;
+ unsigned int i;
+
+ for (i = 0; i < info->hdr->e_shnum; i++) {
+ sechdr = &info->sechdrs[i];
+ if (!sect_empty(sechdr) && sechdr->sh_type == SHT_NOTE &&
+ !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id,
+ sechdr->sh_size))
+ break;
+ }
+}
#else
static inline void layout_symtab(struct module *mod, struct load_info *info)
{
@@ -2778,6 +2793,10 @@ static inline void layout_symtab(struct module *mod, struct load_info *info)
static void add_kallsyms(struct module *mod, const struct load_info *info)
{
}
+
+static void module_init_build_id(struct module *mod, const struct load_info *info)
+{
+}
#endif /* CONFIG_KALLSYMS */

static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug, unsigned int num)
@@ -4004,6 +4023,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
goto free_arch_cleanup;
}

+ module_init_build_id(mod, info);
dynamic_debug_setup(mod, info->debug, info->num_debug);

/* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
--
https://chromeos.dev