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