OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / libbrownie / drd64_libbrownie_ldsohints.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_LIBBROWNIE_LDSOHINTS
38 #include"drd64_libbrownie.h"
39
40 static  char    *gpstr_solibpath[LIBBROWNIE_LDHINTS_MAX];
41
42
43 /***********************************************************************
44 ***********************************************************************/
45 LIBBROWNIE_LDSOHINTS_API
46 char *
47         LibBrownie_GetLoadLibraryPath(
48                         Byte    b_ldbits )
49 {
50         int             i_fd;
51         ssize_t t_size;
52         char    **ppstr_solibpath;
53         char    *pstr_hintsfile;
54         struct  elfhints_hdr    t_elfhints_header;
55
56         if( b_ldbits == LIBBROWNIE_LDHINTS_ELF64 )
57                 { pstr_hintsfile        = LIBBROWNIE_LDHINST_HINTSFILE64; }
58         else if( b_ldbits == LIBBROWNIE_LDHINTS_ELF32 )
59                 { pstr_hintsfile        = LIBBROWNIE_LDHINST_HINTSFILE32; }
60         else    {
61                 return NULL;
62         }
63         ppstr_solibpath = &(gpstr_solibpath[b_ldbits]);
64         if( NULL != *ppstr_solibpath )  { return *ppstr_solibpath; }
65
66         i_fd    = open( pstr_hintsfile, O_RDONLY );
67         if( -0x01 == i_fd )     {
68                 return NULL;
69         }
70
71         t_size = read( i_fd, &t_elfhints_header, sizeof( struct elfhints_hdr ) );
72         if( sizeof( struct elfhints_hdr ) != t_size )   {
73                 goto    goto_LibBrownie_GetLoadLibraryPath_FileClose;
74         }
75
76         if( ELFHINTS_MAGIC != t_elfhints_header.magic ) {
77                 goto    goto_LibBrownie_GetLoadLibraryPath_FileClose;
78         }
79
80         *ppstr_solibpath        = (char *)malloc( t_elfhints_header.strsize + 1 );
81         if( NULL == *ppstr_solibpath )  {
82                 goto    goto_LibBrownie_GetLoadLibraryPath_FileClose;
83         }
84         memset( *ppstr_solibpath, 0x00, (t_elfhints_header.strsize + 1) );
85
86         t_size  = read( i_fd, *ppstr_solibpath, t_elfhints_header.strsize );
87         if( t_elfhints_header.strsize != t_size )       {
88                 goto    goto_LibBrownie_GetLoadLibraryPath_FileClose ;
89         }
90
91 goto_LibBrownie_GetLoadLibraryPath_FileClose:
92         close( i_fd );
93
94         return *ppstr_solibpath;
95 }
96
97
98 /***********************************************************************
99 ***********************************************************************/
100 LIBBROWNIE_LDSOHINTS_API
101 int
102         LibBrownie_FreeLoadLibraryPath(
103                         Byte    b_ldbits )
104 {
105         char    **ppstr_solibpath;
106
107         if(( b_ldbits != LIBBROWNIE_LDHINTS_ELF64 )
108                         && ( b_ldbits != LIBBROWNIE_LDHINTS_ELF32 ))    {
109                 return -0x01;
110         }
111
112         ppstr_solibpath = &(gpstr_solibpath[b_ldbits]);
113
114         if( NULL != *ppstr_solibpath )  {
115                 free( *ppstr_solibpath );
116                 *ppstr_solibpath        = NULL;
117         }
118         
119         return 0x00;
120 }
121
122                 
123 /*----------------------------------------------------------------------
124 ----------------------------------------------------------------------*/
125 LIBBROWNIE_LDSOHINTS_EXTERN
126 int
127         LibBrownie_LdSoHints_Init(
128                         void )
129 {
130         gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ]     = NULL;
131         gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ]     = NULL;
132
133         return 0x00;
134 }
135
136
137 /*----------------------------------------------------------------------
138 ----------------------------------------------------------------------*/
139 LIBBROWNIE_LDSOHINTS_EXTERN
140 void
141         LibBrownie_LdSoHints_Term(
142                         void )
143 {
144         if( NULL !=  gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ] )      {
145                 free( gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ] );
146                 gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF64 ]     = NULL;
147         }
148
149         if( NULL !=  gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ] )      {
150                 free( gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ] );
151                 gpstr_solibpath[ LIBBROWNIE_LDHINTS_ELF32 ]     = NULL;
152         }
153
154         return;
155 }
156
157
158 /* EOF of drd64_.c ----------------------------------- */