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