]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BasePeCoffGetEntryPointLib/PeCoffGetEntryPoint.c
Use PeiPiLib's PeiPiLibBuildPiFvInfoPpi interface instead of installing EFI_PEI_FIRM...
[mirror_edk2.git] / MdePkg / Library / BasePeCoffGetEntryPointLib / PeCoffGetEntryPoint.c
CommitLineData
e1f414b6 1/** @file\r
2 Tiano PE/COFF loader.\r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
e1f414b6 13**/\r
14\r
c892d846 15\r
c7d265a9 16#include <Base.h>\r
c892d846 17\r
18\r
c7d265a9 19#include <Library/PeCoffGetEntryPointLib.h>\r
20#include <Library/DebugLib.h>\r
21\r
22#include <IndustryStandard/PeImage.h>\r
e1f414b6 23\r
24/**\r
25 Retrieves and returns a pointer to the entry point to a PE/COFF image that has been loaded\r
26 into system memory with the PE/COFF Loader Library functions.\r
27\r
28 Retrieves the entry point to the PE/COFF image specified by Pe32Data and returns this entry\r
29 point in EntryPoint. If the entry point could not be retrieved from the PE/COFF image, then\r
30 return RETURN_INVALID_PARAMETER. Otherwise return RETURN_SUCCESS.\r
31 If Pe32Data is NULL, then ASSERT().\r
32 If EntryPoint is NULL, then ASSERT().\r
33\r
34 @param Pe32Data Pointer to the PE/COFF image that is loaded in system memory.\r
35 @param EntryPoint Pointer to entry point to the PE/COFF image to return.\r
36\r
37 @retval RETURN_SUCCESS EntryPoint was returned.\r
38 @retval RETURN_INVALID_PARAMETER The entry point could not be found in the PE/COFF image.\r
39\r
40**/\r
41RETURN_STATUS\r
42EFIAPI\r
43PeCoffLoaderGetEntryPoint (\r
44 IN VOID *Pe32Data,\r
45 OUT VOID **EntryPoint\r
46 )\r
47{\r
48 EFI_IMAGE_DOS_HEADER *DosHeader;\r
49 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header;\r
50\r
51 ASSERT (Pe32Data != NULL);\r
52 ASSERT (EntryPoint != NULL);\r
53\r
54 DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
55 if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
56 //\r
57 // DOS image header is present, so read the PE header after the DOS image header.\r
58 //\r
59 Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff));\r
60 } else {\r
61 //\r
62 // DOS image header is not present, so PE header is at the image base.\r
63 //\r
64 Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
65 }\r
66\r
67 //\r
68 // Calculate the entry point relative to the start of the image.\r
69 // AddressOfEntryPoint is common for PE32 & PE32+\r
70 //\r
71 *EntryPoint = (VOID *)((UINTN)Pe32Data + (UINTN)(Header.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff));\r
72 return RETURN_SUCCESS;\r
73}\r
74\r
75\r
76/**\r
77 Returns the machine type of a PE/COFF image.\r
78\r
79 Returns the machine type from the PE/COFF image specified by Pe32Data.\r
80 If Pe32Data is NULL, then ASSERT().\r
81\r
82 @param Pe32Data Pointer to the PE/COFF image that is loaded in system\r
83 memory.\r
84\r
85 @return Machine type or zero if not a valid iamge.\r
86\r
87**/\r
88UINT16\r
89EFIAPI\r
90PeCoffLoaderGetMachineType (\r
91 IN VOID *Pe32Data\r
92 )\r
93{\r
94 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
95 EFI_IMAGE_DOS_HEADER *DosHdr;\r
96\r
97 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
98 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
99 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + DosHdr->e_lfanew);\r
100 } else {\r
101 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data);\r
102 }\r
103\r
104 if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
105 return Hdr.Pe32->FileHeader.Machine;\r
106 }\r
107\r
108 return 0x0000;\r
109}\r
110\r
111/**\r
112 Returns a pointer to the PDB file name for a PE/COFF image that has been\r
113 loaded into system memory with the PE/COFF Loader Library functions.\r
114\r
115 Returns the PDB file name for the PE/COFF image specified by Pe32Data. If\r
116 the PE/COFF image specified by Pe32Data is not a valid, then NULL is\r
117 returned. If the PE/COFF image specified by Pe32Data does not contain a\r
118 debug directory entry, then NULL is returned. If the debug directory entry\r
119 in the PE/COFF image specified by Pe32Data does not contain a PDB file name,\r
120 then NULL is returned.\r
121 If Pe32Data is NULL, then ASSERT().\r
122\r
123 @param Pe32Data Pointer to the PE/COFF image that is loaded in system\r
124 memory.\r
125\r
126 @return The PDB file name for the PE/COFF image specified by Pe32Data or NULL\r
127 if it cannot be retrieved.\r
128\r
129**/\r
130VOID *\r
131EFIAPI\r
132PeCoffLoaderGetPdbPointer (\r
133 IN VOID *Pe32Data\r
134 )\r
135{\r
136 EFI_IMAGE_DOS_HEADER *DosHeader;\r
137 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
138 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;\r
139 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;\r
140 UINTN DirCount;\r
141 VOID *CodeViewEntryPointer;\r
142 INTN TEImageAdjust;\r
143 UINT32 NumberOfRvaAndSizes;\r
144 UINT16 Magic;\r
145\r
146 ASSERT (Pe32Data != NULL);\r
147\r
148 TEImageAdjust = 0;\r
149 DirectoryEntry = NULL;\r
150 DebugEntry = NULL;\r
151 NumberOfRvaAndSizes = 0;\r
152\r
153 DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
154 if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
155 //\r
156 // DOS image header is present, so read the PE header after the DOS image header.\r
157 //\r
158 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff));\r
159 } else {\r
160 //\r
161 // DOS image header is not present, so PE header is at the image base.\r
162 //\r
163 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
164 }\r
165\r
166 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
167 if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {\r
168 DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];\r
169 TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r
170 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +\r
171 Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +\r
172 TEImageAdjust);\r
173 }\r
174 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
175 //\r
176 // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.\r
177 // It is due to backward-compatibility, for some system might\r
178 // generate PE32+ image with PE32 Magic.\r
179 //\r
180 switch (Hdr.Pe32->FileHeader.Machine) {\r
181 case EFI_IMAGE_MACHINE_IA32:\r
182 //\r
183 // Assume PE32 image with IA32 Machine field.\r
184 //\r
185 Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r
186 break;\r
187 case EFI_IMAGE_MACHINE_X64:\r
188 case EFI_IMAGE_MACHINE_IPF:\r
189 //\r
190 // Assume PE32+ image with X64 or IPF Machine field\r
191 //\r
192 Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
193 break;\r
194 default:\r
195 //\r
196 // For unknow Machine field, use Magic in optional Header\r
197 //\r
198 Magic = Hdr.Pe32->OptionalHeader.Magic;\r
199 }\r
200\r
201 if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
202 //\r
203 // Use PE32 offset get Debug Directory Entry\r
204 //\r
205 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
206 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
207 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r
208 } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
209 //\r
210 // Use PE32+ offset get Debug Directory Entry\r
211 //\r
212 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
213 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
214 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r
215 }\r
216\r
217 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r
218 DirectoryEntry = NULL;\r
219 DebugEntry = NULL;\r
220 }\r
221 } else {\r
222 return NULL;\r
223 }\r
224\r
225 if (DebugEntry == NULL || DirectoryEntry == NULL) {\r
226 return NULL;\r
227 }\r
228\r
229 for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount++, DebugEntry++) {\r
230 if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r
231 if (DebugEntry->SizeOfData > 0) {\r
232 CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);\r
233 switch (* (UINT32 *) CodeViewEntryPointer) {\r
234 case CODEVIEW_SIGNATURE_NB10:\r
235 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));\r
236 case CODEVIEW_SIGNATURE_RSDS:\r
237 return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));\r
238 default:\r
239 break;\r
240 }\r
241 }\r
242 }\r
243 }\r
244\r
245 return NULL;\r
246}\r
247\r
248\r