OSDN Git Service

2019/04/09(Tue) 20:00
[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_AnalyzeMainProgram(
46                 int             i_pgid )
47 {
48         int             i_result;
49         LibGoblin_ProgramInfo   *p_pginfo;
50         LibGoblin_BinaryInfo    *p_binfo;
51
52         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
53         if( NULL == p_pginfo )  {
54                 return -0x01;
55         }
56
57         p_binfo = BinaryInfo_GetBinInfo( p_pginfo->i_binfo_id );
58         if( NULL == p_binfo )   {
59                 return -0x02;
60         }
61
62         i_result        = ELF_ReadELF_Phase2( p_binfo, (PtrValue)0x00000000 );
63         if( 0x00 != i_result )  {
64                 return -0x03;
65         }
66
67         return  i_result;
68 }
69
70
71 /***********************************************************************
72 ***********************************************************************/
73 LIBGOBLIN_API
74 int
75         LibGoblin_AnalyzeLibrary(
76                 int             i_pgid,
77                 int             i_bid,
78                 PtrValue        ptr_loadaddr )
79 {
80         int             i_result;
81         LibGoblin_BinaryInfo    *p_binfo;
82
83         p_binfo = BinaryInfo_GetBinInfo( i_bid );
84         if( NULL == p_binfo )   {
85                 return -0x01;
86         }
87
88         if( p_binfo->i_pginfo != i_pgid )       {
89                 return -0x02;
90         }
91
92         i_result        = ELF_ReadELF_Phase2( p_binfo, ptr_loadaddr );
93         if( 0x00 != i_result )  {
94                 return -0x03;
95         }
96
97         return  i_result;
98 }
99
100
101 /***********************************************************************
102 ***********************************************************************/
103 LIBGOBLIN_API
104 int
105         LibGoblin_GetUnresolv_ObjectFilePath(
106                 int             i_pgid,
107                 LibGoblin_ObjectFilePath *p_objpath )
108 {
109         int             i_result;
110         LibGoblin_ProgramInfo   *p_pginfo;
111         LibGoblin_BinaryInfo    *p_binfo;
112         LibGoblin_BinaryInfo    *p_binfo_parent;
113         LibGoblin_BinaryFile    *p_bfile;
114
115         if( NULL == p_objpath ) {
116                 return -0x02;
117         }
118
119         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
120         if( NULL == p_pginfo )  {
121                 return -0x03;
122         }
123
124         p_binfo = BinaryInfo_GetBinInfo( p_pginfo->i_binfo_id );
125         while( NULL != p_binfo )        {
126                 if( LIBGOBLIN_BINFO_PHASE_SETPROG > p_binfo->b_phase )
127                         { break; }
128                 p_binfo = BinaryInfo_GetBinInfo( p_binfo->i_binfo_next );
129         }
130
131         i_result        = -0x01;
132         if( NULL != p_binfo )   {
133                 memset( p_objpath, 0x00, sizeof( LibGoblin_ObjectFilePath ) );
134
135                 p_objpath->i_bid        = p_binfo->i_id;
136
137                 strncpy( p_objpath->str_filename, p_binfo->str_filename, DRD64_MAX_PATH );
138
139                 if( -1 < p_binfo->i_binfo_parent )      {
140                         p_binfo_parent  = BinaryInfo_GetBinInfo( p_binfo->i_binfo_parent );
141                         p_bfile = BinaryFile_GetBinaryFile( p_binfo_parent->i_binfile );
142                         if( NULL != p_bfile )   {
143                                 strncpy( p_objpath->str_rpath, p_bfile->str_rpath, DRD64_MAX_PATH );
144                                 strncpy( p_objpath->str_runpath, p_bfile->str_runpath, DRD64_MAX_PATH );
145                         }
146                 }
147
148                 i_result        = p_binfo->i_id;
149         }
150
151         return i_result;
152 }
153
154
155 /***********************************************************************
156 ***********************************************************************/
157 LIBGOBLIN_API
158 int
159         LibGoblin_SetPathAndLoad_ObjectFilePath(
160                 LibGoblin_ObjectFilePath        *p_objpath )
161 {
162         int             i_result;
163         LibGoblin_BinaryInfo    *p_binfo;
164
165         p_binfo = BinaryInfo_GetBinInfo( p_objpath->i_bid );
166         if( NULL == p_binfo )   {
167                 return -0x01;
168         }
169
170         i_result        = strncmp( p_objpath->str_filename,
171                                                 p_binfo->str_filename, DRD64_MAX_PATH );
172         if( 0x00 != i_result )  {
173                 return -0x02;
174         }
175         
176         i_result        = BinaryInfo_SetProgramPath(
177                                                 p_binfo, p_objpath->str_remotepath,
178                                                 p_objpath->str_localpath );
179         if( 0x00 != i_result )  {
180                 return -0x03;
181         }
182
183         i_result        = LoadProg_LoadBinaryInfo( p_binfo );
184         if( 0x00 != i_result )  {
185                 return -0x04;
186         }
187
188         return 0x00;
189 }
190
191
192
193 /***********************************************************************
194 ***********************************************************************/
195 LIBGOBLIN_API
196 int
197         LibGoblin_LoadProgram_Main(
198                 int             i_pgid,
199                 const char      *pstr_remotefull,
200                 const char      *pstr_localfull )
201 {
202         int             i_bid;
203         LibGoblin_ProgramInfo   *p_pginfo;
204
205     p_pginfo    = ProgInfo_GetProgInfo( i_pgid );
206         if( NULL == p_pginfo )  {
207                 return -0x01;
208         }
209
210         if( NULL == pstr_localfull )    {
211                 return -0x02;
212         }
213
214         i_bid   = LoadProgram_LoadMainProgram(
215                                                 p_pginfo, pstr_remotefull, pstr_localfull );
216         if(( 0 > i_bid ) || ( NULL == pstr_localfull )) {
217                 return -0x03;
218         }
219
220         return  i_bid;
221 }
222
223
224 /***********************************************************************
225 ***********************************************************************/
226 LIBGOBLIN_API
227 int
228         LibGoblin_AllocBinInfo(
229                 int i_pgid,
230                 const char *pstr_filename )
231 {
232         LibGoblin_ProgramInfo   *p_pginfo       = NULL;
233         LibGoblin_BinaryInfo    *p_binfo        = NULL;
234
235         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
236
237         p_binfo = BinaryInfo_AllocBinInfo( p_pginfo, pstr_filename, -1 );
238         if( NULL == p_binfo )   {
239                 return -0x01;
240         }
241
242         return p_binfo->i_id;
243 }
244
245
246 /***********************************************************************
247 ***********************************************************************/
248 LIBGOBLIN_API
249 int
250         LibGoblin_FreeBinInfo(
251                 int i_binfoid )
252 {
253         int             i_result;
254         LibGoblin_BinaryInfo    *p_binfo        = NULL;
255
256         p_binfo = BinaryInfo_GetBinInfo( i_binfoid );
257         if( NULL == p_binfo )   {
258                 return -0x01;
259         }
260
261         i_result        = BinaryInfo_FreeBinInfo( p_binfo );
262         if( 0x00 != i_result )  {
263                 return -0x02;
264         }
265
266         return i_result;
267 }
268
269
270 /***********************************************************************
271 ***********************************************************************/
272 LIBGOBLIN_API_PROGINFO
273 int
274         LibGoblin_AllocProgInfo(
275                 void )
276 {
277         int             i_result;
278         LibGoblin_ProgramInfo   *p_pginfo       = NULL;
279  
280         p_pginfo        = ProgInfo_AllocProgInfo();
281         if( NULL == p_pginfo )  {
282                 return -0x01;
283         }
284
285         i_result        = ObjectInfo_Init( p_pginfo, 0 );
286         if( 0x00 != i_result )  {
287                 ProgInfo_FreeProgInfo( p_pginfo );      // None use return value.
288                 return -0x02;
289         }
290
291         return p_pginfo->i_id;
292 }
293
294
295 /***********************************************************************
296 ***********************************************************************/
297 LIBGOBLIN_API_PROGINFO
298 int
299         LibGoblin_FreeProgInfo(
300                 int i_pgid )
301 {
302         int             i_result;
303         LibGoblin_ProgramInfo   *p_pginfo;
304
305         p_pginfo        = ProgInfo_GetProgInfo( i_pgid );
306         if( NULL == p_pginfo )  {
307                 return -0x01;
308         }
309         assert( p_pginfo->i_id == i_pgid );
310
311         ObjectInfo_Term( p_pginfo );    // None use retrun value.
312
313         i_result        = ProgInfo_FreeProgInfo( p_pginfo );
314         if( 0x00 != i_result )  {
315                 return -0x02;
316         }
317
318         return 0x00;
319 }
320
321
322 /***********************************************************************
323 ***********************************************************************/
324 LIBGOBLIN_API
325 int
326         LibGoblin_Init(
327                 void )
328 {
329         int             i_err;
330         
331         i_err   = LibFileType_Init();
332         if( 0x00 != i_err )             { return i_err; }
333
334         i_err   = ProgInfo_Init();
335         if( 0x00 != i_err )             { return i_err; }
336
337         i_err   = BinaryInfo_Init();
338         if( 0x00 != i_err )             { return i_err; }
339
340         return 0x00;
341 }
342
343
344 /***********************************************************************
345 ***********************************************************************/
346 LIBGOBLIN_API
347 int
348         LibGoblin_Term(
349                 void )
350 {
351         int             i_err;
352
353         i_err   = BinaryInfo_Term();
354         if( 0x00 != i_err )             { return i_err; }
355
356         i_err   = ProgInfo_Term();
357         if( 0x00 != i_err )             { return i_err; }
358
359         i_err   = LibFileType_Term();
360         if( 0x00 != i_err )             { return i_err; }
361
362         return 0x00;
363 }
364
365
366 /* EOF of drd64_.c ----------------------------------- */