Flat Program

This is the simplest possible file format. It is intended for simple programs whose code use almost nothing from the standard libraries. This executable file format is used on x86 platforms.

2  EXESGN  Signature ("FP" or "PP").
1  PFLAGS  Presentation flags
             Bit 0: 1=Initialization code present
             Bit 1: 1=Run-time code present
             Bit 2: 1=Shutdown code present
             Bit 3: 0=No variables and 4K stack
                    1=Variables present and stack size specified
             Bit 4: 1=Initialized variables present
             Bit 5: 1=RTL libraries links present
             Bit 6: 1=Shared library present
             Bit 7: 1=Library initialization section present
1  CLRLEN  Length of the cleared portion of the program data
           section (optional).
1  STKLEN  Length of the stack (optional).
4  LISLEN  Length of the library init section image (optional).
4  PISLEN  Length of the program init section image in bytes (optional).
4  LRSLEN  Length of the library run-time section image in bytes (optional).
4  PCSLEN  Length of the program cleanup section image in bytes (optional).
4  VARLEN  Length of the initialized variables in bytes (optional).
?  LENLST  Lengths of the pages of the program image (optional)
?  LIBREF  Library references (optional).
?  LIBPUB  Library public symbols description (optional).
?  LISIMG  Library initialization section image (optional).
?  PISIMG  Program init section image (optional).
?  PRSIMG  Program run-time section image (optional).
?  PCSIMG  Program cleanup section image (optional).
?  LRSIMG  Library run-time section image (optional).
?  VARIMG  Image of the initialized portion of the program variables
           (optional).

The first two bytes of the file are called EXESGN and contain the characters 'F' and 'P' that identify the file as OSHS Flat Program or 'P' and 'P' that identify the Packed Flat Program. Other values identify other executable file formats that are not described by this specification.

The next byte called PFLAGS are the presentation flags. It describes which optional sections of the file are present.

The STKLEN byte describes how many bytes the system should allocate for the process's stack. The field uses a special format and it counts only the free space for the stack (the pointers to the program arguments and the enviroment string which all appear in the stack are not counted here). It shows only the minimal count of bytes the stack must have; the system may allocate bigger section of memory if available. The loader is not allowed to attempt to run a program for which it cannot provide stack as long as described in this field.

The CLRLEN byte describes how many bytes in the program's variables section must be initialized to zeroes. The program data area consists of the initialized variables (see the VARIMG description later), the zeroed variables and the heap space. The length of the place for the zeroed variables is specified by the CLRLEN byte. It uses the same special format as the STKLEN byte.

Both bytes are present only when the bit 3 of the PFLAGS field is set. If the bit is clear, no data section is created for the program and the stack is assumed to have 4K.

The INILEN and INIIMG fields describe the initialization section of the program. The initialization section is treated as a procedure, i.e. if present, the system calls it using the address of the first byte of the section as the calling address. These fields are present only if bit 0 of PFLAGS is set. After the procedure in this section (the one starting at the very beginning) returns, the content of this section is discarded and the memory used by this section is freed.

Similarly the DONLEN and DONIMG fields describe the shutdown section of the program and they are present only if bit 3 is set. Also this section is treated as a procedure.

The bit 1 denotes the presence of the run-time code in the program. Usually it is set to 1 in programs and to 0 in libraries. But the binary file format allows binaries that are both, a program and a library. The function main() is located at the very first byte of this section.

The length of the run-time code in an unpacked program can be computed as the length of the file minus the lengths of the header and other fields present in the file. This saves 4 bytes that would be used for field like "program code size".

The LENLST field is present only if the program is a packed one, which is indicated in the signature (packet programs have "PP" as a signature). The length of the program code then can be computed by summing the lengths of the packed pages of the file.