]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/PeCoffLib.h
move BUGBUG comments and add some comments to comply with Spec
[mirror_edk2.git] / MdePkg / Include / Library / PeCoffLib.h
1 /** @file
2 Memory Only PE COFF loader.
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 ///
89 UINT32 SectionAlignment;
90 ///
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.
94 ///
95 UINT32 PeCoffHeaderOffset;
96 ///
97 /// Set by PeCoffLoaderGetImageInfo() to the Relative Virtual Address of the debug directory
98 /// if it exists in the image
99 ///
100 UINT32 DebugDirectoryEntryRva;
101 ///
102 /// Set by PeCoffLoaderLoadImage() to CodeView area of the PE/COFF Debug directory.
103 ///
104 VOID *CodeView;
105 ///
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.
109 ///
110 CHAR8 *PdbPointer;
111 ///
112 /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header.
113 ///
114 UINTN SizeOfHeaders;
115 ///
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.
120 ///
121 UINT32 ImageCodeMemoryType;
122 ///
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
127 ///
128 UINT32 ImageDataMemoryType;
129 ///
130 /// Set by any of the library functions if they encounter an error.
131 ///
132 UINT32 ImageError;
133 ///
134 /// Set by PeCoffLoaderLoadImage() to indicate the size of FixupData that the caller must
135 /// allocate before calling PeCoffLoaderRelocateImage()
136 ///
137 UINTN FixupDataSize;
138 ///
139 /// Set by PeCoffLoaderGetImageInfo() to the machine type stored in the PE/COFF header
140 ///
141 UINT16 Machine;
142 ///
143 /// Set by PeCoffLoaderGetImageInfo() to the subsystem type stored in the PE/COFF header.
144 ///
145 UINT16 ImageType;
146 ///
147 /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain
148 /// relocation information.
149 ///
150 BOOLEAN RelocationsStripped;
151 ///
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.
155 ///
156 BOOLEAN IsTeImage;
157 } PE_COFF_LOADER_IMAGE_CONTEXT;
158
159 /**
160 Retrieves information about a PE/COFF image.
161
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
168 ImageContext.
169
170 @param ImageContext Pointer to the image context structure that describes the PE/COFF
171 image that needs to be examined by this function.
172
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.
176
177 **/
178 RETURN_STATUS
179 EFIAPI
180 PeCoffLoaderGetImageInfo (
181 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
182 );
183
184 /**
185 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
186
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().
192
193 @param ImageContext Pointer to the image context structure that describes the PE/COFF
194 image that is being relocated.
195
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.
202
203 **/
204 RETURN_STATUS
205 EFIAPI
206 PeCoffLoaderRelocateImage (
207 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
208 );
209
210 /**
211 Loads a PE/COFF image into memory.
212
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().
218
219 @param ImageContext Pointer to the image context structure that describes the PE/COFF
220 image that is being loaded.
221
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.
231
232 **/
233 RETURN_STATUS
234 EFIAPI
235 PeCoffLoaderLoadImage (
236 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
237 );
238
239
240 /**
241 ImageRead function that operates on a memory buffer whos base is passed into
242 FileHandle.
243
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
248
249 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
250 the buffer.
251 **/
252 RETURN_STATUS
253 EFIAPI
254 PeCoffLoaderImageReadFromMemory (
255 IN VOID *FileHandle,
256 IN UINTN FileOffset,
257 IN OUT UINTN *ReadSize,
258 OUT VOID *Buffer
259 );
260
261
262 /**
263 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
264 runtime.
265
266 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
267 the fixups with a virtual mapping.
268
269
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
274
275 **/
276 VOID
277 EFIAPI
278 PeCoffLoaderRelocateImageForRuntime (
279 IN PHYSICAL_ADDRESS ImageBase,
280 IN PHYSICAL_ADDRESS VirtImageBase,
281 IN UINTN ImageSize,
282 IN VOID *RelocationData
283 );
284
285 /**
286 Unloads a loaded PE/COFF image from memory and releases its taken resource.
287
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.
291
292 @param ImageContext Pointer to the image context structure that describes the PE/COFF
293 image to be unloaded.
294
295 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
296 **/
297 RETURN_STATUS
298 EFIAPI
299 PeCoffLoaderUnloadImage (
300 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
301 );
302 #endif