]> code.delx.au - pulseaudio/blob - src/daemon/ltdl-bind-now.c
Update code to use libtool 2.2. Use convenience recursive libltdl.
[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 PA_BIND_NOW
57
58 /*
59 To avoid lazy relocations during runtime in our RT threads we add
60 our own shared object loader with uses RTLD_NOW if it is
61 available. The standard ltdl loader prefers RTLD_LAZY.
62
63 Please note that this loader doesn't have any influence on
64 relocations on any libraries that are already loaded into our
65 process, i.e. because the pulseaudio binary links directly to
66 them. To disable lazy relocations for those libraries it is possible
67 to set $LT_BIND_NOW before starting the pulsaudio binary.
68 */
69
70 static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise)
71 {
72 lt_module m;
73
74 pa_assert(fname);
75
76 if (!(m = dlopen(fname, PA_BIND_NOW))) {
77 libtool_set_error(dlerror());
78 return NULL;
79 }
80
81 return m;
82 }
83
84 static int bind_now_close(lt_user_data d, lt_module m) {
85
86 pa_assert(m);
87
88 if (dlclose(m) != 0){
89 libtool_set_error(dlerror());
90 return 1;
91 }
92
93 return 0;
94 }
95
96 static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol) {
97 lt_ptr ptr;
98
99 pa_assert(m);
100 pa_assert(symbol);
101
102 if (!(ptr = dlsym(m, symbol))) {
103 libtool_set_error(dlerror());
104 return NULL;
105 }
106
107 return ptr;
108 }
109
110 #endif
111
112 void pa_ltdl_init(void) {
113
114 #ifdef PA_BIND_NOW
115 static const lt_dlvtable *dlopen_loader;
116 static lt_dlvtable bindnow_loader;
117 #endif
118
119 pa_assert_se(lt_dlinit() == 0);
120
121 #ifdef PA_BIND_NOW
122 /* Already initialised */
123 if (dlopen_loader)
124 return;
125
126 if (!(dlopen_loader = lt_dlloader_find("dlopen"))) {
127 pa_log_warn(_("Failed to find original dlopen loader."));
128 return;
129 }
130
131 memcpy(&bindnow_loader, dlopen_loader, sizeof(bindnow_loader));
132 bindnow_loader.name = "bind-now-loader";
133 bindnow_loader.module_open = bind_now_open;
134 bindnow_loader.module_close = bind_now_close;
135 bindnow_loader.find_sym = bind_now_find_sym;
136 bindnow_loader.priority = LT_DLLOADER_PREPEND;
137
138 /* Add our BIND_NOW loader as the default module loader. */
139 if (lt_dlloader_add(&bindnow_loader) != 0)
140 pa_log_warn(_("Failed to add bind-now-loader."));
141 #endif
142 }
143
144 void pa_ltdl_done(void) {
145 pa_assert_se(lt_dlexit() == 0);
146 }