# Sandy-Bridge Core i5 2500K / GCC 4.6 / Illegal instruction (core dumped)



## NuLL3rr0r (Apr 30, 2011)

Hi Folks,

I bought a new PC with FreeBSD compatibility in mind (ASUS P8P67 + Core i5 2500K). Then I configured my make.conf like this:


```
CPUTYPE?=native
CFLAGS= -O2 -fno-strict-aliasing -pipe -fomit-frame-pointer

.if !empty(.CURDIR:M/usr/ports/*) && exists(/usr/local/bin/gcc46)
CC=gcc46
CXX=g++46
CPP=cpp46
.endif


# added by use.perl 2011-04-29 23:52:16
PERL_VERSION=5.10.1
```

But when I built some ports like pkg-config I got this:

```
checking system definitions for POLLIN POLLOUT POLLPRI POLLERR POLLHUP POLLNVAL.
.. done
configure: creating ./config.status
config.status: creating glib.spec
config.status: creating Makefile
config.status: creating glib-config
config.status: creating gmodule/gmoduleconf.h
config.status: creating gmodule/Makefile
config.status: creating gthread/Makefile
config.status: creating docs/Makefile
config.status: creating docs/glib-config.1
config.status: creating tests/Makefile
config.status: creating glib.pc
config.status: creating gmodule.pc
config.status: creating gthread.pc
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
config.status: executing default-1 commands
creating glibconfig.h
config.status: executing default commands
===>  Building for pkg-config-0.25_1
Illegal instruction (core dumped)
*** Error code 1

Stop in /usr/ports/devel/pkg-config.
```

It seems GCC produces some code which is not compatible with my CPU. This is what I've got from GCC 4.6 when I compile a Hello World program:

```
13x17# g++46 -v -march=native -o t t.cpp
Using built-in specs.
COLLECT_GCC=g++46
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1
/lto-wrapper
Target: x86_64-portbld-freebsd8.2
Configured with: ./../gcc-4.6-20110422/configure --disable-nls --libdir=/usr/loc
al/lib/gcc46 --libexecdir=/usr/local/libexec/gcc46 --program-suffix=46 --with-as
=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/g
cc46/include/c++/ --with-ld=/usr/local/bin/ld --with-libiconv-prefix=/usr/local
--with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --disable-libgcj
 --prefix=/usr/local --mandir=/usr/local/man --infodir=/usr/local/info/gcc46 --b
uild=x86_64-portbld-freebsd8.2
Thread model: posix
gcc version 4.6.1 20110422 (prerelease) (FreeBSD Ports Collection)
COLLECT_GCC_OPTIONS='-v' '-march=native' '-o' 't' '-shared-libgcc'
 /usr/local/libexec/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/cc1plus -quiet -v
t.cpp -march=corei7-avx -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx --param l1-c
ache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=6144 -mtune=cor
ei7-avx -quiet -dumpbase t.cpp -auxbase t -version -o /var/tmp//ccdxjzo7.s
GNU C++ (FreeBSD Ports Collection) version 4.6.1 20110422 (prerelease) (x86_64-p
ortbld-freebsd8.2)
        compiled by GNU C version 4.6.1 20110422 (prerelease), GMP version 5.0.1
, MPFR version 3.0.0, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8
.2/4.6.1/../../../../../x86_64-portbld-freebsd8.2/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/lib/gcc46/include/c++/
 /usr/local/lib/gcc46/include/c++//x86_64-portbld-freebsd8.2
 /usr/local/lib/gcc46/include/c++//backward
 /usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/include
 /usr/local/include
 /usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/include-fixed
 /usr/include
End of search list.
GNU C++ (FreeBSD Ports Collection) version 4.6.1 20110422 (prerelease) (x86_64-p
ortbld-freebsd8.2)
        compiled by GNU C version 4.6.1 20110422 (prerelease), GMP version 5.0.1
, MPFR version 3.0.0, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 1cba659807ed6c09fa5b2b94711e7744
COLLECT_GCC_OPTIONS='-v' '-march=native' '-o' 't' '-shared-libgcc'
 /usr/local/bin/as -v -o /var/tmp//ccq3XPOP.o /var/tmp//ccdxjzo7.s
GNU assembler version 2.21 (x86_64-portbld-freebsd8.2) using BFD version (GNU Bi
nutils) 2.21
COMPILER_PATH=/usr/local/libexec/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/:/usr
/local/libexec/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/:/usr/local/libexec/gcc
46/gcc/x86_64-portbld-freebsd8.2/:/usr/local/lib/gcc46/gcc/x86_64-portbld-freebs
d8.2/4.6.1/:/usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/:/usr/local/lib/g
cc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/../../../../../x86_64-portbld-freebsd8.
2/bin/
LIBRARY_PATH=/usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/:/usr/loca
l/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/../../../../../x86_64-portbld-fr
eebsd8.2/lib/:/usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/../../../
:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-march=native' '-o' 't' '-shared-libgcc'
 /usr/local/libexec/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/collect2 --eh-fram
e-hdr -V -dynamic-linker /libexec/ld-elf.so.1 -o t /usr/lib/crt1.o /usr/lib/crti
.o /usr/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/crtbegin.o -L/usr/lo
cal/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1 -L/usr/local/lib/gcc46/gcc/x86
_64-portbld-freebsd8.2/4.6.1/../../../../../x86_64-portbld-freebsd8.2/lib -L/usr
/local/lib/gcc46/gcc/x86_64-portbld-freebsd8.2/4.6.1/../../.. /var/tmp//ccq3XPOP
.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/local/lib/gcc46/gcc/x86_64-
portbld-freebsd8.2/4.6.1/crtend.o /usr/lib/crtn.o
GNU ld (GNU Binutils) 2.21
  Supported emulations:
   elf_x86_64_fbsd
   elf_i386_fbsd
   elf_x86_64
   elf_i386
   elf_l1om
   elf_l1om_fbsd
```

