]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Include/Library/PeCoffLib.h
Code Scrub
[mirror_edk2.git] / MdePkg / Include / Library / PeCoffLib.h
CommitLineData
fb3df220 1/** @file\r
2 Memory Only PE COFF loader. \r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation \r
5 All rights reserved. This program and the accompanying materials \r
6 are licensed and made available under the terms and conditions of the BSD License \r
7 which accompanies this distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
fb3df220 13**/\r
14\r
15#ifndef __BASE_PE_COFF_LIB_H__\r
16#define __BASE_PE_COFF_LIB_H__\r
17\r
d8dcb666 18#include <IndustryStandard/PeImage.h>\r
fb3df220 19//\r
20// Return status codes from the PE/COFF Loader services\r
fb3df220 21//\r
22#define IMAGE_ERROR_SUCCESS 0\r
23#define IMAGE_ERROR_IMAGE_READ 1 \r
24#define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2\r
25#define IMAGE_ERROR_INVALID_MACHINE_TYPE 3\r
26#define IMAGE_ERROR_INVALID_SUBSYSTEM 4\r
27#define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5\r
28#define IMAGE_ERROR_INVALID_IMAGE_SIZE 6\r
29#define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7\r
30#define IMAGE_ERROR_SECTION_NOT_LOADED 8\r
31#define IMAGE_ERROR_FAILED_RELOCATION 9\r
32#define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10\r
33\r
146332ae 34//\r
35// PE/COFF Loader Read Function passed in by caller\r
36//\r
37typedef\r
38RETURN_STATUS\r
39(EFIAPI *PE_COFF_LOADER_READ_FILE) (\r
40 IN VOID *FileHandle,\r
41 IN UINTN FileOffset,\r
42 IN OUT UINTN *ReadSize,\r
43 OUT VOID *Buffer\r
44 );\r
45\r
f6d7003d 46///\r
47/// Context structure used while PE/COFF image is being loaded and relocated\r
48///\r
146332ae 49typedef struct {\r
f6d7003d 50 ///\r
51 /// Is set by PeCoffLoaderGetImageInfo() to the ImageBase in the PE/COFF header\r
52 ///\r
146332ae 53 PHYSICAL_ADDRESS ImageAddress;\r
f6d7003d 54 ///\r
55 /// Is set by PeCoffLoaderGetImageInfo() to the SizeOfImage in the PE/COFF header.\r
56 /// Image size includes the size of Debug Entry if it is present.\r
57 ///\r
146332ae 58 UINT64 ImageSize;\r
f6d7003d 59 ///\r
60 /// Is set to zero by PeCoffLoaderGetImageInfo(). If DestinationAddress is non zero,\r
61 /// PeCoffLoaderRelocateImage() will relocate the image using this base address.\r
62 /// If the DestinationAddress is zero, the ImageAddress will be used as the base\r
63 /// address of relocation.\r
64 ///\r
146332ae 65 PHYSICAL_ADDRESS DestinationAddress;\r
f6d7003d 66 ///\r
67 /// PeCoffLoaderLoadImage() sets EntryPoint to to the entry point of the PE/COFF image.\r
68 ///\r
146332ae 69 PHYSICAL_ADDRESS EntryPoint;\r
f6d7003d 70 ///\r
71 /// Passed in by the caller to PeCoffLoaderGetImageInfo() and PeCoffLoaderLoadImage()\r
72 /// to abstract accessing the image from the library.\r
73 ///\r
146332ae 74 PE_COFF_LOADER_READ_FILE ImageRead;\r
f6d7003d 75 ///\r
76 /// Used as the FileHandle passed into the ImageRead function when it's called.\r
77 ///\r
146332ae 78 VOID *Handle;\r
f6d7003d 79 ///\r
80 /// Caller allocated buffer of size FixupDataSize that can be optionally allocated\r
81 /// prior to calling PeCoffLoaderRelocateImage(). \r
82 /// This buffer is filled with the information used to fix up the image. \r
83 /// The fixups have been applied to the image and this entry is just for information.\r
84 ///\r
146332ae 85 VOID *FixupData;\r
f6d7003d 86 ///\r
87 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header\r
3ecdcd11 88 /// If the image is a TE image, then this field is set to 0.\r
f6d7003d 89 ///\r
146332ae 90 UINT32 SectionAlignment;\r
f6d7003d 91 ///\r
92 /// Set by PeCoffLoaderGetImageInfo() to offset to the PE/COFF header.\r
93 /// If the PE/COFF image does not start with a DOS header, this value is zero; \r
94 /// otherwise, it's the offset to the PE/COFF header.\r
95 ///\r
146332ae 96 UINT32 PeCoffHeaderOffset;\r
f6d7003d 97 ///\r
98 /// Set by PeCoffLoaderGetImageInfo() to the Relative Virtual Address of the debug directory\r
99 /// if it exists in the image\r
100 ///\r
146332ae 101 UINT32 DebugDirectoryEntryRva;\r
f6d7003d 102 ///\r
103 /// Set by PeCoffLoaderLoadImage() to CodeView area of the PE/COFF Debug directory.\r
104 ///\r
146332ae 105 VOID *CodeView;\r
f6d7003d 106 ///\r
107 /// Set by PeCoffLoaderLoadImage() to point to the PDB entry contained in the CodeView area.\r
108 /// The PdbPointer points to the filename of the PDB file used for source-level debug of \r
109 /// the image by a debugger.\r
110 ///\r
146332ae 111 CHAR8 *PdbPointer;\r
f6d7003d 112 ///\r
113 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header.\r
114 ///\r
146332ae 115 UINTN SizeOfHeaders;\r
f6d7003d 116 ///\r
117 /// Not used by this library class. Other library classes that layer on top of this library\r
118 /// class fill in this value as part of their GetImageInfo call. \r
119 /// This allows the caller of the library to know what type of memory needs to be allocated\r
120 /// to load and relocate the image.\r
121 ///\r
146332ae 122 UINT32 ImageCodeMemoryType;\r
f6d7003d 123 ///\r
124 /// Not used by this library class. Other library classes that layer on top of this library \r
125 /// class fill in this value as part of their GetImageInfo call.\r
126 /// This allows the caller of the library to know what type of memory needs to be allocated\r
127 /// to load and relocate the image\r
128 ///\r
146332ae 129 UINT32 ImageDataMemoryType;\r
f6d7003d 130 ///\r
131 /// Set by any of the library functions if they encounter an error. \r
132 ///\r
146332ae 133 UINT32 ImageError;\r
f6d7003d 134 ///\r
135 /// Set by PeCoffLoaderLoadImage() to indicate the size of FixupData that the caller must\r
136 /// allocate before calling PeCoffLoaderRelocateImage()\r
137 ///\r
146332ae 138 UINTN FixupDataSize;\r
f6d7003d 139 ///\r
140 /// Set by PeCoffLoaderGetImageInfo() to the machine type stored in the PE/COFF header\r
141 ///\r
146332ae 142 UINT16 Machine;\r
f6d7003d 143 ///\r
144 /// Set by PeCoffLoaderGetImageInfo() to the subsystem type stored in the PE/COFF header.\r
145 ///\r
146332ae 146 UINT16 ImageType;\r
f6d7003d 147 ///\r
148 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain\r
149 /// relocation information.\r
150 ///\r
146332ae 151 BOOLEAN RelocationsStripped;\r
f6d7003d 152 ///\r
153 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the image is a TE image. \r
154 /// For a definition of the TE Image format, see the Platform Initialization Pre-EFI\r
155 /// Initialization Core Interface Specification.\r
156 ///\r
146332ae 157 BOOLEAN IsTeImage;\r
158} PE_COFF_LOADER_IMAGE_CONTEXT;\r
fb3df220 159\r
160/**\r
161 Retrieves information about a PE/COFF image.\r
162\r
3ecdcd11
LG
163 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, \r
164 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and \r
165 DebugDirectoryEntryRva fields of the ImageContext structure. \r
166 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. \r
167 If the PE/COFF image accessed through the ImageRead service in the ImageContext \r
168 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. \r
169 If any errors occur while computing the fields of ImageContext, \r
170 then the error status is returned in the ImageError field of ImageContext. \r
171 If the image is a TE image, then SectionAlignment is set to 0.\r
fb3df220 172\r
173 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
174 image that needs to be examined by this function.\r
175\r
176 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r
177 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r
178 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r
179\r
180**/\r
181RETURN_STATUS\r
182EFIAPI\r
183PeCoffLoaderGetImageInfo (\r
184 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
ed66e1bc 185 );\r
fb3df220 186\r
187/**\r
188 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().\r
189\r
190 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r
191 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field\r
192 of ImageContext as the relocation base address. The caller must allocate the relocation\r
193 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function. \r
194 If ImageContext is NULL, then ASSERT().\r
195\r
196 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
197 image that is being relocated.\r
198\r
199 @retval RETURN_SUCCESS The PE/COFF image was relocated.\r
200 Extended status information is in the ImageError field of ImageContext.\r
201 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.\r
202 Extended status information is in the ImageError field of ImageContext.\r
203 @retval RETURN_UNSUPPORTED A relocation record type is not supported.\r
204 Extended status information is in the ImageError field of ImageContext.\r
205\r
206**/\r
207RETURN_STATUS\r
208EFIAPI\r
209PeCoffLoaderRelocateImage (\r
210 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
ed66e1bc 211 );\r
fb3df220 212\r
213/**\r
214 Loads a PE/COFF image into memory.\r
215\r
216 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer\r
217 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate\r
218 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r
219 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.\r
220 If ImageContext is NULL, then ASSERT().\r
221\r
222 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
223 image that is being loaded.\r
224\r
225 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by\r
226 the ImageAddress and ImageSize fields of ImageContext.\r
227 Extended status information is in the ImageError field of ImageContext.\r
228 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.\r
229 Extended status information is in the ImageError field of ImageContext.\r
230 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.\r
231 Extended status information is in the ImageError field of ImageContext.\r
232 @retval RETURN_INVALID_PARAMETER The image address is invalid.\r
233 Extended status information is in the ImageError field of ImageContext.\r
234\r
235**/\r
236RETURN_STATUS\r
237EFIAPI\r
238PeCoffLoaderLoadImage (\r
239 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
ed66e1bc 240 );\r
fb3df220 241\r
242\r
243/**\r
244 ImageRead function that operates on a memory buffer whos base is passed into\r
245 FileHandle. \r
246\r
247 @param FileHandle Ponter to baes of the input stream\r
248 @param FileOffset Offset to the start of the buffer\r
249 @param ReadSize Number of bytes to copy into the buffer\r
250 @param Buffer Location to place results of read\r
251\r
252 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into \r
253 the buffer.\r
254**/\r
255RETURN_STATUS\r
256EFIAPI\r
257PeCoffLoaderImageReadFromMemory (\r
258 IN VOID *FileHandle,\r
259 IN UINTN FileOffset,\r
260 IN OUT UINTN *ReadSize,\r
261 OUT VOID *Buffer\r
ed66e1bc 262 );\r
fb3df220 263\r
264\r
265/**\r
266 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
267 runtime. \r
268 \r
269 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply\r
270 the fixups with a virtual mapping.\r
271\r
272\r
273 @param ImageBase Base address of relocated image\r
274 @param VirtImageBase Virtual mapping for ImageBase\r
275 @param ImageSize Size of the image to relocate\r
276 @param RelocationData Location to place results of read\r
277 \r
278**/\r
279VOID\r
280EFIAPI\r
281PeCoffLoaderRelocateImageForRuntime (\r
282 IN PHYSICAL_ADDRESS ImageBase,\r
283 IN PHYSICAL_ADDRESS VirtImageBase,\r
284 IN UINTN ImageSize,\r
285 IN VOID *RelocationData\r
ed66e1bc 286 );\r
fb3df220 287\r
3d7b0992
LG
288/**\r
289 Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
290 \r
291 For NT32 emulator, the PE/COFF image loaded by system needs to release.\r
292 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r
293 this function can simply return RETURN_SUCCESS.\r
294\r
295 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
296 image to be unloaded.\r
fb3df220 297\r
3d7b0992
LG
298 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r
299**/\r
300RETURN_STATUS\r
301EFIAPI\r
302PeCoffLoaderUnloadImage (\r
303 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
ed66e1bc 304 );\r
fb3df220 305#endif\r