INF: How the Linker Builds an Overlaid MS-DOS Executable

ID Number: Q81368

3.x 4.0x 4.10 5.0x 5.10 5.13 5.15 5.20 5.30

MS-DOS

Summary:

A DOS executable can be up to 1 MB in size. An overlaid DOS executable

can be up to 16 MB. This is done by instructing the linker to divide

the executable into the resident portion that is loaded at load time

and overlays that can be loaded into memory as needed. Link still

produces a single .EXE file. The amount of memory required to run the

program is reduced, but more time will be spent in overhead. The

executable will also take up more disk space because code for the

overlay manager is linked in.

Link substitutes all far calls from resident portion to overlay or

from overlay to overlay with the following sequence:

int IntNo ; IntNo specified with /O, default is 3Fh

DB OverlayNum ; The overlay number

DW OverlayEntry ; Offset of the entry point

The overlay manager takes control wherever link has made this

substitution. It determines which overlay is being called and whether

it is already in memory. If the overlay is not loaded, it reads it

from the executable file and loads it.

More Information:

An overlay is specified on the link command line by parentheses. The

linker assigns segments to overlays, not object modules to overlays.

In the following example command line

link main.obj (mod1.obj mod2.obj) (mod3.obj);

the resident portion will consist of the segments declared in MAIN.OBJ

and any segments defined without class 'code' in MOD1.OBJ, MOD2.OBJ,

or MOD3.OBJ. The linker assigns only segments with class 'code' to

overlays. The particular overlay that a code segment is assigned to is

determined by the object that first defines the segment. For example,

if both MAIN.OBJ and MOD1.OBJ contain a segment ASEG, MOD1.OBJ's ASEG

will end up in the resident portion because ASEG was first defined in

MAIN.OBJ.

Link adds three segments to an overlaid program: OVERLAY_AREA,

OVERLAY_END, and OVERLAY_DATA. The overlay manager loads an overlay

into OVERLAY_AREA as it is needed. OVERLAY_END immediately follows

OVERLAY_AREA so that the size of the overlay area can be found by

subtracting the segment address of OVERLAY_AREA from the segment

address of OVERLAY_END. The OVERLAY_DATA segment is initialized by the

linker with information that the overlay manager will need.

OVERLAY_DATA is included in DGROUP.

Code for the overlay manager comes from module OVLM6L of the run time

libraries that come with Microsoft high-level languages like C and

FORTRAN. The code resides in the _TEXT segment.

An overlaid executable contains an executable header for each overlay.

The structure of the executable from the sample above is

----------------------

| .EXE header | Overlay 0h

----------------------

| |

| Resident portion |

| and |

| overlay segments |

| |

----------------------

| .EXE header | Overlay 1h

----------------------

| |

| Code segments |

| |

----------------------

| .EXE header | Overlay 2h

----------------------

| |

| Code segments |

| |

----------------------

At run time, the resident portion of the executable initializes the

overlay interrupt vector specified by the linker (default 3Fh) with

the address of the overlay manager. The overlay manager takes over

upon the appropriate software interrupt. When it needs to load an

overlay, it gets the filename from OVERLAY_DATA, locates the overlay

using information from that overlay's .EXE header, reads the overlay

into OVERLAY_AREA, and transfers control.

An overlaid executables image in memory is laid out as follows:

----------------------+-> high memory address

| |

| ROOT (data) |

| |

----------------------+-> OVERLAY_END

| |

| OVERLAY AREA (code) |

| |

----------------------+-> OVERLAY_AREA

| |

| ROOT (code) |

| |

----------------------+-> low memory address

Note:

- Segment names in an overlay must differ from those of the resident

portion. In a high-level language, this is accomplished by

compiling for the medium, large, or huge memory models.

- The /INCR[EMENTAL] option is not supported with overlays.

- The DOS portion of a bound executable cannot be overlaid.

- Never rename an executable program file containing overlays if it

is to be run under MS-DOS 2.x or earlier.

For additional information, refer to Chapter 15 of the "Microsoft

C/C++ Environment and Tools" manual. The information in this article

pertains to static overlays only. See MOVEAPI.TXT for a discussion of

the new dynamic overlays (called Microsoft Overlaid Virtual Environment,

or MOVE).

Reference(s):

"The MS-DOS Encyclopedia," page 715, Microsoft Press, 1988

Additional reference words: 3.00 3.01 3.02 3.05 3.04 3.06 3.51

3.55 3.60 3.61 3.64 3.65 3.69 4.06 4.07 4.10 5.01.20 5.02.21

5.02 5.03 5.05 5.10 5.13 5.15 5.20