]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Library/PeiEmuPeCoffGetEntryPointLib/PeiEmuPeCoffGetEntryPointLib.c
EmulatorPkg: Fix Visual Studio build for IA32 & X64
[mirror_edk2.git] / EmulatorPkg / Library / PeiEmuPeCoffGetEntryPointLib / PeiEmuPeCoffGetEntryPointLib.c
CommitLineData
949f388f 1/*++ @file\r
2\r
e148512e 3Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
949f388f 4Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PiPei.h"\r
16#include <Library/PeCoffGetEntryPointLib.h>\r
17#include <Library/PeiServicesLib.h>\r
18#include <IndustryStandard/PeImage.h>\r
19#include <Library/DebugLib.h>\r
20\r
21#include <Ppi/EmuThunk.h>\r
22#include <Protocol/EmuThunk.h>\r
23\r
24\r
25\r
26/**\r
27 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded\r
28 into system memory with the PE/COFF Loader Library functions.\r
29\r
30 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry\r
31 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then\r
32 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.\r
33 If Pe32Data is NULL, then ASSERT().\r
34 If EntryPoint is NULL, then ASSERT().\r
35\r
36 @param Pe32Data The pointer to the PE/COFF image that is loaded in system memory.\r
37 @param EntryPoint The pointer to entry point to the PE/COFF image to return.\r
38\r
39 @retval RETURN_SUCCESS EntryPoint was returned.\r
40 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.\r
41\r
42**/\r
43RETURN_STATUS\r
44EFIAPI\r
45PeCoffLoaderGetEntryPoint (\r
46 IN VOID *Pe32Data,\r
47 IN OUT VOID **EntryPoint\r
48 )\r
49{\r
50 EMU_THUNK_PPI *ThunkPpi;\r
51 EFI_STATUS Status;\r
52 EMU_THUNK_PROTOCOL *Thunk;\r
53\r
54 //\r
55 // Locate EmuThunkPpi for retrieving standard output handle\r
56 //\r
57 Status = PeiServicesLocatePpi (\r
58 &gEmuThunkPpiGuid,\r
65e3f333 59 0,\r
949f388f 60 NULL,\r
61 (VOID **) &ThunkPpi\r
62 );\r
63 ASSERT_EFI_ERROR (Status);\r
64\r
65 Thunk = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();\r
66\r
67 return Thunk->PeCoffGetEntryPoint (Pe32Data, EntryPoint);\r
68}\r
69\r
70/**\r
d18d8a1d 71 Returns the machine type of PE/COFF image.\r
949f388f 72 This is copied from MDE BasePeCoffGetEntryPointLib, the code should be sync with it.\r
73 The reason is Emu package needs to load the image to memory to support source\r
74 level debug.\r
d18d8a1d 75\r
949f388f 76\r
77 @param Pe32Data Pointer to a PE/COFF header\r
78\r
79 @return Machine type or zero if not a valid iamge\r
80\r
81**/\r
82UINT16\r
83EFIAPI\r
84PeCoffLoaderGetMachineType (\r
85 IN VOID *Pe32Data\r
86 )\r
d18d8a1d 87{\r
949f388f 88 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
89 EFI_IMAGE_DOS_HEADER *DosHdr;\r
90\r
91 ASSERT (Pe32Data != NULL);\r
92\r
93 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
94 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
95 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
96\r
97 } else {\r
98 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data);\r
99 }\r
100\r
101 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
102 return Hdr.Te->Machine;\r
103 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
104 return Hdr.Pe32->FileHeader.Machine;\r
105 }\r
106\r
107 return 0x0000;\r
108}\r
109\r
110/**\r
111 Returns a pointer to the PDB file name for a PE/COFF image that has been\r
112 loaded into system memory with the PE/COFF Loader Library functions.\r
113\r
114 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If\r
115 the PE/COFF image specified by Pe32Data is not a valid, then NULL is\r
116 returned. If the PE/COFF image specified by Pe32Data does not contain a\r
117 debug directory entry, then NULL is returned. If the debug directory entry\r
118 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,\r
119 then NULL is returned.\r
120 If Pe32Data is NULL, then ASSERT().\r
121\r
122 @param Pe32Data Pointer to the PE/COFF image that is loaded in system\r
123 memory.\r
124\r
125 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL\r
126 if it cannot be retrieved.\r
127\r
128**/\r
129VOID *\r
130EFIAPI\r
131PeCoffLoaderGetPdbPointer (\r
132 IN VOID *Pe32Data\r
133 )\r
134{\r
135 EFI_IMAGE_DOS_HEADER *DosHdr;\r
136 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
137 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;\r
138 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;\r
139 UINTN DirCount;\r
140 VOID *CodeViewEntryPointer;\r
141 INTN TEImageAdjust;\r
142 UINT32 NumberOfRvaAndSizes;\r
143 UINT16 Magic;\r
144\r
145 ASSERT (Pe32Data != NULL);\r
146\r
147 TEImageAdjust = 0;\r
148 DirectoryEntry = NULL;\r
149 DebugEntry = NULL;\r
150 NumberOfRvaAndSizes = 0;\r
151\r
152 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
153 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
154 //\r
155 // DOS image header is present, so read the PE header after the DOS image header.\r
156 //\r
157 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
158 } else {\r
159 //\r
160 // DOS image header is not present, so PE header is at the image base.\r
161 //\r
162 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
163 }\r
164\r
165 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
166 if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {\r
167 DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];\r
168 TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r
169 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +\r
170 Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +\r
171 TEImageAdjust);\r
172 }\r
173 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
174 //\r
175 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.\r
176 // It is due to backward-compatibility, for some system might\r
177 // generate PE32+ image with PE32 Magic.\r
178 //\r
179 switch (Hdr.Pe32->FileHeader.Machine) {\r
180 case EFI_IMAGE_MACHINE_IA32:\r
181 //\r
182 // Assume PE32 image with IA32 Machine field.\r
183 //\r
184 Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r
185 break;\r
186 case EFI_IMAGE_MACHINE_X64:\r
187 case EFI_IMAGE_MACHINE_IA64:\r
188 //\r
189 // Assume PE32+ image with X64 or IA64 Machine field\r
190 //\r
191 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
192 break;\r
193 default:\r
194 //\r
195 // For unknow Machine field, use Magic in optional Header\r
196 //\r
197 Magic = Hdr.Pe32->OptionalHeader.Magic;\r
198 }\r
199\r
200 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
201 //\r
202 // Use PE32 offset get Debug Directory Entry\r
203 //\r
204 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
205 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
206 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r
207 } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
208 //\r
209 // Use PE32+ offset get Debug Directory Entry\r
210 //\r
211 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
212 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
213 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r
214 }\r
215\r
216 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r
217 DirectoryEntry = NULL;\r
218 DebugEntry = NULL;\r
219 }\r
220 } else {\r
221 return NULL;\r
222 }\r
223\r
224 if (DebugEntry == NULL || DirectoryEntry == NULL) {\r
225 return NULL;\r
226 }\r
227\r
228 for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY), DebugEntry++) {\r
229 if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r
230 if (DebugEntry->SizeOfData > 0) {\r
231 CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);\r
232 switch (* (UINT32 *) CodeViewEntryPointer) {\r
233 case CODEVIEW_SIGNATURE_NB10:\r
234 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));\r
235 case CODEVIEW_SIGNATURE_RSDS:\r
236 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));\r
237 case CODEVIEW_SIGNATURE_MTOC:\r
238 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY));\r
239 default:\r
240 break;\r
241 }\r
242 }\r
243 }\r
244 }\r
245\r
246 return NULL;\r
247}\r
248\r
249\r
250/**\r
251 Returns the size of the PE/COFF headers\r
252\r
253 Returns the size of the PE/COFF header specified by Pe32Data.\r
254 If Pe32Data is NULL, then ASSERT().\r
255\r
256 @param Pe32Data Pointer to the PE/COFF image that is loaded in system\r
257 memory.\r
258\r
259 @return Size of PE/COFF header in bytes or zero if not a valid image.\r
260\r
261**/\r
262UINT32\r
263EFIAPI\r
264PeCoffGetSizeOfHeaders (\r
265 IN VOID *Pe32Data\r
266 )\r
267{\r
268 EFI_IMAGE_DOS_HEADER *DosHdr;\r
269 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
270 UINTN SizeOfHeaders;\r
271\r
272 ASSERT (Pe32Data != NULL);\r
d18d8a1d 273\r
949f388f 274 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
275 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
276 //\r
277 // DOS image header is present, so read the PE header after the DOS image header.\r
278 //\r
279 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
280 } else {\r
281 //\r
282 // DOS image header is not present, so PE header is at the image base.\r
283 //\r
284 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
285 }\r
286\r
287 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
288 SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize;\r
289 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
290 SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r
291 } else {\r
292 SizeOfHeaders = 0;\r
293 }\r
294\r
e148512e 295 return (UINT32) SizeOfHeaders;\r
949f388f 296}\r
297\r