]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Tiano PE/COFF loader.\r | |
3 | \r | |
4 | This PE/COFF loader supports loading any PE32 or PE32+ image type, but\r | |
5 | only supports relocating IA32, X64, IPF, and EBC images.\r | |
6 | \r | |
7 | Copyright (c) 2006, Intel Corporation\r | |
8 | All rights reserved. This program and the accompanying materials\r | |
9 | are licensed and made available under the terms and conditions of the BSD License\r | |
10 | which accompanies this distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | \r | |
16 | Module Name: PeCoffLoader.c\r | |
17 | \r | |
18 | **/\r | |
19 | \r | |
20 | //\r | |
21 | // Include common header file for this module.\r | |
22 | //\r | |
23 | #include "CommonHeader.h"\r | |
24 | \r | |
25 | #include "BasePeCoffLibInternals.h"\r | |
26 | \r | |
27 | /**\r | |
28 | Retrieves the magic value from the PE/COFF header.\r | |
29 | \r | |
30 | @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r | |
31 | \r | |
32 | @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32\r | |
33 | @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+\r | |
34 | \r | |
35 | **/\r | |
36 | UINT16\r | |
37 | PeCoffLoaderGetPeHeaderMagicValue (\r | |
38 | IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r | |
39 | )\r | |
40 | {\r | |
41 | //\r | |
42 | // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value \r | |
43 | // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the \r | |
44 | // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r | |
45 | // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r | |
46 | //\r | |
47 | if (Hdr.Pe32->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
48 | return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r | |
49 | }\r | |
50 | //\r | |
51 | // Return the magic value from the PC/COFF Optional Header\r | |
52 | //\r | |
53 | return Hdr.Pe32->OptionalHeader.Magic;\r | |
54 | }\r | |
55 | \r | |
56 | \r | |
57 | /**\r | |
58 | Retrieves the PE or TE Header from a PE/COFF or TE image.\r | |
59 | \r | |
60 | @param ImageContext The context of the image being loaded.\r | |
61 | @param Hdr The buffer in which to return the PE32, PE32+, or TE header.\r | |
62 | \r | |
63 | @retval RETURN_SUCCESS The PE or TE Header is read.\r | |
64 | @retval Other The error status from reading the PE/COFF or TE image using the ImageRead function.\r | |
65 | \r | |
66 | **/\r | |
67 | RETURN_STATUS\r | |
68 | PeCoffLoaderGetPeHeader (\r | |
69 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
70 | OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr\r | |
71 | )\r | |
72 | {\r | |
73 | RETURN_STATUS Status;\r | |
74 | EFI_IMAGE_DOS_HEADER DosHdr;\r | |
75 | UINTN Size;\r | |
76 | UINT16 Magic;\r | |
77 | \r | |
78 | //\r | |
79 | // Read the DOS image header to check for it's existance\r | |
80 | //\r | |
81 | Size = sizeof (EFI_IMAGE_DOS_HEADER);\r | |
82 | Status = ImageContext->ImageRead (\r | |
83 | ImageContext->Handle,\r | |
84 | 0,\r | |
85 | &Size,\r | |
86 | &DosHdr\r | |
87 | );\r | |
88 | if (RETURN_ERROR (Status)) {\r | |
89 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
90 | return Status;\r | |
91 | }\r | |
92 | \r | |
93 | ImageContext->PeCoffHeaderOffset = 0;\r | |
94 | if (DosHdr.e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
95 | //\r | |
96 | // DOS image header is present, so read the PE header after the DOS image\r | |
97 | // header\r | |
98 | //\r | |
99 | ImageContext->PeCoffHeaderOffset = DosHdr.e_lfanew;\r | |
100 | }\r | |
101 | \r | |
102 | //\r | |
103 | // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much\r | |
104 | // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic\r | |
105 | // determins if this is a PE32 or PE32+ image. The magic is in the same\r | |
106 | // location in both images.\r | |
107 | //\r | |
108 | Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r | |
109 | Status = ImageContext->ImageRead (\r | |
110 | ImageContext->Handle,\r | |
111 | ImageContext->PeCoffHeaderOffset,\r | |
112 | &Size,\r | |
113 | Hdr.Pe32\r | |
114 | );\r | |
115 | if (RETURN_ERROR (Status)) {\r | |
116 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
117 | return Status;\r | |
118 | }\r | |
119 | \r | |
120 | //\r | |
121 | // Use Signature to figure out if we understand the image format\r | |
122 | //\r | |
123 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
124 | ImageContext->IsTeImage = TRUE;\r | |
125 | ImageContext->Machine = Hdr.Te->Machine;\r | |
126 | ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem);\r | |
127 | ImageContext->ImageSize = 0;\r | |
128 | ImageContext->SectionAlignment = 4096;\r | |
129 | ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;\r | |
130 | \r | |
131 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
132 | ImageContext->IsTeImage = FALSE;\r | |
133 | ImageContext->Machine = Hdr.Pe32->FileHeader.Machine;\r | |
134 | \r | |
135 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
136 | \r | |
137 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
138 | //\r | |
139 | // Use PE32 offset\r | |
140 | //\r | |
141 | ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem;\r | |
142 | ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage;\r | |
143 | ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment;\r | |
144 | ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r | |
145 | \r | |
146 | } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r | |
147 | //\r | |
148 | // Use PE32+ offset\r | |
149 | //\r | |
150 | ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem;\r | |
151 | ImageContext->ImageSize = (UINT64) Hdr.Pe32Plus->OptionalHeader.SizeOfImage;\r | |
152 | ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;\r | |
153 | ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;\r | |
154 | } else {\r | |
155 | ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;\r | |
156 | return RETURN_UNSUPPORTED;\r | |
157 | }\r | |
158 | } else {\r | |
159 | ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;\r | |
160 | return RETURN_UNSUPPORTED;\r | |
161 | }\r | |
162 | \r | |
163 | if (!PeCoffLoaderImageFormatSupported (ImageContext->Machine)) {\r | |
164 | //\r | |
165 | // If the PE/COFF loader does not support the image type return\r | |
166 | // unsupported. This library can suport lots of types of images\r | |
167 | // this does not mean the user of this library can call the entry\r | |
168 | // point of the image.\r | |
169 | //\r | |
170 | return RETURN_UNSUPPORTED;\r | |
171 | }\r | |
172 | \r | |
173 | return RETURN_SUCCESS;\r | |
174 | }\r | |
175 | \r | |
176 | \r | |
177 | /**\r | |
178 | Retrieves information about a PE/COFF image.\r | |
179 | \r | |
180 | Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,\r | |
181 | PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva\r | |
182 | fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.\r | |
183 | If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not\r | |
184 | a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while\r | |
185 | computing the fields of ImageContext, then the error status is returned in the ImageError field of\r | |
186 | ImageContext.\r | |
187 | \r | |
188 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
189 | image that needs to be examined by this function.\r | |
190 | \r | |
191 | @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r | |
192 | @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r | |
193 | @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r | |
194 | \r | |
195 | **/\r | |
196 | RETURN_STATUS\r | |
197 | EFIAPI\r | |
198 | PeCoffLoaderGetImageInfo (\r | |
199 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
200 | )\r | |
201 | {\r | |
202 | RETURN_STATUS Status;\r | |
203 | EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r | |
204 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
205 | EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry;\r | |
206 | UINTN Size;\r | |
207 | UINTN Index;\r | |
208 | UINTN DebugDirectoryEntryRva;\r | |
209 | UINTN DebugDirectoryEntryFileOffset;\r | |
210 | UINTN SectionHeaderOffset;\r | |
211 | EFI_IMAGE_SECTION_HEADER SectionHeader;\r | |
212 | EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry;\r | |
213 | UINT32 NumberOfRvaAndSizes;\r | |
214 | UINT16 Magic;\r | |
215 | \r | |
216 | if (NULL == ImageContext) {\r | |
217 | return RETURN_INVALID_PARAMETER;\r | |
218 | }\r | |
219 | //\r | |
220 | // Assume success\r | |
221 | //\r | |
222 | ImageContext->ImageError = IMAGE_ERROR_SUCCESS;\r | |
223 | \r | |
224 | Hdr.Union = &HdrData;\r | |
225 | Status = PeCoffLoaderGetPeHeader (ImageContext, Hdr);\r | |
226 | if (RETURN_ERROR (Status)) {\r | |
227 | return Status;\r | |
228 | }\r | |
229 | \r | |
230 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
231 | \r | |
232 | //\r | |
233 | // Retrieve the base address of the image\r | |
234 | //\r | |
235 | if (!(ImageContext->IsTeImage)) {\r | |
236 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
237 | //\r | |
238 | // Use PE32 offset\r | |
239 | //\r | |
240 | ImageContext->ImageAddress = Hdr.Pe32->OptionalHeader.ImageBase;\r | |
241 | } else {\r | |
242 | //\r | |
243 | // Use PE32+ offset\r | |
244 | //\r | |
245 | ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase;\r | |
246 | }\r | |
247 | } else {\r | |
248 | ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));\r | |
249 | }\r | |
250 | \r | |
251 | //\r | |
252 | // Initialize the alternate destination address to 0 indicating that it\r | |
253 | // should not be used.\r | |
254 | //\r | |
255 | ImageContext->DestinationAddress = 0;\r | |
256 | \r | |
257 | //\r | |
258 | // Initialize the codeview pointer.\r | |
259 | //\r | |
260 | ImageContext->CodeView = NULL;\r | |
261 | ImageContext->PdbPointer = NULL;\r | |
262 | \r | |
263 | //\r | |
264 | // Three cases with regards to relocations:\r | |
265 | // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r | |
266 | // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r | |
267 | // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r | |
268 | // has no base relocs to apply\r | |
269 | // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r | |
270 | //\r | |
271 | // Look at the file header to determine if relocations have been stripped, and\r | |
272 | // save this info in the image context for later use.\r | |
273 | //\r | |
274 | if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {\r | |
275 | ImageContext->RelocationsStripped = TRUE;\r | |
276 | } else {\r | |
277 | ImageContext->RelocationsStripped = FALSE;\r | |
278 | }\r | |
279 | \r | |
280 | if (!(ImageContext->IsTeImage)) {\r | |
281 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
282 | //\r | |
283 | // Use PE32 offset\r | |
284 | //\r | |
285 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
286 | DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
287 | } else {\r | |
288 | //\r | |
289 | // Use PE32+ offset\r | |
290 | //\r | |
291 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
292 | DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
293 | }\r | |
294 | \r | |
295 | if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r | |
296 | \r | |
297 | DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;\r | |
298 | \r | |
299 | //\r | |
300 | // Determine the file offset of the debug directory... This means we walk\r | |
301 | // the sections to find which section contains the RVA of the debug\r | |
302 | // directory\r | |
303 | //\r | |
304 | DebugDirectoryEntryFileOffset = 0;\r | |
305 | \r | |
306 | SectionHeaderOffset = (UINTN)(\r | |
307 | ImageContext->PeCoffHeaderOffset +\r | |
308 | sizeof (UINT32) +\r | |
309 | sizeof (EFI_IMAGE_FILE_HEADER) +\r | |
310 | Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r | |
311 | );\r | |
312 | \r | |
313 | for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {\r | |
314 | //\r | |
315 | // Read section header from file\r | |
316 | //\r | |
317 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
318 | Status = ImageContext->ImageRead (\r | |
319 | ImageContext->Handle,\r | |
320 | SectionHeaderOffset,\r | |
321 | &Size,\r | |
322 | &SectionHeader\r | |
323 | );\r | |
324 | if (RETURN_ERROR (Status)) {\r | |
325 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
326 | return Status;\r | |
327 | }\r | |
328 | \r | |
329 | if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&\r | |
330 | DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {\r | |
331 | \r | |
332 | DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData;\r | |
333 | break;\r | |
334 | }\r | |
335 | \r | |
336 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
337 | }\r | |
338 | \r | |
339 | if (DebugDirectoryEntryFileOffset != 0) {\r | |
340 | for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r | |
341 | //\r | |
342 | // Read next debug directory entry\r | |
343 | //\r | |
344 | Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r | |
345 | Status = ImageContext->ImageRead (\r | |
346 | ImageContext->Handle,\r | |
347 | DebugDirectoryEntryFileOffset,\r | |
348 | &Size,\r | |
349 | &DebugEntry\r | |
350 | );\r | |
351 | if (RETURN_ERROR (Status)) {\r | |
352 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
353 | return Status;\r | |
354 | }\r | |
355 | if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r | |
356 | ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);\r | |
357 | if (DebugEntry.RVA == 0 && DebugEntry.FileOffset != 0) {\r | |
358 | ImageContext->ImageSize += DebugEntry.SizeOfData;\r | |
359 | }\r | |
360 | \r | |
361 | return RETURN_SUCCESS;\r | |
362 | }\r | |
363 | }\r | |
364 | }\r | |
365 | }\r | |
366 | } else {\r | |
367 | \r | |
368 | DebugDirectoryEntry = &Hdr.Te->DataDirectory[1];\r | |
369 | DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;\r | |
370 | SectionHeaderOffset = (UINTN)(sizeof (EFI_TE_IMAGE_HEADER));\r | |
371 | \r | |
372 | DebugDirectoryEntryFileOffset = 0;\r | |
373 | \r | |
374 | for (Index = 0; Index < Hdr.Te->NumberOfSections;) {\r | |
375 | //\r | |
376 | // Read section header from file\r | |
377 | //\r | |
378 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
379 | Status = ImageContext->ImageRead (\r | |
380 | ImageContext->Handle,\r | |
381 | SectionHeaderOffset,\r | |
382 | &Size,\r | |
383 | &SectionHeader\r | |
384 | );\r | |
385 | if (RETURN_ERROR (Status)) {\r | |
386 | ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r | |
387 | return Status;\r | |
388 | }\r | |
389 | \r | |
390 | if (DebugDirectoryEntryRva >= SectionHeader.VirtualAddress &&\r | |
391 | DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) {\r | |
392 | DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva -\r | |
393 | SectionHeader.VirtualAddress +\r | |
394 | SectionHeader.PointerToRawData +\r | |
395 | sizeof (EFI_TE_IMAGE_HEADER) -\r | |
396 | Hdr.Te->StrippedSize;\r | |
397 | \r | |
398 | //\r | |
399 | // File offset of the debug directory was found, if this is not the last\r | |
400 | // section, then skip to the last section for calculating the image size.\r | |
401 | //\r | |
402 | if (Index < (UINTN) Hdr.Te->NumberOfSections - 1) {\r | |
403 | SectionHeaderOffset += (Hdr.Te->NumberOfSections - 1 - Index) * sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
404 | Index = Hdr.Te->NumberOfSections - 1;\r | |
405 | continue;\r | |
406 | }\r | |
407 | }\r | |
408 | \r | |
409 | //\r | |
410 | // In Te image header there is not a field to describe the ImageSize.\r | |
411 | // Actually, the ImageSize equals the RVA plus the VirtualSize of\r | |
412 | // the last section mapped into memory (Must be rounded up to\r | |
413 | // a mulitple of Section Alignment). Per the PE/COFF specification, the\r | |
414 | // section headers in the Section Table must appear in order of the RVA\r | |
415 | // values for the corresponding sections. So the ImageSize can be determined\r | |
416 | // by the RVA and the VirtualSize of the last section header in the\r | |
417 | // Section Table.\r | |
418 | //\r | |
419 | if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) {\r | |
420 | ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize +\r | |
421 | ImageContext->SectionAlignment - 1) & ~(ImageContext->SectionAlignment - 1);\r | |
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 | |
465 | VOID *\r | |
466 | PeCoffLoaderImageAddress (\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 | |
503 | RETURN_STATUS\r | |
504 | EFIAPI\r | |
505 | PeCoffLoaderRelocateImage (\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 | |
519 | UINT16 *F16;\r | |
520 | UINT32 *F32;\r | |
521 | UINT64 *F64;\r | |
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 | |
547 | } else if (!(ImageContext->IsTeImage)) {\r | |
548 | BaseAddress = ImageContext->ImageAddress;\r | |
549 | } else {\r | |
550 | Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);\r | |
551 | BaseAddress = ImageContext->ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize; \r | |
552 | }\r | |
553 | \r | |
554 | if (!(ImageContext->IsTeImage)) {\r | |
555 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);\r | |
556 | \r | |
557 | Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r | |
558 | \r | |
559 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
560 | //\r | |
561 | // Use PE32 offset\r | |
562 | //\r | |
563 | Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase;\r | |
564 | Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;\r | |
565 | \r | |
566 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
567 | RelocDir = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r | |
568 | } else {\r | |
569 | //\r | |
570 | // Use PE32+ offset\r | |
571 | //\r | |
572 | Adjust = (UINT64) BaseAddress - Hdr.Pe32Plus->OptionalHeader.ImageBase;\r | |
573 | Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;\r | |
574 | \r | |
575 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
576 | RelocDir = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r | |
577 | }\r | |
578 | \r | |
579 | //\r | |
580 | // Find the relocation block\r | |
581 | // Per the PE/COFF spec, you can't assume that a given data directory\r | |
582 | // is present in the image. You have to check the NumberOfRvaAndSizes in\r | |
583 | // the optional header to verify a desired directory entry is there.\r | |
584 | //\r | |
585 | \r | |
586 | if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r | |
587 | RelocBase = PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress);\r | |
588 | RelocBaseEnd = PeCoffLoaderImageAddress (\r | |
589 | ImageContext,\r | |
590 | RelocDir->VirtualAddress + RelocDir->Size - 1\r | |
591 | );\r | |
592 | } else {\r | |
593 | //\r | |
594 | // Set base and end to bypass processing below.\r | |
595 | //\r | |
596 | RelocBase = RelocBaseEnd = 0;\r | |
597 | }\r | |
598 | } else {\r | |
599 | Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);\r | |
600 | Adjust = (UINT64) (BaseAddress - Hdr.Te->ImageBase);\r | |
601 | Hdr.Te->ImageBase = (UINT64) (BaseAddress);\r | |
602 | \r | |
603 | //\r | |
604 | // Find the relocation block\r | |
605 | //\r | |
606 | RelocDir = &Hdr.Te->DataDirectory[0];\r | |
607 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(\r | |
608 | ImageContext->ImageAddress +\r | |
609 | RelocDir->VirtualAddress +\r | |
610 | sizeof(EFI_TE_IMAGE_HEADER) -\r | |
611 | Hdr.Te->StrippedSize\r | |
612 | );\r | |
613 | RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) ((UINTN) RelocBase + (UINTN) RelocDir->Size - 1);\r | |
614 | }\r | |
615 | \r | |
616 | //\r | |
617 | // Run the relocation information and apply the fixups\r | |
618 | //\r | |
619 | FixupData = ImageContext->FixupData;\r | |
620 | while (RelocBase < RelocBaseEnd) {\r | |
621 | \r | |
622 | Reloc = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r | |
623 | RelocEnd = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);\r | |
624 | if (!(ImageContext->IsTeImage)) {\r | |
625 | FixupBase = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress);\r | |
626 | } else {\r | |
627 | FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +\r | |
628 | RelocBase->VirtualAddress +\r | |
629 | sizeof(EFI_TE_IMAGE_HEADER) -\r | |
630 | Hdr.Te->StrippedSize\r | |
631 | );\r | |
632 | }\r | |
633 | \r | |
634 | if ((CHAR8 *) RelocEnd < (CHAR8 *) ((UINTN) ImageContext->ImageAddress) ||\r | |
635 | (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress +\r | |
636 | (UINTN)ImageContext->ImageSize)) {\r | |
637 | ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r | |
638 | return RETURN_LOAD_ERROR;\r | |
639 | }\r | |
640 | \r | |
641 | //\r | |
642 | // Run this relocation record\r | |
643 | //\r | |
644 | while (Reloc < RelocEnd) {\r | |
645 | \r | |
646 | Fixup = FixupBase + (*Reloc & 0xFFF);\r | |
647 | switch ((*Reloc) >> 12) {\r | |
648 | case EFI_IMAGE_REL_BASED_ABSOLUTE:\r | |
649 | break;\r | |
650 | \r | |
651 | case EFI_IMAGE_REL_BASED_HIGH:\r | |
652 | F16 = (UINT16 *) Fixup;\r | |
653 | *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16)));\r | |
654 | if (FixupData != NULL) {\r | |
655 | *(UINT16 *) FixupData = *F16;\r | |
656 | FixupData = FixupData + sizeof (UINT16);\r | |
657 | }\r | |
658 | break;\r | |
659 | \r | |
660 | case EFI_IMAGE_REL_BASED_LOW:\r | |
661 | F16 = (UINT16 *) Fixup;\r | |
662 | *F16 = (UINT16) (*F16 + (UINT16) Adjust);\r | |
663 | if (FixupData != NULL) {\r | |
664 | *(UINT16 *) FixupData = *F16;\r | |
665 | FixupData = FixupData + sizeof (UINT16);\r | |
666 | }\r | |
667 | break;\r | |
668 | \r | |
669 | case EFI_IMAGE_REL_BASED_HIGHLOW:\r | |
670 | F32 = (UINT32 *) Fixup;\r | |
671 | *F32 = *F32 + (UINT32) Adjust;\r | |
672 | if (FixupData != NULL) {\r | |
673 | FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r | |
674 | *(UINT32 *)FixupData = *F32;\r | |
675 | FixupData = FixupData + sizeof (UINT32);\r | |
676 | }\r | |
677 | break;\r | |
678 | \r | |
679 | case EFI_IMAGE_REL_BASED_DIR64:\r | |
680 | F64 = (UINT64 *) Fixup;\r | |
681 | *F64 = *F64 + (UINT64) Adjust;\r | |
682 | if (FixupData != NULL) {\r | |
683 | FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));\r | |
684 | *(UINT64 *)(FixupData) = *F64;\r | |
685 | FixupData = FixupData + sizeof(UINT64);\r | |
686 | }\r | |
687 | break;\r | |
688 | \r | |
689 | default:\r | |
690 | //\r | |
691 | // The common code does not handle some of the stranger IPF relocations\r | |
692 | // PeCoffLoaderRelocateImageEx () addes support for these complex fixups\r | |
693 | // on IPF and is a No-Op on other archtiectures.\r | |
694 | //\r | |
695 | Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r | |
696 | if (RETURN_ERROR (Status)) {\r | |
697 | ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r | |
698 | return Status;\r | |
699 | }\r | |
700 | }\r | |
701 | \r | |
702 | //\r | |
703 | // Next relocation record\r | |
704 | //\r | |
705 | Reloc += 1;\r | |
706 | }\r | |
707 | \r | |
708 | //\r | |
709 | // Next reloc block\r | |
710 | //\r | |
711 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r | |
712 | }\r | |
713 | \r | |
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 | |
740 | RETURN_STATUS\r | |
741 | EFIAPI\r | |
742 | PeCoffLoaderLoadImage (\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 | |
909 | if (Section->SizeOfRawData) {\r | |
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 | |
1128 | VOID\r | |
1129 | EFIAPI\r | |
1130 | PeCoffLoaderRelocateImageForRuntime (\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 | |
1214 | // Cannot find relocations, cannot continue\r | |
1215 | //\r | |
1216 | ASSERT (FALSE);\r | |
1217 | return ;\r | |
1218 | }\r | |
1219 | \r | |
1220 | ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);\r | |
1221 | \r | |
1222 | //\r | |
1223 | // Run the whole relocation block. And re-fixup data that has not been\r | |
1224 | // modified. The FixupData is used to see if the image has been modified\r | |
1225 | // since it was relocated. This is so data sections that have been updated\r | |
1226 | // by code will not be fixed up, since that would set them back to\r | |
1227 | // defaults.\r | |
1228 | //\r | |
1229 | FixupData = RelocationData;\r | |
1230 | while (RelocBase < RelocBaseEnd) {\r | |
1231 | \r | |
1232 | Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r | |
1233 | RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);\r | |
1234 | FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;\r | |
1235 | \r | |
1236 | //\r | |
1237 | // Run this relocation record\r | |
1238 | //\r | |
1239 | while (Reloc < RelocEnd) {\r | |
1240 | \r | |
1241 | Fixup = FixupBase + (*Reloc & 0xFFF);\r | |
1242 | switch ((*Reloc) >> 12) {\r | |
1243 | \r | |
1244 | case EFI_IMAGE_REL_BASED_ABSOLUTE:\r | |
1245 | break;\r | |
1246 | \r | |
1247 | case EFI_IMAGE_REL_BASED_HIGH:\r | |
1248 | F16 = (UINT16 *) Fixup;\r | |
1249 | if (*(UINT16 *) FixupData == *F16) {\r | |
1250 | *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16)));\r | |
1251 | }\r | |
1252 | \r | |
1253 | FixupData = FixupData + sizeof (UINT16);\r | |
1254 | break;\r | |
1255 | \r | |
1256 | case EFI_IMAGE_REL_BASED_LOW:\r | |
1257 | F16 = (UINT16 *) Fixup;\r | |
1258 | if (*(UINT16 *) FixupData == *F16) {\r | |
1259 | *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff));\r | |
1260 | }\r | |
1261 | \r | |
1262 | FixupData = FixupData + sizeof (UINT16);\r | |
1263 | break;\r | |
1264 | \r | |
1265 | case EFI_IMAGE_REL_BASED_HIGHLOW:\r | |
1266 | F32 = (UINT32 *) Fixup;\r | |
1267 | FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r | |
1268 | if (*(UINT32 *) FixupData == *F32) {\r | |
1269 | *F32 = *F32 + (UINT32) Adjust;\r | |
1270 | }\r | |
1271 | \r | |
1272 | FixupData = FixupData + sizeof (UINT32);\r | |
1273 | break;\r | |
1274 | \r | |
1275 | case EFI_IMAGE_REL_BASED_DIR64:\r | |
1276 | F64 = (UINT64 *)Fixup;\r | |
1277 | FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));\r | |
1278 | if (*(UINT64 *) FixupData == *F64) {\r | |
1279 | *F64 = *F64 + (UINT64)Adjust;\r | |
1280 | }\r | |
1281 | \r | |
1282 | FixupData = FixupData + sizeof (UINT64);\r | |
1283 | break;\r | |
1284 | \r | |
1285 | case EFI_IMAGE_REL_BASED_HIGHADJ:\r | |
1286 | //\r | |
1287 | // Not implemented, but not used in EFI 1.0\r | |
1288 | //\r | |
1289 | ASSERT (FALSE);\r | |
1290 | break;\r | |
1291 | \r | |
1292 | default:\r | |
1293 | //\r | |
1294 | // Only Itanium requires ConvertPeImage_Ex\r | |
1295 | //\r | |
1296 | Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r | |
1297 | if (RETURN_ERROR (Status)) {\r | |
1298 | return ;\r | |
1299 | }\r | |
1300 | }\r | |
1301 | //\r | |
1302 | // Next relocation record\r | |
1303 | //\r | |
1304 | Reloc += 1;\r | |
1305 | }\r | |
1306 | //\r | |
1307 | // next reloc block\r | |
1308 | //\r | |
1309 | RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r | |
1310 | }\r | |
1311 | }\r | |
1312 | \r | |
1313 | \r | |
1314 | /**\r | |
1315 | ImageRead function that operates on a memory buffer whos base is passed into\r | |
1316 | FileHandle.\r | |
1317 | \r | |
1318 | @param FileHandle Ponter to baes of the input stream\r | |
1319 | @param FileOffset Offset to the start of the buffer\r | |
1320 | @param ReadSize Number of bytes to copy into the buffer\r | |
1321 | @param Buffer Location to place results of read\r | |
1322 | \r | |
1323 | @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into\r | |
1324 | the buffer.\r | |
1325 | **/\r | |
1326 | RETURN_STATUS\r | |
1327 | EFIAPI\r | |
1328 | PeCoffLoaderImageReadFromMemory (\r | |
1329 | IN VOID *FileHandle,\r | |
1330 | IN UINTN FileOffset,\r | |
1331 | IN OUT UINTN *ReadSize,\r | |
1332 | OUT VOID *Buffer\r | |
1333 | )\r | |
1334 | {\r | |
1335 | CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);\r | |
1336 | return RETURN_SUCCESS;\r | |
1337 | }\r | |
1338 | \r |