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