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