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