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