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 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "BasePeCoffLibInternals.h"
19 Retrieves the magic value from the PE/COFF header.
21 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
23 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
24 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
28 PeCoffLoaderGetPeHeaderMagicValue (
29 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
33 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
34 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
35 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
36 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
38 if (Hdr
.Pe32
->FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
39 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
42 // Return the magic value from the PC/COFF Optional Header
44 return Hdr
.Pe32
->OptionalHeader
.Magic
;
49 Retrieves the PE or TE Header from a PE/COFF or TE image.
51 @param ImageContext The context of the image being loaded.
52 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
54 @retval RETURN_SUCCESS The PE or TE Header is read.
55 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
59 PeCoffLoaderGetPeHeader (
60 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
61 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
65 EFI_IMAGE_DOS_HEADER DosHdr
;
70 // Read the DOS image header to check for its existence
72 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
73 Status
= ImageContext
->ImageRead (
79 if (RETURN_ERROR (Status
)) {
80 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
84 ImageContext
->PeCoffHeaderOffset
= 0;
85 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
87 // DOS image header is present, so read the PE header after the DOS image
90 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
94 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
95 // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic
96 // determines if this is a PE32 or PE32+ image. The magic is in the same
97 // location in both images.
99 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
100 Status
= ImageContext
->ImageRead (
101 ImageContext
->Handle
,
102 ImageContext
->PeCoffHeaderOffset
,
106 if (RETURN_ERROR (Status
)) {
107 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
112 // Use Signature to figure out if we understand the image format
114 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
115 ImageContext
->IsTeImage
= TRUE
;
116 ImageContext
->Machine
= Hdr
.Te
->Machine
;
117 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
119 // For TeImage, SectionAlignment is undefined to be set to Zero
120 // ImageSize can be calculated.
122 ImageContext
->ImageSize
= 0;
123 ImageContext
->SectionAlignment
= 0;
124 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
126 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
127 ImageContext
->IsTeImage
= FALSE
;
128 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
130 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
132 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
136 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
137 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
138 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
139 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
141 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
145 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
146 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
147 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
148 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
150 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
151 return RETURN_UNSUPPORTED
;
154 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
155 return RETURN_UNSUPPORTED
;
158 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
160 // If the PE/COFF loader does not support the image type return
161 // unsupported. This library can support lots of types of images
162 // this does not mean the user of this library can call the entry
163 // point of the image.
165 return RETURN_UNSUPPORTED
;
168 return RETURN_SUCCESS
;
173 Retrieves information about a PE/COFF image.
175 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
176 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
177 DebugDirectoryEntryRva fields of the ImageContext structure.
178 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
179 If the PE/COFF image accessed through the ImageRead service in the ImageContext
180 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
181 If any errors occur while computing the fields of ImageContext,
182 then the error status is returned in the ImageError field of ImageContext.
183 If the image is a TE image, then SectionAlignment is set to 0.
184 The ImageRead and Handle fields of ImageContext structure must be valid prior
185 to invoking this service.
187 @param ImageContext Pointer to the image context structure that describes the PE/COFF
188 image that needs to be examined by this function.
190 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
191 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
192 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
197 PeCoffLoaderGetImageInfo (
198 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
201 RETURN_STATUS Status
;
202 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
203 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
204 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
207 UINTN DebugDirectoryEntryRva
;
208 UINTN DebugDirectoryEntryFileOffset
;
209 UINTN SectionHeaderOffset
;
210 EFI_IMAGE_SECTION_HEADER SectionHeader
;
211 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
212 UINT32 NumberOfRvaAndSizes
;
215 if (ImageContext
== NULL
) {
216 return RETURN_INVALID_PARAMETER
;
221 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
223 Hdr
.Union
= &HdrData
;
224 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
225 if (RETURN_ERROR (Status
)) {
229 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
232 // Retrieve the base address of the image
234 if (!(ImageContext
->IsTeImage
)) {
235 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
239 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
244 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
247 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
251 // Initialize the alternate destination address to 0 indicating that it
252 // should not be used.
254 ImageContext
->DestinationAddress
= 0;
257 // Initialize the debug codeview pointer.
259 ImageContext
->DebugDirectoryEntryRva
= 0;
260 ImageContext
->CodeView
= NULL
;
261 ImageContext
->PdbPointer
= NULL
;
264 // Three cases with regards to relocations:
265 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
266 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
267 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
268 // has no base relocs to apply
269 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
271 // Look at the file header to determine if relocations have been stripped, and
272 // save this info in the image context for later use.
274 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
275 ImageContext
->RelocationsStripped
= TRUE
;
276 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
277 ImageContext
->RelocationsStripped
= TRUE
;
279 ImageContext
->RelocationsStripped
= FALSE
;
282 if (!(ImageContext
->IsTeImage
)) {
283 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
287 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
288 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
293 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
294 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
297 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
299 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
302 // Determine the file offset of the debug directory... This means we walk
303 // the sections to find which section contains the RVA of the debug
306 DebugDirectoryEntryFileOffset
= 0;
308 SectionHeaderOffset
= (UINTN
)(
309 ImageContext
->PeCoffHeaderOffset
+
311 sizeof (EFI_IMAGE_FILE_HEADER
) +
312 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
315 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
317 // Read section header from file
319 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
320 Status
= ImageContext
->ImageRead (
321 ImageContext
->Handle
,
326 if (RETURN_ERROR (Status
)) {
327 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
331 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
332 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
334 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
338 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
341 if (DebugDirectoryEntryFileOffset
!= 0) {
342 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
344 // Read next debug directory entry
346 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
347 Status
= ImageContext
->ImageRead (
348 ImageContext
->Handle
,
349 DebugDirectoryEntryFileOffset
,
353 if (RETURN_ERROR (Status
)) {
354 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
357 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
358 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
359 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
360 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
363 return RETURN_SUCCESS
;
370 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
371 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
372 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
374 DebugDirectoryEntryFileOffset
= 0;
376 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
378 // Read section header from file
380 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
381 Status
= ImageContext
->ImageRead (
382 ImageContext
->Handle
,
387 if (RETURN_ERROR (Status
)) {
388 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
392 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
393 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
394 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
395 SectionHeader
.VirtualAddress
+
396 SectionHeader
.PointerToRawData
+
397 sizeof (EFI_TE_IMAGE_HEADER
) -
398 Hdr
.Te
->StrippedSize
;
401 // File offset of the debug directory was found, if this is not the last
402 // section, then skip to the last section for calculating the image size.
404 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
405 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
406 Index
= Hdr
.Te
->NumberOfSections
- 1;
412 // In Te image header there is not a field to describe the ImageSize.
413 // Actually, the ImageSize equals the RVA plus the VirtualSize of
414 // the last section mapped into memory (Must be rounded up to
415 // a multiple of Section Alignment). Per the PE/COFF specification, the
416 // section headers in the Section Table must appear in order of the RVA
417 // values for the corresponding sections. So the ImageSize can be determined
418 // by the RVA and the VirtualSize of the last section header in the
421 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
422 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
425 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
428 if (DebugDirectoryEntryFileOffset
!= 0) {
429 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
431 // Read next debug directory entry
433 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
434 Status
= ImageContext
->ImageRead (
435 ImageContext
->Handle
,
436 DebugDirectoryEntryFileOffset
,
440 if (RETURN_ERROR (Status
)) {
441 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
445 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
446 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
447 return RETURN_SUCCESS
;
453 return RETURN_SUCCESS
;
458 Converts an image address to the loaded address.
460 @param ImageContext The context of the image being loaded.
461 @param Address The relative virtual address to be converted to the loaded address.
463 @return The converted address or NULL if the address can not be converted.
467 PeCoffLoaderImageAddress (
468 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
473 // Make sure that Address and ImageSize is correct for the loaded image.
475 if (Address
>= ImageContext
->ImageSize
) {
476 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
480 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
484 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
486 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
487 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
488 of ImageContext as the relocation base address. The caller must allocate the relocation
489 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
491 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
492 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
493 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
494 the ImageContext structure must be valid prior to invoking this service.
496 If ImageContext is NULL, then ASSERT().
498 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
499 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
500 prior to transferring control to a PE/COFF image that is loaded using this library.
502 @param ImageContext Pointer to the image context structure that describes the PE/COFF
503 image that is being relocated.
505 @retval RETURN_SUCCESS The PE/COFF image was relocated.
506 Extended status information is in the ImageError field of ImageContext.
507 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
508 Extended status information is in the ImageError field of ImageContext.
509 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
510 Extended status information is in the ImageError field of ImageContext.
515 PeCoffLoaderRelocateImage (
516 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
519 RETURN_STATUS Status
;
520 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
521 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
523 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
524 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
533 PHYSICAL_ADDRESS BaseAddress
;
534 UINT32 NumberOfRvaAndSizes
;
537 ASSERT (ImageContext
!= NULL
);
542 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
545 // If there are no relocation entries, then we are done
547 if (ImageContext
->RelocationsStripped
) {
548 // Applies additional environment specific actions to relocate fixups
549 // to a PE/COFF image if needed
550 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
551 return RETURN_SUCCESS
;
555 // If the destination address is not 0, use that rather than the
556 // image address as the relocation target.
558 if (ImageContext
->DestinationAddress
!= 0) {
559 BaseAddress
= ImageContext
->DestinationAddress
;
561 BaseAddress
= ImageContext
->ImageAddress
;
564 if (!(ImageContext
->IsTeImage
)) {
565 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
567 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
569 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
573 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
574 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
576 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
577 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
582 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
583 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
585 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
586 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
590 // Find the relocation block
591 // Per the PE/COFF spec, you can't assume that a given data directory
592 // is present in the image. You have to check the NumberOfRvaAndSizes in
593 // the optional header to verify a desired directory entry is there.
596 if ((NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) && (RelocDir
->Size
> 0)) {
597 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
598 RelocBaseEnd
= PeCoffLoaderImageAddress (
600 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
602 if (RelocBase
== NULL
|| RelocBaseEnd
== NULL
) {
603 return RETURN_LOAD_ERROR
;
607 // Set base and end to bypass processing below.
609 RelocBase
= RelocBaseEnd
= NULL
;
612 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
613 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
614 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
617 // Find the relocation block
619 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
620 if (RelocDir
->Size
> 0) {
621 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
622 ImageContext
->ImageAddress
+
623 RelocDir
->VirtualAddress
+
624 sizeof(EFI_TE_IMAGE_HEADER
) -
627 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
630 // Set base and end to bypass processing below.
632 RelocBase
= RelocBaseEnd
= NULL
;
637 // Run the relocation information and apply the fixups
639 FixupData
= ImageContext
->FixupData
;
640 while (RelocBase
< RelocBaseEnd
) {
642 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
643 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
646 // Make sure RelocEnd is in the Image range.
648 if ((CHAR8
*) RelocEnd
< (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
) ||
649 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+ (UINTN
)ImageContext
->ImageSize
)) {
650 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
651 return RETURN_LOAD_ERROR
;
654 if (!(ImageContext
->IsTeImage
)) {
655 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
656 if (FixupBase
== NULL
) {
657 return RETURN_LOAD_ERROR
;
660 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
661 RelocBase
->VirtualAddress
+
662 sizeof(EFI_TE_IMAGE_HEADER
) -
668 // Run this relocation record
670 while (Reloc
< RelocEnd
) {
672 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
673 switch ((*Reloc
) >> 12) {
674 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
677 case EFI_IMAGE_REL_BASED_HIGH
:
678 Fixup16
= (UINT16
*) Fixup
;
679 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
680 if (FixupData
!= NULL
) {
681 *(UINT16
*) FixupData
= *Fixup16
;
682 FixupData
= FixupData
+ sizeof (UINT16
);
686 case EFI_IMAGE_REL_BASED_LOW
:
687 Fixup16
= (UINT16
*) Fixup
;
688 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
689 if (FixupData
!= NULL
) {
690 *(UINT16
*) FixupData
= *Fixup16
;
691 FixupData
= FixupData
+ sizeof (UINT16
);
695 case EFI_IMAGE_REL_BASED_HIGHLOW
:
696 Fixup32
= (UINT32
*) Fixup
;
697 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
698 if (FixupData
!= NULL
) {
699 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
700 *(UINT32
*)FixupData
= *Fixup32
;
701 FixupData
= FixupData
+ sizeof (UINT32
);
705 case EFI_IMAGE_REL_BASED_DIR64
:
706 Fixup64
= (UINT64
*) Fixup
;
707 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
708 if (FixupData
!= NULL
) {
709 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
710 *(UINT64
*)(FixupData
) = *Fixup64
;
711 FixupData
= FixupData
+ sizeof(UINT64
);
717 // The common code does not handle some of the stranger IPF relocations
718 // PeCoffLoaderRelocateImageEx () adds support for these complex fixups
719 // on IPF and is a No-Op on other architectures.
721 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
722 if (RETURN_ERROR (Status
)) {
723 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
729 // Next relocation record
737 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
741 // Adjust the EntryPoint to match the linked-to address
743 if (ImageContext
->DestinationAddress
!= 0) {
744 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
745 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
748 // Applies additional environment specific actions to relocate fixups
749 // to a PE/COFF image if needed
750 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
752 return RETURN_SUCCESS
;
756 Loads a PE/COFF image into memory.
758 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
759 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
760 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
761 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
762 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
763 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
764 fields of the ImageContext structure must be valid prior to invoking this service.
766 If ImageContext is NULL, then ASSERT().
768 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
769 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
770 prior to transferring control to a PE/COFF image that is loaded using this library.
772 @param ImageContext Pointer to the image context structure that describes the PE/COFF
773 image that is being loaded.
775 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
776 the ImageAddress and ImageSize fields of ImageContext.
777 Extended status information is in the ImageError field of ImageContext.
778 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
779 Extended status information is in the ImageError field of ImageContext.
780 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
781 Extended status information is in the ImageError field of ImageContext.
782 @retval RETURN_INVALID_PARAMETER The image address is invalid.
783 Extended status information is in the ImageError field of ImageContext.
788 PeCoffLoaderLoadImage (
789 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
792 RETURN_STATUS Status
;
793 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
794 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
795 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
796 EFI_IMAGE_SECTION_HEADER
*Section
;
797 UINTN NumberOfSections
;
802 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
803 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
805 UINT32 TempDebugEntryRva
;
806 UINT32 NumberOfRvaAndSizes
;
808 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
809 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
810 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
811 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
814 ASSERT (ImageContext
!= NULL
);
819 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
822 // Copy the provided context info into our local version, get what we
823 // can from the original image, and then use that to make sure everything
826 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
828 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
829 if (RETURN_ERROR (Status
)) {
834 // Make sure there is enough allocated space for the image being loaded
836 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
837 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
838 return RETURN_BUFFER_TOO_SMALL
;
840 if (ImageContext
->ImageAddress
== 0) {
842 // Image cannot be loaded into 0 address.
844 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
845 return RETURN_INVALID_PARAMETER
;
848 // If there's no relocations, then make sure it's not a runtime driver,
849 // and that it's being loaded at the linked address.
851 if (CheckContext
.RelocationsStripped
) {
853 // If the image does not contain relocations and it is a runtime driver
854 // then return an error.
856 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
857 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
858 return RETURN_LOAD_ERROR
;
861 // If the image does not contain relocations, and the requested load address
862 // is not the linked address, then return an error.
864 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
865 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
866 return RETURN_INVALID_PARAMETER
;
870 // Make sure the allocated space has the proper section alignment
872 if (!(ImageContext
->IsTeImage
)) {
873 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
874 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
875 return RETURN_INVALID_PARAMETER
;
879 // Read the entire PE/COFF or TE header into memory
881 if (!(ImageContext
->IsTeImage
)) {
882 Status
= ImageContext
->ImageRead (
883 ImageContext
->Handle
,
885 &ImageContext
->SizeOfHeaders
,
886 (VOID
*) (UINTN
) ImageContext
->ImageAddress
889 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
891 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
892 (UINTN
)ImageContext
->ImageAddress
+
893 ImageContext
->PeCoffHeaderOffset
+
895 sizeof(EFI_IMAGE_FILE_HEADER
) +
896 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
898 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
900 Status
= ImageContext
->ImageRead (
901 ImageContext
->Handle
,
903 &ImageContext
->SizeOfHeaders
,
904 (void *)(UINTN
)ImageContext
->ImageAddress
907 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
909 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
910 (UINTN
)ImageContext
->ImageAddress
+
911 sizeof(EFI_TE_IMAGE_HEADER
)
913 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
917 if (RETURN_ERROR (Status
)) {
918 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
919 return RETURN_LOAD_ERROR
;
923 // Load each section of the image
925 Section
= FirstSection
;
926 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
928 // Compute sections address
930 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
931 End
= PeCoffLoaderImageAddress (
933 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
937 // If the base start or end address resolved to 0, then fail.
939 if ((Base
== NULL
) || (End
== NULL
)) {
940 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
941 return RETURN_LOAD_ERROR
;
944 if (ImageContext
->IsTeImage
) {
945 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
946 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
956 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
957 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
958 Size
= (UINTN
) Section
->SizeOfRawData
;
961 if (Section
->SizeOfRawData
> 0) {
962 if (!(ImageContext
->IsTeImage
)) {
963 Status
= ImageContext
->ImageRead (
964 ImageContext
->Handle
,
965 Section
->PointerToRawData
,
970 Status
= ImageContext
->ImageRead (
971 ImageContext
->Handle
,
972 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
978 if (RETURN_ERROR (Status
)) {
979 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
985 // If raw size is less then virtual size, zero fill the remaining
988 if (Size
< Section
->Misc
.VirtualSize
) {
989 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
999 // Get image's entry point
1001 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1002 if (!(ImageContext
->IsTeImage
)) {
1004 // Sizes of AddressOfEntryPoint are different so we need to do this safely
1006 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1010 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1012 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
1018 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1020 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
1024 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
1025 (UINTN
)ImageContext
->ImageAddress
+
1026 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
1027 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
1028 (UINTN
)Hdr
.Te
->StrippedSize
1033 // Determine the size of the fixup data
1035 // Per the PE/COFF spec, you can't assume that a given data directory
1036 // is present in the image. You have to check the NumberOfRvaAndSizes in
1037 // the optional header to verify a desired directory entry is there.
1039 if (!(ImageContext
->IsTeImage
)) {
1040 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1044 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1045 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1050 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1051 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1054 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1055 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1057 ImageContext
->FixupDataSize
= 0;
1060 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1061 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1064 // Consumer must allocate a buffer for the relocation fixup log.
1065 // Only used for runtime drivers.
1067 ImageContext
->FixupData
= NULL
;
1070 // Load the Codeview info if present
1072 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1073 if (!(ImageContext
->IsTeImage
)) {
1074 DebugEntry
= PeCoffLoaderImageAddress (
1076 ImageContext
->DebugDirectoryEntryRva
1079 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1080 ImageContext
->ImageAddress
+
1081 ImageContext
->DebugDirectoryEntryRva
+
1082 sizeof(EFI_TE_IMAGE_HEADER
) -
1083 Hdr
.Te
->StrippedSize
1087 if (DebugEntry
!= NULL
) {
1088 TempDebugEntryRva
= DebugEntry
->RVA
;
1089 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1091 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1092 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1094 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1098 if (TempDebugEntryRva
!= 0) {
1099 if (!(ImageContext
->IsTeImage
)) {
1100 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1102 ImageContext
->CodeView
= (VOID
*)(
1103 (UINTN
)ImageContext
->ImageAddress
+
1104 (UINTN
)TempDebugEntryRva
+
1105 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1106 (UINTN
) Hdr
.Te
->StrippedSize
1110 if (ImageContext
->CodeView
== NULL
) {
1111 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1112 return RETURN_LOAD_ERROR
;
1115 if (DebugEntry
->RVA
== 0) {
1116 Size
= DebugEntry
->SizeOfData
;
1117 if (!(ImageContext
->IsTeImage
)) {
1118 Status
= ImageContext
->ImageRead (
1119 ImageContext
->Handle
,
1120 DebugEntry
->FileOffset
,
1122 ImageContext
->CodeView
1125 Status
= ImageContext
->ImageRead (
1126 ImageContext
->Handle
,
1127 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1129 ImageContext
->CodeView
1132 // Should we apply fix up to this field according to the size difference between PE and TE?
1133 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1134 // in original PE image.
1138 if (RETURN_ERROR (Status
)) {
1139 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1140 return RETURN_LOAD_ERROR
;
1143 DebugEntry
->RVA
= TempDebugEntryRva
;
1146 switch (*(UINT32
*) ImageContext
->CodeView
) {
1147 case CODEVIEW_SIGNATURE_NB10
:
1148 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1151 case CODEVIEW_SIGNATURE_RSDS
:
1152 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1163 // Get Image's HII resource section
1165 ImageContext
->HiiResourceData
= 0;
1166 if (!(ImageContext
->IsTeImage
)) {
1167 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1171 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1176 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1179 if (DirectoryEntry
->Size
!= 0) {
1180 Base
= PeCoffLoaderImageAddress (ImageContext
, DirectoryEntry
->VirtualAddress
);
1182 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) Base
;
1183 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1185 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
1186 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
1187 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (Base
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
1189 if (ResourceDirectoryString
->Length
== 3 &&
1190 ResourceDirectoryString
->String
[0] == L
'H' &&
1191 ResourceDirectoryString
->String
[1] == L
'I' &&
1192 ResourceDirectoryString
->String
[2] == L
'I') {
1194 // Resource Type "HII" found
1196 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1198 // Move to next level - resource Name
1200 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1201 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1203 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1205 // Move to next level - resource Language
1207 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1208 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1213 // Now it ought to be resource Data
1215 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1216 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (Base
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
1217 ImageContext
->HiiResourceData
= (PHYSICAL_ADDRESS
) (UINTN
) PeCoffLoaderImageAddress (ImageContext
, ResourceDataEntry
->OffsetToData
);
1222 ResourceDirectoryEntry
++;
1233 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1236 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
1237 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
1238 to the address specified by VirtualImageBase. RelocationData must be identical
1239 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
1240 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
1242 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
1243 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
1244 prior to transferring control to a PE/COFF image that is loaded using this library.
1246 @param ImageBase Base address of a PE/COFF image that has been loaded
1247 and relocated into system memory.
1248 @param VirtImageBase The request virtual address that the PE/COFF image is to
1250 @param ImageSize The size, in bytes, of the PE/COFF image.
1251 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1252 image was relocated using PeCoffLoaderRelocateImage().
1257 PeCoffLoaderRelocateImageForRuntime (
1258 IN PHYSICAL_ADDRESS ImageBase
,
1259 IN PHYSICAL_ADDRESS VirtImageBase
,
1261 IN VOID
*RelocationData
1266 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1267 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1268 UINT32 NumberOfRvaAndSizes
;
1269 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1270 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1271 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1272 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1282 RETURN_STATUS Status
;
1285 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1286 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1287 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1290 // Find the image's relocate dir info
1292 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1293 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1295 // Valid DOS header so get address of PE header
1297 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1300 // No Dos header so assume image starts with PE header.
1302 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1305 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1307 // Not a valid PE image so Exit
1312 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1314 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1318 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1319 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1324 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1325 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1329 // Find the relocation block
1331 // Per the PE/COFF spec, you can't assume that a given data directory
1332 // is present in the image. You have to check the NumberOfRvaAndSizes in
1333 // the optional header to verify a desired directory entry is there.
1335 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1336 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1337 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1338 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1341 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1348 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1350 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1353 // Run the whole relocation block. And re-fixup data that has not been
1354 // modified. The FixupData is used to see if the image has been modified
1355 // since it was relocated. This is so data sections that have been updated
1356 // by code will not be fixed up, since that would set them back to
1359 FixupData
= RelocationData
;
1360 while (RelocBase
< RelocBaseEnd
) {
1362 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1363 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1364 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1367 // Run this relocation record
1369 while (Reloc
< RelocEnd
) {
1371 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1372 switch ((*Reloc
) >> 12) {
1374 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1377 case EFI_IMAGE_REL_BASED_HIGH
:
1378 Fixup16
= (UINT16
*) Fixup
;
1379 if (*(UINT16
*) FixupData
== *Fixup16
) {
1380 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1383 FixupData
= FixupData
+ sizeof (UINT16
);
1386 case EFI_IMAGE_REL_BASED_LOW
:
1387 Fixup16
= (UINT16
*) Fixup
;
1388 if (*(UINT16
*) FixupData
== *Fixup16
) {
1389 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1392 FixupData
= FixupData
+ sizeof (UINT16
);
1395 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1396 Fixup32
= (UINT32
*) Fixup
;
1397 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1398 if (*(UINT32
*) FixupData
== *Fixup32
) {
1399 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1402 FixupData
= FixupData
+ sizeof (UINT32
);
1405 case EFI_IMAGE_REL_BASED_DIR64
:
1406 Fixup64
= (UINT64
*)Fixup
;
1407 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1408 if (*(UINT64
*) FixupData
== *Fixup64
) {
1409 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1412 FixupData
= FixupData
+ sizeof (UINT64
);
1415 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1417 // Not valid Relocation type for UEFI image, ASSERT
1424 // Only Itanium requires ConvertPeImage_Ex
1426 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1427 if (RETURN_ERROR (Status
)) {
1432 // Next relocation record
1439 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1445 Reads contents of a PE/COFF image from a buffer in system memory.
1447 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1448 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1449 This function reads contents of the PE/COFF image that starts at the system memory
1450 address specified by FileHandle. The read operation copies ReadSize bytes from the
1451 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1452 The size of the buffer actually read is returned in ReadSize.
1454 If FileHandle is NULL, then ASSERT().
1455 If ReadSize is NULL, then ASSERT().
1456 If Buffer is NULL, then ASSERT().
1458 @param FileHandle Pointer to base of the input stream
1459 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1460 @param ReadSize On input, the size in bytes of the requested read operation.
1461 On output, the number of bytes actually read.
1462 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1464 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1469 PeCoffLoaderImageReadFromMemory (
1470 IN VOID
*FileHandle
,
1471 IN UINTN FileOffset
,
1472 IN OUT UINTN
*ReadSize
,
1476 ASSERT (ReadSize
!= NULL
);
1477 ASSERT (FileHandle
!= NULL
);
1478 ASSERT (Buffer
!= NULL
);
1480 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1481 return RETURN_SUCCESS
;
1485 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1486 Releases any environment specific resources that were allocated when the image
1487 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1489 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1490 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1491 this function can simply return RETURN_SUCCESS.
1493 If ImageContext is NULL, then ASSERT().
1495 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1496 image to be unloaded.
1498 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1502 PeCoffLoaderUnloadImage (
1503 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1507 // Applies additional environment specific actions to unload a
1508 // PE/COFF image if needed
1510 PeCoffLoaderUnloadImageExtraAction (ImageContext
);
1511 return RETURN_SUCCESS
;