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