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 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
6 Portions copyright (c) 2008 - 2009, 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.
17 #include "BasePeCoffLibInternals.h"
20 Retrieves the magic value from the PE/COFF header.
22 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
24 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
25 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
29 PeCoffLoaderGetPeHeaderMagicValue (
30 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
34 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
35 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
36 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
37 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
39 if (Hdr
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
40 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
43 // Return the magic value from the PC/COFF Optional Header
45 return Hdr
.Pe32
->OptionalHeader
.Magic
;
50 Retrieves the PE or TE Header from a PE/COFF or TE image.
51 Also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader,
52 SizeOfHeader, Section Data Region and Security Data Region be in PE image range.
54 @param ImageContext The context of the image being loaded.
55 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
57 @retval RETURN_SUCCESS The PE or TE Header is read.
58 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
62 PeCoffLoaderGetPeHeader (
63 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
64 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
68 EFI_IMAGE_DOS_HEADER DosHdr
;
71 UINT32 SectionHeaderOffset
;
74 EFI_IMAGE_SECTION_HEADER SectionHeader
;
77 // Read the DOS image header to check for its existence
79 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
80 Status
= ImageContext
->ImageRead (
86 if (RETURN_ERROR (Status
)) {
87 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
91 ImageContext
->PeCoffHeaderOffset
= 0;
92 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
94 // DOS image header is present, so read the PE header after the DOS image
97 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
101 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
102 // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
103 // determines if this is a PE32 or PE32+ image. The magic is in the same
104 // location in both images.
106 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
107 Status
= ImageContext
->ImageRead (
108 ImageContext
->Handle
,
109 ImageContext
->PeCoffHeaderOffset
,
113 if (RETURN_ERROR (Status
)) {
114 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
119 // Use Signature to figure out if we understand the image format
121 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
122 ImageContext
->IsTeImage
= TRUE
;
123 ImageContext
->Machine
= Hdr
.Te
->Machine
;
124 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
126 // For TeImage, SectionAlignment is undefined to be set to Zero
127 // ImageSize can be calculated.
129 ImageContext
->ImageSize
= 0;
130 ImageContext
->SectionAlignment
= 0;
131 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
133 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
134 ImageContext
->IsTeImage
= FALSE
;
135 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
137 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
139 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
141 // 1. Check FileHeader.SizeOfOptionalHeader filed.
143 if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
< Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
) {
144 return RETURN_UNSUPPORTED
;
148 // 2. Check the OptionalHeader.SizeOfHeaders field.
149 // This field will be use like the following mode, so just compare the result.
150 // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.
152 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1 < Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
) {
153 if (Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
< (UINT32
)((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINT8
*) &Hdr
)) {
154 return RETURN_UNSUPPORTED
;
159 // Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file
162 Status
= ImageContext
->ImageRead (
163 ImageContext
->Handle
,
164 Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- 1,
168 if (RETURN_ERROR (Status
)) {
173 // Check the EFI_IMAGE_DIRECTORY_ENTRY_SECURITY data.
174 // Read the last byte to make sure the data is in the image region.
175 // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.
177 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
< Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
) {
178 if (Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
!= 0) {
180 // Check the member data to avoid overflow.
182 if ((UINT32
) (~0) - Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
<
183 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
) {
184 return RETURN_INVALID_PARAMETER
;
188 // Read section header from file
191 Status
= ImageContext
->ImageRead (
192 ImageContext
->Handle
,
193 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
+
194 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
- 1,
198 if (RETURN_ERROR (Status
)) {
207 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
208 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
209 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
210 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
212 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
214 // 1. Check FileHeader.SizeOfOptionalHeader filed.
216 if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
< Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
) {
217 return RETURN_UNSUPPORTED
;
221 // 2. Check the OptionalHeader.SizeOfHeaders field.
222 // This field will be use like the following mode, so just compare the result.
223 // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.
225 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1 < Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
) {
226 if (Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
< (UINT32
)((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINT8
*) &Hdr
)) {
227 return RETURN_UNSUPPORTED
;
232 // Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file
235 Status
= ImageContext
->ImageRead (
236 ImageContext
->Handle
,
237 Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- 1,
241 if (RETURN_ERROR (Status
)) {
246 // Check the EFI_IMAGE_DIRECTORY_ENTRY_SECURITY data.
247 // Read the last byte to make sure the data is in the image region.
248 // The DataDirectory array begin with 1, not 0, so here use < to compare not <=.
250 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
< Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
) {
251 if (Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
!= 0) {
253 // Check the member data to avoid overflow.
255 if ((UINT32
) (~0) - Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
<
256 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
) {
257 return RETURN_INVALID_PARAMETER
;
261 // Read section header from file
264 Status
= ImageContext
->ImageRead (
265 ImageContext
->Handle
,
266 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
+
267 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
- 1,
271 if (RETURN_ERROR (Status
)) {
280 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
281 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
282 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
283 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
285 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
286 return RETURN_UNSUPPORTED
;
289 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
290 return RETURN_UNSUPPORTED
;
293 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
295 // If the PE/COFF loader does not support the image type return
296 // unsupported. This library can support lots of types of images
297 // this does not mean the user of this library can call the entry
298 // point of the image.
300 return RETURN_UNSUPPORTED
;
304 // Check each section field.
306 SectionHeaderOffset
= ImageContext
->PeCoffHeaderOffset
+ sizeof (UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
;
307 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
309 // Read section header from file
311 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
312 Status
= ImageContext
->ImageRead (
313 ImageContext
->Handle
,
318 if (RETURN_ERROR (Status
)) {
322 if (SectionHeader
.SizeOfRawData
> 0) {
324 // Check the member data to avoid overflow.
326 if ((UINT32
) (~0) - SectionHeader
.PointerToRawData
< SectionHeader
.SizeOfRawData
) {
327 return RETURN_INVALID_PARAMETER
;
331 // Base on the ImageRead function to check the section data field.
332 // Read the last byte to make sure the data is in the image region.
335 Status
= ImageContext
->ImageRead (
336 ImageContext
->Handle
,
337 SectionHeader
.PointerToRawData
+ SectionHeader
.SizeOfRawData
- 1,
341 if (RETURN_ERROR (Status
)) {
347 // Check next section.
349 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
352 return RETURN_SUCCESS
;
357 Retrieves information about a PE/COFF image.
359 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
360 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
361 DebugDirectoryEntryRva fields of the ImageContext structure.
362 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
363 If the PE/COFF image accessed through the ImageRead service in the ImageContext
364 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
365 If any errors occur while computing the fields of ImageContext,
366 then the error status is returned in the ImageError field of ImageContext.
367 If the image is a TE image, then SectionAlignment is set to 0.
368 The ImageRead and Handle fields of ImageContext structure must be valid prior
369 to invoking this service.
371 Also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader,
372 SizeOfHeader, Section Data Region and Security Data Region be in PE image range.
374 @param ImageContext The pointer to the image context structure that describes the PE/COFF
375 image that needs to be examined by this function.
377 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
378 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
379 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
384 PeCoffLoaderGetImageInfo (
385 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
388 RETURN_STATUS Status
;
389 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
390 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
391 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
394 UINTN DebugDirectoryEntryRva
;
395 UINTN DebugDirectoryEntryFileOffset
;
396 UINTN SectionHeaderOffset
;
397 EFI_IMAGE_SECTION_HEADER SectionHeader
;
398 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
399 UINT32 NumberOfRvaAndSizes
;
402 if (ImageContext
== NULL
) {
403 return RETURN_INVALID_PARAMETER
;
408 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
410 Hdr
.Union
= &HdrData
;
411 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
412 if (RETURN_ERROR (Status
)) {
416 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
419 // Retrieve the base address of the image
421 if (!(ImageContext
->IsTeImage
)) {
422 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
426 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
431 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
434 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
438 // Initialize the alternate destination address to 0 indicating that it
439 // should not be used.
441 ImageContext
->DestinationAddress
= 0;
444 // Initialize the debug codeview pointer.
446 ImageContext
->DebugDirectoryEntryRva
= 0;
447 ImageContext
->CodeView
= NULL
;
448 ImageContext
->PdbPointer
= NULL
;
451 // Three cases with regards to relocations:
452 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
453 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
454 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
455 // has no base relocs to apply
456 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
458 // Look at the file header to determine if relocations have been stripped, and
459 // save this information in the image context for later use.
461 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
462 ImageContext
->RelocationsStripped
= TRUE
;
463 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
464 ImageContext
->RelocationsStripped
= TRUE
;
466 ImageContext
->RelocationsStripped
= FALSE
;
470 // TE Image Relocation Data Directory Entry size is non-zero, but the Relocation Data Directory Virtual Address is zero.
471 // This case is not a valid TE image.
473 if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
!= 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
474 return RETURN_INVALID_PARAMETER
;
477 if (!(ImageContext
->IsTeImage
)) {
478 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
482 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
483 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
488 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
489 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
492 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
494 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
497 // Determine the file offset of the debug directory... This means we walk
498 // the sections to find which section contains the RVA of the debug
501 DebugDirectoryEntryFileOffset
= 0;
503 SectionHeaderOffset
= (UINTN
)(
504 ImageContext
->PeCoffHeaderOffset
+
506 sizeof (EFI_IMAGE_FILE_HEADER
) +
507 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
510 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
512 // Read section header from file
514 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
515 Status
= ImageContext
->ImageRead (
516 ImageContext
->Handle
,
521 if (RETURN_ERROR (Status
)) {
522 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
526 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
527 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
529 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
533 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
536 if (DebugDirectoryEntryFileOffset
!= 0) {
537 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
539 // Read next debug directory entry
541 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
542 Status
= ImageContext
->ImageRead (
543 ImageContext
->Handle
,
544 DebugDirectoryEntryFileOffset
+ Index
,
548 if (RETURN_ERROR (Status
)) {
549 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
552 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
553 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
554 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
555 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
558 return RETURN_SUCCESS
;
565 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
566 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
567 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
569 DebugDirectoryEntryFileOffset
= 0;
571 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
573 // Read section header from file
575 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
576 Status
= ImageContext
->ImageRead (
577 ImageContext
->Handle
,
582 if (RETURN_ERROR (Status
)) {
583 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
587 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
588 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
589 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
590 SectionHeader
.VirtualAddress
+
591 SectionHeader
.PointerToRawData
+
592 sizeof (EFI_TE_IMAGE_HEADER
) -
593 Hdr
.Te
->StrippedSize
;
596 // File offset of the debug directory was found, if this is not the last
597 // section, then skip to the last section for calculating the image size.
599 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
600 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
601 Index
= Hdr
.Te
->NumberOfSections
- 1;
607 // In Te image header there is not a field to describe the ImageSize.
608 // Actually, the ImageSize equals the RVA plus the VirtualSize of
609 // the last section mapped into memory (Must be rounded up to
610 // a multiple of Section Alignment). Per the PE/COFF specification, the
611 // section headers in the Section Table must appear in order of the RVA
612 // values for the corresponding sections. So the ImageSize can be determined
613 // by the RVA and the VirtualSize of the last section header in the
616 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
617 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
620 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
623 if (DebugDirectoryEntryFileOffset
!= 0) {
624 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
626 // Read next debug directory entry
628 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
629 Status
= ImageContext
->ImageRead (
630 ImageContext
->Handle
,
631 DebugDirectoryEntryFileOffset
+ Index
,
635 if (RETURN_ERROR (Status
)) {
636 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
640 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
641 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
642 return RETURN_SUCCESS
;
648 return RETURN_SUCCESS
;
653 Converts an image address to the loaded address.
655 @param ImageContext The context of the image being loaded.
656 @param Address The relative virtual address to be converted to the loaded address.
658 @return The converted address or NULL if the address can not be converted.
662 PeCoffLoaderImageAddress (
663 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
668 // Make sure that Address and ImageSize is correct for the loaded image.
670 if (Address
>= ImageContext
->ImageSize
) {
671 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
675 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
679 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
681 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
682 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
683 of ImageContext as the relocation base address. The caller must allocate the relocation
684 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
686 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
687 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
688 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
689 the ImageContext structure must be valid prior to invoking this service.
691 If ImageContext is NULL, then ASSERT().
693 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
694 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
695 prior to transferring control to a PE/COFF image that is loaded using this library.
697 @param ImageContext The pointer to the image context structure that describes the PE/COFF
698 image that is being relocated.
700 @retval RETURN_SUCCESS The PE/COFF image was relocated.
701 Extended status information is in the ImageError field of ImageContext.
702 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
703 Extended status information is in the ImageError field of ImageContext.
704 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
705 Extended status information is in the ImageError field of ImageContext.
710 PeCoffLoaderRelocateImage (
711 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
714 RETURN_STATUS Status
;
715 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
716 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
718 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
719 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
728 PHYSICAL_ADDRESS BaseAddress
;
729 UINT32 NumberOfRvaAndSizes
;
732 ASSERT (ImageContext
!= NULL
);
737 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
740 // If there are no relocation entries, then we are done
742 if (ImageContext
->RelocationsStripped
) {
743 // Applies additional environment specific actions to relocate fixups
744 // to a PE/COFF image if needed
745 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
746 return RETURN_SUCCESS
;
750 // If the destination address is not 0, use that rather than the
751 // image address as the relocation target.
753 if (ImageContext
->DestinationAddress
!= 0) {
754 BaseAddress
= ImageContext
->DestinationAddress
;
756 BaseAddress
= ImageContext
->ImageAddress
;
759 if (!(ImageContext
->IsTeImage
)) {
760 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
762 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
764 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
768 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
770 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
773 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
774 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
779 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
781 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
784 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
785 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
789 // Find the relocation block
790 // Per the PE/COFF spec, you can't assume that a given data directory
791 // is present in the image. You have to check the NumberOfRvaAndSizes in
792 // the optional header to verify a desired directory entry is there.
795 if ((NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) && (RelocDir
->Size
> 0)) {
796 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
797 RelocBaseEnd
= PeCoffLoaderImageAddress (
799 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
801 if (RelocBase
== NULL
|| RelocBaseEnd
== NULL
) {
802 return RETURN_LOAD_ERROR
;
806 // Set base and end to bypass processing below.
808 RelocBase
= RelocBaseEnd
= NULL
;
811 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
812 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
814 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
818 // Find the relocation block
820 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
821 if (RelocDir
->Size
> 0) {
822 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
823 ImageContext
->ImageAddress
+
824 RelocDir
->VirtualAddress
+
825 sizeof(EFI_TE_IMAGE_HEADER
) -
828 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
831 // Set base and end to bypass processing below.
833 RelocBase
= RelocBaseEnd
= NULL
;
838 // If Adjust is not zero, then apply fix ups to the image
842 // Run the relocation information and apply the fixups
844 FixupData
= ImageContext
->FixupData
;
845 while (RelocBase
< RelocBaseEnd
) {
847 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
848 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
851 // Make sure RelocEnd is in the Image range.
853 if ((CHAR8
*) RelocEnd
< (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
) ||
854 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+ (UINTN
)ImageContext
->ImageSize
)) {
855 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
856 return RETURN_LOAD_ERROR
;
859 if (!(ImageContext
->IsTeImage
)) {
860 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
861 if (FixupBase
== NULL
) {
862 return RETURN_LOAD_ERROR
;
865 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
866 RelocBase
->VirtualAddress
+
867 sizeof(EFI_TE_IMAGE_HEADER
) -
873 // Run this relocation record
875 while (Reloc
< RelocEnd
) {
877 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
878 switch ((*Reloc
) >> 12) {
879 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
882 case EFI_IMAGE_REL_BASED_HIGH
:
883 Fixup16
= (UINT16
*) Fixup
;
884 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
885 if (FixupData
!= NULL
) {
886 *(UINT16
*) FixupData
= *Fixup16
;
887 FixupData
= FixupData
+ sizeof (UINT16
);
891 case EFI_IMAGE_REL_BASED_LOW
:
892 Fixup16
= (UINT16
*) Fixup
;
893 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
894 if (FixupData
!= NULL
) {
895 *(UINT16
*) FixupData
= *Fixup16
;
896 FixupData
= FixupData
+ sizeof (UINT16
);
900 case EFI_IMAGE_REL_BASED_HIGHLOW
:
901 Fixup32
= (UINT32
*) Fixup
;
902 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
903 if (FixupData
!= NULL
) {
904 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
905 *(UINT32
*)FixupData
= *Fixup32
;
906 FixupData
= FixupData
+ sizeof (UINT32
);
910 case EFI_IMAGE_REL_BASED_DIR64
:
911 Fixup64
= (UINT64
*) Fixup
;
912 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
913 if (FixupData
!= NULL
) {
914 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
915 *(UINT64
*)(FixupData
) = *Fixup64
;
916 FixupData
= FixupData
+ sizeof(UINT64
);
922 // The common code does not handle some of the stranger IPF relocations
923 // PeCoffLoaderRelocateImageEx () adds support for these complex fixups
924 // on IPF and is a No-Op on other architectures.
926 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
927 if (RETURN_ERROR (Status
)) {
928 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
934 // Next relocation record
942 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
946 // Adjust the EntryPoint to match the linked-to address
948 if (ImageContext
->DestinationAddress
!= 0) {
949 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
950 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
954 // Applies additional environment specific actions to relocate fixups
955 // to a PE/COFF image if needed
956 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
958 return RETURN_SUCCESS
;
962 Loads a PE/COFF image into memory.
964 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
965 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
966 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
967 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
968 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
969 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
970 fields of the ImageContext structure must be valid prior to invoking this service.
972 If ImageContext is NULL, then ASSERT().
974 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
975 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
976 prior to transferring control to a PE/COFF image that is loaded using this library.
978 @param ImageContext The pointer to the image context structure that describes the PE/COFF
979 image that is being loaded.
981 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
982 the ImageAddress and ImageSize fields of ImageContext.
983 Extended status information is in the ImageError field of ImageContext.
984 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
985 Extended status information is in the ImageError field of ImageContext.
986 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
987 Extended status information is in the ImageError field of ImageContext.
988 @retval RETURN_INVALID_PARAMETER The image address is invalid.
989 Extended status information is in the ImageError field of ImageContext.
994 PeCoffLoaderLoadImage (
995 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
998 RETURN_STATUS Status
;
999 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1000 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
1001 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
1002 EFI_IMAGE_SECTION_HEADER
*Section
;
1003 UINTN NumberOfSections
;
1008 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
1009 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
1011 UINT32 TempDebugEntryRva
;
1012 UINT32 NumberOfRvaAndSizes
;
1014 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
1015 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
1016 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
1017 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
1021 ASSERT (ImageContext
!= NULL
);
1026 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
1029 // Copy the provided context information into our local version, get what we
1030 // can from the original image, and then use that to make sure everything
1033 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
1035 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
1036 if (RETURN_ERROR (Status
)) {
1041 // Make sure there is enough allocated space for the image being loaded
1043 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
1044 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
1045 return RETURN_BUFFER_TOO_SMALL
;
1047 if (ImageContext
->ImageAddress
== 0) {
1049 // Image cannot be loaded into 0 address.
1051 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
1052 return RETURN_INVALID_PARAMETER
;
1055 // If there's no relocations, then make sure it's not a runtime driver,
1056 // and that it's being loaded at the linked address.
1058 if (CheckContext
.RelocationsStripped
) {
1060 // If the image does not contain relocations and it is a runtime driver
1061 // then return an error.
1063 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
1064 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
1065 return RETURN_LOAD_ERROR
;
1068 // If the image does not contain relocations, and the requested load address
1069 // is not the linked address, then return an error.
1071 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
1072 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
1073 return RETURN_INVALID_PARAMETER
;
1077 // Make sure the allocated space has the proper section alignment
1079 if (!(ImageContext
->IsTeImage
)) {
1080 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
1081 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
1082 return RETURN_INVALID_PARAMETER
;
1086 // Read the entire PE/COFF or TE header into memory
1088 if (!(ImageContext
->IsTeImage
)) {
1089 Status
= ImageContext
->ImageRead (
1090 ImageContext
->Handle
,
1092 &ImageContext
->SizeOfHeaders
,
1093 (VOID
*) (UINTN
) ImageContext
->ImageAddress
1096 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
1098 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
1099 (UINTN
)ImageContext
->ImageAddress
+
1100 ImageContext
->PeCoffHeaderOffset
+
1102 sizeof(EFI_IMAGE_FILE_HEADER
) +
1103 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
1105 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
1107 Status
= ImageContext
->ImageRead (
1108 ImageContext
->Handle
,
1110 &ImageContext
->SizeOfHeaders
,
1111 (void *)(UINTN
)ImageContext
->ImageAddress
1114 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
1116 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
1117 (UINTN
)ImageContext
->ImageAddress
+
1118 sizeof(EFI_TE_IMAGE_HEADER
)
1120 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
1124 if (RETURN_ERROR (Status
)) {
1125 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1126 return RETURN_LOAD_ERROR
;
1130 // Load each section of the image
1132 Section
= FirstSection
;
1133 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
1137 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
1138 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
1139 Size
= (UINTN
) Section
->SizeOfRawData
;
1143 // Compute sections address
1145 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
1146 End
= PeCoffLoaderImageAddress (
1148 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
1152 // If the size of the section is non-zero and the base address or end address resolved to 0, then fail.
1154 if ((Size
> 0) && ((Base
== NULL
) || (End
== NULL
))) {
1155 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
1156 return RETURN_LOAD_ERROR
;
1159 if (ImageContext
->IsTeImage
) {
1160 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
1161 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
1168 if (Section
->SizeOfRawData
> 0) {
1169 if (!(ImageContext
->IsTeImage
)) {
1170 Status
= ImageContext
->ImageRead (
1171 ImageContext
->Handle
,
1172 Section
->PointerToRawData
,
1177 Status
= ImageContext
->ImageRead (
1178 ImageContext
->Handle
,
1179 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
1185 if (RETURN_ERROR (Status
)) {
1186 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1192 // If raw size is less then virtual size, zero fill the remaining
1195 if (Size
< Section
->Misc
.VirtualSize
) {
1196 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
1206 // Get image's entry point
1208 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1209 if (!(ImageContext
->IsTeImage
)) {
1211 // Sizes of AddressOfEntryPoint are different so we need to do this safely
1213 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1217 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1219 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
1225 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1227 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
1231 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
1232 (UINTN
)ImageContext
->ImageAddress
+
1233 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
1234 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
1235 (UINTN
)Hdr
.Te
->StrippedSize
1240 // Determine the size of the fixup data
1242 // Per the PE/COFF spec, you can't assume that a given data directory
1243 // is present in the image. You have to check the NumberOfRvaAndSizes in
1244 // the optional header to verify a desired directory entry is there.
1246 if (!(ImageContext
->IsTeImage
)) {
1247 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1251 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1252 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1257 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1258 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1261 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1262 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1264 ImageContext
->FixupDataSize
= 0;
1267 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1268 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1271 // Consumer must allocate a buffer for the relocation fixup log.
1272 // Only used for runtime drivers.
1274 ImageContext
->FixupData
= NULL
;
1277 // Load the Codeview information if present
1279 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1280 if (!(ImageContext
->IsTeImage
)) {
1281 DebugEntry
= PeCoffLoaderImageAddress (
1283 ImageContext
->DebugDirectoryEntryRva
1286 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1287 ImageContext
->ImageAddress
+
1288 ImageContext
->DebugDirectoryEntryRva
+
1289 sizeof(EFI_TE_IMAGE_HEADER
) -
1290 Hdr
.Te
->StrippedSize
1294 if (DebugEntry
!= NULL
) {
1295 TempDebugEntryRva
= DebugEntry
->RVA
;
1296 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1298 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1299 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1301 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1305 if (TempDebugEntryRva
!= 0) {
1306 if (!(ImageContext
->IsTeImage
)) {
1307 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1309 ImageContext
->CodeView
= (VOID
*)(
1310 (UINTN
)ImageContext
->ImageAddress
+
1311 (UINTN
)TempDebugEntryRva
+
1312 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1313 (UINTN
) Hdr
.Te
->StrippedSize
1317 if (ImageContext
->CodeView
== NULL
) {
1318 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1319 return RETURN_LOAD_ERROR
;
1322 if (DebugEntry
->RVA
== 0) {
1323 Size
= DebugEntry
->SizeOfData
;
1324 if (!(ImageContext
->IsTeImage
)) {
1325 Status
= ImageContext
->ImageRead (
1326 ImageContext
->Handle
,
1327 DebugEntry
->FileOffset
,
1329 ImageContext
->CodeView
1332 Status
= ImageContext
->ImageRead (
1333 ImageContext
->Handle
,
1334 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1336 ImageContext
->CodeView
1339 // Should we apply fix up to this field according to the size difference between PE and TE?
1340 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1341 // in original PE image.
1345 if (RETURN_ERROR (Status
)) {
1346 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1347 return RETURN_LOAD_ERROR
;
1350 DebugEntry
->RVA
= TempDebugEntryRva
;
1353 switch (*(UINT32
*) ImageContext
->CodeView
) {
1354 case CODEVIEW_SIGNATURE_NB10
:
1355 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1358 case CODEVIEW_SIGNATURE_RSDS
:
1359 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1362 case CODEVIEW_SIGNATURE_MTOC
:
1363 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
);
1374 // Get Image's HII resource section
1376 ImageContext
->HiiResourceData
= 0;
1377 if (!(ImageContext
->IsTeImage
)) {
1378 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1382 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1387 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1390 if (DirectoryEntry
->Size
!= 0) {
1391 Base
= PeCoffLoaderImageAddress (ImageContext
, DirectoryEntry
->VirtualAddress
);
1393 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) Base
;
1394 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1396 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
1397 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
1398 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (Base
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
1399 String
= &ResourceDirectoryString
->String
[0];
1401 if (ResourceDirectoryString
->Length
== 3 &&
1402 String
[0] == L
'H' &&
1403 String
[1] == L
'I' &&
1404 String
[2] == L
'I') {
1406 // Resource Type "HII" found
1408 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1410 // Move to next level - resource Name
1412 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1413 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1415 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1417 // Move to next level - resource Language
1419 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1420 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1425 // Now it ought to be resource Data
1427 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1428 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (Base
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
1429 ImageContext
->HiiResourceData
= (PHYSICAL_ADDRESS
) (UINTN
) PeCoffLoaderImageAddress (ImageContext
, ResourceDataEntry
->OffsetToData
);
1434 ResourceDirectoryEntry
++;
1445 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1448 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
1449 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
1450 to the address specified by VirtualImageBase. RelocationData must be identical
1451 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
1452 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
1454 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
1455 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
1456 prior to transferring control to a PE/COFF image that is loaded using this library.
1458 @param ImageBase The base address of a PE/COFF image that has been loaded
1459 and relocated into system memory.
1460 @param VirtImageBase The request virtual address that the PE/COFF image is to
1462 @param ImageSize The size, in bytes, of the PE/COFF image.
1463 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1464 image was relocated using PeCoffLoaderRelocateImage().
1469 PeCoffLoaderRelocateImageForRuntime (
1470 IN PHYSICAL_ADDRESS ImageBase
,
1471 IN PHYSICAL_ADDRESS VirtImageBase
,
1473 IN VOID
*RelocationData
1478 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1479 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1480 UINT32 NumberOfRvaAndSizes
;
1481 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1482 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1483 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1484 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1494 RETURN_STATUS Status
;
1497 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1498 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1499 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1502 // Find the image's relocate dir info
1504 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1505 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1507 // Valid DOS header so get address of PE header
1509 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1512 // No Dos header so assume image starts with PE header.
1514 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1517 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1519 // Not a valid PE image so Exit
1524 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1526 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1530 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1531 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1536 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1537 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1541 // Find the relocation block
1543 // Per the PE/COFF spec, you can't assume that a given data directory
1544 // is present in the image. You have to check the NumberOfRvaAndSizes in
1545 // the optional header to verify a desired directory entry is there.
1547 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1548 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1549 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1550 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1553 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1560 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1562 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1565 // Run the whole relocation block. And re-fixup data that has not been
1566 // modified. The FixupData is used to see if the image has been modified
1567 // since it was relocated. This is so data sections that have been updated
1568 // by code will not be fixed up, since that would set them back to
1571 FixupData
= RelocationData
;
1572 while (RelocBase
< RelocBaseEnd
) {
1574 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1575 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1576 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1579 // Run this relocation record
1581 while (Reloc
< RelocEnd
) {
1583 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1584 switch ((*Reloc
) >> 12) {
1586 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1589 case EFI_IMAGE_REL_BASED_HIGH
:
1590 Fixup16
= (UINT16
*) Fixup
;
1591 if (*(UINT16
*) FixupData
== *Fixup16
) {
1592 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1595 FixupData
= FixupData
+ sizeof (UINT16
);
1598 case EFI_IMAGE_REL_BASED_LOW
:
1599 Fixup16
= (UINT16
*) Fixup
;
1600 if (*(UINT16
*) FixupData
== *Fixup16
) {
1601 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1604 FixupData
= FixupData
+ sizeof (UINT16
);
1607 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1608 Fixup32
= (UINT32
*) Fixup
;
1609 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1610 if (*(UINT32
*) FixupData
== *Fixup32
) {
1611 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1614 FixupData
= FixupData
+ sizeof (UINT32
);
1617 case EFI_IMAGE_REL_BASED_DIR64
:
1618 Fixup64
= (UINT64
*)Fixup
;
1619 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1620 if (*(UINT64
*) FixupData
== *Fixup64
) {
1621 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1624 FixupData
= FixupData
+ sizeof (UINT64
);
1627 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1629 // Not valid Relocation type for UEFI image, ASSERT
1636 // Only Itanium requires ConvertPeImage_Ex
1638 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1639 if (RETURN_ERROR (Status
)) {
1644 // Next relocation record
1651 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1657 Reads contents of a PE/COFF image from a buffer in system memory.
1659 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1660 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1661 This function reads contents of the PE/COFF image that starts at the system memory
1662 address specified by FileHandle. The read operation copies ReadSize bytes from the
1663 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1664 The size of the buffer actually read is returned in ReadSize.
1666 If FileHandle is NULL, then ASSERT().
1667 If ReadSize is NULL, then ASSERT().
1668 If Buffer is NULL, then ASSERT().
1670 @param FileHandle The pointer to base of the input stream
1671 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1672 @param ReadSize On input, the size in bytes of the requested read operation.
1673 On output, the number of bytes actually read.
1674 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1676 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1681 PeCoffLoaderImageReadFromMemory (
1682 IN VOID
*FileHandle
,
1683 IN UINTN FileOffset
,
1684 IN OUT UINTN
*ReadSize
,
1688 ASSERT (ReadSize
!= NULL
);
1689 ASSERT (FileHandle
!= NULL
);
1690 ASSERT (Buffer
!= NULL
);
1692 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1693 return RETURN_SUCCESS
;
1697 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1698 Releases any environment specific resources that were allocated when the image
1699 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1701 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1702 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1703 this function can simply return RETURN_SUCCESS.
1705 If ImageContext is NULL, then ASSERT().
1707 @param ImageContext The pointer to the image context structure that describes the PE/COFF
1708 image to be unloaded.
1710 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1714 PeCoffLoaderUnloadImage (
1715 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1719 // Applies additional environment specific actions to unload a
1720 // PE/COFF image if needed
1722 PeCoffLoaderUnloadImageExtraAction (ImageContext
);
1723 return RETURN_SUCCESS
;