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_LIBBROWNIE_SEARCHLIB
38 #include"drd64_libbrownie.h"
41 /*----------------------------------------------------------------------
42 Is Exist the Library File in dir (pstr_path & "/" & pstr_solibname)
45 0x00: OK (exist LibraryFile)
46 -0x01: Path + .so FileName string size over.
48 int LibBrownie_SearchLib_IsExistLibrary(
49 char *pstr_path, // Library Dir. string
50 char *pstr_solibname // Library(.so) FileName
52 ----------------------------------------------------------------------*/
54 SearchLib_IsExistLibrary(
56 char *pstr_solibname )
60 struct stat t_filestat;
61 char str_solibpath[DRD64_MAX_PATH];
63 // Check Library Path & FileName Size ---
64 sz_len = strnlen( pstr_path, DRD64_MAX_PATH );
65 sz_len += strnlen( pstr_solibname, DRD64_MAX_PATH );
67 if( DRD64_MAX_PATH - 1 <= sz_len ) {
71 strncpy( str_solibpath, pstr_path, DRD64_MAX_PATH );
72 strncat( str_solibpath, "/", DRD64_MAX_PATH );
73 strncat( str_solibpath, pstr_solibname, DRD64_MAX_PATH );
74 i_result = stat( str_solibpath, &t_filestat );
80 /*----------------------------------------------------------------------
81 Search & Get Library Exist-Path from colon-separated Path Lists.
82 ----------------------------------------------------------------------*/
84 SearchLib_SearchLibInPathArrays(
85 char *pstr_solibpath, // out
86 char *pstr_pathlist, // in
87 char *pstr_solibname ) // in
97 // alloc & copy pstr_pathlist -> pstr_pathtemp ---
98 sz_len = strlen( pstr_pathlist );
99 pstr_pathtemp = (char *)alloca( sz_len + 1 );
100 strncpy( pstr_pathtemp, pstr_pathlist, sz_len + 1 );
104 pstr_nowpath = pstr_pathtemp;
105 pstr_nowpos = pstr_pathtemp;
107 if(( ':' == *pstr_nowpos ) || ( '\0' == *pstr_nowpos)) {
108 if( '\0' == *pstr_nowpos ) { i_flag = 0x01; }
109 else { *pstr_nowpos = '\0'; }
111 i_err = SearchLib_IsExistLibrary( pstr_nowpath, pstr_solibname );
112 if( 0x00 == i_err ) {
114 strncpy( pstr_solibpath, pstr_nowpath, DRD64_MAX_PATH );
115 goto goto_SearchLib_SearchLibInPathArrays_post;
118 if( 0x00 == i_flag ) { *pstr_nowpos = ':'; }
120 pstr_nowpath = pstr_nowpos + 1;
123 }while( '\0' != *pstr_nowpos++ );
125 goto_SearchLib_SearchLibInPathArrays_post:
130 /***********************************************************************
131 int LibBrownie_GetLibraryPath(
132 char *pstr_solibpath, [OUT] .so existed Library FullPath
133 char *pstr_solibname, [IN] .so Library Filename
134 char *pstr_dt_rpath, [IN] RPATH string from .dynamic seciton
135 char *pstr_dt_runpath, [IN] RUNPATH string from .dynamic seciton
136 char *pstr_ldsohints [IN] LD_LIBRARYPATH env string.
139 * LocalLibrary ( if pstr_rpath != NULL )
140 pstr_solibpath = pstr_rpath & "/" & pstr_solibname
142 ***********************************************************************/
143 LIBBROWNIE_SEARCHLIB_API
145 LibBrownie_GetLibraryPath(
146 char *pstr_solibpath,
147 char *pstr_solibname,
149 char *pstr_dt_runpath,
150 char *pstr_env_ldlibpath,
151 char *pstr_ldsohints )
156 if( NULL == pstr_solibpath ) {
160 if( NULL == pstr_ldsohints ) {
164 // <Order Info from man ld.so>------------------------------------------
165 // 1. DT_RPATH of the referencing object unless that object also
166 // contains a DT_RUNPATH tag
167 // 2. DT_RPATH of the program unless the referencing object contains
169 if( NULL != pstr_dt_rpath ) {
170 // XXX: pstr_dt_runpath != NULL for WARNING!!
171 i_err = SearchLib_SearchLibInPathArrays(
172 pstr_solibpath, pstr_dt_rpath, pstr_solibname );
173 if( 0x00 == i_err ) {
175 goto goto_LibBrownie_GetLibraryPath_post;
179 // <Order Info from man ld.so>------------------------------------------
180 // 3. Path indicated by LD_LIBRARY_PATH environment variable
181 if( NULL != pstr_env_ldlibpath ) {
182 i_err = SearchLib_SearchLibInPathArrays(
183 pstr_solibpath, pstr_env_ldlibpath, pstr_solibname );
184 if( 0x00 == i_err ) {
186 goto goto_LibBrownie_GetLibraryPath_post;
190 // <Order Info from man ld.so>------------------------------------------
191 // 4. DT_RUNPATH of the referencing object
192 if( NULL != pstr_dt_runpath ) {
193 i_err = SearchLib_SearchLibInPathArrays(
194 pstr_solibpath, pstr_dt_runpath, pstr_solibname );
195 if( 0x00 == i_err ) {
197 goto goto_LibBrownie_GetLibraryPath_post;
201 // <Order Info from man ld.so>------------------------------------------
202 // 5. Hints file produced by the ldconfig(8) utility
203 i_err = SearchLib_SearchLibInPathArrays(
204 pstr_solibpath, pstr_ldsohints, pstr_solibname );
205 if( 0x00 == i_err ) {
207 goto goto_LibBrownie_GetLibraryPath_post;
210 // 6. The /lib and /usr/lib directories, unless the referencing
211 // object was linked using the "-z nodefaultlib" option
212 i_err = SearchLib_SearchLibInPathArrays(
213 pstr_solibpath, "/lib:/usr/lib", pstr_solibname );
214 if( 0x00 == i_err ) {
218 goto_LibBrownie_GetLibraryPath_post:
224 /* EOF of drd64_.c ----------------------------------- */