How programs are built¶
32-bit. clang compiles to ELF objects; JWlink links them with
format os2 lx. crt0.o is first: OS/2 calls _start with the
environment and the command line, which become environ and argv. The
heap is address space reserved with DosAllocMem and committed as
malloc grows it. Programs are marked to run in a window (pmcompatible);
-mwindows marks a PM program. A DLL (-shared) starts with dllcrt0.o,
is initialized for each process that loads it, and has its data per
process. Mark its exports with __export (or VisualAge's _Export, or
__attribute__((dllexport))): clang leaves an -export: directive for
each in the object, which JWlink reads.
16-bit. clang compiles to OMF objects for a memory model (small,
medium, compact or large; OS/2 has no tiny or huge model) and JWlink links
them with format os2. The code is compiled as for 16-bit Windows: the
stack segment may not be DGROUP, so va_list and returned structures are
far. The heap is the rest of DGROUP, grown to 64 KiB with DosReallocSeg,
in the small and medium models, and a 64 KiB segment from DosAllocSeg in
the compact and large ones.
16-bit DLLs. -shared links a DLL with libentry.obj first. Like a
32-bit DLL it is initialized for each process that loads it
(initinstance) and each process gets its own DGROUP (manyautodata),
so the C library in the DLL keeps a state for each; the initialization
sets up the heap and environ and runs the constructors. OS/2 1.x has no
call when a DLL is freed, so destructors and atexit functions of a DLL
do not run. Export functions with __export __far __pascal; exported
functions load the DLL's DGROUP into DS themselves, as with __loadds.
A DLL runs on the stack of the thread that calls it, so SS is not DS
and a near pointer cannot point to a local. In the small and medium models
clang (-mwindows-dll, which -shared gives) keeps the locals whose
address escapes in a frame in DGROUP instead, from a 4 KiB region for each
thread taken from the DLL's heap; a DLL that needs more defines
unsigned short __os2_dll_frame_size = 8192;. The C library built this
way is in lib/os2/<model>/dll, which clang links with -shared. Compact
and large DLLs need none of it.
Vio, Kbd and Mou from 32-bit code. These calls are 16-bit far
Pascal functions in VIOCALLS, KBDCALLS and MOUCALLS. Each function of
bsesub.h is an inline one that lays out the 16-bit arguments and calls
__os2_call16 (lib/call16.c, in libos2.a, which the driver links).
That switches to a 16-bit stack of the thread's own and jumps to the
call's stub, 16-bit code in the program that calls the function (call far
VIO16WRTTTY, fixed up by the loader as in a 16-bit program) and comes
back. Pointers become 16:16 ones through the tiles of OS/2's memory, and a
buffer that crosses a 64 KiB boundary is copied for the call.
Resources. tools/os2rc.py compiles a .rc file in the OS/2
toolkits' resource language (STRINGTABLE, MENU, ACCELTABLE, DLGTEMPLATE
with DIALOG, FRAME, WINDOW, CONTROL and the predefined controls, RCDATA,
ICON, POINTER, BITMAP, FONT and user types, with #include and #define
from the headers) and binds the resources into a program: a 32-bit one
gets one more object in its LX file, and a 16-bit one a segment for each
resource in its NE file, which DosGetResource2 returns:
clang --target=i386-pc-os2 -mwindows -Os pmnotes.c -o pmnotes.exe
python3 tools/os2rc.py -I include/os2v2 examples/pmnotes.rc pmnotes.exe
python3 tools/os2rc.py -l pmnotes.exe # list the resources
python3 tools/os2rc.py app.rc app16.exe # a 16-bit program: os21x headers
-r compiles to a .res file only, which a later run binds (-16 -r
for a 16-bit program, whose .rc sees the os21x headers).
The C library is picolibc with an OS/2 layer (libos/os2): a file
descriptor is the OS/2 file handle, and \n is written as \r\n to the
console and other devices. Files are binary. Directories are read with
opendir and readdir (DosFindFirst); getcwd, chdir, getpid and
getppid work too. chdir sets the current directory of a drive but does
not change the current drive.
OS/2 has no fork, so programs are started with DosExecPgm: system
runs COMSPEC (or CMD.EXE from the PATH) with /c, and
<process.h> has the spawn calls of OS/2's C compilers (spawnl,
spawnv, spawnvp, spawnle and the rest). P_WAIT returns the
program's exit code, and P_NOWAIT its process id, which waitpid and
wait wait for. exec starts the program and exits with its exit code.
A name without an extension gets .EXE, and the p forms search the
current directory and then the PATH. pipe, dup and dup2 work on
OS/2's handles, and popen runs the command with this program's end of
the pipe kept from it, so pclose gives it the end of its input.