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