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 @param ImageContext Pointer to the image context structure that describes the PE/COFF
499 image that is being relocated.
501 @retval RETURN_SUCCESS The PE/COFF image was relocated.
502 Extended status information is in the ImageError field of ImageContext.
503 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
504 Extended status information is in the ImageError field of ImageContext.
505 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
506 Extended status information is in the ImageError field of ImageContext.
511 PeCoffLoaderRelocateImage (
512 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
515 RETURN_STATUS Status
;
516 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
517 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
519 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
520 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
529 PHYSICAL_ADDRESS BaseAddress
;
530 UINT32 NumberOfRvaAndSizes
;
533 ASSERT (ImageContext
!= NULL
);
538 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
541 // If there are no relocation entries, then we are done
543 if (ImageContext
->RelocationsStripped
) {
544 // Applies additional environment specific actions to relocate fixups
545 // to a PE/COFF image if needed
546 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
547 return RETURN_SUCCESS
;
551 // If the destination address is not 0, use that rather than the
552 // image address as the relocation target.
554 if (ImageContext
->DestinationAddress
!= 0) {
555 BaseAddress
= ImageContext
->DestinationAddress
;
557 BaseAddress
= ImageContext
->ImageAddress
;
560 if (!(ImageContext
->IsTeImage
)) {
561 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
563 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
565 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
569 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
570 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
572 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
573 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
578 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
579 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
581 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
582 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
586 // Find the relocation block
587 // Per the PE/COFF spec, you can't assume that a given data directory
588 // is present in the image. You have to check the NumberOfRvaAndSizes in
589 // the optional header to verify a desired directory entry is there.
592 if ((NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) && (RelocDir
->Size
> 0)) {
593 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
594 RelocBaseEnd
= PeCoffLoaderImageAddress (
596 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
598 if (RelocBase
== NULL
|| RelocBaseEnd
== NULL
) {
599 return RETURN_LOAD_ERROR
;
603 // Set base and end to bypass processing below.
605 RelocBase
= RelocBaseEnd
= NULL
;
608 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
609 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
610 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
613 // Find the relocation block
615 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
616 if (RelocDir
->Size
> 0) {
617 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
618 ImageContext
->ImageAddress
+
619 RelocDir
->VirtualAddress
+
620 sizeof(EFI_TE_IMAGE_HEADER
) -
623 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
626 // Set base and end to bypass processing below.
628 RelocBase
= RelocBaseEnd
= NULL
;
633 // Run the relocation information and apply the fixups
635 FixupData
= ImageContext
->FixupData
;
636 while (RelocBase
< RelocBaseEnd
) {
638 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
639 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
642 // Make sure RelocEnd is in the Image range.
644 if ((CHAR8
*) RelocEnd
< (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
) ||
645 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+ (UINTN
)ImageContext
->ImageSize
)) {
646 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
647 return RETURN_LOAD_ERROR
;
650 if (!(ImageContext
->IsTeImage
)) {
651 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
652 if (FixupBase
== NULL
) {
653 return RETURN_LOAD_ERROR
;
656 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
657 RelocBase
->VirtualAddress
+
658 sizeof(EFI_TE_IMAGE_HEADER
) -
664 // Run this relocation record
666 while (Reloc
< RelocEnd
) {
668 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
669 switch ((*Reloc
) >> 12) {
670 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
673 case EFI_IMAGE_REL_BASED_HIGH
:
674 Fixup16
= (UINT16
*) Fixup
;
675 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
676 if (FixupData
!= NULL
) {
677 *(UINT16
*) FixupData
= *Fixup16
;
678 FixupData
= FixupData
+ sizeof (UINT16
);
682 case EFI_IMAGE_REL_BASED_LOW
:
683 Fixup16
= (UINT16
*) Fixup
;
684 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
685 if (FixupData
!= NULL
) {
686 *(UINT16
*) FixupData
= *Fixup16
;
687 FixupData
= FixupData
+ sizeof (UINT16
);
691 case EFI_IMAGE_REL_BASED_HIGHLOW
:
692 Fixup32
= (UINT32
*) Fixup
;
693 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
694 if (FixupData
!= NULL
) {
695 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
696 *(UINT32
*)FixupData
= *Fixup32
;
697 FixupData
= FixupData
+ sizeof (UINT32
);
701 case EFI_IMAGE_REL_BASED_DIR64
:
702 Fixup64
= (UINT64
*) Fixup
;
703 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
704 if (FixupData
!= NULL
) {
705 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
706 *(UINT64
*)(FixupData
) = *Fixup64
;
707 FixupData
= FixupData
+ sizeof(UINT64
);
713 // The common code does not handle some of the stranger IPF relocations
714 // PeCoffLoaderRelocateImageEx () adds support for these complex fixups
715 // on IPF and is a No-Op on other architectures.
717 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
718 if (RETURN_ERROR (Status
)) {
719 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
725 // Next relocation record
733 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
737 // Adjust the EntryPoint to match the linked-to address
739 if (ImageContext
->DestinationAddress
!= 0) {
740 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
741 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
744 // Applies additional environment specific actions to relocate fixups
745 // to a PE/COFF image if needed
746 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
748 return RETURN_SUCCESS
;
752 Loads a PE/COFF image into memory.
754 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
755 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
756 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
757 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
758 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
759 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
760 fields of the ImageContext structure must be valid prior to invoking this service.
762 If ImageContext is NULL, then ASSERT().
764 @param ImageContext Pointer to the image context structure that describes the PE/COFF
765 image that is being loaded.
767 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
768 the ImageAddress and ImageSize fields of ImageContext.
769 Extended status information is in the ImageError field of ImageContext.
770 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
771 Extended status information is in the ImageError field of ImageContext.
772 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
773 Extended status information is in the ImageError field of ImageContext.
774 @retval RETURN_INVALID_PARAMETER The image address is invalid.
775 Extended status information is in the ImageError field of ImageContext.
780 PeCoffLoaderLoadImage (
781 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
784 RETURN_STATUS Status
;
785 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
786 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
787 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
788 EFI_IMAGE_SECTION_HEADER
*Section
;
789 UINTN NumberOfSections
;
794 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
795 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
797 UINT32 TempDebugEntryRva
;
798 UINT32 NumberOfRvaAndSizes
;
800 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
801 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
802 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
803 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
806 ASSERT (ImageContext
!= NULL
);
811 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
814 // Copy the provided context info into our local version, get what we
815 // can from the original image, and then use that to make sure everything
818 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
820 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
821 if (RETURN_ERROR (Status
)) {
826 // Make sure there is enough allocated space for the image being loaded
828 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
829 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
830 return RETURN_BUFFER_TOO_SMALL
;
832 if (ImageContext
->ImageAddress
== 0) {
834 // Image cannot be loaded into 0 address.
836 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
837 return RETURN_INVALID_PARAMETER
;
840 // If there's no relocations, then make sure it's not a runtime driver,
841 // and that it's being loaded at the linked address.
843 if (CheckContext
.RelocationsStripped
) {
845 // If the image does not contain relocations and it is a runtime driver
846 // then return an error.
848 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
849 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
850 return RETURN_LOAD_ERROR
;
853 // If the image does not contain relocations, and the requested load address
854 // is not the linked address, then return an error.
856 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
857 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
858 return RETURN_INVALID_PARAMETER
;
862 // Make sure the allocated space has the proper section alignment
864 if (!(ImageContext
->IsTeImage
)) {
865 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
866 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
867 return RETURN_INVALID_PARAMETER
;
871 // Read the entire PE/COFF or TE header into memory
873 if (!(ImageContext
->IsTeImage
)) {
874 Status
= ImageContext
->ImageRead (
875 ImageContext
->Handle
,
877 &ImageContext
->SizeOfHeaders
,
878 (VOID
*) (UINTN
) ImageContext
->ImageAddress
881 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
883 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
884 (UINTN
)ImageContext
->ImageAddress
+
885 ImageContext
->PeCoffHeaderOffset
+
887 sizeof(EFI_IMAGE_FILE_HEADER
) +
888 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
890 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
892 Status
= ImageContext
->ImageRead (
893 ImageContext
->Handle
,
895 &ImageContext
->SizeOfHeaders
,
896 (void *)(UINTN
)ImageContext
->ImageAddress
899 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
901 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
902 (UINTN
)ImageContext
->ImageAddress
+
903 sizeof(EFI_TE_IMAGE_HEADER
)
905 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
909 if (RETURN_ERROR (Status
)) {
910 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
911 return RETURN_LOAD_ERROR
;
915 // Load each section of the image
917 Section
= FirstSection
;
918 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
920 // Compute sections address
922 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
923 End
= PeCoffLoaderImageAddress (
925 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
929 // If the base start or end address resolved to 0, then fail.
931 if ((Base
== NULL
) || (End
== NULL
)) {
932 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
933 return RETURN_LOAD_ERROR
;
936 if (ImageContext
->IsTeImage
) {
937 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
938 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
948 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
949 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
950 Size
= (UINTN
) Section
->SizeOfRawData
;
953 if (Section
->SizeOfRawData
> 0) {
954 if (!(ImageContext
->IsTeImage
)) {
955 Status
= ImageContext
->ImageRead (
956 ImageContext
->Handle
,
957 Section
->PointerToRawData
,
962 Status
= ImageContext
->ImageRead (
963 ImageContext
->Handle
,
964 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
970 if (RETURN_ERROR (Status
)) {
971 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
977 // If raw size is less then virtual size, zero fill the remaining
980 if (Size
< Section
->Misc
.VirtualSize
) {
981 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
991 // Get image's entry point
993 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
994 if (!(ImageContext
->IsTeImage
)) {
996 // Sizes of AddressOfEntryPoint are different so we need to do this safely
998 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1002 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1004 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
1010 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1012 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
1016 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
1017 (UINTN
)ImageContext
->ImageAddress
+
1018 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
1019 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
1020 (UINTN
)Hdr
.Te
->StrippedSize
1025 // Determine the size of the fixup data
1027 // Per the PE/COFF spec, you can't assume that a given data directory
1028 // is present in the image. You have to check the NumberOfRvaAndSizes in
1029 // the optional header to verify a desired directory entry is there.
1031 if (!(ImageContext
->IsTeImage
)) {
1032 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1036 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1037 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1042 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1043 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1046 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1047 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1049 ImageContext
->FixupDataSize
= 0;
1052 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1053 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1056 // Consumer must allocate a buffer for the relocation fixup log.
1057 // Only used for runtime drivers.
1059 ImageContext
->FixupData
= NULL
;
1062 // Load the Codeview info if present
1064 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1065 if (!(ImageContext
->IsTeImage
)) {
1066 DebugEntry
= PeCoffLoaderImageAddress (
1068 ImageContext
->DebugDirectoryEntryRva
1071 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1072 ImageContext
->ImageAddress
+
1073 ImageContext
->DebugDirectoryEntryRva
+
1074 sizeof(EFI_TE_IMAGE_HEADER
) -
1075 Hdr
.Te
->StrippedSize
1079 if (DebugEntry
!= NULL
) {
1080 TempDebugEntryRva
= DebugEntry
->RVA
;
1081 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1083 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1084 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1086 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1090 if (TempDebugEntryRva
!= 0) {
1091 if (!(ImageContext
->IsTeImage
)) {
1092 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1094 ImageContext
->CodeView
= (VOID
*)(
1095 (UINTN
)ImageContext
->ImageAddress
+
1096 (UINTN
)TempDebugEntryRva
+
1097 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1098 (UINTN
) Hdr
.Te
->StrippedSize
1102 if (ImageContext
->CodeView
== NULL
) {
1103 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1104 return RETURN_LOAD_ERROR
;
1107 if (DebugEntry
->RVA
== 0) {
1108 Size
= DebugEntry
->SizeOfData
;
1109 if (!(ImageContext
->IsTeImage
)) {
1110 Status
= ImageContext
->ImageRead (
1111 ImageContext
->Handle
,
1112 DebugEntry
->FileOffset
,
1114 ImageContext
->CodeView
1117 Status
= ImageContext
->ImageRead (
1118 ImageContext
->Handle
,
1119 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1121 ImageContext
->CodeView
1124 // Should we apply fix up to this field according to the size difference between PE and TE?
1125 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1126 // in original PE image.
1130 if (RETURN_ERROR (Status
)) {
1131 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1132 return RETURN_LOAD_ERROR
;
1135 DebugEntry
->RVA
= TempDebugEntryRva
;
1138 switch (*(UINT32
*) ImageContext
->CodeView
) {
1139 case CODEVIEW_SIGNATURE_NB10
:
1140 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1143 case CODEVIEW_SIGNATURE_RSDS
:
1144 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1155 // Get Image's HII resource section
1157 ImageContext
->HiiResourceData
= 0;
1158 if (!(ImageContext
->IsTeImage
)) {
1159 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1163 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1168 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1171 if (DirectoryEntry
->Size
!= 0) {
1172 Base
= PeCoffLoaderImageAddress (ImageContext
, DirectoryEntry
->VirtualAddress
);
1174 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) Base
;
1175 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1177 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
1178 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
1179 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (Base
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
1181 if (ResourceDirectoryString
->Length
== 3 &&
1182 ResourceDirectoryString
->String
[0] == L
'H' &&
1183 ResourceDirectoryString
->String
[1] == L
'I' &&
1184 ResourceDirectoryString
->String
[2] == L
'I') {
1186 // Resource Type "HII" found
1188 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1190 // Move to next level - resource Name
1192 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1193 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1195 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1197 // Move to next level - resource Language
1199 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1200 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1205 // Now it ought to be resource Data
1207 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1208 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (Base
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
1209 ImageContext
->HiiResourceData
= (PHYSICAL_ADDRESS
) (UINTN
) PeCoffLoaderImageAddress (ImageContext
, ResourceDataEntry
->OffsetToData
);
1214 ResourceDirectoryEntry
++;
1225 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1228 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
1229 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
1230 to the address specified by VirtualImageBase. RelocationData must be identical
1231 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
1232 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
1234 @param ImageBase Base address of a PE/COFF image that has been loaded
1235 and relocated into system memory.
1236 @param VirtImageBase The request virtual address that the PE/COFF image is to
1238 @param ImageSize The size, in bytes, of the PE/COFF image.
1239 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1240 image was relocated using PeCoffLoaderRelocateImage().
1245 PeCoffLoaderRelocateImageForRuntime (
1246 IN PHYSICAL_ADDRESS ImageBase
,
1247 IN PHYSICAL_ADDRESS VirtImageBase
,
1249 IN VOID
*RelocationData
1254 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1255 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1256 UINT32 NumberOfRvaAndSizes
;
1257 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1258 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1259 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1260 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1270 RETURN_STATUS Status
;
1273 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1274 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1275 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1278 // Find the image's relocate dir info
1280 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1281 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1283 // Valid DOS header so get address of PE header
1285 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1288 // No Dos header so assume image starts with PE header.
1290 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1293 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1295 // Not a valid PE image so Exit
1300 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1302 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1306 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1307 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1312 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1313 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1317 // Find the relocation block
1319 // Per the PE/COFF spec, you can't assume that a given data directory
1320 // is present in the image. You have to check the NumberOfRvaAndSizes in
1321 // the optional header to verify a desired directory entry is there.
1323 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1324 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1325 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1326 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1329 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1336 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1338 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1341 // Run the whole relocation block. And re-fixup data that has not been
1342 // modified. The FixupData is used to see if the image has been modified
1343 // since it was relocated. This is so data sections that have been updated
1344 // by code will not be fixed up, since that would set them back to
1347 FixupData
= RelocationData
;
1348 while (RelocBase
< RelocBaseEnd
) {
1350 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1351 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1352 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1355 // Run this relocation record
1357 while (Reloc
< RelocEnd
) {
1359 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1360 switch ((*Reloc
) >> 12) {
1362 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1365 case EFI_IMAGE_REL_BASED_HIGH
:
1366 Fixup16
= (UINT16
*) Fixup
;
1367 if (*(UINT16
*) FixupData
== *Fixup16
) {
1368 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1371 FixupData
= FixupData
+ sizeof (UINT16
);
1374 case EFI_IMAGE_REL_BASED_LOW
:
1375 Fixup16
= (UINT16
*) Fixup
;
1376 if (*(UINT16
*) FixupData
== *Fixup16
) {
1377 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1380 FixupData
= FixupData
+ sizeof (UINT16
);
1383 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1384 Fixup32
= (UINT32
*) Fixup
;
1385 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1386 if (*(UINT32
*) FixupData
== *Fixup32
) {
1387 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1390 FixupData
= FixupData
+ sizeof (UINT32
);
1393 case EFI_IMAGE_REL_BASED_DIR64
:
1394 Fixup64
= (UINT64
*)Fixup
;
1395 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1396 if (*(UINT64
*) FixupData
== *Fixup64
) {
1397 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1400 FixupData
= FixupData
+ sizeof (UINT64
);
1403 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1405 // Not valid Relocation type for UEFI image, ASSERT
1412 // Only Itanium requires ConvertPeImage_Ex
1414 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1415 if (RETURN_ERROR (Status
)) {
1420 // Next relocation record
1427 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1433 Reads contents of a PE/COFF image from a buffer in system memory.
1435 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1436 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1437 This function reads contents of the PE/COFF image that starts at the system memory
1438 address specified by FileHandle. The read operation copies ReadSize bytes from the
1439 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1440 The size of the buffer actually read is returned in ReadSize.
1442 If FileHandle is NULL, then ASSERT().
1443 If ReadSize is NULL, then ASSERT().
1444 If Buffer is NULL, then ASSERT().
1446 @param FileHandle Pointer to base of the input stream
1447 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1448 @param ReadSize On input, the size in bytes of the requested read operation.
1449 On output, the number of bytes actually read.
1450 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1452 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1457 PeCoffLoaderImageReadFromMemory (
1458 IN VOID
*FileHandle
,
1459 IN UINTN FileOffset
,
1460 IN OUT UINTN
*ReadSize
,
1464 ASSERT (ReadSize
!= NULL
);
1465 ASSERT (FileHandle
!= NULL
);
1466 ASSERT (Buffer
!= NULL
);
1468 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1469 return RETURN_SUCCESS
;
1473 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1474 Releases any environment specific resources that were allocated when the image
1475 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1477 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1478 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1479 this function can simply return RETURN_SUCCESS.
1481 If ImageContext is NULL, then ASSERT().
1483 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1484 image to be unloaded.
1486 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1490 PeCoffLoaderUnloadImage (
1491 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1495 // Applies additional environment specific actions to unload a
1496 // PE/COFF image if needed
1498 PeCoffLoaderUnloadImageExtraAction (ImageContext
);
1499 return RETURN_SUCCESS
;