]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Library/PeiUnixPeCoffExtraActionLib/PeiUnixPeCoffExtraActionLib.c
3b2af0301bf8b776ef10adbe67d9daf19feca7c3
[mirror_edk2.git] / UnixPkg / Library / PeiUnixPeCoffExtraActionLib / PeiUnixPeCoffExtraActionLib.c
1 /**@file
2
3 Copyright (c) 2006 - 2009, 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 PeiUnixPeCoffExtraActionLib.c
15
16 Abstract:
17
18 Provides services to perform additional actions to relocate and unload
19 PE/Coff image for UNIX environment specific purpose such as souce level debug.
20 This version only works for PEI phase
21
22
23 **/
24 #include <PiPei.h>
25 #include <Ppi/UnixThunk.h>
26
27 #include <Library/PeCoffLib.h>
28 #include <Library/PeiServicesLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/PeCoffExtraActionLib.h>
32
33 //
34 // Cache of UnixThunk protocol
35 //
36 EFI_UNIX_THUNK_PROTOCOL *mUnix = NULL;
37
38 /**
39 The function caches the pointer of the WinNT thunk functions
40 It will ASSERT() if Unix thunk ppi is not installed.
41
42 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 UnixPeCoffGetUnixThunkStucture (
48 )
49 {
50 PEI_UNIX_THUNK_PPI *UnixThunkPpi;
51 EFI_STATUS Status;
52
53
54 //
55 // Locate Unix ThunkPpi for retrieving standard output handle
56 //
57 Status = PeiServicesLocatePpi (
58 &gPeiUnixThunkPpiGuid,
59 0,
60 NULL,
61 (VOID **) &UnixThunkPpi
62 );
63
64 ASSERT_EFI_ERROR (Status);
65
66 mUnix = (EFI_UNIX_THUNK_PROTOCOL *) UnixThunkPpi->UnixThunk ();
67
68 return EFI_SUCCESS;
69 }
70
71 /**
72 Performs additional actions after a PE/COFF image has been loaded and relocated.
73
74 If ImageContext is NULL, then ASSERT().
75
76 @param ImageContext Pointer to the image context structure that describes the
77 PE/COFF image that has already been loaded and relocated.
78
79 **/
80 VOID
81 EFIAPI
82 PeCoffLoaderRelocateImageExtraAction (
83 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
84 )
85 {
86 VOID * Handle;
87 VOID * Entry;
88
89 ASSERT (ImageContext != NULL);
90
91 Handle = NULL;
92 Entry = NULL;
93
94 if (mUnix == NULL) {
95 UnixPeCoffGetUnixThunkStucture ();
96 }
97 DEBUG ((EFI_D_ERROR, "Loading %a 0x%08lx - entry point 0x%08lx\n",
98 ImageContext->PdbPointer,
99 (UINTN)ImageContext->ImageAddress,
100 (UINTN)ImageContext->EntryPoint));
101
102 Handle = mUnix->Dlopen (ImageContext->PdbPointer, RTLD_NOW);
103
104 if (Handle) {
105 Entry = mUnix->Dlsym(Handle, "_ModuleEntryPoint");
106 } else {
107 DEBUG ((EFI_D_ERROR, "%a\n", mUnix->Dlerror()));
108 }
109
110 if (Entry != NULL) {
111 ImageContext->EntryPoint = Entry;
112 DEBUG ((EFI_D_ERROR, "Change %a Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, Entry));
113 }
114
115
116 return;
117 }
118
119 /**
120 Performs additional actions just before a PE/COFF image is unloaded. Any resources
121 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
122
123 If ImageContext is NULL, then ASSERT().
124
125 @param ImageContext Pointer to the image context structure that describes the
126 PE/COFF image that is being unloaded.
127
128 **/
129 VOID
130 EFIAPI
131 PeCoffLoaderUnloadImageExtraAction (
132 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
133 )
134 {
135 ASSERT (ImageContext != NULL);
136 }