2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #ifndef HAVE_STRUCT_LT_USER_DLLOADER
36 /* Only used with ltdl 2.2 */
42 #include <pulsecore/macro.h>
43 #include <pulsecore/mutex.h>
44 #include <pulsecore/thread.h>
45 #include <pulsecore/log.h>
47 #include "ltdl-bind-now.h"
50 #define PA_BIND_NOW RTLD_NOW
52 #define PA_BIND_NOW DL_NOW
57 static pa_mutex
*libtool_mutex
= NULL
;
59 PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls
);
61 static void libtool_lock(void) {
62 pa_mutex_lock(libtool_mutex
);
65 static void libtool_unlock(void) {
66 pa_mutex_unlock(libtool_mutex
);
69 static void libtool_set_error(const char *error
) {
70 PA_STATIC_TLS_SET(libtool_tls
, (char*) error
);
73 static const char *libtool_get_error(void) {
74 return PA_STATIC_TLS_GET(libtool_tls
);
80 To avoid lazy relocations during runtime in our RT threads we add
81 our own shared object loader with uses RTLD_NOW if it is
82 available. The standard ltdl loader prefers RTLD_LAZY.
84 Please note that this loader doesn't have any influence on
85 relocations on any libraries that are already loaded into our
86 process, i.e. because the pulseaudio binary links directly to
87 them. To disable lazy relocations for those libraries it is possible
88 to set $LT_BIND_NOW before starting the pulsaudio binary.
91 #ifndef HAVE_LT_DLADVISE
92 static lt_module
bind_now_open(lt_user_data d
, const char *fname
) {
94 static lt_module
bind_now_open(lt_user_data d
, const char *fname
, lt_dladvise advise
) {
100 if (!(m
= dlopen(fname
, PA_BIND_NOW
))) {
101 libtool_set_error(dlerror());
108 static int bind_now_close(lt_user_data d
, lt_module m
) {
112 if (dlclose(m
) != 0){
113 libtool_set_error(dlerror());
120 static lt_ptr
bind_now_find_sym(lt_user_data d
, lt_module m
, const char *symbol
) {
126 if (!(ptr
= dlsym(m
, symbol
))) {
127 libtool_set_error(dlerror());
136 void pa_ltdl_init(void) {
139 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
141 static const struct lt_user_dlloader loader
= {
142 .module_open
= bind_now_open
,
143 .module_close
= bind_now_close
,
144 .find_sym
= bind_now_find_sym
147 static const lt_dlvtable
*dlopen_loader
;
148 static lt_dlvtable bindnow_loader
;
152 pa_assert_se(lt_dlinit() == 0);
153 pa_assert_se(libtool_mutex
= pa_mutex_new(TRUE
, FALSE
));
154 #ifdef HAVE_LT_DLMUTEX_REGISTER
155 pa_assert_se(lt_dlmutex_register(libtool_lock
, libtool_unlock
, libtool_set_error
, libtool_get_error
) == 0);
159 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
161 if (!(place
= lt_dlloader_find("dlopen")))
162 place
= lt_dlloader_next(NULL
);
164 /* Add our BIND_NOW loader as the default module loader. */
165 if (lt_dlloader_add(place
, &loader
, "bind-now-loader") != 0)
166 pa_log_warn("Failed to add bind-now-loader.");
168 /* Already initialised */
169 if ( dlopen_loader
!= NULL
) return;
171 if (!(dlopen_loader
= lt_dlloader_find("dlopen"))) {
172 pa_log_warn("Failed to find original dlopen loader.");
176 memcpy(&bindnow_loader
, dlopen_loader
, sizeof(bindnow_loader
));
177 bindnow_loader
.name
= "bind-now-loader";
178 bindnow_loader
.module_open
= bind_now_open
;
179 bindnow_loader
.module_close
= bind_now_close
;
180 bindnow_loader
.find_sym
= bind_now_find_sym
;
181 bindnow_loader
.priority
= LT_DLLOADER_PREPEND
;
183 /* Add our BIND_NOW loader as the default module loader. */
184 if (lt_dlloader_add(&bindnow_loader
) != 0)
185 pa_log_warn("Failed to add bind-now-loader.");
190 void pa_ltdl_done(void) {
191 pa_assert_se(lt_dlexit() == 0);
192 pa_mutex_free(libtool_mutex
);
193 libtool_mutex
= NULL
;