2 Memory Only PE COFF loader.
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
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.
15 #ifndef __BASE_PE_COFF_LIB_H__
16 #define __BASE_PE_COFF_LIB_H__
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
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
35 // PE/COFF Loader Read Function passed in by caller
39 (EFIAPI
*PE_COFF_LOADER_READ_FILE
) (
42 IN OUT UINTN
*ReadSize
,
47 // Context structure used while PE/COFF image is being loaded and relocated
50 PHYSICAL_ADDRESS ImageAddress
;
52 PHYSICAL_ADDRESS DestinationAddress
;
53 PHYSICAL_ADDRESS EntryPoint
;
54 PE_COFF_LOADER_READ_FILE ImageRead
;
57 UINT32 SectionAlignment
;
58 UINT32 PeCoffHeaderOffset
;
59 UINT32 DebugDirectoryEntryRva
;
63 UINT32 ImageCodeMemoryType
;
64 UINT32 ImageDataMemoryType
;
69 BOOLEAN RelocationsStripped
;
71 } PE_COFF_LOADER_IMAGE_CONTEXT
;
74 Retrieves information about a PE/COFF image.
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
84 @param ImageContext Pointer to the image context structure that describes the PE/COFF
85 image that needs to be examined by this function.
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.
94 PeCoffLoaderGetImageInfo (
95 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
100 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
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().
108 @param ImageContext Pointer to the image context structure that describes the PE/COFF
109 image that is being relocated.
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.
121 PeCoffLoaderRelocateImage (
122 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
127 Loads a PE/COFF image into memory.
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().
135 @param ImageContext Pointer to the image context structure that describes the PE/COFF
136 image that is being loaded.
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.
151 PeCoffLoaderLoadImage (
152 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
158 ImageRead function that operates on a memory buffer whos base is passed into
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
166 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
171 PeCoffLoaderImageReadFromMemory (
174 IN OUT UINTN
*ReadSize
,
181 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
184 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
185 the fixups with a virtual mapping.
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
196 PeCoffLoaderRelocateImageForRuntime (
197 IN PHYSICAL_ADDRESS ImageBase
,
198 IN PHYSICAL_ADDRESS VirtImageBase
,
200 IN VOID
*RelocationData