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