-3

I have a C program that is tied to a specific version of Linux kernel source.
I only want to compile the application against the kernel source, and then run the binary in a shell.
I will run the code as a user-space application, and not in the kernel space, after building the Linux kernel.

How and where can I include this program in the Linux source so that it can be compiled with the Linux kernel?

sawdust
  • 16,103
  • 3
  • 40
  • 50
sal_guy
  • 199
  • 2
  • 14
  • 3
    There is no such thing like **executable** in the kernel space. However, it is possible to create a Linux **kernel module**, which can execute some code in the kernel space when inserted (`insmod`) into the kernel. – Tsyvarev Sep 27 '19 at 23:35
  • I wanted to mean how I can include a utility binary file like the binutils binaries. – sal_guy Sep 27 '19 at 23:40
  • Sorry, don't understand. Do you want to run a code, which is compiled as an application (executable), in the kernel space? If so, you cannot, You need to compile that code specifically for the kernel, with its own standard library. – Tsyvarev Sep 27 '19 at 23:43
  • Yes, I want to run a code as an application but not in the kernel space. I only want to compile the application binary against the kernel source, then finally run the code in a shell. I am not able to build the binary in the compiled Linux because I do not have gcc in the compiled Linux. – sal_guy Sep 27 '19 at 23:49
  • So you want to take some source files from the Linux kernel tree, and compile these files (and some your ones) as an usual application? The I see no problem, just copy those Linux kernel files into your directory, and compile and link them as usual. – Tsyvarev Sep 28 '19 at 00:06
  • @sawdust: I understand neither the original **question** nor the one after your edit. However, in your form the question *resembles* [this one](https://stackoverflow.com/questions/56693772/how-do-i-compile-a-userspace-application-for-a-particular-linux-kernel) from the same author.. Or [that one](https://stackoverflow.com/questions/57817364/how-to-compile-a-kernel-with-other-application), which has different author, but is better worded. – Tsyvarev Sep 28 '19 at 14:08
  • @SohamSinha: Could you be more specific about what kind of application you have and how **exactly** it is "tied" to the specific version of the Linux kernel. Does you application uses *syscall*, introduced only in the given version? – Tsyvarev Sep 28 '19 at 14:24

1 Answers1

1

Userspace programs do exist in the Linux kernel source tree in the tools/ subdirectory.
There does not seem to be a clear-cut (or any) definition of what kind of program constitutes a "tool" that requires/deserves inclusion/distribution with the kernel source.

The types of utilities that do (currently) exist in the kernel source tree include an admin program for examining status bits of memory pages (tools/vm/page-types.c) to three simple programs that utilize/demonstrate the ("new") chardev GPIO interface (tools/gpio/gpio-event-mon.c and others).

The largest category of userspace programs or tools in the kernel source is in the tools/testing/selftests/ kernel subdirectory.
The documentation is in Documentation/dev-tools/kselftest.rst

======================
Linux Kernel Selftests
======================

The kernel contains a set of "self tests" under the tools/testing/selftests/
directory. These are intended to be small tests to exercise individual code
paths in the kernel. Tests are intended to be run after building, installing
and booting a kernel.  
...  
kselftest runs as a userspace process.  Tests that can be written/run in
userspace may wish to use the `Test Harness`_.  Tests that need to be
run in kernel space may wish to use a `Test Module`_.

Alternatively there are many kernel subsystems and hardware components that do not have their tools in the kernel source, but rather have that code available as separate source/project packages.

Given the stability of the binary API that the Linux kernel provides to userspace, a program is rarely tied to a specific kernel version. A regression (i.e. a change which causes something to break for existing users) is avoided whenever possible by the kernel maintainers.

One reason for inclusion of programs with the kernel source seems to be convenience for the kernel maintainers.
Kernel builders are encouraged to also build and run the selftest programs.

sawdust
  • 16,103
  • 3
  • 40
  • 50