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