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