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