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<BR>
6 Portions copyright (c) 2008-2010 Apple Inc. All rights reserved.<BR>
7 All rights reserved. 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>
25 Retrieves information about a PE/COFF image.
27 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
28 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
29 DebugDirectoryEntryRva fields of the ImageContext structure.
30 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
31 If the PE/COFF image accessed through the ImageRead service in the ImageContext
32 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
33 If any errors occur while computing the fields of ImageContext,
34 then the error status is returned in the ImageError field of ImageContext.
35 If the image is a TE image, then SectionAlignment is set to 0.
36 The ImageRead and Handle fields of ImageContext structure must be valid prior
37 to invoking this service.
39 @param ImageContext Pointer to the image context structure that describes the PE/COFF
40 image that needs to be examined by this function.
42 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
43 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
44 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
49 PeCoffLoaderGetImageInfo (
50 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
53 return gPeCoffLoader
->GetImageInfo (ImageContext
);
58 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
60 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
61 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
62 of ImageContext as the relocation base address. The caller must allocate the relocation
63 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
65 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
66 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
67 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
68 the ImageContext structure must be valid prior to invoking this service.
70 If ImageContext is NULL, then ASSERT().
72 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
73 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
74 prior to transferring control to a PE/COFF image that is loaded using this library.
76 @param ImageContext Pointer to the image context structure that describes the PE/COFF
77 image that is being relocated.
79 @retval RETURN_SUCCESS The PE/COFF image was relocated.
80 Extended status information is in the ImageError field of ImageContext.
81 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
82 Extended status information is in the ImageError field of ImageContext.
83 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
84 Extended status information is in the ImageError field of ImageContext.
89 PeCoffLoaderRelocateImage (
90 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
93 return gPeCoffLoader
->RelocateImage (ImageContext
);
97 Loads a PE/COFF image into memory.
99 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
100 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
101 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
102 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
103 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
104 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
105 fields of the ImageContext structure must be valid prior to invoking this service.
107 If ImageContext is NULL, then ASSERT().
109 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
110 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
111 prior to transferring control to a PE/COFF image that is loaded using this library.
113 @param ImageContext Pointer to the image context structure that describes the PE/COFF
114 image that is being loaded.
116 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
117 the ImageAddress and ImageSize fields of ImageContext.
118 Extended status information is in the ImageError field of ImageContext.
119 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
120 Extended status information is in the ImageError field of ImageContext.
121 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
122 Extended status information is in the ImageError field of ImageContext.
123 @retval RETURN_INVALID_PARAMETER The image address is invalid.
124 Extended status information is in the ImageError field of ImageContext.
129 PeCoffLoaderLoadImage (
130 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
133 return gPeCoffLoader
->LoadImage (ImageContext
);
139 Reads contents of a PE/COFF image from a buffer in system memory.
141 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
142 that assumes FileHandle pointer to the beginning of a PE/COFF image.
143 This function reads contents of the PE/COFF image that starts at the system memory
144 address specified by FileHandle. The read operation copies ReadSize bytes from the
145 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
146 The size of the buffer actually read is returned in ReadSize.
148 If FileHandle is NULL, then ASSERT().
149 If ReadSize is NULL, then ASSERT().
150 If Buffer is NULL, then ASSERT().
152 @param FileHandle Pointer to base of the input stream
153 @param FileOffset Offset into the PE/COFF image to begin the read operation.
154 @param ReadSize On input, the size in bytes of the requested read operation.
155 On output, the number of bytes actually read.
156 @param Buffer Output buffer that contains the data read from the PE/COFF image.
158 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
163 PeCoffLoaderImageReadFromMemory (
166 IN OUT UINTN
*ReadSize
,
170 return gPeCoffLoader
->RelocateImageForRuntime (
182 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
185 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
186 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
187 to the address specified by VirtualImageBase. RelocationData must be identical
188 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
189 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
191 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
192 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
193 prior to transferring control to a PE/COFF image that is loaded using this library.
195 @param ImageBase Base address of a PE/COFF image that has been loaded
196 and relocated into system memory.
197 @param VirtImageBase The request virtual address that the PE/COFF image is to
199 @param ImageSize The size, in bytes, of the PE/COFF image.
200 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
201 image was relocated using PeCoffLoaderRelocateImage().
206 PeCoffLoaderRelocateImageForRuntime (
207 IN PHYSICAL_ADDRESS ImageBase
,
208 IN PHYSICAL_ADDRESS VirtImageBase
,
210 IN VOID
*RelocationData
213 return gPeCoffLoader
->RelocateImageForRuntime (
223 Unloads a loaded PE/COFF image from memory and releases its taken resource.
224 Releases any environment specific resources that were allocated when the image
225 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
227 For NT32 emulator, the PE/COFF image loaded by system needs to release.
228 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
229 this function can simply return RETURN_SUCCESS.
231 If ImageContext is NULL, then ASSERT().
233 @param ImageContext Pointer to the image context structure that describes the PE/COFF
234 image to be unloaded.
236 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
240 PeCoffLoaderUnloadImage (
241 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
244 return gPeCoffLoader
->UnloadImage (ImageContext
);
250 Register LzmaDecompress and LzmaDecompressGetInfo handlers with LzmaCustomerDecompressGuid.
252 @retval RETURN_SUCCESS Register successfully.
253 @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.
257 DxeHobPeCoffLibConstructor (
262 Hob
= GetFirstGuidHob (&gPeCoffLoaderProtocolGuid
);
264 return EFI_NOT_FOUND
;
267 gPeCoffLoader
= Hob
->Interface
;