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