]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/PeCoffLib.h
clean up the un-suitable ';' location when declaring the functions.
[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 // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes
22 //
23 #define IMAGE_ERROR_SUCCESS 0
24 #define IMAGE_ERROR_IMAGE_READ 1
25 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
26 #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
27 #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
28 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
29 #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
30 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
31 #define IMAGE_ERROR_SECTION_NOT_LOADED 8
32 #define IMAGE_ERROR_FAILED_RELOCATION 9
33 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
34
35 //
36 // PE/COFF Loader Read Function passed in by caller
37 //
38 typedef
39 RETURN_STATUS
40 (EFIAPI *PE_COFF_LOADER_READ_FILE) (
41 IN VOID *FileHandle,
42 IN UINTN FileOffset,
43 IN OUT UINTN *ReadSize,
44 OUT VOID *Buffer
45 );
46
47 //
48 // Context structure used while PE/COFF image is being loaded and relocated
49 //
50 typedef struct {
51 PHYSICAL_ADDRESS ImageAddress;
52 UINT64 ImageSize;
53 PHYSICAL_ADDRESS DestinationAddress;
54 PHYSICAL_ADDRESS EntryPoint;
55 PE_COFF_LOADER_READ_FILE ImageRead;
56 VOID *Handle;
57 VOID *FixupData;
58 UINT32 SectionAlignment;
59 UINT32 PeCoffHeaderOffset;
60 UINT32 DebugDirectoryEntryRva;
61 VOID *CodeView;
62 CHAR8 *PdbPointer;
63 UINTN SizeOfHeaders;
64 UINT32 ImageCodeMemoryType;
65 UINT32 ImageDataMemoryType;
66 UINT32 ImageError;
67 UINTN FixupDataSize;
68 UINT16 Machine;
69 UINT16 ImageType;
70 BOOLEAN RelocationsStripped;
71 BOOLEAN IsTeImage;
72 } PE_COFF_LOADER_IMAGE_CONTEXT;
73
74 /**
75 Retrieves information about a PE/COFF image.
76
77 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
78 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
79 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
80 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
81 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
82 computing the fields of ImageContext, then the error status is returned in the ImageError field of
83 ImageContext.
84
85 @param ImageContext Pointer to the image context structure that describes the PE/COFF
86 image that needs to be examined by this function.
87
88 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
89 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
90 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
91
92 **/
93 RETURN_STATUS
94 EFIAPI
95 PeCoffLoaderGetImageInfo (
96 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
97 );
98
99 /**
100 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
101
102 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
103 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
104 of ImageContext as the relocation base address. The caller must allocate the relocation
105 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
106 If ImageContext is NULL, then ASSERT().
107
108 @param ImageContext Pointer to the image context structure that describes the PE/COFF
109 image that is being relocated.
110
111 @retval RETURN_SUCCESS The PE/COFF image was relocated.
112 Extended status information is in the ImageError field of ImageContext.
113 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
114 Extended status information is in the ImageError field of ImageContext.
115 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
116 Extended status information is in the ImageError field of ImageContext.
117
118 **/
119 RETURN_STATUS
120 EFIAPI
121 PeCoffLoaderRelocateImage (
122 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
123 );
124
125 /**
126 Loads a PE/COFF image into memory.
127
128 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
129 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
130 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
131 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
132 If ImageContext is NULL, then ASSERT().
133
134 @param ImageContext Pointer to the image context structure that describes the PE/COFF
135 image that is being loaded.
136
137 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
138 the ImageAddress and ImageSize fields of ImageContext.
139 Extended status information is in the ImageError field of ImageContext.
140 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
141 Extended status information is in the ImageError field of ImageContext.
142 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
143 Extended status information is in the ImageError field of ImageContext.
144 @retval RETURN_INVALID_PARAMETER The image address is invalid.
145 Extended status information is in the ImageError field of ImageContext.
146
147 **/
148 RETURN_STATUS
149 EFIAPI
150 PeCoffLoaderLoadImage (
151 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
152 );
153
154
155 /**
156 ImageRead function that operates on a memory buffer whos base is passed into
157 FileHandle.
158
159 @param FileHandle Ponter to baes of the input stream
160 @param FileOffset Offset to the start of the buffer
161 @param ReadSize Number of bytes to copy into the buffer
162 @param Buffer Location to place results of read
163
164 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
165 the buffer.
166 **/
167 RETURN_STATUS
168 EFIAPI
169 PeCoffLoaderImageReadFromMemory (
170 IN VOID *FileHandle,
171 IN UINTN FileOffset,
172 IN OUT UINTN *ReadSize,
173 OUT VOID *Buffer
174 );
175
176
177 /**
178 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
179 runtime.
180
181 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
182 the fixups with a virtual mapping.
183
184
185 @param ImageBase Base address of relocated image
186 @param VirtImageBase Virtual mapping for ImageBase
187 @param ImageSize Size of the image to relocate
188 @param RelocationData Location to place results of read
189
190 **/
191 VOID
192 EFIAPI
193 PeCoffLoaderRelocateImageForRuntime (
194 IN PHYSICAL_ADDRESS ImageBase,
195 IN PHYSICAL_ADDRESS VirtImageBase,
196 IN UINTN ImageSize,
197 IN VOID *RelocationData
198 );
199
200 /**
201 Unloads a loaded PE/COFF image from memory and releases its taken resource.
202
203 For NT32 emulator, the PE/COFF image loaded by system needs to release.
204 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
205 this function can simply return RETURN_SUCCESS.
206
207 @param ImageContext Pointer to the image context structure that describes the PE/COFF
208 image to be unloaded.
209
210 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
211 **/
212 RETURN_STATUS
213 EFIAPI
214 PeCoffLoaderUnloadImage (
215 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
216 );
217 #endif