]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/PeCoffLib.h
1) Move the structure definitions related GUID EFI_STATUS_CODE_SPECIFIC_DATA_GUID...
[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 /**
101 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
102
103 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
104 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
105 of ImageContext as the relocation base address. The caller must allocate the relocation
106 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
107 If ImageContext is NULL, then ASSERT().
108
109 @param ImageContext Pointer to the image context structure that describes the PE/COFF
110 image that is being relocated.
111
112 @retval RETURN_SUCCESS The PE/COFF image was relocated.
113 Extended status information is in the ImageError field of ImageContext.
114 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
115 Extended status information is in the ImageError field of ImageContext.
116 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
117 Extended status information is in the ImageError field of ImageContext.
118
119 **/
120 RETURN_STATUS
121 EFIAPI
122 PeCoffLoaderRelocateImage (
123 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
124 )
125 ;
126
127 /**
128 Loads a PE/COFF image into memory.
129
130 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
131 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
132 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
133 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
134 If ImageContext is NULL, then ASSERT().
135
136 @param ImageContext Pointer to the image context structure that describes the PE/COFF
137 image that is being loaded.
138
139 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
140 the ImageAddress and ImageSize fields of ImageContext.
141 Extended status information is in the ImageError field of ImageContext.
142 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
143 Extended status information is in the ImageError field of ImageContext.
144 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
145 Extended status information is in the ImageError field of ImageContext.
146 @retval RETURN_INVALID_PARAMETER The image address is invalid.
147 Extended status information is in the ImageError field of ImageContext.
148
149 **/
150 RETURN_STATUS
151 EFIAPI
152 PeCoffLoaderLoadImage (
153 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
154 )
155 ;
156
157
158 /**
159 ImageRead function that operates on a memory buffer whos base is passed into
160 FileHandle.
161
162 @param FileHandle Ponter to baes of the input stream
163 @param FileOffset Offset to the start of the buffer
164 @param ReadSize Number of bytes to copy into the buffer
165 @param Buffer Location to place results of read
166
167 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
168 the buffer.
169 **/
170 RETURN_STATUS
171 EFIAPI
172 PeCoffLoaderImageReadFromMemory (
173 IN VOID *FileHandle,
174 IN UINTN FileOffset,
175 IN OUT UINTN *ReadSize,
176 OUT VOID *Buffer
177 )
178 ;
179
180
181 /**
182 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
183 runtime.
184
185 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
186 the fixups with a virtual mapping.
187
188
189 @param ImageBase Base address of relocated image
190 @param VirtImageBase Virtual mapping for ImageBase
191 @param ImageSize Size of the image to relocate
192 @param RelocationData Location to place results of read
193
194 **/
195 VOID
196 EFIAPI
197 PeCoffLoaderRelocateImageForRuntime (
198 IN PHYSICAL_ADDRESS ImageBase,
199 IN PHYSICAL_ADDRESS VirtImageBase,
200 IN UINTN ImageSize,
201 IN VOID *RelocationData
202 )
203 ;
204
205
206 #endif