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