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