1 // sharedlib.cpp: Shared Library support, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "gnashconfig.h"
27 #include "sharedlib.h"
35 #if defined(WIN32) || defined(_WIN32)
36 # define LIBLTDL_DLL_IMPORT 1
46 #include <boost/thread/mutex.hpp>
48 #if defined(WIN32) || defined(_WIN32)
49 int lt_dlsetsearchpath (const char *search_path
);
51 void * lt_dlsym (lt_dlhandle handle
, const char *name
);
52 const char *lt_dlerror (void);
53 int lt_dlclose (lt_dlhandle handle
);
54 int lt_dlmakeresident (lt_dlhandle handle
);
55 lt_dlhandle
lt_dlopenext (const char *filename
);
58 #if defined(_WIN32) || defined(WIN32)
59 # define PLUGINSDIR "./"
64 SharedLib::SharedLib(const std::string
& filespec
)
69 SharedLib::SharedLib(const std::string
& filespec
, const std::string
& envvar
)
72 scoped_lock
lock(_libMutex
);
74 // Initialize libtool's dynamic library loader
76 int errors
= lt_dlinit ();
78 log_error (_("Couldn't initialize ltdl: %s"), lt_dlerror());
81 # warning "libltdl not enabled in build".
83 std::string pluginsdir
;
84 char *env
= std::getenv (envvar
.c_str());
88 pluginsdir
= PLUGINSDIR
;
93 SharedLib::~SharedLib()
101 return lt_dlclose(_dlhandle
);
110 return openLib(_filespec
);
114 SharedLib::openLib (const std::string
& filespec
)
117 scoped_lock
lock(_libMutex
);
119 log_debug ("Trying to open shared library \"%s\"", filespec
);
122 _dlhandle
= lt_dlopenext (filespec
.c_str());
124 if (_dlhandle
== NULL
) {
125 log_error ("%s", lt_dlerror());
129 // Make this module unloadable
130 lt_dlmakeresident(_dlhandle
);
133 log_debug (_("Opened dynamic library \"%s\""), filespec
);
135 _filespec
= filespec
;
140 SharedLib::initentry
*
141 SharedLib::getInitEntry (const std::string
& symbol
)
143 // GNASH_REPORT_FUNCTION;
146 scoped_lock
lock(_libMutex
);
149 run
= lt_dlsym (_dlhandle
, symbol
.c_str());
152 log_error (_("Couldn't find symbol: %s"), symbol
);
155 log_debug (_("Found symbol %s @ %p"), symbol
, (void *)run
);
159 return (initentry
*)(run
);
162 SharedLib::entrypoint
*
163 SharedLib::getDllSymbol(const std::string
& symbol
)
165 GNASH_REPORT_FUNCTION
;
169 scoped_lock
lock(_libMutex
);
172 run
= lt_dlsym (_dlhandle
, symbol
.c_str());
176 Realistically, we should never get a valid pointer with a value of 0
177 Markus: 'Id est NULL.'
180 log_error (_("Couldn't find symbol: %s"), symbol
);
183 log_debug (_("Found symbol %s @ %p"), symbol
, (void *)run
);
186 return (entrypoint
*)(run
);
189 } // end of gnash namespace