3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Public header file for PeCoff Lib
23 #ifndef __EDKII_GLUE_BASE_PE_COFF_LIB_H__
24 #define __EDKII_GLUE_BASE_PE_COFF_LIB_H__
27 #define PeCoffLoaderGetImageInfo(_IMAGECONTEXT) GluePeCoffLoaderGetImageInfo(_IMAGECONTEXT)
28 #define PeCoffLoaderRelocateImage(_IMAGECONTEXT) GluePeCoffLoaderRelocateImage(_IMAGECONTEXT)
29 #define PeCoffLoaderLoadImage(_IMAGECONTEXT) GluePeCoffLoaderLoadImage(_IMAGECONTEXT)
30 #define PeCoffLoaderGetPeHeader(_IMAGECONTEXT, _HDR) GluePeCoffLoaderGetPeHeader(_IMAGECONTEXT, _HDR)
31 #define PeCoffLoaderImageAddress(_IMAGECONTEXT, _ADR) GluePeCoffLoaderImageAddress(_IMAGECONTEXT, _ADR)
32 #define PeCoffLoaderRelocateImage(_IMAGECONTEXT) GluePeCoffLoaderRelocateImage(_IMAGECONTEXT)
33 #define PeCoffLoaderRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST) \
34 GluePeCoffLoaderRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST)
35 #define PeHotRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST) \
36 GluePeHotRelocateImageEx(_RELOC, _FIXUP, _FIXUPDATA, _ADJUST)
40 // Return status codes from the PE/COFF Loader services
41 // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes
43 #define IMAGE_ERROR_SUCCESS 0
44 #define IMAGE_ERROR_IMAGE_READ 1
45 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
46 #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
47 #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
48 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
49 #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
50 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
51 #define IMAGE_ERROR_SECTION_NOT_LOADED 8
52 #define IMAGE_ERROR_FAILED_RELOCATION 9
53 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
56 // PE/COFF Loader Read Function passed in by caller
60 (EFIAPI
*PE_COFF_LOADER_READ_FILE
) (
63 IN OUT UINTN
*ReadSize
,
68 // Context structure used while PE/COFF image is being loaded and relocated
71 PHYSICAL_ADDRESS ImageAddress
;
73 PHYSICAL_ADDRESS DestinationAddress
;
74 PHYSICAL_ADDRESS EntryPoint
;
75 PE_COFF_LOADER_READ_FILE ImageRead
;
78 UINT32 SectionAlignment
;
79 UINT32 PeCoffHeaderOffset
;
80 UINT32 DebugDirectoryEntryRva
;
84 UINT32 ImageCodeMemoryType
;
85 UINT32 ImageDataMemoryType
;
90 BOOLEAN RelocationsStripped
;
92 } PE_COFF_LOADER_IMAGE_CONTEXT
;
95 Retrieves information about a PE/COFF image.
97 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
98 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
99 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
100 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
101 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
102 computing the fields of ImageContext, then the error status is returned in the ImageError field of
105 @param ImageContext Pointer to the image context structure that describes the PE/COFF
106 image that needs to be examined by this function.
108 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
109 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
110 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
115 GluePeCoffLoaderGetImageInfo (
116 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
120 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
122 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
123 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
124 of ImageContext as the relocation base address. The caller must allocate the relocation
125 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
126 If ImageContext is NULL, then ASSERT().
128 @param ImageContext Pointer to the image context structure that describes the PE/COFF
129 image that is being relocated.
131 @retval RETURN_SUCCESS The PE/COFF image was relocated.
132 Extended status information is in the ImageError field of ImageContext.
133 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
134 Extended status information is in the ImageError field of ImageContext.
135 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
136 Extended status information is in the ImageError field of ImageContext.
141 GluePeCoffLoaderRelocateImage (
142 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
146 Loads a PE/COFF image into memory.
148 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
149 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
150 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
151 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
152 If ImageContext is NULL, then ASSERT().
154 @param ImageContext Pointer to the image context structure that describes the PE/COFF
155 image that is being loaded.
157 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
158 the ImageAddress and ImageSize fields of ImageContext.
159 Extended status information is in the ImageError field of ImageContext.
160 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
161 Extended status information is in the ImageError field of ImageContext.
162 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
163 Extended status information is in the ImageError field of ImageContext.
164 @retval RETURN_INVALID_PARAMETER The image address is invalid.
165 Extended status information is in the ImageError field of ImageContext.
170 GluePeCoffLoaderLoadImage (
171 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
176 ImageRead function that operates on a memory buffer whos base is passed into
179 @param FileHandle Ponter to baes of the input stream
180 @param FileOffset Offset to the start of the buffer
181 @param ReadSize Number of bytes to copy into the buffer
182 @param Buffer Location to place results of read
184 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
189 PeCoffLoaderImageReadFromMemory (
192 IN OUT UINTN
*ReadSize
,
198 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
201 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
202 the fixups with a virtual mapping.
205 @param ImageBase Base address of relocated image
206 @param VirtImageBase Virtual mapping for ImageBase
207 @param ImageSize Size of the image to relocate
208 @param RelocationData Location to place results of read
213 PeCoffLoaderRelocateImageForRuntime (
214 IN PHYSICAL_ADDRESS ImageBase
,
215 IN PHYSICAL_ADDRESS VirtImageBase
,
217 IN VOID
*RelocationData