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