Compiler Flags problem with core2 CPU

Giorgos Keramidas keramida at
Tue Mar 2 19:45:18 UTC 2010

On Sun, 28 Feb 2010 22:37:27 +0800, Aaron Lewis <aaron.lewis1989 at> wrote:
>Paul B Mahol wrote:
>>On 2/28/10, Aaron Lewis <aaron.lewis1989 at> wrote:
>>> Hi,
>>> I gonna recompile kernel for my core2 CPU , so i'd like to pass some
>>> flags to gcc.  Kinds of "-march=core2" , i tried to modify
>>> /etc/make.conf
>>>     e.g    CFLAGS += -march=core2 -O20 -ffast-math -mfpmath=sse
>>>     But it fails .. bad arch switch , core2 cpu is not supported ?
>> It is bad idea to compile kernel with custom flags. And gcc in FreeBSD
>> doesn't know about core2,
>> use 'native' if you must.
>>>     And is that useful to let gcc select cpu specified asm code ?
>> Only for some userland stuff like openssl.
> Really ? It's bad to use custom flags to compile kernel , why do you
> think so ?  I'd like to know more about this : )
> So setting optimize compiler flags is only useful for userland stuff ?

Please do not post your reply on *top* of the original text to this
list.  The preferred form of replying is bottom-posting here (other
lists may have their own rules, but that's ok).  I've fixed this message
manually, but it would be nice if you posted your reply to the bottom of
the quoted text.

You can definitely *try* using optimizations for the kernel too.  The
FreeBSD developers and other users cannot _force_ you to use only a very
limited set of options.  You are more than free to try new things.  This
is precisely the reason why we make the source tree available to
everyone, including detailed instructions for rebuilding the entire
system from source.

Note that the kernel is a very special program that may or may not work
with some of the optimizations performed by higher GCC levels, though.
There may be problems, so if you start building optimized kernels please
make sure you keep a 'safe' backup copy of /boot/kernel before you
install a new one.  This way you will at least be able to boot into the
old kernel if anything breaks.

One way to keep a backup copy of the kernel is to make sure your /boot
partition has enough free space and type as root:

    # cp -a /boot/kernel /boot/

Then if anything goes wrong, you can always break into the loader prompt
and type:

    boot> unload

    boot> set module_path="/boot/;/boot/modules"

    boot> load kernel

    boot> boot -s

Note that any optimization levels higher than the defaults are not
'supported' by the FreeBSD team though.  As the warning in make.conf
says [/usr/share/examples/etc/make.conf]:

  # CFLAGS controls the compiler settings used when compiling C code.
  # Note that optimization settings other than -O and -O2 are not recommended
  # or supported for compiling the world or the kernel - please revert any
  # nonstandard optimization settings to "-O" or "-O2 -fno-strict-aliasing"
  # before submitting bug reports without patches to the developers.

So if you start building highly-optimized kernels and userland binaries,
you are on your own.  Bumping in any problem will require that you
revert to the standard optimization flags, rebuild everything, try to
reproduce the problem again and *then* report it.

- Giorgos

More information about the freebsd-questions mailing list