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