]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
Make MDE package pass intel IPF compiler with /W4 /WX switched on.
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / BasePeCoff.c
1 /** @file
2 Tiano PE/COFF loader.
3
4 This PE/COFF loader supports loading any PE32 or PE32+ image type, but
5 only supports relocating IA32, X64, IPF, and EBC images.
6
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 Module Name: PeCoffLoader.c
17
18 **/
19
20 #include "BasePeCoffLibInternals.h"
21
22 /**
23 Retrieves the magic value from the PE/COFF header.
24
25 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
26
27 @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
28 @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
29
30 **/
31 UINT16
32 PeCoffLoaderGetPeHeaderMagicValue (
33 IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
34 )
35 {
36 //
37 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
38 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
39 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
40 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
41 //
42 if (Hdr.Pe32->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
43 return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
44 }
45 //
46 // Return the magic value from the PC/COFF Optional Header
47 //
48 return Hdr.Pe32->OptionalHeader.Magic;
49 }
50
51
52 /**
53 Retrieves the PE or TE Header from a PE/COFF or TE image.
54
55 @param ImageContext The context of the image being loaded.
56 @param Hdr The buffer in which to return the PE32, PE32+, or TE header.
57
58 @retval RETURN_SUCCESS The PE or TE Header is read.
59 @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.
60
61 **/
62 RETURN_STATUS
63 PeCoffLoaderGetPeHeader (
64 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
65 OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
66 )
67 {
68 RETURN_STATUS Status;
69 EFI_IMAGE_DOS_HEADER DosHdr;
70 UINTN Size;
71 UINT16 Magic;
72
73 //
74 // Read the DOS image header to check for it's existance
75 //
76 Size = sizeof (EFI_IMAGE_DOS_HEADER);
77 Status = ImageContext->ImageRead (
78 ImageContext->Handle,
79 0,
80 &Size,
81 &DosHdr
82 );
83 if (RETURN_ERROR (Status)) {
84 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
85 return Status;
86 }
87
88 ImageContext->PeCoffHeaderOffset = 0;
89 if (DosHdr.e_magic == EFI_IMAGE_DOS_SIGNATURE) {
90 //
91 // DOS image header is present, so read the PE header after the DOS image
92 // header
93 //
94 ImageContext->PeCoffHeaderOffset = DosHdr.e_lfanew;
95 }
96
97 //
98 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much
99 // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic
100 // determins if this is a PE32 or PE32+ image. The magic is in the same
101 // location in both images.
102 //
103 Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
104 Status = ImageContext->ImageRead (
105 ImageContext->Handle,
106 ImageContext->PeCoffHeaderOffset,
107 &Size,
108 Hdr.Pe32
109 );
110 if (RETURN_ERROR (Status)) {
111 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
112 return Status;
113 }
114
115 //
116 // Use Signature to figure out if we understand the image format
117 //
118 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
119 ImageContext->IsTeImage = TRUE;
120 ImageContext->Machine = Hdr.Te->Machine;
121 ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem);
122 ImageContext->ImageSize = 0;
123 ImageContext->SectionAlignment = 4096;
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, ImageAddress, ImageSize, DestinationAddress, CodeView,
176 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
177 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
178 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
179 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
180 computing the fields of ImageContext, then the error status is returned in the ImageError field of
181 ImageContext.
182
183 @param ImageContext Pointer to the image context structure that describes the PE/COFF
184 image that needs to be examined by this function.
185
186 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
187 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
188 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
189
190 **/
191 RETURN_STATUS
192 EFIAPI
193 PeCoffLoaderGetImageInfo (
194 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
195 )
196 {
197 RETURN_STATUS Status;
198 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;
199 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
200 EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry;
201 UINTN Size;
202 UINTN Index;
203 UINTN DebugDirectoryEntryRva;
204 UINTN DebugDirectoryEntryFileOffset;
205 UINTN SectionHeaderOffset;
206 EFI_IMAGE_SECTION_HEADER SectionHeader;
207 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry;
208 UINT32 NumberOfRvaAndSizes;
209 UINT16 Magic;
210
211 if (NULL == ImageContext) {
212 return RETURN_INVALID_PARAMETER;
213 }
214 //
215 // Assume success
216 //
217 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
218
219 Hdr.Union = &HdrData;
220 Status = PeCoffLoaderGetPeHeader (ImageContext, Hdr);
221 if (RETURN_ERROR (Status)) {
222 return Status;
223 }
224
225 Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);
226
227 //
228 // Retrieve the base address of the image
229 //
230 if (!(ImageContext->IsTeImage)) {
231 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
232 //
233 // Use PE32 offset
234 //
235 ImageContext->ImageAddress = Hdr.Pe32->OptionalHeader.ImageBase;
236 } else {
237 //
238 // Use PE32+ offset
239 //
240 ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase;
241 }
242 } else {
243 ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));
244 }
245
246 //
247 // Initialize the alternate destination address to 0 indicating that it
248 // should not be used.
249 //
250 ImageContext->DestinationAddress = 0;
251
252 //
253 // Initialize the codeview pointer.
254 //
255 ImageContext->CodeView = NULL;
256 ImageContext->PdbPointer = NULL;
257
258 //
259 // Three cases with regards to relocations:
260 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable
261 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable
262 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but
263 // has no base relocs to apply
264 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.
265 //
266 // Look at the file header to determine if relocations have been stripped, and
267 // save this info in the image context for later use.
268 //
269 if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {
270 ImageContext->RelocationsStripped = TRUE;
271 } else {
272 ImageContext->RelocationsStripped = FALSE;
273 }
274
275 if (!(ImageContext->IsTeImage)) {
276 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
277 //
278 // Use PE32 offset
279 //
280 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
281 DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
282 } else {
283 //
284 // Use PE32+ offset
285 //
286 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
287 DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
288 }
289
290 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
291
292 DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;
293
294 //
295 // Determine the file offset of the debug directory... This means we walk
296 // the sections to find which section contains the RVA of the debug
297 // directory
298 //
299 DebugDirectoryEntryFileOffset = 0;
300
301 SectionHeaderOffset = (UINTN)(
302 ImageContext->PeCoffHeaderOffset +
303 sizeof (UINT32) +
304 sizeof (EFI_IMAGE_FILE_HEADER) +
305 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
306 );
307
308 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {
309 //
310 // Read section header from file
311 //
312 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
313 Status = ImageContext->ImageRead (
314 ImageContext->Handle,
315 SectionHeaderOffset,
316 &Size,
317 &SectionHeader
318 );
319 if (RETURN_ERROR (Status)) {
320 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
321 return Status;
322 }
323
324 if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&
325 DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {
326
327 DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData;
328 break;
329 }
330
331 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
332 }
333
334 if (DebugDirectoryEntryFileOffset != 0) {
335 for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {
336 //
337 // Read next debug directory entry
338 //
339 Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
340 Status = ImageContext->ImageRead (
341 ImageContext->Handle,
342 DebugDirectoryEntryFileOffset,
343 &Size,
344 &DebugEntry
345 );
346 if (RETURN_ERROR (Status)) {
347 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
348 return Status;
349 }
350 if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
351 ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);
352 if (DebugEntry.RVA == 0 && DebugEntry.FileOffset != 0) {
353 ImageContext->ImageSize += DebugEntry.SizeOfData;
354 }
355
356 return RETURN_SUCCESS;
357 }
358 }
359 }
360 }
361 } else {
362
363 DebugDirectoryEntry = &Hdr.Te->DataDirectory[1];
364 DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;
365 SectionHeaderOffset = (UINTN)(sizeof (EFI_TE_IMAGE_HEADER));
366
367 DebugDirectoryEntryFileOffset = 0;
368
369 for (Index = 0; Index < Hdr.Te->NumberOfSections;) {
370 //
371 // Read section header from file
372 //
373 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
374 Status = ImageContext->ImageRead (
375 ImageContext->Handle,
376 SectionHeaderOffset,
377 &Size,
378 &SectionHeader
379 );
380 if (RETURN_ERROR (Status)) {
381 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
382 return Status;
383 }
384
385 if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&
386 DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {
387 DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva -
388 SectionHeader.VirtualAddress +
389 SectionHeader.PointerToRawData +
390 sizeof (EFI_TE_IMAGE_HEADER) -
391 Hdr.Te->StrippedSize;
392
393 //
394 // File offset of the debug directory was found, if this is not the last
395 // section, then skip to the last section for calculating the image size.
396 //
397 if (Index < (UINTN) Hdr.Te->NumberOfSections - 1) {
398 SectionHeaderOffset += (Hdr.Te->NumberOfSections - 1 - Index) * sizeof (EFI_IMAGE_SECTION_HEADER);
399 Index = Hdr.Te->NumberOfSections - 1;
400 continue;
401 }
402 }
403
404 //
405 // In Te image header there is not a field to describe the ImageSize.
406 // Actually, the ImageSize equals the RVA plus the VirtualSize of
407 // the last section mapped into memory (Must be rounded up to
408 // a mulitple of Section Alignment). Per the PE/COFF specification, the
409 // section headers in the Section Table must appear in order of the RVA
410 // values for the corresponding sections. So the ImageSize can be determined
411 // by the RVA and the VirtualSize of the last section header in the
412 // Section Table.
413 //
414 if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) {
415 ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize +
416 ImageContext->SectionAlignment - 1) & ~(ImageContext->SectionAlignment - 1);
417 }
418
419 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
420 }
421
422 if (DebugDirectoryEntryFileOffset != 0) {
423 for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {
424 //
425 // Read next debug directory entry
426 //
427 Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
428 Status = ImageContext->ImageRead (
429 ImageContext->Handle,
430 DebugDirectoryEntryFileOffset,
431 &Size,
432 &DebugEntry
433 );
434 if (RETURN_ERROR (Status)) {
435 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
436 return Status;
437 }
438
439 if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
440 ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);
441 return RETURN_SUCCESS;
442 }
443 }
444 }
445 }
446
447 return RETURN_SUCCESS;
448 }
449
450
451 /**
452 Converts an image address to the loaded address.
453
454 @param ImageContext The context of the image being loaded.
455 @param Address The address to be converted to the loaded address.
456
457 @return The converted address or NULL if the address can not be converted.
458
459 **/
460 VOID *
461 PeCoffLoaderImageAddress (
462 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
463 IN UINTN Address
464 )
465 {
466 //
467 // @bug Check to make sure ImageSize is correct for the relocated image.
468 // it may only work for the file we start with and not the relocated image
469 //
470 if (Address >= ImageContext->ImageSize) {
471 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
472 return NULL;
473 }
474
475 return (CHAR8 *)((UINTN) ImageContext->ImageAddress + Address);
476 }
477
478 /**
479 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
480
481 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
482 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
483 of ImageContext as the relocation base address. The caller must allocate the relocation
484 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
485 If ImageContext is NULL, then ASSERT().
486
487 @param ImageContext Pointer to the image context structure that describes the PE/COFF
488 image that is being relocated.
489
490 @retval RETURN_SUCCESS The PE/COFF image was relocated.
491 Extended status information is in the ImageError field of ImageContext.
492 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
493 Extended status information is in the ImageError field of ImageContext.
494 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
495 Extended status information is in the ImageError field of ImageContext.
496
497 **/
498 RETURN_STATUS
499 EFIAPI
500 PeCoffLoaderRelocateImage (
501 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
502 )
503 {
504 RETURN_STATUS Status;
505 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
506 EFI_IMAGE_DATA_DIRECTORY *RelocDir;
507 UINT64 Adjust;
508 EFI_IMAGE_BASE_RELOCATION *RelocBase;
509 EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
510 UINT16 *Reloc;
511 UINT16 *RelocEnd;
512 CHAR8 *Fixup;
513 CHAR8 *FixupBase;
514 UINT16 *F16;
515 UINT32 *F32;
516 UINT64 *F64;
517 CHAR8 *FixupData;
518 PHYSICAL_ADDRESS BaseAddress;
519 UINT32 NumberOfRvaAndSizes;
520 UINT16 Magic;
521
522 ASSERT (ImageContext != NULL);
523
524 //
525 // Assume success
526 //
527 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
528
529 //
530 // If there are no relocation entries, then we are done
531 //
532 if (ImageContext->RelocationsStripped) {
533 return RETURN_SUCCESS;
534 }
535
536 //
537 // If the destination address is not 0, use that rather than the
538 // image address as the relocation target.
539 //
540 if (ImageContext->DestinationAddress != 0) {
541 BaseAddress = ImageContext->DestinationAddress;
542 } else if (!(ImageContext->IsTeImage)) {
543 BaseAddress = ImageContext->ImageAddress;
544 } else {
545 Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
546 BaseAddress = ImageContext->ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;
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->ImageBase);
596 Hdr.Te->ImageBase = (UINT64) (BaseAddress);
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 return RETURN_SUCCESS;
710 }
711
712 /**
713 Loads a PE/COFF image into memory.
714
715 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
716 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
717 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
718 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
719 If ImageContext is NULL, then ASSERT().
720
721 @param ImageContext Pointer to the image context structure that describes the PE/COFF
722 image that is being loaded.
723
724 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
725 the ImageAddress and ImageSize fields of ImageContext.
726 Extended status information is in the ImageError field of ImageContext.
727 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
728 Extended status information is in the ImageError field of ImageContext.
729 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
730 Extended status information is in the ImageError field of ImageContext.
731 @retval RETURN_INVALID_PARAMETER The image address is invalid.
732 Extended status information is in the ImageError field of ImageContext.
733
734 **/
735 RETURN_STATUS
736 EFIAPI
737 PeCoffLoaderLoadImage (
738 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
739 )
740 {
741 RETURN_STATUS Status;
742 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
743 PE_COFF_LOADER_IMAGE_CONTEXT CheckContext;
744 EFI_IMAGE_SECTION_HEADER *FirstSection;
745 EFI_IMAGE_SECTION_HEADER *Section;
746 UINTN NumberOfSections;
747 UINTN Index;
748 CHAR8 *Base;
749 CHAR8 *End;
750 CHAR8 *MaxEnd;
751 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
752 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
753 UINTN Size;
754 UINT32 TempDebugEntryRva;
755 UINT32 NumberOfRvaAndSizes;
756 UINT16 Magic;
757
758 ASSERT (ImageContext != NULL);
759
760 //
761 // Assume success
762 //
763 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
764
765 //
766 // Copy the provided context info into our local version, get what we
767 // can from the original image, and then use that to make sure everything
768 // is legit.
769 //
770 CopyMem (&CheckContext, ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
771
772 Status = PeCoffLoaderGetImageInfo (&CheckContext);
773 if (RETURN_ERROR (Status)) {
774 return Status;
775 }
776
777 //
778 // Make sure there is enough allocated space for the image being loaded
779 //
780 if (ImageContext->ImageSize < CheckContext.ImageSize) {
781 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;
782 return RETURN_BUFFER_TOO_SMALL;
783 }
784 if (ImageContext->ImageAddress == 0) {
785 //
786 // Image cannot be loaded into 0 address.
787 //
788 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
789 return RETURN_INVALID_PARAMETER;
790 }
791 //
792 // If there's no relocations, then make sure it's not a runtime driver,
793 // and that it's being loaded at the linked address.
794 //
795 if (CheckContext.RelocationsStripped) {
796 //
797 // If the image does not contain relocations and it is a runtime driver
798 // then return an error.
799 //
800 if (CheckContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
801 ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
802 return RETURN_LOAD_ERROR;
803 }
804 //
805 // If the image does not contain relocations, and the requested load address
806 // is not the linked address, then return an error.
807 //
808 if (CheckContext.ImageAddress != ImageContext->ImageAddress) {
809 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
810 return RETURN_INVALID_PARAMETER;
811 }
812 }
813 //
814 // Make sure the allocated space has the proper section alignment
815 //
816 if (!(ImageContext->IsTeImage)) {
817 if ((ImageContext->ImageAddress & (CheckContext.SectionAlignment - 1)) != 0) {
818 ImageContext->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;
819 return RETURN_INVALID_PARAMETER;
820 }
821 }
822 //
823 // Read the entire PE/COFF or TE header into memory
824 //
825 if (!(ImageContext->IsTeImage)) {
826 Status = ImageContext->ImageRead (
827 ImageContext->Handle,
828 0,
829 &ImageContext->SizeOfHeaders,
830 (VOID *) (UINTN) ImageContext->ImageAddress
831 );
832
833 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);
834
835 FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
836 (UINTN)ImageContext->ImageAddress +
837 ImageContext->PeCoffHeaderOffset +
838 sizeof(UINT32) +
839 sizeof(EFI_IMAGE_FILE_HEADER) +
840 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
841 );
842 NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);
843 } else {
844 Status = ImageContext->ImageRead (
845 ImageContext->Handle,
846 0,
847 &ImageContext->SizeOfHeaders,
848 (void *)(UINTN)ImageContext->ImageAddress
849 );
850
851 Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
852
853 FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
854 (UINTN)ImageContext->ImageAddress +
855 sizeof(EFI_TE_IMAGE_HEADER)
856 );
857 NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections);
858
859 }
860
861 if (RETURN_ERROR (Status)) {
862 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
863 return RETURN_LOAD_ERROR;
864 }
865
866 //
867 // Load each section of the image
868 //
869 Section = FirstSection;
870 for (Index = 0, MaxEnd = NULL; Index < NumberOfSections; Index++) {
871
872 //
873 // Compute sections address
874 //
875 Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress);
876 End = PeCoffLoaderImageAddress (
877 ImageContext,
878 Section->VirtualAddress + Section->Misc.VirtualSize - 1
879 );
880 if (ImageContext->IsTeImage) {
881 Base = (CHAR8 *)((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
882 End = (CHAR8 *)((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
883 }
884
885 if (End > MaxEnd) {
886 MaxEnd = End;
887 }
888 //
889 // If the base start or end address resolved to 0, then fail.
890 //
891 if ((Base == NULL) || (End == NULL)) {
892 ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;
893 return RETURN_LOAD_ERROR;
894 }
895
896 //
897 // Read the section
898 //
899 Size = (UINTN) Section->Misc.VirtualSize;
900 if ((Size == 0) || (Size > Section->SizeOfRawData)) {
901 Size = (UINTN) Section->SizeOfRawData;
902 }
903
904 if (Section->SizeOfRawData) {
905 if (!(ImageContext->IsTeImage)) {
906 Status = ImageContext->ImageRead (
907 ImageContext->Handle,
908 Section->PointerToRawData,
909 &Size,
910 Base
911 );
912 } else {
913 Status = ImageContext->ImageRead (
914 ImageContext->Handle,
915 Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize,
916 &Size,
917 Base
918 );
919 }
920
921 if (RETURN_ERROR (Status)) {
922 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
923 return Status;
924 }
925 }
926
927 //
928 // If raw size is less then virt size, zero fill the remaining
929 //
930
931 if (Size < Section->Misc.VirtualSize) {
932 ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
933 }
934
935 //
936 // Next Section
937 //
938 Section += 1;
939 }
940
941 //
942 // Get image's entry point
943 //
944 Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);
945 if (!(ImageContext->IsTeImage)) {
946 //
947 // Sizes of AddressOfEntryPoint are different so we need to do this safely
948 //
949 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
950 //
951 // Use PE32 offset
952 //
953 ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
954 ImageContext,
955 (UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint
956 );
957 } else {
958 //
959 // Use PE32+ offset
960 //
961 ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
962 ImageContext,
963 (UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint
964 );
965 }
966 } else {
967 ImageContext->EntryPoint = (PHYSICAL_ADDRESS) (
968 (UINTN)ImageContext->ImageAddress +
969 (UINTN)Hdr.Te->AddressOfEntryPoint +
970 (UINTN)sizeof(EFI_TE_IMAGE_HEADER) -
971 (UINTN)Hdr.Te->StrippedSize
972 );
973 }
974
975 //
976 // Determine the size of the fixup data
977 //
978 // Per the PE/COFF spec, you can't assume that a given data directory
979 // is present in the image. You have to check the NumberOfRvaAndSizes in
980 // the optional header to verify a desired directory entry is there.
981 //
982 if (!(ImageContext->IsTeImage)) {
983 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
984 //
985 // Use PE32 offset
986 //
987 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
988 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
989 } else {
990 //
991 // Use PE32+ offset
992 //
993 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
994 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
995 }
996
997 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
998 ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);
999 } else {
1000 ImageContext->FixupDataSize = 0;
1001 }
1002 } else {
1003 DirectoryEntry = &Hdr.Te->DataDirectory[0];
1004 ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);
1005 }
1006 //
1007 // Consumer must allocate a buffer for the relocation fixup log.
1008 // Only used for runtime drivers.
1009 //
1010 ImageContext->FixupData = NULL;
1011
1012 //
1013 // Load the Codeview info if present
1014 //
1015 if (ImageContext->DebugDirectoryEntryRva != 0) {
1016 if (!(ImageContext->IsTeImage)) {
1017 DebugEntry = PeCoffLoaderImageAddress (
1018 ImageContext,
1019 ImageContext->DebugDirectoryEntryRva
1020 );
1021 } else {
1022 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)(UINTN)(
1023 ImageContext->ImageAddress +
1024 ImageContext->DebugDirectoryEntryRva +
1025 sizeof(EFI_TE_IMAGE_HEADER) -
1026 Hdr.Te->StrippedSize
1027 );
1028 }
1029
1030 if (DebugEntry != NULL) {
1031 TempDebugEntryRva = DebugEntry->RVA;
1032 if (DebugEntry->RVA == 0 && DebugEntry->FileOffset != 0) {
1033 Section--;
1034 if ((UINTN)Section->SizeOfRawData < Section->Misc.VirtualSize) {
1035 TempDebugEntryRva = Section->VirtualAddress + Section->Misc.VirtualSize;
1036 } else {
1037 TempDebugEntryRva = Section->VirtualAddress + Section->SizeOfRawData;
1038 }
1039 }
1040
1041 if (TempDebugEntryRva != 0) {
1042 if (!(ImageContext->IsTeImage)) {
1043 ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva);
1044 } else {
1045 ImageContext->CodeView = (VOID *)(
1046 (UINTN)ImageContext->ImageAddress +
1047 (UINTN)TempDebugEntryRva +
1048 (UINTN)sizeof (EFI_TE_IMAGE_HEADER) -
1049 (UINTN) Hdr.Te->StrippedSize
1050 );
1051 }
1052
1053 if (ImageContext->CodeView == NULL) {
1054 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1055 return RETURN_LOAD_ERROR;
1056 }
1057
1058 if (DebugEntry->RVA == 0) {
1059 Size = DebugEntry->SizeOfData;
1060 if (!(ImageContext->IsTeImage)) {
1061 Status = ImageContext->ImageRead (
1062 ImageContext->Handle,
1063 DebugEntry->FileOffset,
1064 &Size,
1065 ImageContext->CodeView
1066 );
1067 } else {
1068 Status = ImageContext->ImageRead (
1069 ImageContext->Handle,
1070 DebugEntry->FileOffset + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize,
1071 &Size,
1072 ImageContext->CodeView
1073 );
1074 //
1075 // Should we apply fix up to this field according to the size difference between PE and TE?
1076 // Because now we maintain TE header fields unfixed, this field will also remain as they are
1077 // in original PE image.
1078 //
1079 }
1080
1081 if (RETURN_ERROR (Status)) {
1082 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1083 return RETURN_LOAD_ERROR;
1084 }
1085
1086 DebugEntry->RVA = TempDebugEntryRva;
1087 }
1088
1089 switch (*(UINT32 *) ImageContext->CodeView) {
1090 case CODEVIEW_SIGNATURE_NB10:
1091 ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);
1092 break;
1093
1094 case CODEVIEW_SIGNATURE_RSDS:
1095 ImageContext->PdbPointer = (CHAR8 *)ImageContext->CodeView + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
1096 break;
1097
1098 default:
1099 break;
1100 }
1101 }
1102 }
1103 }
1104
1105 return Status;
1106 }
1107
1108
1109 /**
1110 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
1111 runtime.
1112
1113 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
1114 the fixups with a virtual mapping.
1115
1116
1117 @param ImageBase Base address of relocated image
1118 @param VirtImageBase Virtual mapping for ImageBase
1119 @param ImageSize Size of the image to relocate
1120 @param RelocationData Location to place results of read
1121
1122 **/
1123 VOID
1124 EFIAPI
1125 PeCoffLoaderRelocateImageForRuntime (
1126 IN PHYSICAL_ADDRESS ImageBase,
1127 IN PHYSICAL_ADDRESS VirtImageBase,
1128 IN UINTN ImageSize,
1129 IN VOID *RelocationData
1130 )
1131 {
1132 CHAR8 *OldBase;
1133 CHAR8 *NewBase;
1134 EFI_IMAGE_DOS_HEADER *DosHdr;
1135 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
1136 UINT32 NumberOfRvaAndSizes;
1137 EFI_IMAGE_DATA_DIRECTORY *DataDirectory;
1138 EFI_IMAGE_DATA_DIRECTORY *RelocDir;
1139 EFI_IMAGE_BASE_RELOCATION *RelocBase;
1140 EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
1141 UINT16 *Reloc;
1142 UINT16 *RelocEnd;
1143 CHAR8 *Fixup;
1144 CHAR8 *FixupBase;
1145 UINT16 *F16;
1146 UINT32 *F32;
1147 UINT64 *F64;
1148 CHAR8 *FixupData;
1149 UINTN Adjust;
1150 RETURN_STATUS Status;
1151 UINT16 Magic;
1152
1153 OldBase = (CHAR8 *)((UINTN)ImageBase);
1154 NewBase = (CHAR8 *)((UINTN)VirtImageBase);
1155 Adjust = (UINTN) NewBase - (UINTN) OldBase;
1156
1157 //
1158 // Find the image's relocate dir info
1159 //
1160 DosHdr = (EFI_IMAGE_DOS_HEADER *)OldBase;
1161 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
1162 //
1163 // Valid DOS header so get address of PE header
1164 //
1165 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(((CHAR8 *)DosHdr) + DosHdr->e_lfanew);
1166 } else {
1167 //
1168 // No Dos header so assume image starts with PE header.
1169 //
1170 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)OldBase;
1171 }
1172
1173 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1174 //
1175 // Not a valid PE image so Exit
1176 //
1177 return ;
1178 }
1179
1180 Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);
1181
1182 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1183 //
1184 // Use PE32 offset
1185 //
1186 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1187 DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]);
1188 } else {
1189 //
1190 // Use PE32+ offset
1191 //
1192 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1193 DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);
1194 }
1195
1196 //
1197 // Find the relocation block
1198 //
1199 // Per the PE/COFF spec, you can't assume that a given data directory
1200 // is present in the image. You have to check the NumberOfRvaAndSizes in
1201 // the optional header to verify a desired directory entry is there.
1202 //
1203 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1204 RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;
1205 RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);
1206 RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);
1207 } else {
1208 //
1209 // Cannot find relocations, cannot continue
1210 //
1211 ASSERT (FALSE);
1212 return ;
1213 }
1214
1215 ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);
1216
1217 //
1218 // Run the whole relocation block. And re-fixup data that has not been
1219 // modified. The FixupData is used to see if the image has been modified
1220 // since it was relocated. This is so data sections that have been updated
1221 // by code will not be fixed up, since that would set them back to
1222 // defaults.
1223 //
1224 FixupData = RelocationData;
1225 while (RelocBase < RelocBaseEnd) {
1226
1227 Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));
1228 RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);
1229 FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;
1230
1231 //
1232 // Run this relocation record
1233 //
1234 while (Reloc < RelocEnd) {
1235
1236 Fixup = FixupBase + (*Reloc & 0xFFF);
1237 switch ((*Reloc) >> 12) {
1238
1239 case EFI_IMAGE_REL_BASED_ABSOLUTE:
1240 break;
1241
1242 case EFI_IMAGE_REL_BASED_HIGH:
1243 F16 = (UINT16 *) Fixup;
1244 if (*(UINT16 *) FixupData == *F16) {
1245 *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16)));
1246 }
1247
1248 FixupData = FixupData + sizeof (UINT16);
1249 break;
1250
1251 case EFI_IMAGE_REL_BASED_LOW:
1252 F16 = (UINT16 *) Fixup;
1253 if (*(UINT16 *) FixupData == *F16) {
1254 *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff));
1255 }
1256
1257 FixupData = FixupData + sizeof (UINT16);
1258 break;
1259
1260 case EFI_IMAGE_REL_BASED_HIGHLOW:
1261 F32 = (UINT32 *) Fixup;
1262 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));
1263 if (*(UINT32 *) FixupData == *F32) {
1264 *F32 = *F32 + (UINT32) Adjust;
1265 }
1266
1267 FixupData = FixupData + sizeof (UINT32);
1268 break;
1269
1270 case EFI_IMAGE_REL_BASED_DIR64:
1271 F64 = (UINT64 *)Fixup;
1272 FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));
1273 if (*(UINT64 *) FixupData == *F64) {
1274 *F64 = *F64 + (UINT64)Adjust;
1275 }
1276
1277 FixupData = FixupData + sizeof (UINT64);
1278 break;
1279
1280 case EFI_IMAGE_REL_BASED_HIGHADJ:
1281 //
1282 // Not implemented, but not used in EFI 1.0
1283 //
1284 ASSERT (FALSE);
1285 break;
1286
1287 default:
1288 //
1289 // Only Itanium requires ConvertPeImage_Ex
1290 //
1291 Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);
1292 if (RETURN_ERROR (Status)) {
1293 return ;
1294 }
1295 }
1296 //
1297 // Next relocation record
1298 //
1299 Reloc += 1;
1300 }
1301 //
1302 // next reloc block
1303 //
1304 RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
1305 }
1306 }
1307
1308
1309 /**
1310 ImageRead function that operates on a memory buffer whos base is passed into
1311 FileHandle.
1312
1313 @param FileHandle Ponter to baes of the input stream
1314 @param FileOffset Offset to the start of the buffer
1315 @param ReadSize Number of bytes to copy into the buffer
1316 @param Buffer Location to place results of read
1317
1318 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
1319 the buffer.
1320 **/
1321 RETURN_STATUS
1322 EFIAPI
1323 PeCoffLoaderImageReadFromMemory (
1324 IN VOID *FileHandle,
1325 IN UINTN FileOffset,
1326 IN OUT UINTN *ReadSize,
1327 OUT VOID *Buffer
1328 )
1329 {
1330 CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);
1331 return RETURN_SUCCESS;
1332 }
1333