System unit structure

From Free Pascal wiki

English (en) español (es) français (fr)

(work in progress - incomplete!)

This page is supposed to document functions and procedures serving as "interfaces" between common and platform specific parts of unit System as well as commonly used subroutines for particular tasks performed during unit System initialization and specify their behaviour. This should then serve as specification for maintainers of existing and new platforms to ensure fully compatible behaviour (as far as possible considering limitations of the individual platforms).

All listed routines are mandatory (at least as stub) unless specifically mentioned otherwise. If name of the usual source file containing implemenation of the particular routine is not mentioned, it's supposed to be system.pas for platform specific parts and for the shared routines.


The following types are assumed to be defined in platform specific part of System unit interface.

  • THandle
  • TThreadID
  • TRTLCriticalSection
  • PRTLCriticalSection

Constants and variables

The following list includes mandatory constants and variables that have to be declared (and usually initialized to reasonable values for the particular platform) for all platforms:

  • LineEnding
  • DirectorySeparator
  • DriveSeparator
  • PathSeparator
  • MaxExitCode
  • MaxPathLen
  • UnusedHandle
  • StdInputHandle
  • StdOutputHandle
  • StdErrorHandle
  • LFNSupport
  • FileNameCaseSensitive
  • CtrlZMarksEof
  • sLineBreak
  • DefaultTextLineBreakStyle
  • ArgC
  • ArgV
  • EnvC
  • EnvP





Implemented in

Initialization of internal structures used by the default heap memory manager. No heap may be used before calling InitHeap.


Initialization of internal structures used by the exception support.



Common part of initialization for standard file variables. It's similar to Assign and Reset/Rewrite calls for normal text files, but the files are associated with empty file names and no attempt is made to open the files (they're assumed to be already open).












Sysinit unit and indirect entry information

(note: This is currently at very initial stage and applies only to i386-win32 target)

Supporting runtime packages requires possibility to link the system unit and the main program into different executable modules. Therefore, global information generated by compiler, such as unit initialization table, cannot be directly referenced from the system unit. Instead, it is referenced from a separate small unit ('sysinit') and passed to routines in system unit via function arguments. The module entry point is placed in sysinit unit, too. A copy of sysinit unit is statically linked into every executable module, so every module ends up containing the initialization table only for the units it actually contains.

This global information is not limited to unit initialization table, see TEntryInformation record for complete list.

A target may have more than one sysinit unit, each solving specific initialization tasks. A particular unit to use is determined by compiler, see InitSysInitUnitName method in descendants of TLinker class. For example, while normally using sysinitpas.pp, complier will use sysinitcyg.pp instead if it detects linking with cygwin libraries.

Platform specific

unit initialization

The following tasks are performed:

  • IsLibrary variable initialization (true if running in a shared library, false otherwise)
  • ProcessID and ThreadID variables initialization
  • IsConsole variable initialization (true if running in a text console, false if running in windowed GUI application)
  • ExitProc variable initialization <TODO>
  • Heap initialization (calling shared InitHeap)
  • Initialization of potential procedural variables and/or parameters needed to support System unit routines requiring different implementation depending on detected version of operating system (possibly including dynamic loading of procedures or functions from shared libraries where appropriate) - see OS aware RTL
  • InOutRes variable initialization (set to 0)
  • Environment variables initialization (EnvC and EnvP), usually by calling InitEnvironment - only needed for platforms not providing access to these variables natively
  • Internal thread manager is initialized by calling InitSystemThreads
  • Internal variant manager is initialized by calling InitVariantManager
  • Internal widestring manager is initialized by calling InitWideStringManager


Only needed for platforms not providing access to EnvC and EnvP variables natively.

Procedure checks amount of environment variables and stores it in EnvC. Then memory for EnvP^ is allocated from heap (array of EnvC + 1 items of type PChar - the list pointer will point to nil). Finally the individual items in the array are initialized with PChars pointing to null terminated strings containing the environment variable name, equal sign and the environment variable value. Memory for the individual null terminated strings may be allocated from heap for these strings and they may be copied there if needed (i.e. if the particular platform doesn't provide pointer to a fixed location for these strings in required format).


Only needed for platforms not providing access to ArgC, ArgV and/or CmdLine variables natively.



Initialization of standard file variables (Input, Output, ErrOutput, StdOut and StdErr).




General tasks

Platform specific

File handling

Usually implemented in file


Heap management


Threading Support




Public routines

The following routines are part of System unit interface and are supposed to be implemented fully in platform specific way. Their specification is available in FPC documentation, so they're listed here mostly for completeness sake to provide complete list for porting purposes.

Directory handling

Usually implemented in

  • MkDir
  • RmDir
  • ChDir
  • GetDir


  • Randomize
  • ParamCount, ParamStr (possibly to be moved to common - <TODO>)







Platform specific



An editor has declared this article to be a stub, meaning that it needs more information. Can you help out and add some? If you have some useful information, you can help the Free Pascal Wiki by clicking on the edit box on the left and expanding this page.