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