]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/PeCoffLib.h
Synchronization of MDE Library Spec., Mde.dec, and corresponding head files in MdePkg...
[mirror_edk2.git] / MdePkg / Include / Library / PeCoffLib.h
1 /** @file
2 Provides services to load and relocate a PE/COFF image.
3
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
9
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.
12
13 **/
14
15 #ifndef __BASE_PE_COFF_LIB_H__
16 #define __BASE_PE_COFF_LIB_H__
17
18 #include <IndustryStandard/PeImage.h>
19 //
20 // Return status codes from the PE/COFF Loader services
21 //
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
33
34 //
35 // PE/COFF Loader Read Function passed in by caller
36 //
37 typedef
38 RETURN_STATUS
39 (EFIAPI *PE_COFF_LOADER_READ_FILE) (
40 IN VOID *FileHandle,
41 IN UINTN FileOffset,
42 IN OUT UINTN *ReadSize,
43 OUT VOID *Buffer
44 );
45
46 ///
47 /// Context structure used while PE/COFF image is being loaded and relocated
48 ///
49 typedef struct {
50 ///
51 /// Is set by PeCoffLoaderGetImageInfo() to the ImageBase in the PE/COFF header
52 ///
53 PHYSICAL_ADDRESS ImageAddress;
54 ///
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.
57 ///
58 UINT64 ImageSize;
59 ///
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.
64 ///
65 PHYSICAL_ADDRESS DestinationAddress;
66 ///
67 /// PeCoffLoaderLoadImage() sets EntryPoint to to the entry point of the PE/COFF image.
68 ///
69 PHYSICAL_ADDRESS EntryPoint;
70 ///
71 /// Passed in by the caller to PeCoffLoaderGetImageInfo() and PeCoffLoaderLoadImage()
72 /// to abstract accessing the image from the library.
73 ///
74 PE_COFF_LOADER_READ_FILE ImageRead;
75 ///
76 /// Used as the FileHandle passed into the ImageRead function when it's called.
77 ///
78 VOID *Handle;
79 ///
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.
84 ///
85 VOID *FixupData;
86 ///
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.
89 ///
90 UINT32 SectionAlignment;
91 ///
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.
95 ///
96 UINT32 PeCoffHeaderOffset;
97 ///
98 /// Set by PeCoffLoaderGetImageInfo() to the Relative Virtual Address of the debug directory
99 /// if it exists in the image
100 ///
101 UINT32 DebugDirectoryEntryRva;
102 ///
103 /// Set by PeCoffLoaderLoadImage() to CodeView area of the PE/COFF Debug directory.
104 ///
105 VOID *CodeView;
106 ///
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.
110 ///
111 CHAR8 *PdbPointer;
112 ///
113 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header.
114 ///
115 UINTN SizeOfHeaders;
116 ///
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.
121 ///
122 UINT32 ImageCodeMemoryType;
123 ///
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
128 ///
129 UINT32 ImageDataMemoryType;
130 ///
131 /// Set by any of the library functions if they encounter an error.
132 ///
133 UINT32 ImageError;
134 ///
135 /// Set by PeCoffLoaderLoadImage() to indicate the size of FixupData that the caller must
136 /// allocate before calling PeCoffLoaderRelocateImage()
137 ///
138 UINTN FixupDataSize;
139 ///
140 /// Set by PeCoffLoaderGetImageInfo() to the machine type stored in the PE/COFF header
141 ///
142 UINT16 Machine;
143 ///
144 /// Set by PeCoffLoaderGetImageInfo() to the subsystem type stored in the PE/COFF header.
145 ///
146 UINT16 ImageType;
147 ///
148 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain
149 /// relocation information.
150 ///
151 BOOLEAN RelocationsStripped;
152 ///
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.
156 ///
157 BOOLEAN IsTeImage;
158 } PE_COFF_LOADER_IMAGE_CONTEXT;
159
160 /**
161 Retrieves information about a PE/COFF image.
162
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.
172
173 @param ImageContext Pointer to the image context structure that describes the PE/COFF
174 image that needs to be examined by this function.
175
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.
179
180 **/
181 RETURN_STATUS
182 EFIAPI
183 PeCoffLoaderGetImageInfo (
184 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
185 );
186
187 /**
188 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
189
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().
195
196 @param ImageContext Pointer to the image context structure that describes the PE/COFF
197 image that is being relocated.
198
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.
205
206 **/
207 RETURN_STATUS
208 EFIAPI
209 PeCoffLoaderRelocateImage (
210 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
211 );
212
213 /**
214 Loads a PE/COFF image into memory.
215
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().
221
222 @param ImageContext Pointer to the image context structure that describes the PE/COFF
223 image that is being loaded.
224
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.
234
235 **/
236 RETURN_STATUS
237 EFIAPI
238 PeCoffLoaderLoadImage (
239 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
240 );
241
242
243 /**
244 ImageRead function that operates on a memory buffer whos base is passed into
245 FileHandle.
246
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
251
252 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
253 the buffer.
254 **/
255 RETURN_STATUS
256 EFIAPI
257 PeCoffLoaderImageReadFromMemory (
258 IN VOID *FileHandle,
259 IN UINTN FileOffset,
260 IN OUT UINTN *ReadSize,
261 OUT VOID *Buffer
262 );
263
264
265 /**
266 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
267 runtime.
268
269 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
270 the fixups with a virtual mapping.
271
272
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
277
278 **/
279 VOID
280 EFIAPI
281 PeCoffLoaderRelocateImageForRuntime (
282 IN PHYSICAL_ADDRESS ImageBase,
283 IN PHYSICAL_ADDRESS VirtImageBase,
284 IN UINTN ImageSize,
285 IN VOID *RelocationData
286 );
287
288 /**
289 Unloads a loaded PE/COFF image from memory and releases its taken resource.
290
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.
294
295 @param ImageContext Pointer to the image context structure that describes the PE/COFF
296 image to be unloaded.
297
298 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
299 **/
300 RETURN_STATUS
301 EFIAPI
302 PeCoffLoaderUnloadImage (
303 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
304 );
305 #endif