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