Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the Linux kernel compile itself?

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.

like image 806
MainID Avatar asked Jan 30 '09 03:01

MainID


People also ask

Why we compile Linux kernel?

The main purpose of kernel compilation is to provide hardware support and software support that you do not need, or to add the software and hardware tools you need.

Is the Linux kernel compiled with GCC?

The Linux kernel to now supported building on kernels as far back as GCC 4.9 while now it has been bumped to GCC 5.1. AArch64 already required at least GCC 5.1 while this bump affects all other architectures.

How does the Linux kernel works?

The Linux kernel mainly acts as a resource manager acting as an abstract layer for the applications. The applications have a connection with the kernel which in turn interacts with the hardware and services the applications. Linux is a multitasking system allowing multiple processes to execute concurrently.


2 Answers

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".

like image 193
dmckee --- ex-moderator kitten Avatar answered Oct 13 '22 05:10

dmckee --- ex-moderator kitten


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.

  1. 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.

like image 34
sastanin Avatar answered Oct 13 '22 04:10

sastanin