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