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