As you can see I've got Corei7-avx for march and mtune, and mavx parameters for cc1plus:

```
-march=corei7-avx -mcx16 -msahf -maes -mpclmul -mpopcnt -mavx --param l1-c
ache-size=32 --param l1-cache-line-size=64 --param l2-cache-size=6144 -mtune=cor
ei7-avx
```

Which I'm sure supported by my CPU: http://www.phoronix.com/scan.php?page=article&item=intel_corei5_2500k&num=1.

When I portupgrade -af using CPUTYPE?=core2 everything builds fine. But I've got -march=nocona which I'm not comfortable with. I think it comes from  /usr/share/mk/bsd.cpu.mk

```
.  endif
. elif ${MACHINE_ARCH} == "amd64"
.  if ${CPUTYPE} == "prescott" || ${CPUTYPE} == "core2"
CPUTYPE = nocona
.  endif
. endif
```

I still did not tried 4.7, because I doubt its stability.

Any suggestions?


----------



## xibo (May 1, 2011)

At automake/autoconf based projects like pkgconfig, the *configure* process will create a file at its build path named config.log, namely $WRKDIRPREFIX/$PORTSDIR/devel/pkg-config-0.25/config.log, which contains a detailed listing of what each of the steps performed by configure really did. It therefore also contains a hint what crashed on your setup.

It'll be either gcc-4.6 or a binary built with gcc-4.6 though. FYI, while it's nice to play with new gcc versions on your own projects, it's traditionally very .... .... "brave" to compile your system with a dot-zero version of gcc, especially if it's supposed to be a production environment. So rather use 4.5 even if it builds 2% slower code for your new chip.


----------



## wblock@ (May 1, 2011)

Setting custom CFLAGS often results in problems.  Even if they aren't the super go-fast variety, they override the custom settings in some ports.  Eliminate that and rebuild everything that was built with them in place.  Then see if gcc 4.6 will work.


----------



## NuLL3rr0r (May 5, 2011)

Thank you for your response. And sorry for delay, I'd a very busy week.

I tried 4.7 too and the result were the same.

@wblock
Thank you for your suggestion, I removed CFLAGS line from make.conf and rebuilt all ports using native CPUTYPE, but results are the same.

@xibo
Thank you for the hint. There was bunch of errors in config.log. I'll attach config.log for binutils package at the end.
I'm using gcc-4.5.4 on my web server, But this is my development machine that's why I'm using gcc-4.6.1 on this machine (I built a complete FreeBSD box using 4.6.1 on a MacBook Pro with Core 2 Duo processor and it has no problem, of course some ports refuse to built).

Thanks anyway.


----------



## NuLL3rr0r (May 5, 2011)

Sorry forgot to attach the config.log file .


----------



## NuLL3rr0r (May 5, 2011)

http://en.wikipedia.org/wiki/SIGILL


----------



## NuLL3rr0r (Jun 17, 2011)

Finally after a month playing with *GCC* and make.conf, I got my FreeBSD box running on this system.

Now I realized why:
1. Even Gentoo folks still using GCC 4.4.5. (@xibo: you are right on this)
2. Too much optimization is EVIL. (@wblock: you are right on this)

I used HACK-on-HACK-on-HACK-.....-on-HACK method to build a FreeBSD box using the above settings in make.conf (I installed trouble maker packages like gmake, etc from binary packages. Until I get to the last package: GCC itself). But even *GCC* itself get killed by a *SIGILL* signal and getting _core dumped_.

I tried GCC 4.4.7, 4.5.4, 4.6.1, 4.7.0 and results are the same. When I used native *CPUTYPE* on 4.5 and 4.4 they detect my CPU as core2 but that makes a same crap too.

For example NTFS-3G always after some moments get crashed an my NTFS partitions are not usable till next reboot, so many ports refuse to build, etc.

But using your suggestions I finally realized that I'm wrong, And now I have a running solid *BSD* desktop.

This is my final make.conf:

```
CPUTYPE?=core2

.if !empty(.CURDIR:M/usr/ports/*) && exists(/usr/local/bin/gcc45)
CC=gcc45
CXX=g++45
CPP=cpp45
.endif


# added by use.perl 2011-05-24 21:26:11
PERL_VERSION=5.12.3
```

And pretty much liked that. It makes a stable FreeBSD box now.

Thank you guys.


----------

