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
== EFI_IMAGE_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 it's existance
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 anythine. Hdr.Pe32->OptionalHeader.Magic
96 // determins 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 suport 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 codeview pointer.
259 ImageContext
->CodeView
= NULL
;
260 ImageContext
->PdbPointer
= NULL
;
263 // Three cases with regards to relocations:
264 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
265 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
266 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
267 // has no base relocs to apply
268 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
270 // Look at the file header to determine if relocations have been stripped, and
271 // save this info in the image context for later use.
273 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
274 ImageContext
->RelocationsStripped
= TRUE
;
275 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
276 ImageContext
->RelocationsStripped
= TRUE
;
278 ImageContext
->RelocationsStripped
= FALSE
;
281 if (!(ImageContext
->IsTeImage
)) {
282 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
286 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
287 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
292 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
293 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
296 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
298 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
301 // Determine the file offset of the debug directory... This means we walk
302 // the sections to find which section contains the RVA of the debug
305 DebugDirectoryEntryFileOffset
= 0;
307 SectionHeaderOffset
= (UINTN
)(
308 ImageContext
->PeCoffHeaderOffset
+
310 sizeof (EFI_IMAGE_FILE_HEADER
) +
311 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
314 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
316 // Read section header from file
318 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
319 Status
= ImageContext
->ImageRead (
320 ImageContext
->Handle
,
325 if (RETURN_ERROR (Status
)) {
326 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
330 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
331 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
333 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
337 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
340 if (DebugDirectoryEntryFileOffset
!= 0) {
341 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
343 // Read next debug directory entry
345 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
346 Status
= ImageContext
->ImageRead (
347 ImageContext
->Handle
,
348 DebugDirectoryEntryFileOffset
,
352 if (RETURN_ERROR (Status
)) {
353 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
356 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
357 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
358 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
359 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
362 return RETURN_SUCCESS
;
369 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
370 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
371 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
373 DebugDirectoryEntryFileOffset
= 0;
375 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
377 // Read section header from file
379 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
380 Status
= ImageContext
->ImageRead (
381 ImageContext
->Handle
,
386 if (RETURN_ERROR (Status
)) {
387 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
391 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
392 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
393 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
394 SectionHeader
.VirtualAddress
+
395 SectionHeader
.PointerToRawData
+
396 sizeof (EFI_TE_IMAGE_HEADER
) -
397 Hdr
.Te
->StrippedSize
;
400 // File offset of the debug directory was found, if this is not the last
401 // section, then skip to the last section for calculating the image size.
403 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
404 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
405 Index
= Hdr
.Te
->NumberOfSections
- 1;
411 // In Te image header there is not a field to describe the ImageSize.
412 // Actually, the ImageSize equals the RVA plus the VirtualSize of
413 // the last section mapped into memory (Must be rounded up to
414 // a mulitple of Section Alignment). Per the PE/COFF specification, the
415 // section headers in the Section Table must appear in order of the RVA
416 // values for the corresponding sections. So the ImageSize can be determined
417 // by the RVA and the VirtualSize of the last section header in the
420 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
421 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
424 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
427 if (DebugDirectoryEntryFileOffset
!= 0) {
428 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
430 // Read next debug directory entry
432 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
433 Status
= ImageContext
->ImageRead (
434 ImageContext
->Handle
,
435 DebugDirectoryEntryFileOffset
,
439 if (RETURN_ERROR (Status
)) {
440 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
444 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
445 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
446 return RETURN_SUCCESS
;
452 return RETURN_SUCCESS
;
457 Converts an image address to the loaded address.
459 @param ImageContext The context of the image being loaded.
460 @param Address The address to be converted to the loaded address.
462 @return The converted address or NULL if the address can not be converted.
466 PeCoffLoaderImageAddress (
467 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
472 // @bug Check to make sure ImageSize is correct for the relocated image.
473 // it may only work for the file we start with and not the relocated 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 return RETURN_SUCCESS
;
548 // If the destination address is not 0, use that rather than the
549 // image address as the relocation target.
551 if (ImageContext
->DestinationAddress
!= 0) {
552 BaseAddress
= ImageContext
->DestinationAddress
;
554 BaseAddress
= ImageContext
->ImageAddress
;
557 if (!(ImageContext
->IsTeImage
)) {
558 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
560 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
562 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
566 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
567 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
569 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
570 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
575 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
576 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
578 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
579 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
583 // Find the relocation block
584 // Per the PE/COFF spec, you can't assume that a given data directory
585 // is present in the image. You have to check the NumberOfRvaAndSizes in
586 // the optional header to verify a desired directory entry is there.
589 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
590 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
591 ASSERT(RelocBase
!= NULL
);
592 RelocBaseEnd
= PeCoffLoaderImageAddress (
594 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
596 ASSERT(RelocBaseEnd
!=NULL
);
599 // Set base and end to bypass processing below.
601 RelocBase
= RelocBaseEnd
= 0;
604 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
605 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
606 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
609 // Find the relocation block
611 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
612 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
613 ImageContext
->ImageAddress
+
614 RelocDir
->VirtualAddress
+
615 sizeof(EFI_TE_IMAGE_HEADER
) -
618 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
622 // Run the relocation information and apply the fixups
624 FixupData
= ImageContext
->FixupData
;
625 while (RelocBase
< RelocBaseEnd
) {
627 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
628 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
629 if (!(ImageContext
->IsTeImage
)) {
630 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
631 ASSERT(FixupBase
!= NULL
);
633 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
634 RelocBase
->VirtualAddress
+
635 sizeof(EFI_TE_IMAGE_HEADER
) -
640 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
641 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
642 (UINTN
)ImageContext
->ImageSize
)) {
643 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
644 return RETURN_LOAD_ERROR
;
648 // Run this relocation record
650 while (Reloc
< RelocEnd
) {
652 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
653 switch ((*Reloc
) >> 12) {
654 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
657 case EFI_IMAGE_REL_BASED_HIGH
:
658 Fixup16
= (UINT16
*) Fixup
;
659 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
660 if (FixupData
!= NULL
) {
661 *(UINT16
*) FixupData
= *Fixup16
;
662 FixupData
= FixupData
+ sizeof (UINT16
);
666 case EFI_IMAGE_REL_BASED_LOW
:
667 Fixup16
= (UINT16
*) Fixup
;
668 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
669 if (FixupData
!= NULL
) {
670 *(UINT16
*) FixupData
= *Fixup16
;
671 FixupData
= FixupData
+ sizeof (UINT16
);
675 case EFI_IMAGE_REL_BASED_HIGHLOW
:
676 Fixup32
= (UINT32
*) Fixup
;
677 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
678 if (FixupData
!= NULL
) {
679 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
680 *(UINT32
*)FixupData
= *Fixup32
;
681 FixupData
= FixupData
+ sizeof (UINT32
);
685 case EFI_IMAGE_REL_BASED_DIR64
:
686 Fixup64
= (UINT64
*) Fixup
;
687 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
688 if (FixupData
!= NULL
) {
689 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
690 *(UINT64
*)(FixupData
) = *Fixup64
;
691 FixupData
= FixupData
+ sizeof(UINT64
);
697 // The common code does not handle some of the stranger IPF relocations
698 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
699 // on IPF and is a No-Op on other archtiectures.
701 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
702 if (RETURN_ERROR (Status
)) {
703 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
709 // Next relocation record
717 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
721 // Adjust the EntryPoint to match the linked-to address
723 if (ImageContext
->DestinationAddress
!= 0) {
724 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
725 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
727 return RETURN_SUCCESS
;
731 Loads a PE/COFF image into memory.
733 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
734 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
735 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
736 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
737 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,
738 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
739 fields of the ImageContext structure must be valid prior to invoking this service.
741 If ImageContext is NULL, then ASSERT().
743 @param ImageContext Pointer to the image context structure that describes the PE/COFF
744 image that is being loaded.
746 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
747 the ImageAddress and ImageSize fields of ImageContext.
748 Extended status information is in the ImageError field of ImageContext.
749 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
750 Extended status information is in the ImageError field of ImageContext.
751 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
752 Extended status information is in the ImageError field of ImageContext.
753 @retval RETURN_INVALID_PARAMETER The image address is invalid.
754 Extended status information is in the ImageError field of ImageContext.
759 PeCoffLoaderLoadImage (
760 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
763 RETURN_STATUS Status
;
764 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
765 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
766 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
767 EFI_IMAGE_SECTION_HEADER
*Section
;
768 UINTN NumberOfSections
;
773 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
774 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
776 UINT32 TempDebugEntryRva
;
777 UINT32 NumberOfRvaAndSizes
;
780 ASSERT (ImageContext
!= NULL
);
785 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
788 // Copy the provided context info into our local version, get what we
789 // can from the original image, and then use that to make sure everything
792 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
794 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
795 if (RETURN_ERROR (Status
)) {
800 // Make sure there is enough allocated space for the image being loaded
802 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
803 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
804 return RETURN_BUFFER_TOO_SMALL
;
806 if (ImageContext
->ImageAddress
== 0) {
808 // Image cannot be loaded into 0 address.
810 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
811 return RETURN_INVALID_PARAMETER
;
814 // If there's no relocations, then make sure it's not a runtime driver,
815 // and that it's being loaded at the linked address.
817 if (CheckContext
.RelocationsStripped
) {
819 // If the image does not contain relocations and it is a runtime driver
820 // then return an error.
822 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
823 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
824 return RETURN_LOAD_ERROR
;
827 // If the image does not contain relocations, and the requested load address
828 // is not the linked address, then return an error.
830 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
831 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
832 return RETURN_INVALID_PARAMETER
;
836 // Make sure the allocated space has the proper section alignment
838 if (!(ImageContext
->IsTeImage
)) {
839 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
840 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
841 return RETURN_INVALID_PARAMETER
;
845 // Read the entire PE/COFF or TE header into memory
847 if (!(ImageContext
->IsTeImage
)) {
848 Status
= ImageContext
->ImageRead (
849 ImageContext
->Handle
,
851 &ImageContext
->SizeOfHeaders
,
852 (VOID
*) (UINTN
) ImageContext
->ImageAddress
855 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
857 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
858 (UINTN
)ImageContext
->ImageAddress
+
859 ImageContext
->PeCoffHeaderOffset
+
861 sizeof(EFI_IMAGE_FILE_HEADER
) +
862 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
864 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
866 Status
= ImageContext
->ImageRead (
867 ImageContext
->Handle
,
869 &ImageContext
->SizeOfHeaders
,
870 (void *)(UINTN
)ImageContext
->ImageAddress
873 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
875 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
876 (UINTN
)ImageContext
->ImageAddress
+
877 sizeof(EFI_TE_IMAGE_HEADER
)
879 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
883 if (RETURN_ERROR (Status
)) {
884 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
885 return RETURN_LOAD_ERROR
;
889 // Load each section of the image
891 Section
= FirstSection
;
892 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
895 // Compute sections address
897 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
898 End
= PeCoffLoaderImageAddress (
900 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
902 if (ImageContext
->IsTeImage
) {
903 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
904 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
911 // If the base start or end address resolved to 0, then fail.
913 if ((Base
== NULL
) || (End
== NULL
)) {
914 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
915 return RETURN_LOAD_ERROR
;
921 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
922 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
923 Size
= (UINTN
) Section
->SizeOfRawData
;
926 if (Section
->SizeOfRawData
> 0) {
927 if (!(ImageContext
->IsTeImage
)) {
928 Status
= ImageContext
->ImageRead (
929 ImageContext
->Handle
,
930 Section
->PointerToRawData
,
935 Status
= ImageContext
->ImageRead (
936 ImageContext
->Handle
,
937 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
943 if (RETURN_ERROR (Status
)) {
944 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
950 // If raw size is less then virt size, zero fill the remaining
953 if (Size
< Section
->Misc
.VirtualSize
) {
954 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
964 // Get image's entry point
966 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
967 if (!(ImageContext
->IsTeImage
)) {
969 // Sizes of AddressOfEntryPoint are different so we need to do this safely
971 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
975 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
977 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
983 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
985 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
989 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
990 (UINTN
)ImageContext
->ImageAddress
+
991 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
992 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
993 (UINTN
)Hdr
.Te
->StrippedSize
998 // Determine the size of the fixup data
1000 // Per the PE/COFF spec, you can't assume that a given data directory
1001 // is present in the image. You have to check the NumberOfRvaAndSizes in
1002 // the optional header to verify a desired directory entry is there.
1004 if (!(ImageContext
->IsTeImage
)) {
1005 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1009 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1010 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1015 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1016 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1019 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1020 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1022 ImageContext
->FixupDataSize
= 0;
1025 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1026 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1029 // Consumer must allocate a buffer for the relocation fixup log.
1030 // Only used for runtime drivers.
1032 ImageContext
->FixupData
= NULL
;
1035 // Load the Codeview info if present
1037 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1038 if (!(ImageContext
->IsTeImage
)) {
1039 DebugEntry
= PeCoffLoaderImageAddress (
1041 ImageContext
->DebugDirectoryEntryRva
1044 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1045 ImageContext
->ImageAddress
+
1046 ImageContext
->DebugDirectoryEntryRva
+
1047 sizeof(EFI_TE_IMAGE_HEADER
) -
1048 Hdr
.Te
->StrippedSize
1052 if (DebugEntry
!= NULL
) {
1053 TempDebugEntryRva
= DebugEntry
->RVA
;
1054 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1056 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1057 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1059 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1063 if (TempDebugEntryRva
!= 0) {
1064 if (!(ImageContext
->IsTeImage
)) {
1065 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1067 ImageContext
->CodeView
= (VOID
*)(
1068 (UINTN
)ImageContext
->ImageAddress
+
1069 (UINTN
)TempDebugEntryRva
+
1070 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1071 (UINTN
) Hdr
.Te
->StrippedSize
1075 if (ImageContext
->CodeView
== NULL
) {
1076 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1077 return RETURN_LOAD_ERROR
;
1080 if (DebugEntry
->RVA
== 0) {
1081 Size
= DebugEntry
->SizeOfData
;
1082 if (!(ImageContext
->IsTeImage
)) {
1083 Status
= ImageContext
->ImageRead (
1084 ImageContext
->Handle
,
1085 DebugEntry
->FileOffset
,
1087 ImageContext
->CodeView
1090 Status
= ImageContext
->ImageRead (
1091 ImageContext
->Handle
,
1092 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1094 ImageContext
->CodeView
1097 // Should we apply fix up to this field according to the size difference between PE and TE?
1098 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1099 // in original PE image.
1103 if (RETURN_ERROR (Status
)) {
1104 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1105 return RETURN_LOAD_ERROR
;
1108 DebugEntry
->RVA
= TempDebugEntryRva
;
1111 switch (*(UINT32
*) ImageContext
->CodeView
) {
1112 case CODEVIEW_SIGNATURE_NB10
:
1113 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1116 case CODEVIEW_SIGNATURE_RSDS
:
1117 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1132 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1135 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1136 the fixups with a virtual mapping.
1139 @param ImageBase Base address of a PE/COFF image that has been loaded
1140 and relocated into system memory.
1141 @param VirtImageBase The request virtual address that the PE/COFF image is to
1143 @param ImageSize The size, in bytes, of the PE/COFF image.
1144 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF
1145 image was relocated using PeCoffLoaderRelocateImage().
1150 PeCoffLoaderRelocateImageForRuntime (
1151 IN PHYSICAL_ADDRESS ImageBase
,
1152 IN PHYSICAL_ADDRESS VirtImageBase
,
1154 IN VOID
*RelocationData
1159 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1160 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1161 UINT32 NumberOfRvaAndSizes
;
1162 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1163 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1164 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1165 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1175 RETURN_STATUS Status
;
1178 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1179 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1180 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1183 // Find the image's relocate dir info
1185 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1186 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1188 // Valid DOS header so get address of PE header
1190 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1193 // No Dos header so assume image starts with PE header.
1195 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1198 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1200 // Not a valid PE image so Exit
1205 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1207 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1211 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1212 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1217 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1218 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1222 // Find the relocation block
1224 // Per the PE/COFF spec, you can't assume that a given data directory
1225 // is present in the image. You have to check the NumberOfRvaAndSizes in
1226 // the optional header to verify a desired directory entry is there.
1228 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1229 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1230 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1231 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1234 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1241 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1243 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1246 // Run the whole relocation block. And re-fixup data that has not been
1247 // modified. The FixupData is used to see if the image has been modified
1248 // since it was relocated. This is so data sections that have been updated
1249 // by code will not be fixed up, since that would set them back to
1252 FixupData
= RelocationData
;
1253 while (RelocBase
< RelocBaseEnd
) {
1255 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1256 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1257 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1260 // Run this relocation record
1262 while (Reloc
< RelocEnd
) {
1264 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1265 switch ((*Reloc
) >> 12) {
1267 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1270 case EFI_IMAGE_REL_BASED_HIGH
:
1271 Fixup16
= (UINT16
*) Fixup
;
1272 if (*(UINT16
*) FixupData
== *Fixup16
) {
1273 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1276 FixupData
= FixupData
+ sizeof (UINT16
);
1279 case EFI_IMAGE_REL_BASED_LOW
:
1280 Fixup16
= (UINT16
*) Fixup
;
1281 if (*(UINT16
*) FixupData
== *Fixup16
) {
1282 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1285 FixupData
= FixupData
+ sizeof (UINT16
);
1288 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1289 Fixup32
= (UINT32
*) Fixup
;
1290 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1291 if (*(UINT32
*) FixupData
== *Fixup32
) {
1292 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1295 FixupData
= FixupData
+ sizeof (UINT32
);
1298 case EFI_IMAGE_REL_BASED_DIR64
:
1299 Fixup64
= (UINT64
*)Fixup
;
1300 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1301 if (*(UINT64
*) FixupData
== *Fixup64
) {
1302 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1305 FixupData
= FixupData
+ sizeof (UINT64
);
1308 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1310 // Not valid Relocation type for UEFI image, ASSERT
1317 // Only Itanium requires ConvertPeImage_Ex
1319 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1320 if (RETURN_ERROR (Status
)) {
1325 // Next relocation record
1332 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1338 Reads contents of a PE/COFF image from a buffer in system memory.
1340 This is the default implementation of a PE_COFF_LOADER_READ_FILE function
1341 that assumes FileHandle pointer to the beginning of a PE/COFF image.
1342 This function reads contents of the PE/COFF image that starts at the system memory
1343 address specified by FileHandle. The read operation copies ReadSize bytes from the
1344 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.
1345 The size of the buffer actually read is returned in ReadSize.
1347 If FileHandle is NULL, then ASSERT().
1348 If ReadSize is NULL, then ASSERT().
1349 If Buffer is NULL, then ASSERT().
1351 @param FileHandle Pointer to base of the input stream
1352 @param FileOffset Offset into the PE/COFF image to begin the read operation.
1353 @param ReadSize On input, the size in bytes of the requested read operation.
1354 On output, the number of bytes actually read.
1355 @param Buffer Output buffer that contains the data read from the PE/COFF image.
1357 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1362 PeCoffLoaderImageReadFromMemory (
1363 IN VOID
*FileHandle
,
1364 IN UINTN FileOffset
,
1365 IN OUT UINTN
*ReadSize
,
1369 ASSERT (ReadSize
!= NULL
);
1370 ASSERT (FileHandle
!= NULL
);
1371 ASSERT (Buffer
!= NULL
);
1373 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1374 return RETURN_SUCCESS
;
1378 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1379 Releases any environment specific resources that were allocated when the image
1380 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
1382 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1383 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1384 this function can simply return RETURN_SUCCESS.
1386 If ImageContext is NULL, then ASSERT().
1388 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1389 image to be unloaded.
1391 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1395 PeCoffLoaderUnloadImage (
1396 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1399 return RETURN_SUCCESS
;