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<BR>
6 Portions Copyright (c) 2008-2009 Apple Inc.<BR>
7 All rights reserved. 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 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 info 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
;
283 if (!(ImageContext
->IsTeImage
)) {
284 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
288 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
289 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
294 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
295 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
298 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
300 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
303 // Determine the file offset of the debug directory... This means we walk
304 // the sections to find which section contains the RVA of the debug
307 DebugDirectoryEntryFileOffset
= 0;
309 SectionHeaderOffset
= (UINTN
)(
310 ImageContext
->PeCoffHeaderOffset
+
312 sizeof (EFI_IMAGE_FILE_HEADER
) +
313 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
316 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
318 // Read section header from file
320 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
321 Status
= ImageContext
->ImageRead (
322 ImageContext
->Handle
,
327 if (RETURN_ERROR (Status
)) {
328 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
332 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
333 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
335 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
339 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
342 if (DebugDirectoryEntryFileOffset
!= 0) {
343 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
345 // Read next debug directory entry
347 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
348 Status
= ImageContext
->ImageRead (
349 ImageContext
->Handle
,
350 DebugDirectoryEntryFileOffset
,
354 if (RETURN_ERROR (Status
)) {
355 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
358 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
359 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
360 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
361 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
364 return RETURN_SUCCESS
;
371 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
372 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
373 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
375 DebugDirectoryEntryFileOffset
= 0;
377 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
379 // Read section header from file
381 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
382 Status
= ImageContext
->ImageRead (
383 ImageContext
->Handle
,
388 if (RETURN_ERROR (Status
)) {
389 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
393 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
394 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
395 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
396 SectionHeader
.VirtualAddress
+
397 SectionHeader
.PointerToRawData
+
398 sizeof (EFI_TE_IMAGE_HEADER
) -
399 Hdr
.Te
->StrippedSize
;
402 // File offset of the debug directory was found, if this is not the last
403 // section, then skip to the last section for calculating the image size.
405 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
406 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
407 Index
= Hdr
.Te
->NumberOfSections
- 1;
413 // In Te image header there is not a field to describe the ImageSize.
414 // Actually, the ImageSize equals the RVA plus the VirtualSize of
415 // the last section mapped into memory (Must be rounded up to
416 // a multiple of Section Alignment). Per the PE/COFF specification, the
417 // section headers in the Section Table must appear in order of the RVA
418 // values for the corresponding sections. So the ImageSize can be determined
419 // by the RVA and the VirtualSize of the last section header in the
422 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
423 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
426 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
429 if (DebugDirectoryEntryFileOffset
!= 0) {
430 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
432 // Read next debug directory entry
434 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
435 Status
= ImageContext
->ImageRead (
436 ImageContext
->Handle
,
437 DebugDirectoryEntryFileOffset
,
441 if (RETURN_ERROR (Status
)) {
442 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
446 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
447 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
448 return RETURN_SUCCESS
;
454 return RETURN_SUCCESS
;
459 Converts an image address to the loaded address.
461 @param ImageContext The context of the image being loaded.
462 @param Address The relative virtual address to be converted to the loaded address.
464 @return The converted address or NULL if the address can not be converted.
468 PeCoffLoaderImageAddress (
469 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
474 // Make sure that Address and ImageSize is correct for the loaded image.
476 if (Address
>= ImageContext
->ImageSize
) {
477 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
481 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
485 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
487 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
488 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
489 of ImageContext as the relocation base address. The caller must allocate the relocation
490 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
492 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,
493 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,
494 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of
495 the ImageContext structure must be valid prior to invoking this service.
497 If ImageContext is NULL, then ASSERT().
499 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
500 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
501 prior to transferring control to a PE/COFF image that is loaded using this library.
503 @param ImageContext Pointer to the image context structure that describes the PE/COFF
504 image that is being relocated.
506 @retval RETURN_SUCCESS The PE/COFF image was relocated.
507 Extended status information is in the ImageError field of ImageContext.
508 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
509 Extended status information is in the ImageError field of ImageContext.
510 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
511 Extended status information is in the ImageError field of ImageContext.
516 PeCoffLoaderRelocateImage (
517 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
520 RETURN_STATUS Status
;
521 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
522 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
524 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
525 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
534 PHYSICAL_ADDRESS BaseAddress
;
535 UINT32 NumberOfRvaAndSizes
;
538 ASSERT (ImageContext
!= NULL
);
543 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
546 // If there are no relocation entries, then we are done
548 if (ImageContext
->RelocationsStripped
) {
549 // Applies additional environment specific actions to relocate fixups
550 // to a PE/COFF image if needed
551 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
552 return RETURN_SUCCESS
;
556 // If the destination address is not 0, use that rather than the
557 // image address as the relocation target.
559 if (ImageContext
->DestinationAddress
!= 0) {
560 BaseAddress
= ImageContext
->DestinationAddress
;
562 BaseAddress
= ImageContext
->ImageAddress
;
565 if (!(ImageContext
->IsTeImage
)) {
566 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
568 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
570 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
574 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
575 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
577 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
578 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
583 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
584 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
586 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
587 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
591 // Find the relocation block
592 // Per the PE/COFF spec, you can't assume that a given data directory
593 // is present in the image. You have to check the NumberOfRvaAndSizes in
594 // the optional header to verify a desired directory entry is there.
597 if ((NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) && (RelocDir
->Size
> 0)) {
598 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
599 RelocBaseEnd
= PeCoffLoaderImageAddress (
601 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
603 if (RelocBase
== NULL
|| RelocBaseEnd
== NULL
) {
604 return RETURN_LOAD_ERROR
;
608 // Set base and end to bypass processing below.
610 RelocBase
= RelocBaseEnd
= NULL
;
613 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
614 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
615 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
618 // Find the relocation block
620 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
621 if (RelocDir
->Size
> 0) {
622 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
623 ImageContext
->ImageAddress
+
624 RelocDir
->VirtualAddress
+
625 sizeof(EFI_TE_IMAGE_HEADER
) -
628 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
631 // Set base and end to bypass processing below.
633 RelocBase
= RelocBaseEnd
= NULL
;
638 // Run the relocation information and apply the fixups
640 FixupData
= ImageContext
->FixupData
;
641 while (RelocBase
< RelocBaseEnd
) {
643 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
644 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
647 // Make sure RelocEnd is in the Image range.
649 if ((CHAR8
*) RelocEnd
< (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
) ||
650 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+ (UINTN
)ImageContext
->ImageSize
)) {
651 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
652 return RETURN_LOAD_ERROR
;
655 if (!(ImageContext
->IsTeImage
)) {
656 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
657 if (FixupBase
== NULL
) {
658 return RETURN_LOAD_ERROR
;
661 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
662 RelocBase
->VirtualAddress
+
663 sizeof(EFI_TE_IMAGE_HEADER
) -
669 // Run this relocation record
671 while (Reloc
< RelocEnd
) {
673 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
674 switch ((*Reloc
) >> 12) {
675 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
678 case EFI_IMAGE_REL_BASED_HIGH
:
679 Fixup16
= (UINT16
*) Fixup
;
680 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
681 if (FixupData
!= NULL
) {
682 *(UINT16
*) FixupData
= *Fixup16
;
683 FixupData
= FixupData
+ sizeof (UINT16
);
687 case EFI_IMAGE_REL_BASED_LOW
:
688 Fixup16
= (UINT16
*) Fixup
;
689 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
690 if (FixupData
!= NULL
) {
691 *(UINT16
*) FixupData
= *Fixup16
;
692 FixupData
= FixupData
+ sizeof (UINT16
);
696 case EFI_IMAGE_REL_BASED_HIGHLOW
:
697 Fixup32
= (UINT32
*) Fixup
;
698 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
699 if (FixupData
!= NULL
) {
700 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
701 *(UINT32
*)FixupData
= *Fixup32
;
702 FixupData
= FixupData
+ sizeof (UINT32
);
706 case EFI_IMAGE_REL_BASED_DIR64
:
707 Fixup64
= (UINT64
*) Fixup
;
708 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
709 if (FixupData
!= NULL
) {
710 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
711 *(UINT64
*)(FixupData
) = *Fixup64
;
712 FixupData
= FixupData
+ sizeof(UINT64
);
718 // The common code does not handle some of the stranger IPF relocations
719 // PeCoffLoaderRelocateImageEx () adds support for these complex fixups
720 // on IPF and is a No-Op on other architectures.
722 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
723 if (RETURN_ERROR (Status
)) {
724 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
730 // Next relocation record
738 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
742 // Adjust the EntryPoint to match the linked-to address
744 if (ImageContext
->DestinationAddress
!= 0) {
745 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
746 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
749 // Applies additional environment specific actions to relocate fixups
750 // to a PE/COFF image if needed
751 PeCoffLoaderRelocateImageExtraAction (ImageContext
);
753 return RETURN_SUCCESS
;
757 Loads a PE/COFF image into memory.
759 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
760 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
761 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
762 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.
763 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
764 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
765 fields of the ImageContext structure must be valid prior to invoking this service.
767 If ImageContext is NULL, then ASSERT().
769 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
770 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
771 prior to transferring control to a PE/COFF image that is loaded using this library.
773 @param ImageContext Pointer to the image context structure that describes the PE/COFF
774 image that is being loaded.
776 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
777 the ImageAddress and ImageSize fields of ImageContext.
778 Extended status information is in the ImageError field of ImageContext.
779 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
780 Extended status information is in the ImageError field of ImageContext.
781 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
782 Extended status information is in the ImageError field of ImageContext.
783 @retval RETURN_INVALID_PARAMETER The image address is invalid.
784 Extended status information is in the ImageError field of ImageContext.
789 PeCoffLoaderLoadImage (
790 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
793 RETURN_STATUS Status
;
794 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
795 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
796 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
797 EFI_IMAGE_SECTION_HEADER
*Section
;
798 UINTN NumberOfSections
;
803 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
804 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
806 UINT32 TempDebugEntryRva
;
807 UINT32 NumberOfRvaAndSizes
;
809 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
810 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
811 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
812 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
815 ASSERT (ImageContext
!= NULL
);
820 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
823 // Copy the provided context info into our local version, get what we
824 // can from the original image, and then use that to make sure everything
827 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
829 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
830 if (RETURN_ERROR (Status
)) {
835 // Make sure there is enough allocated space for the image being loaded
837 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
838 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
839 return RETURN_BUFFER_TOO_SMALL
;
841 if (ImageContext
->ImageAddress
== 0) {
843 // Image cannot be loaded into 0 address.
845 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
846 return RETURN_INVALID_PARAMETER
;
849 // If there's no relocations, then make sure it's not a runtime driver,
850 // and that it's being loaded at the linked address.
852 if (CheckContext
.RelocationsStripped
) {
854 // If the image does not contain relocations and it is a runtime driver
855 // then return an error.
857 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
858 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
859 return RETURN_LOAD_ERROR
;
862 // If the image does not contain relocations, and the requested load address
863 // is not the linked address, then return an error.
865 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
866 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
867 return RETURN_INVALID_PARAMETER
;
871 // Make sure the allocated space has the proper section alignment
873 if (!(ImageContext
->IsTeImage
)) {
874 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
875 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
876 return RETURN_INVALID_PARAMETER
;
880 // Read the entire PE/COFF or TE header into memory
882 if (!(ImageContext
->IsTeImage
)) {
883 Status
= ImageContext
->ImageRead (
884 ImageContext
->Handle
,
886 &ImageContext
->SizeOfHeaders
,
887 (VOID
*) (UINTN
) ImageContext
->ImageAddress
890 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
892 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
893 (UINTN
)ImageContext
->ImageAddress
+
894 ImageContext
->PeCoffHeaderOffset
+
896 sizeof(EFI_IMAGE_FILE_HEADER
) +
897 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
899 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
901 Status
= ImageContext
->ImageRead (
902 ImageContext
->Handle
,
904 &ImageContext
->SizeOfHeaders
,
905 (void *)(UINTN
)ImageContext
->ImageAddress
908 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
910 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
911 (UINTN
)ImageContext
->ImageAddress
+
912 sizeof(EFI_TE_IMAGE_HEADER
)
914 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
918 if (RETURN_ERROR (Status
)) {
919 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
920 return RETURN_LOAD_ERROR
;
924 // Load each section of the image
926 Section
= FirstSection
;
927 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
929 // Compute sections address
931 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
932 End
= PeCoffLoaderImageAddress (
934 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
938 // If the base start or end address resolved to 0, then fail.
940 if ((Base
== NULL
) || (End
== NULL
)) {
941 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
942 return RETURN_LOAD_ERROR
;
945 if (ImageContext
->IsTeImage
) {
946 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
947 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
957 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
958 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
959 Size
= (UINTN
) Section
->SizeOfRawData
;
962 if (Section
->SizeOfRawData
> 0) {
963 if (!(ImageContext
->IsTeImage
)) {
964 Status
= ImageContext
->ImageRead (
965 ImageContext
->Handle
,
966 Section
->PointerToRawData
,
971 Status
= ImageContext
->ImageRead (
972 ImageContext
->Handle
,
973 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
979 if (RETURN_ERROR (Status
)) {
980 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
986 // If raw size is less then virtual size, zero fill the remaining
989 if (Size
< Section
->Misc
.VirtualSize
) {
990 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
1000 // Get image's entry point
1002 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1003 if (!(ImageContext
->IsTeImage
)) {
1005 // Sizes of AddressOfEntryPoint are different so we need to do this safely
1007 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1011 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1013 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
1019 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1021 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
1025 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
1026 (UINTN
)ImageContext
->ImageAddress
+
1027 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
1028 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
1029 (UINTN
)Hdr
.Te
->StrippedSize
1034 // Determine the size of the fixup data
1036 // Per the PE/COFF spec, you can't assume that a given data directory
1037 // is present in the image. You have to check the NumberOfRvaAndSizes in
1038 // the optional header to verify a desired directory entry is there.
1040 if (!(ImageContext
->IsTeImage
)) {
1041 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1045 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1046 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1051 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1052 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1055 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1056 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1058 ImageContext
->FixupDataSize
= 0;
1061 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1062 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1065 // Consumer must allocate a buffer for the relocation fixup log.
1066 // Only used for runtime drivers.
1068 ImageContext
->FixupData
= NULL
;
1071 // Load the Codeview info if present
1073 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1074 if (!(ImageContext
->IsTeImage
)) {
1075 DebugEntry
= PeCoffLoaderImageAddress (
1077 ImageContext
->DebugDirectoryEntryRva
1080 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1081 ImageContext
->ImageAddress
+
1082 ImageContext
->DebugDirectoryEntryRva
+
1083 sizeof(EFI_TE_IMAGE_HEADER
) -
1084 Hdr
.Te
->StrippedSize
1088 if (DebugEntry
!= NULL
) {
1089 TempDebugEntryRva
= DebugEntry
->RVA
;
1090 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1092 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1093 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1095 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1099 if (TempDebugEntryRva
!= 0) {
1100 if (!(ImageContext
->IsTeImage
)) {
1101 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1103 ImageContext
->CodeView
= (VOID
*)(
1104 (UINTN
)ImageContext
->ImageAddress
+
1105 (UINTN
)TempDebugEntryRva
+
1106 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1107 (UINTN
) Hdr
.Te
->StrippedSize
1111 if (ImageContext
->CodeView
== NULL
) {
1112 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1113 return RETURN_LOAD_ERROR
;
1116 if (DebugEntry
->RVA
== 0) {
1117 Size
= DebugEntry
->SizeOfData
;
1118 if (!(ImageContext
->IsTeImage
)) {
1119 Status
= ImageContext
->ImageRead (
1120 ImageContext
->Handle
,
1121 DebugEntry
->FileOffset
,
1123 ImageContext
->CodeView
1126 Status
= ImageContext
->ImageRead (
1127 ImageContext
->Handle
,
1128 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1130 ImageContext
->CodeView
1133 // Should we apply fix up to this field according to the size difference between PE and TE?
1134 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1135 // in original PE image.
1139 if (RETURN_ERROR (Status
)) {
1140 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1141 return RETURN_LOAD_ERROR
;
1144 DebugEntry
->RVA
= TempDebugEntryRva
;
1147 switch (*(UINT32
*) ImageContext
->CodeView
) {
1148 case CODEVIEW_SIGNATURE_NB10
:
1149 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1152 case CODEVIEW_SIGNATURE_RSDS
:
1153 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1156 case CODEVIEW_SIGNATURE_MTOC
:
1157 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
);
1168 // Get Image's HII resource section
1170 ImageContext
->HiiResourceData
= 0;
1171 if (!(ImageContext
->IsTeImage
)) {
1172 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1176 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1181 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
];
1184 if (DirectoryEntry
->Size
!= 0) {
1185 Base
= PeCoffLoaderImageAddress (ImageContext
, DirectoryEntry
->VirtualAddress
);
1187 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) Base
;
1188 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1190 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
1191 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
1192 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (Base
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
1194 if (ResourceDirectoryString
->Length
== 3 &&
1195 ResourceDirectoryString
->String
[0] == L
'H' &&
1196 ResourceDirectoryString
->String
[1] == L
'I' &&
1197 ResourceDirectoryString
->String
[2] == L
'I') {
1199 // Resource Type "HII" found
1201 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1203 // Move to next level - resource Name
1205 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1206 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1208 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1210 // Move to next level - resource Language
1212 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (Base
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
1213 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
1218 // Now it ought to be resource Data
1220 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
1221 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (Base
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
1222 ImageContext
->HiiResourceData
= (PHYSICAL_ADDRESS
) (UINTN
) PeCoffLoaderImageAddress (ImageContext
, ResourceDataEntry
->OffsetToData
);
1227 ResourceDirectoryEntry
++;
1238 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1241 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase
1242 and ImageSize so the image will execute correctly when the PE/COFF image is mapped
1243 to the address specified by VirtualImageBase. RelocationData must be identical
1244 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure
1245 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().
1247 Note that if the platform does not maintain coherency between the instruction cache(s) and the data
1248 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations
1249 prior to transferring control to a PE/COFF image that is loaded using this library.
1251 @param ImageBase Base address of a PE/COFF image that has been loaded
1252 and relocated into system memory.
1253 @param VirtImageBase The request virtual address that the PE/COFF image is to
1255 @param ImageSize The size, in bytes, of the PE/COFF image.
1256 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1257 image was relocated using PeCoffLoaderRelocateImage().
1262 PeCoffLoaderRelocateImageForRuntime (
1263 IN PHYSICAL_ADDRESS ImageBase
,
1264 IN PHYSICAL_ADDRESS VirtImageBase
,
1266 IN VOID
*RelocationData
1271 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1272 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1273 UINT32 NumberOfRvaAndSizes
;
1274 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1275 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1276 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1277 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1287 RETURN_STATUS Status
;
1290 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1291 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1292 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1295 // Find the image's relocate dir info
1297 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1298 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1300 // Valid DOS header so get address of PE header
1302 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1305 // No Dos header so assume image starts with PE header.
1307 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1310 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1312 // Not a valid PE image so Exit
1317 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1319 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1323 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1324 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1329 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1330 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1334 // Find the relocation block
1336 // Per the PE/COFF spec, you can't assume that a given data directory
1337 // is present in the image. You have to check the NumberOfRvaAndSizes in
1338 // the optional header to verify a desired directory entry is there.
1340 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1341 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1342 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1343 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1346 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1353 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1355 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1358 // Run the whole relocation block. And re-fixup data that has not been
1359 // modified. The FixupData is used to see if the image has been modified
1360 // since it was relocated. This is so data sections that have been updated
1361 // by code will not be fixed up, since that would set them back to
1364 FixupData
= RelocationData
;
1365 while (RelocBase
< RelocBaseEnd
) {
1367 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1368 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1369 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1372 // Run this relocation record
1374 while (Reloc
< RelocEnd
) {
1376 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1377 switch ((*Reloc
) >> 12) {
1379 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1382 case EFI_IMAGE_REL_BASED_HIGH
:
1383 Fixup16
= (UINT16
*) Fixup
;
1384 if (*(UINT16
*) FixupData
== *Fixup16
) {
1385 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1388 FixupData
= FixupData
+ sizeof (UINT16
);
1391 case EFI_IMAGE_REL_BASED_LOW
:
1392 Fixup16
= (UINT16
*) Fixup
;
1393 if (*(UINT16
*) FixupData
== *Fixup16
) {
1394 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1397 FixupData
= FixupData
+ sizeof (UINT16
);
1400 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1401 Fixup32
= (UINT32
*) Fixup
;
1402 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1403 if (*(UINT32
*) FixupData
== *Fixup32
) {
1404 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1407 FixupData
= FixupData
+ sizeof (UINT32
);
1410 case EFI_IMAGE_REL_BASED_DIR64
:
1411 Fixup64
= (UINT64
*)Fixup
;
1412 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1413 if (*(UINT64
*) FixupData
== *Fixup64
) {
1414 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1417 FixupData
= FixupData
+ sizeof (UINT64
);
1420 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1422 // Not valid Relocation type for UEFI image, ASSERT
1429 // Only Itanium requires ConvertPeImage_Ex
1431 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1432 if (RETURN_ERROR (Status
)) {
1437 // Next relocation record
1444 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1450 Reads contents of a PE/COFF image from a buffer in system memory.
1452 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1453 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1454 This function reads contents of the PE/COFF image that starts at the system memory
1455 address specified by FileHandle. The read operation copies ReadSize bytes from the
1456 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1457 The size of the buffer actually read is returned in ReadSize.
1459 If FileHandle is NULL, then ASSERT().
1460 If ReadSize is NULL, then ASSERT().
1461 If Buffer is NULL, then ASSERT().
1463 @param FileHandle Pointer to base of the input stream
1464 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1465 @param ReadSize On input, the size in bytes of the requested read operation.
1466 On output, the number of bytes actually read.
1467 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1469 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1474 PeCoffLoaderImageReadFromMemory (
1475 IN VOID
*FileHandle
,
1476 IN UINTN FileOffset
,
1477 IN OUT UINTN
*ReadSize
,
1481 ASSERT (ReadSize
!= NULL
);
1482 ASSERT (FileHandle
!= NULL
);
1483 ASSERT (Buffer
!= NULL
);
1485 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1486 return RETURN_SUCCESS
;
1490 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1491 Releases any environment specific resources that were allocated when the image
1492 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1494 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1495 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1496 this function can simply return RETURN_SUCCESS.
1498 If ImageContext is NULL, then ASSERT().
1500 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1501 image to be unloaded.
1503 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1507 PeCoffLoaderUnloadImage (
1508 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1512 // Applies additional environment specific actions to unload a
1513 // PE/COFF image if needed
1515 PeCoffLoaderUnloadImageExtraAction (ImageContext
);
1516 return RETURN_SUCCESS
;