Sample Header Ad - 728x90

Compiling stand-alone function binaries from the Linux kernel

4 votes
2 answers
698 views
How can I take snippets from the Linux kernel drivers' code, for example this [drm_fb_helper_hotplug_event](https://github.com/torvalds/linux/blob/6832a9317eee280117cd695fa885b2b7a7a38daf/drivers/gpu/drm/drm_fb_helper.c#L1943) function inside drivers/gpu/drm/drm_fb_helper.c, and compile a binary file that I can run to conveniently achieve a particular action in the kernel? In my case, I'd like to call the function arbitrarily to hopefully force a reset of the list of video connectors detected by the kernel, because of monitor output issues. There are probably better ways of achieving this, such as making an out-of-tree module, but I'm not sure how I can do that here. I want to compile an executable that I run from userspace, which will get my task done. So far as a proof of concept, I have tried to get printk/printfs done using only gcc to compile a binary instead of make: HOSTCC=gcc-11 CC=gcc-11 gcc ../drivers/gpu/drm/testing_printout.c -o printAMessage -I. (Compiling an older version that needs GCC 11, after a cd include using -I as the [equivalent to CPATH](https://stackoverflow.com/questions/558803/how-can-i-add-a-default-include-path-for-gcc-in-linux).) But this comes up with problems.
./linux/atomic.h:7:10: fatal error: asm/atomic.h: No such file or directory
So I made a symlink; ln -s asm-generic asm But I still get other header file related errors and warnings:
./linux/thread_info.h:60:10: fatal error: asm/thread_info.h: No such file or directory
I have tried appending ARCH=x86 to the gcc command but it didn't help. Other errors like
./linux/stddef.h:11:9: error: cannot use keyword ‘false’ as enumeration constant
./linux/stddef.h:11:9: note: ‘false’ is a keyword with ‘-std=c23’ onwards
./linux/types.h:30:33: error: ‘bool’ cannot be defined via ‘typedef’
./linux/types.h:30:33: note: ‘bool’ is a keyword with ‘-std=c23’ onwards
...
./linux/atomic/atomic-arch-fallback.h:2245:35: error: unknown type name ‘atomic64_t’; did you mean ‘atomic_t’?
tell me something is wrong with the include files and the CLAGS. What am I missing? Seeing the -std=c23 note made me look at the Makefile at the root of the source directory:
LINUXINCLUDE    := \
		-I$(srctree)/arch/$(SRCARCH)/include \
		-I$(objtree)/arch/$(SRCARCH)/include/generated \
		$(if $(building_out_of_srctree),-I$(srctree)/include) \
		-I$(objtree)/include \
		$(USERINCLUDE)

KBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-PIE
KBUILD_CFLAGS   := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
		   -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \
		   -Werror=implicit-function-declaration -Werror=implicit-int \
		   -Werror=return-type -Wno-format-security \
		   -std=gnu89
I guess using gcc directly instead of the makefiles is really inconvenient. What is the usual way of achieving my goal?
Asked by feearent (61 rep)
Jul 19, 2025, 05:18 AM
Last activity: Jul 19, 2025, 11:48 AM