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.
52 @param ImageContext The context of the image being loaded.
53 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
55 @retval RETURN_SUCCESS The PE or TE Header is read.
56 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
60 PeCoffLoaderGetPeHeader (
61 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
62 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
66 EFI_IMAGE_DOS_HEADER DosHdr
;
71 // Read the DOS image header to check for its existence
73 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
74 Status
= ImageContext
->ImageRead (
80 if (RETURN_ERROR (Status
)) {
81 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
85 ImageContext
->PeCoffHeaderOffset
= 0;
86 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
88 // DOS image header is present, so read the PE header after the DOS image
91 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
95 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
96 // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
97 // determines if this is a PE32 or PE32+ image. The magic is in the same
98 // location in both images.
100 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
101 Status
= ImageContext
->ImageRead (
102 ImageContext
->Handle
,
103 ImageContext
->PeCoffHeaderOffset
,
107 if (RETURN_ERROR (Status
)) {
108 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
113 // Use Signature to figure out if we understand the image format
115 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
116 ImageContext
->IsTeImage
= TRUE
;
117 ImageContext
->Machine
= Hdr
.Te
->Machine
;
118 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
120 // For TeImage, SectionAlignment is undefined to be set to Zero
121 // ImageSize can be calculated.
123 ImageContext
->ImageSize
= 0;
124 ImageContext
->SectionAlignment
= 0;
125 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
127 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
128 ImageContext
->IsTeImage
= FALSE
;
129 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
131 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
133 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
137 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
138 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
139 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
140 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
142 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
146 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
147 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
148 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
149 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
151 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
152 return RETURN_UNSUPPORTED
;
155 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
156 return RETURN_UNSUPPORTED
;
159 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
161 // If the PE/COFF loader does not support the image type return
162 // unsupported. This library can support lots of types of images
163 // this does not mean the user of this library can call the entry
164 // point of the image.
166 return RETURN_UNSUPPORTED
;
169 return RETURN_SUCCESS
;
174 Retrieves information about a PE/COFF image.
176 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
177 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
178 DebugDirectoryEntryRva fields of the ImageContext structure.
179 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
180 If the PE/COFF image accessed through the ImageRead service in the ImageContext
181 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
182 If any errors occur while computing the fields of ImageContext,
183 then the error status is returned in the ImageError field of ImageContext.
184 If the image is a TE image, then SectionAlignment is set to 0.
185 The ImageRead and Handle fields of ImageContext structure must be valid prior
186 to invoking this service.
188 @param ImageContext The pointer to the image context structure that describes the PE/COFF
189 image that needs to be examined by this function.
191 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
192 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
193 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
198 PeCoffLoaderGetImageInfo (
199 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
202 RETURN_STATUS Status
;
203 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
204 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
205 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
208 UINTN DebugDirectoryEntryRva
;
209 UINTN DebugDirectoryEntryFileOffset
;
210 UINTN SectionHeaderOffset
;
211 EFI_IMAGE_SECTION_HEADER SectionHeader
;
212 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
213 UINT32 NumberOfRvaAndSizes
;
216 if (ImageContext
== NULL
) {
217 return RETURN_INVALID_PARAMETER
;
222 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
224 Hdr
.Union
= &HdrData
;
225 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
226 if (RETURN_ERROR (Status
)) {
230 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
233 // Retrieve the base address of the image
235 if (!(ImageContext
->IsTeImage
)) {
236 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
240 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
245 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
248 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
252 // Initialize the alternate destination address to 0 indicating that it
253 // should not be used.
255 ImageContext
->DestinationAddress
= 0;
258 // Initialize the debug codeview pointer.
260 ImageContext
->DebugDirectoryEntryRva
= 0;
261 ImageContext
->CodeView
= NULL
;
262 ImageContext
->PdbPointer
= NULL
;
265 // Three cases with regards to relocations:
266 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
267 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
268 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
269 // has no base relocs to apply
270 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
272 // Look at the file header to determine if relocations have been stripped, and
273 // save this information in the image context for later use.
275 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
276 ImageContext
->RelocationsStripped
= TRUE
;
277 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
278 ImageContext
->RelocationsStripped
= TRUE
;
280 ImageContext
->RelocationsStripped
= FALSE
;
284 // TE Image Relocation Data Directory Entry size is non-zero, but the Relocation Data Directory Virtual Address is zero.
285 // This case is not a valid TE image.
287 if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
!= 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
288 return RETURN_INVALID_PARAMETER
;
291 if (!(ImageContext
->IsTeImage
)) {
292 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
296 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
297 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
302 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
303 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
306 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
308 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
311 // Determine the file offset of the debug directory... This means we walk
312 // the sections to find which section contains the RVA of the debug
315 DebugDirectoryEntryFileOffset
= 0;
317 SectionHeaderOffset
= (UINTN
)(
318 ImageContext
->PeCoffHeaderOffset
+
320 sizeof (EFI_IMAGE_FILE_HEADER
) +
321 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
324 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
326 // Read section header from file
328 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
329 Status
= ImageContext
->ImageRead (
330 ImageContext
->Handle
,
335 if (RETURN_ERROR (Status
)) {
336 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
340 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
341 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
343 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
347 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
350 if (DebugDirectoryEntryFileOffset
!= 0) {
351 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
353 // Read next debug directory entry
355 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
356 Status
= ImageContext
->ImageRead (
357 ImageContext
->Handle
,
358 DebugDirectoryEntryFileOffset
+ Index
,
362 if (RETURN_ERROR (Status
)) {
363 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
366 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
367 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
368 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
369 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
372 return RETURN_SUCCESS
;
379 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
380 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
381 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
383 DebugDirectoryEntryFileOffset
= 0;
385 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
387 // Read section header from file
389 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
390 Status
= ImageContext
->ImageRead (
391 ImageContext
->Handle
,
396 if (RETURN_ERROR (Status
)) {
397 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
401 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
402 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
403 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
404 SectionHeader
.VirtualAddress
+
405 SectionHeader
.PointerToRawData
+
406 sizeof (EFI_TE_IMAGE_HEADER
) -
407 Hdr
.Te
->StrippedSize
;
410 // File offset of the debug directory was found, if this is not the last
411 // section, then skip to the last section for calculating the image size.
413 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
414 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
415 Index
= Hdr
.Te
->NumberOfSections
- 1;
421 // In Te image header there is not a field to describe the ImageSize.
422 // Actually, the ImageSize equals the RVA plus the VirtualSize of
423 // the last section mapped into memory (Must be rounded up to
424 // a multiple of Section Alignment). Per the PE/COFF specification, the
425 // section headers in the Section Table must appear in order of the RVA
426 // values for the corresponding sections. So the ImageSize can be determined
427 // by the RVA and the VirtualSize of the last section header in the
430 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
431 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
434 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
437 if (DebugDirectoryEntryFileOffset
!= 0) {
438 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
440 // Read next debug directory entry
442 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
443 Status
= ImageContext
->ImageRead (
444 ImageContext
->Handle
,
445 DebugDirectoryEntryFileOffset
+ Index
,
449 if (RETURN_ERROR (Status
)) {
450 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
454 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
455 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
456 return RETURN_SUCCESS
;
462 return RETURN_SUCCESS
;
467 Converts an image address to the loaded address.
469 @param ImageContext The context of the image being loaded.
470 @param Address The relative virtual address to be converted to the loaded address.
472 @return The converted address or NULL if the address can not be converted.
476 PeCoffLoaderImageAddress (
477 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
482 // Make sure that Address and ImageSize is correct for the loaded image.
484 if (Address
>= ImageContext
->ImageSize
) {
485 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
489 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
493 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
495 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
496 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
497 of ImageContext as the relocation base address. The caller must allocate the relocation
498 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
500 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
501 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
502 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
503 the ImageContext structure must be valid prior to invoking this service.
505 If ImageContext is NULL, then ASSERT().
507 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
508 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
509 prior to transferring control to a PE/COFF image that is loaded using this library.
511 @param ImageContext The pointer to the image context structure that describes the PE/COFF
512 image that is being relocated.
514 @retval RETURN_SUCCESS The PE/COFF image was relocated.
515 Extended status information is in the ImageError field of ImageContext.
516 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
517 Extended status information is in the ImageError field of ImageContext.
518 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
519 Extended status information is in the ImageError field of ImageContext.
524 PeCoffLoaderRelocateImage (
525 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
528 RETURN_STATUS Status
;
529 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
530 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
532 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
533 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
542 PHYSICAL_ADDRESS BaseAddress
;
543 UINT32 NumberOfRvaAndSizes
;
546 ASSERT (ImageContext
!= NULL
);
551 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
554 // If there are no relocation entries, then we are done
556 if (ImageContext
->RelocationsStripped
) {
557 // Applies additional environment specific actions to relocate fixups
558 // to a PE/COFF image if needed
559 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
560 return RETURN_SUCCESS
;
564 // If the destination address is not 0, use that rather than the
565 // image address as the relocation target.
567 if (ImageContext
->DestinationAddress
!= 0) {
568 BaseAddress
= ImageContext
->DestinationAddress
;
570 BaseAddress
= ImageContext
->ImageAddress
;
573 if (!(ImageContext
->IsTeImage
)) {
574 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
576 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
578 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
582 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
584 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
587 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
588 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
593 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
595 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
598 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
599 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
603 // Find the relocation block
604 // Per the PE/COFF spec, you can't assume that a given data directory
605 // is present in the image. You have to check the NumberOfRvaAndSizes in
606 // the optional header to verify a desired directory entry is there.
609 if ((NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) && (RelocDir
->Size
> 0)) {
610 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
611 RelocBaseEnd
= PeCoffLoaderImageAddress (
613 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
615 if (RelocBase
== NULL
|| RelocBaseEnd
== NULL
) {
616 return RETURN_LOAD_ERROR
;
620 // Set base and end to bypass processing below.
622 RelocBase
= RelocBaseEnd
= NULL
;
625 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
626 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
628 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
632 // Find the relocation block
634 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
635 if (RelocDir
->Size
> 0) {
636 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
637 ImageContext
->ImageAddress
+
638 RelocDir
->VirtualAddress
+
639 sizeof(EFI_TE_IMAGE_HEADER
) -
642 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
645 // Set base and end to bypass processing below.
647 RelocBase
= RelocBaseEnd
= NULL
;
652 // If Adjust is not zero, then apply fix ups to the image
656 // Run the relocation information and apply the fixups
658 FixupData
= ImageContext
->FixupData
;
659 while (RelocBase
< RelocBaseEnd
) {
661 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
662 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
665 // Make sure RelocEnd is in the Image range.
667 if ((CHAR8
*) RelocEnd
< (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
) ||
668 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+ (UINTN
)ImageContext
->ImageSize
)) {
669 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
670 return RETURN_LOAD_ERROR
;
673 if (!(ImageContext
->IsTeImage
)) {
674 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
675 if (FixupBase
== NULL
) {
676 return RETURN_LOAD_ERROR
;
679 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
680 RelocBase
->VirtualAddress
+
681 sizeof(EFI_TE_IMAGE_HEADER
) -
687 // Run this relocation record
689 while (Reloc
< RelocEnd
) {
691 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
692 switch ((*Reloc
) >> 12) {
693 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
696 case EFI_IMAGE_REL_BASED_HIGH
:
697 Fixup16
= (UINT16
*) Fixup
;
698 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
699 if (FixupData
!= NULL
) {
700 *(UINT16
*) FixupData
= *Fixup16
;
701 FixupData
= FixupData
+ sizeof (UINT16
);
705 case EFI_IMAGE_REL_BASED_LOW
:
706 Fixup16
= (UINT16
*) Fixup
;
707 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
708 if (FixupData
!= NULL
) {
709 *(UINT16
*) FixupData
= *Fixup16
;
710 FixupData
= FixupData
+ sizeof (UINT16
);
714 case EFI_IMAGE_REL_BASED_HIGHLOW
:
715 Fixup32
= (UINT32
*) Fixup
;
716 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
717 if (FixupData
!= NULL
) {
718 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
719 *(UINT32
*)FixupData
= *Fixup32
;
720 FixupData
= FixupData
+ sizeof (UINT32
);
724 case EFI_IMAGE_REL_BASED_DIR64
:
725 Fixup64
= (UINT64
*) Fixup
;
726 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
727 if (FixupData
!= NULL
) {
728 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
729 *(UINT64
*)(FixupData
) = *Fixup64
;
730 FixupData
= FixupData
+ sizeof(UINT64
);
736 // The common code does not handle some of the stranger IPF relocations
737 // PeCoffLoaderRelocateImageEx () adds support for these complex fixups
738 // on IPF and is a No-Op on other architectures.
740 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
741 if (RETURN_ERROR (Status
)) {
742 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
748 // Next relocation record
756 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
760 // Adjust the EntryPoint to match the linked-to address
762 if (ImageContext
->DestinationAddress
!= 0) {
763 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
764 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
768 // Applies additional environment specific actions to relocate fixups
769 // to a PE/COFF image if needed
770 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
772 return RETURN_SUCCESS
;
776 Loads a PE/COFF image into memory.
778 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
779 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
780 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
781 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
782 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
783 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
784 fields of the ImageContext structure must be valid prior to invoking this service.
786 If ImageContext is NULL, then ASSERT().
788 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
789 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
790 prior to transferring control to a PE/COFF image that is loaded using this library.
792 @param ImageContext The pointer to the image context structure that describes the PE/COFF
793 image that is being loaded.
795 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
796 the ImageAddress and ImageSize fields of ImageContext.
797 Extended status information is in the ImageError field of ImageContext.
798 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
799 Extended status information is in the ImageError field of ImageContext.
800 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
801 Extended status information is in the ImageError field of ImageContext.
802 @retval RETURN_INVALID_PARAMETER The image address is invalid.
803 Extended status information is in the ImageError field of ImageContext.
808 PeCoffLoaderLoadImage (
809 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
812 RETURN_STATUS Status
;
813 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
814 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
815 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
816 EFI_IMAGE_SECTION_HEADER
*Section
;
817 UINTN NumberOfSections
;
822 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
823 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
825 UINT32 TempDebugEntryRva
;
826 UINT32 NumberOfRvaAndSizes
;
828 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
829 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
830 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
831 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
835 ASSERT (ImageContext
!= NULL
);
840 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
843 // Copy the provided context information into our local version, get what we
844 // can from the original image, and then use that to make sure everything
847 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
849 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
850 if (RETURN_ERROR (Status
)) {
855 // Make sure there is enough allocated space for the image being loaded
857 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
858 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
859 return RETURN_BUFFER_TOO_SMALL
;
861 if (ImageContext
->ImageAddress
== 0) {
863 // Image cannot be loaded into 0 address.
865 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
866 return RETURN_INVALID_PARAMETER
;
869 // If there's no relocations, then make sure it's not a runtime driver,
870 // and that it's being loaded at the linked address.
872 if (CheckContext
.RelocationsStripped
) {
874 // If the image does not contain relocations and it is a runtime driver
875 // then return an error.
877 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
878 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
879 return RETURN_LOAD_ERROR
;
882 // If the image does not contain relocations, and the requested load address
883 // is not the linked address, then return an error.
885 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
886 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
887 return RETURN_INVALID_PARAMETER
;
891 // Make sure the allocated space has the proper section alignment
893 if (!(ImageContext
->IsTeImage
)) {
894 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
895 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
896 return RETURN_INVALID_PARAMETER
;
900 // Read the entire PE/COFF or TE header into memory
902 if (!(ImageContext
->IsTeImage
)) {
903 Status
= ImageContext
->ImageRead (
904 ImageContext
->Handle
,
906 &ImageContext
->SizeOfHeaders
,
907 (VOID
*) (UINTN
) ImageContext
->ImageAddress
910 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
912 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
913 (UINTN
)ImageContext
->ImageAddress
+
914 ImageContext
->PeCoffHeaderOffset
+
916 sizeof(EFI_IMAGE_FILE_HEADER
) +
917 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
919 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
921 Status
= ImageContext
->ImageRead (
922 ImageContext
->Handle
,
924 &ImageContext
->SizeOfHeaders
,
925 (void *)(UINTN
)ImageContext
->ImageAddress
928 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
930 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
931 (UINTN
)ImageContext
->ImageAddress
+
932 sizeof(EFI_TE_IMAGE_HEADER
)
934 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
938 if (RETURN_ERROR (Status
)) {
939 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
940 return RETURN_LOAD_ERROR
;
944 // Load each section of the image
946 Section
= FirstSection
;
947 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
951 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
952 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
953 Size
= (UINTN
) Section
->SizeOfRawData
;
957 // Compute sections address
959 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
960 End
= PeCoffLoaderImageAddress (
962 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
966 // If the size of the section is non-zero and the base address or end address resolved to 0, then fail.
968 if ((Size
> 0) && ((Base
== NULL
) || (End
== NULL
))) {
969 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
970 return RETURN_LOAD_ERROR
;
973 if (ImageContext
->IsTeImage
) {
974 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
975 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
982 if (Section
->SizeOfRawData
> 0) {
983 if (!(ImageContext
->IsTeImage
)) {
984 Status
= ImageContext
->ImageRead (
985 ImageContext
->Handle
,
986 Section
->PointerToRawData
,
991 Status
= ImageContext
->ImageRead (
992 ImageContext
->Handle
,
993 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
999 if (RETURN_ERROR (Status
)) {
1000 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1006 // If raw size is less then virtual size, zero fill the remaining
1009 if (Size
< Section
->Misc
.VirtualSize
) {
1010 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
1020 // Get image's entry point
1022 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1023 if (!(ImageContext
->IsTeImage
)) {
1025 // Sizes of AddressOfEntryPoint are different so we need to do this safely
1027 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1031 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1033 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
1039 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1041 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
1045 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
1046 (UINTN
)ImageContext
->ImageAddress
+
1047 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
1048 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
1049 (UINTN
)Hdr
.Te
->StrippedSize
1054 // Determine the size of the fixup data
1056 // Per the PE/COFF spec, you can't assume that a given data directory
1057 // is present in the image. You have to check the NumberOfRvaAndSizes in
1058 // the optional header to verify a desired directory entry is there.
1060 if (!(ImageContext
->IsTeImage
)) {
1061 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1065 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1066 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1071 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1072 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1075 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1076 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1078 ImageContext
->FixupDataSize
= 0;
1081 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1082 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1085 // Consumer must allocate a buffer for the relocation fixup log.
1086 // Only used for runtime drivers.
1088 ImageContext
->FixupData
= NULL
;
1091 // Load the Codeview information if present
1093 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1094 if (!(ImageContext
->IsTeImage
)) {
1095 DebugEntry
= PeCoffLoaderImageAddress (
1097 ImageContext
->DebugDirectoryEntryRva
1100 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1101 ImageContext
->ImageAddress
+
1102 ImageContext
->DebugDirectoryEntryRva
+
1103 sizeof(EFI_TE_IMAGE_HEADER
) -
1104 Hdr
.Te
->StrippedSize
1108 if (DebugEntry
!= NULL
) {
1109 TempDebugEntryRva
= DebugEntry
->RVA
;
1110 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1112 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1113 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1115 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1119 if (TempDebugEntryRva
!= 0) {
1120 if (!(ImageContext
->IsTeImage
)) {
1121 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1123 ImageContext
->CodeView
= (VOID
*)(
1124 (UINTN
)ImageContext
->ImageAddress
+
1125 (UINTN
)TempDebugEntryRva
+
1126 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1127 (UINTN
) Hdr
.Te
->StrippedSize
1131 if (ImageContext
->CodeView
== NULL
) {
1132 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1133 return RETURN_LOAD_ERROR
;
1136 if (DebugEntry
->RVA
== 0) {
1137 Size
= DebugEntry
->SizeOfData
;
1138 if (!(ImageContext
->IsTeImage
)) {
1139 Status
= ImageContext
->ImageRead (
1140 ImageContext
->Handle
,
1141 DebugEntry
->FileOffset
,
1143 ImageContext
->CodeView
1146 Status
= ImageContext
->ImageRead (
1147 ImageContext
->Handle
,
1148 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1150 ImageContext
->CodeView
1153 // Should we apply fix up to this field according to the size difference between PE and TE?
1154 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1155 // in original PE image.
1159 if (RETURN_ERROR (Status
)) {
1160 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1161 return RETURN_LOAD_ERROR
;
1164 DebugEntry
->RVA
= TempDebugEntryRva
;
1167 switch (*(UINT32
*) ImageContext
->CodeView
) {
1168 case CODEVIEW_SIGNATURE_NB10
:
1169 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1172 case CODEVIEW_SIGNATURE_RSDS
:
1173 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1176 case CODEVIEW_SIGNATURE_MTOC
:
1177 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
);
1188 // Get Image's HII resource section
1190 ImageContext
->HiiResourceData
= 0;
1191 if (!(ImageContext
->IsTeImage
)) {
1192 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1196 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1201 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1204 if (DirectoryEntry
->Size
!= 0) {
1205 Base
= PeCoffLoaderImageAddress (ImageContext
, DirectoryEntry
->VirtualAddress
);
1207 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) Base
;
1208 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1210 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
1211 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
1212 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (Base
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
1213 String
= &ResourceDirectoryString
->String
[0];
1215 if (ResourceDirectoryString
->Length
== 3 &&
1216 String
[0] == L
'H' &&
1217 String
[1] == L
'I' &&
1218 String
[2] == L
'I') {
1220 // Resource Type "HII" found
1222 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1224 // Move to next level - resource Name
1226 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1227 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1229 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1231 // Move to next level - resource Language
1233 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1234 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1239 // Now it ought to be resource Data
1241 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1242 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (Base
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
1243 ImageContext
->HiiResourceData
= (PHYSICAL_ADDRESS
) (UINTN
) PeCoffLoaderImageAddress (ImageContext
, ResourceDataEntry
->OffsetToData
);
1248 ResourceDirectoryEntry
++;
1259 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1262 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
1263 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
1264 to the address specified by VirtualImageBase. RelocationData must be identical
1265 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
1266 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
1268 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
1269 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
1270 prior to transferring control to a PE/COFF image that is loaded using this library.
1272 @param ImageBase The base address of a PE/COFF image that has been loaded
1273 and relocated into system memory.
1274 @param VirtImageBase The request virtual address that the PE/COFF image is to
1276 @param ImageSize The size, in bytes, of the PE/COFF image.
1277 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1278 image was relocated using PeCoffLoaderRelocateImage().
1283 PeCoffLoaderRelocateImageForRuntime (
1284 IN PHYSICAL_ADDRESS ImageBase
,
1285 IN PHYSICAL_ADDRESS VirtImageBase
,
1287 IN VOID
*RelocationData
1292 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1293 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1294 UINT32 NumberOfRvaAndSizes
;
1295 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1296 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1297 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1298 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1308 RETURN_STATUS Status
;
1311 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1312 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1313 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1316 // Find the image's relocate dir info
1318 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1319 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1321 // Valid DOS header so get address of PE header
1323 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1326 // No Dos header so assume image starts with PE header.
1328 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1331 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1333 // Not a valid PE image so Exit
1338 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1340 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1344 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1345 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1350 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1351 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1355 // Find the relocation block
1357 // Per the PE/COFF spec, you can't assume that a given data directory
1358 // is present in the image. You have to check the NumberOfRvaAndSizes in
1359 // the optional header to verify a desired directory entry is there.
1361 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1362 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1363 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1364 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1367 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1374 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1376 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1379 // Run the whole relocation block. And re-fixup data that has not been
1380 // modified. The FixupData is used to see if the image has been modified
1381 // since it was relocated. This is so data sections that have been updated
1382 // by code will not be fixed up, since that would set them back to
1385 FixupData
= RelocationData
;
1386 while (RelocBase
< RelocBaseEnd
) {
1388 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1389 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1390 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1393 // Run this relocation record
1395 while (Reloc
< RelocEnd
) {
1397 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1398 switch ((*Reloc
) >> 12) {
1400 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1403 case EFI_IMAGE_REL_BASED_HIGH
:
1404 Fixup16
= (UINT16
*) Fixup
;
1405 if (*(UINT16
*) FixupData
== *Fixup16
) {
1406 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1409 FixupData
= FixupData
+ sizeof (UINT16
);
1412 case EFI_IMAGE_REL_BASED_LOW
:
1413 Fixup16
= (UINT16
*) Fixup
;
1414 if (*(UINT16
*) FixupData
== *Fixup16
) {
1415 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1418 FixupData
= FixupData
+ sizeof (UINT16
);
1421 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1422 Fixup32
= (UINT32
*) Fixup
;
1423 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1424 if (*(UINT32
*) FixupData
== *Fixup32
) {
1425 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1428 FixupData
= FixupData
+ sizeof (UINT32
);
1431 case EFI_IMAGE_REL_BASED_DIR64
:
1432 Fixup64
= (UINT64
*)Fixup
;
1433 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1434 if (*(UINT64
*) FixupData
== *Fixup64
) {
1435 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1438 FixupData
= FixupData
+ sizeof (UINT64
);
1441 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1443 // Not valid Relocation type for UEFI image, ASSERT
1450 // Only Itanium requires ConvertPeImage_Ex
1452 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1453 if (RETURN_ERROR (Status
)) {
1458 // Next relocation record
1465 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1471 Reads contents of a PE/COFF image from a buffer in system memory.
1473 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1474 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1475 This function reads contents of the PE/COFF image that starts at the system memory
1476 address specified by FileHandle. The read operation copies ReadSize bytes from the
1477 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1478 The size of the buffer actually read is returned in ReadSize.
1480 If FileHandle is NULL, then ASSERT().
1481 If ReadSize is NULL, then ASSERT().
1482 If Buffer is NULL, then ASSERT().
1484 @param FileHandle The pointer to base of the input stream
1485 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1486 @param ReadSize On input, the size in bytes of the requested read operation.
1487 On output, the number of bytes actually read.
1488 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1490 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1495 PeCoffLoaderImageReadFromMemory (
1496 IN VOID
*FileHandle
,
1497 IN UINTN FileOffset
,
1498 IN OUT UINTN
*ReadSize
,
1502 ASSERT (ReadSize
!= NULL
);
1503 ASSERT (FileHandle
!= NULL
);
1504 ASSERT (Buffer
!= NULL
);
1506 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1507 return RETURN_SUCCESS
;
1511 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1512 Releases any environment specific resources that were allocated when the image
1513 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1515 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1516 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1517 this function can simply return RETURN_SUCCESS.
1519 If ImageContext is NULL, then ASSERT().
1521 @param ImageContext The pointer to the image context structure that describes the PE/COFF
1522 image to be unloaded.
1524 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1528 PeCoffLoaderUnloadImage (
1529 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1533 // Applies additional environment specific actions to unload a
1534 // PE/COFF image if needed
1536 PeCoffLoaderUnloadImageExtraAction (ImageContext
);
1537 return RETURN_SUCCESS
;