]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/Library/DxeNt32PeCoffExtraActionLib/DxeNt32PeCoffExtraActionLib.c
Remove PeRemove PeiPeCoffLoader.h and gPeiPeCoffLoaderGuid, and Add PeCoffExtraAction...
[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 <FrameworkModuleDxe.h>
29 #include <WinNtDxe.h>
30
31 //
32 // The protocols, PPI and GUID defintions for this module
33 //
34 #include <Protocol/WinNtThunk.h>
35
36 #include <Library/PeCoffLib.h>
37 #include <Library/PeiServicesLib.h>
38
39 #include <Library/BaseLib.h>
40 #include <Library/DebugLib.h>
41 #include <Library/HobLib.h>
42 #include <Library/BaseMemoryLib.h>
43 #include <Library/PeCoffExtraActionLib.h>
44
45 #define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100
46
47 typedef struct {
48 CHAR8 *PdbPointer;
49 VOID *ModHandle;
50 } PDB_NAME_TO_MOD_HANDLE;
51
52
53 //
54 // Cache of WinNtThunk protocol
55 //
56 EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;
57
58 //
59 // An Array to hold the ModHandle
60 //
61 PDB_NAME_TO_MOD_HANDLE *mPdbNameModHandleArray = NULL;
62 UINTN mPdbNameModHandleArraySize = 0;
63
64
65 /**
66 The constructor function gets the pointer of the WinNT thunk functions
67 It will ASSERT() if NT thunk protocol is not installed.
68
69 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.
70
71 **/
72 EFI_STATUS
73 EFIAPI
74 DxeNt32PeCoffLibExtraActionConstructor (
75 IN EFI_HANDLE ImageHandle,
76 IN EFI_SYSTEM_TABLE *SystemTable
77 )
78 {
79 EFI_HOB_GUID_TYPE *GuidHob;
80
81 //
82 // Retrieve WinNtThunkProtocol from GUID'ed HOB
83 //
84 GuidHob = GetFirstGuidHob (&gEfiWinNtThunkProtocolGuid);
85 ASSERT (GuidHob != NULL);
86 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));
87 ASSERT (mWinNt != NULL);
88
89
90 return EFI_SUCCESS;
91 }
92
93 /**
94 Convert the passed in Ascii string to Unicode.
95
96 This function Convert the passed in Ascii string to Unicode.Optionally return
97 the length of the strings..
98
99 @param AsciiString Pointer to an AscII string
100 @param StrLen Length of string
101
102 @return Pointer to malloc'ed Unicode version of Ascii
103
104 **/
105 CHAR16 *
106 AsciiToUnicode (
107 IN CHAR8 *Ascii,
108 IN UINTN *StrLen OPTIONAL
109 )
110 {
111 UINTN Index;
112 CHAR16 *Unicode;
113
114 //
115 // Allocate a buffer for unicode string
116 //
117 for (Index = 0; Ascii[Index] != '\0'; Index++)
118 ;
119 Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),
120 HEAP_ZERO_MEMORY,
121 ((Index + 1) * sizeof (CHAR16))
122 );
123 if (Unicode == NULL) {
124 return NULL;
125 }
126
127 for (Index = 0; Ascii[Index] != '\0'; Index++) {
128 Unicode[Index] = (CHAR16) Ascii[Index];
129 }
130
131 Unicode[Index] = '\0';
132
133 if (StrLen != NULL) {
134 *StrLen = Index;
135 }
136
137 return Unicode;
138 }
139 /**
140 Store the ModHandle in an array indexed by the Pdb File name.
141 The ModHandle is needed to unload the image.
142
143
144 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
145 .PDB file name of the PE Image.
146 @param ModHandle - Returned from LoadLibraryEx() and stored for call to
147 FreeLibrary().
148
149 @return return EFI_SUCCESS when ModHandle was stored.
150
151 --*/
152 EFI_STATUS
153 AddModHandle (
154 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
155 IN VOID *ModHandle
156 )
157
158 {
159 UINTN Index;
160 PDB_NAME_TO_MOD_HANDLE *Array;
161 UINTN PreviousSize;
162 PDB_NAME_TO_MOD_HANDLE *TempArray;
163
164 Array = mPdbNameModHandleArray;
165 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
166 if (Array->PdbPointer == NULL) {
167 //
168 // Make a copy of the stirng and store the ModHandle
169 //
170 Array->PdbPointer = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),
171 HEAP_ZERO_MEMORY,
172 AsciiStrLen (ImageContext->PdbPointer) + 1
173 );
174
175 ASSERT (Array->PdbPointer != NULL);
176
177 AsciiStrCpy (Array->PdbPointer, ImageContext->PdbPointer);
178 Array->ModHandle = ModHandle;
179 return EFI_SUCCESS;
180 }
181 }
182
183 //
184 // No free space in mPdbNameModHandleArray so grow it by
185 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.
186 //
187 PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);
188 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;
189 //
190 // re-allocate a new buffer and copy the old values to the new locaiton.
191 //
192 TempArray = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),
193 HEAP_ZERO_MEMORY,
194 mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)
195 );
196
197 CopyMem ((VOID *) (UINTN) TempArray, (VOID *) (UINTN)mPdbNameModHandleArray, PreviousSize);
198
199 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, mPdbNameModHandleArray);
200
201 mPdbNameModHandleArray = TempArray;
202 if (mPdbNameModHandleArray == NULL) {
203 ASSERT (FALSE);
204 return EFI_OUT_OF_RESOURCES;
205 }
206
207
208 return AddModHandle (ImageContext, ModHandle);
209 }
210 /**
211 Return the ModHandle and delete the entry in the array.
212
213
214 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
215 .PDB file name of the PE Image.
216
217 @return
218 ModHandle - ModHandle assoicated with ImageContext is returned
219 NULL - No ModHandle associated with ImageContext
220
221 **/
222 VOID *
223 RemoveModeHandle (
224 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
225 )
226 {
227 UINTN Index;
228 PDB_NAME_TO_MOD_HANDLE *Array;
229
230 if (ImageContext->PdbPointer == NULL) {
231 //
232 // If no PDB pointer there is no ModHandle so return NULL
233 //
234 return NULL;
235 }
236
237 Array = mPdbNameModHandleArray;
238 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
239 if ((Array->PdbPointer != NULL) && (AsciiStrCmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {
240 //
241 // If you find a match return it and delete the entry
242 //
243 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, Array->PdbPointer);
244 Array->PdbPointer = NULL;
245 return Array->ModHandle;
246 }
247 }
248
249 return NULL;
250 }
251
252 /**
253 Applies additional actions to relocate fixups to a PE/COFF image.
254
255 Generally this function is called after sucessfully Applying relocation fixups
256 to a PE/COFF image for some specicial purpose.
257 As a example, For NT32 emulator, the function should be implemented and called
258 to support source level debug.
259
260 @param ImageContext Pointer to the image context structure that describes the PE/COFF
261 image that is being relocated.
262
263 **/
264 VOID
265 EFIAPI
266 PeCoffLoaderRelocateImageExtraAction (
267 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
268 )
269 {
270 VOID *DllEntryPoint;
271 CHAR16 *DllFileName;
272 HMODULE Library;
273 UINTN Index;
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 Unloads a loaded PE/COFF image from memory and releases its taken resource.
355
356 Releases any environment specific resources that were allocated when the image
357 specified by ImageContext was loaded using PeCoffLoaderLoadImage().
358 For NT32 emulator, the PE/COFF image loaded by system needs to release.
359 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
360
361 If ImageContext is NULL, then ASSERT().
362
363 @param ImageContext Pointer to the image context structure that describes the PE/COFF
364 image to be unloaded.
365
366 **/
367 VOID
368 EFIAPI
369 PeCoffLoaderUnloadImageExtraAction (
370 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
371 )
372 {
373 VOID *ModHandle;
374
375 ModHandle = RemoveModeHandle (ImageContext);
376 if (ModHandle != NULL) {
377 mWinNt->FreeLibrary (ModHandle);
378 }
379 return;
380 }