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