The object module produced by MASM from a source file is in a form that contains relocation information and may also contain unresolved references to external locations or subroutines. It is written in a common format that is also produced by the various high-level compilers (such as FORTRAN and C) that run under MS-DOS. The computer cannot execute object modules without further processing.
The Microsoft Object Linker (LINK), distributed as the file LINK.EXE, accepts one or more of these object modules, resolves external references, includes any necessary routines from designated libraries, performs any necessary offset relocations, and writes a file that can be loaded and executed by MS-DOS. The output of LINK is always in .EXE load-module format. (See Chapter 3.)
As with MASM, you can give LINK its parameters interactively or by entering all the required information in a single command line. If you enter the name of the linker alone, the following type of dialog ensues:
C>LINK <Enter>
Microsoft Ò Overlay Linker Version 3.61
Copyright Ó Microsoft Corp 1983-1987. All rights reserved.
Object Modules [.OBJ]: HELLO <Enter>
Run File [HELLO.EXE]: <Enter>
List File [NUL.MAP]: HELLO <Enter>
Libraries [.LIB]: <Enter>
C>
If you are using LINK version 4.0 or later, the linker also asks for the name of a module-definition (.DEF) file. Simply press the Enter key in response to such a prompt. Module-definition files are used when building Microsoft Windows or MS OS/2 "new .EXE" executable files but are not relevant in normal MS-DOS applications.
The input file for this example was HELLO.OBJ; the output files were HELLO.EXE (the executable program) and HELLO.MAP (the load map produced by the linker after all references and addresses were resolved). Figure 4-3 shows the load map.
Start Stop Length Name Class
00000H 00017H 00018H _TEXT CODE
00018H 00027H 00010H _DATA DATA
00030H 000AFH 00080H STACK STACK
000B0H 000BBH 0000CH $$TYPES DEBTYP
000C0H 000D6H 00017H $$SYMBOLS DEBSYM
Address Publics by Name
Address Publics by Value
Program entry point at 0000:0000
Figure 4-3. Map produced by the Microsoft Object Linker (LINK) during the generation of the HELLO.EXE program from Chapter 3. The program contains one CODE, one DATA, and one STACK segment. The first instruction to be executed lies in the first byte of the CODE segment. The $$TYPES and $$SYMBOLS segments contain information for the CodeView debugger and are not part of the program; these segments are ignored by the normal MS-DOS loader.
You can obtain the same result more quickly by entering all parameters in the command line, in the following form:
LINK options objectfile, [exefile], [mapfile], [libraries]
Thus, the command-line equivalent to the preceding interactive session is
C>LINK HELLO,HELLO,HELLO,, <Enter>
or
C>LINK HELLO,,HELLO; <Enter>
If you enter a semicolon as the last character in the command line, LINK assumes the default values for all further parameters.
A third method of commanding LINK is with a response file. A response file contains lines of text that correspond to the responses you would give the linker interactively. You specify the name of the response file in the command line with a leading @ character, as follows:
LINK @filename
You can also enter the name of a response file at any prompt. If the response file is not complete, LINK will prompt you for the missing information.
When entering linker commands, you can specify multiple object files with the + operator or with spaces, as in the following example:
C>LINK HELLO+VMODE+DOSINT,MYPROG,,GRAPHICS; <Enter>
This command would link the files HELLO.OBJ, VMODE.OBJ, and DOSINT.OBJ, searching the library file GRAPHICS.LIB to resolve any references to symbols not defined in the specified object files, and would produce a file named MYPROG.EXE. LINK uses the current drive and directory when they are not explicitly included in a filename; it will not automatically use the same drive and directory you specified for a previous file in the same command line.
By using the + operator or space characters in the libraries field, you can specify up to 32 library files to be searched. Each high-level- language compiler provides default libraries that are searched automatically during the linkage process if the linker can find them (unless they are explicitly excluded with the /NOD switch). LINK looks for libraries first in the current directory of the default disk drive, then along any paths that were provided in the command line, and finally along the path(s) specified by the LIB variable if it is present in the environment.
LINK accepts several optional switches as part of the command line or at the end of any interactive prompt. Figure 4-4 lists these switches. The number of switches available and their actions vary among different versions of LINK. See your Microsoft Object Linker instruction manual for detailed information about your particular version.
Switch Full form Meaning
/A:n /ALIGNMENT:n Set segment sector alignment factor.
N must be a power of 2 (default =
512). Not related to logical-segment
alignment (BYTE, WORD, PARA, PAGE,
and so forth). Relevant to segmented
executable files (Microsoft Windows
and MS OS/2) only.
/B /BATCH Suppress linker prompt if a library
cannot be found in the current
directory or in the locations
specified by the LIB environment
variable.
/CO /CODEVIEW Include symbolic debugging
information in the .EXE file for use
by CodeView.
/CP /CPARMAXALLOC Set the field in the .EXE file header
controlling the amount of memory
allocated to the program in addition
to the memory required for the
program's code, stack, and
initialized data.
/DO /DOSSEG Use standard Microsoft segment naming
and ordering conventions.
/DS /DSALLOCATE Load data at high end of the data
segment. Relevant to real-mode
programs only.
/E /EXEPACK Pack executable file by removing
sequences of repeated bytes and
optimizing relocation table.
/F /FARCALLTRANSLATION Optimize far calls to labels within
the same physical segment for speed
by replacing them with near calls and
NOPs.
/HE /HELP Display information about available
options.
/HI /HIGH Load program as high in memory as
possible.
/I /INFORMATION Display information about progress of
linking, including pass numbers and
the names of object files being
linked.
/INC /INCREMENTAL Force production of .SYM and .ILK
files for subsequent use by ILINK
(incremental linker). May not be used
with /EXEPACK. Relevant to segmented
executable files (Microsoft Windows
and MS OS/2) only.
/LI /LINENUMBERS Write address of the first
instruction that corresponds to each
source-code line to the map file. Has
no effect if the compiler does not
include line-number information in
the object module. Force creation of
a map file.
/M[:n] /MAP[:n] Force creation of a .MAP file listing
all public symbols, sorted by name
and by location. The optional value n
is the maximum number of symbols that
can be sorted (default = 2048); when
n is supplied, the alphabetically
sorted list is omitted.
/NOD /NODEFAULTLIBRARYSEARCH Skip search of any default compiler
libraries specified in the .OBJ file.
/NOE /NOEXTENDEDDICTSEARCH Ignore extended library dictionary
(if it is present). The extended
dictionary ordinarily provides the
linker with information about
inter-module dependencies, to speed
up linking.
/NOF /NOFARCALLTRANSLATION Disable optimization of far calls to
labels within the same segment.
/NOG /NOGROUPASSOCIATION Ignore group associations when
assigning addresses to data and code
items.
/NOI /NOIGNORECASE Do not ignore case in names during
linking.
/NON /NONULLSDOSSEG Arrange segments as for /DOSSEG but
do not insert 16 null bytes at start
of _TEXT segment.
/NOP /NOPACKCODE Do not pack contiguous logical code
segments into a single physical
segment.
/O:n /OVERLAYINTERRUPT:n Use interrupt number n with the
overlay manager supplied with some
Microsoft high-level languages.
/PAC[:n] /PACKCODE[:n] Pack contiguous logical code segments
into a single physical code segment.
The optional value n is the maximum
size for each packed physical code
segment (default = 65,536 bytes).
Segments in different groups are not
packed.
/PADC:n /PADCODE:n Add n filler bytes to end of each
code module so that a larger module
can be inserted later with ILINK.
Relevant to segmented executable
files (Windows and MS OS/2) only.
/PADD:n /PADDATA:n Add n filler bytes to end of each
data module so that a larger module
can be inserted later with ILINK.
Relevant to segmented executable
files (Microsoft Windows and MS OS/2)
only.
/PAU /PAUSE Pause during linking, allowing a
change of disks before .EXE file is
written.
/SE:n /SEGMENTS:n Set maximum number of segments in
linked program (default = 128).
/ST:n /STACK:n Set stack size of program in bytes;
ignore stack segment size
declarations within object modules
and definition file.
/W /WARNFIXUP Display warning messages for offsets
relative to a segment base that is
not the same as the group base.
Relevant to segmented executable
files (Microsoft Windows and MS OS/2)
only.
Figure 4-4. Switches accepted by the Microsoft Object Linker (LINK) version 5.0. Earlier versions use a subset of these switches. Note that any abbreviation for a switch is acceptable as long as it is sufficient to specify the switch uniquely.