2 PE/COFF Loader Library implementation that wraps a protocol passed up from
3 SEC/PEI via a HOB. This is done to save space.
5 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
6 Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/DebugLib.h>
19 #include <Library/HobLib.h>
21 #include <Protocol/PeCoffLoader.h>
24 PE_COFF_LOADER_PROTOCOL
*gPeCoffLoader
= NULL
;
28 Retrieves information about a PE/COFF image.
30 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
31 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
32 DebugDirectoryEntryRva fields of the ImageContext structure.
33 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
34 If the PE/COFF image accessed through the ImageRead service in the ImageContext
35 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
36 If any errors occur while computing the fields of ImageContext,
37 then the error status is returned in the ImageError field of ImageContext.
38 If the image is a TE image, then SectionAlignment is set to 0.
39 The ImageRead and Handle fields of ImageContext structure must be valid prior
40 to invoking this service.
42 @param ImageContext Pointer to the image context structure that describes the PE/COFF
43 image that needs to be examined by this function.
45 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
46 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
47 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
52 PeCoffLoaderGetImageInfo (
53 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
56 return gPeCoffLoader
->GetImageInfo (ImageContext
);
61 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
63 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
64 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
65 of ImageContext as the relocation base address. The caller must allocate the relocation
66 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
68 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
69 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
70 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
71 the ImageContext structure must be valid prior to invoking this service.
73 If ImageContext is NULL, then ASSERT().
75 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
76 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
77 prior to transferring control to a PE/COFF image that is loaded using this library.
79 @param ImageContext Pointer to the image context structure that describes the PE/COFF
80 image that is being relocated.
82 @retval RETURN_SUCCESS The PE/COFF image was relocated.
83 Extended status information is in the ImageError field of ImageContext.
84 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
85 Extended status information is in the ImageError field of ImageContext.
86 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
87 Extended status information is in the ImageError field of ImageContext.
92 PeCoffLoaderRelocateImage (
93 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
96 return gPeCoffLoader
->RelocateImage (ImageContext
);
100 Loads a PE/COFF image into memory.
102 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
103 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
104 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
105 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
106 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
107 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
108 fields of the ImageContext structure must be valid prior to invoking this service.
110 If ImageContext is NULL, then ASSERT().
112 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
113 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
114 prior to transferring control to a PE/COFF image that is loaded using this library.
116 @param ImageContext Pointer to the image context structure that describes the PE/COFF
117 image that is being loaded.
119 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
120 the ImageAddress and ImageSize fields of ImageContext.
121 Extended status information is in the ImageError field of ImageContext.
122 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
123 Extended status information is in the ImageError field of ImageContext.
124 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
125 Extended status information is in the ImageError field of ImageContext.
126 @retval RETURN_INVALID_PARAMETER The image address is invalid.
127 Extended status information is in the ImageError field of ImageContext.
132 PeCoffLoaderLoadImage (
133 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
136 return gPeCoffLoader
->LoadImage (ImageContext
);
142 Reads contents of a PE/COFF image from a buffer in system memory.
144 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
145 that assumes FileHandle pointer to the beginning of a PE/COFF image.
146 This function reads contents of the PE/COFF image that starts at the system memory
147 address specified by FileHandle. The read operation copies ReadSize bytes from the
148 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
149 The size of the buffer actually read is returned in ReadSize.
151 If FileHandle is NULL, then ASSERT().
152 If ReadSize is NULL, then ASSERT().
153 If Buffer is NULL, then ASSERT().
155 @param FileHandle Pointer to base of the input stream
156 @param FileOffset Offset into the PE/COFF image to begin the read operation.
157 @param ReadSize On input, the size in bytes of the requested read operation.
158 On output, the number of bytes actually read.
159 @param Buffer Output buffer that contains the data read from the PE/COFF image.
161 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
166 PeCoffLoaderImageReadFromMemory (
169 IN OUT UINTN
*ReadSize
,
173 return gPeCoffLoader
->ReadFromMemory (
185 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
188 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
189 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
190 to the address specified by VirtualImageBase. RelocationData must be identical
191 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
192 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
194 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
195 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
196 prior to transferring control to a PE/COFF image that is loaded using this library.
198 @param ImageBase Base address of a PE/COFF image that has been loaded
199 and relocated into system memory.
200 @param VirtImageBase The request virtual address that the PE/COFF image is to
202 @param ImageSize The size, in bytes, of the PE/COFF image.
203 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
204 image was relocated using PeCoffLoaderRelocateImage().
209 PeCoffLoaderRelocateImageForRuntime (
210 IN PHYSICAL_ADDRESS ImageBase
,
211 IN PHYSICAL_ADDRESS VirtImageBase
,
213 IN VOID
*RelocationData
216 return gPeCoffLoader
->RelocateImageForRuntime (
226 Unloads a loaded PE/COFF image from memory and releases its taken resource.
227 Releases any environment specific resources that were allocated when the image
228 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
230 For NT32 emulator, the PE/COFF image loaded by system needs to release.
231 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
232 this function can simply return RETURN_SUCCESS.
234 If ImageContext is NULL, then ASSERT().
236 @param ImageContext Pointer to the image context structure that describes the PE/COFF
237 image to be unloaded.
239 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
243 PeCoffLoaderUnloadImage (
244 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
247 return gPeCoffLoader
->UnloadImage (ImageContext
);
251 EFI_HOB_GUID_TYPE Hob
;
257 The constructor function caches the pointer of DXE Services Table.
259 The constructor function caches the pointer of DXE Services Table.
260 It will ASSERT() if that operation fails.
261 It will ASSERT() if the pointer of DXE Services Table is NULL.
262 It will always return EFI_SUCCESS.
264 @param ImageHandle The firmware allocated handle for the EFI image.
265 @param SystemTable A pointer to the EFI System Table.
267 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
272 DxeHobPeCoffLibConstructor (
273 IN EFI_HANDLE ImageHandle
,
274 IN EFI_SYSTEM_TABLE
*SystemTable
279 Hob
= GetFirstGuidHob (&gPeCoffLoaderProtocolGuid
);
281 return EFI_NOT_FOUND
;
284 gPeCoffLoader
= Hob
->Interface
;