Are there global constructors in the Linux kernel?

Giovanni Mascellani gio at debian.org
Wed Oct 9 02:54:02 EDT 2019


Hi,

[sending again after having replied just to the OP by mistake, sorry!]

Il 09/10/19 05:44, CRISTIAN ANDRES VARGAS GONZALEZ ha scritto:
> The gcc compiler searches for this function to run my program, but
> before that happens, I can use the global constructors and write a code
> that runs before the main function, but in the case of writing a kernel,
> why do I need to run lines of code before the kernel is executed ?, I
> understand that when writing a kernel there is no main function and that
> it is disabled to indicate it to the compiler. In the Linux kernel are
> there global constructors? If so, what things are executed before
> starting the kernel?

I am not a Linux expert, but I think I know enough on this specific
topic to give at least a partial answer: the global constructors feature
depends on the initialization code in the C runtime environment, which
depends on the operating system. In the case of the kernel, there is not
another operating system around, so everything that must happen at
initialization time must be done by the kernel itself, including setting
up the stack, zeroing uninitialized data and calling whatever you want
to be called. There cannot be any automatic way to do these things,
because there is nobody except the kernel itself who can do that.

This book has a nice and rather detailed write-up on what happens when
Linux boots (at least on x86_64):

  https://0xax.gitbooks.io/linux-insides/

You will see that nothing is called "automatically" like for global
constructors: everything is called explicitly and at the right moment.
This is also necessary because early kernel initialization routines
cannot assume that the system is in a consistent state, since their job
is to bring it to be. Therefore you must control precisely what must
happen when.

Hope this helps!

Giovanni.
-- 
Giovanni Mascellani <g.mascellani at gmail.com>
Postdoc researcher - Université Libre de Bruxelles





-- 
Giovanni Mascellani <g.mascellani at gmail.com>
Postdoc researcher - Université Libre de Bruxelles

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: OpenPGP digital signature
URL: <http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20191009/6c0f5668/attachment.sig>


More information about the Kernelnewbies mailing list