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