]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/Library/PeiNt32PeCoffExtraActionLib/PeiNt32PeCoffExtraActionLib.c
Sync with PE/COFF Extra Action library class comments and add ASSERT() conditions...
[mirror_edk2.git] / Nt32Pkg / Library / PeiNt32PeCoffExtraActionLib / PeiNt32PeCoffExtraActionLib.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 PEI phase
21
22
23 **/
24 //
25 // The package level header files this module uses
26 //
27 #include <FrameworkPei.h>
28 #include <FrameworkModulePei.h>
29 #include <WinNtPeim.h>
30
31 //
32 // The protocols, PPI and GUID defintions for this module
33 //
34 #include <Ppi/NtThunk.h>
35
36 #include <PiPei.h>
37 #include <Library/PeCoffLib.h>
38 #include <Library/PeiServicesLib.h>
39 #include <Library/DebugLib.h>
40 #include <Library/BaseLib.h>
41 #include <Library/PeCoffExtraActionLib.h>
42
43 //
44 // Cache of WinNtThunk protocol
45 //
46 EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;
47
48 /**
49 The function caches the pointer of the WinNT thunk functions
50 It will ASSERT() if NT thunk ppi is not installed.
51
52 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.
53
54 **/
55 EFI_STATUS
56 EFIAPI
57 Nt32PeCoffGetWinNtThunkStucture (
58 )
59 {
60 PEI_NT_THUNK_PPI *NtThunkPpi;
61 EFI_STATUS Status;
62
63
64 //
65 // Locate NtThunkPpi for retrieving standard output handle
66 //
67 Status = PeiServicesLocatePpi (
68 &gPeiNtThunkPpiGuid,
69 0,
70 NULL,
71 (VOID **) &NtThunkPpi
72 );
73
74 ASSERT_EFI_ERROR (Status);
75
76 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *) NtThunkPpi->NtThunk ();
77
78 return EFI_SUCCESS;
79 }
80
81 /**
82 Convert the passed in Ascii string to Unicode.
83
84 This function Convert the passed in Ascii string to Unicode.Optionally return
85 the length of the strings..
86
87 @param AsciiString Pointer to an AscII string
88 @param StrLen Length of string
89
90 @return Pointer to malloc'ed Unicode version of Ascii
91
92 **/
93 CHAR16 *
94 AsciiToUnicode (
95 IN CHAR8 *Ascii,
96 IN UINTN *StrLen OPTIONAL
97 )
98 {
99 UINTN Index;
100 CHAR16 *Unicode;
101
102 //
103 // Allocate a buffer for unicode string
104 //
105 for (Index = 0; Ascii[Index] != '\0'; Index++)
106 ;
107 Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),
108 HEAP_ZERO_MEMORY,
109 ((Index + 1) * sizeof (CHAR16))
110 );
111 if (Unicode == NULL) {
112 return NULL;
113 }
114
115 for (Index = 0; Ascii[Index] != '\0'; Index++) {
116 Unicode[Index] = (CHAR16) Ascii[Index];
117 }
118
119 Unicode[Index] = '\0';
120
121 if (StrLen != NULL) {
122 *StrLen = Index;
123 }
124
125 return Unicode;
126 }
127
128 /**
129 Performs additional actions after a PE/COFF image has been loaded and relocated.
130
131 For NT32, this function load symbols to support source level debugging.
132
133 If ImageContext is NULL, then ASSERT().
134
135 @param ImageContext Pointer to the image context structure that describes the
136 PE/COFF image that has already been loaded and relocated.
137
138 **/
139 VOID
140 EFIAPI
141 PeCoffLoaderRelocateImageExtraAction (
142 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
143 )
144 {
145 VOID *DllEntryPoint;
146 CHAR16 *DllFileName;
147 HMODULE Library;
148 UINTN Index;
149
150 ASSERT (ImageContext != NULL);
151
152 if (mWinNt == NULL) {
153 Nt32PeCoffGetWinNtThunkStucture ();
154 }
155 //
156 // If we load our own PE COFF images the Windows debugger can not source
157 // level debug our code. If a valid PDB pointer exists usw it to load
158 // the *.dll file as a library using Windows* APIs. This allows
159 // source level debug. The image is still loaded and reloaced
160 // in the Framework memory space like on a real system (by the code above),
161 // but the entry point points into the DLL loaded by the code bellow.
162 //
163
164 DllEntryPoint = NULL;
165
166 //
167 // Load the DLL if it's not an EBC image.
168 //
169 if ((ImageContext->PdbPointer != NULL) &&
170 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {
171 //
172 // Convert filename from ASCII to Unicode
173 //
174 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);
175
176 //
177 // Check that we have a valid filename
178 //
179 if (Index < 5 || DllFileName[Index - 4] != '.') {
180 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);
181
182 //
183 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
184 // The image will run, but we just can't source level debug. If we
185 // return an error the image will not run.
186 //
187 return;
188 }
189 //
190 // Replace .PDB with .DLL on the filename
191 //
192 DllFileName[Index - 3] = 'D';
193 DllFileName[Index - 2] = 'L';
194 DllFileName[Index - 1] = 'L';
195
196 //
197 // Load the .DLL file into the user process's address space for source
198 // level debug
199 //
200 Library = mWinNt->LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);
201 if (Library != NULL) {
202 //
203 // InitializeDriver is the entry point we put in all our EFI DLL's. The
204 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
205 // normal DLL entry point of DllMain, and prevents other modules that are
206 // referenced in side the DllFileName from being loaded. There is no error
207 // checking as the we can point to the PE32 image loaded by Tiano. This
208 // step is only needed for source level debuging
209 //
210 DllEntryPoint = (VOID *) (UINTN) mWinNt->GetProcAddress (Library, "InitializeDriver");
211
212 }
213
214 if ((Library != NULL) && (DllEntryPoint != NULL)) {
215 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;
216 DEBUG ((EFI_D_INFO, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName));
217 } else {
218 DEBUG ((EFI_D_ERROR, "WARNING: No source level debug %s. \n", DllFileName));
219 }
220
221 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);
222 }
223
224 //
225 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
226 // The image will run, but we just can't source level debug. If we
227 // return an error the image will not run.
228 //
229 return;
230 }
231
232 /**
233 Performs additional actions just before a PE/COFF image is unloaded. Any resources
234 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
235
236 For NT32, this function unloads symbols for source level debugging.
237
238 If ImageContext is NULL, then ASSERT().
239
240 @param ImageContext Pointer to the image context structure that describes the
241 PE/COFF image that is being unloaded.
242
243 **/
244 VOID
245 EFIAPI
246 PeCoffLoaderUnloadImageExtraAction (
247 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
248 )
249 {
250 ASSERT (ImageContext != NULL);
251 }