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