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