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