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
22 #define IMAGE_ERROR_SUCCESS 0
23 #define IMAGE_ERROR_IMAGE_READ 1
24 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
25 #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
26 #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
27 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
28 #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
29 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
30 #define IMAGE_ERROR_SECTION_NOT_LOADED 8
31 #define IMAGE_ERROR_FAILED_RELOCATION 9
32 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
35 // PE/COFF Loader Read Function passed in by caller
39 (EFIAPI
*PE_COFF_LOADER_READ_FILE
) (
42 IN OUT UINTN
*ReadSize
,
47 /// Context structure used while PE/COFF image is being loaded and relocated
51 /// Is set by PeCoffLoaderGetImageInfo() to the ImageBase in the PE/COFF header
53 PHYSICAL_ADDRESS ImageAddress
;
55 /// Is set by PeCoffLoaderGetImageInfo() to the SizeOfImage in the PE/COFF header.
56 /// Image size includes the size of Debug Entry if it is present.
60 /// Is set to zero by PeCoffLoaderGetImageInfo(). If DestinationAddress is non zero,
61 /// PeCoffLoaderRelocateImage() will relocate the image using this base address.
62 /// If the DestinationAddress is zero, the ImageAddress will be used as the base
63 /// address of relocation.
65 PHYSICAL_ADDRESS DestinationAddress
;
67 /// PeCoffLoaderLoadImage() sets EntryPoint to to the entry point of the PE/COFF image.
69 PHYSICAL_ADDRESS EntryPoint
;
71 /// Passed in by the caller to PeCoffLoaderGetImageInfo() and PeCoffLoaderLoadImage()
72 /// to abstract accessing the image from the library.
74 PE_COFF_LOADER_READ_FILE ImageRead
;
76 /// Used as the FileHandle passed into the ImageRead function when it's called.
80 /// Caller allocated buffer of size FixupDataSize that can be optionally allocated
81 /// prior to calling PeCoffLoaderRelocateImage().
82 /// This buffer is filled with the information used to fix up the image.
83 /// The fixups have been applied to the image and this entry is just for information.
87 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header
88 /// If the image is a TE image, then this field is set to 0.
90 UINT32 SectionAlignment
;
92 /// Set by PeCoffLoaderGetImageInfo() to offset to the PE/COFF header.
93 /// If the PE/COFF image does not start with a DOS header, this value is zero;
94 /// otherwise, it's the offset to the PE/COFF header.
96 UINT32 PeCoffHeaderOffset
;
98 /// Set by PeCoffLoaderGetImageInfo() to the Relative Virtual Address of the debug directory
99 /// if it exists in the image
101 UINT32 DebugDirectoryEntryRva
;
103 /// Set by PeCoffLoaderLoadImage() to CodeView area of the PE/COFF Debug directory.
107 /// Set by PeCoffLoaderLoadImage() to point to the PDB entry contained in the CodeView area.
108 /// The PdbPointer points to the filename of the PDB file used for source-level debug of
109 /// the image by a debugger.
113 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header.
117 /// Not used by this library class. Other library classes that layer on top of this library
118 /// class fill in this value as part of their GetImageInfo call.
119 /// This allows the caller of the library to know what type of memory needs to be allocated
120 /// to load and relocate the image.
122 UINT32 ImageCodeMemoryType
;
124 /// Not used by this library class. Other library classes that layer on top of this library
125 /// class fill in this value as part of their GetImageInfo call.
126 /// This allows the caller of the library to know what type of memory needs to be allocated
127 /// to load and relocate the image
129 UINT32 ImageDataMemoryType
;
131 /// Set by any of the library functions if they encounter an error.
135 /// Set by PeCoffLoaderLoadImage() to indicate the size of FixupData that the caller must
136 /// allocate before calling PeCoffLoaderRelocateImage()
140 /// Set by PeCoffLoaderGetImageInfo() to the machine type stored in the PE/COFF header
144 /// Set by PeCoffLoaderGetImageInfo() to the subsystem type stored in the PE/COFF header.
148 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain
149 /// relocation information.
151 BOOLEAN RelocationsStripped
;
153 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the image is a TE image.
154 /// For a definition of the TE Image format, see the Platform Initialization Pre-EFI
155 /// Initialization Core Interface Specification.
158 } PE_COFF_LOADER_IMAGE_CONTEXT
;
161 Retrieves information about a PE/COFF image.
163 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
164 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
165 DebugDirectoryEntryRva fields of the ImageContext structure.
166 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
167 If the PE/COFF image accessed through the ImageRead service in the ImageContext
168 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
169 If any errors occur while computing the fields of ImageContext,
170 then the error status is returned in the ImageError field of ImageContext.
171 If the image is a TE image, then SectionAlignment is set to 0.
173 @param ImageContext Pointer to the image context structure that describes the PE/COFF
174 image that needs to be examined by this function.
176 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
177 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
178 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
183 PeCoffLoaderGetImageInfo (
184 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
188 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
190 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
191 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
192 of ImageContext as the relocation base address. The caller must allocate the relocation
193 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
194 If ImageContext is NULL, then ASSERT().
196 @param ImageContext Pointer to the image context structure that describes the PE/COFF
197 image that is being relocated.
199 @retval RETURN_SUCCESS The PE/COFF image was relocated.
200 Extended status information is in the ImageError field of ImageContext.
201 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
202 Extended status information is in the ImageError field of ImageContext.
203 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
204 Extended status information is in the ImageError field of ImageContext.
209 PeCoffLoaderRelocateImage (
210 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
214 Loads a PE/COFF image into memory.
216 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
217 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
218 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
219 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
220 If ImageContext is NULL, then ASSERT().
222 @param ImageContext Pointer to the image context structure that describes the PE/COFF
223 image that is being loaded.
225 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
226 the ImageAddress and ImageSize fields of ImageContext.
227 Extended status information is in the ImageError field of ImageContext.
228 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
229 Extended status information is in the ImageError field of ImageContext.
230 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
231 Extended status information is in the ImageError field of ImageContext.
232 @retval RETURN_INVALID_PARAMETER The image address is invalid.
233 Extended status information is in the ImageError field of ImageContext.
238 PeCoffLoaderLoadImage (
239 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
244 ImageRead function that operates on a memory buffer whos base is passed into
247 @param FileHandle Ponter to baes of the input stream
248 @param FileOffset Offset to the start of the buffer
249 @param ReadSize Number of bytes to copy into the buffer
250 @param Buffer Location to place results of read
252 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
257 PeCoffLoaderImageReadFromMemory (
260 IN OUT UINTN
*ReadSize
,
266 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
269 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
270 the fixups with a virtual mapping.
273 @param ImageBase Base address of relocated image
274 @param VirtImageBase Virtual mapping for ImageBase
275 @param ImageSize Size of the image to relocate
276 @param RelocationData Location to place results of read
281 PeCoffLoaderRelocateImageForRuntime (
282 IN PHYSICAL_ADDRESS ImageBase
,
283 IN PHYSICAL_ADDRESS VirtImageBase
,
285 IN VOID
*RelocationData
289 Unloads a loaded PE/COFF image from memory and releases its taken resource.
291 For NT32 emulator, the PE/COFF image loaded by system needs to release.
292 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
293 this function can simply return RETURN_SUCCESS.
295 @param ImageContext Pointer to the image context structure that describes the PE/COFF
296 image to be unloaded.
298 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
302 PeCoffLoaderUnloadImage (
303 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext