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