HOW-TO: Customizing FPC target
- Introduction
- The things we need first
- Programme internals
- Building RTL
- Compiling your programme
- Resume
1. Introduction
A question about developing system or OS-free software has
been arised several times at the Free Pascal dedicated forums. I won't tell
a lot about modifing huge mass of source code but I shall try to add a special
target by simply providing a custom RTL.
The main purpose of this document is to get ready to compile
a Multiboot OS kernel which will be launched by GRUB. There's an answer on
how to produce a plain binary as well.
I'm not an FPC developer, I'm just a user. So for the further
info consider the programmers of the FPC.
Contact: Juras B. (Yury Benesh aka ybx) mailto:ybxsoft(put @)tut.by
http://ybx.narod.ru - don't be afraid of
the "ru", the index is in three languages.
This manual is too short, so ask questions and I may expand
it.
2. The things we need first
- FPC 1.0.10 (including misc. tools like make, fpcmake)
- RTL sources
3. Programme internals
The first piece, i.e. the entry point is a startup code
written in the assembler language. We need to specify a low-level initialization
and finalization here. For Win32 target the file wprt0.as contains
two functions: _mainCRTStartup and _WinMainCRTStartup. The code in the FPC
source passes the needed entry point function to the linker. So that _mainCRTStartup
is specified for a console (default) target.
The prt0.as file for Linux target contains function
_start. The common things between these startup files is that they both pass
control to a corresponding inside (in-library) function or directly
call the PASCALMAIN routine. The routine is placed in the output main
programme module. The generated assembly file for a GO32V2 and WIN32
main programme is almost the same. It contains PASCALMAIN label, which is
the actual start point, HEAPSIZE, HEAP labels, a table of unit's initialization
functions and several other definitions, the major of them are used by the
system unit.To generate a main module listing, write a programme with an empty
"begin;end." and execute "fpc -a -Anasmobj myprog.pas".
Now when we know some peculiarities of the already defined
targets we can write our own startup files (named accroding to the compiler
presumptions) and the system unit. For instance, if we use the Win32 target
we must use
wprt0.as as the startup code for a console application
and
syswin32.pas as a system unit. When compiling for the Win32 target
the compiler uses
aswand
ldw while for the Linux target it uses
ld and
as. I'll use the Linux target because I want to use
ELF-targeted binutils. The compiled
binutils package for Win32 which
produces ELF executables is available at my
home site.
4. Building RTL
So we selected the Linux target and as a result we'll use
Linux units directory. To make both real Linux and our customized target units
available, write a simple script which will rename the units directories,
i.e. to swap the names.
The next step is to create a basic RTL, mainly the system
unit and friends, for me they were system and strings units.
To create a unit we should take
system.pp from the
"source/rtl/template" directory, write a custom makefile and startup file.
- Create a directory in the /source/rtl/ tree. Let it be custom.
- Copy system.pp template as syslinux.pp.
- Create prt0.asm or prt0.as. I used the first one as I
prefer using NASM.
The file must contain _start function and a memory
for stack reserved in the bss section.
The _start function must initialize ESP register and FPU, then start PASCALMAIN
procedure.
- Create Makefile.fpc and write corresponding rules to build system,
strings, mmx, cpu and some other needed units.
- Run fpcmake Makefile.fpc, then run make and copy all .ppu, .o, .a files
to your units directory (/pp/units/mytarget/).
5. Compiling your programme
Now you're ready yo compile your programmes for the custom target. I used
the following
command line:
fpc -Tlinux -k-Ttext -k0x220000 -XX sample.pas
I specified the starting address for the
text section because I need
the kernel to be loaded at a certain address.
6. Resume
As you can see I explained everything as briefly as possible. So if you have
several questions feel free to ask me via email. The samples for this document
you'll find at my
home site.
6 September, 2003