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