Windows IT Pro
Windows IT Library
  - Advertise        
Windows IT Pro Logo

  Home  |   Books  |   Chapters  |   Topics  |   Authors  |   Book Reviews  |   Whitepapers  |   About Us  |   Contact Us

search for  on    power search   help
 






Portable Executable File Format
View the book table of contents
Author: Prasad Dabak
Milind Borate
Sandeep Phadke
Published: October 1999
Copyright: 1999
Publisher: M&T Books
 


RETURN VALUES
If the function succeeds, the return value is TRUE; otherwise, it is FALSE.

The other fields in the optional header are as follows:

SectionAlignmentA section needs to be loaded at an address that is a multiple of the section alignment. Refer to the discussion on RVA for more information.
FileAlignmentIn the file, a section always starts at an offset that is a multiple of the file alignment. This value is some multiple of the sector size.
MajorOperatingSystemVersion, MinorOperatingSystemVersionMinimum operating system version required to execute this file.
MajorImageVersion, MinorImageVersionA developer can use these fields to version his or her files. It can be specified with a linker flag.
MajorSubsystemVersion, MinorSubsystemVersionMinimum subsystem version required to execute this file.
Win32VersionValueReserved for future use.
SizeOfImageSize of the image after considering the section alignment. This amount of virtual memory needs to be reserved for loading the file.
SizeOfHeadersTotal size of the headers, including the DOS header, the PE header, and the section table. The sections containing the actual data start at this offset in the file.
CheckSumThis is used only for the kernel-mode drivers/DLLs. It can be set to 0 for user-mode executables/DLLs.
SubsystemSubsystem used by the file. The following values are defined in the WINNT.H file:
IMAGE_SUBSYSTEM_NATIVEImage doesn’t require a subsystem. The kernel-mode drivers and native applications such as CSRSS.EXE have this value for the field.
IMAGE_SUBSYSTEM_WINDOWS_GUIFile uses the Win32 GUI interface.
IMAGE_SUBSYSTEM_WINDOWS_CUIFile uses the character-based user interface.
IMAGE_SUBSYSTEM_OS2_CUIFile requires the OS/2 subsystem.
IMAGE_SUBSYSTEM_POSIX_CUIFile uses the POSIX API.
DllCharacteristicsObsolete.
SizeOfStackReserveAddress space to be reserved for the stack. Only the virtual address space is marked–the swap space is not allocated.
SizeOfStackCommitActual memory committed for the stack. This much swap space is initially allocated. The committed stack size is increased on demand until it reaches the SizeOfStackReserve.
SizeOfHeapReserveAddress space to be reserved for the heap. Similar to the SizeOfStackReserve field.
SizeOfHeapCommitActual committed heap space. Similar to the SizeOfStackCommit field.
LoaderFlagsObsolete.
NumberOfRvaAndSizesNumber of entries in the data directory that follows this field. It is always set to 16.
DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]As mentioned earlier, each entry in the data directory points to some important piece of information. Each of these entries is of the type IMAGE_DATA_DIRECTORY, which is defined as follows:
typedef struct _IMAGE_DATA_DIRECTORY {

DWORD	VirtualAddress;

DWORD	Size;

} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
ImageDirectoryEntryToData()
The VirtualAddress field contains the RVA of the respective piece of information, and the Size field contains the size of the data. To get to the actual data, you need to convert the RVA to the actual address in the memory-mapped PE file. This can be accomplished with the ImageDirectoryEntryToData() function exported by IMAGEHLP.DLL.
PVOID ImageDirectoryEntryToData(

	LPVOID Base,

	BOOLEAN MappedAsImage,

	USHORT DirectoryEntry,

	PULONG Size

);
PARAMETERS
BaseBase address where the file is mapped in memory.
MappedAsImageSet this flag to TRUE if the system loader maps the file. Otherwise, set the flag to FALSE.
DirectoryEntryIndex into the data directory array.
SizeUpon return, the size from the data directory is filled here.

RETURN VALUES
If the function succeeds, the return value is the address in the memory-mapped file where the required data resides. Otherwise, the function returns NULL.


INDICES IN THE DATA DIRECTORY

Each index in the data directory (except a few at the end that are still unused) represents some important piece of information. In the following sections, we discuss some of the important entries in this directory and the format in which the respective information is stored.

Export Directory
The data directory entry at the IMAGE_DIRECTORY_ENTRY_EXPORT index points to the export directory for the file. The RVA in this directory entry points to the .edata section. The information about the functions exported by the file (generally a DLL) is stored here. The data directory entry points to the export directory that is defined as the IMAGE_EXPORT_DIRECTORY structure in the WINNT.H file. The fields in this structure are as follows:

