]> code.delx.au - pulseaudio/blob - src/daemon/ltdl-bind-now.c
400772488f943f4528782f2aaceeca6a54787204
[pulseaudio] / src / daemon / ltdl-bind-now.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
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.
11
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.
16
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
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #if HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
30
31 #if HAVE_SYS_DL_H
32 #include <sys/dl.h>
33 #endif
34
35 #include <string.h>
36
37 #include <ltdl.h>
38
39 #include <pulse/i18n.h>
40
41 #include <pulsecore/macro.h>
42 #include <pulsecore/mutex.h>
43 #include <pulsecore/thread.h>
44 #include <pulsecore/log.h>
45
46 #include "ltdl-bind-now.h"
47
48 #ifdef RTLD_NOW
49 #define PA_BIND_NOW RTLD_NOW
50 #elif defined(DL_NOW)
51 #define PA_BIND_NOW DL_NOW
52 #else
53 #undef PA_BIND_NOW
54 #endif
55
56 #ifdef HAVE_LT_DLMUTEX_REGISTER
57
58 static pa_mutex *libtool_mutex = NULL;
59
60 PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls);
61
62 static void libtool_lock(void) {
63 pa_mutex_lock(libtool_mutex);
64 }
65
66 static void libtool_unlock(void) {
67 pa_mutex_unlock(libtool_mutex);
68 }
69
70 static void libtool_set_error(const char *error) {
71 PA_STATIC_TLS_SET(libtool_tls, (char*) error);
72 }
73
74 static const char *libtool_get_error(void) {
75 return PA_STATIC_TLS_GET(libtool_tls);
76 }
77
78 #endif
79
80 #ifdef PA_BIND_NOW
81
82 /*
83 To avoid lazy relocations during runtime in our RT threads we add
84 our own shared object loader with uses RTLD_NOW if it is
85 available. The standard ltdl loader prefers RTLD_LAZY.
86
87 Please note that this loader doesn't have any influence on
88 relocations on any libraries that are already loaded into our
89 process, i.e. because the pulseaudio binary links directly to
90 them. To disable lazy relocations for those libraries it is possible
91 to set $LT_BIND_NOW before starting the pulsaudio binary.
92 */
93
94 #ifndef HAVE_LT_DLADVISE
95 static lt_module bind_now_open(lt_user_data d, const char *fname)
96 #else
97 static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise)
98 #endif
99 {
100 lt_module m;
101
102 pa_assert(fname);
103
104 if (!(m = dlopen(fname, PA_BIND_NOW))) {
105 libtool_set_error(dlerror());
106 return NULL;
107 }
108
109 return m;
110 }
111
112 static int bind_now_close(lt_user_data d, lt_module m) {
113
114 pa_assert(m);
115
116 if (dlclose(m) != 0){
117 libtool_set_error(dlerror());
118 return 1;
119 }
120
121 return 0;
122 }
123
124 static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol) {
125 lt_ptr ptr;
126
127 pa_assert(m);
128 pa_assert(symbol);
129
130 if (!(ptr = dlsym(m, symbol))) {
131 libtool_set_error(dlerror());
132 return NULL;
133 }
134
135 return ptr;
136 }
137
138 #endif
139
140 void pa_ltdl_init(void) {
141
142 #ifdef PA_BIND_NOW
143 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
144 lt_dlloader *place;
145 static const struct lt_user_dlloader loader = {
146 .module_open = bind_now_open,
147 .module_close = bind_now_close,
148 .find_sym = bind_now_find_sym
149 };
150 # else
151 static const lt_dlvtable *dlopen_loader;
152 static lt_dlvtable bindnow_loader;
153 # endif
154 #endif
155
156 pa_assert_se(lt_dlinit() == 0);
157
158 #ifdef HAVE_LT_DLMUTEX_REGISTER
159 pa_assert_se(libtool_mutex = pa_mutex_new(TRUE, FALSE));
160 pa_assert_se(lt_dlmutex_register(libtool_lock, libtool_unlock, libtool_set_error, libtool_get_error) == 0);
161 #endif
162
163 #ifdef PA_BIND_NOW
164 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
165
166 if (!(place = lt_dlloader_find("dlopen")))
167 place = lt_dlloader_next(NULL);
168
169 /* Add our BIND_NOW loader as the default module loader. */
170 if (lt_dlloader_add(place, &loader, "bind-now-loader") != 0)
171 pa_log_warn(_("Failed to add bind-now-loader."));
172 # else
173 /* Already initialised */
174 if (dlopen_loader)
175 return;
176
177 if (!(dlopen_loader = lt_dlloader_find("dlopen"))) {
178 pa_log_warn(_("Failed to find original dlopen loader."));
179 return;
180 }
181
182 memcpy(&bindnow_loader, dlopen_loader, sizeof(bindnow_loader));
183 bindnow_loader.name = "bind-now-loader";
184 bindnow_loader.module_open = bind_now_open;
185 bindnow_loader.module_close = bind_now_close;
186 bindnow_loader.find_sym = bind_now_find_sym;
187 bindnow_loader.priority = LT_DLLOADER_PREPEND;
188
189 /* Add our BIND_NOW loader as the default module loader. */
190 if (lt_dlloader_add(&bindnow_loader) != 0)
191 pa_log_warn(_("Failed to add bind-now-loader."));
192 # endif
193 #endif
194 }
195
196 void pa_ltdl_done(void) {
197 pa_assert_se(lt_dlexit() == 0);
198
199 #ifdef HAVE_LT_DLMUTEX_REGISTER
200 pa_mutex_free(libtool_mutex);
201 libtool_mutex = NULL;
202 #endif
203 }