4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #ifndef HAVE_STRUCT_LT_USER_DLLOADER
38 /* Only used with ltdl 2.2 */
44 #include <pulsecore/macro.h>
45 #include <pulsecore/mutex.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/log.h>
49 #include "ltdl-bind-now.h"
52 #define PA_BIND_NOW RTLD_NOW
54 #define PA_BIND_NOW DL_NOW
59 static pa_mutex
*libtool_mutex
= NULL
;
61 PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls
);
63 static void libtool_lock(void) {
64 pa_mutex_lock(libtool_mutex
);
67 static void libtool_unlock(void) {
68 pa_mutex_unlock(libtool_mutex
);
71 static void libtool_set_error(const char *error
) {
72 PA_STATIC_TLS_SET(libtool_tls
, (char*) error
);
75 static const char *libtool_get_error(void) {
76 return PA_STATIC_TLS_GET(libtool_tls
);
82 To avoid lazy relocations during runtime in our RT threads we add
83 our own shared object loader with uses RTLD_NOW if it is
84 available. The standard ltdl loader prefers RTLD_LAZY.
86 Please note that this loader doesn't have any influence on
87 relocations on any libraries that are already loaded into our
88 process, i.e. because the pulseaudio binary links directly to
89 them. To disable lazy relocations for those libraries it is possible
90 to set $LT_BIND_NOW before starting the pulsaudio binary.
93 #ifndef HAVE_LT_DLADVISE
94 static lt_module
bind_now_open(lt_user_data d
, const char *fname
) {
96 static lt_module
bind_now_open(lt_user_data d
, const char *fname
, lt_dladvise advise
) {
102 if (!(m
= dlopen(fname
, PA_BIND_NOW
))) {
103 libtool_set_error(dlerror());
110 static int bind_now_close(lt_user_data d
, lt_module m
) {
114 if (dlclose(m
) != 0){
115 libtool_set_error(dlerror());
122 static lt_ptr
bind_now_find_sym(lt_user_data d
, lt_module m
, const char *symbol
) {
128 if (!(ptr
= dlsym(m
, symbol
))) {
129 libtool_set_error(dlerror());
138 void pa_ltdl_init(void) {
141 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
143 static const struct lt_user_dlloader loader
= {
144 .module_open
= bind_now_open
,
145 .module_close
= bind_now_close
,
146 .find_sym
= bind_now_find_sym
149 static const lt_dlvtable
*dlopen_loader
;
150 static lt_dlvtable bindnow_loader
;
154 pa_assert_se(lt_dlinit() == 0);
155 pa_assert_se(libtool_mutex
= pa_mutex_new(TRUE
, FALSE
));
156 #ifdef HAVE_LT_DLMUTEX_REGISTER
157 pa_assert_se(lt_dlmutex_register(libtool_lock
, libtool_unlock
, libtool_set_error
, libtool_get_error
) == 0);
161 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
163 if (!(place
= lt_dlloader_find("dlopen")))
164 place
= lt_dlloader_next(NULL
);
166 /* Add our BIND_NOW loader as the default module loader. */
167 if (lt_dlloader_add(place
, &loader
, "bind-now-loader") != 0)
168 pa_log_warn("Failed to add bind-now-loader.");
170 /* Already initialised */
171 if ( dlopen_loader
!= NULL
) return;
173 if (!(dlopen_loader
= lt_dlloader_find("dlopen"))) {
174 pa_log_warn("Failed to find original dlopen loader.");
178 memcpy(&bindnow_loader
, dlopen_loader
, sizeof(bindnow_loader
));
179 bindnow_loader
.name
= "bind-now-loader";
180 bindnow_loader
.module_open
= bind_now_open
;
181 bindnow_loader
.module_close
= bind_now_close
;
182 bindnow_loader
.find_sym
= bind_now_find_sym
;
183 bindnow_loader
.priority
= LT_DLLOADER_PREPEND
;
185 /* Add our BIND_NOW loader as the default module loader. */
186 if (lt_dlloader_add(&bindnow_loader
) != 0)
187 pa_log_warn("Failed to add bind-now-loader.");
192 void pa_ltdl_done(void) {
193 pa_assert_se(lt_dlexit() == 0);
194 pa_mutex_free(libtool_mutex
);
195 libtool_mutex
= NULL
;