CharacteristicsReserved field. Always set to 0.
TimeDateStampDate and time of creation.
MajorVersion, MinorVersionDeveloper can set the version of the export table.
NameRVA of the zero-terminated name of the DLL.
BaseStarting ordinal for the exported functions–that is, the least of the ordinals. Generally, this field is 1.
NumberOfFunctionsTotal number of functions exported from the DLL.
NumberOfNamesNumber of functions that are exported by name. Some functions may be exported only by ordinal, so this number may be less than NumberOfFunctions.
AddressOfFunctionsRVA of an array (let’s call it as the export-functions array) that has an entry for each function exported from the DLL. Hence, the size of this array is equal to the NumberOfFunctions field. The entry at index i corresponds to the function exported with ordinal i + Base. Each entry in this array is also an RVA. If the RVA for a particular array entry points within the export section, then it is a forwarder. Forwarder means that the function is not present in this DLL, but it is a forwarder reference to some function in another DLL. In such a case, the RVA points to an ASCIIZ string that stores the name of the other DLL and the function name separated by a period. In case the target DLL exports the function by ordinal, the function name is formed as # followed by the ordinal printed in decimal. For example, the KERNEL32.DLL for Windows NT forwards the HeapAlloc() function to the RtlAllocateHeap() function in the NTDLL.DLL. Hence, the corresponding RVA in this case points to a location within the export section that holds the string NTDLL.RtlAllocateHeap. The Win32 applications can import the HeapAlloc() function from the KERNEL32.DLL without worrying about all these details. When the application runs on Windows 95, the loader resolves the import reference to the function in the KERNEL32.DLL. When the same application runs on Windows NT, the loader finds that the function is forwarded to the NTDLL.DLL. Hence, the loader automatically loads the NTDLL.DLL and resolves the imported function to the RtlAllocateHeap() function.

When an export-functions array entry is not a forwarder–that is, the RVA does not lie within the export section–the RVA points to the entry point of the function or to the location of the exported variable.

The export-functions array may have gaps. This is beacause some ordinals might be left unused while exporting functions, and some ordinals might not have any corresponding export. In such a case, the corresponding array entry is set to 0.

AddressOfNamesRVA of an array called as the export-names array that has an entry for every function that is exported by name. Hence, the size of this array is equal to the NumberOfNames field. Each entry in this array is an RVA pointing to an ASCIIZ string containing the export name. The array is sorted on the lexical order so as to allow binary search.
AddressOfNameOrdinalsRVA of an array of ordinals henceforth called as the export-ordinals array. This array has the size same as that of the AddressOfNames array. All three arrays, namely, export-names, export-ordinals, and export-functions, are instrumental in resolving imports by name. For resolving an import by name, the loader first searches the name in the export-names array. If the name matches an entry with index i, the ith entry in the export-ordinals array is the ordinal of the function. Finally, the address of the function can be found from the export-functions array.



Page: 1, 2, 3, 4, 5, 6

next page



ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

EXCHANGE 2007 Mastery Series – May 29, 2008
3 Info-packed eLearning seminars for only $99! Learn the pros and cons of your mailbox high availability options, see real-world examples of Transport Rules, and get started with basic PowerShell commands with Mark Arnold, MCSE+M and Microsoft MVP.

Windows IT Pro Master CD: Take the Experts with You!
Find the solutions you need in thousands of searchable articles, helpful bonus content, and loads of expert advice with the Windows IT Pro Master CD. Order comes with a 1-year subscription to the new, online articles posted every day!

SQL Server Magazine Master CD: Take the Experts with You!
Find the solutions you need in thousands of searchable articles, helpful bonus content, and loads of expert advice with the SQL Server Magazine Master CD. Order comes with a 1-year subscription to the new, online articles posted every day!

Attention User Group Leaders...
Announcing the eNews Generator—a FREE HTML e-newsletter builder for user group leaders. Build your HTML and text e-newsletters in minutes. And add Windows IT Pro & SQL Server Mag articles alongside your own message!.

Become a fan of Windows IT Pro on Facebook
Join the Windows IT Pro fan club on Facebook. Chat with other IT Pros, upload your pictures, check out what's up n' coming in the next issue and more!



Become a Response Point Specialist
Earn more with the small biz phone solution from Microsoft.

Get Started with Oracle on Windows DVD
Learn how Oracle gives you the power to grow by providing a scalable, easy-to-use platform for running your business at a price you can afford.

Agent-less Remote Backup Service, Free 30 Day Trial
Award winning remote backup service at a competitive price with no min GB/month. Sign up Now!
Windows IT Pro Home Register About Us Affiliates / Licensing Press Room Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing