]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
Fixed bugs found when this code was ported into R8.
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / BasePeCoff.c
1 /** @file
2 Tiano PE/COFF loader.
3
4 This PE/COFF loader supports loading any PE32 or PE32+ image type, but
5 only supports relocating IA32, X64, IPF, and EBC images.
6
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
12
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.
15
16 Module Name: PeCoffLoader.c
17
18 **/
19
20
21 /**
22 Performs an Itanium-based specific relocation fixup and is a no-op on other
23 instruction sets.
24
25 @param Reloc Pointer to the relocation record.
26 @param Fixup Pointer to the address to fix up.
27 @param FixupData Pointer to a buffer to log the fixups.
28 @param Adjust The offset to adjust the fixup.
29
30 @return Status code.
31
32 **/
33 RETURN_STATUS
34 PeCoffLoaderRelocateImageEx (
35 IN UINT16 *Reloc,
36 IN OUT CHAR8 *Fixup,
37 IN OUT CHAR8 **FixupData,
38 IN UINT64 Adjust
39 );
40
41
42 /**
43 Performs an Itanium-based specific re-relocation fixup and is a no-op on other
44 instruction sets. This is used to re-relocated the image into the EFI virtual
45 space for runtime calls.
46
47 @param Reloc Pointer to the relocation record.
48 @param Fixup Pointer to the address to fix up.
49 @param FixupData Pointer to a buffer to log the fixups.
50 @param Adjust The offset to adjust the fixup.
51
52 @return Status code.
53
54 **/
55 RETURN_STATUS
56 PeHotRelocateImageEx (
57 IN UINT16 *Reloc,
58 IN OUT CHAR8 *Fixup,
59 IN OUT CHAR8 **FixupData,
60 IN UINT64 Adjust
61 );
62
63
64 /**
65 Returns TRUE if the machine type of PE/COFF image is supported. Supported
66 does not mean the image can be executed it means the PE/COFF loader supports
67 loading and relocating of the image type. It's up to the caller to support
68 the entry point.
69
70 @param Machine Machine type from the PE Header.
71
72 @return TRUE if this PE/COFF loader can load the image
73
74 **/
75 BOOLEAN
76 PeCoffLoaderImageFormatSupported (
77 IN UINT16 Machine
78 );
79
80
81
82 /**
83 Retrieves the PE or TE Header from a PE/COFF or TE image.
84
85 @param ImageContext The context of the image being loaded.
86 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
87
88 @retval RETURN_SUCCESS The PE or TE Header is read.
89 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
90
91 **/
92 RETURN_STATUS
93 PeCoffLoaderGetPeHeader (
94 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
95 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
96 )
97 {
98 RETURN_STATUS Status;
99 EFI_IMAGE_DOS_HEADER DosHdr;
100 UINTN Size;
101
102 //
103 // Read the DOS image header to check for it's existance
104 //
105 Size = sizeof (EFI_IMAGE_DOS_HEADER);
106 Status = ImageContext->ImageRead (
107 ImageContext->Handle,
108 0,
109 &Size,
110 &DosHdr
111 );
112 if (RETURN_ERROR (Status)) {
113 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
114 return Status;
115 }
116
117 ImageContext->PeCoffHeaderOffset = 0;
118 if (DosHdr.e_magic == EFI_IMAGE_DOS_SIGNATURE) {
119 //
120 // DOS image header is present, so read the PE header after the DOS image
121 // header
122 //
123 ImageContext->PeCoffHeaderOffset = DosHdr.e_lfanew;
124 }
125
126 //
127 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
128 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
129 // determins if this is a PE32 or PE32+ image. The magic is in the same
130 // location in both images.
131 //
132 Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
133 Status = ImageContext->ImageRead (
134 ImageContext->Handle,
135 ImageContext->PeCoffHeaderOffset,
136 &Size,
137 Hdr.Pe32
138 );
139 if (RETURN_ERROR (Status)) {
140 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
141 return Status;
142 }
143
144 //
145 // Use Signature to figure out if we understand the image format
146 //
147 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
148 ImageContext->IsTeImage = TRUE;
149 ImageContext->Machine = Hdr.Te->Machine;
150 ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem);
151 ImageContext->ImageSize = 0;
152 ImageContext->SectionAlignment = 4096;
153 ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;
154
155 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
156 ImageContext->IsTeImage = FALSE;
157 ImageContext->Machine = Hdr.Pe32->FileHeader.Machine;
158
159 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
160 //
161 // Use PE32 offset
162 //
163 ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem;
164 ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage;
165 ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment;
166 ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;
167
168 } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
169 //
170 // Use PE32+ offset
171 //
172 ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem;
173 ImageContext->ImageSize = (UINT64) Hdr.Pe32Plus->OptionalHeader.SizeOfImage;
174 ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;
175 ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;
176 } else {
177 ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;
178 return RETURN_UNSUPPORTED;
179 }
180 } else {
181 ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;
182 return RETURN_UNSUPPORTED;
183 }
184
185 if (!PeCoffLoaderImageFormatSupported (ImageContext->Machine)) {
186 //
187 // If the PE/COFF loader does not support the image type return
188 // unsupported. This library can suport lots of types of images
189 // this does not mean the user of this library can call the entry
190 // point of the image.
191 //
192 return RETURN_UNSUPPORTED;
193 }
194
195 return RETURN_SUCCESS;
196 }
197
198
199 /**
200 Retrieves information about a PE/COFF image.
201
202 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
203 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
204 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
205 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
206 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
207 computing the fields of ImageContext, then the error status is returned in the ImageError field of
208 ImageContext.
209
210 @param ImageContext Pointer to the image context structure that describes the PE/COFF
211 image that needs to be examined by this function.
212
213 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
214 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
215 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
216
217 **/
218 RETURN_STATUS
219 EFIAPI
220 PeCoffLoaderGetImageInfo (
221 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
222 )
223 {
224 RETURN_STATUS Status;
225 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
226 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
227 EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry;
228 UINTN Size;
229 UINTN Index;
230 UINTN DebugDirectoryEntryRva;
231 UINTN DebugDirectoryEntryFileOffset;
232 UINTN SectionHeaderOffset;
233 EFI_IMAGE_SECTION_HEADER SectionHeader;
234 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry;
235 UINT32 NumberOfRvaAndSizes;
236
237 if (NULL == ImageContext) {
238 return RETURN_INVALID_PARAMETER;
239 }
240 //
241 // Assume success
242 //
243 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
244
245 Hdr.Union = &HdrData;
246 Status = PeCoffLoaderGetPeHeader (ImageContext, Hdr);
247 if (RETURN_ERROR (Status)) {
248 return Status;
249 }
250
251 //
252 // Retrieve the base address of the image
253 //
254 if (!(ImageContext->IsTeImage)) {
255 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
256 //
257 // Use PE32 offset
258 //
259 ImageContext->ImageAddress = Hdr.Pe32->OptionalHeader.ImageBase;
260 } else {
261 //
262 // Use PE32+ offset
263 //
264 ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase;
265 }
266 } else {
267 ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase);
268 }
269
270 //
271 // Initialize the alternate destination address to 0 indicating that it
272 // should not be used.
273 //
274 ImageContext->DestinationAddress = 0;
275
276 //
277 // Initialize the codeview pointer.
278 //
279 ImageContext->CodeView = NULL;
280 ImageContext->PdbPointer = NULL;
281
282 //
283 // Three cases with regards to relocations:
284 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
285 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
286 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
287 // has no base relocs to apply
288 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
289 //
290 // Look at the file header to determine if relocations have been stripped, and
291 // save this info in the image context for later use.
292 //
293 if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {
294 ImageContext->RelocationsStripped = TRUE;
295 } else {
296 ImageContext->RelocationsStripped = FALSE;
297 }
298
299 if (!(ImageContext->IsTeImage)) {
300 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
301 //
302 // Use PE32 offset
303 //
304 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
305 DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
306 } else {
307 //
308 // Use PE32+ offset
309 //
310 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
311 DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
312 }
313
314 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
315
316 DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;
317
318 //
319 // Determine the file offset of the debug directory... This means we walk
320 // the sections to find which section contains the RVA of the debug
321 // directory
322 //
323 DebugDirectoryEntryFileOffset = 0;
324
325 SectionHeaderOffset = (UINTN)(
326 ImageContext->PeCoffHeaderOffset +
327 sizeof (UINT32) +
328 sizeof (EFI_IMAGE_FILE_HEADER) +
329 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
330 );
331
332 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {
333 //
334 // Read section header from file
335 //
336 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
337 Status = ImageContext->ImageRead (
338 ImageContext->Handle,
339 SectionHeaderOffset,
340 &Size,
341 &SectionHeader
342 );
343 if (RETURN_ERROR (Status)) {
344 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
345 return Status;
346 }
347
348 if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&
349 DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {
350
351 DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData;
352 break;
353 }
354
355 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
356 }
357
358 if (DebugDirectoryEntryFileOffset != 0) {
359 for (Index = 0; Index < DebugDirectoryEntry->Size; Index++) {
360 //
361 // Read next debug directory entry
362 //
363 Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
364 Status = ImageContext->ImageRead (
365 ImageContext->Handle,
366 DebugDirectoryEntryFileOffset,
367 &Size,
368 &DebugEntry
369 );
370 if (RETURN_ERROR (Status)) {
371 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
372 return Status;
373 }
374
375 if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
376 ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY));
377 if (DebugEntry.RVA == 0 && DebugEntry.FileOffset != 0) {
378 ImageContext->ImageSize += DebugEntry.SizeOfData;
379 }
380
381 return RETURN_SUCCESS;
382 }
383 }
384 }
385 }
386 } else {
387
388 DebugDirectoryEntry = &Hdr.Te->DataDirectory[1];
389 DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;
390 SectionHeaderOffset = (UINTN)(sizeof (EFI_TE_IMAGE_HEADER));
391
392 DebugDirectoryEntryFileOffset = 0;
393
394 for (Index = 0; Index < Hdr.Te->NumberOfSections;) {
395 //
396 // Read section header from file
397 //
398 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
399 Status = ImageContext->ImageRead (
400 ImageContext->Handle,
401 SectionHeaderOffset,
402 &Size,
403 &SectionHeader
404 );
405 if (RETURN_ERROR (Status)) {
406 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
407 return Status;
408 }
409
410 if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&
411 DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {
412 DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva -
413 SectionHeader.VirtualAddress +
414 SectionHeader.PointerToRawData +
415 sizeof (EFI_TE_IMAGE_HEADER) -
416 Hdr.Te->StrippedSize;
417
418 //
419 // File offset of the debug directory was found, if this is not the last
420 // section, then skip to the last section for calculating the image size.
421 //
422 if (Index < (UINTN) Hdr.Te->NumberOfSections - 1) {
423 SectionHeaderOffset += (Hdr.Te->NumberOfSections - 1 - Index) * sizeof (EFI_IMAGE_SECTION_HEADER);
424 Index = Hdr.Te->NumberOfSections - 1;
425 continue;
426 }
427 }
428
429 //
430 // In Te image header there is not a field to describe the ImageSize.
431 // Actually, the ImageSize equals the RVA plus the VirtualSize of
432 // the last section mapped into memory (Must be rounded up to
433 // a mulitple of Section Alignment). Per the PE/COFF specification, the
434 // section headers in the Section Table must appear in order of the RVA
435 // values for the corresponding sections. So the ImageSize can be determined
436 // by the RVA and the VirtualSize of the last section header in the
437 // Section Table.
438 //
439 if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) {
440 ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize +
441 ImageContext->SectionAlignment - 1) & ~(ImageContext->SectionAlignment - 1);
442 }
443
444 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
445 }
446
447 if (DebugDirectoryEntryFileOffset != 0) {
448 for (Index = 0; Index < DebugDirectoryEntry->Size; Index++) {
449 //
450 // Read next debug directory entry
451 //
452 Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
453 Status = ImageContext->ImageRead (
454 ImageContext->Handle,
455 DebugDirectoryEntryFileOffset,
456 &Size,
457 &DebugEntry
458 );
459 if (RETURN_ERROR (Status)) {
460 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
461 return Status;
462 }
463
464 if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
465 ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY));
466 return RETURN_SUCCESS;
467 }
468 }
469 }
470 }
471
472 return RETURN_SUCCESS;
473 }
474
475
476 /**
477 Converts an image address to the loaded address.
478
479 @param ImageContext The context of the image being loaded.
480 @param Address The address to be converted to the loaded address.
481
482 @return The converted address or NULL if the address can not be converted.
483
484 **/
485 VOID *
486 PeCoffLoaderImageAddress (
487 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
488 IN UINTN Address
489 )
490 {
491 //
492 // @bug Check to make sure ImageSize is correct for the relocated image.
493 // it may only work for the file we start with and not the relocated image
494 //
495 if (Address >= ImageContext->ImageSize) {
496 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
497 return NULL;
498 }
499
500 return (CHAR8 *)((UINTN) ImageContext->ImageAddress + Address);
501 }
502
503 /**
504 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
505
506 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
507 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
508 of ImageContext as the relocation base address. The caller must allocate the relocation
509 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
510 If ImageContext is NULL, then ASSERT().
511
512 @param ImageContext Pointer to the image context structure that describes the PE/COFF
513 image that is being relocated.
514
515 @retval RETURN_SUCCESS The PE/COFF image was relocated.
516 Extended status information is in the ImageError field of ImageContext.
517 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
518 Extended status information is in the ImageError field of ImageContext.
519 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
520 Extended status information is in the ImageError field of ImageContext.
521
522 **/
523 RETURN_STATUS
524 EFIAPI
525 PeCoffLoaderRelocateImage (
526 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
527 )
528 {
529 RETURN_STATUS Status;
530 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
531 EFI_IMAGE_DATA_DIRECTORY *RelocDir;
532 UINT64 Adjust;
533 EFI_IMAGE_BASE_RELOCATION *RelocBase;
534 EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
535 UINT16 *Reloc;
536 UINT16 *RelocEnd;
537 CHAR8 *Fixup;
538 CHAR8 *FixupBase;
539 UINT16 *F16;
540 UINT32 *F32;
541 UINT64 *F64;
542 CHAR8 *FixupData;
543 PHYSICAL_ADDRESS BaseAddress;
544 UINT32 NumberOfRvaAndSizes;
545
546 ASSERT (ImageContext != NULL);
547
548 //
549 // Assume success
550 //
551 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
552
553 //
554 // If there are no relocation entries, then we are done
555 //
556 if (ImageContext->RelocationsStripped) {
557 return RETURN_SUCCESS;
558 }
559
560 //
561 // If the destination address is not 0, use that rather than the
562 // image address as the relocation target.
563 //
564 if (ImageContext->DestinationAddress != 0) {
565 BaseAddress = ImageContext->DestinationAddress;
566 } else {
567 BaseAddress = ImageContext->ImageAddress;
568 }
569
570 if (!(ImageContext->IsTeImage)) {
571 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);
572 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
573 //
574 // Use PE32 offset
575 //
576 Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase;
577 Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;
578
579 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
580 RelocDir = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
581 } else {
582 //
583 // Use PE32+ offset
584 //
585 Adjust = (UINT64) BaseAddress - Hdr.Pe32Plus->OptionalHeader.ImageBase;
586 Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;
587
588 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
589 RelocDir = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
590 }
591
592 //
593 // Find the relocation block
594 // Per the PE/COFF spec, you can't assume that a given data directory
595 // is present in the image. You have to check the NumberOfRvaAndSizes in
596 // the optional header to verify a desired directory entry is there.
597 //
598
599 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
600 RelocBase = PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress);
601 RelocBaseEnd = PeCoffLoaderImageAddress (
602 ImageContext,
603 RelocDir->VirtualAddress + RelocDir->Size - 1
604 );
605 } else {
606 //
607 // Set base and end to bypass processing below.
608 //
609 RelocBase = RelocBaseEnd = 0;
610 }
611 } else {
612 Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
613 Adjust = (UINT64) (BaseAddress - Hdr.Te->ImageBase);
614 Hdr.Te->ImageBase = (UINT64) (BaseAddress);
615
616 //
617 // Find the relocation block
618 //
619 RelocDir = &Hdr.Te->DataDirectory[0];
620 RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(
621 ImageContext->ImageAddress +
622 RelocDir->VirtualAddress +
623 sizeof(EFI_TE_IMAGE_HEADER) -
624 Hdr.Te->StrippedSize
625 );
626 RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) ((UINTN) RelocBase + (UINTN) RelocDir->Size - 1);
627 }
628
629 //
630 // Run the relocation information and apply the fixups
631 //
632 FixupData = ImageContext->FixupData;
633 while (RelocBase < RelocBaseEnd) {
634
635 Reloc = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));
636 RelocEnd = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);
637 if (!(ImageContext->IsTeImage)) {
638 FixupBase = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress);
639 } else {
640 FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +
641 RelocBase->VirtualAddress +
642 sizeof(EFI_TE_IMAGE_HEADER) -
643 Hdr.Te->StrippedSize
644 );
645 }
646
647 if ((CHAR8 *) RelocEnd < (CHAR8 *) ((UINTN) ImageContext->ImageAddress) ||
648 (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress +
649 (UINTN)ImageContext->ImageSize)) {
650 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
651 return RETURN_LOAD_ERROR;
652 }
653
654 //
655 // Run this relocation record
656 //
657 while (Reloc < RelocEnd) {
658
659 Fixup = FixupBase + (*Reloc & 0xFFF);
660 switch ((*Reloc) >> 12) {
661 case EFI_IMAGE_REL_BASED_ABSOLUTE:
662 break;
663
664 case EFI_IMAGE_REL_BASED_HIGH:
665 F16 = (UINT16 *) Fixup;
666 *F16 = (UINT16) ((*F16 << 16) + (UINT16) Adjust);
667 if (FixupData != NULL) {
668 *(UINT16 *) FixupData = *F16;
669 FixupData = FixupData + sizeof (UINT16);
670 }
671 break;
672
673 case EFI_IMAGE_REL_BASED_LOW:
674 F16 = (UINT16 *) Fixup;
675 *F16 = (UINT16) (*F16 + (UINT16) Adjust);
676 if (FixupData != NULL) {
677 *(UINT16 *) FixupData = *F16;
678 FixupData = FixupData + sizeof (UINT16);
679 }
680 break;
681
682 case EFI_IMAGE_REL_BASED_HIGHLOW:
683 F32 = (UINT32 *) Fixup;
684 *F32 = *F32 + (UINT32) Adjust;
685 if (FixupData != NULL) {
686 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));
687 *(UINT32 *)FixupData = *F32;
688 FixupData = FixupData + sizeof (UINT32);
689 }
690 break;
691
692 case EFI_IMAGE_REL_BASED_DIR64:
693 F64 = (UINT64 *) Fixup;
694 *F64 = *F64 + (UINT64) Adjust;
695 if (FixupData != NULL) {
696 FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));
697 *(UINT64 *)(FixupData) = *F64;
698 FixupData = FixupData + sizeof(UINT64);
699 }
700 break;
701
702 default:
703 //
704 // The common code does not handle some of the stranger IPF relocations
705 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
706 // on IPF and is a No-Op on other archtiectures.
707 //
708 Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);
709 if (RETURN_ERROR (Status)) {
710 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
711 return Status;
712 }
713 }
714
715 //
716 // Next relocation record
717 //
718 Reloc += 1;
719 }
720
721 //
722 // Next reloc block
723 //
724 RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
725 }
726
727 return RETURN_SUCCESS;
728 }
729
730 /**
731 Loads a PE/COFF image into memory.
732
733 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
734 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
735 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
736 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
737 If ImageContext is NULL, then ASSERT().
738
739 @param ImageContext Pointer to the image context structure that describes the PE/COFF
740 image that is being loaded.
741
742 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
743 the ImageAddress and ImageSize fields of ImageContext.
744 Extended status information is in the ImageError field of ImageContext.
745 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
746 Extended status information is in the ImageError field of ImageContext.
747 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
748 Extended status information is in the ImageError field of ImageContext.
749 @retval RETURN_INVALID_PARAMETER The image address is invalid.
750 Extended status information is in the ImageError field of ImageContext.
751
752 **/
753 RETURN_STATUS
754 EFIAPI
755 PeCoffLoaderLoadImage (
756 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
757 )
758 {
759 RETURN_STATUS Status;
760 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
761 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext;
762 EFI_IMAGE_SECTION_HEADER *FirstSection;
763 EFI_IMAGE_SECTION_HEADER *Section;
764 UINTN NumberOfSections;
765 UINTN Index;
766 CHAR8 *Base;
767 CHAR8 *End;
768 CHAR8 *MaxEnd;
769 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
770 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
771 UINTN Size;
772 UINT32 TempDebugEntryRva;
773 UINT32 NumberOfRvaAndSizes;
774
775 ASSERT (ImageContext != NULL);
776
777 //
778 // Assume success
779 //
780 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
781
782 //
783 // Copy the provided context info into our local version, get what we
784 // can from the original image, and then use that to make sure everything
785 // is legit.
786 //
787 CopyMem (&CheckContext, ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
788
789 Status = PeCoffLoaderGetImageInfo (&CheckContext);
790 if (RETURN_ERROR (Status)) {
791 return Status;
792 }
793
794 //
795 // Make sure there is enough allocated space for the image being loaded
796 //
797 if (ImageContext->ImageSize < CheckContext.ImageSize) {
798 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;
799 return RETURN_BUFFER_TOO_SMALL;
800 }
801 if (ImageContext->ImageAddress == 0) {
802 //
803 // Image cannot be loaded into 0 address.
804 //
805 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
806 return RETURN_INVALID_PARAMETER;
807 }
808 //
809 // If there's no relocations, then make sure it's not a runtime driver,
810 // and that it's being loaded at the linked address.
811 //
812 if (CheckContext.RelocationsStripped) {
813 //
814 // If the image does not contain relocations and it is a runtime driver
815 // then return an error.
816 //
817 if (CheckContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
818 ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
819 return RETURN_LOAD_ERROR;
820 }
821 //
822 // If the image does not contain relocations, and the requested load address
823 // is not the linked address, then return an error.
824 //
825 if (CheckContext.ImageAddress != ImageContext->ImageAddress) {
826 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
827 return RETURN_INVALID_PARAMETER;
828 }
829 }
830 //
831 // Make sure the allocated space has the proper section alignment
832 //
833 if (!(ImageContext->IsTeImage)) {
834 if ((ImageContext->ImageAddress & (CheckContext.SectionAlignment - 1)) != 0) {
835 ImageContext->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;
836 return RETURN_INVALID_PARAMETER;
837 }
838 }
839 //
840 // Read the entire PE/COFF or TE header into memory
841 //
842 if (!(ImageContext->IsTeImage)) {
843 Status = ImageContext->ImageRead (
844 ImageContext->Handle,
845 0,
846 &ImageContext->SizeOfHeaders,
847 (VOID *) (UINTN) ImageContext->ImageAddress
848 );
849
850 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);
851
852 FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
853 (UINTN)ImageContext->ImageAddress +
854 ImageContext->PeCoffHeaderOffset +
855 sizeof(UINT32) +
856 sizeof(EFI_IMAGE_FILE_HEADER) +
857 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
858 );
859 NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);
860 } else {
861 Status = ImageContext->ImageRead (
862 ImageContext->Handle,
863 0,
864 &ImageContext->SizeOfHeaders,
865 (void *)(UINTN)ImageContext->ImageAddress
866 );
867
868 Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
869
870 FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
871 (UINTN)ImageContext->ImageAddress +
872 sizeof(EFI_TE_IMAGE_HEADER)
873 );
874 NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections);
875
876 }
877
878 if (RETURN_ERROR (Status)) {
879 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
880 return RETURN_LOAD_ERROR;
881 }
882
883 //
884 // Load each section of the image
885 //
886 Section = FirstSection;
887 for (Index = 0, MaxEnd = NULL; Index < NumberOfSections; Index++) {
888
889 //
890 // Compute sections address
891 //
892 Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress);
893 End = PeCoffLoaderImageAddress (
894 ImageContext,
895 Section->VirtualAddress + Section->Misc.VirtualSize - 1
896 );
897 if (ImageContext->IsTeImage) {
898 Base = (CHAR8 *)((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
899 End = (CHAR8 *)((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
900 }
901
902 if (End > MaxEnd) {
903 MaxEnd = End;
904 }
905 //
906 // If the base start or end address resolved to 0, then fail.
907 //
908 if ((Base == NULL) || (End == NULL)) {
909 ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;
910 return RETURN_LOAD_ERROR;
911 }
912
913 //
914 // Read the section
915 //
916 Size = (UINTN) Section->Misc.VirtualSize;
917 if ((Size == 0) || (Size > Section->SizeOfRawData)) {
918 Size = (UINTN) Section->SizeOfRawData;
919 }
920
921 if (Section->SizeOfRawData) {
922 if (!(ImageContext->IsTeImage)) {
923 Status = ImageContext->ImageRead (
924 ImageContext->Handle,
925 Section->PointerToRawData,
926 &Size,
927 Base
928 );
929 } else {
930 Status = ImageContext->ImageRead (
931 ImageContext->Handle,
932 Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize,
933 &Size,
934 Base
935 );
936 }
937
938 if (RETURN_ERROR (Status)) {
939 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
940 return Status;
941 }
942 }
943
944 //
945 // If raw size is less then virt size, zero fill the remaining
946 //
947
948 if (Size < Section->Misc.VirtualSize) {
949 ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
950 }
951
952 //
953 // Next Section
954 //
955 Section += 1;
956 }
957
958 //
959 // Get image's entry point
960 //
961 if (!(ImageContext->IsTeImage)) {
962 //
963 // Sizes of AddressOfEntryPoint are different so we need to do this safely
964 //
965 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
966 //
967 // Use PE32 offset
968 //
969 ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
970 ImageContext,
971 (UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint
972 );
973 } else {
974 //
975 // Use PE32+ offset
976 //
977 ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
978 ImageContext,
979 (UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint
980 );
981 }
982 } else {
983 ImageContext->EntryPoint = (PHYSICAL_ADDRESS) (
984 (UINTN)ImageContext->ImageAddress +
985 (UINTN)Hdr.Te->AddressOfEntryPoint +
986 (UINTN)sizeof(EFI_TE_IMAGE_HEADER) -
987 (UINTN)Hdr.Te->StrippedSize
988 );
989 }
990
991 //
992 // Determine the size of the fixup data
993 //
994 // Per the PE/COFF spec, you can't assume that a given data directory
995 // is present in the image. You have to check the NumberOfRvaAndSizes in
996 // the optional header to verify a desired directory entry is there.
997 //
998 if (!(ImageContext->IsTeImage)) {
999 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1000 //
1001 // Use PE32 offset
1002 //
1003 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1004 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1005 } else {
1006 //
1007 // Use PE32+ offset
1008 //
1009 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1010 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1011 }
1012
1013 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1014 ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);
1015 } else {
1016 ImageContext->FixupDataSize = 0;
1017 }
1018 } else {
1019 DirectoryEntry = &Hdr.Te->DataDirectory[0];
1020 ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);
1021 }
1022 //
1023 // Consumer must allocate a buffer for the relocation fixup log.
1024 // Only used for runtime drivers.
1025 //
1026 ImageContext->FixupData = NULL;
1027
1028 //
1029 // Load the Codeview info if present
1030 //
1031 if (ImageContext->DebugDirectoryEntryRva != 0) {
1032 if (!(ImageContext->IsTeImage)) {
1033 DebugEntry = PeCoffLoaderImageAddress (
1034 ImageContext,
1035 ImageContext->DebugDirectoryEntryRva
1036 );
1037 } else {
1038 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)(UINTN)(
1039 ImageContext->ImageAddress +
1040 ImageContext->DebugDirectoryEntryRva +
1041 sizeof(EFI_TE_IMAGE_HEADER) -
1042 Hdr.Te->StrippedSize
1043 );
1044 }
1045
1046 if (DebugEntry != NULL) {
1047 TempDebugEntryRva = DebugEntry->RVA;
1048 if (DebugEntry->RVA == 0 && DebugEntry->FileOffset != 0) {
1049 Section--;
1050 if ((UINTN)Section->SizeOfRawData < Section->Misc.VirtualSize) {
1051 TempDebugEntryRva = Section->VirtualAddress + Section->Misc.VirtualSize;
1052 } else {
1053 TempDebugEntryRva = Section->VirtualAddress + Section->SizeOfRawData;
1054 }
1055 }
1056
1057 if (TempDebugEntryRva != 0) {
1058 if (!(ImageContext->IsTeImage)) {
1059 ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva);
1060 } else {
1061 ImageContext->CodeView = (VOID *)(
1062 (UINTN)ImageContext->ImageAddress +
1063 (UINTN)TempDebugEntryRva +
1064 (UINTN)sizeof (EFI_TE_IMAGE_HEADER) -
1065 (UINTN) Hdr.Te->StrippedSize
1066 );
1067 }
1068
1069 if (ImageContext->CodeView == NULL) {
1070 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1071 return RETURN_LOAD_ERROR;
1072 }
1073
1074 if (DebugEntry->RVA == 0) {
1075 Size = DebugEntry->SizeOfData;
1076 if (!(ImageContext->IsTeImage)) {
1077 Status = ImageContext->ImageRead (
1078 ImageContext->Handle,
1079 DebugEntry->FileOffset,
1080 &Size,
1081 ImageContext->CodeView
1082 );
1083 } else {
1084 Status = ImageContext->ImageRead (
1085 ImageContext->Handle,
1086 DebugEntry->FileOffset + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize,
1087 &Size,
1088 ImageContext->CodeView
1089 );
1090 //
1091 // Should we apply fix up to this field according to the size difference between PE and TE?
1092 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1093 // in original PE image.
1094 //
1095 }
1096
1097 if (RETURN_ERROR (Status)) {
1098 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1099 return RETURN_LOAD_ERROR;
1100 }
1101
1102 DebugEntry->RVA = TempDebugEntryRva;
1103 }
1104
1105 switch (*(UINT32 *) ImageContext->CodeView) {
1106 case CODEVIEW_SIGNATURE_NB10:
1107 ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);
1108 break;
1109
1110 case CODEVIEW_SIGNATURE_RSDS:
1111 ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
1112 break;
1113
1114 default:
1115 break;
1116 }
1117 }
1118 }
1119 }
1120
1121 return Status;
1122 }
1123
1124
1125 /**
1126 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1127 runtime.
1128
1129 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1130 the fixups with a virtual mapping.
1131
1132
1133 @param ImageBase Base address of relocated image
1134 @param VirtImageBase Virtual mapping for ImageBase
1135 @param ImageSize Size of the image to relocate
1136 @param RelocationData Location to place results of read
1137
1138 **/
1139 VOID
1140 EFIAPI
1141 PeCoffLoaderRelocateImageForRuntime (
1142 IN PHYSICAL_ADDRESS ImageBase,
1143 IN PHYSICAL_ADDRESS VirtImageBase,
1144 IN UINTN ImageSize,
1145 IN VOID *RelocationData
1146 )
1147 {
1148 CHAR8 *OldBase;
1149 CHAR8 *NewBase;
1150 EFI_IMAGE_DOS_HEADER *DosHdr;
1151 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1152 UINT32 NumberOfRvaAndSizes;
1153 EFI_IMAGE_DATA_DIRECTORY *DataDirectory;
1154 EFI_IMAGE_DATA_DIRECTORY *RelocDir;
1155 EFI_IMAGE_BASE_RELOCATION *RelocBase;
1156 EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
1157 UINT16 *Reloc;
1158 UINT16 *RelocEnd;
1159 CHAR8 *Fixup;
1160 CHAR8 *FixupBase;
1161 UINT16 *F16;
1162 UINT32 *F32;
1163 UINT64 *F64;
1164 CHAR8 *FixupData;
1165 UINTN Adjust;
1166 RETURN_STATUS Status;
1167
1168 OldBase = (CHAR8 *)((UINTN)ImageBase);
1169 NewBase = (CHAR8 *)((UINTN)VirtImageBase);
1170 Adjust = (UINTN) NewBase - (UINTN) OldBase;
1171
1172 //
1173 // Find the image's relocate dir info
1174 //
1175 DosHdr = (EFI_IMAGE_DOS_HEADER *)OldBase;
1176 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
1177 //
1178 // Valid DOS header so get address of PE header
1179 //
1180 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(((CHAR8 *)DosHdr) + DosHdr->e_lfanew);
1181 } else {
1182 //
1183 // No Dos header so assume image starts with PE header.
1184 //
1185 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)OldBase;
1186 }
1187
1188 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1189 //
1190 // Not a valid PE image so Exit
1191 //
1192 return ;
1193 }
1194
1195 //
1196 // Get some data from the PE type dependent data
1197 //
1198 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1199 //
1200 // Use PE32 offset
1201 //
1202 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1203 DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]);
1204 } else {
1205 //
1206 // Use PE32+ offset
1207 //
1208 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1209 DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);
1210 }
1211
1212 //
1213 // Find the relocation block
1214 //
1215 // Per the PE/COFF spec, you can't assume that a given data directory
1216 // is present in the image. You have to check the NumberOfRvaAndSizes in
1217 // the optional header to verify a desired directory entry is there.
1218 //
1219 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1220 RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;
1221 RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);
1222 RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);
1223 } else {
1224 //
1225 // Cannot find relocations, cannot continue
1226 //
1227 ASSERT (FALSE);
1228 return ;
1229 }
1230
1231 ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);
1232
1233 //
1234 // Run the whole relocation block. And re-fixup data that has not been
1235 // modified. The FixupData is used to see if the image has been modified
1236 // since it was relocated. This is so data sections that have been updated
1237 // by code will not be fixed up, since that would set them back to
1238 // defaults.
1239 //
1240 FixupData = RelocationData;
1241 while (RelocBase < RelocBaseEnd) {
1242
1243 Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));
1244 RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);
1245 FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;
1246
1247 //
1248 // Run this relocation record
1249 //
1250 while (Reloc < RelocEnd) {
1251
1252 Fixup = FixupBase + (*Reloc & 0xFFF);
1253 switch ((*Reloc) >> 12) {
1254
1255 case EFI_IMAGE_REL_BASED_ABSOLUTE:
1256 break;
1257
1258 case EFI_IMAGE_REL_BASED_HIGH:
1259 F16 = (UINT16 *) Fixup;
1260 if (*(UINT16 *) FixupData == *F16) {
1261 *F16 = (UINT16) (*F16 + ((UINT16)(Adjust >> 16)));
1262 }
1263
1264 FixupData = FixupData + sizeof (UINT16);
1265 break;
1266
1267 case EFI_IMAGE_REL_BASED_LOW:
1268 F16 = (UINT16 *) Fixup;
1269 if (*(UINT16 *) FixupData == *F16) {
1270 *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff));
1271 }
1272
1273 FixupData = FixupData + sizeof (UINT16);
1274 break;
1275
1276 case EFI_IMAGE_REL_BASED_HIGHLOW:
1277 F32 = (UINT32 *) Fixup;
1278 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));
1279 if (*(UINT32 *) FixupData == *F32) {
1280 *F32 = *F32 + (UINT32) Adjust;
1281 }
1282
1283 FixupData = FixupData + sizeof (UINT32);
1284 break;
1285
1286 case EFI_IMAGE_REL_BASED_DIR64:
1287 F64 = (UINT64 *)Fixup;
1288 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));
1289 if (*(UINT64 *) FixupData == *F64) {
1290 *F64 = *F64 + (UINT64)Adjust;
1291 }
1292
1293 FixupData = FixupData + sizeof (UINT64);
1294 break;
1295
1296 case EFI_IMAGE_REL_BASED_HIGHADJ:
1297 //
1298 // Not implemented, but not used in EFI 1.0
1299 //
1300 ASSERT (FALSE);
1301 break;
1302
1303 default:
1304 //
1305 // Only Itanium requires ConvertPeImage_Ex
1306 //
1307 Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);
1308 if (RETURN_ERROR (Status)) {
1309 return ;
1310 }
1311 }
1312 //
1313 // Next relocation record
1314 //
1315 Reloc += 1;
1316 }
1317 //
1318 // next reloc block
1319 //
1320 RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
1321 }
1322 }
1323
1324
1325 /**
1326 ImageRead function that operates on a memory buffer whos base is passed into
1327 FileHandle.
1328
1329 @param FileHandle Ponter to baes of the input stream
1330 @param FileOffset Offset to the start of the buffer
1331 @param ReadSize Number of bytes to copy into the buffer
1332 @param Buffer Location to place results of read
1333
1334 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1335 the buffer.
1336 **/
1337 RETURN_STATUS
1338 EFIAPI
1339 PeCoffLoaderImageReadFromMemory (
1340 IN VOID *FileHandle,
1341 IN UINTN FileOffset,
1342 IN OUT UINTN *ReadSize,
1343 OUT VOID *Buffer
1344 )
1345 {
1346 CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);
1347 return RETURN_SUCCESS;
1348 }
1349