3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 EdkIIGlueDxeDriverEntryPoint.c
23 #include "BasePeCoffLibInternals.h"
26 Retrieves the magic value from the PE/COFF header.
28 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
30 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
31 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
35 PeCoffLoaderGetPeHeaderMagicValue (
36 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
40 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
41 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
42 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
43 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
45 if (Hdr
.Pe32
->FileHeader
.Machine
== EFI_IMAGE_MACHINE_IA64
&& Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
46 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
49 // Return the magic value from the PC/COFF Optional Header
51 return Hdr
.Pe32
->OptionalHeader
.Magic
;
56 Retrieves the PE or TE Header from a PE/COFF or TE image.
58 @param ImageContext The context of the image being loaded.
59 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
61 @retval RETURN_SUCCESS The PE or TE Header is read.
62 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
66 GluePeCoffLoaderGetPeHeader (
67 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
68 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
72 EFI_IMAGE_DOS_HEADER DosHdr
;
77 // Read the DOS image header to check for it's existance
79 Size
= sizeof (EFI_IMAGE_DOS_HEADER
);
80 Status
= ImageContext
->ImageRead (
86 if (RETURN_ERROR (Status
)) {
87 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
91 ImageContext
->PeCoffHeaderOffset
= 0;
92 if (DosHdr
.e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
94 // DOS image header is present, so read the PE header after the DOS image
97 ImageContext
->PeCoffHeaderOffset
= DosHdr
.e_lfanew
;
101 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
102 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
103 // determins if this is a PE32 or PE32+ image. The magic is in the same
104 // location in both images.
106 Size
= sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION
);
107 Status
= ImageContext
->ImageRead (
108 ImageContext
->Handle
,
109 ImageContext
->PeCoffHeaderOffset
,
113 if (RETURN_ERROR (Status
)) {
114 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
119 // Use Signature to figure out if we understand the image format
121 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
122 ImageContext
->IsTeImage
= TRUE
;
123 ImageContext
->Machine
= Hdr
.Te
->Machine
;
124 ImageContext
->ImageType
= (UINT16
)(Hdr
.Te
->Subsystem
);
125 ImageContext
->ImageSize
= 0;
126 ImageContext
->SectionAlignment
= 4096;
127 ImageContext
->SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
129 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
130 ImageContext
->IsTeImage
= FALSE
;
131 ImageContext
->Machine
= Hdr
.Pe32
->FileHeader
.Machine
;
133 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
135 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
139 ImageContext
->ImageType
= Hdr
.Pe32
->OptionalHeader
.Subsystem
;
140 ImageContext
->ImageSize
= (UINT64
)Hdr
.Pe32
->OptionalHeader
.SizeOfImage
;
141 ImageContext
->SectionAlignment
= Hdr
.Pe32
->OptionalHeader
.SectionAlignment
;
142 ImageContext
->SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
144 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
148 ImageContext
->ImageType
= Hdr
.Pe32Plus
->OptionalHeader
.Subsystem
;
149 ImageContext
->ImageSize
= (UINT64
) Hdr
.Pe32Plus
->OptionalHeader
.SizeOfImage
;
150 ImageContext
->SectionAlignment
= Hdr
.Pe32Plus
->OptionalHeader
.SectionAlignment
;
151 ImageContext
->SizeOfHeaders
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
153 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
154 return RETURN_UNSUPPORTED
;
157 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_MACHINE_TYPE
;
158 return RETURN_UNSUPPORTED
;
161 if (!PeCoffLoaderImageFormatSupported (ImageContext
->Machine
)) {
163 // If the PE/COFF loader does not support the image type return
164 // unsupported. This library can suport lots of types of images
165 // this does not mean the user of this library can call the entry
166 // point of the image.
168 return RETURN_UNSUPPORTED
;
171 return RETURN_SUCCESS
;
176 Retrieves information about a PE/COFF image.
178 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
179 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
180 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
181 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
182 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
183 computing the fields of ImageContext, then the error status is returned in the ImageError field of
186 @param ImageContext Pointer to the image context structure that describes the PE/COFF
187 image that needs to be examined by this function.
189 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
190 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
191 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
196 GluePeCoffLoaderGetImageInfo (
197 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
200 RETURN_STATUS Status
;
201 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData
;
202 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
203 EFI_IMAGE_DATA_DIRECTORY
*DebugDirectoryEntry
;
206 UINTN DebugDirectoryEntryRva
;
207 UINTN DebugDirectoryEntryFileOffset
;
208 UINTN SectionHeaderOffset
;
209 EFI_IMAGE_SECTION_HEADER SectionHeader
;
210 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry
;
211 UINT32 NumberOfRvaAndSizes
;
214 if (NULL
== ImageContext
) {
215 return RETURN_INVALID_PARAMETER
;
220 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
222 Hdr
.Union
= &HdrData
;
223 Status
= PeCoffLoaderGetPeHeader (ImageContext
, Hdr
);
224 if (RETURN_ERROR (Status
)) {
228 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
231 // Retrieve the base address of the image
233 if (!(ImageContext
->IsTeImage
)) {
234 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
238 ImageContext
->ImageAddress
= Hdr
.Pe32
->OptionalHeader
.ImageBase
;
243 ImageContext
->ImageAddress
= Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
246 ImageContext
->ImageAddress
= (PHYSICAL_ADDRESS
)(Hdr
.Te
->ImageBase
+ Hdr
.Te
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
));
250 // Initialize the alternate destination address to 0 indicating that it
251 // should not be used.
253 ImageContext
->DestinationAddress
= 0;
256 // Initialize the codeview pointer.
258 ImageContext
->CodeView
= NULL
;
259 ImageContext
->PdbPointer
= NULL
;
262 // Three cases with regards to relocations:
263 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
264 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
265 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
266 // has no base relocs to apply
267 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
269 // Look at the file header to determine if relocations have been stripped, and
270 // save this info in the image context for later use.
272 if ((!(ImageContext
->IsTeImage
)) && ((Hdr
.Pe32
->FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0)) {
273 ImageContext
->RelocationsStripped
= TRUE
;
275 ImageContext
->RelocationsStripped
= FALSE
;
278 if (!(ImageContext
->IsTeImage
)) {
279 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
283 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
284 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
289 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
290 DebugDirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
293 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
295 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
298 // Determine the file offset of the debug directory... This means we walk
299 // the sections to find which section contains the RVA of the debug
302 DebugDirectoryEntryFileOffset
= 0;
304 SectionHeaderOffset
= (UINTN
)(
305 ImageContext
->PeCoffHeaderOffset
+
307 sizeof (EFI_IMAGE_FILE_HEADER
) +
308 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
311 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
313 // Read section header from file
315 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
316 Status
= ImageContext
->ImageRead (
317 ImageContext
->Handle
,
322 if (RETURN_ERROR (Status
)) {
323 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
327 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
328 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
330 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
- SectionHeader
.VirtualAddress
+ SectionHeader
.PointerToRawData
;
334 SectionHeaderOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
337 if (DebugDirectoryEntryFileOffset
!= 0) {
338 for (Index
= 0; Index
< DebugDirectoryEntry
->Size
; Index
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)) {
340 // Read next debug directory entry
342 Size
= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
343 Status
= ImageContext
->ImageRead (
344 ImageContext
->Handle
,
345 DebugDirectoryEntryFileOffset
,
349 if (RETURN_ERROR (Status
)) {
350 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
353 if (DebugEntry
.Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
354 ImageContext
->DebugDirectoryEntryRva
= (UINT32
) (DebugDirectoryEntryRva
+ Index
);
355 if (DebugEntry
.RVA
== 0 && DebugEntry
.FileOffset
!= 0) {
356 ImageContext
->ImageSize
+= DebugEntry
.SizeOfData
;
359 return RETURN_SUCCESS
;
366 DebugDirectoryEntry
= &Hdr
.Te
->DataDirectory
[1];
367 DebugDirectoryEntryRva
= DebugDirectoryEntry
->VirtualAddress
;
368 SectionHeaderOffset
= (UINTN
)(sizeof (EFI_TE_IMAGE_HEADER
));
370 DebugDirectoryEntryFileOffset
= 0;
372 for (Index
= 0; Index
< Hdr
.Te
->NumberOfSections
;) {
374 // Read section header from file
376 Size
= sizeof (EFI_IMAGE_SECTION_HEADER
);
377 Status
= ImageContext
->ImageRead (
378 ImageContext
->Handle
,
383 if (RETURN_ERROR (Status
)) {
384 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
388 if (DebugDirectoryEntryRva
>= SectionHeader
.VirtualAddress
&&
389 DebugDirectoryEntryRva
< SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
) {
390 DebugDirectoryEntryFileOffset
= DebugDirectoryEntryRva
-
391 SectionHeader
.VirtualAddress
+
392 SectionHeader
.PointerToRawData
+
393 sizeof (EFI_TE_IMAGE_HEADER
) -
394 Hdr
.Te
->StrippedSize
;
397 // File offset of the debug directory was found, if this is not the last
398 // section, then skip to the last section for calculating the image size.
400 if (Index
< (UINTN
) Hdr
.Te
->NumberOfSections
- 1) {
401 SectionHeaderOffset
+= (Hdr
.Te
->NumberOfSections
- 1 - Index
) * sizeof (EFI_IMAGE_SECTION_HEADER
);
402 Index
= Hdr
.Te
->NumberOfSections
- 1;
408 // In Te image header there is not a field to describe the ImageSize.
409 // Actually, the ImageSize equals the RVA plus the VirtualSize of
410 // the last section mapped into memory (Must be rounded up to
411 // a mulitple of Section Alignment). Per the PE/COFF specification, the
412 // section headers in the Section Table must appear in order of the RVA
413 // values for the corresponding sections. So the ImageSize can be determined
414 // by the RVA and the VirtualSize of the last section header in the
417 if ((++Index
) == (UINTN
)Hdr
.Te
->NumberOfSections
) {
418 ImageContext
->ImageSize
= (SectionHeader
.VirtualAddress
+ SectionHeader
.Misc
.VirtualSize
+
419 ImageContext
->SectionAlignment
- 1) & ~(ImageContext
->SectionAlignment
- 1);
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 GluePeCoffLoaderImageAddress (
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 GluePeCoffLoaderRelocateImage (
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
;
545 } else if (!(ImageContext
->IsTeImage
)) {
546 BaseAddress
= ImageContext
->ImageAddress
;
548 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
549 BaseAddress
= ImageContext
->ImageAddress
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
552 if (!(ImageContext
->IsTeImage
)) {
553 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
555 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
557 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
561 Adjust
= (UINT64
)BaseAddress
- Hdr
.Pe32
->OptionalHeader
.ImageBase
;
562 Hdr
.Pe32
->OptionalHeader
.ImageBase
= (UINT32
)BaseAddress
;
564 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
565 RelocDir
= &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
570 Adjust
= (UINT64
) BaseAddress
- Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
;
571 Hdr
.Pe32Plus
->OptionalHeader
.ImageBase
= (UINT64
)BaseAddress
;
573 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
574 RelocDir
= &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
578 // Find the relocation block
579 // Per the PE/COFF spec, you can't assume that a given data directory
580 // is present in the image. You have to check the NumberOfRvaAndSizes in
581 // the optional header to verify a desired directory entry is there.
584 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
&& RelocDir
->Size
> 0) {
585 RelocBase
= PeCoffLoaderImageAddress (ImageContext
, RelocDir
->VirtualAddress
);
586 RelocBaseEnd
= PeCoffLoaderImageAddress (
588 RelocDir
->VirtualAddress
+ RelocDir
->Size
- 1
590 if ((RelocBase
== NULL
) || (RelocBaseEnd
== NULL
)) {
592 // If the base start or end address resolved to 0, then fail.
594 return RETURN_LOAD_ERROR
;
598 // Set base and end to bypass processing below.
600 RelocBase
= RelocBaseEnd
= 0;
603 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
604 Adjust
= (UINT64
) (BaseAddress
- Hdr
.Te
->ImageBase
);
605 Hdr
.Te
->ImageBase
= (UINT64
) (BaseAddress
);
608 // Find the relocation block
610 RelocDir
= &Hdr
.Te
->DataDirectory
[0];
611 if (RelocDir
->Size
> 0) {
612 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(
613 ImageContext
->ImageAddress
+
614 RelocDir
->VirtualAddress
+
615 sizeof(EFI_TE_IMAGE_HEADER
) -
618 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*) ((UINTN
) RelocBase
+ (UINTN
) RelocDir
->Size
- 1);
621 // Set base and end to bypass processing below.
629 // Run the relocation information and apply the fixups
631 FixupData
= ImageContext
->FixupData
;
632 while (RelocBase
< RelocBaseEnd
) {
634 Reloc
= (UINT16
*) ((CHAR8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
635 RelocEnd
= (UINT16
*) ((CHAR8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
636 if (!(ImageContext
->IsTeImage
)) {
637 FixupBase
= PeCoffLoaderImageAddress (ImageContext
, RelocBase
->VirtualAddress
);
639 if (FixupBase
== NULL
) {
641 // If the FixupBase address resolved to 0, then fail.
643 return RETURN_LOAD_ERROR
;
646 FixupBase
= (CHAR8
*)(UINTN
)(ImageContext
->ImageAddress
+
647 RelocBase
->VirtualAddress
+
648 sizeof(EFI_TE_IMAGE_HEADER
) -
653 if ((CHAR8
*) RelocEnd
< (CHAR8
*) ((UINTN
) ImageContext
->ImageAddress
) ||
654 (CHAR8
*) RelocEnd
> (CHAR8
*)((UINTN
)ImageContext
->ImageAddress
+
655 (UINTN
)ImageContext
->ImageSize
)) {
656 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
657 return RETURN_LOAD_ERROR
;
661 // Run this relocation record
663 while (Reloc
< RelocEnd
) {
665 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
666 switch ((*Reloc
) >> 12) {
667 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
670 case EFI_IMAGE_REL_BASED_HIGH
:
671 F16
= (UINT16
*) Fixup
;
672 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
673 if (FixupData
!= NULL
) {
674 *(UINT16
*) FixupData
= *F16
;
675 FixupData
= FixupData
+ sizeof (UINT16
);
679 case EFI_IMAGE_REL_BASED_LOW
:
680 F16
= (UINT16
*) Fixup
;
681 *F16
= (UINT16
) (*F16
+ (UINT16
) Adjust
);
682 if (FixupData
!= NULL
) {
683 *(UINT16
*) FixupData
= *F16
;
684 FixupData
= FixupData
+ sizeof (UINT16
);
688 case EFI_IMAGE_REL_BASED_HIGHLOW
:
689 F32
= (UINT32
*) Fixup
;
690 *F32
= *F32
+ (UINT32
) Adjust
;
691 if (FixupData
!= NULL
) {
692 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
693 *(UINT32
*)FixupData
= *F32
;
694 FixupData
= FixupData
+ sizeof (UINT32
);
698 case EFI_IMAGE_REL_BASED_DIR64
:
699 F64
= (UINT64
*) Fixup
;
700 *F64
= *F64
+ (UINT64
) Adjust
;
701 if (FixupData
!= NULL
) {
702 FixupData
= ALIGN_POINTER (FixupData
, sizeof(UINT64
));
703 *(UINT64
*)(FixupData
) = *F64
;
704 FixupData
= FixupData
+ sizeof(UINT64
);
710 // The common code does not handle some of the stranger IPF relocations
711 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
712 // on IPF and is a No-Op on other archtiectures.
714 Status
= PeCoffLoaderRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
715 if (RETURN_ERROR (Status
)) {
716 ImageContext
->ImageError
= IMAGE_ERROR_FAILED_RELOCATION
;
722 // Next relocation record
730 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
733 return RETURN_SUCCESS
;
737 Loads a PE/COFF image into memory.
739 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
740 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
741 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
742 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
743 If ImageContext is NULL, then ASSERT().
745 @param ImageContext Pointer to the image context structure that describes the PE/COFF
746 image that is being loaded.
748 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
749 the ImageAddress and ImageSize fields of ImageContext.
750 Extended status information is in the ImageError field of ImageContext.
751 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
752 Extended status information is in the ImageError field of ImageContext.
753 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
754 Extended status information is in the ImageError field of ImageContext.
755 @retval RETURN_INVALID_PARAMETER The image address is invalid.
756 Extended status information is in the ImageError field of ImageContext.
761 GluePeCoffLoaderLoadImage (
762 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
765 RETURN_STATUS Status
;
766 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
767 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext
;
768 EFI_IMAGE_SECTION_HEADER
*FirstSection
;
769 EFI_IMAGE_SECTION_HEADER
*Section
;
770 UINTN NumberOfSections
;
775 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
776 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
778 UINT32 TempDebugEntryRva
;
779 UINT32 NumberOfRvaAndSizes
;
782 ASSERT (ImageContext
!= NULL
);
787 ImageContext
->ImageError
= IMAGE_ERROR_SUCCESS
;
790 // Copy the provided context info into our local version, get what we
791 // can from the original image, and then use that to make sure everything
794 CopyMem (&CheckContext
, ImageContext
, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT
));
796 Status
= PeCoffLoaderGetImageInfo (&CheckContext
);
797 if (RETURN_ERROR (Status
)) {
802 // Make sure there is enough allocated space for the image being loaded
804 if (ImageContext
->ImageSize
< CheckContext
.ImageSize
) {
805 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_SIZE
;
806 return RETURN_BUFFER_TOO_SMALL
;
808 if (ImageContext
->ImageAddress
== 0) {
810 // Image cannot be loaded into 0 address.
812 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
813 return RETURN_INVALID_PARAMETER
;
816 // If there's no relocations, then make sure it's not a runtime driver,
817 // and that it's being loaded at the linked address.
819 if (CheckContext
.RelocationsStripped
) {
821 // If the image does not contain relocations and it is a runtime driver
822 // then return an error.
824 if (CheckContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
) {
825 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
826 return RETURN_LOAD_ERROR
;
829 // If the image does not contain relocations, and the requested load address
830 // is not the linked address, then return an error.
832 if (CheckContext
.ImageAddress
!= ImageContext
->ImageAddress
) {
833 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_IMAGE_ADDRESS
;
834 return RETURN_INVALID_PARAMETER
;
838 // Make sure the allocated space has the proper section alignment
840 if (!(ImageContext
->IsTeImage
)) {
841 if ((ImageContext
->ImageAddress
& (CheckContext
.SectionAlignment
- 1)) != 0) {
842 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SECTION_ALIGNMENT
;
843 return RETURN_INVALID_PARAMETER
;
847 // Read the entire PE/COFF or TE header into memory
849 if (!(ImageContext
->IsTeImage
)) {
850 Status
= ImageContext
->ImageRead (
851 ImageContext
->Handle
,
853 &ImageContext
->SizeOfHeaders
,
854 (VOID
*) (UINTN
) ImageContext
->ImageAddress
857 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)ImageContext
->ImageAddress
+ ImageContext
->PeCoffHeaderOffset
);
859 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
860 (UINTN
)ImageContext
->ImageAddress
+
861 ImageContext
->PeCoffHeaderOffset
+
863 sizeof(EFI_IMAGE_FILE_HEADER
) +
864 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
866 NumberOfSections
= (UINTN
) (Hdr
.Pe32
->FileHeader
.NumberOfSections
);
868 Status
= ImageContext
->ImageRead (
869 ImageContext
->Handle
,
871 &ImageContext
->SizeOfHeaders
,
872 (void *)(UINTN
)ImageContext
->ImageAddress
875 Hdr
.Te
= (EFI_TE_IMAGE_HEADER
*)(UINTN
)(ImageContext
->ImageAddress
);
877 FirstSection
= (EFI_IMAGE_SECTION_HEADER
*) (
878 (UINTN
)ImageContext
->ImageAddress
+
879 sizeof(EFI_TE_IMAGE_HEADER
)
881 NumberOfSections
= (UINTN
) (Hdr
.Te
->NumberOfSections
);
885 if (RETURN_ERROR (Status
)) {
886 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
887 return RETURN_LOAD_ERROR
;
891 // Load each section of the image
893 Section
= FirstSection
;
894 for (Index
= 0, MaxEnd
= NULL
; Index
< NumberOfSections
; Index
++) {
897 // Compute sections address
899 Base
= PeCoffLoaderImageAddress (ImageContext
, Section
->VirtualAddress
);
900 End
= PeCoffLoaderImageAddress (
902 Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
- 1
904 if (ImageContext
->IsTeImage
) {
905 Base
= (CHAR8
*)((UINTN
) Base
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
906 End
= (CHAR8
*)((UINTN
) End
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
);
913 // If the base start or end address resolved to 0, then fail.
915 if ((Base
== NULL
) || (End
== NULL
)) {
916 ImageContext
->ImageError
= IMAGE_ERROR_SECTION_NOT_LOADED
;
917 return RETURN_LOAD_ERROR
;
923 Size
= (UINTN
) Section
->Misc
.VirtualSize
;
924 if ((Size
== 0) || (Size
> Section
->SizeOfRawData
)) {
925 Size
= (UINTN
) Section
->SizeOfRawData
;
928 if (Section
->SizeOfRawData
) {
929 if (!(ImageContext
->IsTeImage
)) {
930 Status
= ImageContext
->ImageRead (
931 ImageContext
->Handle
,
932 Section
->PointerToRawData
,
937 Status
= ImageContext
->ImageRead (
938 ImageContext
->Handle
,
939 Section
->PointerToRawData
+ sizeof (EFI_TE_IMAGE_HEADER
) - (UINTN
)Hdr
.Te
->StrippedSize
,
945 if (RETURN_ERROR (Status
)) {
946 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
952 // If raw size is less then virt size, zero fill the remaining
955 if (Size
< Section
->Misc
.VirtualSize
) {
956 ZeroMem (Base
+ Size
, Section
->Misc
.VirtualSize
- Size
);
966 // Get image's entry point
968 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
969 if (!(ImageContext
->IsTeImage
)) {
971 // Sizes of AddressOfEntryPoint are different so we need to do this safely
973 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
977 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
979 (UINTN
)Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
985 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
)(UINTN
)PeCoffLoaderImageAddress (
987 (UINTN
)Hdr
.Pe32Plus
->OptionalHeader
.AddressOfEntryPoint
991 ImageContext
->EntryPoint
= (PHYSICAL_ADDRESS
) (
992 (UINTN
)ImageContext
->ImageAddress
+
993 (UINTN
)Hdr
.Te
->AddressOfEntryPoint
+
994 (UINTN
)sizeof(EFI_TE_IMAGE_HEADER
) -
995 (UINTN
)Hdr
.Te
->StrippedSize
1000 // Determine the size of the fixup data
1002 // Per the PE/COFF spec, you can't assume that a given data directory
1003 // is present in the image. You have to check the NumberOfRvaAndSizes in
1004 // the optional header to verify a desired directory entry is there.
1006 if (!(ImageContext
->IsTeImage
)) {
1007 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1011 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1012 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1017 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1018 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1021 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1022 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1024 ImageContext
->FixupDataSize
= 0;
1027 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[0];
1028 ImageContext
->FixupDataSize
= DirectoryEntry
->Size
/ sizeof (UINT16
) * sizeof (UINTN
);
1031 // Consumer must allocate a buffer for the relocation fixup log.
1032 // Only used for runtime drivers.
1034 ImageContext
->FixupData
= NULL
;
1037 // Load the Codeview info if present
1039 if (ImageContext
->DebugDirectoryEntryRva
!= 0) {
1040 if (!(ImageContext
->IsTeImage
)) {
1041 DebugEntry
= PeCoffLoaderImageAddress (
1043 ImageContext
->DebugDirectoryEntryRva
1046 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(UINTN
)(
1047 ImageContext
->ImageAddress
+
1048 ImageContext
->DebugDirectoryEntryRva
+
1049 sizeof(EFI_TE_IMAGE_HEADER
) -
1050 Hdr
.Te
->StrippedSize
1054 if (DebugEntry
!= NULL
) {
1055 TempDebugEntryRva
= DebugEntry
->RVA
;
1056 if (DebugEntry
->RVA
== 0 && DebugEntry
->FileOffset
!= 0) {
1058 if ((UINTN
)Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
) {
1059 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->Misc
.VirtualSize
;
1061 TempDebugEntryRva
= Section
->VirtualAddress
+ Section
->SizeOfRawData
;
1065 if (TempDebugEntryRva
!= 0) {
1066 if (!(ImageContext
->IsTeImage
)) {
1067 ImageContext
->CodeView
= PeCoffLoaderImageAddress (ImageContext
, TempDebugEntryRva
);
1069 ImageContext
->CodeView
= (VOID
*)(
1070 (UINTN
)ImageContext
->ImageAddress
+
1071 (UINTN
)TempDebugEntryRva
+
1072 (UINTN
)sizeof (EFI_TE_IMAGE_HEADER
) -
1073 (UINTN
) Hdr
.Te
->StrippedSize
1077 if (ImageContext
->CodeView
== NULL
) {
1078 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1079 return RETURN_LOAD_ERROR
;
1082 if (DebugEntry
->RVA
== 0) {
1083 Size
= DebugEntry
->SizeOfData
;
1084 if (!(ImageContext
->IsTeImage
)) {
1085 Status
= ImageContext
->ImageRead (
1086 ImageContext
->Handle
,
1087 DebugEntry
->FileOffset
,
1089 ImageContext
->CodeView
1092 Status
= ImageContext
->ImageRead (
1093 ImageContext
->Handle
,
1094 DebugEntry
->FileOffset
+ sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
,
1096 ImageContext
->CodeView
1099 // Should we apply fix up to this field according to the size difference between PE and TE?
1100 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1101 // in original PE image.
1105 if (RETURN_ERROR (Status
)) {
1106 ImageContext
->ImageError
= IMAGE_ERROR_IMAGE_READ
;
1107 return RETURN_LOAD_ERROR
;
1110 DebugEntry
->RVA
= TempDebugEntryRva
;
1113 switch (*(UINT32
*) ImageContext
->CodeView
) {
1114 case CODEVIEW_SIGNATURE_NB10
:
1115 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
1118 case CODEVIEW_SIGNATURE_RSDS
:
1119 ImageContext
->PdbPointer
= (CHAR8
*)ImageContext
->CodeView
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
1134 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1137 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1138 the fixups with a virtual mapping.
1141 @param ImageBase Base address of relocated image
1142 @param VirtImageBase Virtual mapping for ImageBase
1143 @param ImageSize Size of the image to relocate
1144 @param RelocationData Location to place results of read
1149 PeCoffLoaderRelocateImageForRuntime (
1150 IN PHYSICAL_ADDRESS ImageBase
,
1151 IN PHYSICAL_ADDRESS VirtImageBase
,
1153 IN VOID
*RelocationData
1158 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1159 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
1160 UINT32 NumberOfRvaAndSizes
;
1161 EFI_IMAGE_DATA_DIRECTORY
*DataDirectory
;
1162 EFI_IMAGE_DATA_DIRECTORY
*RelocDir
;
1163 EFI_IMAGE_BASE_RELOCATION
*RelocBase
;
1164 EFI_IMAGE_BASE_RELOCATION
*RelocBaseEnd
;
1174 RETURN_STATUS Status
;
1177 OldBase
= (CHAR8
*)((UINTN
)ImageBase
);
1178 NewBase
= (CHAR8
*)((UINTN
)VirtImageBase
);
1179 Adjust
= (UINTN
) NewBase
- (UINTN
) OldBase
;
1182 // Find the image's relocate dir info
1184 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)OldBase
;
1185 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
1187 // Valid DOS header so get address of PE header
1189 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(((CHAR8
*)DosHdr
) + DosHdr
->e_lfanew
);
1192 // No Dos header so assume image starts with PE header.
1194 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)OldBase
;
1197 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
1199 // Not a valid PE image so Exit
1204 Magic
= PeCoffLoaderGetPeHeaderMagicValue (Hdr
);
1206 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
1210 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
1211 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[0]);
1216 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
1217 DataDirectory
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[0]);
1221 // Find the relocation block
1223 // Per the PE/COFF spec, you can't assume that a given data directory
1224 // is present in the image. You have to check the NumberOfRvaAndSizes in
1225 // the optional header to verify a desired directory entry is there.
1227 if (NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
1228 RelocDir
= DataDirectory
+ EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
;
1229 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
);
1230 RelocBaseEnd
= (EFI_IMAGE_BASE_RELOCATION
*)(UINTN
)(ImageBase
+ RelocDir
->VirtualAddress
+ RelocDir
->Size
);
1233 // Cannot find relocations, cannot continue
1239 ASSERT (RelocBase
!= NULL
&& RelocBaseEnd
!= NULL
);
1242 // Run the whole relocation block. And re-fixup data that has not been
1243 // modified. The FixupData is used to see if the image has been modified
1244 // since it was relocated. This is so data sections that have been updated
1245 // by code will not be fixed up, since that would set them back to
1248 FixupData
= RelocationData
;
1249 while (RelocBase
< RelocBaseEnd
) {
1251 Reloc
= (UINT16
*) ((UINT8
*) RelocBase
+ sizeof (EFI_IMAGE_BASE_RELOCATION
));
1252 RelocEnd
= (UINT16
*) ((UINT8
*) RelocBase
+ RelocBase
->SizeOfBlock
);
1253 FixupBase
= (CHAR8
*) ((UINTN
)ImageBase
) + RelocBase
->VirtualAddress
;
1256 // Run this relocation record
1258 while (Reloc
< RelocEnd
) {
1260 Fixup
= FixupBase
+ (*Reloc
& 0xFFF);
1261 switch ((*Reloc
) >> 12) {
1263 case EFI_IMAGE_REL_BASED_ABSOLUTE
:
1266 case EFI_IMAGE_REL_BASED_HIGH
:
1267 F16
= (UINT16
*) Fixup
;
1268 if (*(UINT16
*) FixupData
== *F16
) {
1269 *F16
= (UINT16
) (*F16
+ ((UINT16
) ((UINT32
) Adjust
>> 16)));
1272 FixupData
= FixupData
+ sizeof (UINT16
);
1275 case EFI_IMAGE_REL_BASED_LOW
:
1276 F16
= (UINT16
*) Fixup
;
1277 if (*(UINT16
*) FixupData
== *F16
) {
1278 *F16
= (UINT16
) (*F16
+ ((UINT16
) Adjust
& 0xffff));
1281 FixupData
= FixupData
+ sizeof (UINT16
);
1284 case EFI_IMAGE_REL_BASED_HIGHLOW
:
1285 F32
= (UINT32
*) Fixup
;
1286 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT32
));
1287 if (*(UINT32
*) FixupData
== *F32
) {
1288 *F32
= *F32
+ (UINT32
) Adjust
;
1291 FixupData
= FixupData
+ sizeof (UINT32
);
1294 case EFI_IMAGE_REL_BASED_DIR64
:
1295 F64
= (UINT64
*)Fixup
;
1296 FixupData
= ALIGN_POINTER (FixupData
, sizeof (UINT64
));
1297 if (*(UINT64
*) FixupData
== *F64
) {
1298 *F64
= *F64
+ (UINT64
)Adjust
;
1301 FixupData
= FixupData
+ sizeof (UINT64
);
1304 case EFI_IMAGE_REL_BASED_HIGHADJ
:
1306 // Not implemented, but not used in EFI 1.0
1313 // Only Itanium requires ConvertPeImage_Ex
1315 Status
= PeHotRelocateImageEx (Reloc
, Fixup
, &FixupData
, Adjust
);
1316 if (RETURN_ERROR (Status
)) {
1321 // Next relocation record
1328 RelocBase
= (EFI_IMAGE_BASE_RELOCATION
*) RelocEnd
;
1334 ImageRead function that operates on a memory buffer whos base is passed into
1337 @param FileHandle Ponter to baes of the input stream
1338 @param FileOffset Offset to the start of the buffer
1339 @param ReadSize Number of bytes to copy into the buffer
1340 @param Buffer Location to place results of read
1342 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1347 PeCoffLoaderImageReadFromMemory (
1348 IN VOID
*FileHandle
,
1349 IN UINTN FileOffset
,
1350 IN OUT UINTN
*ReadSize
,
1354 CopyMem (Buffer
, ((UINT8
*)FileHandle
) + FileOffset
, *ReadSize
);
1355 return RETURN_SUCCESS
;