kern/99979: Get Ready for Kernel Module in C++

mag at mag at
Tue Jul 11 10:05:14 UTC 2006

Dag-Erling  [iso-8859-1] Sm  grav wrote:

> mag at writes:
>> But prior to long-term discussion, please commit my 4 patches
>> firstly.  They are nearly CPP-independent and do no harm to current
>> FreeBSD kernel.
> We don't do the kind of changes you propose without discussion.
>> ---	Fri Jun 30 05:15:25 2006
>> +++	Mon Jul 10 18:42:43 2006
>> @@ -88,7 +88,7 @@
>>  .if ${CC} == "icc"
>>  CFLAGS+=	-nolib_inline
>>  .else
>> -CFLAGS+=	-ffreestanding
>> +CFLAGS+=	-fno-builtin
>>  .endif
> Are you sure this is correct?  I'm pretty certain it isn't.  The
> kernel's C environment is not a hosted implementation.

GCC manual page gcc(1) says:

       Compile for a freestanding environment; this implies the  `-fno-
       builtin'  option,  and implies that main has no special require-

GCC manual says:

       Assert that compilation takes place in a freestanding environment.
       This implies -fno-builtin. A freestanding environment is one in
       which the standard library may not exist, and program startup may
       not necessarily be at main. The most obvious example is an OS
       kernel. This is equivalent to -fno-hosted.

But "-ffreestanding" doesn't work with C++. According to above
explanation, "-fno-builtin" is a subset of "-ffreestanding" and it's
enough for kernel.

In /usr/src/contrib/gcc/c-opts.c, "-ffreestanding" is recognized as:

     case OPT_ffreestanding:
       value = !value;
       /* Fall through....  */
     case OPT_fhosted:
       flag_hosted = value;
       flag_no_builtin = !value;
       /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
       if (!value && warn_main == 2)
	warn_main = 0;

In /usr/src/contrib/gcc/c-decl.c, "-ffreestanding" takes effects as:

   if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
	  != integer_type_node)
	  /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
	     If warn_main is -1 (-Wno-main) we don't want to be warned.  */
	  if (!warn_main)
	    pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
	  /* Make it so that `main' always returns success by default.  */
	  if (flag_isoc99)
	    c_expand_return (integer_zero_node);

>From above code we can conclude that "-ffreestanding" can affect only
something about main() more than "-fno-builtin".

I have compiled the whole FreeBSD kernel with "-fno-builtin" and met
no problems. Now I am running the kernel with "-fno-builtin" to write
to you with Linux ABI Mozilla-GTK1 1.7.12 and NVIDIA X11 driver 1.0-8762.

>> --- systm.h.orig	Mon Jul 10 05:42:58 2006
>> +++ systm.h	Mon Jul 10 18:44:01 2006
>> @@ -203,7 +203,7 @@
>>  int	suword16(void *base, int word);
>>  int	suword32(void *base, int32_t word);
>>  int	suword64(void *base, int64_t word);
>> -intptr_t casuptr(intptr_t *p, intptr_t old, intptr_t new);
>> +intptr_t casuptr(intptr_t *p, intptr_t old, intptr_t __new__);
> This is a namespace violation.  A simpler solution is to leave out
> argument names entirely.

If the code style permits, I agree with you.

GCC support library usually uses "namespace" to add various prefixes
to its built-in functions/variables in order to avoid conflicts against
user code. In ELF binary file, built-in functions/variables' names are
much longer than "__new__". See files in

                                                From Beijing, China

More information about the freebsd-hackers mailing list