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.
16 Module Name: PeCoffLoader.c
22 Performs an Itanium-based specific relocation fixup and is a no-op on other
25 @param Reloc Pointer to the relocation record.
26 @param Fixup Pointer to the address to fix up.
27 @param FixupData Pointer to a buffer to log the fixups.
28 @param Adjust The offset to adjust the fixup.
34 PeCoffLoaderRelocateImageEx (
37 IN OUT CHAR8
**FixupData
,
43 Performs an Itanium-based specific re-relocation fixup and is a no-op on other
44 instruction sets. This is used to re-relocated the image into the EFI virtual
45 space for runtime calls.
47 @param Reloc Pointer to the relocation record.
48 @param Fixup Pointer to the address to fix up.
49 @param FixupData Pointer to a buffer to log the fixups.
50 @param Adjust The offset to adjust the fixup.
56 PeHotRelocateImageEx (
59 IN OUT CHAR8
**FixupData
,
65 Returns TRUE if the machine type of PE/COFF image is supported. Supported
66 does not mean the image can be executed it means the PE/COFF loader supports
67 loading and relocating of the image type. It's up to the caller to support
70 @param Machine Machine type from the PE Header.
72 @return TRUE if this PE/COFF loader can load the image
76 PeCoffLoaderImageFormatSupported (
82 Retrieves the magic value from the PE/COFF header.
84 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
86 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
87 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
91 PeCoffLoaderGetPeHeaderMagicValue (
92 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
96 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
97 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
98 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
99 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
101 if (Hdr
.Pe32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
102 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
105 // Return the magic value from the PC/COFF Optional Header
107 return Hdr
.Pe32
->OptionalHeader
.Magic
;
112 Retrieves the PE or TE Header from a PE/COFF or TE image.
114 @param ImageContext The context of the image being loaded.
115 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
117 @retval RETURN_SUCCESS The PE or TE Header is read.
118 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
122 PeCoffLoaderGetPeHeader (
123 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
124 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
127 RETURN_STATUS Status
;
128 EFI_IMAGE_DOS_HEADER DosHdr
;
133 // Read the DOS image header to check for it's existance
135 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
136 Status
= ImageContext
->ImageRead (
137 ImageContext
->Handle
,
142 if (RETURN_ERROR (Status
)) {
143 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
147 ImageContext
->PeCoffHeaderOffset
= 0;
148 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
150 // DOS image header is present, so read the PE header after the DOS image
153 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
157 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
158 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
159 // determins if this is a PE32 or PE32+ image. The magic is in the same
160 // location in both images.
162 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
163 Status
= ImageContext
->ImageRead (
164 ImageContext
->Handle
,
165 ImageContext
->PeCoffHeaderOffset
,
169 if (RETURN_ERROR (Status
)) {
170 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
175 // Use Signature to figure out if we understand the image format
177 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
178 ImageContext
->IsTeImage
= TRUE
;
179 ImageContext
->Machine
= Hdr
.Te
->Machine
;
180 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
181 ImageContext
->ImageSize
= 0;
182 ImageContext
->SectionAlignment
= 4096;
183 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
185 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
186 ImageContext
->IsTeImage
= FALSE
;
187 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
189 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
191 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
195 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
196 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
197 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
198 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
200 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
204 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
205 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
206 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
207 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
209 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
210 return RETURN_UNSUPPORTED
;
213 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
214 return RETURN_UNSUPPORTED
;
217 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
219 // If the PE/COFF loader does not support the image type return
220 // unsupported. This library can suport lots of types of images
221 // this does not mean the user of this library can call the entry
222 // point of the image.
224 return RETURN_UNSUPPORTED
;
227 return RETURN_SUCCESS
;
232 Retrieves information about a PE/COFF image.
234 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
235 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
236 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
237 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
238 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
239 computing the fields of ImageContext, then the error status is returned in the ImageError field of
242 @param ImageContext Pointer to the image context structure that describes the PE/COFF
243 image that needs to be examined by this function.
245 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
246 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
247 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
252 PeCoffLoaderGetImageInfo (
253 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
256 RETURN_STATUS Status
;
257 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
258 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
259 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
262 UINTN DebugDirectoryEntryRva
;
263 UINTN DebugDirectoryEntryFileOffset
;
264 UINTN SectionHeaderOffset
;
265 EFI_IMAGE_SECTION_HEADER SectionHeader
;
266 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
267 UINT32 NumberOfRvaAndSizes
;
270 if (NULL
== ImageContext
) {
271 return RETURN_INVALID_PARAMETER
;
276 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
278 Hdr
.Union
= &HdrData
;
279 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
280 if (RETURN_ERROR (Status
)) {
284 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
287 // Retrieve the base address of the image
289 if (!(ImageContext
->IsTeImage
)) {
290 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
294 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
299 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
302 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
);
306 // Initialize the alternate destination address to 0 indicating that it
307 // should not be used.
309 ImageContext
->DestinationAddress
= 0;
312 // Initialize the codeview pointer.
314 ImageContext
->CodeView
= NULL
;
315 ImageContext
->PdbPointer
= NULL
;
318 // Three cases with regards to relocations:
319 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
320 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
321 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
322 // has no base relocs to apply
323 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
325 // Look at the file header to determine if relocations have been stripped, and
326 // save this info in the image context for later use.
328 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
329 ImageContext
->RelocationsStripped
= TRUE
;
331 ImageContext
->RelocationsStripped
= FALSE
;
334 if (!(ImageContext
->IsTeImage
)) {
335 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
339 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
340 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
345 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
346 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
349 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
351 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
354 // Determine the file offset of the debug directory... This means we walk
355 // the sections to find which section contains the RVA of the debug
358 DebugDirectoryEntryFileOffset
= 0;
360 SectionHeaderOffset
= (UINTN
)(
361 ImageContext
->PeCoffHeaderOffset
+
363 sizeof (EFI_IMAGE_FILE_HEADER
) +
364 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
367 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
369 // Read section header from file
371 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
372 Status
= ImageContext
->ImageRead (
373 ImageContext
->Handle
,
378 if (RETURN_ERROR (Status
)) {
379 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
383 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
384 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
386 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
390 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
393 if (DebugDirectoryEntryFileOffset
!= 0) {
394 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= Size
) {
396 // Read next debug directory entry
398 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
399 Status
= ImageContext
->ImageRead (
400 ImageContext
->Handle
,
401 DebugDirectoryEntryFileOffset
,
405 if (RETURN_ERROR (Status
)) {
406 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
409 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
410 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
411 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
412 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
415 return RETURN_SUCCESS
;
422 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
423 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
424 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
426 DebugDirectoryEntryFileOffset
= 0;
428 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
430 // Read section header from file
432 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
433 Status
= ImageContext
->ImageRead (
434 ImageContext
->Handle
,
439 if (RETURN_ERROR (Status
)) {
440 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
444 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
445 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
446 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
447 SectionHeader
.VirtualAddress
+
448 SectionHeader
.PointerToRawData
+
449 sizeof (EFI_TE_IMAGE_HEADER
) -
450 Hdr
.Te
->StrippedSize
;
453 // File offset of the debug directory was found, if this is not the last
454 // section, then skip to the last section for calculating the image size.
456 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
457 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
458 Index
= Hdr
.Te
->NumberOfSections
- 1;
464 // In Te image header there is not a field to describe the ImageSize.
465 // Actually, the ImageSize equals the RVA plus the VirtualSize of
466 // the last section mapped into memory (Must be rounded up to
467 // a mulitple of Section Alignment). Per the PE/COFF specification, the
468 // section headers in the Section Table must appear in order of the RVA
469 // values for the corresponding sections. So the ImageSize can be determined
470 // by the RVA and the VirtualSize of the last section header in the
473 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
474 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
+
475 ImageContext
->SectionAlignment
- 1) & ~(ImageContext
->SectionAlignment
- 1);
478 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
481 if (DebugDirectoryEntryFileOffset
!= 0) {
482 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= Size
) {
484 // Read next debug directory entry
486 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
487 Status
= ImageContext
->ImageRead (
488 ImageContext
->Handle
,
489 DebugDirectoryEntryFileOffset
,
493 if (RETURN_ERROR (Status
)) {
494 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
498 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
499 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
500 return RETURN_SUCCESS
;
506 return RETURN_SUCCESS
;
511 Converts an image address to the loaded address.
513 @param ImageContext The context of the image being loaded.
514 @param Address The address to be converted to the loaded address.
516 @return The converted address or NULL if the address can not be converted.
520 PeCoffLoaderImageAddress (
521 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
526 // @bug Check to make sure ImageSize is correct for the relocated image.
527 // it may only work for the file we start with and not the relocated image
529 if (Address
>= ImageContext
->ImageSize
) {
530 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
534 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
538 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
540 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
541 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
542 of ImageContext as the relocation base address. The caller must allocate the relocation
543 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
544 If ImageContext is NULL, then ASSERT().
546 @param ImageContext Pointer to the image context structure that describes the PE/COFF
547 image that is being relocated.
549 @retval RETURN_SUCCESS The PE/COFF image was relocated.
550 Extended status information is in the ImageError field of ImageContext.
551 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
552 Extended status information is in the ImageError field of ImageContext.
553 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
554 Extended status information is in the ImageError field of ImageContext.
559 PeCoffLoaderRelocateImage (
560 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
563 RETURN_STATUS Status
;
564 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
565 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
567 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
568 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
577 PHYSICAL_ADDRESS BaseAddress
;
578 UINT32 NumberOfRvaAndSizes
;
581 ASSERT (ImageContext
!= NULL
);
586 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
589 // If there are no relocation entries, then we are done
591 if (ImageContext
->RelocationsStripped
) {
592 return RETURN_SUCCESS
;
596 // If the destination address is not 0, use that rather than the
597 // image address as the relocation target.
599 if (ImageContext
->DestinationAddress
!= 0) {
600 BaseAddress
= ImageContext
->DestinationAddress
;
602 BaseAddress
= ImageContext
->ImageAddress
;
605 if (!(ImageContext
->IsTeImage
)) {
606 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
608 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
610 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
614 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
615 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
617 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
618 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
623 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
624 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
626 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
627 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
631 // Find the relocation block
632 // Per the PE/COFF spec, you can't assume that a given data directory
633 // is present in the image. You have to check the NumberOfRvaAndSizes in
634 // the optional header to verify a desired directory entry is there.
637 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
638 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
639 RelocBaseEnd
= PeCoffLoaderImageAddress (
641 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
645 // Set base and end to bypass processing below.
647 RelocBase
= RelocBaseEnd
= 0;
650 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
651 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->ImageBase
);
652 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
);
655 // Find the relocation block
657 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
658 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
659 ImageContext
->ImageAddress
+
660 RelocDir
->VirtualAddress
+
661 sizeof(EFI_TE_IMAGE_HEADER
) -
664 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
668 // Run the relocation information and apply the fixups
670 FixupData
= ImageContext
->FixupData
;
671 while (RelocBase
< RelocBaseEnd
) {
673 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
674 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
675 if (!(ImageContext
->IsTeImage
)) {
676 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
678 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
679 RelocBase
->VirtualAddress
+
680 sizeof(EFI_TE_IMAGE_HEADER
) -
685 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
686 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
687 (UINTN
)ImageContext
->ImageSize
)) {
688 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
689 return RETURN_LOAD_ERROR
;
693 // Run this relocation record
695 while (Reloc
< RelocEnd
) {
697 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
698 switch ((*Reloc
) >> 12) {
699 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
702 case EFI_IMAGE_REL_BASED_HIGH
:
703 F16
= (UINT16
*) Fixup
;
704 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
705 if (FixupData
!= NULL
) {
706 *(UINT16
*) FixupData
= *F16
;
707 FixupData
= FixupData
+ sizeof (UINT16
);
711 case EFI_IMAGE_REL_BASED_LOW
:
712 F16
= (UINT16
*) Fixup
;
713 *F16
= (UINT16
) (*F16
+ (UINT16
) Adjust
);
714 if (FixupData
!= NULL
) {
715 *(UINT16
*) FixupData
= *F16
;
716 FixupData
= FixupData
+ sizeof (UINT16
);
720 case EFI_IMAGE_REL_BASED_HIGHLOW
:
721 F32
= (UINT32
*) Fixup
;
722 *F32
= *F32
+ (UINT32
) Adjust
;
723 if (FixupData
!= NULL
) {
724 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
725 *(UINT32
*)FixupData
= *F32
;
726 FixupData
= FixupData
+ sizeof (UINT32
);
730 case EFI_IMAGE_REL_BASED_DIR64
:
731 F64
= (UINT64
*) Fixup
;
732 *F64
= *F64
+ (UINT64
) Adjust
;
733 if (FixupData
!= NULL
) {
734 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
735 *(UINT64
*)(FixupData
) = *F64
;
736 FixupData
= FixupData
+ sizeof(UINT64
);
742 // The common code does not handle some of the stranger IPF relocations
743 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
744 // on IPF and is a No-Op on other archtiectures.
746 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
747 if (RETURN_ERROR (Status
)) {
748 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
754 // Next relocation record
762 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
765 return RETURN_SUCCESS
;
769 Loads a PE/COFF image into memory.
771 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
772 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
773 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
774 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
775 If ImageContext is NULL, then ASSERT().
777 @param ImageContext Pointer to the image context structure that describes the PE/COFF
778 image that is being loaded.
780 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
781 the ImageAddress and ImageSize fields of ImageContext.
782 Extended status information is in the ImageError field of ImageContext.
783 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
784 Extended status information is in the ImageError field of ImageContext.
785 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
786 Extended status information is in the ImageError field of ImageContext.
787 @retval RETURN_INVALID_PARAMETER The image address is invalid.
788 Extended status information is in the ImageError field of ImageContext.
793 PeCoffLoaderLoadImage (
794 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
797 RETURN_STATUS Status
;
798 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
799 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
800 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
801 EFI_IMAGE_SECTION_HEADER
*Section
;
802 UINTN NumberOfSections
;
807 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
808 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
810 UINT32 TempDebugEntryRva
;
811 UINT32 NumberOfRvaAndSizes
;
814 ASSERT (ImageContext
!= NULL
);
819 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
822 // Copy the provided context info into our local version, get what we
823 // can from the original image, and then use that to make sure everything
826 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
828 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
829 if (RETURN_ERROR (Status
)) {
834 // Make sure there is enough allocated space for the image being loaded
836 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
837 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
838 return RETURN_BUFFER_TOO_SMALL
;
840 if (ImageContext
->ImageAddress
== 0) {
842 // Image cannot be loaded into 0 address.
844 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
845 return RETURN_INVALID_PARAMETER
;
848 // If there's no relocations, then make sure it's not a runtime driver,
849 // and that it's being loaded at the linked address.
851 if (CheckContext
.RelocationsStripped
) {
853 // If the image does not contain relocations and it is a runtime driver
854 // then return an error.
856 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
857 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
858 return RETURN_LOAD_ERROR
;
861 // If the image does not contain relocations, and the requested load address
862 // is not the linked address, then return an error.
864 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
865 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
866 return RETURN_INVALID_PARAMETER
;
870 // Make sure the allocated space has the proper section alignment
872 if (!(ImageContext
->IsTeImage
)) {
873 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
874 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
875 return RETURN_INVALID_PARAMETER
;
879 // Read the entire PE/COFF or TE header into memory
881 if (!(ImageContext
->IsTeImage
)) {
882 Status
= ImageContext
->ImageRead (
883 ImageContext
->Handle
,
885 &ImageContext
->SizeOfHeaders
,
886 (VOID
*) (UINTN
) ImageContext
->ImageAddress
889 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
891 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
892 (UINTN
)ImageContext
->ImageAddress
+
893 ImageContext
->PeCoffHeaderOffset
+
895 sizeof(EFI_IMAGE_FILE_HEADER
) +
896 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
898 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
900 Status
= ImageContext
->ImageRead (
901 ImageContext
->Handle
,
903 &ImageContext
->SizeOfHeaders
,
904 (void *)(UINTN
)ImageContext
->ImageAddress
907 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
909 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
910 (UINTN
)ImageContext
->ImageAddress
+
911 sizeof(EFI_TE_IMAGE_HEADER
)
913 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
917 if (RETURN_ERROR (Status
)) {
918 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
919 return RETURN_LOAD_ERROR
;
923 // Load each section of the image
925 Section
= FirstSection
;
926 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
929 // Compute sections address
931 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
932 End
= PeCoffLoaderImageAddress (
934 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
936 if (ImageContext
->IsTeImage
) {
937 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
938 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
945 // If the base start or end address resolved to 0, then fail.
947 if ((Base
== NULL
) || (End
== NULL
)) {
948 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
949 return RETURN_LOAD_ERROR
;
955 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
956 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
957 Size
= (UINTN
) Section
->SizeOfRawData
;
960 if (Section
->SizeOfRawData
) {
961 if (!(ImageContext
->IsTeImage
)) {
962 Status
= ImageContext
->ImageRead (
963 ImageContext
->Handle
,
964 Section
->PointerToRawData
,
969 Status
= ImageContext
->ImageRead (
970 ImageContext
->Handle
,
971 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
977 if (RETURN_ERROR (Status
)) {
978 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
984 // If raw size is less then virt size, zero fill the remaining
987 if (Size
< Section
->Misc
.VirtualSize
) {
988 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
998 // Get image's entry point
1000 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1001 if (!(ImageContext
->IsTeImage
)) {
1003 // Sizes of AddressOfEntryPoint are different so we need to do this safely
1005 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1009 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1011 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
1017 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
1019 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
1023 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
1024 (UINTN
)ImageContext
->ImageAddress
+
1025 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
1026 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
1027 (UINTN
)Hdr
.Te
->StrippedSize
1032 // Determine the size of the fixup data
1034 // Per the PE/COFF spec, you can't assume that a given data directory
1035 // is present in the image. You have to check the NumberOfRvaAndSizes in
1036 // the optional header to verify a desired directory entry is there.
1038 if (!(ImageContext
->IsTeImage
)) {
1039 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1043 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1044 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1049 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1050 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1053 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1054 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1056 ImageContext
->FixupDataSize
= 0;
1059 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1060 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1063 // Consumer must allocate a buffer for the relocation fixup log.
1064 // Only used for runtime drivers.
1066 ImageContext
->FixupData
= NULL
;
1069 // Load the Codeview info if present
1071 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1072 if (!(ImageContext
->IsTeImage
)) {
1073 DebugEntry
= PeCoffLoaderImageAddress (
1075 ImageContext
->DebugDirectoryEntryRva
1078 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1079 ImageContext
->ImageAddress
+
1080 ImageContext
->DebugDirectoryEntryRva
+
1081 sizeof(EFI_TE_IMAGE_HEADER
) -
1082 Hdr
.Te
->StrippedSize
1086 if (DebugEntry
!= NULL
) {
1087 TempDebugEntryRva
= DebugEntry
->RVA
;
1088 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1090 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1091 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1093 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1097 if (TempDebugEntryRva
!= 0) {
1098 if (!(ImageContext
->IsTeImage
)) {
1099 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1101 ImageContext
->CodeView
= (VOID
*)(
1102 (UINTN
)ImageContext
->ImageAddress
+
1103 (UINTN
)TempDebugEntryRva
+
1104 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1105 (UINTN
) Hdr
.Te
->StrippedSize
1109 if (ImageContext
->CodeView
== NULL
) {
1110 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1111 return RETURN_LOAD_ERROR
;
1114 if (DebugEntry
->RVA
== 0) {
1115 Size
= DebugEntry
->SizeOfData
;
1116 if (!(ImageContext
->IsTeImage
)) {
1117 Status
= ImageContext
->ImageRead (
1118 ImageContext
->Handle
,
1119 DebugEntry
->FileOffset
,
1121 ImageContext
->CodeView
1124 Status
= ImageContext
->ImageRead (
1125 ImageContext
->Handle
,
1126 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1128 ImageContext
->CodeView
1131 // Should we apply fix up to this field according to the size difference between PE and TE?
1132 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1133 // in original PE image.
1137 if (RETURN_ERROR (Status
)) {
1138 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1139 return RETURN_LOAD_ERROR
;
1142 DebugEntry
->RVA
= TempDebugEntryRva
;
1145 switch (*(UINT32
*) ImageContext
->CodeView
) {
1146 case CODEVIEW_SIGNATURE_NB10
:
1147 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1150 case CODEVIEW_SIGNATURE_RSDS
:
1151 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1166 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1169 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1170 the fixups with a virtual mapping.
1173 @param ImageBase Base address of relocated image
1174 @param VirtImageBase Virtual mapping for ImageBase
1175 @param ImageSize Size of the image to relocate
1176 @param RelocationData Location to place results of read
1181 PeCoffLoaderRelocateImageForRuntime (
1182 IN PHYSICAL_ADDRESS ImageBase
,
1183 IN PHYSICAL_ADDRESS VirtImageBase
,
1185 IN VOID
*RelocationData
1190 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1191 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1192 UINT32 NumberOfRvaAndSizes
;
1193 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1194 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1195 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1196 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1206 RETURN_STATUS Status
;
1209 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1210 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1211 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1214 // Find the image's relocate dir info
1216 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1217 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1219 // Valid DOS header so get address of PE header
1221 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1224 // No Dos header so assume image starts with PE header.
1226 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1229 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1231 // Not a valid PE image so Exit
1236 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1238 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1242 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1243 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1248 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1249 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1253 // Find the relocation block
1255 // Per the PE/COFF spec, you can't assume that a given data directory
1256 // is present in the image. You have to check the NumberOfRvaAndSizes in
1257 // the optional header to verify a desired directory entry is there.
1259 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1260 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1261 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1262 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1265 // Cannot find relocations, cannot continue
1271 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1274 // Run the whole relocation block. And re-fixup data that has not been
1275 // modified. The FixupData is used to see if the image has been modified
1276 // since it was relocated. This is so data sections that have been updated
1277 // by code will not be fixed up, since that would set them back to
1280 FixupData
= RelocationData
;
1281 while (RelocBase
< RelocBaseEnd
) {
1283 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1284 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1285 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1288 // Run this relocation record
1290 while (Reloc
< RelocEnd
) {
1292 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1293 switch ((*Reloc
) >> 12) {
1295 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1298 case EFI_IMAGE_REL_BASED_HIGH
:
1299 F16
= (UINT16
*) Fixup
;
1300 if (*(UINT16
*) FixupData
== *F16
) {
1301 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1304 FixupData
= FixupData
+ sizeof (UINT16
);
1307 case EFI_IMAGE_REL_BASED_LOW
:
1308 F16
= (UINT16
*) Fixup
;
1309 if (*(UINT16
*) FixupData
== *F16
) {
1310 *F16
= (UINT16
) (*F16
+ ((UINT16
) Adjust
& 0xffff));
1313 FixupData
= FixupData
+ sizeof (UINT16
);
1316 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1317 F32
= (UINT32
*) Fixup
;
1318 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1319 if (*(UINT32
*) FixupData
== *F32
) {
1320 *F32
= *F32
+ (UINT32
) Adjust
;
1323 FixupData
= FixupData
+ sizeof (UINT32
);
1326 case EFI_IMAGE_REL_BASED_DIR64
:
1327 F64
= (UINT64
*)Fixup
;
1328 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1329 if (*(UINT64
*) FixupData
== *F64
) {
1330 *F64
= *F64
+ (UINT64
)Adjust
;
1333 FixupData
= FixupData
+ sizeof (UINT64
);
1336 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1338 // Not implemented, but not used in EFI 1.0
1345 // Only Itanium requires ConvertPeImage_Ex
1347 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1348 if (RETURN_ERROR (Status
)) {
1353 // Next relocation record
1360 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1366 ImageRead function that operates on a memory buffer whos base is passed into
1369 @param FileHandle Ponter to baes of the input stream
1370 @param FileOffset Offset to the start of the buffer
1371 @param ReadSize Number of bytes to copy into the buffer
1372 @param Buffer Location to place results of read
1374 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1379 PeCoffLoaderImageReadFromMemory (
1380 IN VOID
*FileHandle
,
1381 IN UINTN FileOffset
,
1382 IN OUT UINTN
*ReadSize
,
1386 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1387 return RETURN_SUCCESS
;