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