1*404b540aSrobert /* Provide relocatable packages.
2*404b540aSrobert Copyright (C) 2003 Free Software Foundation, Inc.
3*404b540aSrobert Written by Bruno Haible <bruno@clisp.org>, 2003.
4*404b540aSrobert
5*404b540aSrobert This program is free software; you can redistribute it and/or modify it
6*404b540aSrobert under the terms of the GNU Library General Public License as published
7*404b540aSrobert by the Free Software Foundation; either version 2, or (at your option)
8*404b540aSrobert any later version.
9*404b540aSrobert
10*404b540aSrobert This program is distributed in the hope that it will be useful,
11*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
12*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*404b540aSrobert Library General Public License for more details.
14*404b540aSrobert
15*404b540aSrobert You should have received a copy of the GNU Library General Public
16*404b540aSrobert License along with this program; if not, write to the Free Software
17*404b540aSrobert Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
18*404b540aSrobert USA. */
19*404b540aSrobert
20*404b540aSrobert
21*404b540aSrobert /* Tell glibc's <stdio.h> to provide a prototype for getline().
22*404b540aSrobert This must come before <config.h> because <config.h> may include
23*404b540aSrobert <features.h>, and once <features.h> has been included, it's too late. */
24*404b540aSrobert #ifndef _GNU_SOURCE
25*404b540aSrobert # define _GNU_SOURCE 1
26*404b540aSrobert #endif
27*404b540aSrobert
28*404b540aSrobert #ifdef HAVE_CONFIG_H
29*404b540aSrobert # include "config.h"
30*404b540aSrobert #endif
31*404b540aSrobert
32*404b540aSrobert /* Specification. */
33*404b540aSrobert #include "relocatable.h"
34*404b540aSrobert
35*404b540aSrobert #if ENABLE_RELOCATABLE
36*404b540aSrobert
37*404b540aSrobert #include <stddef.h>
38*404b540aSrobert #include <stdio.h>
39*404b540aSrobert #include <stdlib.h>
40*404b540aSrobert #include <string.h>
41*404b540aSrobert
42*404b540aSrobert #ifdef NO_XMALLOC
43*404b540aSrobert # define xmalloc malloc
44*404b540aSrobert #else
45*404b540aSrobert # include "xmalloc.h"
46*404b540aSrobert #endif
47*404b540aSrobert
48*404b540aSrobert #if DEPENDS_ON_LIBCHARSET
49*404b540aSrobert # include <libcharset.h>
50*404b540aSrobert #endif
51*404b540aSrobert #if DEPENDS_ON_LIBICONV && HAVE_ICONV
52*404b540aSrobert # include <iconv.h>
53*404b540aSrobert #endif
54*404b540aSrobert #if DEPENDS_ON_LIBINTL && ENABLE_NLS
55*404b540aSrobert # include <libintl.h>
56*404b540aSrobert #endif
57*404b540aSrobert
58*404b540aSrobert /* Faked cheap 'bool'. */
59*404b540aSrobert #undef bool
60*404b540aSrobert #undef false
61*404b540aSrobert #undef true
62*404b540aSrobert #define bool int
63*404b540aSrobert #define false 0
64*404b540aSrobert #define true 1
65*404b540aSrobert
66*404b540aSrobert /* Pathname support.
67*404b540aSrobert ISSLASH(C) tests whether C is a directory separator character.
68*404b540aSrobert IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
69*404b540aSrobert */
70*404b540aSrobert #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
71*404b540aSrobert /* Win32, OS/2, DOS */
72*404b540aSrobert # define ISSLASH(C) ((C) == '/' || (C) == '\\')
73*404b540aSrobert # define HAS_DEVICE(P) \
74*404b540aSrobert ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
75*404b540aSrobert && (P)[1] == ':')
76*404b540aSrobert # define IS_PATH_WITH_DIR(P) \
77*404b540aSrobert (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
78*404b540aSrobert # define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
79*404b540aSrobert #else
80*404b540aSrobert /* Unix */
81*404b540aSrobert # define ISSLASH(C) ((C) == '/')
82*404b540aSrobert # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
83*404b540aSrobert # define FILESYSTEM_PREFIX_LEN(P) 0
84*404b540aSrobert #endif
85*404b540aSrobert
86*404b540aSrobert /* Original installation prefix. */
87*404b540aSrobert static char *orig_prefix;
88*404b540aSrobert static size_t orig_prefix_len;
89*404b540aSrobert /* Current installation prefix. */
90*404b540aSrobert static char *curr_prefix;
91*404b540aSrobert static size_t curr_prefix_len;
92*404b540aSrobert /* These prefixes do not end in a slash. Anything that will be concatenated
93*404b540aSrobert to them must start with a slash. */
94*404b540aSrobert
95*404b540aSrobert /* Sets the original and the current installation prefix of this module.
96*404b540aSrobert Relocation simply replaces a pathname starting with the original prefix
97*404b540aSrobert by the corresponding pathname with the current prefix instead. Both
98*404b540aSrobert prefixes should be directory names without trailing slash (i.e. use ""
99*404b540aSrobert instead of "/"). */
100*404b540aSrobert static void
set_this_relocation_prefix(const char * orig_prefix_arg,const char * curr_prefix_arg)101*404b540aSrobert set_this_relocation_prefix (const char *orig_prefix_arg,
102*404b540aSrobert const char *curr_prefix_arg)
103*404b540aSrobert {
104*404b540aSrobert if (orig_prefix_arg != NULL && curr_prefix_arg != NULL
105*404b540aSrobert /* Optimization: if orig_prefix and curr_prefix are equal, the
106*404b540aSrobert relocation is a nop. */
107*404b540aSrobert && strcmp (orig_prefix_arg, curr_prefix_arg) != 0)
108*404b540aSrobert {
109*404b540aSrobert /* Duplicate the argument strings. */
110*404b540aSrobert char *memory;
111*404b540aSrobert
112*404b540aSrobert orig_prefix_len = strlen (orig_prefix_arg);
113*404b540aSrobert curr_prefix_len = strlen (curr_prefix_arg);
114*404b540aSrobert memory = (char *) xmalloc (orig_prefix_len + 1 + curr_prefix_len + 1);
115*404b540aSrobert #ifdef NO_XMALLOC
116*404b540aSrobert if (memory != NULL)
117*404b540aSrobert #endif
118*404b540aSrobert {
119*404b540aSrobert memcpy (memory, orig_prefix_arg, orig_prefix_len + 1);
120*404b540aSrobert orig_prefix = memory;
121*404b540aSrobert memory += orig_prefix_len + 1;
122*404b540aSrobert memcpy (memory, curr_prefix_arg, curr_prefix_len + 1);
123*404b540aSrobert curr_prefix = memory;
124*404b540aSrobert return;
125*404b540aSrobert }
126*404b540aSrobert }
127*404b540aSrobert orig_prefix = NULL;
128*404b540aSrobert curr_prefix = NULL;
129*404b540aSrobert /* Don't worry about wasted memory here - this function is usually only
130*404b540aSrobert called once. */
131*404b540aSrobert }
132*404b540aSrobert
133*404b540aSrobert /* Sets the original and the current installation prefix of the package.
134*404b540aSrobert Relocation simply replaces a pathname starting with the original prefix
135*404b540aSrobert by the corresponding pathname with the current prefix instead. Both
136*404b540aSrobert prefixes should be directory names without trailing slash (i.e. use ""
137*404b540aSrobert instead of "/"). */
138*404b540aSrobert void
set_relocation_prefix(const char * orig_prefix_arg,const char * curr_prefix_arg)139*404b540aSrobert set_relocation_prefix (const char *orig_prefix_arg, const char *curr_prefix_arg)
140*404b540aSrobert {
141*404b540aSrobert set_this_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
142*404b540aSrobert
143*404b540aSrobert /* Now notify all dependent libraries. */
144*404b540aSrobert #if DEPENDS_ON_LIBCHARSET
145*404b540aSrobert libcharset_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
146*404b540aSrobert #endif
147*404b540aSrobert #if DEPENDS_ON_LIBICONV && HAVE_ICONV && _LIBICONV_VERSION >= 0x0109
148*404b540aSrobert libiconv_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
149*404b540aSrobert #endif
150*404b540aSrobert #if DEPENDS_ON_LIBINTL && ENABLE_NLS && defined libintl_set_relocation_prefix
151*404b540aSrobert libintl_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
152*404b540aSrobert #endif
153*404b540aSrobert }
154*404b540aSrobert
155*404b540aSrobert /* Convenience function:
156*404b540aSrobert Computes the current installation prefix, based on the original
157*404b540aSrobert installation prefix, the original installation directory of a particular
158*404b540aSrobert file, and the current pathname of this file. Returns NULL upon failure. */
159*404b540aSrobert #ifdef IN_LIBRARY
160*404b540aSrobert #define compute_curr_prefix local_compute_curr_prefix
161*404b540aSrobert static
162*404b540aSrobert #endif
163*404b540aSrobert const char *
compute_curr_prefix(const char * orig_installprefix,const char * orig_installdir,const char * curr_pathname)164*404b540aSrobert compute_curr_prefix (const char *orig_installprefix,
165*404b540aSrobert const char *orig_installdir,
166*404b540aSrobert const char *curr_pathname)
167*404b540aSrobert {
168*404b540aSrobert const char *curr_installdir;
169*404b540aSrobert const char *rel_installdir;
170*404b540aSrobert
171*404b540aSrobert if (curr_pathname == NULL)
172*404b540aSrobert return NULL;
173*404b540aSrobert
174*404b540aSrobert /* Determine the relative installation directory, relative to the prefix.
175*404b540aSrobert This is simply the difference between orig_installprefix and
176*404b540aSrobert orig_installdir. */
177*404b540aSrobert if (strncmp (orig_installprefix, orig_installdir, strlen (orig_installprefix))
178*404b540aSrobert != 0)
179*404b540aSrobert /* Shouldn't happen - nothing should be installed outside $(prefix). */
180*404b540aSrobert return NULL;
181*404b540aSrobert rel_installdir = orig_installdir + strlen (orig_installprefix);
182*404b540aSrobert
183*404b540aSrobert /* Determine the current installation directory. */
184*404b540aSrobert {
185*404b540aSrobert const char *p_base = curr_pathname + FILESYSTEM_PREFIX_LEN (curr_pathname);
186*404b540aSrobert const char *p = curr_pathname + strlen (curr_pathname);
187*404b540aSrobert char *q;
188*404b540aSrobert
189*404b540aSrobert while (p > p_base)
190*404b540aSrobert {
191*404b540aSrobert p--;
192*404b540aSrobert if (ISSLASH (*p))
193*404b540aSrobert break;
194*404b540aSrobert }
195*404b540aSrobert
196*404b540aSrobert q = (char *) xmalloc (p - curr_pathname + 1);
197*404b540aSrobert #ifdef NO_XMALLOC
198*404b540aSrobert if (q == NULL)
199*404b540aSrobert return NULL;
200*404b540aSrobert #endif
201*404b540aSrobert memcpy (q, curr_pathname, p - curr_pathname);
202*404b540aSrobert q[p - curr_pathname] = '\0';
203*404b540aSrobert curr_installdir = q;
204*404b540aSrobert }
205*404b540aSrobert
206*404b540aSrobert /* Compute the current installation prefix by removing the trailing
207*404b540aSrobert rel_installdir from it. */
208*404b540aSrobert {
209*404b540aSrobert const char *rp = rel_installdir + strlen (rel_installdir);
210*404b540aSrobert const char *cp = curr_installdir + strlen (curr_installdir);
211*404b540aSrobert const char *cp_base =
212*404b540aSrobert curr_installdir + FILESYSTEM_PREFIX_LEN (curr_installdir);
213*404b540aSrobert
214*404b540aSrobert while (rp > rel_installdir && cp > cp_base)
215*404b540aSrobert {
216*404b540aSrobert bool same = false;
217*404b540aSrobert const char *rpi = rp;
218*404b540aSrobert const char *cpi = cp;
219*404b540aSrobert
220*404b540aSrobert while (rpi > rel_installdir && cpi > cp_base)
221*404b540aSrobert {
222*404b540aSrobert rpi--;
223*404b540aSrobert cpi--;
224*404b540aSrobert if (ISSLASH (*rpi) || ISSLASH (*cpi))
225*404b540aSrobert {
226*404b540aSrobert if (ISSLASH (*rpi) && ISSLASH (*cpi))
227*404b540aSrobert same = true;
228*404b540aSrobert break;
229*404b540aSrobert }
230*404b540aSrobert #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
231*404b540aSrobert /* Win32, OS/2, DOS - case insignificant filesystem */
232*404b540aSrobert if ((*rpi >= 'a' && *rpi <= 'z' ? *rpi - 'a' + 'A' : *rpi)
233*404b540aSrobert != (*cpi >= 'a' && *cpi <= 'z' ? *cpi - 'a' + 'A' : *cpi))
234*404b540aSrobert break;
235*404b540aSrobert #else
236*404b540aSrobert if (*rpi != *cpi)
237*404b540aSrobert break;
238*404b540aSrobert #endif
239*404b540aSrobert }
240*404b540aSrobert if (!same)
241*404b540aSrobert break;
242*404b540aSrobert /* The last pathname component was the same. opi and cpi now point
243*404b540aSrobert to the slash before it. */
244*404b540aSrobert rp = rpi;
245*404b540aSrobert cp = cpi;
246*404b540aSrobert }
247*404b540aSrobert
248*404b540aSrobert if (rp > rel_installdir)
249*404b540aSrobert /* Unexpected: The curr_installdir does not end with rel_installdir. */
250*404b540aSrobert return NULL;
251*404b540aSrobert
252*404b540aSrobert {
253*404b540aSrobert size_t curr_prefix_len = cp - curr_installdir;
254*404b540aSrobert char *curr_prefix;
255*404b540aSrobert
256*404b540aSrobert curr_prefix = (char *) xmalloc (curr_prefix_len + 1);
257*404b540aSrobert #ifdef NO_XMALLOC
258*404b540aSrobert if (curr_prefix == NULL)
259*404b540aSrobert return NULL;
260*404b540aSrobert #endif
261*404b540aSrobert memcpy (curr_prefix, curr_installdir, curr_prefix_len);
262*404b540aSrobert curr_prefix[curr_prefix_len] = '\0';
263*404b540aSrobert
264*404b540aSrobert return curr_prefix;
265*404b540aSrobert }
266*404b540aSrobert }
267*404b540aSrobert }
268*404b540aSrobert
269*404b540aSrobert #if defined PIC && defined INSTALLDIR
270*404b540aSrobert
271*404b540aSrobert /* Full pathname of shared library, or NULL. */
272*404b540aSrobert static char *shared_library_fullname;
273*404b540aSrobert
274*404b540aSrobert #if defined _WIN32 || defined __WIN32__
275*404b540aSrobert
276*404b540aSrobert /* Determine the full pathname of the shared library when it is loaded. */
277*404b540aSrobert
278*404b540aSrobert BOOL WINAPI
DllMain(HINSTANCE module_handle,DWORD event,LPVOID reserved)279*404b540aSrobert DllMain (HINSTANCE module_handle, DWORD event, LPVOID reserved)
280*404b540aSrobert {
281*404b540aSrobert (void) reserved;
282*404b540aSrobert
283*404b540aSrobert if (event == DLL_PROCESS_ATTACH)
284*404b540aSrobert {
285*404b540aSrobert /* The DLL is being loaded into an application's address range. */
286*404b540aSrobert static char location[MAX_PATH];
287*404b540aSrobert
288*404b540aSrobert if (!GetModuleFileName (module_handle, location, sizeof (location)))
289*404b540aSrobert /* Shouldn't happen. */
290*404b540aSrobert return FALSE;
291*404b540aSrobert
292*404b540aSrobert if (!IS_PATH_WITH_DIR (location))
293*404b540aSrobert /* Shouldn't happen. */
294*404b540aSrobert return FALSE;
295*404b540aSrobert
296*404b540aSrobert shared_library_fullname = strdup (location);
297*404b540aSrobert }
298*404b540aSrobert
299*404b540aSrobert return TRUE;
300*404b540aSrobert }
301*404b540aSrobert
302*404b540aSrobert #else /* Unix */
303*404b540aSrobert
304*404b540aSrobert static void
find_shared_library_fullname()305*404b540aSrobert find_shared_library_fullname ()
306*404b540aSrobert {
307*404b540aSrobert #ifdef __linux__
308*404b540aSrobert FILE *fp;
309*404b540aSrobert
310*404b540aSrobert /* Open the current process' maps file. It describes one VMA per line. */
311*404b540aSrobert fp = fopen ("/proc/self/maps", "r");
312*404b540aSrobert if (fp)
313*404b540aSrobert {
314*404b540aSrobert unsigned long address = (unsigned long) &find_shared_library_fullname;
315*404b540aSrobert for (;;)
316*404b540aSrobert {
317*404b540aSrobert unsigned long start, end;
318*404b540aSrobert int c;
319*404b540aSrobert
320*404b540aSrobert if (fscanf (fp, "%lx-%lx", &start, &end) != 2)
321*404b540aSrobert break;
322*404b540aSrobert if (address >= start && address <= end - 1)
323*404b540aSrobert {
324*404b540aSrobert /* Found it. Now see if this line contains a filename. */
325*404b540aSrobert while (c = getc (fp), c != EOF && c != '\n' && c != '/')
326*404b540aSrobert continue;
327*404b540aSrobert if (c == '/')
328*404b540aSrobert {
329*404b540aSrobert size_t size;
330*404b540aSrobert int len;
331*404b540aSrobert
332*404b540aSrobert ungetc (c, fp);
333*404b540aSrobert shared_library_fullname = NULL; size = 0;
334*404b540aSrobert len = getline (&shared_library_fullname, &size, fp);
335*404b540aSrobert if (len >= 0)
336*404b540aSrobert {
337*404b540aSrobert /* Success: filled shared_library_fullname. */
338*404b540aSrobert if (len > 0 && shared_library_fullname[len - 1] == '\n')
339*404b540aSrobert shared_library_fullname[len - 1] = '\0';
340*404b540aSrobert }
341*404b540aSrobert }
342*404b540aSrobert break;
343*404b540aSrobert }
344*404b540aSrobert while (c = getc (fp), c != EOF && c != '\n')
345*404b540aSrobert continue;
346*404b540aSrobert }
347*404b540aSrobert fclose (fp);
348*404b540aSrobert }
349*404b540aSrobert #endif
350*404b540aSrobert }
351*404b540aSrobert
352*404b540aSrobert #endif /* WIN32 / Unix */
353*404b540aSrobert
354*404b540aSrobert /* Return the full pathname of the current shared library.
355*404b540aSrobert Return NULL if unknown.
356*404b540aSrobert Guaranteed to work only on Linux and Woe32. */
357*404b540aSrobert static char *
get_shared_library_fullname()358*404b540aSrobert get_shared_library_fullname ()
359*404b540aSrobert {
360*404b540aSrobert #if !(defined _WIN32 || defined __WIN32__)
361*404b540aSrobert static bool tried_find_shared_library_fullname;
362*404b540aSrobert if (!tried_find_shared_library_fullname)
363*404b540aSrobert {
364*404b540aSrobert find_shared_library_fullname ();
365*404b540aSrobert tried_find_shared_library_fullname = true;
366*404b540aSrobert }
367*404b540aSrobert #endif
368*404b540aSrobert return shared_library_fullname;
369*404b540aSrobert }
370*404b540aSrobert
371*404b540aSrobert #endif /* PIC */
372*404b540aSrobert
373*404b540aSrobert /* Returns the pathname, relocated according to the current installation
374*404b540aSrobert directory. */
375*404b540aSrobert const char *
relocate(const char * pathname)376*404b540aSrobert relocate (const char *pathname)
377*404b540aSrobert {
378*404b540aSrobert #if defined PIC && defined INSTALLDIR
379*404b540aSrobert static int initialized;
380*404b540aSrobert
381*404b540aSrobert /* Initialization code for a shared library. */
382*404b540aSrobert if (!initialized)
383*404b540aSrobert {
384*404b540aSrobert /* At this point, orig_prefix and curr_prefix likely have already been
385*404b540aSrobert set through the main program's set_program_name_and_installdir
386*404b540aSrobert function. This is sufficient in the case that the library has
387*404b540aSrobert initially been installed in the same orig_prefix. But we can do
388*404b540aSrobert better, to also cover the cases that 1. it has been installed
389*404b540aSrobert in a different prefix before being moved to orig_prefix and (later)
390*404b540aSrobert to curr_prefix, 2. unlike the program, it has not moved away from
391*404b540aSrobert orig_prefix. */
392*404b540aSrobert const char *orig_installprefix = INSTALLPREFIX;
393*404b540aSrobert const char *orig_installdir = INSTALLDIR;
394*404b540aSrobert const char *curr_prefix_better;
395*404b540aSrobert
396*404b540aSrobert curr_prefix_better =
397*404b540aSrobert compute_curr_prefix (orig_installprefix, orig_installdir,
398*404b540aSrobert get_shared_library_fullname ());
399*404b540aSrobert if (curr_prefix_better == NULL)
400*404b540aSrobert curr_prefix_better = curr_prefix;
401*404b540aSrobert
402*404b540aSrobert set_relocation_prefix (orig_installprefix, curr_prefix_better);
403*404b540aSrobert
404*404b540aSrobert initialized = 1;
405*404b540aSrobert }
406*404b540aSrobert #endif
407*404b540aSrobert
408*404b540aSrobert /* Note: It is not necessary to perform case insensitive comparison here,
409*404b540aSrobert even for DOS-like filesystems, because the pathname argument was
410*404b540aSrobert typically created from the same Makefile variable as orig_prefix came
411*404b540aSrobert from. */
412*404b540aSrobert if (orig_prefix != NULL && curr_prefix != NULL
413*404b540aSrobert && strncmp (pathname, orig_prefix, orig_prefix_len) == 0)
414*404b540aSrobert {
415*404b540aSrobert if (pathname[orig_prefix_len] == '\0')
416*404b540aSrobert /* pathname equals orig_prefix. */
417*404b540aSrobert return curr_prefix;
418*404b540aSrobert if (ISSLASH (pathname[orig_prefix_len]))
419*404b540aSrobert {
420*404b540aSrobert /* pathname starts with orig_prefix. */
421*404b540aSrobert const char *pathname_tail = &pathname[orig_prefix_len];
422*404b540aSrobert char *result =
423*404b540aSrobert (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1);
424*404b540aSrobert
425*404b540aSrobert #ifdef NO_XMALLOC
426*404b540aSrobert if (result != NULL)
427*404b540aSrobert #endif
428*404b540aSrobert {
429*404b540aSrobert memcpy (result, curr_prefix, curr_prefix_len);
430*404b540aSrobert strcpy (result + curr_prefix_len, pathname_tail);
431*404b540aSrobert return result;
432*404b540aSrobert }
433*404b540aSrobert }
434*404b540aSrobert }
435*404b540aSrobert /* Nothing to relocate. */
436*404b540aSrobert return pathname;
437*404b540aSrobert }
438*404b540aSrobert
439*404b540aSrobert #endif
440