]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Provides the services to get the entry point to a PE/COFF image that has either been\r | |
3 | loaded into memory or is executing at it's linked address.\r | |
4 | \r | |
5 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
6 | Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r | |
7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
8 | \r | |
9 | **/\r | |
10 | \r | |
11 | \r | |
12 | #include <Base.h>\r | |
13 | \r | |
14 | #include <Library/PeCoffGetEntryPointLib.h>\r | |
15 | #include <Library/DebugLib.h>\r | |
16 | \r | |
17 | #include <IndustryStandard/PeImage.h>\r | |
18 | \r | |
19 | #define PE_COFF_IMAGE_ALIGN_SIZE 4\r | |
20 | \r | |
21 | /**\r | |
22 | Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded\r | |
23 | into system memory with the PE/COFF Loader Library functions.\r | |
24 | \r | |
25 | Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry\r | |
26 | point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then\r | |
27 | return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.\r | |
28 | If Pe32Data is NULL, then ASSERT().\r | |
29 | If EntryPoint is NULL, then ASSERT().\r | |
30 | \r | |
31 | @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.\r | |
32 | @param EntryPoint The pointer to entry point to the PE/COFF image to return.\r | |
33 | \r | |
34 | @retval RETURN_SUCCESS EntryPoint was returned.\r | |
35 | @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.\r | |
36 | \r | |
37 | **/\r | |
38 | RETURN_STATUS\r | |
39 | EFIAPI\r | |
40 | PeCoffLoaderGetEntryPoint (\r | |
41 | IN VOID *Pe32Data,\r | |
42 | OUT VOID **EntryPoint\r | |
43 | )\r | |
44 | {\r | |
45 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
46 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
47 | \r | |
48 | ASSERT (Pe32Data != NULL);\r | |
49 | ASSERT (EntryPoint != NULL);\r | |
50 | \r | |
51 | DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
52 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
53 | //\r | |
54 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
55 | //\r | |
56 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
57 | } else {\r | |
58 | //\r | |
59 | // DOS image header is not present, so PE header is at the image base.\r | |
60 | //\r | |
61 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
62 | }\r | |
63 | \r | |
64 | //\r | |
65 | // Calculate the entry point relative to the start of the image.\r | |
66 | // AddressOfEntryPoint is common for PE32 & PE32+\r | |
67 | //\r | |
68 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
69 | *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize);\r | |
70 | return RETURN_SUCCESS;\r | |
71 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
72 | *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Hdr.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));\r | |
73 | return RETURN_SUCCESS;\r | |
74 | }\r | |
75 | \r | |
76 | return RETURN_UNSUPPORTED;\r | |
77 | }\r | |
78 | \r | |
79 | \r | |
80 | /**\r | |
81 | Returns the machine type of a PE/COFF image.\r | |
82 | \r | |
83 | Returns the machine type from the PE/COFF image specified by Pe32Data.\r | |
84 | If Pe32Data is NULL, then ASSERT().\r | |
85 | \r | |
86 | @param Pe32Data The pointer to the PE/COFF image that is loaded in system\r | |
87 | memory.\r | |
88 | \r | |
89 | @return Machine type or zero if not a valid image.\r | |
90 | \r | |
91 | **/\r | |
92 | UINT16\r | |
93 | EFIAPI\r | |
94 | PeCoffLoaderGetMachineType (\r | |
95 | IN VOID *Pe32Data\r | |
96 | )\r | |
97 | {\r | |
98 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
99 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
100 | \r | |
101 | ASSERT (Pe32Data != NULL);\r | |
102 | \r | |
103 | DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
104 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
105 | //\r | |
106 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
107 | //\r | |
108 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
109 | } else {\r | |
110 | //\r | |
111 | // DOS image header is not present, so PE header is at the image base.\r | |
112 | //\r | |
113 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
114 | }\r | |
115 | \r | |
116 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
117 | return Hdr.Te->Machine;\r | |
118 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
119 | return Hdr.Pe32->FileHeader.Machine;\r | |
120 | }\r | |
121 | \r | |
122 | return 0x0000;\r | |
123 | }\r | |
124 | \r | |
125 | /**\r | |
126 | Returns a pointer to the PDB file name for a PE/COFF image that has been\r | |
127 | loaded into system memory with the PE/COFF Loader Library functions.\r | |
128 | \r | |
129 | Returns the PDB file name for the PE/COFF image specified by Pe32Data. If\r | |
130 | the PE/COFF image specified by Pe32Data is not a valid, then NULL is\r | |
131 | returned. If the PE/COFF image specified by Pe32Data does not contain a\r | |
132 | debug directory entry, then NULL is returned. If the debug directory entry\r | |
133 | in the PE/COFF image specified by Pe32Data does not contain a PDB file name,\r | |
134 | then NULL is returned.\r | |
135 | If Pe32Data is NULL, then ASSERT().\r | |
136 | \r | |
137 | @param Pe32Data The pointer to the PE/COFF image that is loaded in system\r | |
138 | memory.\r | |
139 | \r | |
140 | @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL\r | |
141 | if it cannot be retrieved.\r | |
142 | \r | |
143 | **/\r | |
144 | VOID *\r | |
145 | EFIAPI\r | |
146 | PeCoffLoaderGetPdbPointer (\r | |
147 | IN VOID *Pe32Data\r | |
148 | )\r | |
149 | {\r | |
150 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
151 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
152 | EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;\r | |
153 | EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;\r | |
154 | UINTN DirCount;\r | |
155 | VOID *CodeViewEntryPointer;\r | |
156 | INTN TEImageAdjust;\r | |
157 | UINT32 NumberOfRvaAndSizes;\r | |
158 | UINT16 Magic;\r | |
159 | \r | |
160 | ASSERT (Pe32Data != NULL);\r | |
161 | \r | |
162 | TEImageAdjust = 0;\r | |
163 | DirectoryEntry = NULL;\r | |
164 | DebugEntry = NULL;\r | |
165 | NumberOfRvaAndSizes = 0;\r | |
166 | \r | |
167 | DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
168 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
169 | //\r | |
170 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
171 | //\r | |
172 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
173 | } else {\r | |
174 | //\r | |
175 | // DOS image header is not present, so PE header is at the image base.\r | |
176 | //\r | |
177 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
178 | }\r | |
179 | \r | |
180 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
181 | if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {\r | |
182 | DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];\r | |
183 | TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r | |
184 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +\r | |
185 | Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +\r | |
186 | TEImageAdjust);\r | |
187 | }\r | |
188 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
189 | //\r | |
190 | // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.\r | |
191 | // It is due to backward-compatibility, for some system might\r | |
192 | // generate PE32+ image with PE32 Magic.\r | |
193 | //\r | |
194 | switch (Hdr.Pe32->FileHeader.Machine) {\r | |
195 | case IMAGE_FILE_MACHINE_I386:\r | |
196 | //\r | |
197 | // Assume PE32 image with IA32 Machine field.\r | |
198 | //\r | |
199 | Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r | |
200 | break;\r | |
201 | case IMAGE_FILE_MACHINE_X64:\r | |
202 | case IMAGE_FILE_MACHINE_IA64:\r | |
203 | //\r | |
204 | // Assume PE32+ image with x64 or IA64 Machine field\r | |
205 | //\r | |
206 | Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r | |
207 | break;\r | |
208 | default:\r | |
209 | //\r | |
210 | // For unknow Machine field, use Magic in optional Header\r | |
211 | //\r | |
212 | Magic = Hdr.Pe32->OptionalHeader.Magic;\r | |
213 | }\r | |
214 | \r | |
215 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
216 | //\r | |
217 | // Use PE32 offset get Debug Directory Entry\r | |
218 | //\r | |
219 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
220 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
221 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r | |
222 | } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r | |
223 | //\r | |
224 | // Use PE32+ offset get Debug Directory Entry\r | |
225 | //\r | |
226 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
227 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
228 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r | |
229 | }\r | |
230 | \r | |
231 | if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r | |
232 | DirectoryEntry = NULL;\r | |
233 | DebugEntry = NULL;\r | |
234 | }\r | |
235 | } else {\r | |
236 | return NULL;\r | |
237 | }\r | |
238 | \r | |
239 | if (DebugEntry == NULL || DirectoryEntry == NULL) {\r | |
240 | return NULL;\r | |
241 | }\r | |
242 | \r | |
243 | //\r | |
244 | // Scan the directory to find the debug entry.\r | |
245 | //\r | |
246 | for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {\r | |
247 | if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r | |
248 | if (DebugEntry->SizeOfData > 0) {\r | |
249 | CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);\r | |
250 | switch (* (UINT32 *) CodeViewEntryPointer) {\r | |
251 | case CODEVIEW_SIGNATURE_NB10:\r | |
252 | return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));\r | |
253 | case CODEVIEW_SIGNATURE_RSDS:\r | |
254 | return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));\r | |
255 | case CODEVIEW_SIGNATURE_MTOC:\r | |
256 | return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY));\r | |
257 | default:\r | |
258 | break;\r | |
259 | }\r | |
260 | }\r | |
261 | }\r | |
262 | }\r | |
263 | \r | |
264 | return NULL;\r | |
265 | }\r | |
266 | \r | |
267 | /**\r | |
268 | Returns the size of the PE/COFF headers\r | |
269 | \r | |
270 | Returns the size of the PE/COFF header specified by Pe32Data.\r | |
271 | If Pe32Data is NULL, then ASSERT().\r | |
272 | \r | |
273 | @param Pe32Data The pointer to the PE/COFF image that is loaded in system\r | |
274 | memory.\r | |
275 | \r | |
276 | @return Size of PE/COFF header in bytes or zero if not a valid image.\r | |
277 | \r | |
278 | **/\r | |
279 | UINT32\r | |
280 | EFIAPI\r | |
281 | PeCoffGetSizeOfHeaders (\r | |
282 | IN VOID *Pe32Data\r | |
283 | )\r | |
284 | {\r | |
285 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
286 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
287 | UINTN SizeOfHeaders;\r | |
288 | \r | |
289 | ASSERT (Pe32Data != NULL);\r | |
290 | \r | |
291 | DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
292 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
293 | //\r | |
294 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
295 | //\r | |
296 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
297 | } else {\r | |
298 | //\r | |
299 | // DOS image header is not present, so PE header is at the image base.\r | |
300 | //\r | |
301 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
302 | }\r | |
303 | \r | |
304 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
305 | SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;\r | |
306 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
307 | SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r | |
308 | } else {\r | |
309 | SizeOfHeaders = 0;\r | |
310 | }\r | |
311 | \r | |
312 | return (UINT32) SizeOfHeaders;\r | |
313 | }\r | |
314 | \r | |
315 | /**\r | |
316 | Returns PE/COFF image base is loaded in system memory where the input address is in.\r | |
317 | \r | |
318 | On DEBUG build, searches the PE/COFF image base forward the input address and\r | |
319 | returns it.\r | |
320 | \r | |
321 | @param Address Address located in one PE/COFF image.\r | |
322 | \r | |
323 | @retval 0 RELEASE build or cannot find the PE/COFF image base.\r | |
324 | @retval others PE/COFF image base found.\r | |
325 | \r | |
326 | **/\r | |
327 | UINTN\r | |
328 | EFIAPI\r | |
329 | PeCoffSearchImageBase (\r | |
330 | IN UINTN Address\r | |
331 | )\r | |
332 | {\r | |
333 | UINTN Pe32Data;\r | |
334 | \r | |
335 | Pe32Data = 0;\r | |
336 | \r | |
337 | DEBUG_CODE (\r | |
338 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
339 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
340 | \r | |
341 | //\r | |
342 | // Find Image Base\r | |
343 | //\r | |
344 | Pe32Data = Address & ~(PE_COFF_IMAGE_ALIGN_SIZE - 1);\r | |
345 | while (Pe32Data != 0) {\r | |
346 | DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;\r | |
347 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
348 | //\r | |
349 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
350 | //\r | |
351 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
352 | //\r | |
353 | // Make sure PE header address does not overflow and is less than the initial address.\r | |
354 | //\r | |
355 | if (((UINTN)Hdr.Pe32 > Pe32Data) && ((UINTN)Hdr.Pe32 < Address)) {\r | |
356 | if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
357 | break;\r | |
358 | }\r | |
359 | }\r | |
360 | } else {\r | |
361 | //\r | |
362 | // DOS image header is not present, TE header is at the image base.\r | |
363 | //\r | |
364 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
365 | if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&\r | |
366 | ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || (Hdr.Te->Machine == IMAGE_FILE_MACHINE_IA64) ||\r | |
367 | (Hdr.Te->Machine == IMAGE_FILE_MACHINE_EBC) || (Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64) ||\r | |
368 | (Hdr.Te->Machine == IMAGE_FILE_MACHINE_ARM64) || (Hdr.Te->Machine == IMAGE_FILE_MACHINE_ARMTHUMB_MIXED))\r | |
369 | ) {\r | |
370 | break;\r | |
371 | }\r | |
372 | }\r | |
373 | \r | |
374 | //\r | |
375 | // Not found the image base, check the previous aligned address\r | |
376 | //\r | |
377 | Pe32Data -= PE_COFF_IMAGE_ALIGN_SIZE;\r | |
378 | }\r | |
379 | );\r | |
380 | \r | |
381 | return Pe32Data;\r | |
382 | }\r |