]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
1e8f573009540d01686a302ec029c560e81ab357
[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.Pe32->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 return (CHAR8 *)((UINTN) ImageContext->ImageAddress + Address);
492 }
493
494 /**
495 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
496
497 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
498 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
499 of ImageContext as the relocation base address. The caller must allocate the relocation
500 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
501 If ImageContext is NULL, then ASSERT().
502
503 @param ImageContext Pointer to the image context structure that describes the PE/COFF
504 image that is being relocated.
505
506 @retval RETURN_SUCCESS The PE/COFF image was relocated.
507 Extended status information is in the ImageError field of ImageContext.
508 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
509 Extended status information is in the ImageError field of ImageContext.
510 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
511 Extended status information is in the ImageError field of ImageContext.
512
513 **/
514 RETURN_STATUS
515 EFIAPI
516 PeCoffLoaderRelocateImage (
517 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
518 )
519 {
520 RETURN_STATUS Status;
521 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
522 EFI_IMAGE_DATA_DIRECTORY *RelocDir;
523 UINT64 Adjust;
524 EFI_IMAGE_BASE_RELOCATION *RelocBase;
525 EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
526 UINT16 *Reloc;
527 UINT16 *RelocEnd;
528 CHAR8 *Fixup;
529 CHAR8 *FixupBase;
530 UINT16 *F16;
531 UINT32 *F32;
532 UINT64 *F64;
533 CHAR8 *FixupData;
534 PHYSICAL_ADDRESS BaseAddress;
535 UINT32 NumberOfRvaAndSizes;
536
537 ASSERT (ImageContext != NULL);
538
539 //
540 // Assume success
541 //
542 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
543
544 //
545 // If there are no relocation entries, then we are done
546 //
547 if (ImageContext->RelocationsStripped) {
548 return RETURN_SUCCESS;
549 }
550
551 //
552 // If the destination address is not 0, use that rather than the
553 // image address as the relocation target.
554 //
555 if (ImageContext->DestinationAddress != 0) {
556 BaseAddress = ImageContext->DestinationAddress;
557 } else {
558 BaseAddress = ImageContext->ImageAddress;
559 }
560
561 if (!(ImageContext->IsTeImage)) {
562 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);
563 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
564 //
565 // Use PE32 offset
566 //
567 Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase;
568 Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;
569
570 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
571 RelocDir = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
572 } else {
573 //
574 // Use PE32+ offset
575 //
576 Adjust = (UINT64) BaseAddress - Hdr.Pe32Plus->OptionalHeader.ImageBase;
577 Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;
578
579 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
580 RelocDir = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
581 }
582
583 //
584 // Find the relocation block
585 // Per the PE/COFF spec, you can't assume that a given data directory
586 // is present in the image. You have to check the NumberOfRvaAndSizes in
587 // the optional header to verify a desired directory entry is there.
588 //
589
590 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
591 RelocBase = PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress);
592 RelocBaseEnd = PeCoffLoaderImageAddress (
593 ImageContext,
594 RelocDir->VirtualAddress + RelocDir->Size - 1
595 );
596 } else {
597 //
598 // Set base and end to bypass processing below.
599 //
600 RelocBase = RelocBaseEnd = 0;
601 }
602 } else {
603 Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
604 Adjust = (UINT64) (BaseAddress - Hdr.Te->ImageBase);
605 Hdr.Te->ImageBase = (UINT64) (BaseAddress);
606
607 //
608 // Find the relocation block
609 //
610 RelocDir = &Hdr.Te->DataDirectory[0];
611 RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(
612 ImageContext->ImageAddress +
613 RelocDir->VirtualAddress +
614 sizeof(EFI_TE_IMAGE_HEADER) -
615 Hdr.Te->StrippedSize
616 );
617 RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) ((UINTN) RelocBase + (UINTN) RelocDir->Size - 1);
618 }
619
620 //
621 // Run the relocation information and apply the fixups
622 //
623 FixupData = ImageContext->FixupData;
624 while (RelocBase < RelocBaseEnd) {
625
626 Reloc = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));
627 RelocEnd = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);
628 if (!(ImageContext->IsTeImage)) {
629 FixupBase = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress);
630 } else {
631 FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +
632 RelocBase->VirtualAddress +
633 sizeof(EFI_TE_IMAGE_HEADER) -
634 Hdr.Te->StrippedSize
635 );
636 }
637
638 if ((CHAR8 *) RelocEnd < (CHAR8 *) ((UINTN) ImageContext->ImageAddress) ||
639 (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress +
640 (UINTN)ImageContext->ImageSize)) {
641 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
642 return RETURN_LOAD_ERROR;
643 }
644
645 //
646 // Run this relocation record
647 //
648 while (Reloc < RelocEnd) {
649
650 Fixup = FixupBase + (*Reloc & 0xFFF);
651 switch ((*Reloc) >> 12) {
652 case EFI_IMAGE_REL_BASED_ABSOLUTE:
653 break;
654
655 case EFI_IMAGE_REL_BASED_HIGH:
656 F16 = (UINT16 *) Fixup;
657 *F16 = (UINT16) ((*F16 << 16) + (UINT16) Adjust);
658 if (FixupData != NULL) {
659 *(UINT16 *) FixupData = *F16;
660 FixupData = FixupData + sizeof (UINT16);
661 }
662 break;
663
664 case EFI_IMAGE_REL_BASED_LOW:
665 F16 = (UINT16 *) Fixup;
666 *F16 = (UINT16) (*F16 + (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_HIGHLOW:
674 F32 = (UINT32 *) Fixup;
675 *F32 = *F32 + (UINT32) Adjust;
676 if (FixupData != NULL) {
677 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));
678 *(UINT32 *)FixupData = *F32;
679 FixupData = FixupData + sizeof (UINT32);
680 }
681 break;
682
683 case EFI_IMAGE_REL_BASED_DIR64:
684 F64 = (UINT64 *) Fixup;
685 *F64 = *F64 + (UINT64) Adjust;
686 if (FixupData != NULL) {
687 FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));
688 *(UINT64 *)(FixupData) = *F64;
689 FixupData = FixupData + sizeof(UINT64);
690 }
691 break;
692
693 default:
694 //
695 // The common code does not handle some of the stranger IPF relocations
696 // PeCoffLoaderRelocateImageEx () addes support for these complex fixups
697 // on IPF and is a No-Op on other archtiectures.
698 //
699 Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);
700 if (RETURN_ERROR (Status)) {
701 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
702 return Status;
703 }
704 }
705
706 //
707 // Next relocation record
708 //
709 Reloc += 1;
710 }
711
712 //
713 // Next reloc block
714 //
715 RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
716 }
717
718 return RETURN_SUCCESS;
719 }
720
721 /**
722 Loads a PE/COFF image into memory.
723
724 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
725 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
726 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
727 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
728 If ImageContext is NULL, then ASSERT().
729
730 @param ImageContext Pointer to the image context structure that describes the PE/COFF
731 image that is being loaded.
732
733 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
734 the ImageAddress and ImageSize fields of ImageContext.
735 Extended status information is in the ImageError field of ImageContext.
736 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
737 Extended status information is in the ImageError field of ImageContext.
738 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
739 Extended status information is in the ImageError field of ImageContext.
740 @retval RETURN_INVALID_PARAMETER The image address is invalid.
741 Extended status information is in the ImageError field of ImageContext.
742
743 **/
744 RETURN_STATUS
745 EFIAPI
746 PeCoffLoaderLoadImage (
747 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
748 )
749 {
750 RETURN_STATUS Status;
751 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
752 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext;
753 EFI_IMAGE_SECTION_HEADER *FirstSection;
754 EFI_IMAGE_SECTION_HEADER *Section;
755 UINTN NumberOfSections;
756 UINTN Index;
757 CHAR8 *Base;
758 CHAR8 *End;
759 CHAR8 *MaxEnd;
760 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
761 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
762 UINTN Size;
763 UINT32 TempDebugEntryRva;
764 UINT32 NumberOfRvaAndSizes;
765
766 ASSERT (ImageContext != NULL);
767
768 //
769 // Assume success
770 //
771 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
772
773 //
774 // Copy the provided context info into our local version, get what we
775 // can from the original image, and then use that to make sure everything
776 // is legit.
777 //
778 CopyMem (&CheckContext, ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
779
780 Status = PeCoffLoaderGetImageInfo (&CheckContext);
781 if (RETURN_ERROR (Status)) {
782 return Status;
783 }
784
785 //
786 // Make sure there is enough allocated space for the image being loaded
787 //
788 if (ImageContext->ImageSize < CheckContext.ImageSize) {
789 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;
790 return RETURN_BUFFER_TOO_SMALL;
791 }
792 if (ImageContext->ImageAddress == 0) {
793 //
794 // Image cannot be loaded into 0 address.
795 //
796 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
797 return RETURN_INVALID_PARAMETER;
798 }
799 //
800 // If there's no relocations, then make sure it's not a runtime driver,
801 // and that it's being loaded at the linked address.
802 //
803 if (CheckContext.RelocationsStripped) {
804 //
805 // If the image does not contain relocations and it is a runtime driver
806 // then return an error.
807 //
808 if (CheckContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
809 ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
810 return RETURN_LOAD_ERROR;
811 }
812 //
813 // If the image does not contain relocations, and the requested load address
814 // is not the linked address, then return an error.
815 //
816 if (CheckContext.ImageAddress != ImageContext->ImageAddress) {
817 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
818 return RETURN_INVALID_PARAMETER;
819 }
820 }
821 //
822 // Make sure the allocated space has the proper section alignment
823 //
824 if (!(ImageContext->IsTeImage)) {
825 if ((ImageContext->ImageAddress & (CheckContext.SectionAlignment - 1)) != 0) {
826 ImageContext->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;
827 return RETURN_INVALID_PARAMETER;
828 }
829 }
830 //
831 // Read the entire PE/COFF or TE header into memory
832 //
833 if (!(ImageContext->IsTeImage)) {
834 Status = ImageContext->ImageRead (
835 ImageContext->Handle,
836 0,
837 &ImageContext->SizeOfHeaders,
838 (VOID *) (UINTN) ImageContext->ImageAddress
839 );
840
841 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);
842
843 FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
844 (UINTN)ImageContext->ImageAddress +
845 ImageContext->PeCoffHeaderOffset +
846 sizeof(UINT32) +
847 sizeof(EFI_IMAGE_FILE_HEADER) +
848 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
849 );
850 NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);
851 } else {
852 Status = ImageContext->ImageRead (
853 ImageContext->Handle,
854 0,
855 &ImageContext->SizeOfHeaders,
856 (void *)(UINTN)ImageContext->ImageAddress
857 );
858
859 Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
860
861 FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
862 (UINTN)ImageContext->ImageAddress +
863 sizeof(EFI_TE_IMAGE_HEADER)
864 );
865 NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections);
866
867 }
868
869 if (RETURN_ERROR (Status)) {
870 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
871 return RETURN_LOAD_ERROR;
872 }
873
874 //
875 // Load each section of the image
876 //
877 Section = FirstSection;
878 for (Index = 0, MaxEnd = NULL; Index < NumberOfSections; Index++) {
879
880 //
881 // Compute sections address
882 //
883 Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress);
884 End = PeCoffLoaderImageAddress (
885 ImageContext,
886 Section->VirtualAddress + Section->Misc.VirtualSize - 1
887 );
888 if (ImageContext->IsTeImage) {
889 Base = (CHAR8 *)((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
890 End = (CHAR8 *)((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
891 }
892
893 if (End > MaxEnd) {
894 MaxEnd = End;
895 }
896 //
897 // If the base start or end address resolved to 0, then fail.
898 //
899 if ((Base == NULL) || (End == NULL)) {
900 ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;
901 return RETURN_LOAD_ERROR;
902 }
903
904 //
905 // Read the section
906 //
907 Size = (UINTN) Section->Misc.VirtualSize;
908 if ((Size == 0) || (Size > Section->SizeOfRawData)) {
909 Size = (UINTN) Section->SizeOfRawData;
910 }
911
912 if (Section->SizeOfRawData) {
913 if (!(ImageContext->IsTeImage)) {
914 Status = ImageContext->ImageRead (
915 ImageContext->Handle,
916 Section->PointerToRawData,
917 &Size,
918 Base
919 );
920 } else {
921 Status = ImageContext->ImageRead (
922 ImageContext->Handle,
923 Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize,
924 &Size,
925 Base
926 );
927 }
928
929 if (RETURN_ERROR (Status)) {
930 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
931 return Status;
932 }
933 }
934
935 //
936 // If raw size is less then virt size, zero fill the remaining
937 //
938
939 if (Size < Section->Misc.VirtualSize) {
940 ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
941 }
942
943 //
944 // Next Section
945 //
946 Section += 1;
947 }
948
949 //
950 // Get image's entry point
951 //
952 if (!(ImageContext->IsTeImage)) {
953 //
954 // Sizes of AddressOfEntryPoint are different so we need to do this safely
955 //
956 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
957 //
958 // Use PE32 offset
959 //
960 ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
961 ImageContext,
962 (UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint
963 );
964 } else {
965 //
966 // Use PE32+ offset
967 //
968 ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
969 ImageContext,
970 (UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint
971 );
972 }
973 } else {
974 ImageContext->EntryPoint = (PHYSICAL_ADDRESS) (
975 (UINTN)ImageContext->ImageAddress +
976 (UINTN)Hdr.Te->AddressOfEntryPoint +
977 (UINTN)sizeof(EFI_TE_IMAGE_HEADER) -
978 (UINTN)Hdr.Te->StrippedSize
979 );
980 }
981
982 //
983 // Determine the size of the fixup data
984 //
985 // Per the PE/COFF spec, you can't assume that a given data directory
986 // is present in the image. You have to check the NumberOfRvaAndSizes in
987 // the optional header to verify a desired directory entry is there.
988 //
989 if (!(ImageContext->IsTeImage)) {
990 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
991 //
992 // Use PE32 offset
993 //
994 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
995 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
996 } else {
997 //
998 // Use PE32+ offset
999 //
1000 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1001 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1002 }
1003
1004 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1005 ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);
1006 } else {
1007 ImageContext->FixupDataSize = 0;
1008 }
1009 } else {
1010 DirectoryEntry = &Hdr.Te->DataDirectory[0];
1011 ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);
1012 }
1013 //
1014 // Consumer must allocate a buffer for the relocation fixup log.
1015 // Only used for runtime drivers.
1016 //
1017 ImageContext->FixupData = NULL;
1018
1019 //
1020 // Load the Codeview info if present
1021 //
1022 if (ImageContext->DebugDirectoryEntryRva != 0) {
1023 if (!(ImageContext->IsTeImage)) {
1024 DebugEntry = PeCoffLoaderImageAddress (
1025 ImageContext,
1026 ImageContext->DebugDirectoryEntryRva
1027 );
1028 } else {
1029 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)(UINTN)(
1030 ImageContext->ImageAddress +
1031 ImageContext->DebugDirectoryEntryRva +
1032 sizeof(EFI_TE_IMAGE_HEADER) -
1033 Hdr.Te->StrippedSize
1034 );
1035 }
1036
1037 if (DebugEntry != NULL) {
1038 TempDebugEntryRva = DebugEntry->RVA;
1039 if (DebugEntry->RVA == 0 && DebugEntry->FileOffset != 0) {
1040 Section--;
1041 if ((UINTN)Section->SizeOfRawData < Section->Misc.VirtualSize) {
1042 TempDebugEntryRva = Section->VirtualAddress + Section->Misc.VirtualSize;
1043 } else {
1044 TempDebugEntryRva = Section->VirtualAddress + Section->SizeOfRawData;
1045 }
1046 }
1047
1048 if (TempDebugEntryRva != 0) {
1049 if (!(ImageContext->IsTeImage)) {
1050 ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva);
1051 } else {
1052 ImageContext->CodeView = (VOID *)(
1053 (UINTN)ImageContext->ImageAddress +
1054 (UINTN)TempDebugEntryRva +
1055 (UINTN)sizeof (EFI_TE_IMAGE_HEADER) -
1056 (UINTN) Hdr.Te->StrippedSize
1057 );
1058 }
1059
1060 if (ImageContext->CodeView == NULL) {
1061 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1062 return RETURN_LOAD_ERROR;
1063 }
1064
1065 if (DebugEntry->RVA == 0) {
1066 Size = DebugEntry->SizeOfData;
1067 if (!(ImageContext->IsTeImage)) {
1068 Status = ImageContext->ImageRead (
1069 ImageContext->Handle,
1070 DebugEntry->FileOffset,
1071 &Size,
1072 ImageContext->CodeView
1073 );
1074 } else {
1075 Status = ImageContext->ImageRead (
1076 ImageContext->Handle,
1077 DebugEntry->FileOffset + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize,
1078 &Size,
1079 ImageContext->CodeView
1080 );
1081 //
1082 // Should we apply fix up to this field according to the size difference between PE and TE?
1083 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1084 // in original PE image.
1085 //
1086 }
1087
1088 if (RETURN_ERROR (Status)) {
1089 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1090 return RETURN_LOAD_ERROR;
1091 }
1092
1093 DebugEntry->RVA = TempDebugEntryRva;
1094 }
1095
1096 switch (*(UINT32 *) ImageContext->CodeView) {
1097 case CODEVIEW_SIGNATURE_NB10:
1098 ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);
1099 break;
1100
1101 case CODEVIEW_SIGNATURE_RSDS:
1102 ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
1103 break;
1104
1105 default:
1106 break;
1107 }
1108 }
1109 }
1110 }
1111
1112 return Status;
1113 }
1114
1115
1116 /**
1117 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1118 runtime.
1119
1120 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1121 the fixups with a virtual mapping.
1122
1123
1124 @param ImageBase Base address of relocated image
1125 @param VirtImageBase Virtual mapping for ImageBase
1126 @param ImageSize Size of the image to relocate
1127 @param RelocationData Location to place results of read
1128
1129 **/
1130 VOID
1131 EFIAPI
1132 PeCoffLoaderRelocateImageForRuntime (
1133 IN PHYSICAL_ADDRESS ImageBase,
1134 IN PHYSICAL_ADDRESS VirtImageBase,
1135 IN UINTN ImageSize,
1136 IN VOID *RelocationData
1137 )
1138 {
1139 CHAR8 *OldBase;
1140 CHAR8 *NewBase;
1141 EFI_IMAGE_DOS_HEADER *DosHdr;
1142 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1143 UINT32 NumberOfRvaAndSizes;
1144 EFI_IMAGE_DATA_DIRECTORY *DataDirectory;
1145 EFI_IMAGE_DATA_DIRECTORY *RelocDir;
1146 EFI_IMAGE_BASE_RELOCATION *RelocBase;
1147 EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
1148 UINT16 *Reloc;
1149 UINT16 *RelocEnd;
1150 CHAR8 *Fixup;
1151 CHAR8 *FixupBase;
1152 UINT16 *F16;
1153 UINT32 *F32;
1154 UINT64 *F64;
1155 CHAR8 *FixupData;
1156 UINTN Adjust;
1157 RETURN_STATUS Status;
1158
1159 OldBase = (CHAR8 *)((UINTN)ImageBase);
1160 NewBase = (CHAR8 *)((UINTN)VirtImageBase);
1161 Adjust = (UINTN) NewBase - (UINTN) OldBase;
1162
1163 //
1164 // Find the image's relocate dir info
1165 //
1166 DosHdr = (EFI_IMAGE_DOS_HEADER *)OldBase;
1167 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
1168 //
1169 // Valid DOS header so get address of PE header
1170 //
1171 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(((CHAR8 *)DosHdr) + DosHdr->e_lfanew);
1172 } else {
1173 //
1174 // No Dos header so assume image starts with PE header.
1175 //
1176 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)OldBase;
1177 }
1178
1179 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1180 //
1181 // Not a valid PE image so Exit
1182 //
1183 return ;
1184 }
1185
1186 //
1187 // Get some data from the PE type dependent data
1188 //
1189 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1190 //
1191 // Use PE32 offset
1192 //
1193 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1194 DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
1195 } else {
1196 //
1197 // Use PE32+ offset
1198 //
1199 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1200 DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
1201 }
1202
1203 //
1204 // Find the relocation block
1205 //
1206 // Per the PE/COFF spec, you can't assume that a given data directory
1207 // is present in the image. You have to check the NumberOfRvaAndSizes in
1208 // the optional header to verify a desired directory entry is there.
1209 //
1210 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1211 RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;
1212 RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);
1213 RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);
1214 } else {
1215 //
1216 // Cannot find relocations, cannot continue
1217 //
1218 ASSERT (FALSE);
1219 return ;
1220 }
1221
1222 ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);
1223
1224 //
1225 // Run the whole relocation block. And re-fixup data that has not been
1226 // modified. The FixupData is used to see if the image has been modified
1227 // since it was relocated. This is so data sections that have been updated
1228 // by code will not be fixed up, since that would set them back to
1229 // defaults.
1230 //
1231 FixupData = RelocationData;
1232 while (RelocBase < RelocBaseEnd) {
1233
1234 Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));
1235 RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);
1236 FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;
1237
1238 //
1239 // Run this relocation record
1240 //
1241 while (Reloc < RelocEnd) {
1242
1243 Fixup = FixupBase + (*Reloc & 0xFFF);
1244 switch ((*Reloc) >> 12) {
1245
1246 case EFI_IMAGE_REL_BASED_ABSOLUTE:
1247 break;
1248
1249 case EFI_IMAGE_REL_BASED_HIGH:
1250 F16 = (UINT16 *) Fixup;
1251 if (*(UINT16 *) FixupData == *F16) {
1252 *F16 = (UINT16) ((*F16 << 16) + ((UINT16) Adjust & 0xffff));
1253 }
1254
1255 FixupData = FixupData + sizeof (UINT16);
1256 break;
1257
1258 case EFI_IMAGE_REL_BASED_LOW:
1259 F16 = (UINT16 *) Fixup;
1260 if (*(UINT16 *) FixupData == *F16) {
1261 *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff));
1262 }
1263
1264 FixupData = FixupData + sizeof (UINT16);
1265 break;
1266
1267 case EFI_IMAGE_REL_BASED_HIGHLOW:
1268 F32 = (UINT32 *) Fixup;
1269 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));
1270 if (*(UINT32 *) FixupData == *F32) {
1271 *F32 = *F32 + (UINT32) Adjust;
1272 }
1273
1274 FixupData = FixupData + sizeof (UINT32);
1275 break;
1276
1277 case EFI_IMAGE_REL_BASED_DIR64:
1278 F64 = (UINT64 *)Fixup;
1279 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));
1280 if (*(UINT32 *) FixupData == *F64) {
1281 *F64 = *F64 + (UINT64)Adjust;
1282 }
1283 break;
1284
1285 case EFI_IMAGE_REL_BASED_HIGHADJ:
1286 //
1287 // Not implemented, but not used in EFI 1.0
1288 //
1289 ASSERT (FALSE);
1290 break;
1291
1292 default:
1293 //
1294 // Only Itanium requires ConvertPeImage_Ex
1295 //
1296 Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);
1297 if (RETURN_ERROR (Status)) {
1298 return ;
1299 }
1300 }
1301 //
1302 // Next relocation record
1303 //
1304 Reloc += 1;
1305 }
1306 //
1307 // next reloc block
1308 //
1309 RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
1310 }
1311 }
1312
1313
1314 /**
1315 ImageRead function that operates on a memory buffer whos base is passed into
1316 FileHandle.
1317
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
1322
1323 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1324 the buffer.
1325 **/
1326 RETURN_STATUS
1327 EFIAPI
1328 PeCoffLoaderImageReadFromMemory (
1329 IN VOID *FileHandle,
1330 IN UINTN FileOffset,
1331 IN OUT UINTN *ReadSize,
1332 OUT VOID *Buffer
1333 )
1334 {
1335 CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);
1336 return RETURN_SUCCESS;
1337 }
1338