How can the Linux kernel compile itself?

I do not quite understand the process of compiling the Linux kernel when installing Linux on my machine.

Here are some things that confused me:

  • The kernel is written in C, however, how was the kernel compiled without a compiler?
  • If the C compiler is installed on my computer before the kernel is compiled, how can the compiler itself be compiled without a compiler installed?

I was so confused for several days, thanks for the reply.

+84
c compiler-construction linux
Jan 30 '09 at 3:22
source share
7 answers

The first round of binaries for your Linux box was built on another Linux box (maybe).

The binaries for the first Linux system were built on a different platform.

Binaries for this computer can track their root to the source system, which was built on another platform.

...

Push it far enough and you will find compilers created using simpler tools, which, in turn, were created on machines other than their host.

...

Keep pressing and you will find that computers are built so that their commands can be entered using the switches on the front of the device .

Very cool stuff.

Rule: "Create tools to create tools to create tools ...". Very similar to the tools that control our physical environment. Also known as "pulling yourself by bootstraps."

+203
Jan 30 '09 at 3:34
source share

I think you should distinguish between:

compile , v: use the compiler to process the source code and create executable code [1] .

and

install , v: connect, configure, or prepare something for use [2] .

Compilation creates binary executables from source code. The installation simply puts these binary executables in the right place to run later. Thus, installation and use do not require compilation if binary files are available. Think of “compiling” and “installing,” for example, “cook” and “serve,” respectively.

Now your questions:

  • The kernel is written in C, however, how was the kernel compiled without a compiler?

A kernel cannot be compiled without a compiler, but it can be installed from a compiled binary.

Usually, when you install the operating system, you install the pre-compiled kernel (binary executable). It was composed by someone else. And only if you want to compile the kernel yourself, you will need a source and a compiler, as well as all other tools.

Even in the “source” distributions, such as gentoo, you run the compiled binary.

So, you can live your whole life without compiling the kernels, because you compiled them by someone else.

  1. If the C compiler is installed on my computer before the kernel is compiled, how can the compiler itself be compiled without a compiler installed?

The compiler cannot be started if there is no kernel (OS). Thus, you need to install the compiled kernel to run the compiler, but it does not need to compile the kernel itself.

Again, the most common practice is to install compiled compiler binaries and use them to compile anything else (including the compiler and the kernel).

Now the problem is with the chicken and the egg. The first binary is compiled by someone else ... See dmckee's excellent answer.

+32
Jan 30 '09 at 10:56
source share

The term describing this phenomenon, bootstrapping , is an interesting concept for reading. If you think about embedded development, it becomes clear that many devices, such as alarms, microwave ovens, remote controls that require software, are not strong enough to compile their own software. In fact, these devices usually do not have sufficient resources to run anything remotely complex, like a compiler.

Their software is developed on a desktop machine and then copied after compilation.

If this interests you, the article that comes to mind from my head looks like this: Reflections on Trusting Trust ( pdf ), this is a classic and fun reading.

+14
Jan 30 '09 at 10:56
source share

The kernel does not compile - it is compiled by the C compiler in user space. In most processor architectures, the CPU has several bits in special registers that represent what privileges the current code has. On x86, these are the current privilege level bits (CPL) in the code segment register (CS). If the CPL bit is 00, this code is said to work in guard ring 0, also known as kernel mode. If the CPL bits are 11, this code is said to work in guard ring 3, also known as user mode. Two other combinations, 01 and 10 (guard rings 1 and 2, respectively) are rarely used.

The rules about what code can and cannot do in user mode depending on the kernel mode are quite complicated, but suffice it to say that user mode has greatly reduced privileges.

Now, when people talk about the kernel of the operating system, they refer to parts of the OS code that run in kernel mode with elevated privileges. As a rule, kernel authors try to keep the kernel as small as possible for security reasons, so code that does not need additional privileges does not have them.

The C compiler is one example of such a program - it does not need the additional privileges offered by kernel mode, so it runs in user mode, like most other programs.

In the case of Linux, the kernel consists of two parts: the kernel source code and the compiled kernel executable. Any machine with a C compiler can compile a kernel from source into a binary image. The question then becomes what to do with this binary image.

When you install Linux on a new system, you install a precompiled binary image, usually from any physical medium (such as a CD DVD) or from a network. The BIOS will download the kernel loader (binary image) from the media or network, and then the loader will install the (binary image) kernel on your hard drive. Then, when you reboot, the BIOS loads the kernel loader from your hard drive, and the loader loads the kernel into memory, and you are turned off and running.

If you want to recompile your own kernel, this is a little more complicated, but you can do it.

+10
Jan 30 '09 at 3:56
source share

Which one was the first? chicken or egg?

There have been eggs since the time of the dinosaurs.

.. some confuse everyone by saying that chickens are actually descendants of great animals. In short: technology (egg) existed before the current product (chicken)

You need a kernel to build the kernel, i.e. you will create it with another.

The first core can be whatever you want (preferably something sensible that can create the desired end product ^ __ ^)

This tutorial from Bran Kernel Development teaches you to design and build a small kernel that you can test with the virtual machine of your choice.

Meaning: you write and compile the kernel somewhere and read it on an empty (without OS) virtual machine.

What happens with these Linux installations follows the same idea with additional complexity.

+5
Jan 30 '09 at 5:45
source share

These are not turtles all the way down. As you say, you cannot compile an operating system that has never been compiled before on a system running this operating system. Similarly, at least the very first compiler build should be done on another compiler (and, as a rule, on some subsequent builds too, if this first build is unable to compile its own source code yet).

I think the first Linux kernels were compiled in a Minix window, although I'm not sure about that. GCC was available at the time. One of the earliest goals of many operating systems is to run the compiler fast enough to compile your own source code. Go ahead, the first compiler is almost certainly written in assembler. The first assemblers were written by those poor people who had to write raw machine code.

You can check out the Linux From Scratch project. In fact, you create two systems in the book: the “temporary system”, which is built on a system that you did not create yourself, and then the “LFS system”, which is built on your temporary system. The way the book is currently written, you actually create a temporary system on another Linux box, but theoretically you can adapt it to create a temporary system on a completely different OS.

+5
Sep 17 '13 at 13:51 on
source share

If I understand your question correctly. Nowadays, the kernel does not "compile". Currently, most Linux distributions provide system installation via live cd. The kernel boots from the CD into the memory and works as if it were installed on the disk. Using the linux environment running on your system, it is easy to simply transfer the necessary files to your disk.

If you talked about the boot problem; dmckee summarized it quite nicely.

Just offering another opportunity ...

0
Sep 17 '13 at 15:21
source share



All Articles