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