]>
Commit | Line | Data |
---|---|---|
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 | |
36 | RETURN_STATUS\r | |
37 | EFIAPI\r | |
38 | PeCoffLoaderGetEntryPoint (\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 | |
83 | UINT16\r | |
84 | EFIAPI\r | |
85 | PeCoffLoaderGetMachineType (\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 | |
125 | VOID *\r | |
126 | EFIAPI\r | |
127 | PeCoffLoaderGetPdbPointer (\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 | |
28dec501 | 139 | UINT16 Magic;\r |
0d49d8f6 | 140 | \r |
141 | ASSERT (Pe32Data != NULL);\r | |
142 | \r | |
143 | TEImageAdjust = 0;\r | |
144 | DirectoryEntry = NULL;\r | |
145 | DebugEntry = NULL;\r | |
146 | NumberOfRvaAndSizes = 0;\r | |
147 | \r | |
148 | DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
149 | if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
150 | //\r | |
151 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
152 | //\r | |
153 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHeader->e_lfanew) & 0x0ffff));\r | |
154 | } else {\r | |
155 | //\r | |
156 | // DOS image header is not present, so PE header is at the image base.\r | |
157 | //\r | |
158 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
159 | }\r | |
160 | \r | |
161 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
162 | if (Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {\r | |
163 | DirectoryEntry = &Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];\r | |
164 | TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r | |
165 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) Hdr.Te +\r | |
166 | Hdr.Te->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +\r | |
167 | TEImageAdjust);\r | |
168 | }\r | |
169 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
28dec501 | 170 | //\r |
171 | // NOTE: We use Machine field to identify PE32/PE32+, instead of Magic.\r | |
172 | // It is due to backward-compatibility, for some system might\r | |
173 | // generate PE32+ image with PE32 Magic.\r | |
174 | //\r | |
175 | switch (Hdr.Pe32->FileHeader.Machine) {\r | |
176 | case EFI_IMAGE_MACHINE_IA32:\r | |
177 | //\r | |
178 | // Assume PE32 image with IA32 Machine field.\r | |
179 | //\r | |
180 | Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r | |
181 | break;\r | |
182 | case EFI_IMAGE_MACHINE_X64:\r | |
183 | case EFI_IMAGE_MACHINE_IPF:\r | |
184 | //\r | |
185 | // Assume PE32+ image with X64 or IPF Machine field\r | |
186 | //\r | |
187 | Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r | |
188 | break;\r | |
189 | default:\r | |
190 | //\r | |
191 | // For unknow Machine field, use Magic in optional Header\r | |
192 | //\r | |
193 | Magic = Hdr.Pe32->OptionalHeader.Magic;\r | |
194 | }\r | |
195 | \r | |
196 | if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r | |
0d49d8f6 | 197 | // \r |
198 | // Use PE32 offset get Debug Directory Entry\r | |
199 | //\r | |
200 | NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r | |
201 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
202 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r | |
203 | } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r | |
204 | // \r | |
205 | // Use PE32+ offset get Debug Directory Entry\r | |
206 | //\r | |
207 | NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r | |
208 | DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r | |
209 | DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *) ((UINTN) Pe32Data + DirectoryEntry->VirtualAddress);\r | |
210 | }\r | |
211 | \r | |
212 | if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r | |
213 | DirectoryEntry = NULL;\r | |
214 | DebugEntry = NULL;\r | |
215 | }\r | |
216 | } else {\r | |
217 | return NULL;\r | |
218 | }\r | |
219 | \r | |
220 | if (DebugEntry == NULL || DirectoryEntry == NULL) {\r | |
221 | return NULL;\r | |
222 | }\r | |
223 | \r | |
224 | for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount++, DebugEntry++) {\r | |
225 | if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r | |
226 | if (DebugEntry->SizeOfData > 0) {\r | |
227 | CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + ((UINTN)Pe32Data) + (UINTN)TEImageAdjust);\r | |
228 | switch (* (UINT32 *) CodeViewEntryPointer) {\r | |
229 | case CODEVIEW_SIGNATURE_NB10:\r | |
230 | return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY));\r | |
231 | case CODEVIEW_SIGNATURE_RSDS:\r | |
232 | return (VOID *) ((CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY));\r | |
233 | default:\r | |
234 | break;\r | |
235 | }\r | |
236 | }\r | |
237 | }\r | |
238 | }\r | |
239 | \r | |
240 | return NULL;\r | |
241 | }\r | |
242 | \r | |
2ce31132 | 243 | \r |