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