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.
5 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
6 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/DebugLib.h>
23 #include <IndustryStandard/PeImage.h>
25 #define PE_COFF_IMAGE_ALIGN_SIZE 4
28 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded
29 into system memory with the PE/COFF Loader Library functions.
31 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry
32 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then
33 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.
34 If Pe32Data is NULL, then ASSERT().
35 If EntryPoint is NULL, then ASSERT().
37 @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.
38 @param EntryPoint The pointer to entry point to the PE/COFF image to return.
40 @retval RETURN_SUCCESS EntryPoint was returned.
41 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.
46 PeCoffLoaderGetEntryPoint (
51 EFI_IMAGE_DOS_HEADER
*DosHdr
;
52 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
54 ASSERT (Pe32Data
!= NULL
);
55 ASSERT (EntryPoint
!= NULL
);
57 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
58 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
60 // DOS image header is present, so read the PE header after the DOS image header.
62 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
65 // DOS image header is not present, so PE header is at the image base.
67 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
71 // Calculate the entry point relative to the start of the image.
72 // AddressOfEntryPoint is common for PE32 & PE32+
74 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
75 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Hdr
.Te
->AddressOfEntryPoint
& 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
);
76 return RETURN_SUCCESS
;
77 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
78 *EntryPoint
= (VOID
*)((UINTN
)Pe32Data
+ (UINTN
)(Hdr
.Pe32
->OptionalHeader
.AddressOfEntryPoint
& 0x0ffffffff));
79 return RETURN_SUCCESS
;
82 return RETURN_UNSUPPORTED
;
87 Returns the machine type of a PE/COFF image.
89 Returns the machine type from the PE/COFF image specified by Pe32Data.
90 If Pe32Data is NULL, then ASSERT().
92 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
95 @return Machine type or zero if not a valid image.
100 PeCoffLoaderGetMachineType (
104 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
105 EFI_IMAGE_DOS_HEADER
*DosHdr
;
107 ASSERT (Pe32Data
!= NULL
);
109 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
110 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
112 // DOS image header is present, so read the PE header after the DOS image header.
114 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
117 // DOS image header is not present, so PE header is at the image base.
119 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
122 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
123 return Hdr
.Te
->Machine
;
124 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
125 return Hdr
.Pe32
->FileHeader
.Machine
;
132 Returns a pointer to the PDB file name for a PE/COFF image that has been
133 loaded into system memory with the PE/COFF Loader Library functions.
135 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
136 the PE/COFF image specified by Pe32Data is not a valid, then NULL is
137 returned. If the PE/COFF image specified by Pe32Data does not contain a
138 debug directory entry, then NULL is returned. If the debug directory entry
139 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
140 then NULL is returned.
141 If Pe32Data is NULL, then ASSERT().
143 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
146 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
147 if it cannot be retrieved.
152 PeCoffLoaderGetPdbPointer (
156 EFI_IMAGE_DOS_HEADER
*DosHdr
;
157 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
158 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
159 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
161 VOID
*CodeViewEntryPointer
;
163 UINT32 NumberOfRvaAndSizes
;
166 ASSERT (Pe32Data
!= NULL
);
169 DirectoryEntry
= NULL
;
171 NumberOfRvaAndSizes
= 0;
173 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
174 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
176 // DOS image header is present, so read the PE header after the DOS image header.
178 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
181 // DOS image header is not present, so PE header is at the image base.
183 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
186 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
187 if (Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) {
188 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
];
189 TEImageAdjust
= sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
190 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
) Hdr
.Te
+
191 Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
+
194 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
196 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
197 // It is due to backward-compatibility, for some system might
198 // generate PE32+ image with PE32 Magic.
200 switch (Hdr
.Pe32
->FileHeader
.Machine
) {
201 case IMAGE_FILE_MACHINE_I386
:
203 // Assume PE32 image with IA32 Machine field.
205 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
207 case IMAGE_FILE_MACHINE_X64
:
208 case IMAGE_FILE_MACHINE_IA64
:
210 // Assume PE32+ image with x64 or IA64 Machine field
212 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
216 // For unknow Machine field, use Magic in optional Header
218 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
221 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
223 // Use PE32 offset get Debug Directory Entry
225 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
226 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
227 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
228 } else if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
230 // Use PE32+ offset get Debug Directory Entry
232 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
233 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
234 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
237 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
238 DirectoryEntry
= NULL
;
245 if (DebugEntry
== NULL
|| DirectoryEntry
== NULL
) {
250 // Scan the directory to find the debug entry.
252 for (DirCount
= 0; DirCount
< DirectoryEntry
->Size
; DirCount
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
), DebugEntry
++) {
253 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
254 if (DebugEntry
->SizeOfData
> 0) {
255 CodeViewEntryPointer
= (VOID
*) ((UINTN
) DebugEntry
->RVA
+ ((UINTN
)Pe32Data
) + (UINTN
)TEImageAdjust
);
256 switch (* (UINT32
*) CodeViewEntryPointer
) {
257 case CODEVIEW_SIGNATURE_NB10
:
258 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
));
259 case CODEVIEW_SIGNATURE_RSDS
:
260 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
));
261 case CODEVIEW_SIGNATURE_MTOC
:
262 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
));
274 Returns the size of the PE/COFF headers
276 Returns the size of the PE/COFF header specified by Pe32Data.
277 If Pe32Data is NULL, then ASSERT().
279 @param Pe32Data The pointer to the PE/COFF image that is loaded in system
282 @return Size of PE/COFF header in bytes or zero if not a valid image.
287 PeCoffGetSizeOfHeaders (
291 EFI_IMAGE_DOS_HEADER
*DosHdr
;
292 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
295 ASSERT (Pe32Data
!= NULL
);
297 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
298 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
300 // DOS image header is present, so read the PE header after the DOS image header.
302 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
305 // DOS image header is not present, so PE header is at the image base.
307 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
310 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
311 SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
312 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
313 SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
318 return (UINT32
) SizeOfHeaders
;
322 Returns PE/COFF image base is loaded in system memory where the input address is in.
324 On DEBUG build, searches the PE/COFF image base forward the input address and
327 @param Address Address located in one PE/COFF image.
329 @retval 0 RELEASE build or cannot find the PE/COFF image base.
330 @retval others PE/COFF image base found.
335 PeCoffSearchImageBase (
344 EFI_IMAGE_DOS_HEADER
*DosHdr
;
345 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
350 Pe32Data
= Address
& ~(PE_COFF_IMAGE_ALIGN_SIZE
- 1);
351 while (Pe32Data
!= 0) {
352 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) Pe32Data
;
353 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
355 // DOS image header is present, so read the PE header after the DOS image header.
357 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
359 // Make sure PE header address does not overflow and is less than the initial address.
361 if (((UINTN
)Hdr
.Pe32
> Pe32Data
) && ((UINTN
)Hdr
.Pe32
< Address
)) {
362 if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
368 // DOS image header is not present, TE header is at the image base.
370 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
371 if ((Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) &&
372 ((Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_I386
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_IA64
) ||
373 (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_EBC
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_X64
) ||
374 (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_ARM64
) || (Hdr
.Te
->Machine
== IMAGE_FILE_MACHINE_ARMTHUMB_MIXED
))
381 // Not found the image base, check the previous aligned address
383 Pe32Data
-= PE_COFF_IMAGE_ALIGN_SIZE
;