1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
4 for INTEL64(R), AMD64(R)
6 Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
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.
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.
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
31 /* File Info -----------------------------------------------------------
35 ----------------------------------------------------------------------*/
37 #define DRD64_SRC_LIBGOBLIN_BINFO
38 #include"drd64_libgoblin.h"
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_BINFO_EXTERN
44 LibGoblin_BinaryInfo *
45 BinaryInfo_GetBinInfo(
48 if(( gi_max_binary_info <= i_bid ) || ( 0 > i_bid ))
55 /*----------------------------------------------------------------------
56 ----------------------------------------------------------------------*/
57 LIBGOBLIN_BINFO_EXTERN
60 LibGoblin_BinaryInfo *p_binfo,
63 const char *pcstr_result;
64 LibGoblin_BinaryFile *p_binfile;
66 assert( NULL != p_binfo );
67 p_binfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
68 assert( NULL != p_binfile );
70 if( NULL != pstr_rpath ) {
71 strncpy( pstr_rpath, p_binfile->str_rpath, DRD64_MAX_PATH );
72 pcstr_result = (const char *)pstr_rpath;
75 pcstr_result = (const char *)p_binfile->str_rpath;
82 /***********************************************************************
83 ***********************************************************************/
86 LibGoblin_GetRPath_fromBinInfo(
90 const char *pcstr_result;
91 LibGoblin_BinaryInfo *p_binfo;
93 p_binfo = BinaryInfo_GetBinInfo( i_bid );
94 if( NULL == p_binfo ) {
98 pcstr_result = BinaryInfo_GetRPath( p_binfo, pstr_rpath );
104 /*----------------------------------------------------------------------
105 ----------------------------------------------------------------------*/
107 BinaryInfo_ClearBinInfo(
108 LibGoblin_BinaryInfo *p_binfo )
110 assert( NULL != p_binfo );
112 memset( p_binfo, 0x00, sizeof( LibGoblin_BinaryInfo ) );
115 if( NULL != p_binfo->p_binfile ) {
116 p_binfo->p_binfile->i_fd = -0x01;
120 p_binfo->i_id = -0x01;
121 p_binfo->i_binfo_next = -0x01;
122 p_binfo->i_binfile = -0x01;
128 /*----------------------------------------------------------------------
129 ----------------------------------------------------------------------*/
130 LIBGOBLIN_BINFO_EXTERN
133 LibGoblin_BinaryInfo *p_binfo_now )
135 LibGoblin_BinaryInfo *p_binfo_top;
136 LibGoblin_BinaryInfo *p_binfo_end;
138 assert( NULL != p_binfo_now );
140 // Search Parent BinaryInfo ---
141 p_binfo_top = p_binfo_now;
142 while( -1 < p_binfo_top->i_parent_bid )
143 { p_binfo_top = BINFO( p_binfo_top->i_parent_bid ); }
145 if( p_binfo_now == p_binfo_top )
148 // Search & Get ChainEnd BinaryInfo ---
149 p_binfo_end = p_binfo_top;
150 while( ( -1 != p_binfo_end->i_binfo_next )
151 && ( p_binfo_end != p_binfo_now ) ) {
152 p_binfo_end = BINFO( p_binfo_end->i_binfo_next);
155 assert( NULL != p_binfo_end );
157 p_binfo_end->i_binfo_next = BINFOID(p_binfo_now);
163 /*----------------------------------------------------------------------
164 ----------------------------------------------------------------------*/
165 LIBGOBLIN_BINFO_EXTERN
168 LibGoblin_BinaryInfo *p_binfo_del )
170 LibGoblin_BinaryInfo *p_binfo_top;
171 LibGoblin_BinaryInfo *p_binfo_now;
172 LibGoblin_BinaryInfo *p_binfo_before;
173 LibGoblin_BinaryInfo *p_binfo_next;
175 assert( NULL != p_binfo_del );
177 // Search Parent BinaryInfo ---
178 p_binfo_top = p_binfo_del;
179 while( -1 < p_binfo_top->i_parent_bid )
180 { p_binfo_top = BINFO( p_binfo_top->i_parent_bid ); }
182 // Search & Get ChainEnd & ChainEnd-1 BinaryInfo ---
183 p_binfo_before = NULL;
184 p_binfo_now = p_binfo_top;
185 while( p_binfo_now != p_binfo_del ) {
186 assert( NULL != p_binfo_now );
187 p_binfo_before = p_binfo_now;
188 p_binfo_now = BINFO(p_binfo_now->i_binfo_next);
191 if( NULL != p_binfo_before ) {
192 p_binfo_next = BINFO(p_binfo_now->i_binfo_next);
193 p_binfo_before->i_binfo_next = BINFOID(p_binfo_next);
195 p_binfo_del->i_binfo_next = -1;
201 /*----------------------------------------------------------------------
202 ----------------------------------------------------------------------*/
203 LIBGOBLIN_BINFO_EXTERN
206 LibGoblin_BinaryInfo *p_binfo,
207 const char *pstr_rpath )
210 char str_src[MAXPATHLEN];
211 char str_dest[MAXPATHLEN];
213 char *pstr_rbasepath;
214 LibGoblin_BinaryFile *p_binfile;
216 if( LIBGOBLIN_BINFO_PHASE_SETPROG > p_binfo->b_phase ) {
220 p_binfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
221 assert( NULL != p_binfile );
223 memset( str_src, 0x00, MAXPATHLEN );
225 // p_binfo->str_path is current path
226 if( 0x00 != p_binfile->str_remotepath[0] )
227 { pstr_rbasepath = p_binfile->str_remotepath; }
229 { pstr_rbasepath = p_binfile->str_localpath; }
232 i_len = strnlen( pstr_rbasepath, DRD64_MAX_PATH );
233 strncpy( str_src, pstr_rbasepath, i_len + 1 );
234 str_src[ i_len ] = '/';
235 strncat( str_src, pstr_rpath, (MAXPATHLEN - i_len - 1) );
237 pstr_result = realpath( str_src, str_dest );
238 if( NULL == pstr_result ) {
242 i_len = strnlen( str_dest, MAXPATHLEN );
243 if( DRD64_MAX_PATH <= i_len ) {
246 strncpy( p_binfile->str_rpath, str_dest, DRD64_MAX_PATH );
252 /*----------------------------------------------------------------------
253 pstr_progpath: Absolute & Relative Path in this-machine program.
254 Absolute Path ONLY in remote-machine program.
255 * pstr_progpath != pstr_realpath is remote-machine program.
256 ----------------------------------------------------------------------*/
257 LIBGOBLIN_BINFO_EXTERN
259 BinaryInfo_SetProgramPath(
260 LibGoblin_BinaryInfo *p_binfo,
261 const char *pstr_remotepath,
262 const char *pstr_localpath )
265 char str_local[MAXPATHLEN];
266 char str_remote[MAXPATHLEN];
270 LibGoblin_BinaryFile *p_bfile;
272 assert( NULL != p_binfo );
274 // Check BinaryInfo & BinaryFile struct status ---
275 if( LIBGOBLIN_BINFO_PHASE_SETPROG <= p_binfo->b_phase ) {
279 // localpath convert to RelativePath to AbsolutePath ---
280 pstr_result = realpath( pstr_localpath, str_local );
281 if( NULL == pstr_result ) {
284 i_len = strnlen( str_local, DRD64_MAX_PATH );
285 if( '/' == str_local[ i_len ] ) { str_local[ i_len ] = '\0'; }
287 // remotepath convert to RelativePath to AbsolutePath ---
289 if( NULL != pstr_remotepath ) {
290 if( '\0' != *pstr_remotepath ) {
291 pstr_result = realpath( pstr_remotepath, str_remote );
292 if( NULL == pstr_result ) {
295 i_len = strnlen( str_remote, DRD64_MAX_PATH );
296 if( '/' == str_remote[ i_len ] ) { str_remote[ i_len ] = '\0'; }
298 pstr_remote = str_remote;
302 // Alloc BinaryFile struct & Set File Path ---
303 p_bfile = BinaryFile_DispenseBinaryFile( p_binfo, str_local, pstr_remote);
304 if( NULL == p_bfile ) {
308 p_binfo->i_binfile = p_bfile->i_id;
309 p_binfo->b_phase = LIBGOBLIN_BINFO_PHASE_SETPROG;
315 /*----------------------------------------------------------------------
316 ----------------------------------------------------------------------*/
317 LIBGOBLIN_BINFO_EXTERN
318 LibGoblin_BinaryInfo *
319 BinaryInfo_AllocBinInfo(
320 LibGoblin_ProgramInfo *p_pginfo,
321 const char *pstr_filename,
326 LibGoblin_BinaryInfo *p_binfo_base;
327 LibGoblin_BinaryInfo *p_binfo_now;
332 if( gi_now_binary_info < gi_max_binary_info ) {
333 for( i_cnt = 0; i_cnt < gi_max_binary_info; i_cnt++ ) {
335 if( -1 == BINFO(i_cnt)->i_id ) {
336 p_binfo_now = BINFO(i_cnt);
338 gi_now_binary_info++;
343 else if( gi_now_binary_info == gi_alloc_binary_info ) {
345 p_binfo_base = realloc( gp_binary_info,
346 sizeof( LibGoblin_BinaryInfo ) *
347 (gi_alloc_binary_info + LIBGOBLIN_BINFO_UNITS) );
348 if( NULL == p_binfo_base ) {
352 for( i_cnt = gi_alloc_binary_info;
353 i_cnt < (gi_alloc_binary_info + LIBGOBLIN_BINFO_UNITS); i_cnt++ ) {
354 BinaryInfo_ClearBinInfo( p_binfo_base + i_cnt );
357 gp_binary_info = p_binfo_base;
358 gi_alloc_binary_info += LIBGOBLIN_BINFO_UNITS;
360 p_binfo_now = BINFO(gi_now_binary_info);
361 i_id_now = gi_now_binary_info++;
363 if( i_id_now == gi_max_binary_info ) { gi_max_binary_info++; }
366 p_binfo_now = BINFO(gi_now_binary_info);
367 i_id_now = gi_now_binary_info++;
369 if( i_id_now == gi_max_binary_info ) { gi_max_binary_info++; }
372 assert( NULL != p_binfo_now );
374 BinaryInfo_ClearBinInfo( p_binfo_now );
375 p_binfo_now->i_id = i_id_now;
376 //p_binfo_now->b_phase = LIBGOBLIN_BINFO_PHASE_ALLOCED;
377 p_binfo_now->pv_pginfo = (void *)p_pginfo;
378 p_binfo_now->i_parent_bid = i_parent_bid;
379 strncpy( p_binfo_now->str_filename, pstr_filename, DRD64_MAX_PATH );
380 p_binfo_now->b_phase = LIBGOBLIN_BINFO_PHASE_SETNAME;
382 BinInfo_AppendChain( p_binfo_now );
388 /*----------------------------------------------------------------------
389 ----------------------------------------------------------------------*/
390 LIBGOBLIN_BINFO_EXTERN
392 BinaryInfo_FreeBinInfo(
393 LibGoblin_BinaryInfo *p_binfo )
397 LibGoblin_BinaryFile *p_binfile;
399 if( NULL == p_binfo ) { return 0x01; }
401 if( -0x01 != p_binfo->i_binfile ) {
402 p_binfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
403 assert( NULL != p_binfile );
405 if( NULL == p_binfile->p_elf ) {
406 free( p_binfile->p_elf );
407 p_binfile->p_elf = NULL;
410 BinaryFile_FreeBinaryFile( p_binfile );
411 p_binfo->i_binfile = -0x01;
414 BinInfo_DeleteChain( p_binfo );
416 i_binfoid = p_binfo->i_id;
417 BinaryInfo_ClearBinInfo( p_binfo );
419 if( gi_max_binary_info == (i_binfoid + 1) ) { gi_max_binary_info--; }
420 gi_now_binary_info--;
421 if( 0 == gi_now_binary_info ) { gi_max_binary_info = 0; }
427 /*----------------------------------------------------------------------
428 ----------------------------------------------------------------------*/
429 LIBGOBLIN_BINFO_EXTERN
435 LibGoblin_BinaryInfo *p_binfo;
437 if( 0 != gi_max_binary_info ) {
441 gp_binary_info = malloc( sizeof( LibGoblin_BinaryInfo )
442 * LIBGOBLIN_BINFO_UNITS );
443 if( NULL == gp_binary_info ) {
447 for( i_cnt = 0; i_cnt < LIBGOBLIN_BINFO_UNITS; i_cnt++ ) {
448 p_binfo = BINFO(i_cnt);
449 BinaryInfo_ClearBinInfo( p_binfo );
452 gi_alloc_binary_info = LIBGOBLIN_BINFO_UNITS;
453 gi_max_binary_info = 0;
454 gi_now_binary_info = 0;
460 /*----------------------------------------------------------------------
461 ----------------------------------------------------------------------*/
462 LIBGOBLIN_BINFO_EXTERN
467 LibGoblin_BinaryInfo *p_binfo;
469 if( 0 < gi_now_binary_info ) {
470 for( ; gi_now_binary_info >= 0; gi_now_binary_info-- ) {
471 p_binfo = BinaryInfo_GetBinInfo( gi_now_binary_info );
472 BinaryInfo_FreeBinInfo( p_binfo );
476 free( gp_binary_info );
478 gp_binary_info = NULL;
479 gi_alloc_binary_info = 0;
480 gi_max_binary_info = 0;
481 gi_now_binary_info = 0;
487 /* EOF of drd64_.c ----------------------------------- */