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