4 This PE/COFF loader supports loading any PE32 or PE32+ image type, but
5 only supports relocating IA32, X64, IPF, and EBC images.
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // Include common header file for this module.
23 #include "BasePeCoffLibInternals.h"
26 Retrieves the magic value from the PE/COFF header.
28 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
30 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
31 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
35 PeCoffLoaderGetPeHeaderMagicValue (
36 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
40 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
41 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
42 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
43 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
45 if (Hdr
.Pe32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
46 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
49 // Return the magic value from the PC/COFF Optional Header
51 return Hdr
.Pe32
->OptionalHeader
.Magic
;
56 Retrieves the PE or TE Header from a PE/COFF or TE image.
58 @param ImageContext The context of the image being loaded.
59 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
61 @retval RETURN_SUCCESS The PE or TE Header is read.
62 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
66 PeCoffLoaderGetPeHeader (
67 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
68 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
72 EFI_IMAGE_DOS_HEADER DosHdr
;
77 // Read the DOS image header to check for it's existance
79 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
80 Status
= ImageContext
->ImageRead (
86 if (RETURN_ERROR (Status
)) {
87 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
91 ImageContext
->PeCoffHeaderOffset
= 0;
92 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
94 // DOS image header is present, so read the PE header after the DOS image
97 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
101 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
102 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
103 // determins if this is a PE32 or PE32+ image. The magic is in the same
104 // location in both images.
106 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
107 Status
= ImageContext
->ImageRead (
108 ImageContext
->Handle
,
109 ImageContext
->PeCoffHeaderOffset
,
113 if (RETURN_ERROR (Status
)) {
114 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
119 // Use Signature to figure out if we understand the image format
121 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
122 ImageContext
->IsTeImage
= TRUE
;
123 ImageContext
->Machine
= Hdr
.Te
->Machine
;
124 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
125 ImageContext
->ImageSize
= 0;
126 ImageContext
->SectionAlignment
= 4096;
127 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
129 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
130 ImageContext
->IsTeImage
= FALSE
;
131 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
133 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
135 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
139 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
140 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
141 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
142 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
144 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
148 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
149 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
150 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
151 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
153 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
154 return RETURN_UNSUPPORTED
;
157 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
158 return RETURN_UNSUPPORTED
;
161 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
163 // If the PE/COFF loader does not support the image type return
164 // unsupported. This library can suport lots of types of images
165 // this does not mean the user of this library can call the entry
166 // point of the image.
168 return RETURN_UNSUPPORTED
;
171 return RETURN_SUCCESS
;
176 Retrieves information about a PE/COFF image.
178 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
179 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
180 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
181 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
182 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
183 computing the fields of ImageContext, then the error status is returned in the ImageError field of
186 @param ImageContext Pointer to the image context structure that describes the PE/COFF
187 image that needs to be examined by this function.
189 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
190 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
191 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
196 PeCoffLoaderGetImageInfo (
197 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
200 RETURN_STATUS Status
;
201 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
202 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
203 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
206 UINTN DebugDirectoryEntryRva
;
207 UINTN DebugDirectoryEntryFileOffset
;
208 UINTN SectionHeaderOffset
;
209 EFI_IMAGE_SECTION_HEADER SectionHeader
;
210 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
211 UINT32 NumberOfRvaAndSizes
;
214 if (NULL
== ImageContext
) {
215 return RETURN_INVALID_PARAMETER
;
220 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
222 Hdr
.Union
= &HdrData
;
223 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
224 if (RETURN_ERROR (Status
)) {
228 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
231 // Retrieve the base address of the image
233 if (!(ImageContext
->IsTeImage
)) {
234 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
238 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
243 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
246 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
250 // Initialize the alternate destination address to 0 indicating that it
251 // should not be used.
253 ImageContext
->DestinationAddress
= 0;
256 // Initialize the codeview pointer.
258 ImageContext
->CodeView
= NULL
;
259 ImageContext
->PdbPointer
= NULL
;
262 // Three cases with regards to relocations:
263 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
264 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
265 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
266 // has no base relocs to apply
267 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
269 // Look at the file header to determine if relocations have been stripped, and
270 // save this info in the image context for later use.
272 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
273 ImageContext
->RelocationsStripped
= TRUE
;
274 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
275 ImageContext
->RelocationsStripped
= TRUE
;
277 ImageContext
->RelocationsStripped
= FALSE
;
280 if (!(ImageContext
->IsTeImage
)) {
281 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
285 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
286 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
291 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
292 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
295 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
297 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
300 // Determine the file offset of the debug directory... This means we walk
301 // the sections to find which section contains the RVA of the debug
304 DebugDirectoryEntryFileOffset
= 0;
306 SectionHeaderOffset
= (UINTN
)(
307 ImageContext
->PeCoffHeaderOffset
+
309 sizeof (EFI_IMAGE_FILE_HEADER
) +
310 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
313 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
315 // Read section header from file
317 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
318 Status
= ImageContext
->ImageRead (
319 ImageContext
->Handle
,
324 if (RETURN_ERROR (Status
)) {
325 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
329 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
330 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
332 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
336 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
339 if (DebugDirectoryEntryFileOffset
!= 0) {
340 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
342 // Read next debug directory entry
344 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
345 Status
= ImageContext
->ImageRead (
346 ImageContext
->Handle
,
347 DebugDirectoryEntryFileOffset
,
351 if (RETURN_ERROR (Status
)) {
352 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
355 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
356 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
357 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
358 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
361 return RETURN_SUCCESS
;
368 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
369 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
370 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
372 DebugDirectoryEntryFileOffset
= 0;
374 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
376 // Read section header from file
378 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
379 Status
= ImageContext
->ImageRead (
380 ImageContext
->Handle
,
385 if (RETURN_ERROR (Status
)) {
386 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
390 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
391 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
392 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
393 SectionHeader
.VirtualAddress
+
394 SectionHeader
.PointerToRawData
+
395 sizeof (EFI_TE_IMAGE_HEADER
) -
396 Hdr
.Te
->StrippedSize
;
399 // File offset of the debug directory was found, if this is not the last
400 // section, then skip to the last section for calculating the image size.
402 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
403 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
404 Index
= Hdr
.Te
->NumberOfSections
- 1;
410 // In Te image header there is not a field to describe the ImageSize.
411 // Actually, the ImageSize equals the RVA plus the VirtualSize of
412 // the last section mapped into memory (Must be rounded up to
413 // a mulitple of Section Alignment). Per the PE/COFF specification, the
414 // section headers in the Section Table must appear in order of the RVA
415 // values for the corresponding sections. So the ImageSize can be determined
416 // by the RVA and the VirtualSize of the last section header in the
419 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
420 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
+
421 ImageContext
->SectionAlignment
- 1) & ~(ImageContext
->SectionAlignment
- 1);
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.
490 If ImageContext is NULL, then ASSERT().
492 @param ImageContext Pointer to the image context structure that describes the PE/COFF
493 image that is being relocated.
495 @retval RETURN_SUCCESS The PE/COFF image was relocated.
496 Extended status information is in the ImageError field of ImageContext.
497 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
498 Extended status information is in the ImageError field of ImageContext.
499 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
500 Extended status information is in the ImageError field of ImageContext.
505 PeCoffLoaderRelocateImage (
506 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
509 RETURN_STATUS Status
;
510 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
511 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
513 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
514 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
523 PHYSICAL_ADDRESS BaseAddress
;
524 UINT32 NumberOfRvaAndSizes
;
527 ASSERT (ImageContext
!= NULL
);
532 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
535 // If there are no relocation entries, then we are done
537 if (ImageContext
->RelocationsStripped
) {
538 return RETURN_SUCCESS
;
542 // If the destination address is not 0, use that rather than the
543 // image address as the relocation target.
545 if (ImageContext
->DestinationAddress
!= 0) {
546 BaseAddress
= ImageContext
->DestinationAddress
;
548 BaseAddress
= ImageContext
->ImageAddress
;
551 if (!(ImageContext
->IsTeImage
)) {
552 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
554 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
556 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
560 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
561 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
563 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
564 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
569 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
570 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
572 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
573 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
577 // Find the relocation block
578 // Per the PE/COFF spec, you can't assume that a given data directory
579 // is present in the image. You have to check the NumberOfRvaAndSizes in
580 // the optional header to verify a desired directory entry is there.
583 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
584 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
585 RelocBaseEnd
= PeCoffLoaderImageAddress (
587 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
591 // Set base and end to bypass processing below.
593 RelocBase
= RelocBaseEnd
= 0;
596 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
597 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
598 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
601 // Find the relocation block
603 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
604 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
605 ImageContext
->ImageAddress
+
606 RelocDir
->VirtualAddress
+
607 sizeof(EFI_TE_IMAGE_HEADER
) -
610 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
614 // Run the relocation information and apply the fixups
616 FixupData
= ImageContext
->FixupData
;
617 while (RelocBase
< RelocBaseEnd
) {
619 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
620 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
621 if (!(ImageContext
->IsTeImage
)) {
622 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
624 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
625 RelocBase
->VirtualAddress
+
626 sizeof(EFI_TE_IMAGE_HEADER
) -
631 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
632 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
633 (UINTN
)ImageContext
->ImageSize
)) {
634 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
635 return RETURN_LOAD_ERROR
;
639 // Run this relocation record
641 while (Reloc
< RelocEnd
) {
643 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
644 switch ((*Reloc
) >> 12) {
645 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
648 case EFI_IMAGE_REL_BASED_HIGH
:
649 F16
= (UINT16
*) Fixup
;
650 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
651 if (FixupData
!= NULL
) {
652 *(UINT16
*) FixupData
= *F16
;
653 FixupData
= FixupData
+ sizeof (UINT16
);
657 case EFI_IMAGE_REL_BASED_LOW
:
658 F16
= (UINT16
*) Fixup
;
659 *F16
= (UINT16
) (*F16
+ (UINT16
) Adjust
);
660 if (FixupData
!= NULL
) {
661 *(UINT16
*) FixupData
= *F16
;
662 FixupData
= FixupData
+ sizeof (UINT16
);
666 case EFI_IMAGE_REL_BASED_HIGHLOW
:
667 F32
= (UINT32
*) Fixup
;
668 *F32
= *F32
+ (UINT32
) Adjust
;
669 if (FixupData
!= NULL
) {
670 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
671 *(UINT32
*)FixupData
= *F32
;
672 FixupData
= FixupData
+ sizeof (UINT32
);
676 case EFI_IMAGE_REL_BASED_DIR64
:
677 F64
= (UINT64
*) Fixup
;
678 *F64
= *F64
+ (UINT64
) Adjust
;
679 if (FixupData
!= NULL
) {
680 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
681 *(UINT64
*)(FixupData
) = *F64
;
682 FixupData
= FixupData
+ sizeof(UINT64
);
688 // The common code does not handle some of the stranger IPF relocations
689 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
690 // on IPF and is a No-Op on other archtiectures.
692 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
693 if (RETURN_ERROR (Status
)) {
694 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
700 // Next relocation record
708 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
712 // Adjust the EntryPoint to match the linked-to address
714 if (ImageContext
->DestinationAddress
!= 0) {
715 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
716 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
718 return RETURN_SUCCESS
;
722 Loads a PE/COFF image into memory.
724 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
725 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
726 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
727 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
728 If ImageContext is NULL, then ASSERT().
730 @param ImageContext Pointer to the image context structure that describes the PE/COFF
731 image that is being loaded.
733 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
734 the ImageAddress and ImageSize fields of ImageContext.
735 Extended status information is in the ImageError field of ImageContext.
736 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
737 Extended status information is in the ImageError field of ImageContext.
738 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
739 Extended status information is in the ImageError field of ImageContext.
740 @retval RETURN_INVALID_PARAMETER The image address is invalid.
741 Extended status information is in the ImageError field of ImageContext.
746 PeCoffLoaderLoadImage (
747 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
750 RETURN_STATUS Status
;
751 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
752 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
753 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
754 EFI_IMAGE_SECTION_HEADER
*Section
;
755 UINTN NumberOfSections
;
760 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
761 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
763 UINT32 TempDebugEntryRva
;
764 UINT32 NumberOfRvaAndSizes
;
767 ASSERT (ImageContext
!= NULL
);
772 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
775 // Copy the provided context info into our local version, get what we
776 // can from the original image, and then use that to make sure everything
779 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
781 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
782 if (RETURN_ERROR (Status
)) {
787 // Make sure there is enough allocated space for the image being loaded
789 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
790 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
791 return RETURN_BUFFER_TOO_SMALL
;
793 if (ImageContext
->ImageAddress
== 0) {
795 // Image cannot be loaded into 0 address.
797 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
798 return RETURN_INVALID_PARAMETER
;
801 // If there's no relocations, then make sure it's not a runtime driver,
802 // and that it's being loaded at the linked address.
804 if (CheckContext
.RelocationsStripped
) {
806 // If the image does not contain relocations and it is a runtime driver
807 // then return an error.
809 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
810 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
811 return RETURN_LOAD_ERROR
;
814 // If the image does not contain relocations, and the requested load address
815 // is not the linked address, then return an error.
817 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
818 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
819 return RETURN_INVALID_PARAMETER
;
823 // Make sure the allocated space has the proper section alignment
825 if (!(ImageContext
->IsTeImage
)) {
826 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
827 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
828 return RETURN_INVALID_PARAMETER
;
832 // Read the entire PE/COFF or TE header into memory
834 if (!(ImageContext
->IsTeImage
)) {
835 Status
= ImageContext
->ImageRead (
836 ImageContext
->Handle
,
838 &ImageContext
->SizeOfHeaders
,
839 (VOID
*) (UINTN
) ImageContext
->ImageAddress
842 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
844 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
845 (UINTN
)ImageContext
->ImageAddress
+
846 ImageContext
->PeCoffHeaderOffset
+
848 sizeof(EFI_IMAGE_FILE_HEADER
) +
849 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
851 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
853 Status
= ImageContext
->ImageRead (
854 ImageContext
->Handle
,
856 &ImageContext
->SizeOfHeaders
,
857 (void *)(UINTN
)ImageContext
->ImageAddress
860 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
862 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
863 (UINTN
)ImageContext
->ImageAddress
+
864 sizeof(EFI_TE_IMAGE_HEADER
)
866 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
870 if (RETURN_ERROR (Status
)) {
871 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
872 return RETURN_LOAD_ERROR
;
876 // Load each section of the image
878 Section
= FirstSection
;
879 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
882 // Compute sections address
884 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
885 End
= PeCoffLoaderImageAddress (
887 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
889 if (ImageContext
->IsTeImage
) {
890 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
891 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
898 // If the base start or end address resolved to 0, then fail.
900 if ((Base
== NULL
) || (End
== NULL
)) {
901 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
902 return RETURN_LOAD_ERROR
;
908 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
909 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
910 Size
= (UINTN
) Section
->SizeOfRawData
;
913 if (Section
->SizeOfRawData
) {
914 if (!(ImageContext
->IsTeImage
)) {
915 Status
= ImageContext
->ImageRead (
916 ImageContext
->Handle
,
917 Section
->PointerToRawData
,
922 Status
= ImageContext
->ImageRead (
923 ImageContext
->Handle
,
924 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
930 if (RETURN_ERROR (Status
)) {
931 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
937 // If raw size is less then virt size, zero fill the remaining
940 if (Size
< Section
->Misc
.VirtualSize
) {
941 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
951 // Get image's entry point
953 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
954 if (!(ImageContext
->IsTeImage
)) {
956 // Sizes of AddressOfEntryPoint are different so we need to do this safely
958 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
962 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
964 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
970 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
972 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
976 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
977 (UINTN
)ImageContext
->ImageAddress
+
978 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
979 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
980 (UINTN
)Hdr
.Te
->StrippedSize
985 // Determine the size of the fixup data
987 // Per the PE/COFF spec, you can't assume that a given data directory
988 // is present in the image. You have to check the NumberOfRvaAndSizes in
989 // the optional header to verify a desired directory entry is there.
991 if (!(ImageContext
->IsTeImage
)) {
992 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
996 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
997 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1002 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1003 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1006 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1007 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1009 ImageContext
->FixupDataSize
= 0;
1012 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1013 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1016 // Consumer must allocate a buffer for the relocation fixup log.
1017 // Only used for runtime drivers.
1019 ImageContext
->FixupData
= NULL
;
1022 // Load the Codeview info if present
1024 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1025 if (!(ImageContext
->IsTeImage
)) {
1026 DebugEntry
= PeCoffLoaderImageAddress (
1028 ImageContext
->DebugDirectoryEntryRva
1031 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1032 ImageContext
->ImageAddress
+
1033 ImageContext
->DebugDirectoryEntryRva
+
1034 sizeof(EFI_TE_IMAGE_HEADER
) -
1035 Hdr
.Te
->StrippedSize
1039 if (DebugEntry
!= NULL
) {
1040 TempDebugEntryRva
= DebugEntry
->RVA
;
1041 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1043 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1044 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1046 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1050 if (TempDebugEntryRva
!= 0) {
1051 if (!(ImageContext
->IsTeImage
)) {
1052 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1054 ImageContext
->CodeView
= (VOID
*)(
1055 (UINTN
)ImageContext
->ImageAddress
+
1056 (UINTN
)TempDebugEntryRva
+
1057 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1058 (UINTN
) Hdr
.Te
->StrippedSize
1062 if (ImageContext
->CodeView
== NULL
) {
1063 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1064 return RETURN_LOAD_ERROR
;
1067 if (DebugEntry
->RVA
== 0) {
1068 Size
= DebugEntry
->SizeOfData
;
1069 if (!(ImageContext
->IsTeImage
)) {
1070 Status
= ImageContext
->ImageRead (
1071 ImageContext
->Handle
,
1072 DebugEntry
->FileOffset
,
1074 ImageContext
->CodeView
1077 Status
= ImageContext
->ImageRead (
1078 ImageContext
->Handle
,
1079 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1081 ImageContext
->CodeView
1084 // Should we apply fix up to this field according to the size difference between PE and TE?
1085 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1086 // in original PE image.
1090 if (RETURN_ERROR (Status
)) {
1091 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1092 return RETURN_LOAD_ERROR
;
1095 DebugEntry
->RVA
= TempDebugEntryRva
;
1098 switch (*(UINT32
*) ImageContext
->CodeView
) {
1099 case CODEVIEW_SIGNATURE_NB10
:
1100 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1103 case CODEVIEW_SIGNATURE_RSDS
:
1104 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1119 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1122 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1123 the fixups with a virtual mapping.
1126 @param ImageBase Base address of relocated image
1127 @param VirtImageBase Virtual mapping for ImageBase
1128 @param ImageSize Size of the image to relocate
1129 @param RelocationData Location to place results of read
1134 PeCoffLoaderRelocateImageForRuntime (
1135 IN PHYSICAL_ADDRESS ImageBase
,
1136 IN PHYSICAL_ADDRESS VirtImageBase
,
1138 IN VOID
*RelocationData
1143 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1144 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1145 UINT32 NumberOfRvaAndSizes
;
1146 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1147 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1148 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1149 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1159 RETURN_STATUS Status
;
1162 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1163 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1164 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1167 // Find the image's relocate dir info
1169 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1170 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1172 // Valid DOS header so get address of PE header
1174 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1177 // No Dos header so assume image starts with PE header.
1179 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1182 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1184 // Not a valid PE image so Exit
1189 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1191 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1195 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1196 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1201 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1202 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1206 // Find the relocation block
1208 // Per the PE/COFF spec, you can't assume that a given data directory
1209 // is present in the image. You have to check the NumberOfRvaAndSizes in
1210 // the optional header to verify a desired directory entry is there.
1212 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1213 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1214 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1215 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1218 // Cannot find relocations, cannot continue
1224 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1227 // Run the whole relocation block. And re-fixup data that has not been
1228 // modified. The FixupData is used to see if the image has been modified
1229 // since it was relocated. This is so data sections that have been updated
1230 // by code will not be fixed up, since that would set them back to
1233 FixupData
= RelocationData
;
1234 while (RelocBase
< RelocBaseEnd
) {
1236 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1237 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1238 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1241 // Run this relocation record
1243 while (Reloc
< RelocEnd
) {
1245 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1246 switch ((*Reloc
) >> 12) {
1248 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1251 case EFI_IMAGE_REL_BASED_HIGH
:
1252 F16
= (UINT16
*) Fixup
;
1253 if (*(UINT16
*) FixupData
== *F16
) {
1254 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1257 FixupData
= FixupData
+ sizeof (UINT16
);
1260 case EFI_IMAGE_REL_BASED_LOW
:
1261 F16
= (UINT16
*) Fixup
;
1262 if (*(UINT16
*) FixupData
== *F16
) {
1263 *F16
= (UINT16
) (*F16
+ ((UINT16
) Adjust
& 0xffff));
1266 FixupData
= FixupData
+ sizeof (UINT16
);
1269 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1270 F32
= (UINT32
*) Fixup
;
1271 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1272 if (*(UINT32
*) FixupData
== *F32
) {
1273 *F32
= *F32
+ (UINT32
) Adjust
;
1276 FixupData
= FixupData
+ sizeof (UINT32
);
1279 case EFI_IMAGE_REL_BASED_DIR64
:
1280 F64
= (UINT64
*)Fixup
;
1281 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1282 if (*(UINT64
*) FixupData
== *F64
) {
1283 *F64
= *F64
+ (UINT64
)Adjust
;
1286 FixupData
= FixupData
+ sizeof (UINT64
);
1289 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1291 // Not implemented, but not used in UEFI 2.0
1298 // Only Itanium requires ConvertPeImage_Ex
1300 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1301 if (RETURN_ERROR (Status
)) {
1306 // Next relocation record
1313 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1319 ImageRead function that operates on a memory buffer whos base is passed into
1322 @param FileHandle Ponter to baes of the input stream
1323 @param FileOffset Offset to the start of the buffer
1324 @param ReadSize Number of bytes to copy into the buffer
1325 @param Buffer Location to place results of read
1327 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1332 PeCoffLoaderImageReadFromMemory (
1333 IN VOID
*FileHandle
,
1334 IN UINTN FileOffset
,
1335 IN OUT UINTN
*ReadSize
,
1339 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1340 return RETURN_SUCCESS
;
1344 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1346 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1347 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1348 this function can simply return RETURN_SUCCESS.
1350 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1351 image to be unloaded.
1353 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1357 PeCoffLoaderUnloadImage (
1358 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1361 return RETURN_SUCCESS
;