]> code.delx.au - pulseaudio/blob - src/daemon/ltdl-bind-now.c
Merge dead branch 'liboil-test'
[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 #ifndef HAVE_STRUCT_LT_USER_DLLOADER
36 /* Only used with ltdl 2.2 */
37 #include <string.h>
38 #endif
39
40 #include <ltdl.h>
41
42 #include <pulsecore/macro.h>
43 #include <pulsecore/mutex.h>
44 #include <pulsecore/thread.h>
45 #include <pulsecore/log.h>
46
47 #include "ltdl-bind-now.h"
48
49 #ifdef RTLD_NOW
50 #define PA_BIND_NOW RTLD_NOW
51 #elif defined(DL_NOW)
52 #define PA_BIND_NOW DL_NOW
53 #else
54 #undef PA_BIND_NOW
55 #endif
56
57 static pa_mutex *libtool_mutex = NULL;
58
59 PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls);
60
61 static void libtool_lock(void) {
62 pa_mutex_lock(libtool_mutex);
63 }
64
65 static void libtool_unlock(void) {
66 pa_mutex_unlock(libtool_mutex);
67 }
68
69 static void libtool_set_error(const char *error) {
70 PA_STATIC_TLS_SET(libtool_tls, (char*) error);
71 }
72
73 static const char *libtool_get_error(void) {
74 return PA_STATIC_TLS_GET(libtool_tls);
75 }
76
77 #ifdef PA_BIND_NOW
78
79 /*
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.
83
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.
89 */
90
91 #ifndef HAVE_LT_DLADVISE
92 static lt_module bind_now_open(lt_user_data d, const char *fname) {
93 #else
94 static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise) {
95 #endif
96 lt_module m;
97
98 pa_assert(fname);
99
100 if (!(m = dlopen(fname, PA_BIND_NOW))) {
101 libtool_set_error(dlerror());
102 return NULL;
103 }
104
105 return m;
106 }
107
108 static int bind_now_close(lt_user_data d, lt_module m) {
109
110 pa_assert(m);
111
112 if (dlclose(m) != 0){
113 libtool_set_error(dlerror());
114 return 1;
115 }
116
117 return 0;
118 }
119
120 static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol) {
121 lt_ptr ptr;
122
123 pa_assert(m);
124 pa_assert(symbol);
125
126 if (!(ptr = dlsym(m, symbol))) {
127 libtool_set_error(dlerror());
128 return NULL;
129 }
130
131 return ptr;
132 }
133
134 #endif
135
136 void pa_ltdl_init(void) {
137
138 #ifdef PA_BIND_NOW
139 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
140 lt_dlloader *place;
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
145 };
146 # else
147 static const lt_dlvtable *dlopen_loader;
148 static lt_dlvtable bindnow_loader;
149 # endif
150 #endif
151
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);
156 #endif
157
158 #ifdef PA_BIND_NOW
159 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
160
161 if (!(place = lt_dlloader_find("dlopen")))
162 place = lt_dlloader_next(NULL);
163
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.");
167 # else
168 /* Already initialised */
169 if ( dlopen_loader != NULL ) return;
170
171 if (!(dlopen_loader = lt_dlloader_find("dlopen"))) {
172 pa_log_warn("Failed to find original dlopen loader.");
173 return;
174 }
175
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;
182
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.");
186 # endif
187 #endif
188 }
189
190 void pa_ltdl_done(void) {
191 pa_assert_se(lt_dlexit() == 0);
192 pa_mutex_free(libtool_mutex);
193 libtool_mutex = NULL;
194 }
195