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