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
21 // Include common header file for this module.
25 #include "BasePeCoffLibInternals.h"
28 Retrieves the magic value from the PE/COFF header.
30 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
32 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
33 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
37 PeCoffLoaderGetPeHeaderMagicValue (
38 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
42 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
43 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
44 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
45 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
47 if (Hdr
.Pe32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
48 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
51 // Return the magic value from the PC/COFF Optional Header
53 return Hdr
.Pe32
->OptionalHeader
.Magic
;
58 Retrieves the PE or TE Header from a PE/COFF or TE image.
60 @param ImageContext The context of the image being loaded.
61 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
63 @retval RETURN_SUCCESS The PE or TE Header is read.
64 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
68 PeCoffLoaderGetPeHeader (
69 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
70 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
74 EFI_IMAGE_DOS_HEADER DosHdr
;
79 // Read the DOS image header to check for it's existance
81 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
82 Status
= ImageContext
->ImageRead (
88 if (RETURN_ERROR (Status
)) {
89 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
93 ImageContext
->PeCoffHeaderOffset
= 0;
94 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
96 // DOS image header is present, so read the PE header after the DOS image
99 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
103 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
104 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
105 // determins if this is a PE32 or PE32+ image. The magic is in the same
106 // location in both images.
108 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
109 Status
= ImageContext
->ImageRead (
110 ImageContext
->Handle
,
111 ImageContext
->PeCoffHeaderOffset
,
115 if (RETURN_ERROR (Status
)) {
116 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
121 // Use Signature to figure out if we understand the image format
123 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
124 ImageContext
->IsTeImage
= TRUE
;
125 ImageContext
->Machine
= Hdr
.Te
->Machine
;
126 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
127 ImageContext
->ImageSize
= 0;
128 ImageContext
->SectionAlignment
= 4096;
129 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
131 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
132 ImageContext
->IsTeImage
= FALSE
;
133 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
135 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
137 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
141 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
142 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
143 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
144 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
146 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
150 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
151 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
152 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
153 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
155 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
156 return RETURN_UNSUPPORTED
;
159 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
160 return RETURN_UNSUPPORTED
;
163 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
165 // If the PE/COFF loader does not support the image type return
166 // unsupported. This library can suport lots of types of images
167 // this does not mean the user of this library can call the entry
168 // point of the image.
170 return RETURN_UNSUPPORTED
;
173 return RETURN_SUCCESS
;
178 Retrieves information about a PE/COFF image.
180 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
181 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
182 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
183 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
184 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
185 computing the fields of ImageContext, then the error status is returned in the ImageError field of
188 @param ImageContext Pointer to the image context structure that describes the PE/COFF
189 image that needs to be examined by this function.
191 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
192 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
193 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
198 PeCoffLoaderGetImageInfo (
199 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
202 RETURN_STATUS Status
;
203 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
204 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
205 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
208 UINTN DebugDirectoryEntryRva
;
209 UINTN DebugDirectoryEntryFileOffset
;
210 UINTN SectionHeaderOffset
;
211 EFI_IMAGE_SECTION_HEADER SectionHeader
;
212 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
213 UINT32 NumberOfRvaAndSizes
;
216 if (NULL
== ImageContext
) {
217 return RETURN_INVALID_PARAMETER
;
222 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
224 Hdr
.Union
= &HdrData
;
225 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
226 if (RETURN_ERROR (Status
)) {
230 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
233 // Retrieve the base address of the image
235 if (!(ImageContext
->IsTeImage
)) {
236 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
240 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
245 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
248 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
252 // Initialize the alternate destination address to 0 indicating that it
253 // should not be used.
255 ImageContext
->DestinationAddress
= 0;
258 // Initialize the codeview pointer.
260 ImageContext
->CodeView
= NULL
;
261 ImageContext
->PdbPointer
= NULL
;
264 // Three cases with regards to relocations:
265 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
266 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
267 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
268 // has no base relocs to apply
269 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
271 // Look at the file header to determine if relocations have been stripped, and
272 // save this info in the image context for later use.
274 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
275 ImageContext
->RelocationsStripped
= TRUE
;
277 ImageContext
->RelocationsStripped
= FALSE
;
280 if (!(ImageContext
->IsTeImage
)) {
281 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
285 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
286 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
291 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
292 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
295 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
297 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
300 // Determine the file offset of the debug directory... This means we walk
301 // the sections to find which section contains the RVA of the debug
304 DebugDirectoryEntryFileOffset
= 0;
306 SectionHeaderOffset
= (UINTN
)(
307 ImageContext
->PeCoffHeaderOffset
+
309 sizeof (EFI_IMAGE_FILE_HEADER
) +
310 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
313 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
315 // Read section header from file
317 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
318 Status
= ImageContext
->ImageRead (
319 ImageContext
->Handle
,
324 if (RETURN_ERROR (Status
)) {
325 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
329 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
330 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
332 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
336 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
339 if (DebugDirectoryEntryFileOffset
!= 0) {
340 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
342 // Read next debug directory entry
344 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
345 Status
= ImageContext
->ImageRead (
346 ImageContext
->Handle
,
347 DebugDirectoryEntryFileOffset
,
351 if (RETURN_ERROR (Status
)) {
352 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
355 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
356 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
357 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
358 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
361 return RETURN_SUCCESS
;
368 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
369 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
370 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
372 DebugDirectoryEntryFileOffset
= 0;
374 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
376 // Read section header from file
378 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
379 Status
= ImageContext
->ImageRead (
380 ImageContext
->Handle
,
385 if (RETURN_ERROR (Status
)) {
386 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
390 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
391 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
392 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
393 SectionHeader
.VirtualAddress
+
394 SectionHeader
.PointerToRawData
+
395 sizeof (EFI_TE_IMAGE_HEADER
) -
396 Hdr
.Te
->StrippedSize
;
399 // File offset of the debug directory was found, if this is not the last
400 // section, then skip to the last section for calculating the image size.
402 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
403 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
404 Index
= Hdr
.Te
->NumberOfSections
- 1;
410 // In Te image header there is not a field to describe the ImageSize.
411 // Actually, the ImageSize equals the RVA plus the VirtualSize of
412 // the last section mapped into memory (Must be rounded up to
413 // a mulitple of Section Alignment). Per the PE/COFF specification, the
414 // section headers in the Section Table must appear in order of the RVA
415 // values for the corresponding sections. So the ImageSize can be determined
416 // by the RVA and the VirtualSize of the last section header in the
419 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
420 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
+
421 ImageContext
->SectionAlignment
- 1) & ~(ImageContext
->SectionAlignment
- 1);
424 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
427 if (DebugDirectoryEntryFileOffset
!= 0) {
428 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
430 // Read next debug directory entry
432 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
433 Status
= ImageContext
->ImageRead (
434 ImageContext
->Handle
,
435 DebugDirectoryEntryFileOffset
,
439 if (RETURN_ERROR (Status
)) {
440 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
444 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
445 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
446 return RETURN_SUCCESS
;
452 return RETURN_SUCCESS
;
457 Converts an image address to the loaded address.
459 @param ImageContext The context of the image being loaded.
460 @param Address The address to be converted to the loaded address.
462 @return The converted address or NULL if the address can not be converted.
466 PeCoffLoaderImageAddress (
467 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
472 // @bug Check to make sure ImageSize is correct for the relocated image.
473 // it may only work for the file we start with and not the relocated image
475 if (Address
>= ImageContext
->ImageSize
) {
476 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
480 return (CHAR8
*)((UINTN
) ImageContext
->ImageAddress
+ Address
);
484 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
486 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
487 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
488 of ImageContext as the relocation base address. The caller must allocate the relocation
489 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
490 If ImageContext is NULL, then ASSERT().
492 @param ImageContext Pointer to the image context structure that describes the PE/COFF
493 image that is being relocated.
495 @retval RETURN_SUCCESS The PE/COFF image was relocated.
496 Extended status information is in the ImageError field of ImageContext.
497 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
498 Extended status information is in the ImageError field of ImageContext.
499 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
500 Extended status information is in the ImageError field of ImageContext.
505 PeCoffLoaderRelocateImage (
506 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
509 RETURN_STATUS Status
;
510 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
511 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
513 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
514 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
523 PHYSICAL_ADDRESS BaseAddress
;
524 UINT32 NumberOfRvaAndSizes
;
527 ASSERT (ImageContext
!= NULL
);
532 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
535 // If there are no relocation entries, then we are done
537 if (ImageContext
->RelocationsStripped
) {
538 return RETURN_SUCCESS
;
542 // If the destination address is not 0, use that rather than the
543 // image address as the relocation target.
545 if (ImageContext
->DestinationAddress
!= 0) {
546 BaseAddress
= ImageContext
->DestinationAddress
;
547 } else if (!(ImageContext
->IsTeImage
)) {
548 BaseAddress
= ImageContext
->ImageAddress
;
550 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
551 BaseAddress
= ImageContext
->ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
554 if (!(ImageContext
->IsTeImage
)) {
555 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
557 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
559 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
563 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
564 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
566 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
567 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
572 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
573 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
575 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
576 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
580 // Find the relocation block
581 // Per the PE/COFF spec, you can't assume that a given data directory
582 // is present in the image. You have to check the NumberOfRvaAndSizes in
583 // the optional header to verify a desired directory entry is there.
586 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
587 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
588 RelocBaseEnd
= PeCoffLoaderImageAddress (
590 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
594 // Set base and end to bypass processing below.
596 RelocBase
= RelocBaseEnd
= 0;
599 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
600 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->ImageBase
);
601 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
);
604 // Find the relocation block
606 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
607 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
608 ImageContext
->ImageAddress
+
609 RelocDir
->VirtualAddress
+
610 sizeof(EFI_TE_IMAGE_HEADER
) -
613 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
617 // Run the relocation information and apply the fixups
619 FixupData
= ImageContext
->FixupData
;
620 while (RelocBase
< RelocBaseEnd
) {
622 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
623 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
624 if (!(ImageContext
->IsTeImage
)) {
625 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
627 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
628 RelocBase
->VirtualAddress
+
629 sizeof(EFI_TE_IMAGE_HEADER
) -
634 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
635 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
636 (UINTN
)ImageContext
->ImageSize
)) {
637 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
638 return RETURN_LOAD_ERROR
;
642 // Run this relocation record
644 while (Reloc
< RelocEnd
) {
646 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
647 switch ((*Reloc
) >> 12) {
648 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
651 case EFI_IMAGE_REL_BASED_HIGH
:
652 F16
= (UINT16
*) Fixup
;
653 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
654 if (FixupData
!= NULL
) {
655 *(UINT16
*) FixupData
= *F16
;
656 FixupData
= FixupData
+ sizeof (UINT16
);
660 case EFI_IMAGE_REL_BASED_LOW
:
661 F16
= (UINT16
*) Fixup
;
662 *F16
= (UINT16
) (*F16
+ (UINT16
) Adjust
);
663 if (FixupData
!= NULL
) {
664 *(UINT16
*) FixupData
= *F16
;
665 FixupData
= FixupData
+ sizeof (UINT16
);
669 case EFI_IMAGE_REL_BASED_HIGHLOW
:
670 F32
= (UINT32
*) Fixup
;
671 *F32
= *F32
+ (UINT32
) Adjust
;
672 if (FixupData
!= NULL
) {
673 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
674 *(UINT32
*)FixupData
= *F32
;
675 FixupData
= FixupData
+ sizeof (UINT32
);
679 case EFI_IMAGE_REL_BASED_DIR64
:
680 F64
= (UINT64
*) Fixup
;
681 *F64
= *F64
+ (UINT64
) Adjust
;
682 if (FixupData
!= NULL
) {
683 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
684 *(UINT64
*)(FixupData
) = *F64
;
685 FixupData
= FixupData
+ sizeof(UINT64
);
691 // The common code does not handle some of the stranger IPF relocations
692 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
693 // on IPF and is a No-Op on other archtiectures.
695 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
696 if (RETURN_ERROR (Status
)) {
697 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
703 // Next relocation record
711 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
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
) {
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
1220 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1223 // Run the whole relocation block. And re-fixup data that has not been
1224 // modified. The FixupData is used to see if the image has been modified
1225 // since it was relocated. This is so data sections that have been updated
1226 // by code will not be fixed up, since that would set them back to
1229 FixupData
= RelocationData
;
1230 while (RelocBase
< RelocBaseEnd
) {
1232 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1233 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1234 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1237 // Run this relocation record
1239 while (Reloc
< RelocEnd
) {
1241 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1242 switch ((*Reloc
) >> 12) {
1244 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1247 case EFI_IMAGE_REL_BASED_HIGH
:
1248 F16
= (UINT16
*) Fixup
;
1249 if (*(UINT16
*) FixupData
== *F16
) {
1250 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1253 FixupData
= FixupData
+ sizeof (UINT16
);
1256 case EFI_IMAGE_REL_BASED_LOW
:
1257 F16
= (UINT16
*) Fixup
;
1258 if (*(UINT16
*) FixupData
== *F16
) {
1259 *F16
= (UINT16
) (*F16
+ ((UINT16
) Adjust
& 0xffff));
1262 FixupData
= FixupData
+ sizeof (UINT16
);
1265 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1266 F32
= (UINT32
*) Fixup
;
1267 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1268 if (*(UINT32
*) FixupData
== *F32
) {
1269 *F32
= *F32
+ (UINT32
) Adjust
;
1272 FixupData
= FixupData
+ sizeof (UINT32
);
1275 case EFI_IMAGE_REL_BASED_DIR64
:
1276 F64
= (UINT64
*)Fixup
;
1277 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1278 if (*(UINT64
*) FixupData
== *F64
) {
1279 *F64
= *F64
+ (UINT64
)Adjust
;
1282 FixupData
= FixupData
+ sizeof (UINT64
);
1285 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1287 // Not implemented, but not used in EFI 1.0
1294 // Only Itanium requires ConvertPeImage_Ex
1296 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1297 if (RETURN_ERROR (Status
)) {
1302 // Next relocation record
1309 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1315 ImageRead function that operates on a memory buffer whos base is passed into
1318 @param FileHandle Ponter to baes of the input stream
1319 @param FileOffset Offset to the start of the buffer
1320 @param ReadSize Number of bytes to copy into the buffer
1321 @param Buffer Location to place results of read
1323 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1328 PeCoffLoaderImageReadFromMemory (
1329 IN VOID
*FileHandle
,
1330 IN UINTN FileOffset
,
1331 IN OUT UINTN
*ReadSize
,
1335 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1336 return RETURN_SUCCESS
;