OSDN Git Service

2018/11/02(Fri) 21:34
[drdeamon64/drdeamon64.git] / libgoblin / drd64_libgoblin_api.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBGOBLIN_API
38 #include"drd64_libgoblin.h"
39
40
41 /***********************************************************************
42 ***********************************************************************/
43 LIBGOBLIN_API
44 int
45         LibGoblin_AnalyzeProgram(
46                 int             i_pgid )
47 {
48         int             i_result;
49         LibGoblin_ProgramInfo   *p_pginfo;
50
51         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
52         if( NULL == p_pginfo )  {
53                 return -0x01;
54         }
55
56         i_result        = ELF_ReadELF_Phase2( p_pginfo->p_binfo[LIBGOBLIN_BINFO_FILE_EXEC] );
57         if( 0x00 != i_result )  {
58                 return -0x02;
59         }
60
61         return  i_result;
62 }
63
64
65 /***********************************************************************
66 ***********************************************************************/
67 LIBGOBLIN_API
68 int
69         LibGoblin_GetUnresolv_ObjectFilePath(
70                 int             i_pgid,
71                 LibGoblin_ObjectFilePath *p_objpath )
72 {
73         int             i_result;
74         LibGoblin_ProgramInfo   *p_pginfo;
75         LibGoblin_BinaryInfo    *p_binfo;
76         LibGoblin_BinaryInfo    *p_binfo_parent;
77         LibGoblin_BinaryFile    *p_bfile;
78
79         if( NULL == p_objpath ) {
80                 return -0x01;
81         }
82
83         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
84         if( NULL == p_pginfo )  {
85                 return -0x02;
86         }
87
88         p_binfo = p_pginfo->p_binfo[LIBGOBLIN_BINFO_FILE_EXEC];
89         while( NULL != p_binfo )        {
90                 if( LIBGOBLIN_BINFO_PHASE_SETPROG > p_binfo->b_phase )
91                         { break; }
92                 p_binfo = BinaryInfo_GetBinInfo( p_binfo->i_binfo_next );
93         }
94
95         i_result        = 0x00;
96         if( NULL != p_binfo )   {
97                 memset( p_objpath, 0x00, sizeof( LibGoblin_ObjectFilePath ) );
98
99                 p_objpath->i_bid        = p_binfo->i_id;
100
101                 strncpy( p_objpath->str_filename, p_binfo->str_filename, DRD64_MAX_PATH );
102
103                 if( -1 < p_binfo->i_parent_bid )        {
104                         p_binfo_parent  = BinaryInfo_GetBinInfo( p_binfo->i_parent_bid );
105                         p_bfile = BinaryFile_GetBinaryFile( p_binfo_parent->i_binfile );
106                         if( NULL != p_bfile )
107                                 { strncpy( p_objpath->str_rpath, p_bfile->str_rpath, DRD64_MAX_PATH ); }
108                 }
109
110                 i_result        = 0x01;
111         }
112
113         return i_result;
114 }
115
116
117 /***********************************************************************
118 ***********************************************************************/
119 LIBGOBLIN_API
120 int
121         LibGoblin_SetPathAndLoad_ObjectFilePath(
122                 LibGoblin_ObjectFilePath        *p_objpath )
123 {
124         int             i_result;
125         LibGoblin_BinaryInfo    *p_binfo;
126
127         p_binfo = BinaryInfo_GetBinInfo( p_objpath->i_bid );
128         if( NULL == p_binfo )   {
129                 return -0x01;
130         }
131
132         i_result        = strncmp( p_objpath->str_filename,
133                                                 p_binfo->str_filename, DRD64_MAX_PATH );
134         if( 0x00 != i_result )  {
135                 return -0x02;
136         }
137         
138         i_result        = BinaryInfo_SetProgramPath(
139                                                 p_binfo, p_objpath->str_remotepath,
140                                                 p_objpath->str_localpath );
141         if( 0x00 != i_result )  {
142                 return -0x03;
143         }
144
145         i_result        = LoadProg_LoadBinaryInfo( p_binfo );
146         if( 0x00 != i_result )  {
147                 return -0x04;
148         }
149
150         return 0x00;
151 }
152
153
154
155 /***********************************************************************
156 ***********************************************************************/
157 LIBGOBLIN_API
158 int
159         LibGoblin_LoadProgram_Main(
160                 int             i_pgid,
161                 const char      *pstr_remotefull,
162                 const char      *pstr_localfull )
163 {
164         int             i_result;
165         LibGoblin_ProgramInfo   *p_pginfo;
166
167     p_pginfo    = ProgInfo_GetProgInfo( i_pgid );
168         if( NULL == p_pginfo )  {
169                 return -0x01;
170         }
171
172         if( NULL == pstr_localfull )    {
173                 return -0x02;
174         }
175
176         i_result        = LoadProgram_LoadMainProgram(
177                                                 p_pginfo, pstr_remotefull, pstr_localfull );
178         if( NULL == pstr_localfull )    {
179                 return -0x03;
180         }
181
182         return  i_result;
183 }
184
185
186 /***********************************************************************
187 ***********************************************************************/
188 LIBGOBLIN_API
189 int
190         LibGoblin_AllocBinInfo(
191                 int i_pgid,
192                 const char *pstr_filename )
193 {
194         LibGoblin_ProgramInfo   *p_pginfo       = NULL;
195         LibGoblin_BinaryInfo    *p_binfo        = NULL;
196
197         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
198
199         p_binfo = BinaryInfo_AllocBinInfo( p_pginfo, pstr_filename, -1 );
200         if( NULL == p_binfo )   {
201                 return -0x01;
202         }
203
204         return p_binfo->i_id;
205 }
206
207
208 /***********************************************************************
209 ***********************************************************************/
210 LIBGOBLIN_API
211 int
212         LibGoblin_FreeBinInfo(
213                 int i_binfoid )
214 {
215         int             i_result;
216         LibGoblin_BinaryInfo    *p_binfo        = NULL;
217
218         p_binfo = BinaryInfo_GetBinInfo( i_binfoid );
219         if( NULL == p_binfo )   {
220                 return -0x01;
221         }
222
223         i_result        = BinaryInfo_FreeBinInfo( p_binfo );
224         if( 0x00 != i_result )  {
225                 return -0x02;
226         }
227
228         return i_result;
229 }
230
231
232 /***********************************************************************
233 ***********************************************************************/
234 LIBGOBLIN_API_PROGINFO
235 int
236         LibGoblin_AllocProgInfo(
237                 void )
238 {
239         LibGoblin_ProgramInfo   *p_pginfo       = NULL;
240  
241         p_pginfo        = ProgInfo_AllocProgInfo();
242         if( NULL == p_pginfo )  {
243                 return -0x01;
244         }
245
246         return p_pginfo->i_id;
247 }
248
249
250 /***********************************************************************
251 ***********************************************************************/
252 LIBGOBLIN_API_PROGINFO
253 int
254         LibGoblin_FreeProgInfo(
255                 int i_pgid )
256 {
257         int             i_result;
258         LibGoblin_ProgramInfo   *p_pginfo;
259
260         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
261         if( NULL == p_pginfo )  {
262                 return -0x01;
263         }
264         assert( p_pginfo->i_id == i_pgid );
265
266         i_result        = ProgInfo_FreeProgInfo( p_pginfo );
267         if( 0x00 != i_result )  {
268                 return -0x02;
269         }
270
271         return 0x00;
272 }
273
274
275 /***********************************************************************
276 ***********************************************************************/
277 LIBGOBLIN_API
278 int
279         LibGoblin_Init(
280                 void )
281 {
282         int             i_err;
283         
284         i_err   = LibFileType_Init();
285         if( 0x00 != i_err )             { return i_err; }
286
287         i_err   = ProgInfo_Init();
288         if( 0x00 != i_err )             { return i_err; }
289
290         i_err   = BinaryInfo_Init();
291         if( 0x00 != i_err )             { return i_err; }
292
293         i_err   = LibGoblin_SymWork_Init();
294         if( 0x00 != i_err )             { return i_err; }
295
296         return 0x00;
297 }
298
299
300 /***********************************************************************
301 ***********************************************************************/
302 LIBGOBLIN_API
303 int
304         LibGoblin_Term(
305                 void )
306 {
307         int             i_err;
308
309         i_err   = LibGoblin_SymWork_Term();
310         if( 0x00 != i_err )             { return i_err; }
311
312         i_err   = BinaryInfo_Term();
313         if( 0x00 != i_err )             { return i_err; }
314
315         i_err   = ProgInfo_Term();
316         if( 0x00 != i_err )             { return i_err; }
317
318         i_err   = LibFileType_Term();
319         if( 0x00 != i_err )             { return i_err; }
320
321         return 0x00;
322 }
323
324
325 /* EOF of drd64_.c ----------------------------------- */