Skip to content

Add build-id to modinfo#432

Open
lucasdemarchi wants to merge 4 commits into
masterfrom
tip-build-id
Open

Add build-id to modinfo#432
lucasdemarchi wants to merge 4 commits into
masterfrom
tip-build-id

Conversation

@lucasdemarchi
Copy link
Copy Markdown
Contributor

@lucasdemarchi lucasdemarchi commented Mar 25, 2026

Kernel uses -Wl,--build-id in the build system, which causes a .notes.gnu.build-id section to be created containing. This can then be used by people developing modules to make sure the running module is the one that was last built.

I used to rely on srcversion for that purpose, but it has 2 issues:

  1. It doesn't change if the compilation options change
  2. It may be going away in a future kernel version

As per last commit message, this can already be used as is, but is rather cumbersome. I will add more commits later to probably have a modinfo -F raw-build-id that simply dumps the section, which would allow to compare with the sysfs with something like:

cmp <(cat /sys/module/foobar/notes/.note.gnu.build-id) <(modinfo -F raw-build-id foobar)

I'm on the fence about that because we could simply leave modinfo alone for that and add built-in tool:

$ kmod build-id --check --verbose foobar
live: a:b:c:...
file: a:b:c:...
match: ok
$ echo $?
0

@lucasdemarchi lucasdemarchi requested review from a team and evelikov March 25, 2026 06:32
@lucasdemarchi lucasdemarchi force-pushed the tip-build-id branch 2 times, most recently from 4136683 to 62c0325 Compare March 25, 2026 06:51
Copy link
Copy Markdown
Collaborator

@evelikov evelikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code-wise this looks good, left some stylistic suggestions - nothing too crazy I hope 😅

Comment thread libkmod/libkmod-elf.c Outdated
Comment thread libkmod/libkmod-elf.c Outdated
Comment thread libkmod/libkmod-elf.c Outdated
Comment thread libkmod/libkmod-elf.c Outdated
Comment thread libkmod/libkmod-elf.c Outdated
Comment thread libkmod/libkmod-elf.c Outdated
Comment thread libkmod/libkmod-elf.c Outdated
@evelikov evelikov added the status:reviewed The proposed change is in good condition and ready for merging. label Apr 20, 2026
@lucasdemarchi
Copy link
Copy Markdown
Contributor Author

@evelikov any thoughts about a new build-id subcommand?

@evelikov
Copy link
Copy Markdown
Collaborator

evelikov commented May 1, 2026

@evelikov any thoughts about a new build-id subcommand?

Without having many other sub-command to compare against - the proposed syntax seems a bit odd IMHO.

For better or worse I appreciate the style and consistency of the systemd "ctl" tools. Would be in favour if we have a proposal of two how we can structure the existing options (modprobe et al) and see how those will align with the build-id bits.

Aside: please add a note - here in PR, in a commit message, or in-code... more or less anywhere - which kernel version introduced support for -Wl,--build-id. Eg. circa 4.10

Let the compiler decide depending on the optimization level.

Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
Shortcut to the size.

Keep this wrapper inline even after the previous de-inlining change:
it is a tiny typed helper around elf_get_uint() that avoids repeating
the uint32_t size at call sites, while the larger helpers can still be
left to the compiler.

Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
Read the .note.gnu.build-id section so it can be displayed by modinfo in
a future change. For now this is only added to pretty-print the id, but
later can be refactored to easily compare with the section dump from
/sys/module/<module-name>/.note.gnu.build-id

The section in the elf is potentially available since kernel 4.10 (if
toolchain supports it) and became unconditional since commit
89ff7131f78a ("kbuild: add --hash-style= and --build-id unconditionally")
since minimum toolchain versions got raised.

Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
If available, add the build-id to the info list returned by
kmod_module_get_info(). This allows us to compare the build-id of a
loaded module versus the build-id of the file. It's rather cumbersome
command, but it works:

	$ ./build/modinfo -F build-id hid
	AE:7A:2E:37:E0:2B:31:D5:72:7C:87:87:15:8C:05:FA:77:7A:4F:57
	$ xxd -p -u -s16  /sys/module/hid/notes/.note.gnu.build-id  | sed 's/../&:/g;s/:$//'
	AE:7A:2E:37:E0:2B:31:D5:72:7C:87:87:15:8C:05:FA:77:7A:4F:57

An option to modinfo to really to just dump the raw value can be added
in future.

Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
@lucasdemarchi
Copy link
Copy Markdown
Contributor Author

Without having many other sub-command to compare against - the proposed syntax seems a bit odd IMHO.

For better or worse I appreciate the style and consistency of the systemd "ctl" tools. Would be in favour if we have a proposal of two how we can structure the existing options (modprobe et al) and see how those will align with the build-id bits.

Note that the ctl tools in systemd are targeted at talking to a daemon. Once upon a time I wanted to deprecate the standalone modprobe/insmod/... etc and have a unified "kmod ". I'm not pursuing that anymore as I don't really see pressuring need to break people's workflow/scripts. However for new things that don't fit the interface of these old tools, I'd rather use a subcommand for kmod.

Here what I want is to give a quick command for kernel module developers to a) check the build-id of a filename/module and b) check if it matches what is loaded in the kernel. I don't think this fits any of modinfo/modprobe/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:reviewed The proposed change is in good condition and ready for merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants