3 Copyright (c) 2006 - 2010, Intel Corporation
4 Portions copyright (c) 2008-2009 Apple Inc. All rights reserved.
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25 #include <Library/PeCoffGetEntryPointLib.h>
26 #include <Library/PeiServicesLib.h>
27 #include <Ppi/UnixPeiLoadFile.h>
28 #include <IndustryStandard/PeImage.h>
29 #include <Library/DebugLib.h>
33 PeCoffLoaderGetEntryPoint (
35 IN OUT VOID
**EntryPoint
41 Loads a PE/COFF image into memory, this is not follow the original purpose of
42 PeCoffGetEntryPoint library class. But it's ok that Unix package not run on a real
43 platform and this is for source level debug.
47 Pe32Data - Pointer to a PE/COFF Image
49 EntryPoint - Pointer to the entry point of the PE/COFF image
53 EFI_SUCCESS if the EntryPoint was returned
54 EFI_INVALID_PARAMETER if the EntryPoint could not be found from Pe32Data
58 PEI_UNIX_THUNK_PPI
*UnixThunkPpi
;
60 EFI_UNIX_THUNK_PROTOCOL
*Unix
;
63 // Locate Unix ThunkPpi for retrieving standard output handle
65 Status
= PeiServicesLocatePpi (
66 &gPeiUnixThunkPpiGuid
,
69 (VOID
**) &UnixThunkPpi
71 ASSERT_EFI_ERROR (Status
);
73 Unix
= (EFI_UNIX_THUNK_PROTOCOL
*)UnixThunkPpi
->UnixThunk ();
75 return Unix
->PeCoffGetEntryPoint (Pe32Data
, EntryPoint
);
79 Returns the machine type of PE/COFF image.
80 This is copied from MDE BasePeCoffGetEntryPointLib, the code should be sync with it.
81 The reason is Unix package needs to load the image to memory to support source
85 @param Pe32Data Pointer to a PE/COFF header
87 @return Machine type or zero if not a valid iamge
92 PeCoffLoaderGetMachineType (
96 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
97 EFI_IMAGE_DOS_HEADER
*DosHdr
;
99 ASSERT (Pe32Data
!= NULL
);
101 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
102 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
103 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
106 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(Pe32Data
);
109 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
110 return Hdr
.Te
->Machine
;
111 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
112 return Hdr
.Pe32
->FileHeader
.Machine
;
119 Returns a pointer to the PDB file name for a PE/COFF image that has been
120 loaded into system memory with the PE/COFF Loader Library functions.
122 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If
123 the PE/COFF image specified by Pe32Data is not a valid, then NULL is
124 returned. If the PE/COFF image specified by Pe32Data does not contain a
125 debug directory entry, then NULL is returned. If the debug directory entry
126 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,
127 then NULL is returned.
128 If Pe32Data is NULL, then ASSERT().
130 @param Pe32Data Pointer to the PE/COFF image that is loaded in system
133 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL
134 if it cannot be retrieved.
139 PeCoffLoaderGetPdbPointer (
143 EFI_IMAGE_DOS_HEADER
*DosHdr
;
144 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
145 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
146 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
148 VOID
*CodeViewEntryPointer
;
150 UINT32 NumberOfRvaAndSizes
;
153 ASSERT (Pe32Data
!= NULL
);
156 DirectoryEntry
= NULL
;
158 NumberOfRvaAndSizes
= 0;
160 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
161 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
163 // DOS image header is present, so read the PE header after the DOS image header.
165 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
168 // DOS image header is not present, so PE header is at the image base.
170 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
173 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
174 if (Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) {
175 DirectoryEntry
= &Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
];
176 TEImageAdjust
= sizeof (EFI_TE_IMAGE_HEADER
) - Hdr
.Te
->StrippedSize
;
177 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
) Hdr
.Te
+
178 Hdr
.Te
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
+
181 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
183 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.
184 // It is due to backward-compatibility, for some system might
185 // generate PE32+ image with PE32 Magic.
187 switch (Hdr
.Pe32
->FileHeader
.Machine
) {
188 case EFI_IMAGE_MACHINE_IA32
:
190 // Assume PE32 image with IA32 Machine field.
192 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
194 case EFI_IMAGE_MACHINE_X64
:
195 case EFI_IMAGE_MACHINE_IA64
:
197 // Assume PE32+ image with X64 or IA64 Machine field
199 Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
203 // For unknow Machine field, use Magic in optional Header
205 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
208 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
210 // Use PE32 offset get Debug Directory Entry
212 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
213 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
214 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
215 } else if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
217 // Use PE32+ offset get Debug Directory Entry
219 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
220 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
221 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
224 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
225 DirectoryEntry
= NULL
;
232 if (DebugEntry
== NULL
|| DirectoryEntry
== NULL
) {
236 for (DirCount
= 0; DirCount
< DirectoryEntry
->Size
; DirCount
+= sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
), DebugEntry
++) {
237 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
238 if (DebugEntry
->SizeOfData
> 0) {
239 CodeViewEntryPointer
= (VOID
*) ((UINTN
) DebugEntry
->RVA
+ ((UINTN
)Pe32Data
) + (UINTN
)TEImageAdjust
);
240 switch (* (UINT32
*) CodeViewEntryPointer
) {
241 case CODEVIEW_SIGNATURE_NB10
:
242 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
));
243 case CODEVIEW_SIGNATURE_RSDS
:
244 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
));
245 case CODEVIEW_SIGNATURE_MTOC
:
246 return (VOID
*) ((CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY
));
259 Returns the size of the PE/COFF headers
261 Returns the size of the PE/COFF header specified by Pe32Data.
262 If Pe32Data is NULL, then ASSERT().
264 @param Pe32Data Pointer to the PE/COFF image that is loaded in system
267 @return Size of PE/COFF header in bytes or zero if not a valid image.
272 PeCoffGetSizeOfHeaders (
276 EFI_IMAGE_DOS_HEADER
*DosHdr
;
277 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
280 ASSERT (Pe32Data
!= NULL
);
282 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
283 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
285 // DOS image header is present, so read the PE header after the DOS image header.
287 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
) Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
290 // DOS image header is not present, so PE header is at the image base.
292 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
295 if (Hdr
.Te
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
296 SizeOfHeaders
= sizeof (EFI_TE_IMAGE_HEADER
) + (UINTN
)Hdr
.Te
->BaseOfCode
- (UINTN
)Hdr
.Te
->StrippedSize
;
297 } else if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
298 SizeOfHeaders
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
303 return SizeOfHeaders
;