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