How can the Linux kernel compile itself?

CLinuxCompiler Construction

C Problem Overview


I don't quite understand the compiling process of the Linux kernel when I install a Linux system on my machine.

Here are some things that confused me:

  1. The kernel is written in C, however how did the kernel get compiled without a compiler installed?
  2. If the C compiler is installed on my machine before the kernel is compiled, how can the compiler itself get compiled without a compiler installed?

I was so confused for a couple of days, thanks for the response.

C Solutions


Solution 1 - C

The first round of binaries for your Linux box were built on some other Linux box (probably).

The binaries for the first Linux system were built on some other platform.

The binaries for that computer can trace their root back to an original system that was built on yet another platform.

...

Push this far enough, and you find compilers built with more primitive tools, which were in turn built on machines other than their host.

...

Keep pushing and you find computers built so that their instructions could be entered by setting switches on the front panel of the machine.

Very cool stuff.

The rule is "build the tools to build the tools to build the tools...". Very much like the tools which run our physical environment. Also known as "pulling yourself up by the bootstraps".

Solution 2 - C

I think you should distinguish between:

compile, v: To use a compiler to process source code and produce executable code [1].

and

install, v: To connect, set up or prepare something for use [2].

Compilation produces binary executables from source code. Installation merely puts those binary executables in the right place to run them later. So, installation and use do not require compilation if the binaries are available. Think about ”compile” and “install” like about “cook” and “serve”, correspondingly.

Now, your questions:

> 1. The kernel is written in C, however how did the kernel get compiled without a compiler installed?

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

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

Even in ”source-based” distributions like gentoo you start from running a compiled binary.

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

> 2. If the C compiler is installed on my machine before the kernel is compiled, how can the compiler itself get compiled without a compiler installed?

The compiler cannot be run if there is no kernel (OS). So one has to install a compiled kernel to run the compiler, but does not need to compile the kernel himself.

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

Now, chicken and egg problem. The first binary is compiled by someone else... See an excellent answer by dmckee.

Solution 3 - C

The term describing this phenomenon is bootstrapping, it's an interesting concept to read up on. If you think about embedded development, it becomes clear that a lot of devices, say alarm clocks, microwaves, remote controls, that require software aren't powerful enough to compile their own software. In fact, these sorts of devices typically don't have enough resources to run anything remotely as complicated as a compiler.

Their software is developed on a desktop machine and then copied once it's been compiled.

If this sort of thing interests you, an article that comes to mind off the top of my head is: Reflections on Trusting Trust (pdf), it's a classic and a fun read.

Solution 4 - C

The kernel doesn't compile itself -- it's compiled by a C compiler in userspace. In most CPU architectures, the CPU has a number of bits in special registers that represent what privileges the code currently running has. In x86, these are the current privilege level bits (CPL) in the code segment (CS) register. If the CPL bits are 00, the code is said to be running in security ring 0, also known as kernel mode. If the CPL bits are 11, the code is said to be running in security ring 3, also known as user mode. The other two combinations, 01 and 10 (security rings 1 and 2 respectively) are seldom used.

The rules about what code can and can't do in user mode versus kernel mode are rather complicated, but suffice to say, user mode has severely reduced privileges.

Now, when people talk about the kernel of an operating system, they're referring to the portions of the OS's code that get to run in kernel mode with elevated privileges. Generally, the kernel authors try to keep the kernel as small as possible for security reasons, so that code which doesn't need extra privileges doesn't have them.

The C compiler is one example of such a program -- it doesn't need the extra 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 source code of the kernel, and the compiled executable of the kernel. Any machine with a C compiler can compile the kernel from the source code into the binary image. The question, then, is what to do with that binary image.

When you install Linux on a new system, you're installing a precompiled binary image, usually from either physical media (such as a CD DVD) or from the network. The BIOS will load the (binary image of the) kernel's bootloader from the media or network, and then the bootloader will install the (binary image of the) kernel onto your hard disk. Then, when you reboot, the BIOS loads the kernel's bootloader from your hard disk, and the bootloader loads the kernel into memory, and you're off and running.

If you want to recompile your own kernel, that's a little trickier, but it can be done.

Solution 5 - C

Which one was there first? the chicken or the egg?

Eggs have been around since the time of the dinosaurs..

..some confuse everything by saying chickens are actually descendants of the great beasts.. long story short: The technology (Egg) was existent prior to the Current product (Chicken)

You need a kernel to build a kernel, i.e. you build one with the other.

The first kernel can be anything you want (preferably something sensible that can create your desired end product ^__^)

This tutorial from Bran's Kernel Development teaches you to develop and build a smallish kernel which you can then test with a Virtual Machine of your choice.

Meaning: you write and compile a kernel someplace, and read it on an empty (no OS) virtual machine.

What happens with those Linux installs follows the same idea with added complexity.

Solution 6 - C

It's not turtles all the way down. Just like you say, you can't compile an operating system that has never been compiled before on a system that's running that operating system. Similarly, at least the very first build of a compiler must be done on another compiler (and usually some subsequent builds too, if that first build turns out not to be able to compile its own source code just yet).

I think the very first Linux kernels were compiled on a Minix box, though I'm not certain about that. GCC was available at the time. One of the very early goals of many operating systems is to run a compiler well enough to compile their own source code. Going further, the first compiler was almost certainly written in assembly language. The first assemblers were written by those poor folks who had to write in raw machine code.

You may want to check out the Linux From Scratch project. You actually build two systems in the book: a "temporary system" that is built on a system you didn't build yourself, and then the "LFS system" that is built on your temporary system. The way the book is currently written, you actually build the temporary system on another Linux box, but in theory you could adapt it to build the temporary system on a completely different OS.

Solution 7 - C

If I am understanding your question correctly. The kernel isn't "compiling itself" these days. Most Linux distributions today provide system installation through a linux live cd. The kernel is loaded from the CD into memory and operates as it would normally as if it were installed to disk. With a linux environment up and running on your system it is easy to just commit the necessary files to your disk.

If you were talking about the bootstrapping issue; dmckee summed it up pretty nice.

Just offering another possibility...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMainIDView Question on Stackoverflow
Solution 1 - Cdmckee --- ex-moderator kittenView Answer on Stackoverflow
Solution 2 - CsastaninView Answer on Stackoverflow
Solution 3 - Ca2800276View Answer on Stackoverflow
Solution 4 - CAdam RosenfieldView Answer on Stackoverflow
Solution 5 - CRic TokyoView Answer on Stackoverflow
Solution 6 - CThe SpooniestView Answer on Stackoverflow
Solution 7 - Cin70xView Answer on Stackoverflow