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