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