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