Sunday, August 24, 2008

PCC C extensions

Hi,

It's been a while that I wanted to know how PCC (Portable C Compiler) works . So I decided to use it for ksyl (my own educational kernel). I've been thinking that if pcc compiles the whole userland of OpenBSD and whole kernelland with some patches, then it must work with my own kernel :)

The first step for me is to fetch the source of pcc on my desktop and I am lucky because the source tree of my OS has it. So I just need to checkout pcc and compile it:

cvs -danoncvs.de.openbsd.org:/cvs co -P src/usr.bin/pcc
cd src/usr.bin/pcc
make depend && make && make install

Now I ve got a pcc binary and a cpp binary in /usr/local/bin/. So I need to fix the CC var in the ksyl Makefile :

CC = /usr/local/bin/pcc.

Everything seems to be ok, now I will try to compile ksyl :

ksyl@rincewind {4}> make

And then appears the first error :

include/./sys/gdt.h, line 15: syntax error
include/./sys/gdt.h, line 15: invalid function definition
include/./sys/gdt.h, line 15: syntax error
include/./sys/gdt.h, line 21: syntax error
include/./sys/gdt.h, line 21: invalid function definition
include/./sys/gdt.h, line 21: redeclaration of packed
include/./sys/gdt.h, line 21: syntax error
include/./386/idt.h, line 26: syntax error
include/./386/idt.h, line 26: invalid function definition
include/./386/idt.h, line 26: redeclaration of packed
include/./386/idt.h, line 26: syntax error
include/./386/idt.h, line 32: syntax error
include/./386/idt.h, line 32: invalid function definition
include/./386/idt.h, line 32: redeclaration of packed
include/./386/idt.h, line 32: syntax error

These errors came from the different gcc extentions needed to do packing like __attribute__((packed)). The packing of the structure included into gdt.h and idt.h allowed ksyl to work properly. Thus I googled "pcc packing" to find a solution without success to finally.... have a look in pcc sources. I found the solution in the pcc lexer. I must used a pragma :

#pagma pack(1)
struct idt
{
...
};
#pragma pack

After the patch I try to compile it :

ksyl@rincewind {4}> make
ksyl -> compilation OK

All works \o/

0 comments: