2 Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but
3 only supports relocating IA32, X64, IPF, and EBC images.
5 Copyright (c) 2006 - 2008, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "BasePeCoffLibInternals.h"
19 Retrieves the magic value from the PE/COFF header.
21 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
23 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
24 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
28 PeCoffLoaderGetPeHeaderMagicValue (
29 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
33 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
34 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
35 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
36 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
38 if (Hdr
.Pe32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
39 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
42 // Return the magic value from the PC/COFF Optional Header
44 return Hdr
.Pe32
->OptionalHeader
.Magic
;
49 Retrieves the PE or TE Header from a PE/COFF or TE image.
51 @param ImageContext The context of the image being loaded.
52 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
54 @retval RETURN_SUCCESS The PE or TE Header is read.
55 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
59 PeCoffLoaderGetPeHeader (
60 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
61 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
65 EFI_IMAGE_DOS_HEADER DosHdr
;
70 // Read the DOS image header to check for it's existance
72 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
73 Status
= ImageContext
->ImageRead (
79 if (RETURN_ERROR (Status
)) {
80 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
84 ImageContext
->PeCoffHeaderOffset
= 0;
85 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
87 // DOS image header is present, so read the PE header after the DOS image
90 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
94 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
95 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
96 // determins if this is a PE32 or PE32+ image. The magic is in the same
97 // location in both images.
99 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
100 Status
= ImageContext
->ImageRead (
101 ImageContext
->Handle
,
102 ImageContext
->PeCoffHeaderOffset
,
106 if (RETURN_ERROR (Status
)) {
107 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
112 // Use Signature to figure out if we understand the image format
114 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
115 ImageContext
->IsTeImage
= TRUE
;
116 ImageContext
->Machine
= Hdr
.Te
->Machine
;
117 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
119 // For TeImage, SectionAlignment is undefined to be set to Zero
120 // ImageSize can be calculated.
122 ImageContext
->ImageSize
= 0;
123 ImageContext
->SectionAlignment
= 0;
124 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
126 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
127 ImageContext
->IsTeImage
= FALSE
;
128 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
130 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
132 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
136 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
137 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
138 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
139 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
141 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
145 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
146 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
147 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
148 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
150 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
151 return RETURN_UNSUPPORTED
;
154 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
155 return RETURN_UNSUPPORTED
;
158 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
160 // If the PE/COFF loader does not support the image type return
161 // unsupported. This library can suport lots of types of images
162 // this does not mean the user of this library can call the entry
163 // point of the image.
165 return RETURN_UNSUPPORTED
;
168 return RETURN_SUCCESS
;
173 Retrieves information about a PE/COFF image.
175 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,
176 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and
177 DebugDirectoryEntryRva fields of the ImageContext structure.
178 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
179 If the PE/COFF image accessed through the ImageRead service in the ImageContext
180 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.
181 If any errors occur while computing the fields of ImageContext,
182 then the error status is returned in the ImageError field of ImageContext.
183 If the image is a TE image, then SectionAlignment is set to 0.
185 @param ImageContext Pointer to the image context structure that describes the PE/COFF
186 image that needs to be examined by this function.
188 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
189 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
190 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
195 PeCoffLoaderGetImageInfo (
196 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
199 RETURN_STATUS Status
;
200 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
201 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
202 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
205 UINTN DebugDirectoryEntryRva
;
206 UINTN DebugDirectoryEntryFileOffset
;
207 UINTN SectionHeaderOffset
;
208 EFI_IMAGE_SECTION_HEADER SectionHeader
;
209 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
210 UINT32 NumberOfRvaAndSizes
;
213 if (ImageContext
== NULL
) {
214 return RETURN_INVALID_PARAMETER
;
219 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
221 Hdr
.Union
= &HdrData
;
222 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
223 if (RETURN_ERROR (Status
)) {
227 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
230 // Retrieve the base address of the image
232 if (!(ImageContext
->IsTeImage
)) {
233 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
237 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
242 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
245 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
249 // Initialize the alternate destination address to 0 indicating that it
250 // should not be used.
252 ImageContext
->DestinationAddress
= 0;
255 // Initialize the codeview pointer.
257 ImageContext
->CodeView
= NULL
;
258 ImageContext
->PdbPointer
= NULL
;
261 // Three cases with regards to relocations:
262 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
263 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
264 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
265 // has no base relocs to apply
266 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
268 // Look at the file header to determine if relocations have been stripped, and
269 // save this info in the image context for later use.
271 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
272 ImageContext
->RelocationsStripped
= TRUE
;
273 } else if ((ImageContext
->IsTeImage
) && (Hdr
.Te
->DataDirectory
[0].Size
== 0) && (Hdr
.Te
->DataDirectory
[0].VirtualAddress
== 0)) {
274 ImageContext
->RelocationsStripped
= TRUE
;
276 ImageContext
->RelocationsStripped
= FALSE
;
279 if (!(ImageContext
->IsTeImage
)) {
280 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
284 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
285 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
290 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
291 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
294 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
296 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
299 // Determine the file offset of the debug directory... This means we walk
300 // the sections to find which section contains the RVA of the debug
303 DebugDirectoryEntryFileOffset
= 0;
305 SectionHeaderOffset
= (UINTN
)(
306 ImageContext
->PeCoffHeaderOffset
+
308 sizeof (EFI_IMAGE_FILE_HEADER
) +
309 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
312 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
314 // Read section header from file
316 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
317 Status
= ImageContext
->ImageRead (
318 ImageContext
->Handle
,
323 if (RETURN_ERROR (Status
)) {
324 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
328 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
329 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
331 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
335 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
338 if (DebugDirectoryEntryFileOffset
!= 0) {
339 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
341 // Read next debug directory entry
343 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
344 Status
= ImageContext
->ImageRead (
345 ImageContext
->Handle
,
346 DebugDirectoryEntryFileOffset
,
350 if (RETURN_ERROR (Status
)) {
351 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
354 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
355 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
356 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
357 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
360 return RETURN_SUCCESS
;
367 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
368 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
369 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
371 DebugDirectoryEntryFileOffset
= 0;
373 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
375 // Read section header from file
377 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
378 Status
= ImageContext
->ImageRead (
379 ImageContext
->Handle
,
384 if (RETURN_ERROR (Status
)) {
385 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
389 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
390 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
391 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
392 SectionHeader
.VirtualAddress
+
393 SectionHeader
.PointerToRawData
+
394 sizeof (EFI_TE_IMAGE_HEADER
) -
395 Hdr
.Te
->StrippedSize
;
398 // File offset of the debug directory was found, if this is not the last
399 // section, then skip to the last section for calculating the image size.
401 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
402 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
403 Index
= Hdr
.Te
->NumberOfSections
- 1;
409 // In Te image header there is not a field to describe the ImageSize.
410 // Actually, the ImageSize equals the RVA plus the VirtualSize of
411 // the last section mapped into memory (Must be rounded up to
412 // a mulitple of Section Alignment). Per the PE/COFF specification, the
413 // section headers in the Section Table must appear in order of the RVA
414 // values for the corresponding sections. So the ImageSize can be determined
415 // by the RVA and the VirtualSize of the last section header in the
418 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
419 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
);
422 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
425 if (DebugDirectoryEntryFileOffset
!= 0) {
426 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
428 // Read next debug directory entry
430 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
431 Status
= ImageContext
->ImageRead (
432 ImageContext
->Handle
,
433 DebugDirectoryEntryFileOffset
,
437 if (RETURN_ERROR (Status
)) {
438 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
442 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
443 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
444 return RETURN_SUCCESS
;
450 return RETURN_SUCCESS
;
455 Converts an image address to the loaded address.
457 @param ImageContext The context of the image being loaded.
458 @param Address The address to be converted to the loaded address.
460 @return The converted address or NULL if the address can not be converted.
464 PeCoffLoaderImageAddress (
465 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
470 // @bug Check to make sure ImageSize is correct for the relocated image.
471 // it may only work for the file we start with and not the relocated image
473 if (Address
>= ImageContext
->ImageSize
) {
474 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
478 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
482 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
484 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
485 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
486 of ImageContext as the relocation base address. The caller must allocate the relocation
487 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
488 If ImageContext is NULL, then ASSERT().
490 @param ImageContext Pointer to the image context structure that describes the PE/COFF
491 image that is being relocated.
493 @retval RETURN_SUCCESS The PE/COFF image was relocated.
494 Extended status information is in the ImageError field of ImageContext.
495 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
496 Extended status information is in the ImageError field of ImageContext.
497 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
498 Extended status information is in the ImageError field of ImageContext.
503 PeCoffLoaderRelocateImage (
504 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
507 RETURN_STATUS Status
;
508 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
509 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
511 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
512 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
521 PHYSICAL_ADDRESS BaseAddress
;
522 UINT32 NumberOfRvaAndSizes
;
525 ASSERT (ImageContext
!= NULL
);
530 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
533 // If there are no relocation entries, then we are done
535 if (ImageContext
->RelocationsStripped
) {
536 return RETURN_SUCCESS
;
540 // If the destination address is not 0, use that rather than the
541 // image address as the relocation target.
543 if (ImageContext
->DestinationAddress
!= 0) {
544 BaseAddress
= ImageContext
->DestinationAddress
;
546 BaseAddress
= ImageContext
->ImageAddress
;
549 if (!(ImageContext
->IsTeImage
)) {
550 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
552 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
554 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
558 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
559 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
561 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
562 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
567 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
568 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
570 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
571 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
575 // Find the relocation block
576 // Per the PE/COFF spec, you can't assume that a given data directory
577 // is present in the image. You have to check the NumberOfRvaAndSizes in
578 // the optional header to verify a desired directory entry is there.
581 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
582 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
583 RelocBaseEnd
= PeCoffLoaderImageAddress (
585 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
589 // Set base and end to bypass processing below.
591 RelocBase
= RelocBaseEnd
= 0;
594 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
595 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->ImageBase
);
596 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
- Hdr
.Te
->StrippedSize
+ sizeof (EFI_TE_IMAGE_HEADER
));
599 // Find the relocation block
601 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
602 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
603 ImageContext
->ImageAddress
+
604 RelocDir
->VirtualAddress
+
605 sizeof(EFI_TE_IMAGE_HEADER
) -
608 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
612 // Run the relocation information and apply the fixups
614 FixupData
= ImageContext
->FixupData
;
615 while (RelocBase
< RelocBaseEnd
) {
617 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
618 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
619 if (!(ImageContext
->IsTeImage
)) {
620 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
622 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
623 RelocBase
->VirtualAddress
+
624 sizeof(EFI_TE_IMAGE_HEADER
) -
629 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
630 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
631 (UINTN
)ImageContext
->ImageSize
)) {
632 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
633 return RETURN_LOAD_ERROR
;
637 // Run this relocation record
639 while (Reloc
< RelocEnd
) {
641 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
642 switch ((*Reloc
) >> 12) {
643 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
646 case EFI_IMAGE_REL_BASED_HIGH
:
647 Fixup16
= (UINT16
*) Fixup
;
648 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
649 if (FixupData
!= NULL
) {
650 *(UINT16
*) FixupData
= *Fixup16
;
651 FixupData
= FixupData
+ sizeof (UINT16
);
655 case EFI_IMAGE_REL_BASED_LOW
:
656 Fixup16
= (UINT16
*) Fixup
;
657 *Fixup16
= (UINT16
) (*Fixup16
+ (UINT16
) Adjust
);
658 if (FixupData
!= NULL
) {
659 *(UINT16
*) FixupData
= *Fixup16
;
660 FixupData
= FixupData
+ sizeof (UINT16
);
664 case EFI_IMAGE_REL_BASED_HIGHLOW
:
665 Fixup32
= (UINT32
*) Fixup
;
666 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
667 if (FixupData
!= NULL
) {
668 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
669 *(UINT32
*)FixupData
= *Fixup32
;
670 FixupData
= FixupData
+ sizeof (UINT32
);
674 case EFI_IMAGE_REL_BASED_DIR64
:
675 Fixup64
= (UINT64
*) Fixup
;
676 *Fixup64
= *Fixup64
+ (UINT64
) Adjust
;
677 if (FixupData
!= NULL
) {
678 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
679 *(UINT64
*)(FixupData
) = *Fixup64
;
680 FixupData
= FixupData
+ sizeof(UINT64
);
686 // The common code does not handle some of the stranger IPF relocations
687 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
688 // on IPF and is a No-Op on other archtiectures.
690 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
691 if (RETURN_ERROR (Status
)) {
692 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
698 // Next relocation record
706 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
710 // Adjust the EntryPoint to match the linked-to address
712 if (ImageContext
->DestinationAddress
!= 0) {
713 ImageContext
->EntryPoint
-= (UINT64
) ImageContext
->ImageAddress
;
714 ImageContext
->EntryPoint
+= (UINT64
) ImageContext
->DestinationAddress
;
716 return RETURN_SUCCESS
;
720 Loads a PE/COFF image into memory.
722 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
723 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
724 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
725 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
726 If ImageContext is NULL, then ASSERT().
728 @param ImageContext Pointer to the image context structure that describes the PE/COFF
729 image that is being loaded.
731 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
732 the ImageAddress and ImageSize fields of ImageContext.
733 Extended status information is in the ImageError field of ImageContext.
734 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
735 Extended status information is in the ImageError field of ImageContext.
736 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
737 Extended status information is in the ImageError field of ImageContext.
738 @retval RETURN_INVALID_PARAMETER The image address is invalid.
739 Extended status information is in the ImageError field of ImageContext.
744 PeCoffLoaderLoadImage (
745 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
748 RETURN_STATUS Status
;
749 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
750 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
751 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
752 EFI_IMAGE_SECTION_HEADER
*Section
;
753 UINTN NumberOfSections
;
758 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
759 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
761 UINT32 TempDebugEntryRva
;
762 UINT32 NumberOfRvaAndSizes
;
765 ASSERT (ImageContext
!= NULL
);
770 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
773 // Copy the provided context info into our local version, get what we
774 // can from the original image, and then use that to make sure everything
777 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
779 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
780 if (RETURN_ERROR (Status
)) {
785 // Make sure there is enough allocated space for the image being loaded
787 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
788 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
789 return RETURN_BUFFER_TOO_SMALL
;
791 if (ImageContext
->ImageAddress
== 0) {
793 // Image cannot be loaded into 0 address.
795 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
796 return RETURN_INVALID_PARAMETER
;
799 // If there's no relocations, then make sure it's not a runtime driver,
800 // and that it's being loaded at the linked address.
802 if (CheckContext
.RelocationsStripped
) {
804 // If the image does not contain relocations and it is a runtime driver
805 // then return an error.
807 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
808 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
809 return RETURN_LOAD_ERROR
;
812 // If the image does not contain relocations, and the requested load address
813 // is not the linked address, then return an error.
815 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
816 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
817 return RETURN_INVALID_PARAMETER
;
821 // Make sure the allocated space has the proper section alignment
823 if (!(ImageContext
->IsTeImage
)) {
824 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
825 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
826 return RETURN_INVALID_PARAMETER
;
830 // Read the entire PE/COFF or TE header into memory
832 if (!(ImageContext
->IsTeImage
)) {
833 Status
= ImageContext
->ImageRead (
834 ImageContext
->Handle
,
836 &ImageContext
->SizeOfHeaders
,
837 (VOID
*) (UINTN
) ImageContext
->ImageAddress
840 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
842 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
843 (UINTN
)ImageContext
->ImageAddress
+
844 ImageContext
->PeCoffHeaderOffset
+
846 sizeof(EFI_IMAGE_FILE_HEADER
) +
847 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
849 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
851 Status
= ImageContext
->ImageRead (
852 ImageContext
->Handle
,
854 &ImageContext
->SizeOfHeaders
,
855 (void *)(UINTN
)ImageContext
->ImageAddress
858 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
860 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
861 (UINTN
)ImageContext
->ImageAddress
+
862 sizeof(EFI_TE_IMAGE_HEADER
)
864 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
868 if (RETURN_ERROR (Status
)) {
869 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
870 return RETURN_LOAD_ERROR
;
874 // Load each section of the image
876 Section
= FirstSection
;
877 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
880 // Compute sections address
882 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
883 End
= PeCoffLoaderImageAddress (
885 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
887 if (ImageContext
->IsTeImage
) {
888 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
889 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
896 // If the base start or end address resolved to 0, then fail.
898 if ((Base
== NULL
) || (End
== NULL
)) {
899 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
900 return RETURN_LOAD_ERROR
;
906 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
907 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
908 Size
= (UINTN
) Section
->SizeOfRawData
;
911 if (Section
->SizeOfRawData
> 0) {
912 if (!(ImageContext
->IsTeImage
)) {
913 Status
= ImageContext
->ImageRead (
914 ImageContext
->Handle
,
915 Section
->PointerToRawData
,
920 Status
= ImageContext
->ImageRead (
921 ImageContext
->Handle
,
922 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
928 if (RETURN_ERROR (Status
)) {
929 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
935 // If raw size is less then virt size, zero fill the remaining
938 if (Size
< Section
->Misc
.VirtualSize
) {
939 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
949 // Get image's entry point
951 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
952 if (!(ImageContext
->IsTeImage
)) {
954 // Sizes of AddressOfEntryPoint are different so we need to do this safely
956 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
960 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
962 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
968 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
970 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
974 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
975 (UINTN
)ImageContext
->ImageAddress
+
976 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
977 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
978 (UINTN
)Hdr
.Te
->StrippedSize
983 // Determine the size of the fixup data
985 // Per the PE/COFF spec, you can't assume that a given data directory
986 // is present in the image. You have to check the NumberOfRvaAndSizes in
987 // the optional header to verify a desired directory entry is there.
989 if (!(ImageContext
->IsTeImage
)) {
990 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
994 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
995 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1000 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1001 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1004 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1005 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1007 ImageContext
->FixupDataSize
= 0;
1010 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1011 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1014 // Consumer must allocate a buffer for the relocation fixup log.
1015 // Only used for runtime drivers.
1017 ImageContext
->FixupData
= NULL
;
1020 // Load the Codeview info if present
1022 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1023 if (!(ImageContext
->IsTeImage
)) {
1024 DebugEntry
= PeCoffLoaderImageAddress (
1026 ImageContext
->DebugDirectoryEntryRva
1029 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1030 ImageContext
->ImageAddress
+
1031 ImageContext
->DebugDirectoryEntryRva
+
1032 sizeof(EFI_TE_IMAGE_HEADER
) -
1033 Hdr
.Te
->StrippedSize
1037 if (DebugEntry
!= NULL
) {
1038 TempDebugEntryRva
= DebugEntry
->RVA
;
1039 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1041 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1042 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1044 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1048 if (TempDebugEntryRva
!= 0) {
1049 if (!(ImageContext
->IsTeImage
)) {
1050 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1052 ImageContext
->CodeView
= (VOID
*)(
1053 (UINTN
)ImageContext
->ImageAddress
+
1054 (UINTN
)TempDebugEntryRva
+
1055 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1056 (UINTN
) Hdr
.Te
->StrippedSize
1060 if (ImageContext
->CodeView
== NULL
) {
1061 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1062 return RETURN_LOAD_ERROR
;
1065 if (DebugEntry
->RVA
== 0) {
1066 Size
= DebugEntry
->SizeOfData
;
1067 if (!(ImageContext
->IsTeImage
)) {
1068 Status
= ImageContext
->ImageRead (
1069 ImageContext
->Handle
,
1070 DebugEntry
->FileOffset
,
1072 ImageContext
->CodeView
1075 Status
= ImageContext
->ImageRead (
1076 ImageContext
->Handle
,
1077 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1079 ImageContext
->CodeView
1082 // Should we apply fix up to this field according to the size difference between PE and TE?
1083 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1084 // in original PE image.
1088 if (RETURN_ERROR (Status
)) {
1089 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1090 return RETURN_LOAD_ERROR
;
1093 DebugEntry
->RVA
= TempDebugEntryRva
;
1096 switch (*(UINT32
*) ImageContext
->CodeView
) {
1097 case CODEVIEW_SIGNATURE_NB10
:
1098 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1101 case CODEVIEW_SIGNATURE_RSDS
:
1102 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1117 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1120 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1121 the fixups with a virtual mapping.
1124 @param ImageBase Base address of relocated image
1125 @param VirtImageBase Virtual mapping for ImageBase
1126 @param ImageSize Size of the image to relocate
1127 @param RelocationData Location to place results of read
1132 PeCoffLoaderRelocateImageForRuntime (
1133 IN PHYSICAL_ADDRESS ImageBase
,
1134 IN PHYSICAL_ADDRESS VirtImageBase
,
1136 IN VOID
*RelocationData
1141 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1142 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1143 UINT32 NumberOfRvaAndSizes
;
1144 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1145 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1146 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1147 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1157 RETURN_STATUS Status
;
1160 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1161 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1162 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1165 // Find the image's relocate dir info
1167 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1168 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1170 // Valid DOS header so get address of PE header
1172 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1175 // No Dos header so assume image starts with PE header.
1177 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1180 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1182 // Not a valid PE image so Exit
1187 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1189 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1193 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1194 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1199 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1200 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1204 // Find the relocation block
1206 // Per the PE/COFF spec, you can't assume that a given data directory
1207 // is present in the image. You have to check the NumberOfRvaAndSizes in
1208 // the optional header to verify a desired directory entry is there.
1210 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1211 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1212 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1213 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1216 // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
1223 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.
1225 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1228 // Run the whole relocation block. And re-fixup data that has not been
1229 // modified. The FixupData is used to see if the image has been modified
1230 // since it was relocated. This is so data sections that have been updated
1231 // by code will not be fixed up, since that would set them back to
1234 FixupData
= RelocationData
;
1235 while (RelocBase
< RelocBaseEnd
) {
1237 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1238 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1239 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1242 // Run this relocation record
1244 while (Reloc
< RelocEnd
) {
1246 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1247 switch ((*Reloc
) >> 12) {
1249 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1252 case EFI_IMAGE_REL_BASED_HIGH
:
1253 Fixup16
= (UINT16
*) Fixup
;
1254 if (*(UINT16
*) FixupData
== *Fixup16
) {
1255 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1258 FixupData
= FixupData
+ sizeof (UINT16
);
1261 case EFI_IMAGE_REL_BASED_LOW
:
1262 Fixup16
= (UINT16
*) Fixup
;
1263 if (*(UINT16
*) FixupData
== *Fixup16
) {
1264 *Fixup16
= (UINT16
) (*Fixup16
+ ((UINT16
) Adjust
& 0xffff));
1267 FixupData
= FixupData
+ sizeof (UINT16
);
1270 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1271 Fixup32
= (UINT32
*) Fixup
;
1272 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1273 if (*(UINT32
*) FixupData
== *Fixup32
) {
1274 *Fixup32
= *Fixup32
+ (UINT32
) Adjust
;
1277 FixupData
= FixupData
+ sizeof (UINT32
);
1280 case EFI_IMAGE_REL_BASED_DIR64
:
1281 Fixup64
= (UINT64
*)Fixup
;
1282 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1283 if (*(UINT64
*) FixupData
== *Fixup64
) {
1284 *Fixup64
= *Fixup64
+ (UINT64
)Adjust
;
1287 FixupData
= FixupData
+ sizeof (UINT64
);
1290 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1292 // Not valid Relocation type for UEFI image, ASSERT
1299 // Only Itanium requires ConvertPeImage_Ex
1301 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1302 if (RETURN_ERROR (Status
)) {
1307 // Next relocation record
1314 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1320 ImageRead function that operates on a memory buffer whos base is passed into
1323 @param FileHandle Ponter to baes of the input stream
1324 @param FileOffset Offset to the start of the buffer
1325 @param ReadSize Number of bytes to copy into the buffer
1326 @param Buffer Location to place results of read
1328 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1333 PeCoffLoaderImageReadFromMemory (
1334 IN VOID
*FileHandle
,
1335 IN UINTN FileOffset
,
1336 IN OUT UINTN
*ReadSize
,
1340 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1341 return RETURN_SUCCESS
;
1345 Unloads a loaded PE/COFF image from memory and releases its taken resource.
1347 For NT32 emulator, the PE/COFF image loaded by system needs to release.
1348 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
1349 this function can simply return RETURN_SUCCESS.
1351 @param ImageContext Pointer to the image context structure that describes the PE/COFF
1352 image to be unloaded.
1354 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
1358 PeCoffLoaderUnloadImage (
1359 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1362 return RETURN_SUCCESS
;