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, ImageAddress, ImageSize, DestinationAddress, RelocationsStripped,
176 SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva fields of the ImageContext structure.
177 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
178 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
179 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
180 computing the fields of ImageContext, then the error status is returned in the ImageError field of
183 @param ImageContext Pointer to the image context structure that describes the PE/COFF
184 image that needs to be examined by this function.
186 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
187 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
188 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
193 PeCoffLoaderGetImageInfo (
194 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
197 RETURN_STATUS Status
;
198 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
199 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
200 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
203 UINTN DebugDirectoryEntryRva
;
204 UINTN DebugDirectoryEntryFileOffset
;
205 UINTN SectionHeaderOffset
;
206 EFI_IMAGE_SECTION_HEADER SectionHeader
;
207 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
208 UINT32 NumberOfRvaAndSizes
;
211 if (ImageContext
== NULL
) {
212 return RETURN_INVALID_PARAMETER
;
217 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
219 Hdr
.Union
= &HdrData
;
220 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
221 if (RETURN_ERROR (Status
)) {
225 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
228 // Retrieve the base address of the image
230 if (!(ImageContext
->IsTeImage
)) {
231 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
235 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
240 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
243 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
247 // Initialize the alternate destination address to 0 indicating that it
248 // should not be used.
250 ImageContext
->DestinationAddress
= 0;
253 // Initialize the codeview pointer.
255 ImageContext
->CodeView
= NULL
;
256 ImageContext
->PdbPointer
= NULL
;
259 // Three cases with regards to relocations:
260 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
261 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
262 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
263 // has no base relocs to apply
264 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
266 // Look at the file header to determine if relocations have been stripped, and
267 // save this info in the image context for later use.
269 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
270 ImageContext
->RelocationsStripped
= TRUE
;
271 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
272 ImageContext
->RelocationsStripped
= TRUE
;
274 ImageContext
->RelocationsStripped
= FALSE
;
277 if (!(ImageContext
->IsTeImage
)) {
278 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
282 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
283 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
288 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
289 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
292 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
294 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
297 // Determine the file offset of the debug directory... This means we walk
298 // the sections to find which section contains the RVA of the debug
301 DebugDirectoryEntryFileOffset
= 0;
303 SectionHeaderOffset
= (UINTN
)(
304 ImageContext
->PeCoffHeaderOffset
+
306 sizeof (EFI_IMAGE_FILE_HEADER
) +
307 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
310 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
312 // Read section header from file
314 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
315 Status
= ImageContext
->ImageRead (
316 ImageContext
->Handle
,
321 if (RETURN_ERROR (Status
)) {
322 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
326 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
327 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
329 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
333 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
336 if (DebugDirectoryEntryFileOffset
!= 0) {
337 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
339 // Read next debug directory entry
341 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
342 Status
= ImageContext
->ImageRead (
343 ImageContext
->Handle
,
344 DebugDirectoryEntryFileOffset
,
348 if (RETURN_ERROR (Status
)) {
349 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
352 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
353 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
354 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
355 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
358 return RETURN_SUCCESS
;
365 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
366 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
367 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
369 DebugDirectoryEntryFileOffset
= 0;
371 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
373 // Read section header from file
375 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
376 Status
= ImageContext
->ImageRead (
377 ImageContext
->Handle
,
382 if (RETURN_ERROR (Status
)) {
383 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
387 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
388 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
389 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
390 SectionHeader
.VirtualAddress
+
391 SectionHeader
.PointerToRawData
+
392 sizeof (EFI_TE_IMAGE_HEADER
) -
393 Hdr
.Te
->StrippedSize
;
396 // File offset of the debug directory was found, if this is not the last
397 // section, then skip to the last section for calculating the image size.
399 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
400 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
401 Index
= Hdr
.Te
->NumberOfSections
- 1;
407 // In Te image header there is not a field to describe the ImageSize.
408 // Actually, the ImageSize equals the RVA plus the VirtualSize of
409 // the last section mapped into memory (Must be rounded up to
410 // a mulitple of Section Alignment). Per the PE/COFF specification, the
411 // section headers in the Section Table must appear in order of the RVA
412 // values for the corresponding sections. So the ImageSize can be determined
413 // by the RVA and the VirtualSize of the last section header in the
416 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
417 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
420 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
423 if (DebugDirectoryEntryFileOffset
!= 0) {
424 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
426 // Read next debug directory entry
428 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
429 Status
= ImageContext
->ImageRead (
430 ImageContext
->Handle
,
431 DebugDirectoryEntryFileOffset
,
435 if (RETURN_ERROR (Status
)) {
436 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
440 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
441 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
442 return RETURN_SUCCESS
;
448 return RETURN_SUCCESS
;
453 Converts an image address to the loaded address.
455 @param ImageContext The context of the image being loaded.
456 @param Address The address to be converted to the loaded address.
458 @return The converted address or NULL if the address can not be converted.
462 PeCoffLoaderImageAddress (
463 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
468 // @bug Check to make sure ImageSize is correct for the relocated image.
469 // it may only work for the file we start with and not the relocated image
471 if (Address
>= ImageContext
->ImageSize
) {
472 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
476 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
480 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
482 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
483 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
484 of ImageContext as the relocation base address. The caller must allocate the relocation
485 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
486 If ImageContext is NULL, then ASSERT().
488 @param ImageContext Pointer to the image context structure that describes the PE/COFF
489 image that is being relocated.
491 @retval RETURN_SUCCESS The PE/COFF image was relocated.
492 Extended status information is in the ImageError field of ImageContext.
493 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
494 Extended status information is in the ImageError field of ImageContext.
495 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
496 Extended status information is in the ImageError field of ImageContext.
501 PeCoffLoaderRelocateImage (
502 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
505 RETURN_STATUS Status
;
506 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
507 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
509 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
510 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
519 PHYSICAL_ADDRESS BaseAddress
;
520 UINT32 NumberOfRvaAndSizes
;
523 ASSERT (ImageContext
!= NULL
);
528 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
531 // If there are no relocation entries, then we are done
533 if (ImageContext
->RelocationsStripped
) {
534 return RETURN_SUCCESS
;
538 // If the destination address is not 0, use that rather than the
539 // image address as the relocation target.
541 if (ImageContext
->DestinationAddress
!= 0) {
542 BaseAddress
= ImageContext
->DestinationAddress
;
544 BaseAddress
= ImageContext
->ImageAddress
;
547 if (!(ImageContext
->IsTeImage
)) {
548 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
550 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
552 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
556 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
557 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
559 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
560 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
565 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
566 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
568 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
569 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
573 // Find the relocation block
574 // Per the PE/COFF spec, you can't assume that a given data directory
575 // is present in the image. You have to check the NumberOfRvaAndSizes in
576 // the optional header to verify a desired directory entry is there.
579 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
580 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
581 RelocBaseEnd
= PeCoffLoaderImageAddress (
583 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
587 // Set base and end to bypass processing below.
589 RelocBase
= RelocBaseEnd
= 0;
592 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
593 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
594 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
597 // Find the relocation block
599 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
600 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
601 ImageContext
->ImageAddress
+
602 RelocDir
->VirtualAddress
+
603 sizeof(EFI_TE_IMAGE_HEADER
) -
606 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
610 // Run the relocation information and apply the fixups
612 FixupData
= ImageContext
->FixupData
;
613 while (RelocBase
< RelocBaseEnd
) {
615 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
616 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
617 if (!(ImageContext
->IsTeImage
)) {
618 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
620 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
621 RelocBase
->VirtualAddress
+
622 sizeof(EFI_TE_IMAGE_HEADER
) -
627 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
628 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
629 (UINTN
)ImageContext
->ImageSize
)) {
630 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
631 return RETURN_LOAD_ERROR
;
635 // Run this relocation record
637 while (Reloc
< RelocEnd
) {
639 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
640 switch ((*Reloc
) >> 12) {
641 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
644 case EFI_IMAGE_REL_BASED_HIGH
:
645 F16
= (UINT16
*) Fixup
;
646 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
647 if (FixupData
!= NULL
) {
648 *(UINT16
*) FixupData
= *F16
;
649 FixupData
= FixupData
+ sizeof (UINT16
);
653 case EFI_IMAGE_REL_BASED_LOW
:
654 F16
= (UINT16
*) Fixup
;
655 *F16
= (UINT16
) (*F16
+ (UINT16
) Adjust
);
656 if (FixupData
!= NULL
) {
657 *(UINT16
*) FixupData
= *F16
;
658 FixupData
= FixupData
+ sizeof (UINT16
);
662 case EFI_IMAGE_REL_BASED_HIGHLOW
:
663 F32
= (UINT32
*) Fixup
;
664 *F32
= *F32
+ (UINT32
) Adjust
;
665 if (FixupData
!= NULL
) {
666 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
667 *(UINT32
*)FixupData
= *F32
;
668 FixupData
= FixupData
+ sizeof (UINT32
);
672 case EFI_IMAGE_REL_BASED_DIR64
:
673 F64
= (UINT64
*) Fixup
;
674 *F64
= *F64
+ (UINT64
) Adjust
;
675 if (FixupData
!= NULL
) {
676 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
677 *(UINT64
*)(FixupData
) = *F64
;
678 FixupData
= FixupData
+ sizeof(UINT64
);
684 // The common code does not handle some of the stranger IPF relocations
685 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
686 // on IPF and is a No-Op on other archtiectures.
688 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
689 if (RETURN_ERROR (Status
)) {
690 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
696 // Next relocation record
704 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
708 // Adjust the EntryPoint to match the linked-to address
710 if (ImageContext
->DestinationAddress
!= 0) {
711 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
712 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
714 return RETURN_SUCCESS
;
718 Loads a PE/COFF image into memory.
720 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
721 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
722 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
723 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
724 If ImageContext is NULL, then ASSERT().
726 @param ImageContext Pointer to the image context structure that describes the PE/COFF
727 image that is being loaded.
729 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
730 the ImageAddress and ImageSize fields of ImageContext.
731 Extended status information is in the ImageError field of ImageContext.
732 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
733 Extended status information is in the ImageError field of ImageContext.
734 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
735 Extended status information is in the ImageError field of ImageContext.
736 @retval RETURN_INVALID_PARAMETER The image address is invalid.
737 Extended status information is in the ImageError field of ImageContext.
742 PeCoffLoaderLoadImage (
743 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
746 RETURN_STATUS Status
;
747 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
748 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
749 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
750 EFI_IMAGE_SECTION_HEADER
*Section
;
751 UINTN NumberOfSections
;
756 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
757 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
759 UINT32 TempDebugEntryRva
;
760 UINT32 NumberOfRvaAndSizes
;
763 ASSERT (ImageContext
!= NULL
);
768 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
771 // Copy the provided context info into our local version, get what we
772 // can from the original image, and then use that to make sure everything
775 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
777 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
778 if (RETURN_ERROR (Status
)) {
783 // Make sure there is enough allocated space for the image being loaded
785 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
786 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
787 return RETURN_BUFFER_TOO_SMALL
;
789 if (ImageContext
->ImageAddress
== 0) {
791 // Image cannot be loaded into 0 address.
793 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
794 return RETURN_INVALID_PARAMETER
;
797 // If there's no relocations, then make sure it's not a runtime driver,
798 // and that it's being loaded at the linked address.
800 if (CheckContext
.RelocationsStripped
) {
802 // If the image does not contain relocations and it is a runtime driver
803 // then return an error.
805 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
806 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
807 return RETURN_LOAD_ERROR
;
810 // If the image does not contain relocations, and the requested load address
811 // is not the linked address, then return an error.
813 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
814 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
815 return RETURN_INVALID_PARAMETER
;
819 // Make sure the allocated space has the proper section alignment
821 if (!(ImageContext
->IsTeImage
)) {
822 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
823 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
824 return RETURN_INVALID_PARAMETER
;
828 // Read the entire PE/COFF or TE header into memory
830 if (!(ImageContext
->IsTeImage
)) {
831 Status
= ImageContext
->ImageRead (
832 ImageContext
->Handle
,
834 &ImageContext
->SizeOfHeaders
,
835 (VOID
*) (UINTN
) ImageContext
->ImageAddress
838 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
840 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
841 (UINTN
)ImageContext
->ImageAddress
+
842 ImageContext
->PeCoffHeaderOffset
+
844 sizeof(EFI_IMAGE_FILE_HEADER
) +
845 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
847 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
849 Status
= ImageContext
->ImageRead (
850 ImageContext
->Handle
,
852 &ImageContext
->SizeOfHeaders
,
853 (void *)(UINTN
)ImageContext
->ImageAddress
856 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
858 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
859 (UINTN
)ImageContext
->ImageAddress
+
860 sizeof(EFI_TE_IMAGE_HEADER
)
862 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
866 if (RETURN_ERROR (Status
)) {
867 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
868 return RETURN_LOAD_ERROR
;
872 // Load each section of the image
874 Section
= FirstSection
;
875 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
878 // Compute sections address
880 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
881 End
= PeCoffLoaderImageAddress (
883 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
885 if (ImageContext
->IsTeImage
) {
886 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
887 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
894 // If the base start or end address resolved to 0, then fail.
896 if ((Base
== NULL
) || (End
== NULL
)) {
897 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
898 return RETURN_LOAD_ERROR
;
904 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
905 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
906 Size
= (UINTN
) Section
->SizeOfRawData
;
909 if (Section
->SizeOfRawData
> 0) {
910 if (!(ImageContext
->IsTeImage
)) {
911 Status
= ImageContext
->ImageRead (
912 ImageContext
->Handle
,
913 Section
->PointerToRawData
,
918 Status
= ImageContext
->ImageRead (
919 ImageContext
->Handle
,
920 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
926 if (RETURN_ERROR (Status
)) {
927 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
933 // If raw size is less then virt size, zero fill the remaining
936 if (Size
< Section
->Misc
.VirtualSize
) {
937 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
947 // Get image's entry point
949 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
950 if (!(ImageContext
->IsTeImage
)) {
952 // Sizes of AddressOfEntryPoint are different so we need to do this safely
954 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
958 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
960 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
966 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
968 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
972 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
973 (UINTN
)ImageContext
->ImageAddress
+
974 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
975 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
976 (UINTN
)Hdr
.Te
->StrippedSize
981 // Determine the size of the fixup data
983 // Per the PE/COFF spec, you can't assume that a given data directory
984 // is present in the image. You have to check the NumberOfRvaAndSizes in
985 // the optional header to verify a desired directory entry is there.
987 if (!(ImageContext
->IsTeImage
)) {
988 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
992 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
993 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
998 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
999 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1002 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1003 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1005 ImageContext
->FixupDataSize
= 0;
1008 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1009 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1012 // Consumer must allocate a buffer for the relocation fixup log.
1013 // Only used for runtime drivers.
1015 ImageContext
->FixupData
= NULL
;
1018 // Load the Codeview info if present
1020 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1021 if (!(ImageContext
->IsTeImage
)) {
1022 DebugEntry
= PeCoffLoaderImageAddress (
1024 ImageContext
->DebugDirectoryEntryRva
1027 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1028 ImageContext
->ImageAddress
+
1029 ImageContext
->DebugDirectoryEntryRva
+
1030 sizeof(EFI_TE_IMAGE_HEADER
) -
1031 Hdr
.Te
->StrippedSize
1035 if (DebugEntry
!= NULL
) {
1036 TempDebugEntryRva
= DebugEntry
->RVA
;
1037 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1039 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1040 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1042 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1046 if (TempDebugEntryRva
!= 0) {
1047 if (!(ImageContext
->IsTeImage
)) {
1048 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1050 ImageContext
->CodeView
= (VOID
*)(
1051 (UINTN
)ImageContext
->ImageAddress
+
1052 (UINTN
)TempDebugEntryRva
+
1053 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1054 (UINTN
) Hdr
.Te
->StrippedSize
1058 if (ImageContext
->CodeView
== NULL
) {
1059 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1060 return RETURN_LOAD_ERROR
;
1063 if (DebugEntry
->RVA
== 0) {
1064 Size
= DebugEntry
->SizeOfData
;
1065 if (!(ImageContext
->IsTeImage
)) {
1066 Status
= ImageContext
->ImageRead (
1067 ImageContext
->Handle
,
1068 DebugEntry
->FileOffset
,
1070 ImageContext
->CodeView
1073 Status
= ImageContext
->ImageRead (
1074 ImageContext
->Handle
,
1075 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1077 ImageContext
->CodeView
1080 // Should we apply fix up to this field according to the size difference between PE and TE?
1081 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1082 // in original PE image.
1086 if (RETURN_ERROR (Status
)) {
1087 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1088 return RETURN_LOAD_ERROR
;
1091 DebugEntry
->RVA
= TempDebugEntryRva
;
1094 switch (*(UINT32
*) ImageContext
->CodeView
) {
1095 case CODEVIEW_SIGNATURE_NB10
:
1096 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1099 case CODEVIEW_SIGNATURE_RSDS
:
1100 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1115 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1118 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1119 the fixups with a virtual mapping.
1122 @param ImageBase Base address of relocated image
1123 @param VirtImageBase Virtual mapping for ImageBase
1124 @param ImageSize Size of the image to relocate
1125 @param RelocationData Location to place results of read
1130 PeCoffLoaderRelocateImageForRuntime (
1131 IN PHYSICAL_ADDRESS ImageBase
,
1132 IN PHYSICAL_ADDRESS VirtImageBase
,
1134 IN VOID
*RelocationData
1139 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1140 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1141 UINT32 NumberOfRvaAndSizes
;
1142 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1143 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1144 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1145 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1155 RETURN_STATUS Status
;
1158 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1159 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1160 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1163 // Find the image's relocate dir info
1165 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1166 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1168 // Valid DOS header so get address of PE header
1170 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1173 // No Dos header so assume image starts with PE header.
1175 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1178 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1180 // Not a valid PE image so Exit
1185 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1187 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1191 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1192 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1197 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1198 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1202 // Find the relocation block
1204 // Per the PE/COFF spec, you can't assume that a given data directory
1205 // is present in the image. You have to check the NumberOfRvaAndSizes in
1206 // the optional header to verify a desired directory entry is there.
1208 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1209 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1210 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1211 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1214 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1221 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1223 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1226 // Run the whole relocation block. And re-fixup data that has not been
1227 // modified. The FixupData is used to see if the image has been modified
1228 // since it was relocated. This is so data sections that have been updated
1229 // by code will not be fixed up, since that would set them back to
1232 FixupData
= RelocationData
;
1233 while (RelocBase
< RelocBaseEnd
) {
1235 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1236 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1237 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1240 // Run this relocation record
1242 while (Reloc
< RelocEnd
) {
1244 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1245 switch ((*Reloc
) >> 12) {
1247 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1250 case EFI_IMAGE_REL_BASED_HIGH
:
1251 F16
= (UINT16
*) Fixup
;
1252 if (*(UINT16
*) FixupData
== *F16
) {
1253 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1256 FixupData
= FixupData
+ sizeof (UINT16
);
1259 case EFI_IMAGE_REL_BASED_LOW
:
1260 F16
= (UINT16
*) Fixup
;
1261 if (*(UINT16
*) FixupData
== *F16
) {
1262 *F16
= (UINT16
) (*F16
+ ((UINT16
) Adjust
& 0xffff));
1265 FixupData
= FixupData
+ sizeof (UINT16
);
1268 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1269 F32
= (UINT32
*) Fixup
;
1270 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1271 if (*(UINT32
*) FixupData
== *F32
) {
1272 *F32
= *F32
+ (UINT32
) Adjust
;
1275 FixupData
= FixupData
+ sizeof (UINT32
);
1278 case EFI_IMAGE_REL_BASED_DIR64
:
1279 F64
= (UINT64
*)Fixup
;
1280 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1281 if (*(UINT64
*) FixupData
== *F64
) {
1282 *F64
= *F64
+ (UINT64
)Adjust
;
1285 FixupData
= FixupData
+ sizeof (UINT64
);
1288 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1290 // Not valid Relocation type for UEFI image, ASSERT
1297 // Only Itanium requires ConvertPeImage_Ex
1299 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1300 if (RETURN_ERROR (Status
)) {
1305 // Next relocation record
1312 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1318 ImageRead function that operates on a memory buffer whos base is passed into
1321 @param FileHandle Ponter to baes of the input stream
1322 @param FileOffset Offset to the start of the buffer
1323 @param ReadSize Number of bytes to copy into the buffer
1324 @param Buffer Location to place results of read
1326 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1331 PeCoffLoaderImageReadFromMemory (
1332 IN VOID
*FileHandle
,
1333 IN UINTN FileOffset
,
1334 IN OUT UINTN
*ReadSize
,
1338 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1339 return RETURN_SUCCESS
;
1343 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1345 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1346 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1347 this function can simply return RETURN_SUCCESS.
1349 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1350 image to be unloaded.
1352 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1356 PeCoffLoaderUnloadImage (
1357 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1360 return RETURN_SUCCESS
;