TianoCore EDK2 master
Loading...
Searching...
No Matches
PeCoffGetEntryPoint.c
Go to the documentation of this file.
1
11#include <Base.h>
12
14#include <Library/DebugLib.h>
15
17
18#define PE_COFF_IMAGE_ALIGN_SIZE 4
19
37RETURN_STATUS
38EFIAPI
40 IN VOID *Pe32Data,
41 OUT VOID **EntryPoint
42 )
43{
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
90UINT16
91EFIAPI
93 IN VOID *Pe32Data
94 )
95{
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
142VOID *
143EFIAPI
145 IN VOID *Pe32Data
146 )
147{
148 EFI_IMAGE_DOS_HEADER *DosHdr;
150 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
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 //
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 //
205 break;
206 default:
207 //
208 // For unknow Machine field, use Magic in optional Header
209 //
210 Magic = Hdr.Pe32->OptionalHeader.Magic;
211 }
212
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) {
250 return (VOID *)((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));
252 return (VOID *)((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));
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
277UINT32
278EFIAPI
280 IN VOID *Pe32Data
281 )
282{
283 EFI_IMAGE_DOS_HEADER *DosHdr;
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
325UINTN
326EFIAPI
328 IN UINTN Address
329 )
330{
331 UINTN Pe32Data;
332
333 Pe32Data = 0;
334
336 EFI_IMAGE_DOS_HEADER *DosHdr;
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
380
381 return Pe32Data;
382}
UINT64 UINTN
INT64 INTN
#define NULL
Definition: Base.h:319
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define RETURN_SUCCESS
Definition: Base.h:1066
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG_CODE_BEGIN()
Definition: DebugLib.h:564
#define DEBUG_CODE_END()
Definition: DebugLib.h:578
UINT32 EFIAPI PeCoffGetSizeOfHeaders(IN VOID *Pe32Data)
RETURN_STATUS EFIAPI PeCoffLoaderGetEntryPoint(IN VOID *Pe32Data, OUT VOID **EntryPoint)
UINT16 EFIAPI PeCoffLoaderGetMachineType(IN VOID *Pe32Data)
VOID *EFIAPI PeCoffLoaderGetPdbPointer(IN VOID *Pe32Data)
UINTN EFIAPI PeCoffSearchImageBase(IN UINTN Address)
#define CODEVIEW_SIGNATURE_NB10
Definition: PeImage.h:657
#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW
The Visual C++ debug information.
Definition: PeImage.h:651
#define CODEVIEW_SIGNATURE_RSDS
Definition: PeImage.h:671
#define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
Definition: PeImage.h:143
#define EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
Definition: PeImage.h:194
#define CODEVIEW_SIGNATURE_MTOC
Definition: PeImage.h:687
UINT32 RVA
The address of the debug data when loaded, relative to the image base.
Definition: PeImage.h:647
UINT32 e_lfanew
File address of new exe header.
Definition: PeImage.h:76
UINT16 e_magic
Magic number.
Definition: PeImage.h:58
UINT32 AddressOfEntryPoint
Offset to entry point – from original optional header.
Definition: PeImage.h:786
UINT16 Signature
The signature for TE format = "VZ".
Definition: PeImage.h:781
UINT32 BaseOfCode
From original image – required for ITP debug.
Definition: PeImage.h:787
EFI_IMAGE_DATA_DIRECTORY DataDirectory[2]
Only base relocation and debug directory.
Definition: PeImage.h:789
UINT16 StrippedSize
Number of bytes we removed from the header.
Definition: PeImage.h:785
UINT16 Machine
From the original file header.
Definition: PeImage.h:782