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