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