2 Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but
3 only supports relocating IA32, x64, IPF, and EBC images.
5 Caution: This file requires additional review when modified.
6 This library will have external input - PE/COFF image.
7 This external input must be validated carefully to avoid security issue like
8 buffer overflow, integer overflow.
10 The basic guideline is that caller need provide ImageContext->ImageRead () with the
11 necessary data range check, to make sure when this library reads PE/COFF image, the
12 PE image buffer is always in valid range.
13 This library will also do some additional check for PE header fields.
15 PeCoffLoaderGetPeHeader() routine will do basic check for PE/COFF header.
16 PeCoffLoaderGetImageInfo() routine will do basic check for whole PE/COFF image.
18 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
19 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
20 This program and the accompanying materials
21 are licensed and made available under the terms and conditions of the BSD License
22 which accompanies this distribution. The full text of the license may be found at
23 http://opensource.org/licenses/bsd-license.php.
25 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
26 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
30 #include "BasePeCoffLibInternals.h"
33 Adjust some fields in section header for TE image.
35 @param SectionHeader Pointer to the section header.
36 @param TeStrippedOffset Size adjust for the TE image.
40 PeCoffLoaderAdjustOffsetForTeImage (
41 EFI_IMAGE_SECTION_HEADER
*SectionHeader
,
42 UINT32 TeStrippedOffset
45 SectionHeader
->VirtualAddress
-= TeStrippedOffset
;
46 SectionHeader
->PointerToRawData
-= TeStrippedOffset
;
50 Retrieves the PE or TE Header from a PE/COFF or TE image.
52 Caution: This function may receive untrusted input.
53 PE/COFF image is external input, so this routine will
54 also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader,
55 SizeOfHeader, Section Data Region and Security Data Region be in PE image range.
57 @param ImageContext The context of the image being loaded.
58 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
60 @retval RETURN_SUCCESS The PE or TE Header is read.
61 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
65 PeCoffLoaderGetPeHeader (
66 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
67 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
71 EFI_IMAGE_DOS_HEADER DosHdr
;
74 UINT32 SectionHeaderOffset
;
76 UINT32 HeaderWithoutDataDir
;
78 UINTN NumberOfSections
;
79 EFI_IMAGE_SECTION_HEADER SectionHeader
;
82 // Read the DOS image header to check for its existence
84 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
86 Status
= ImageContext
->ImageRead (
92 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
93 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
94 if (Size
!= ReadSize
) {
95 Status
= RETURN_UNSUPPORTED
;
100 ImageContext
->PeCoffHeaderOffset
= 0;
101 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
103 // DOS image header is present, so read the PE header after the DOS image
106 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
110 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
111 // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
112 // determines if this is a PE32 or PE32+ image. The magic is in the same
113 // location in both images.
115 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
117 Status
= ImageContext
->ImageRead (
118 ImageContext
->Handle
,
119 ImageContext
->PeCoffHeaderOffset
,
123 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
124 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
125 if (Size
!= ReadSize
) {
126 Status
= RETURN_UNSUPPORTED
;
132 // Use Signature to figure out if we understand the image format
134 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
135 ImageContext
->IsTeImage
= TRUE
;
136 ImageContext
->Machine
= Hdr
.Te
->Machine
;
137 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
139 // For TeImage, SectionAlignment is undefined to be set to Zero
140 // ImageSize can be calculated.
142 ImageContext
->ImageSize
= 0;
143 ImageContext
->SectionAlignment
= 0;
144 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
147 // Check the StrippedSize.
149 if (sizeof (EFI_TE_IMAGE_HEADER
) >= (UINT32
)Hdr
.Te
->StrippedSize
) {
150 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
151 return RETURN_UNSUPPORTED
;
155 // Check the SizeOfHeaders field.
157 if (Hdr
.Te
->BaseOfCode
<= Hdr
.Te
->StrippedSize
) {
158 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
159 return RETURN_UNSUPPORTED
;
163 // Read last byte of Hdr.Te->SizeOfHeaders from the file.
167 Status
= ImageContext
->ImageRead (
168 ImageContext
->Handle
,
169 ImageContext
->SizeOfHeaders
- 1,
173 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
174 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
175 if (Size
!= ReadSize
) {
176 Status
= RETURN_UNSUPPORTED
;
182 // TE Image Data Directory Entry size is non-zero, but the Data Directory Virtual Address is zero.
183 // This case is not a valid TE image.
185 if ((Hdr
.Te
->DataDirectory
[0].Size
!= 0 && Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0) ||
186 (Hdr
.Te
->DataDirectory
[1].Size
!= 0 && Hdr
.Te
->DataDirectory
[1].VirtualAddress
== 0)) {
187 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
188 return RETURN_UNSUPPORTED
;
190 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
191 ImageContext
->IsTeImage
= FALSE
;
192 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
194 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
196 // 1. Check OptionalHeader.NumberOfRvaAndSizes filed.
198 if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
< Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
) {
199 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
200 return RETURN_UNSUPPORTED
;
204 // 2. Check the FileHeader.SizeOfOptionalHeader field.
205 // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so
206 // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow.
208 HeaderWithoutDataDir
= sizeof (EFI_IMAGE_OPTIONAL_HEADER32
) - sizeof (EFI_IMAGE_DATA_DIRECTORY
) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
209 if (((UINT32
)Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
- HeaderWithoutDataDir
) !=
210 Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
* sizeof (EFI_IMAGE_DATA_DIRECTORY
)) {
211 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
212 return RETURN_UNSUPPORTED
;
215 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+ sizeof (UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
;
217 // 3. Check the FileHeader.NumberOfSections field.
219 if (Hdr
.Pe32
->OptionalHeader
.SizeOfImage
<= SectionHeaderOffset
) {
220 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
221 return RETURN_UNSUPPORTED
;
223 if ((Hdr
.Pe32
->OptionalHeader
.SizeOfImage
- SectionHeaderOffset
) / EFI_IMAGE_SIZEOF_SECTION_HEADER
<= Hdr
.Pe32
->FileHeader
.NumberOfSections
) {
224 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
225 return RETURN_UNSUPPORTED
;
229 // 4. Check the OptionalHeader.SizeOfHeaders field.
231 if (Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
<= SectionHeaderOffset
) {
232 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
233 return RETURN_UNSUPPORTED
;
235 if (Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
>= Hdr
.Pe32
->OptionalHeader
.SizeOfImage
) {
236 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
237 return RETURN_UNSUPPORTED
;
239 if ((Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- SectionHeaderOffset
) / EFI_IMAGE_SIZEOF_SECTION_HEADER
< (UINT32
)Hdr
.Pe32
->FileHeader
.NumberOfSections
) {
240 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
241 return RETURN_UNSUPPORTED
;
245 // 4.2 Read last byte of Hdr.Pe32.OptionalHeader.SizeOfHeaders from the file.
249 Status
= ImageContext
->ImageRead (
250 ImageContext
->Handle
,
251 Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- 1,
255 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
256 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
257 if (Size
!= ReadSize
) {
258 Status
= RETURN_UNSUPPORTED
;
264 // Check the EFI_IMAGE_DIRECTORY_ENTRY_SECURITY data.
265 // Read the last byte to make sure the data is in the image region.
266 // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.
268 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
< Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
) {
269 if (Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
!= 0) {
271 // Check the member data to avoid overflow.
273 if ((UINT32
) (~0) - Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
<
274 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
) {
275 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
276 return RETURN_UNSUPPORTED
;
280 // Read last byte of section header from file
284 Status
= ImageContext
->ImageRead (
285 ImageContext
->Handle
,
286 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
+
287 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
- 1,
291 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
292 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
293 if (Size
!= ReadSize
) {
294 Status
= RETURN_UNSUPPORTED
;
304 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
305 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
306 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
307 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
309 } else if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
311 // 1. Check FileHeader.NumberOfRvaAndSizes filed.
313 if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
< Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
) {
314 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
315 return RETURN_UNSUPPORTED
;
318 // 2. Check the FileHeader.SizeOfOptionalHeader field.
319 // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so
320 // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow.
322 HeaderWithoutDataDir
= sizeof (EFI_IMAGE_OPTIONAL_HEADER64
) - sizeof (EFI_IMAGE_DATA_DIRECTORY
) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
323 if (((UINT32
)Hdr
.Pe32Plus
->FileHeader
.SizeOfOptionalHeader
- HeaderWithoutDataDir
) !=
324 Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
* sizeof (EFI_IMAGE_DATA_DIRECTORY
)) {
325 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
326 return RETURN_UNSUPPORTED
;
329 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+ sizeof (UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + Hdr
.Pe32Plus
->FileHeader
.SizeOfOptionalHeader
;
331 // 3. Check the FileHeader.NumberOfSections field.
333 if (Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
<= SectionHeaderOffset
) {
334 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
335 return RETURN_UNSUPPORTED
;
337 if ((Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
- SectionHeaderOffset
) / EFI_IMAGE_SIZEOF_SECTION_HEADER
<= Hdr
.Pe32Plus
->FileHeader
.NumberOfSections
) {
338 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
339 return RETURN_UNSUPPORTED
;
343 // 4. Check the OptionalHeader.SizeOfHeaders field.
345 if (Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
<= SectionHeaderOffset
) {
346 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
347 return RETURN_UNSUPPORTED
;
349 if (Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
>= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
) {
350 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
351 return RETURN_UNSUPPORTED
;
353 if ((Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- SectionHeaderOffset
) / EFI_IMAGE_SIZEOF_SECTION_HEADER
< (UINT32
)Hdr
.Pe32Plus
->FileHeader
.NumberOfSections
) {
354 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
355 return RETURN_UNSUPPORTED
;
359 // 4.2 Read last byte of Hdr.Pe32Plus.OptionalHeader.SizeOfHeaders from the file.
363 Status
= ImageContext
->ImageRead (
364 ImageContext
->Handle
,
365 Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- 1,
369 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
370 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
371 if (Size
!= ReadSize
) {
372 Status
= RETURN_UNSUPPORTED
;
378 // Check the EFI_IMAGE_DIRECTORY_ENTRY_SECURITY data.
379 // Read the last byte to make sure the data is in the image region.
380 // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.
382 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
< Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
) {
383 if (Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
!= 0) {
385 // Check the member data to avoid overflow.
387 if ((UINT32
) (~0) - Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
<
388 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
) {
389 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
390 return RETURN_UNSUPPORTED
;
394 // Read last byte of section header from file
398 Status
= ImageContext
->ImageRead (
399 ImageContext
->Handle
,
400 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
+
401 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
- 1,
405 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
406 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
407 if (Size
!= ReadSize
) {
408 Status
= RETURN_UNSUPPORTED
;
418 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
419 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
420 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
421 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
423 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
424 return RETURN_UNSUPPORTED
;
427 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
428 return RETURN_UNSUPPORTED
;
431 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
433 // If the PE/COFF loader does not support the image type return
434 // unsupported. This library can support lots of types of images
435 // this does not mean the user of this library can call the entry
436 // point of the image.
438 return RETURN_UNSUPPORTED
;
442 // Check each section field.
444 if (ImageContext
->IsTeImage
) {
445 SectionHeaderOffset
= sizeof(EFI_TE_IMAGE_HEADER
);
446 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
448 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+ sizeof (UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
;
449 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
452 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
454 // Read section header from file
456 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
458 Status
= ImageContext
->ImageRead (
459 ImageContext
->Handle
,
464 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
465 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
466 if (Size
!= ReadSize
) {
467 Status
= RETURN_UNSUPPORTED
;
473 // Adjust some field in Section Header for TE image.
475 if (ImageContext
->IsTeImage
) {
476 PeCoffLoaderAdjustOffsetForTeImage (&SectionHeader
, (UINT32
)Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
479 if (SectionHeader
.SizeOfRawData
> 0) {
481 // Section data should bigger than the Pe header.
483 if (SectionHeader
.VirtualAddress
< ImageContext
->SizeOfHeaders
||
484 SectionHeader
.PointerToRawData
< ImageContext
->SizeOfHeaders
) {
485 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
486 return RETURN_UNSUPPORTED
;
490 // Check the member data to avoid overflow.
492 if ((UINT32
) (~0) - SectionHeader
.PointerToRawData
< SectionHeader
.SizeOfRawData
) {
493 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
494 return RETURN_UNSUPPORTED
;
498 // Base on the ImageRead function to check the section data field.
499 // Read the last byte to make sure the data is in the image region.
503 Status
= ImageContext
->ImageRead (
504 ImageContext
->Handle
,
505 SectionHeader
.PointerToRawData
+ SectionHeader
.SizeOfRawData
- 1,
509 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
510 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
511 if (Size
!= ReadSize
) {
512 Status
= RETURN_UNSUPPORTED
;
519 // Check next section.
521 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
524 return RETURN_SUCCESS
;
529 Retrieves information about a PE/COFF image.
531 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
532 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
533 DebugDirectoryEntryRva fields of the ImageContext structure.
534 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
535 If the PE/COFF image accessed through the ImageRead service in the ImageContext
536 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
537 If any errors occur while computing the fields of ImageContext,
538 then the error status is returned in the ImageError field of ImageContext.
539 If the image is a TE image, then SectionAlignment is set to 0.
540 The ImageRead and Handle fields of ImageContext structure must be valid prior
541 to invoking this service.
543 Caution: This function may receive untrusted input.
544 PE/COFF image is external input, so this routine will
545 also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader,
546 SizeOfHeader, Section Data Region and Security Data Region be in PE image range.
548 @param ImageContext The pointer to the image context structure that describes the PE/COFF
549 image that needs to be examined by this function.
551 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
552 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
553 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
558 PeCoffLoaderGetImageInfo (
559 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
562 RETURN_STATUS Status
;
563 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
564 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
565 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
569 UINTN DebugDirectoryEntryRva
;
570 UINTN DebugDirectoryEntryFileOffset
;
571 UINTN SectionHeaderOffset
;
572 EFI_IMAGE_SECTION_HEADER SectionHeader
;
573 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
574 UINT32 NumberOfRvaAndSizes
;
575 UINT32 TeStrippedOffset
;
577 if (ImageContext
== NULL
) {
578 return RETURN_INVALID_PARAMETER
;
583 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
585 Hdr
.Union
= &HdrData
;
586 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
587 if (RETURN_ERROR (Status
)) {
592 // Retrieve the base address of the image
594 if (!(ImageContext
->IsTeImage
)) {
595 TeStrippedOffset
= 0;
596 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
600 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
605 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
608 TeStrippedOffset
= (UINT32
)Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
609 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ TeStrippedOffset
);
613 // Initialize the alternate destination address to 0 indicating that it
614 // should not be used.
616 ImageContext
->DestinationAddress
= 0;
619 // Initialize the debug codeview pointer.
621 ImageContext
->DebugDirectoryEntryRva
= 0;
622 ImageContext
->CodeView
= NULL
;
623 ImageContext
->PdbPointer
= NULL
;
626 // Three cases with regards to relocations:
627 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
628 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
629 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
630 // has no base relocs to apply
631 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
633 // Look at the file header to determine if relocations have been stripped, and
634 // save this information in the image context for later use.
636 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
637 ImageContext
->RelocationsStripped
= TRUE
;
638 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
639 ImageContext
->RelocationsStripped
= TRUE
;
641 ImageContext
->RelocationsStripped
= FALSE
;
644 if (!(ImageContext
->IsTeImage
)) {
645 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
649 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
650 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
655 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
656 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
659 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
661 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
664 // Determine the file offset of the debug directory... This means we walk
665 // the sections to find which section contains the RVA of the debug
668 DebugDirectoryEntryFileOffset
= 0;
670 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+
672 sizeof (EFI_IMAGE_FILE_HEADER
) +
673 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
;
675 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
677 // Read section header from file
679 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
681 Status
= ImageContext
->ImageRead (
682 ImageContext
->Handle
,
687 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
688 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
689 if (Size
!= ReadSize
) {
690 Status
= RETURN_UNSUPPORTED
;
695 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
696 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
698 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
702 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
705 if (DebugDirectoryEntryFileOffset
!= 0) {
706 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
708 // Read next debug directory entry
710 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
712 Status
= ImageContext
->ImageRead (
713 ImageContext
->Handle
,
714 DebugDirectoryEntryFileOffset
+ Index
,
718 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
719 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
720 if (Size
!= ReadSize
) {
721 Status
= RETURN_UNSUPPORTED
;
727 // From PeCoff spec, when DebugEntry.RVA == 0 means this debug info will not load into memory.
728 // Here we will always load EFI_IMAGE_DEBUG_TYPE_CODEVIEW type debug info. so need adjust the
729 // ImageContext->ImageSize when DebugEntry.RVA == 0.
731 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
732 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
733 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
734 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
737 return RETURN_SUCCESS
;
744 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
745 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
746 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
748 DebugDirectoryEntryFileOffset
= 0;
750 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
752 // Read section header from file
754 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
756 Status
= ImageContext
->ImageRead (
757 ImageContext
->Handle
,
762 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
763 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
764 if (Size
!= ReadSize
) {
765 Status
= RETURN_UNSUPPORTED
;
770 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
771 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
772 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
773 SectionHeader
.VirtualAddress
+
774 SectionHeader
.PointerToRawData
-
778 // File offset of the debug directory was found, if this is not the last
779 // section, then skip to the last section for calculating the image size.
781 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
782 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
783 Index
= Hdr
.Te
->NumberOfSections
- 1;
789 // In Te image header there is not a field to describe the ImageSize.
790 // Actually, the ImageSize equals the RVA plus the VirtualSize of
791 // the last section mapped into memory (Must be rounded up to
792 // a multiple of Section Alignment). Per the PE/COFF specification, the
793 // section headers in the Section Table must appear in order of the RVA
794 // values for the corresponding sections. So the ImageSize can be determined
795 // by the RVA and the VirtualSize of the last section header in the
798 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
799 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) - TeStrippedOffset
;
802 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
805 if (DebugDirectoryEntryFileOffset
!= 0) {
806 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
808 // Read next debug directory entry
810 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
812 Status
= ImageContext
->ImageRead (
813 ImageContext
->Handle
,
814 DebugDirectoryEntryFileOffset
+ Index
,
818 if (RETURN_ERROR (Status
) || (Size
!= ReadSize
)) {
819 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
820 if (Size
!= ReadSize
) {
821 Status
= RETURN_UNSUPPORTED
;
826 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
827 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
828 return RETURN_SUCCESS
;
834 return RETURN_SUCCESS
;
839 Converts an image address to the loaded address.
841 @param ImageContext The context of the image being loaded.
842 @param Address The address to be converted to the loaded address.
843 @param TeStrippedOffset Stripped offset for TE image.
845 @return The converted address or NULL if the address can not be converted.
849 PeCoffLoaderImageAddress (
850 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
852 IN UINTN TeStrippedOffset
856 // Make sure that Address and ImageSize is correct for the loaded image.
858 if (Address
>= ImageContext
->ImageSize
+ TeStrippedOffset
) {
859 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
863 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
- TeStrippedOffset
);
867 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
869 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
870 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
871 of ImageContext as the relocation base address. The caller must allocate the relocation
872 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
874 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
875 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
876 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
877 the ImageContext structure must be valid prior to invoking this service.
879 If ImageContext is NULL, then ASSERT().
881 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
882 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
883 prior to transferring control to a PE/COFF image that is loaded using this library.
885 @param ImageContext The pointer to the image context structure that describes the PE/COFF
886 image that is being relocated.
888 @retval RETURN_SUCCESS The PE/COFF image was relocated.
889 Extended status information is in the ImageError field of ImageContext.
890 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
891 Extended status information is in the ImageError field of ImageContext.
892 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
893 Extended status information is in the ImageError field of ImageContext.
898 PeCoffLoaderRelocateImage (
899 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
902 RETURN_STATUS Status
;
903 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
904 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
906 EFI_IMAGE_BASE_RELOCATION
*RelocBaseOrg
;
907 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
908 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
917 PHYSICAL_ADDRESS BaseAddress
;
918 UINT32 NumberOfRvaAndSizes
;
919 UINT32 TeStrippedOffset
;
921 ASSERT (ImageContext
!= NULL
);
926 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
929 // If there are no relocation entries, then we are done
931 if (ImageContext
->RelocationsStripped
) {
932 // Applies additional environment specific actions to relocate fixups
933 // to a PE/COFF image if needed
934 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
935 return RETURN_SUCCESS
;
939 // If the destination address is not 0, use that rather than the
940 // image address as the relocation target.
942 if (ImageContext
->DestinationAddress
!= 0) {
943 BaseAddress
= ImageContext
->DestinationAddress
;
945 BaseAddress
= ImageContext
->ImageAddress
;
948 if (!(ImageContext
->IsTeImage
)) {
949 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
950 TeStrippedOffset
= 0;
952 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
956 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
958 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
961 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
962 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
967 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
969 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
972 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
973 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
977 // Find the relocation block
978 // Per the PE/COFF spec, you can't assume that a given data directory
979 // is present in the image. You have to check the NumberOfRvaAndSizes in
980 // the optional header to verify a desired directory entry is there.
982 if ((NumberOfRvaAndSizes
< EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
)) {
986 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
987 TeStrippedOffset
= (UINT32
)Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
988 Adjust
= (UINT64
) (BaseAddress
- (Hdr
.Te
->ImageBase
+ TeStrippedOffset
));
990 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- TeStrippedOffset
);
994 // Find the relocation block
996 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
999 if ((RelocDir
!= NULL
) && (RelocDir
->Size
> 0)) {
1000 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
, TeStrippedOffset
);
1001 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) PeCoffLoaderImageAddress (ImageContext
,
1002 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1,
1005 if (RelocBase
== NULL
|| RelocBaseEnd
== NULL
|| RelocBaseEnd
< RelocBase
) {
1006 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1007 return RETURN_LOAD_ERROR
;
1011 // Set base and end to bypass processing below.
1013 RelocBase
= RelocBaseEnd
= NULL
;
1015 RelocBaseOrg
= RelocBase
;
1018 // If Adjust is not zero, then apply fix ups to the image
1022 // Run the relocation information and apply the fixups
1024 FixupData
= ImageContext
->FixupData
;
1025 while (RelocBase
< RelocBaseEnd
) {
1027 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1029 // Add check for RelocBase->SizeOfBlock field.
1031 if (RelocBase
->SizeOfBlock
== 0) {
1032 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1033 return RETURN_LOAD_ERROR
;
1035 if ((UINTN
)RelocBase
> MAX_ADDRESS
- RelocBase
->SizeOfBlock
) {
1036 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1037 return RETURN_LOAD_ERROR
;
1040 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1041 if ((UINTN
)RelocEnd
> (UINTN
)RelocBaseOrg
+ RelocDir
->Size
) {
1042 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1043 return RETURN_LOAD_ERROR
;
1045 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
, TeStrippedOffset
);
1046 if (FixupBase
== NULL
) {
1047 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1048 return RETURN_LOAD_ERROR
;
1052 // Run this relocation record
1054 while (Reloc
< RelocEnd
) {
1055 Fixup
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
+ (*Reloc
& 0xFFF), TeStrippedOffset
);
1056 if (Fixup
== NULL
) {
1057 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1058 return RETURN_LOAD_ERROR
;
1060 switch ((*Reloc
) >> 12) {
1061 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1064 case EFI_IMAGE_REL_BASED_HIGH
:
1065 Fixup16
= (UINT16
*) Fixup
;
1066 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1067 if (FixupData
!= NULL
) {
1068 *(UINT16
*) FixupData
= *Fixup16
;
1069 FixupData
= FixupData
+ sizeof (UINT16
);
1073 case EFI_IMAGE_REL_BASED_LOW
:
1074 Fixup16
= (UINT16
*) Fixup
;
1075 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
1076 if (FixupData
!= NULL
) {
1077 *(UINT16
*) FixupData
= *Fixup16
;
1078 FixupData
= FixupData
+ sizeof (UINT16
);
1082 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1083 Fixup32
= (UINT32
*) Fixup
;
1084 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1085 if (FixupData
!= NULL
) {
1086 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1087 *(UINT32
*)FixupData
= *Fixup32
;
1088 FixupData
= FixupData
+ sizeof (UINT32
);
1092 case EFI_IMAGE_REL_BASED_DIR64
:
1093 Fixup64
= (UINT64
*) Fixup
;
1094 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
1095 if (FixupData
!= NULL
) {
1096 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
1097 *(UINT64
*)(FixupData
) = *Fixup64
;
1098 FixupData
= FixupData
+ sizeof(UINT64
);
1104 // The common code does not handle some of the stranger IPF relocations
1105 // PeCoffLoaderRelocateImageEx () adds support for these complex fixups
1106 // on IPF and is a No-Op on other architectures.
1108 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1109 if (RETURN_ERROR (Status
)) {
1110 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1116 // Next relocation record
1124 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1126 ASSERT ((UINTN
)FixupData
<= (UINTN
)ImageContext
->FixupData
+ ImageContext
->FixupDataSize
);
1129 // Adjust the EntryPoint to match the linked-to address
1131 if (ImageContext
->DestinationAddress
!= 0) {
1132 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
1133 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
1137 // Applies additional environment specific actions to relocate fixups
1138 // to a PE/COFF image if needed
1139 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
1141 return RETURN_SUCCESS
;
1145 Loads a PE/COFF image into memory.
1147 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
1148 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
1149 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
1150 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
1151 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
1152 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
1153 fields of the ImageContext structure must be valid prior to invoking this service.
1155 If ImageContext is NULL, then ASSERT().
1157 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
1158 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
1159 prior to transferring control to a PE/COFF image that is loaded using this library.
1161 @param ImageContext The pointer to the image context structure that describes the PE/COFF
1162 image that is being loaded.
1164 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
1165 the ImageAddress and ImageSize fields of ImageContext.
1166 Extended status information is in the ImageError field of ImageContext.
1167 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
1168 Extended status information is in the ImageError field of ImageContext.
1169 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
1170 Extended status information is in the ImageError field of ImageContext.
1171 @retval RETURN_INVALID_PARAMETER The image address is invalid.
1172 Extended status information is in the ImageError field of ImageContext.
1177 PeCoffLoaderLoadImage (
1178 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1181 RETURN_STATUS Status
;
1182 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1183 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
1184 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
1185 EFI_IMAGE_SECTION_HEADER
*Section
;
1186 UINTN NumberOfSections
;
1190 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
1191 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
1193 UINT32 TempDebugEntryRva
;
1194 UINT32 NumberOfRvaAndSizes
;
1195 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
1196 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
1197 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
1198 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
1201 UINT32 TeStrippedOffset
;
1203 ASSERT (ImageContext
!= NULL
);
1208 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
1211 // Copy the provided context information into our local version, get what we
1212 // can from the original image, and then use that to make sure everything
1215 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
1217 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
1218 if (RETURN_ERROR (Status
)) {
1223 // Make sure there is enough allocated space for the image being loaded
1225 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
1226 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
1227 return RETURN_BUFFER_TOO_SMALL
;
1229 if (ImageContext
->ImageAddress
== 0) {
1231 // Image cannot be loaded into 0 address.
1233 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
1234 return RETURN_INVALID_PARAMETER
;
1237 // If there's no relocations, then make sure it's not a runtime driver,
1238 // and that it's being loaded at the linked address.
1240 if (CheckContext
.RelocationsStripped
) {
1242 // If the image does not contain relocations and it is a runtime driver
1243 // then return an error.
1245 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
1246 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
1247 return RETURN_LOAD_ERROR
;
1250 // If the image does not contain relocations, and the requested load address
1251 // is not the linked address, then return an error.
1253 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
1254 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
1255 return RETURN_INVALID_PARAMETER
;
1259 // Make sure the allocated space has the proper section alignment
1261 if (!(ImageContext
->IsTeImage
)) {
1262 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
1263 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
1264 return RETURN_INVALID_PARAMETER
;
1268 // Read the entire PE/COFF or TE header into memory
1270 if (!(ImageContext
->IsTeImage
)) {
1271 Status
= ImageContext
->ImageRead (
1272 ImageContext
->Handle
,
1274 &ImageContext
->SizeOfHeaders
,
1275 (VOID
*) (UINTN
) ImageContext
->ImageAddress
1278 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
1280 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
1281 (UINTN
)ImageContext
->ImageAddress
+
1282 ImageContext
->PeCoffHeaderOffset
+
1284 sizeof(EFI_IMAGE_FILE_HEADER
) +
1285 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
1287 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
1288 TeStrippedOffset
= 0;
1290 Status
= ImageContext
->ImageRead (
1291 ImageContext
->Handle
,
1293 &ImageContext
->SizeOfHeaders
,
1294 (void *)(UINTN
)ImageContext
->ImageAddress
1297 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
1298 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
1299 (UINTN
)ImageContext
->ImageAddress
+
1300 sizeof(EFI_TE_IMAGE_HEADER
)
1302 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
1303 TeStrippedOffset
= (UINT32
) Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
);
1306 if (RETURN_ERROR (Status
)) {
1307 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1308 return RETURN_LOAD_ERROR
;
1312 // Load each section of the image
1314 Section
= FirstSection
;
1315 for (Index
= 0; Index
< NumberOfSections
; Index
++) {
1319 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
1320 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
1321 Size
= (UINTN
) Section
->SizeOfRawData
;
1325 // Compute sections address
1327 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
, TeStrippedOffset
);
1328 End
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1, TeStrippedOffset
);
1331 // If the size of the section is non-zero and the base address or end address resolved to 0, then fail.
1333 if ((Size
> 0) && ((Base
== NULL
) || (End
== NULL
))) {
1334 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
1335 return RETURN_LOAD_ERROR
;
1338 if (Section
->SizeOfRawData
> 0) {
1339 Status
= ImageContext
->ImageRead (
1340 ImageContext
->Handle
,
1341 Section
->PointerToRawData
- TeStrippedOffset
,
1345 if (RETURN_ERROR (Status
)) {
1346 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1352 // If raw size is less then virtual size, zero fill the remaining
1355 if (Size
< Section
->Misc
.VirtualSize
) {
1356 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
1366 // Get image's entry point
1368 if (!(ImageContext
->IsTeImage
)) {
1370 // Sizes of AddressOfEntryPoint are different so we need to do this safely
1372 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1376 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1378 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
,
1385 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1387 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
,
1392 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1394 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
,
1400 // Determine the size of the fixup data
1402 // Per the PE/COFF spec, you can't assume that a given data directory
1403 // is present in the image. You have to check the NumberOfRvaAndSizes in
1404 // the optional header to verify a desired directory entry is there.
1406 if (!(ImageContext
->IsTeImage
)) {
1407 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1411 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1412 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1417 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1418 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1422 // Must use UINT64 here, because there might a case that 32bit loader to load 64bit image.
1424 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1425 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINT64
);
1427 ImageContext
->FixupDataSize
= 0;
1430 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1431 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINT64
);
1434 // Consumer must allocate a buffer for the relocation fixup log.
1435 // Only used for runtime drivers.
1437 ImageContext
->FixupData
= NULL
;
1440 // Load the Codeview information if present
1442 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1443 DebugEntry
= PeCoffLoaderImageAddress (
1445 ImageContext
->DebugDirectoryEntryRva
,
1448 if (DebugEntry
== NULL
) {
1449 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1450 return RETURN_LOAD_ERROR
;
1453 TempDebugEntryRva
= DebugEntry
->RVA
;
1454 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1456 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1457 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1459 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1463 if (TempDebugEntryRva
!= 0) {
1464 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
, TeStrippedOffset
);
1465 if (ImageContext
->CodeView
== NULL
) {
1466 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
1467 return RETURN_LOAD_ERROR
;
1470 if (DebugEntry
->RVA
== 0) {
1471 Size
= DebugEntry
->SizeOfData
;
1472 Status
= ImageContext
->ImageRead (
1473 ImageContext
->Handle
,
1474 DebugEntry
->FileOffset
- TeStrippedOffset
,
1476 ImageContext
->CodeView
1479 // Should we apply fix up to this field according to the size difference between PE and TE?
1480 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1481 // in original PE image.
1484 if (RETURN_ERROR (Status
)) {
1485 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1486 return RETURN_LOAD_ERROR
;
1489 DebugEntry
->RVA
= TempDebugEntryRva
;
1492 switch (*(UINT32
*) ImageContext
->CodeView
) {
1493 case CODEVIEW_SIGNATURE_NB10
:
1494 if (DebugEntry
->SizeOfData
< sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)) {
1495 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1496 return RETURN_UNSUPPORTED
;
1498 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1501 case CODEVIEW_SIGNATURE_RSDS
:
1502 if (DebugEntry
->SizeOfData
< sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
)) {
1503 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1504 return RETURN_UNSUPPORTED
;
1506 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1509 case CODEVIEW_SIGNATURE_MTOC
:
1510 if (DebugEntry
->SizeOfData
< sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
)) {
1511 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1512 return RETURN_UNSUPPORTED
;
1514 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
);
1524 // Get Image's HII resource section
1526 ImageContext
->HiiResourceData
= 0;
1527 if (!(ImageContext
->IsTeImage
)) {
1528 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1532 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1533 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1538 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1539 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1542 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
&& DirectoryEntry
->Size
!= 0) {
1543 Base
= PeCoffLoaderImageAddress (ImageContext
, DirectoryEntry
->VirtualAddress
, 0);
1545 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) Base
;
1546 Offset
= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
) *
1547 (ResourceDirectory
->NumberOfNamedEntries
+ ResourceDirectory
->NumberOfIdEntries
);
1548 if (Offset
> DirectoryEntry
->Size
) {
1549 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1550 return RETURN_UNSUPPORTED
;
1552 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1554 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
1555 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
1557 // Check the ResourceDirectoryEntry->u1.s.NameOffset before use it.
1559 if (ResourceDirectoryEntry
->u1
.s
.NameOffset
>= DirectoryEntry
->Size
) {
1560 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1561 return RETURN_UNSUPPORTED
;
1563 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (Base
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
1564 String
= &ResourceDirectoryString
->String
[0];
1566 if (ResourceDirectoryString
->Length
== 3 &&
1567 String
[0] == L
'H' &&
1568 String
[1] == L
'I' &&
1569 String
[2] == L
'I') {
1571 // Resource Type "HII" found
1573 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1575 // Move to next level - resource Name
1577 if (ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
>= DirectoryEntry
->Size
) {
1578 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1579 return RETURN_UNSUPPORTED
;
1581 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1582 Offset
= ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
+ sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
) +
1583 sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
) * (ResourceDirectory
->NumberOfNamedEntries
+ ResourceDirectory
->NumberOfIdEntries
);
1584 if (Offset
> DirectoryEntry
->Size
) {
1585 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1586 return RETURN_UNSUPPORTED
;
1588 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1590 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1592 // Move to next level - resource Language
1594 if (ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
>= DirectoryEntry
->Size
) {
1595 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1596 return RETURN_UNSUPPORTED
;
1598 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1599 Offset
= ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
+ sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
) +
1600 sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
) * (ResourceDirectory
->NumberOfNamedEntries
+ ResourceDirectory
->NumberOfIdEntries
);
1601 if (Offset
> DirectoryEntry
->Size
) {
1602 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1603 return RETURN_UNSUPPORTED
;
1605 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1610 // Now it ought to be resource Data
1612 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1613 if (ResourceDirectoryEntry
->u2
.OffsetToData
>= DirectoryEntry
->Size
) {
1614 ImageContext
->ImageError
= IMAGE_ERROR_UNSUPPORTED
;
1615 return RETURN_UNSUPPORTED
;
1617 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (Base
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
1618 ImageContext
->HiiResourceData
= (PHYSICAL_ADDRESS
) (UINTN
) PeCoffLoaderImageAddress (ImageContext
, ResourceDataEntry
->OffsetToData
, 0);
1623 ResourceDirectoryEntry
++;
1634 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1637 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
1638 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
1639 to the address specified by VirtualImageBase. RelocationData must be identical
1640 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
1641 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
1643 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
1644 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
1645 prior to transferring control to a PE/COFF image that is loaded using this library.
1647 @param ImageBase The base address of a PE/COFF image that has been loaded
1648 and relocated into system memory.
1649 @param VirtImageBase The request virtual address that the PE/COFF image is to
1651 @param ImageSize The size, in bytes, of the PE/COFF image.
1652 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1653 image was relocated using PeCoffLoaderRelocateImage().
1658 PeCoffLoaderRelocateImageForRuntime (
1659 IN PHYSICAL_ADDRESS ImageBase
,
1660 IN PHYSICAL_ADDRESS VirtImageBase
,
1662 IN VOID
*RelocationData
1667 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1668 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1669 UINT32 NumberOfRvaAndSizes
;
1670 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1671 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1672 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1673 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1674 EFI_IMAGE_BASE_RELOCATION
*RelocBaseOrig
;
1684 RETURN_STATUS Status
;
1685 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1687 if (RelocationData
== NULL
|| ImageBase
== 0x0 || VirtImageBase
== 0x0) {
1691 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1692 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1693 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1695 ImageContext
.ImageAddress
= ImageBase
;
1696 ImageContext
.ImageSize
= ImageSize
;
1699 // Find the image's relocate dir info
1701 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1702 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1704 // Valid DOS header so get address of PE header
1706 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1709 // No Dos header so assume image starts with PE header.
1711 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1714 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1716 // Not a valid PE image so Exit
1721 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1725 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1726 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1731 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1732 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1736 // Find the relocation block
1738 // Per the PE/COFF spec, you can't assume that a given data directory
1739 // is present in the image. You have to check the NumberOfRvaAndSizes in
1740 // the optional header to verify a desired directory entry is there.
1742 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1743 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1744 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) PeCoffLoaderImageAddress (&ImageContext
, RelocDir
->VirtualAddress
, 0);
1745 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) PeCoffLoaderImageAddress (&ImageContext
,
1746 RelocDir
->VirtualAddress
+ RelocDir
->Size
,
1751 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1758 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1760 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1764 // Run the whole relocation block. And re-fixup data that has not been
1765 // modified. The FixupData is used to see if the image has been modified
1766 // since it was relocated. This is so data sections that have been updated
1767 // by code will not be fixed up, since that would set them back to
1770 FixupData
= RelocationData
;
1771 RelocBaseOrig
= RelocBase
;
1772 while (RelocBase
< RelocBaseEnd
) {
1774 // Add check for RelocBase->SizeOfBlock field.
1776 if ((RelocBase
->SizeOfBlock
== 0) || (RelocBase
->SizeOfBlock
> RelocDir
->Size
)) {
1778 // Data invalid, cannot continue to relocate the image, just return.
1783 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1784 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1785 if ((UINTN
)RelocEnd
> (UINTN
)RelocBaseOrig
+ RelocDir
->Size
) {
1789 FixupBase
= PeCoffLoaderImageAddress (&ImageContext
, RelocBase
->VirtualAddress
, 0);
1790 if (FixupBase
== NULL
) {
1795 // Run this relocation record
1797 while (Reloc
< RelocEnd
) {
1799 Fixup
= PeCoffLoaderImageAddress (&ImageContext
, RelocBase
->VirtualAddress
+ (*Reloc
& 0xFFF), 0);
1800 if (Fixup
== NULL
) {
1803 switch ((*Reloc
) >> 12) {
1805 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1808 case EFI_IMAGE_REL_BASED_HIGH
:
1809 Fixup16
= (UINT16
*) Fixup
;
1810 if (*(UINT16
*) FixupData
== *Fixup16
) {
1811 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1814 FixupData
= FixupData
+ sizeof (UINT16
);
1817 case EFI_IMAGE_REL_BASED_LOW
:
1818 Fixup16
= (UINT16
*) Fixup
;
1819 if (*(UINT16
*) FixupData
== *Fixup16
) {
1820 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1823 FixupData
= FixupData
+ sizeof (UINT16
);
1826 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1827 Fixup32
= (UINT32
*) Fixup
;
1828 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1829 if (*(UINT32
*) FixupData
== *Fixup32
) {
1830 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1833 FixupData
= FixupData
+ sizeof (UINT32
);
1836 case EFI_IMAGE_REL_BASED_DIR64
:
1837 Fixup64
= (UINT64
*)Fixup
;
1838 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1839 if (*(UINT64
*) FixupData
== *Fixup64
) {
1840 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1843 FixupData
= FixupData
+ sizeof (UINT64
);
1848 // Only Itanium requires ConvertPeImage_Ex
1850 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1851 if (RETURN_ERROR (Status
)) {
1856 // Next relocation record
1863 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1870 Reads contents of a PE/COFF image from a buffer in system memory.
1872 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1873 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1874 This function reads contents of the PE/COFF image that starts at the system memory
1875 address specified by FileHandle. The read operation copies ReadSize bytes from the
1876 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1877 The size of the buffer actually read is returned in ReadSize.
1879 The caller must make sure the FileOffset and ReadSize within the file scope.
1881 If FileHandle is NULL, then ASSERT().
1882 If ReadSize is NULL, then ASSERT().
1883 If Buffer is NULL, then ASSERT().
1885 @param FileHandle The pointer to base of the input stream
1886 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1887 @param ReadSize On input, the size in bytes of the requested read operation.
1888 On output, the number of bytes actually read.
1889 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1891 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1896 PeCoffLoaderImageReadFromMemory (
1897 IN VOID
*FileHandle
,
1898 IN UINTN FileOffset
,
1899 IN OUT UINTN
*ReadSize
,
1903 ASSERT (ReadSize
!= NULL
);
1904 ASSERT (FileHandle
!= NULL
);
1905 ASSERT (Buffer
!= NULL
);
1907 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1908 return RETURN_SUCCESS
;
1912 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1913 Releases any environment specific resources that were allocated when the image
1914 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1916 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1917 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1918 this function can simply return RETURN_SUCCESS.
1920 If ImageContext is NULL, then ASSERT().
1922 @param ImageContext The pointer to the image context structure that describes the PE/COFF
1923 image to be unloaded.
1925 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1929 PeCoffLoaderUnloadImage (
1930 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1934 // Applies additional environment specific actions to unload a
1935 // PE/COFF image if needed
1937 PeCoffLoaderUnloadImageExtraAction (ImageContext
);
1938 return RETURN_SUCCESS
;