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
89 UINT32 SectionAlignment
;
91 /// Set by PeCoffLoaderGetImageInfo() to offset to the PE/COFF header.
92 /// If the PE/COFF image does not start with a DOS header, this value is zero;
93 /// otherwise, it's the offset to the PE/COFF header.
95 UINT32 PeCoffHeaderOffset
;
97 /// Set by PeCoffLoaderGetImageInfo() to the Relative Virtual Address of the debug directory
98 /// if it exists in the image
100 UINT32 DebugDirectoryEntryRva
;
102 /// Set by PeCoffLoaderLoadImage() to CodeView area of the PE/COFF Debug directory.
106 /// Set by PeCoffLoaderLoadImage() to point to the PDB entry contained in the CodeView area.
107 /// The PdbPointer points to the filename of the PDB file used for source-level debug of
108 /// the image by a debugger.
112 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header.
116 /// Not used by this library class. Other library classes that layer on top of this library
117 /// class fill in this value as part of their GetImageInfo call.
118 /// This allows the caller of the library to know what type of memory needs to be allocated
119 /// to load and relocate the image.
121 UINT32 ImageCodeMemoryType
;
123 /// Not used by this library class. Other library classes that layer on top of this library
124 /// class fill in this value as part of their GetImageInfo call.
125 /// This allows the caller of the library to know what type of memory needs to be allocated
126 /// to load and relocate the image
128 UINT32 ImageDataMemoryType
;
130 /// Set by any of the library functions if they encounter an error.
134 /// Set by PeCoffLoaderLoadImage() to indicate the size of FixupData that the caller must
135 /// allocate before calling PeCoffLoaderRelocateImage()
139 /// Set by PeCoffLoaderGetImageInfo() to the machine type stored in the PE/COFF header
143 /// Set by PeCoffLoaderGetImageInfo() to the subsystem type stored in the PE/COFF header.
147 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain
148 /// relocation information.
150 BOOLEAN RelocationsStripped
;
152 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the image is a TE image.
153 /// For a definition of the TE Image format, see the Platform Initialization Pre-EFI
154 /// Initialization Core Interface Specification.
157 } PE_COFF_LOADER_IMAGE_CONTEXT
;
160 Retrieves information about a PE/COFF image.
162 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
163 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
164 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
165 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
166 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
167 computing the fields of ImageContext, then the error status is returned in the ImageError field of
170 @param ImageContext Pointer to the image context structure that describes the PE/COFF
171 image that needs to be examined by this function.
173 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
174 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
175 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
180 PeCoffLoaderGetImageInfo (
181 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
185 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
187 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
188 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
189 of ImageContext as the relocation base address. The caller must allocate the relocation
190 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
191 If ImageContext is NULL, then ASSERT().
193 @param ImageContext Pointer to the image context structure that describes the PE/COFF
194 image that is being relocated.
196 @retval RETURN_SUCCESS The PE/COFF image was relocated.
197 Extended status information is in the ImageError field of ImageContext.
198 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
199 Extended status information is in the ImageError field of ImageContext.
200 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
201 Extended status information is in the ImageError field of ImageContext.
206 PeCoffLoaderRelocateImage (
207 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
211 Loads a PE/COFF image into memory.
213 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
214 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
215 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
216 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
217 If ImageContext is NULL, then ASSERT().
219 @param ImageContext Pointer to the image context structure that describes the PE/COFF
220 image that is being loaded.
222 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
223 the ImageAddress and ImageSize fields of ImageContext.
224 Extended status information is in the ImageError field of ImageContext.
225 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
226 Extended status information is in the ImageError field of ImageContext.
227 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
228 Extended status information is in the ImageError field of ImageContext.
229 @retval RETURN_INVALID_PARAMETER The image address is invalid.
230 Extended status information is in the ImageError field of ImageContext.
235 PeCoffLoaderLoadImage (
236 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
241 ImageRead function that operates on a memory buffer whos base is passed into
244 @param FileHandle Ponter to baes of the input stream
245 @param FileOffset Offset to the start of the buffer
246 @param ReadSize Number of bytes to copy into the buffer
247 @param Buffer Location to place results of read
249 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
254 PeCoffLoaderImageReadFromMemory (
257 IN OUT UINTN
*ReadSize
,
263 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
266 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
267 the fixups with a virtual mapping.
270 @param ImageBase Base address of relocated image
271 @param VirtImageBase Virtual mapping for ImageBase
272 @param ImageSize Size of the image to relocate
273 @param RelocationData Location to place results of read
278 PeCoffLoaderRelocateImageForRuntime (
279 IN PHYSICAL_ADDRESS ImageBase
,
280 IN PHYSICAL_ADDRESS VirtImageBase
,
282 IN VOID
*RelocationData
286 Unloads a loaded PE/COFF image from memory and releases its taken resource.
288 For NT32 emulator, the PE/COFF image loaded by system needs to release.
289 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
290 this function can simply return RETURN_SUCCESS.
292 @param ImageContext Pointer to the image context structure that describes the PE/COFF
293 image to be unloaded.
295 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
299 PeCoffLoaderUnloadImage (
300 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext