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