2 Memory Only PE COFF loader.
4 Copyright (c) 2006 - 2007, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #ifndef __BASE_PE_COFF_LIB_H__
16 #define __BASE_PE_COFF_LIB_H__
18 #include <IndustryStandard/PeImage.h>
20 // Return status codes from the PE/COFF Loader services
21 // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes
23 #define IMAGE_ERROR_SUCCESS 0
24 #define IMAGE_ERROR_IMAGE_READ 1
25 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
26 #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
27 #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
28 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
29 #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
30 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
31 #define IMAGE_ERROR_SECTION_NOT_LOADED 8
32 #define IMAGE_ERROR_FAILED_RELOCATION 9
33 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
36 // PE/COFF Loader Read Function passed in by caller
40 (EFIAPI
*PE_COFF_LOADER_READ_FILE
) (
43 IN OUT UINTN
*ReadSize
,
48 // Context structure used while PE/COFF image is being loaded and relocated
51 PHYSICAL_ADDRESS ImageAddress
;
53 PHYSICAL_ADDRESS DestinationAddress
;
54 PHYSICAL_ADDRESS EntryPoint
;
55 PE_COFF_LOADER_READ_FILE ImageRead
;
58 UINT32 SectionAlignment
;
59 UINT32 PeCoffHeaderOffset
;
60 UINT32 DebugDirectoryEntryRva
;
64 UINT32 ImageCodeMemoryType
;
65 UINT32 ImageDataMemoryType
;
70 BOOLEAN RelocationsStripped
;
72 } PE_COFF_LOADER_IMAGE_CONTEXT
;
75 Retrieves information about a PE/COFF image.
77 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
78 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
79 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
80 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
81 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
82 computing the fields of ImageContext, then the error status is returned in the ImageError field of
85 @param ImageContext Pointer to the image context structure that describes the PE/COFF
86 image that needs to be examined by this function.
88 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
89 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
90 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
95 PeCoffLoaderGetImageInfo (
96 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
101 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
103 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
104 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
105 of ImageContext as the relocation base address. The caller must allocate the relocation
106 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
107 If ImageContext is NULL, then ASSERT().
109 @param ImageContext Pointer to the image context structure that describes the PE/COFF
110 image that is being relocated.
112 @retval RETURN_SUCCESS The PE/COFF image was relocated.
113 Extended status information is in the ImageError field of ImageContext.
114 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
115 Extended status information is in the ImageError field of ImageContext.
116 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
117 Extended status information is in the ImageError field of ImageContext.
122 PeCoffLoaderRelocateImage (
123 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
128 Loads a PE/COFF image into memory.
130 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
131 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
132 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
133 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
134 If ImageContext is NULL, then ASSERT().
136 @param ImageContext Pointer to the image context structure that describes the PE/COFF
137 image that is being loaded.
139 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
140 the ImageAddress and ImageSize fields of ImageContext.
141 Extended status information is in the ImageError field of ImageContext.
142 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
143 Extended status information is in the ImageError field of ImageContext.
144 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
145 Extended status information is in the ImageError field of ImageContext.
146 @retval RETURN_INVALID_PARAMETER The image address is invalid.
147 Extended status information is in the ImageError field of ImageContext.
152 PeCoffLoaderLoadImage (
153 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
159 ImageRead function that operates on a memory buffer whos base is passed into
162 @param FileHandle Ponter to baes of the input stream
163 @param FileOffset Offset to the start of the buffer
164 @param ReadSize Number of bytes to copy into the buffer
165 @param Buffer Location to place results of read
167 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
172 PeCoffLoaderImageReadFromMemory (
175 IN OUT UINTN
*ReadSize
,
182 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
185 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
186 the fixups with a virtual mapping.
189 @param ImageBase Base address of relocated image
190 @param VirtImageBase Virtual mapping for ImageBase
191 @param ImageSize Size of the image to relocate
192 @param RelocationData Location to place results of read
197 PeCoffLoaderRelocateImageForRuntime (
198 IN PHYSICAL_ADDRESS ImageBase
,
199 IN PHYSICAL_ADDRESS VirtImageBase
,
201 IN VOID
*RelocationData
206 Unloads a loaded PE/COFF image from memory and releases its taken resource.
208 For NT32 emulator, the PE/COFF image loaded by system needs to release.
209 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
210 this function can simply return RETURN_SUCCESS.
212 @param ImageContext Pointer to the image context structure that describes the PE/COFF
213 image to be unloaded.
215 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
219 PeCoffLoaderUnloadImage (
220 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext