]>
Commit | Line | Data |
---|---|---|
ffdd18bb | 1 | /**@file\r |
2 | \r | |
3 | Copyright (c) 2006, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | PeiNt32PeCoffExtraActionLib.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | Provides services to perform additional actions to relocate and unload\r | |
19 | PE/Coff image for NT32 environment specific purpose such as souce level debug.\r | |
20 | This version only works for DXE phase \r | |
21 | \r | |
22 | \r | |
23 | **/\r | |
24 | //\r | |
25 | // The package level header files this module uses\r | |
26 | //\r | |
27 | #include <FrameworkDxe.h>\r | |
ffdd18bb | 28 | #include <WinNtDxe.h>\r |
29 | \r | |
30 | //\r | |
31 | // The protocols, PPI and GUID defintions for this module\r | |
32 | //\r | |
33 | #include <Protocol/WinNtThunk.h>\r | |
34 | \r | |
35 | #include <Library/PeCoffLib.h>\r | |
36 | #include <Library/PeiServicesLib.h>\r | |
37 | \r | |
38 | #include <Library/BaseLib.h>\r | |
39 | #include <Library/DebugLib.h>\r | |
40 | #include <Library/HobLib.h>\r | |
41 | #include <Library/BaseMemoryLib.h>\r | |
42 | #include <Library/PeCoffExtraActionLib.h>\r | |
43 | \r | |
44 | #define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100\r | |
45 | \r | |
46 | typedef struct {\r | |
47 | CHAR8 *PdbPointer;\r | |
48 | VOID *ModHandle;\r | |
49 | } PDB_NAME_TO_MOD_HANDLE;\r | |
50 | \r | |
51 | \r | |
52 | //\r | |
53 | // Cache of WinNtThunk protocol\r | |
54 | //\r | |
55 | EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;\r | |
56 | \r | |
57 | //\r | |
58 | // An Array to hold the ModHandle\r | |
59 | //\r | |
60 | PDB_NAME_TO_MOD_HANDLE *mPdbNameModHandleArray = NULL;\r | |
61 | UINTN mPdbNameModHandleArraySize = 0;\r | |
62 | \r | |
63 | \r | |
64 | /**\r | |
65 | The constructor function gets the pointer of the WinNT thunk functions\r | |
66 | It will ASSERT() if NT thunk protocol is not installed.\r | |
67 | \r | |
68 | @retval EFI_SUCCESS WinNT thunk protocol is found and cached.\r | |
69 | \r | |
70 | **/\r | |
71 | EFI_STATUS\r | |
72 | EFIAPI\r | |
73 | DxeNt32PeCoffLibExtraActionConstructor (\r | |
74 | IN EFI_HANDLE ImageHandle,\r | |
75 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
76 | )\r | |
77 | {\r | |
78 | EFI_HOB_GUID_TYPE *GuidHob;\r | |
79 | \r | |
80 | //\r | |
81 | // Retrieve WinNtThunkProtocol from GUID'ed HOB\r | |
82 | //\r | |
83 | GuidHob = GetFirstGuidHob (&gEfiWinNtThunkProtocolGuid);\r | |
84 | ASSERT (GuidHob != NULL);\r | |
85 | mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));\r | |
86 | ASSERT (mWinNt != NULL);\r | |
87 | \r | |
88 | \r | |
89 | return EFI_SUCCESS;\r | |
90 | }\r | |
91 | \r | |
92 | /**\r | |
93 | Convert the passed in Ascii string to Unicode.\r | |
94 | \r | |
95 | This function Convert the passed in Ascii string to Unicode.Optionally return\r | |
96 | the length of the strings..\r | |
97 | \r | |
98 | @param AsciiString Pointer to an AscII string\r | |
99 | @param StrLen Length of string\r | |
100 | \r | |
101 | @return Pointer to malloc'ed Unicode version of Ascii\r | |
102 | \r | |
103 | **/\r | |
104 | CHAR16 *\r | |
105 | AsciiToUnicode (\r | |
106 | IN CHAR8 *Ascii,\r | |
107 | IN UINTN *StrLen OPTIONAL\r | |
108 | )\r | |
109 | {\r | |
110 | UINTN Index;\r | |
111 | CHAR16 *Unicode;\r | |
112 | \r | |
113 | //\r | |
114 | // Allocate a buffer for unicode string\r | |
115 | //\r | |
116 | for (Index = 0; Ascii[Index] != '\0'; Index++)\r | |
117 | ;\r | |
118 | Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r | |
119 | HEAP_ZERO_MEMORY,\r | |
120 | ((Index + 1) * sizeof (CHAR16))\r | |
121 | ); \r | |
122 | if (Unicode == NULL) {\r | |
123 | return NULL;\r | |
124 | }\r | |
125 | \r | |
126 | for (Index = 0; Ascii[Index] != '\0'; Index++) {\r | |
127 | Unicode[Index] = (CHAR16) Ascii[Index];\r | |
128 | }\r | |
129 | \r | |
130 | Unicode[Index] = '\0';\r | |
131 | \r | |
132 | if (StrLen != NULL) {\r | |
133 | *StrLen = Index;\r | |
134 | }\r | |
135 | \r | |
136 | return Unicode;\r | |
137 | }\r | |
138 | /**\r | |
139 | Store the ModHandle in an array indexed by the Pdb File name.\r | |
140 | The ModHandle is needed to unload the image. \r | |
141 | \r | |
142 | \r | |
143 | @param ImageContext - Input data returned from PE Laoder Library. Used to find the \r | |
144 | .PDB file name of the PE Image.\r | |
145 | @param ModHandle - Returned from LoadLibraryEx() and stored for call to \r | |
146 | FreeLibrary().\r | |
147 | \r | |
148 | @return return EFI_SUCCESS when ModHandle was stored. \r | |
149 | \r | |
150 | --*/\r | |
151 | EFI_STATUS\r | |
152 | AddModHandle (\r | |
153 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
154 | IN VOID *ModHandle\r | |
155 | )\r | |
156 | \r | |
157 | {\r | |
158 | UINTN Index;\r | |
159 | PDB_NAME_TO_MOD_HANDLE *Array;\r | |
160 | UINTN PreviousSize;\r | |
161 | PDB_NAME_TO_MOD_HANDLE *TempArray;\r | |
162 | \r | |
163 | Array = mPdbNameModHandleArray;\r | |
164 | for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r | |
165 | if (Array->PdbPointer == NULL) {\r | |
166 | //\r | |
167 | // Make a copy of the stirng and store the ModHandle\r | |
168 | //\r | |
169 | Array->PdbPointer = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r | |
170 | HEAP_ZERO_MEMORY,\r | |
171 | AsciiStrLen (ImageContext->PdbPointer) + 1\r | |
172 | ); \r | |
173 | \r | |
174 | ASSERT (Array->PdbPointer != NULL);\r | |
175 | \r | |
176 | AsciiStrCpy (Array->PdbPointer, ImageContext->PdbPointer);\r | |
177 | Array->ModHandle = ModHandle;\r | |
178 | return EFI_SUCCESS;\r | |
179 | }\r | |
180 | }\r | |
181 | \r | |
182 | //\r | |
183 | // No free space in mPdbNameModHandleArray so grow it by \r | |
184 | // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. \r | |
185 | //\r | |
186 | PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r | |
187 | mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r | |
188 | //\r | |
189 | // re-allocate a new buffer and copy the old values to the new locaiton. \r | |
190 | //\r | |
191 | TempArray = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r | |
192 | HEAP_ZERO_MEMORY,\r | |
193 | mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r | |
194 | ); \r | |
195 | \r | |
196 | CopyMem ((VOID *) (UINTN) TempArray, (VOID *) (UINTN)mPdbNameModHandleArray, PreviousSize);\r | |
197 | \r | |
198 | mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, mPdbNameModHandleArray);\r | |
199 | \r | |
200 | mPdbNameModHandleArray = TempArray;\r | |
201 | if (mPdbNameModHandleArray == NULL) {\r | |
202 | ASSERT (FALSE);\r | |
203 | return EFI_OUT_OF_RESOURCES;\r | |
204 | }\r | |
205 | \r | |
206 | \r | |
207 | return AddModHandle (ImageContext, ModHandle);\r | |
208 | }\r | |
209 | /**\r | |
210 | Return the ModHandle and delete the entry in the array.\r | |
211 | \r | |
212 | \r | |
213 | @param ImageContext - Input data returned from PE Laoder Library. Used to find the \r | |
214 | .PDB file name of the PE Image.\r | |
215 | \r | |
216 | @return \r | |
217 | ModHandle - ModHandle assoicated with ImageContext is returned\r | |
218 | NULL - No ModHandle associated with ImageContext\r | |
219 | \r | |
220 | **/\r | |
221 | VOID *\r | |
222 | RemoveModeHandle (\r | |
223 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
224 | )\r | |
225 | {\r | |
226 | UINTN Index;\r | |
227 | PDB_NAME_TO_MOD_HANDLE *Array;\r | |
228 | \r | |
229 | if (ImageContext->PdbPointer == NULL) {\r | |
230 | //\r | |
231 | // If no PDB pointer there is no ModHandle so return NULL\r | |
232 | //\r | |
233 | return NULL;\r | |
234 | }\r | |
235 | \r | |
236 | Array = mPdbNameModHandleArray;\r | |
237 | for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r | |
238 | if ((Array->PdbPointer != NULL) && (AsciiStrCmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r | |
239 | //\r | |
240 | // If you find a match return it and delete the entry\r | |
241 | //\r | |
242 | mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, Array->PdbPointer);\r | |
243 | Array->PdbPointer = NULL;\r | |
244 | return Array->ModHandle;\r | |
245 | }\r | |
246 | }\r | |
247 | \r | |
248 | return NULL;\r | |
249 | }\r | |
250 | \r | |
251 | /**\r | |
51f1a2b5 | 252 | Performs additional actions after a PE/COFF image has been loaded and relocated.\r |
ffdd18bb | 253 | \r |
51f1a2b5 | 254 | For NT32, this function load symbols to support source level debugging.\r |
255 | \r | |
256 | If ImageContext is NULL, then ASSERT().\r | |
257 | \r | |
258 | @param ImageContext Pointer to the image context structure that describes the\r | |
259 | PE/COFF image that has already been loaded and relocated.\r | |
ffdd18bb | 260 | \r |
261 | **/\r | |
262 | VOID\r | |
263 | EFIAPI\r | |
264 | PeCoffLoaderRelocateImageExtraAction (\r | |
265 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
266 | )\r | |
267 | {\r | |
268 | VOID *DllEntryPoint;\r | |
269 | CHAR16 *DllFileName;\r | |
270 | HMODULE Library;\r | |
271 | UINTN Index;\r | |
51f1a2b5 | 272 | \r |
273 | ASSERT (ImageContext != NULL);\r | |
274 | \r | |
ffdd18bb | 275 | //\r |
276 | // If we load our own PE COFF images the Windows debugger can not source\r | |
277 | // level debug our code. If a valid PDB pointer exists usw it to load\r | |
278 | // the *.dll file as a library using Windows* APIs. This allows \r | |
279 | // source level debug. The image is still loaded and reloaced\r | |
280 | // in the Framework memory space like on a real system (by the code above),\r | |
281 | // but the entry point points into the DLL loaded by the code bellow. \r | |
282 | //\r | |
283 | \r | |
284 | DllEntryPoint = NULL;\r | |
285 | \r | |
286 | //\r | |
287 | // Load the DLL if it's not an EBC image.\r | |
288 | //\r | |
289 | if ((ImageContext->PdbPointer != NULL) &&\r | |
290 | (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r | |
291 | //\r | |
292 | // Convert filename from ASCII to Unicode\r | |
293 | //\r | |
294 | DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r | |
295 | \r | |
296 | //\r | |
297 | // Check that we have a valid filename\r | |
298 | //\r | |
299 | if (Index < 5 || DllFileName[Index - 4] != '.') {\r | |
300 | mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r | |
301 | \r | |
302 | //\r | |
303 | // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r | |
304 | // The image will run, but we just can't source level debug. If we\r | |
305 | // return an error the image will not run.\r | |
306 | //\r | |
307 | return;\r | |
308 | }\r | |
309 | //\r | |
310 | // Replace .PDB with .DLL on the filename\r | |
311 | //\r | |
312 | DllFileName[Index - 3] = 'D';\r | |
313 | DllFileName[Index - 2] = 'L';\r | |
314 | DllFileName[Index - 1] = 'L';\r | |
315 | \r | |
316 | //\r | |
317 | // Load the .DLL file into the user process's address space for source \r | |
318 | // level debug\r | |
319 | //\r | |
320 | Library = mWinNt->LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r | |
321 | if (Library != NULL) {\r | |
322 | //\r | |
323 | // InitializeDriver is the entry point we put in all our EFI DLL's. The\r | |
324 | // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r | |
325 | // normal DLL entry point of DllMain, and prevents other modules that are\r | |
326 | // referenced in side the DllFileName from being loaded. There is no error \r | |
327 | // checking as the we can point to the PE32 image loaded by Tiano. This \r | |
328 | // step is only needed for source level debuging\r | |
329 | //\r | |
330 | DllEntryPoint = (VOID *) (UINTN) mWinNt->GetProcAddress (Library, "InitializeDriver");\r | |
331 | \r | |
332 | }\r | |
333 | \r | |
334 | if ((Library != NULL) && (DllEntryPoint != NULL)) {\r | |
335 | AddModHandle (ImageContext, Library);\r | |
336 | ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r | |
337 | DEBUG ((EFI_D_INFO, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName));\r | |
338 | } else {\r | |
339 | DEBUG ((EFI_D_ERROR, "WARNING: No source level debug %s. \n", DllFileName));\r | |
340 | }\r | |
341 | \r | |
342 | mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r | |
343 | }\r | |
344 | \r | |
345 | //\r | |
346 | // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r | |
347 | // The image will run, but we just can't source level debug. If we\r | |
348 | // return an error the image will not run.\r | |
349 | //\r | |
350 | return;\r | |
351 | } \r | |
352 | \r | |
353 | /**\r | |
51f1a2b5 | 354 | Performs additional actions just before a PE/COFF image is unloaded. Any resources\r |
355 | that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r | |
ffdd18bb | 356 | \r |
51f1a2b5 | 357 | For NT32, this function unloads symbols for source level debugging.\r |
358 | \r | |
ffdd18bb | 359 | If ImageContext is NULL, then ASSERT().\r |
360 | \r | |
51f1a2b5 | 361 | @param ImageContext Pointer to the image context structure that describes the\r |
362 | PE/COFF image that is being unloaded.\r | |
ffdd18bb | 363 | \r |
364 | **/\r | |
365 | VOID\r | |
366 | EFIAPI\r | |
367 | PeCoffLoaderUnloadImageExtraAction (\r | |
368 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
369 | )\r | |
370 | {\r | |
371 | VOID *ModHandle;\r | |
372 | \r | |
51f1a2b5 | 373 | ASSERT (ImageContext != NULL);\r |
374 | \r | |
ffdd18bb | 375 | ModHandle = RemoveModeHandle (ImageContext);\r |
376 | if (ModHandle != NULL) {\r | |
377 | mWinNt->FreeLibrary (ModHandle);\r | |
378 | }\r | |
379 | return;\r | |
ee31b443 | 380 | }\r |