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
20 #include "BasePeCoffLibInternals.h"
23 Retrieves the magic value from the PE/COFF header.
25 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
27 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
28 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
32 PeCoffLoaderGetPeHeaderMagicValue (
33 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
37 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
38 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
39 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
40 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
42 if (Hdr
.Pe32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
43 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
46 // Return the magic value from the PC/COFF Optional Header
48 return Hdr
.Pe32
->OptionalHeader
.Magic
;
53 Retrieves the PE or TE Header from a PE/COFF or TE image.
55 @param ImageContext The context of the image being loaded.
56 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
58 @retval RETURN_SUCCESS The PE or TE Header is read.
59 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
63 PeCoffLoaderGetPeHeader (
64 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
65 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
69 EFI_IMAGE_DOS_HEADER DosHdr
;
74 // Read the DOS image header to check for it's existance
76 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
77 Status
= ImageContext
->ImageRead (
83 if (RETURN_ERROR (Status
)) {
84 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
88 ImageContext
->PeCoffHeaderOffset
= 0;
89 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
91 // DOS image header is present, so read the PE header after the DOS image
94 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
98 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
99 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
100 // determins if this is a PE32 or PE32+ image. The magic is in the same
101 // location in both images.
103 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
104 Status
= ImageContext
->ImageRead (
105 ImageContext
->Handle
,
106 ImageContext
->PeCoffHeaderOffset
,
110 if (RETURN_ERROR (Status
)) {
111 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
116 // Use Signature to figure out if we understand the image format
118 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
119 ImageContext
->IsTeImage
= TRUE
;
120 ImageContext
->Machine
= Hdr
.Te
->Machine
;
121 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
122 ImageContext
->ImageSize
= 0;
123 ImageContext
->SectionAlignment
= 4096;
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, CodeView,
176 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
177 fields of the ImageContext structure. 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 (NULL
== ImageContext
) {
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
;
272 ImageContext
->RelocationsStripped
= FALSE
;
275 if (!(ImageContext
->IsTeImage
)) {
276 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
280 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
281 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
286 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
287 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
290 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
292 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
295 // Determine the file offset of the debug directory... This means we walk
296 // the sections to find which section contains the RVA of the debug
299 DebugDirectoryEntryFileOffset
= 0;
301 SectionHeaderOffset
= (UINTN
)(
302 ImageContext
->PeCoffHeaderOffset
+
304 sizeof (EFI_IMAGE_FILE_HEADER
) +
305 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
308 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
310 // Read section header from file
312 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
313 Status
= ImageContext
->ImageRead (
314 ImageContext
->Handle
,
319 if (RETURN_ERROR (Status
)) {
320 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
324 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
325 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
327 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
331 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
334 if (DebugDirectoryEntryFileOffset
!= 0) {
335 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
337 // Read next debug directory entry
339 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
340 Status
= ImageContext
->ImageRead (
341 ImageContext
->Handle
,
342 DebugDirectoryEntryFileOffset
,
346 if (RETURN_ERROR (Status
)) {
347 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
350 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
351 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
352 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
353 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
356 return RETURN_SUCCESS
;
363 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
364 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
365 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
367 DebugDirectoryEntryFileOffset
= 0;
369 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
371 // Read section header from file
373 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
374 Status
= ImageContext
->ImageRead (
375 ImageContext
->Handle
,
380 if (RETURN_ERROR (Status
)) {
381 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
385 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
386 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
387 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
388 SectionHeader
.VirtualAddress
+
389 SectionHeader
.PointerToRawData
+
390 sizeof (EFI_TE_IMAGE_HEADER
) -
391 Hdr
.Te
->StrippedSize
;
394 // File offset of the debug directory was found, if this is not the last
395 // section, then skip to the last section for calculating the image size.
397 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
398 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
399 Index
= Hdr
.Te
->NumberOfSections
- 1;
405 // In Te image header there is not a field to describe the ImageSize.
406 // Actually, the ImageSize equals the RVA plus the VirtualSize of
407 // the last section mapped into memory (Must be rounded up to
408 // a mulitple of Section Alignment). Per the PE/COFF specification, the
409 // section headers in the Section Table must appear in order of the RVA
410 // values for the corresponding sections. So the ImageSize can be determined
411 // by the RVA and the VirtualSize of the last section header in the
414 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
415 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
+
416 ImageContext
->SectionAlignment
- 1) & ~(ImageContext
->SectionAlignment
- 1);
419 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
422 if (DebugDirectoryEntryFileOffset
!= 0) {
423 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
425 // Read next debug directory entry
427 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
428 Status
= ImageContext
->ImageRead (
429 ImageContext
->Handle
,
430 DebugDirectoryEntryFileOffset
,
434 if (RETURN_ERROR (Status
)) {
435 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
439 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
440 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
441 return RETURN_SUCCESS
;
447 return RETURN_SUCCESS
;
452 Converts an image address to the loaded address.
454 @param ImageContext The context of the image being loaded.
455 @param Address The address to be converted to the loaded address.
457 @return The converted address or NULL if the address can not be converted.
461 PeCoffLoaderImageAddress (
462 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
467 // @bug Check to make sure ImageSize is correct for the relocated image.
468 // it may only work for the file we start with and not the relocated image
470 if (Address
>= ImageContext
->ImageSize
) {
471 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
475 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
479 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
481 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
482 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
483 of ImageContext as the relocation base address. The caller must allocate the relocation
484 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
485 If ImageContext is NULL, then ASSERT().
487 @param ImageContext Pointer to the image context structure that describes the PE/COFF
488 image that is being relocated.
490 @retval RETURN_SUCCESS The PE/COFF image was relocated.
491 Extended status information is in the ImageError field of ImageContext.
492 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
493 Extended status information is in the ImageError field of ImageContext.
494 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
495 Extended status information is in the ImageError field of ImageContext.
500 PeCoffLoaderRelocateImage (
501 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
504 RETURN_STATUS Status
;
505 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
506 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
508 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
509 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
518 PHYSICAL_ADDRESS BaseAddress
;
519 UINT32 NumberOfRvaAndSizes
;
522 ASSERT (ImageContext
!= NULL
);
527 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
530 // If there are no relocation entries, then we are done
532 if (ImageContext
->RelocationsStripped
) {
533 return RETURN_SUCCESS
;
537 // If the destination address is not 0, use that rather than the
538 // image address as the relocation target.
540 if (ImageContext
->DestinationAddress
!= 0) {
541 BaseAddress
= ImageContext
->DestinationAddress
;
542 } else if (!(ImageContext
->IsTeImage
)) {
543 BaseAddress
= ImageContext
->ImageAddress
;
545 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
546 BaseAddress
= ImageContext
->ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
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
->ImageBase
);
596 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
);
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 F16
= (UINT16
*) Fixup
;
648 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
649 if (FixupData
!= NULL
) {
650 *(UINT16
*) FixupData
= *F16
;
651 FixupData
= FixupData
+ sizeof (UINT16
);
655 case EFI_IMAGE_REL_BASED_LOW
:
656 F16
= (UINT16
*) Fixup
;
657 *F16
= (UINT16
) (*F16
+ (UINT16
) Adjust
);
658 if (FixupData
!= NULL
) {
659 *(UINT16
*) FixupData
= *F16
;
660 FixupData
= FixupData
+ sizeof (UINT16
);
664 case EFI_IMAGE_REL_BASED_HIGHLOW
:
665 F32
= (UINT32
*) Fixup
;
666 *F32
= *F32
+ (UINT32
) Adjust
;
667 if (FixupData
!= NULL
) {
668 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
669 *(UINT32
*)FixupData
= *F32
;
670 FixupData
= FixupData
+ sizeof (UINT32
);
674 case EFI_IMAGE_REL_BASED_DIR64
:
675 F64
= (UINT64
*) Fixup
;
676 *F64
= *F64
+ (UINT64
) Adjust
;
677 if (FixupData
!= NULL
) {
678 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
679 *(UINT64
*)(FixupData
) = *F64
;
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
;
709 return RETURN_SUCCESS
;
713 Loads a PE/COFF image into memory.
715 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
716 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
717 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
718 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
719 If ImageContext is NULL, then ASSERT().
721 @param ImageContext Pointer to the image context structure that describes the PE/COFF
722 image that is being loaded.
724 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
725 the ImageAddress and ImageSize fields of ImageContext.
726 Extended status information is in the ImageError field of ImageContext.
727 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
728 Extended status information is in the ImageError field of ImageContext.
729 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
730 Extended status information is in the ImageError field of ImageContext.
731 @retval RETURN_INVALID_PARAMETER The image address is invalid.
732 Extended status information is in the ImageError field of ImageContext.
737 PeCoffLoaderLoadImage (
738 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
741 RETURN_STATUS Status
;
742 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
743 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
744 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
745 EFI_IMAGE_SECTION_HEADER
*Section
;
746 UINTN NumberOfSections
;
751 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
752 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
754 UINT32 TempDebugEntryRva
;
755 UINT32 NumberOfRvaAndSizes
;
758 ASSERT (ImageContext
!= NULL
);
763 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
766 // Copy the provided context info into our local version, get what we
767 // can from the original image, and then use that to make sure everything
770 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
772 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
773 if (RETURN_ERROR (Status
)) {
778 // Make sure there is enough allocated space for the image being loaded
780 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
781 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
782 return RETURN_BUFFER_TOO_SMALL
;
784 if (ImageContext
->ImageAddress
== 0) {
786 // Image cannot be loaded into 0 address.
788 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
789 return RETURN_INVALID_PARAMETER
;
792 // If there's no relocations, then make sure it's not a runtime driver,
793 // and that it's being loaded at the linked address.
795 if (CheckContext
.RelocationsStripped
) {
797 // If the image does not contain relocations and it is a runtime driver
798 // then return an error.
800 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
801 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
802 return RETURN_LOAD_ERROR
;
805 // If the image does not contain relocations, and the requested load address
806 // is not the linked address, then return an error.
808 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
809 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
810 return RETURN_INVALID_PARAMETER
;
814 // Make sure the allocated space has the proper section alignment
816 if (!(ImageContext
->IsTeImage
)) {
817 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
818 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
819 return RETURN_INVALID_PARAMETER
;
823 // Read the entire PE/COFF or TE header into memory
825 if (!(ImageContext
->IsTeImage
)) {
826 Status
= ImageContext
->ImageRead (
827 ImageContext
->Handle
,
829 &ImageContext
->SizeOfHeaders
,
830 (VOID
*) (UINTN
) ImageContext
->ImageAddress
833 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
835 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
836 (UINTN
)ImageContext
->ImageAddress
+
837 ImageContext
->PeCoffHeaderOffset
+
839 sizeof(EFI_IMAGE_FILE_HEADER
) +
840 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
842 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
844 Status
= ImageContext
->ImageRead (
845 ImageContext
->Handle
,
847 &ImageContext
->SizeOfHeaders
,
848 (void *)(UINTN
)ImageContext
->ImageAddress
851 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
853 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
854 (UINTN
)ImageContext
->ImageAddress
+
855 sizeof(EFI_TE_IMAGE_HEADER
)
857 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
861 if (RETURN_ERROR (Status
)) {
862 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
863 return RETURN_LOAD_ERROR
;
867 // Load each section of the image
869 Section
= FirstSection
;
870 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
873 // Compute sections address
875 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
876 End
= PeCoffLoaderImageAddress (
878 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
880 if (ImageContext
->IsTeImage
) {
881 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
882 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
889 // If the base start or end address resolved to 0, then fail.
891 if ((Base
== NULL
) || (End
== NULL
)) {
892 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
893 return RETURN_LOAD_ERROR
;
899 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
900 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
901 Size
= (UINTN
) Section
->SizeOfRawData
;
904 if (Section
->SizeOfRawData
) {
905 if (!(ImageContext
->IsTeImage
)) {
906 Status
= ImageContext
->ImageRead (
907 ImageContext
->Handle
,
908 Section
->PointerToRawData
,
913 Status
= ImageContext
->ImageRead (
914 ImageContext
->Handle
,
915 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
921 if (RETURN_ERROR (Status
)) {
922 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
928 // If raw size is less then virt size, zero fill the remaining
931 if (Size
< Section
->Misc
.VirtualSize
) {
932 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
942 // Get image's entry point
944 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
945 if (!(ImageContext
->IsTeImage
)) {
947 // Sizes of AddressOfEntryPoint are different so we need to do this safely
949 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
953 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
955 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
961 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
963 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
967 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
968 (UINTN
)ImageContext
->ImageAddress
+
969 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
970 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
971 (UINTN
)Hdr
.Te
->StrippedSize
976 // Determine the size of the fixup data
978 // Per the PE/COFF spec, you can't assume that a given data directory
979 // is present in the image. You have to check the NumberOfRvaAndSizes in
980 // the optional header to verify a desired directory entry is there.
982 if (!(ImageContext
->IsTeImage
)) {
983 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
987 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
988 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
993 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
994 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
997 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
998 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1000 ImageContext
->FixupDataSize
= 0;
1003 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1004 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1007 // Consumer must allocate a buffer for the relocation fixup log.
1008 // Only used for runtime drivers.
1010 ImageContext
->FixupData
= NULL
;
1013 // Load the Codeview info if present
1015 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1016 if (!(ImageContext
->IsTeImage
)) {
1017 DebugEntry
= PeCoffLoaderImageAddress (
1019 ImageContext
->DebugDirectoryEntryRva
1022 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1023 ImageContext
->ImageAddress
+
1024 ImageContext
->DebugDirectoryEntryRva
+
1025 sizeof(EFI_TE_IMAGE_HEADER
) -
1026 Hdr
.Te
->StrippedSize
1030 if (DebugEntry
!= NULL
) {
1031 TempDebugEntryRva
= DebugEntry
->RVA
;
1032 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1034 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1035 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1037 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1041 if (TempDebugEntryRva
!= 0) {
1042 if (!(ImageContext
->IsTeImage
)) {
1043 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1045 ImageContext
->CodeView
= (VOID
*)(
1046 (UINTN
)ImageContext
->ImageAddress
+
1047 (UINTN
)TempDebugEntryRva
+
1048 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1049 (UINTN
) Hdr
.Te
->StrippedSize
1053 if (ImageContext
->CodeView
== NULL
) {
1054 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1055 return RETURN_LOAD_ERROR
;
1058 if (DebugEntry
->RVA
== 0) {
1059 Size
= DebugEntry
->SizeOfData
;
1060 if (!(ImageContext
->IsTeImage
)) {
1061 Status
= ImageContext
->ImageRead (
1062 ImageContext
->Handle
,
1063 DebugEntry
->FileOffset
,
1065 ImageContext
->CodeView
1068 Status
= ImageContext
->ImageRead (
1069 ImageContext
->Handle
,
1070 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1072 ImageContext
->CodeView
1075 // Should we apply fix up to this field according to the size difference between PE and TE?
1076 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1077 // in original PE image.
1081 if (RETURN_ERROR (Status
)) {
1082 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1083 return RETURN_LOAD_ERROR
;
1086 DebugEntry
->RVA
= TempDebugEntryRva
;
1089 switch (*(UINT32
*) ImageContext
->CodeView
) {
1090 case CODEVIEW_SIGNATURE_NB10
:
1091 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1094 case CODEVIEW_SIGNATURE_RSDS
:
1095 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1110 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1113 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1114 the fixups with a virtual mapping.
1117 @param ImageBase Base address of relocated image
1118 @param VirtImageBase Virtual mapping for ImageBase
1119 @param ImageSize Size of the image to relocate
1120 @param RelocationData Location to place results of read
1125 PeCoffLoaderRelocateImageForRuntime (
1126 IN PHYSICAL_ADDRESS ImageBase
,
1127 IN PHYSICAL_ADDRESS VirtImageBase
,
1129 IN VOID
*RelocationData
1134 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1135 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1136 UINT32 NumberOfRvaAndSizes
;
1137 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1138 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1139 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1140 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1150 RETURN_STATUS Status
;
1153 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1154 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1155 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1158 // Find the image's relocate dir info
1160 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1161 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1163 // Valid DOS header so get address of PE header
1165 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1168 // No Dos header so assume image starts with PE header.
1170 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1173 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1175 // Not a valid PE image so Exit
1180 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1182 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1186 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1187 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1192 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1193 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1197 // Find the relocation block
1199 // Per the PE/COFF spec, you can't assume that a given data directory
1200 // is present in the image. You have to check the NumberOfRvaAndSizes in
1201 // the optional header to verify a desired directory entry is there.
1203 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1204 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1205 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1206 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1209 // Cannot find relocations, cannot continue
1215 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1218 // Run the whole relocation block. And re-fixup data that has not been
1219 // modified. The FixupData is used to see if the image has been modified
1220 // since it was relocated. This is so data sections that have been updated
1221 // by code will not be fixed up, since that would set them back to
1224 FixupData
= RelocationData
;
1225 while (RelocBase
< RelocBaseEnd
) {
1227 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1228 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1229 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1232 // Run this relocation record
1234 while (Reloc
< RelocEnd
) {
1236 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1237 switch ((*Reloc
) >> 12) {
1239 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1242 case EFI_IMAGE_REL_BASED_HIGH
:
1243 F16
= (UINT16
*) Fixup
;
1244 if (*(UINT16
*) FixupData
== *F16
) {
1245 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1248 FixupData
= FixupData
+ sizeof (UINT16
);
1251 case EFI_IMAGE_REL_BASED_LOW
:
1252 F16
= (UINT16
*) Fixup
;
1253 if (*(UINT16
*) FixupData
== *F16
) {
1254 *F16
= (UINT16
) (*F16
+ ((UINT16
) Adjust
& 0xffff));
1257 FixupData
= FixupData
+ sizeof (UINT16
);
1260 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1261 F32
= (UINT32
*) Fixup
;
1262 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1263 if (*(UINT32
*) FixupData
== *F32
) {
1264 *F32
= *F32
+ (UINT32
) Adjust
;
1267 FixupData
= FixupData
+ sizeof (UINT32
);
1270 case EFI_IMAGE_REL_BASED_DIR64
:
1271 F64
= (UINT64
*)Fixup
;
1272 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1273 if (*(UINT64
*) FixupData
== *F64
) {
1274 *F64
= *F64
+ (UINT64
)Adjust
;
1277 FixupData
= FixupData
+ sizeof (UINT64
);
1280 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1282 // Not implemented, but not used in EFI 1.0
1289 // Only Itanium requires ConvertPeImage_Ex
1291 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1292 if (RETURN_ERROR (Status
)) {
1297 // Next relocation record
1304 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1310 ImageRead function that operates on a memory buffer whos base is passed into
1313 @param FileHandle Ponter to baes of the input stream
1314 @param FileOffset Offset to the start of the buffer
1315 @param ReadSize Number of bytes to copy into the buffer
1316 @param Buffer Location to place results of read
1318 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1323 PeCoffLoaderImageReadFromMemory (
1324 IN VOID
*FileHandle
,
1325 IN UINTN FileOffset
,
1326 IN OUT UINTN
*ReadSize
,
1330 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1331 return RETURN_SUCCESS
;