_ _ | | _ _ .Exe File Structure and Loading _ _______________________________________ _ ________________________________________________________________ Note This chapter describes .exe file structure and loading procedures for systems that use a version of MS-DOS earlier than 2.0. For MS-DOS versions 2.0 and later, use Function 4B00H (Load and Execute a Pro- gram) to load (or load and execute) an .exe file. _ ________________________________________________________________ The .exe files produced by link consist of two parts: o Control and relocation information o The load module The control and relocation information is at the beginning of the file in an area called the header. Immediately following this header is the load module. 6.1 Format of a File Header The header is formatted as follows (note that offsets are in hexadecimal): Offset Contents _ ________________________________________________________________ 0-1 Must contain 4DH, 5AH. 2-3 Number of bytes contained in last page; useful for reading overlays. 4-5 Size of the file in 512-byte pages, including the header. 6-7 Number of relocation entries in table. 8-9 Size of the header in 16-byte paragraphs. Used to locate the beginning of the load module in the file. AH-BH Minimum number of 16-byte paragraphs required above the end of the loaded program. CH-DH Maximum number of 16-byte paragraphs required above the end of the loaded program. If both minalloc and maxalloc are 0, the program is loaded as high as possible. EH-FH Initial value to be loaded into stack segment before starting program execution. Must be adjusted by relocation. 3 _ _ | | _ _ _ _ | | _ _ _ ______________ 10-11 Value to be loaded into the SP register before starting pro- gram execution. 12-13 Negative sum of all the words in the file. 14-15 Initial value to be loaded into the IP register before starting program execution. 16-17 Initial value to be loaded into the CS register before starting program execution. Must be adjusted by relocation. 18-19 Relative byte offset from beginning of run file to relocation table. 1AH-1BH The number of the overlay as generated by link. 6.2 The Relocation Table The relocation table that follows the formatted area above, consists of a variable number of relocation items. Each relocation item contains two fields: a two-byte offset value, followed by a two-byte segment value. These two fields contain the offset into a word's load module. This item requires modification before the module is given control. The following steps describe this process: 1. The formatted part of the header is read into memory. Its size is 1BH. 2. MS-DOS allocates a portion of memory depending on the size of the load module and the allocation numbers (AH-BH and CH-DH). MS-DOS then attempts to allocate 0FFFH paragraphs. This attempt always fails, and returns the size of the largest free block. If this block is smaller than minalloc and loadsize, there is no memory error. But if this block is larger than maxalloc and loadsize, MS-DOS allocates (maxalloc + loadsize). Otherwise, it allocates the largest free block of memory. 3. A Program Segment Prefix is built in the lowest part of the allo- cated memory. 4. MS-DOS calculates the load module size (using offsets 4-5 and 8-9) by subtracting the header size from the file size. The actual size is adjusted down based on the contents of offsets 2-3. The operating system determines (based on the setting of the high/low load switch) an appropriate segment, called the start segment, where it loads the load module. 5. The load module is read into memory beginning with the start seg- ment. 4 _ _ | | _ _ _ _ | | _ _ .Exe File Structure and Loading _ _______________________________________ 6. The items in the relocation table are read into a work area. 7. MS-DOS adds the segment value of each relocation table item to the start segment value. This calculated segment, plus value, points to the module to which the start segment value is added. The result is then placed back into the word in the load module. 8. Once all relocation items have been processed, the operating sys- tem sets the SS and SP registers, using the values in the header. MS-DOS then adds the start segment value to SS and sets the ES and DS registers to the segment address of the Program Segment Prefix. The start segment value is then added to the header CS register value. The result, along with the header IP value, is the initial CS:IP to transfer to before starting execution of the pro- gram. 5 _ _ | | _ _ _ _ | | _ _ _ ______________ Chapter 6 .Exe File Structure and Loading _ ________________________________________________________________ 6.1 Format of a File Header 3 6.2 The Relocation Table 4 1 _ _ | | _ _ _ _ | | _ _ _ ______________ 5 _ _ | | _ _