What GNU C extensions are available that aren't trivial to implement in C99?
Asked Answered
T

4

8

How come the Linux kernel can compile only with GCC? What GNU C extensions are really necessary for some projects and why?

Tetravalent answered 20/4, 2010 at 20:49 Comment(4)
If you poke around you can find a question with links to other compilers that have successfully built linux. tcc can do it for some kernel versions at least.Swearingen
This article explains the extensions used: GCC hacks in the Linux kernel. Some of them are trivial, some are not (mostly the optimization tricks).Supererogate
The title and question don't match very well.Cece
dmckee: The Linux kernel needs to be patched a bit to be able to be built with tcc.Tetravalent
B
10

Here's a couple gcc extensions the Linux kernel uses:

  • inline assembly
  • gcc builtins, such as __builtin_expect,__builtin_constant,__builtin_return_address
  • function attributes to specify e.g. what registers to use (e.g. __attribute__((regparm(0)),__attribute__((packed, aligned(PAGE_SIZE))) ) )
  • specific code depending on gcc predefined macros (e.g. workarounds for certain gcc bugs in certain versions)
  • ranges in switch cases (case 8 ... 15:)

Here's a few more: http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/

Much of these gcc specifics are very architecture dependent, or is made possible because of how gcc is implemented, and probably do not make sense to be specified by a C standard. Others are just convenient extensions to C. As the Linux kernel is built to rely on these extensions, other compilers have to provide the same extensions as gcc to be able to build the kernel.

It's not that Linux had to rely on these features of gcc, e.g. the NetBSD kernel relies very little on gcc specific stuff.

Beniamino answered 20/4, 2010 at 20:49 Comment(1)
The IBM Link is outdated.Alexandro
S
3

GCC supports Nested Functions, which are not part of the C99 standard. That said, some analysis is required to see how prevalent they actually are within the linux kernel.

Symptomatology answered 20/4, 2010 at 20:49 Comment(0)
D
2

Linux kernel was written to be compiled by GCC, so standard compliance was never an objective for kernel developers.

And if GCC offers some useful extensions that make coding easier or compiled kernel smaller or faster, it was a natural choice to use these extensions.

Dichromatism answered 20/4, 2010 at 20:49 Comment(0)
A
0

I guess it's not they are really that necessary. Just there are many useful ones and cross-compiler portability is not that much an issue for Linux kernel to forgo the niceties. Not to mention sheer amount of work it would take to get rid of relying on extensions.

Adim answered 20/4, 2010 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.