1 /* Provide relocatable packages.
2 Copyright (C) 2003-2006, 2008-2009 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 /* Tell glibc's <stdio.h> to provide a prototype for getline().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
25 # define _GNU_SOURCE 1
31 #include "relocatable.h"
33 #if ENABLE_RELOCATABLE
41 # define xmalloc malloc
46 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
47 # define WIN32_LEAN_AND_MEAN
51 #if DEPENDS_ON_LIBCHARSET
52 # include <libcharset.h>
54 #if DEPENDS_ON_LIBICONV && HAVE_ICONV
57 #if DEPENDS_ON_LIBINTL && ENABLE_NLS
61 /* Faked cheap 'bool'. */
70 ISSLASH(C) tests whether C is a directory separator character.
71 IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
73 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
74 /* Win32, Cygwin, OS/2, DOS */
75 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
76 # define HAS_DEVICE(P) \
77 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
79 # define IS_PATH_WITH_DIR(P) \
80 (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
81 # define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
84 # define ISSLASH(C) ((C) == '/')
85 # define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
86 # define FILE_SYSTEM_PREFIX_LEN(P) 0
89 /* Original installation prefix. */
90 static char *orig_prefix;
91 static size_t orig_prefix_len;
92 /* Current installation prefix. */
93 static char *curr_prefix;
94 static size_t curr_prefix_len;
95 /* These prefixes do not end in a slash. Anything that will be concatenated
96 to them must start with a slash. */
98 /* Sets the original and the current installation prefix of this module.
99 Relocation simply replaces a pathname starting with the original prefix
100 by the corresponding pathname with the current prefix instead. Both
101 prefixes should be directory names without trailing slash (i.e. use ""
104 set_this_relocation_prefix (const char *orig_prefix_arg,
105 const char *curr_prefix_arg)
107 if (orig_prefix_arg != NULL && curr_prefix_arg != NULL
108 /* Optimization: if orig_prefix and curr_prefix are equal, the
109 relocation is a nop. */
110 && strcmp (orig_prefix_arg, curr_prefix_arg) != 0)
112 /* Duplicate the argument strings. */
115 orig_prefix_len = strlen (orig_prefix_arg);
116 curr_prefix_len = strlen (curr_prefix_arg);
117 memory = (char *) xmalloc (orig_prefix_len + 1 + curr_prefix_len + 1);
122 memcpy (memory, orig_prefix_arg, orig_prefix_len + 1);
123 orig_prefix = memory;
124 memory += orig_prefix_len + 1;
125 memcpy (memory, curr_prefix_arg, curr_prefix_len + 1);
126 curr_prefix = memory;
132 /* Don't worry about wasted memory here - this function is usually only
136 /* Sets the original and the current installation prefix of the package.
137 Relocation simply replaces a pathname starting with the original prefix
138 by the corresponding pathname with the current prefix instead. Both
139 prefixes should be directory names without trailing slash (i.e. use ""
142 set_relocation_prefix (const char *orig_prefix_arg, const char *curr_prefix_arg)
144 set_this_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
146 /* Now notify all dependent libraries. */
147 #if DEPENDS_ON_LIBCHARSET
148 libcharset_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
150 #if DEPENDS_ON_LIBICONV && HAVE_ICONV && _LIBICONV_VERSION >= 0x0109
151 libiconv_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
153 #if DEPENDS_ON_LIBINTL && ENABLE_NLS && defined libintl_set_relocation_prefix
154 libintl_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
158 #if !defined IN_LIBRARY || (defined PIC && defined INSTALLDIR)
160 /* Convenience function:
161 Computes the current installation prefix, based on the original
162 installation prefix, the original installation directory of a particular
163 file, and the current pathname of this file.
164 Returns it, freshly allocated. Returns NULL upon failure. */
166 #define compute_curr_prefix local_compute_curr_prefix
170 compute_curr_prefix (const char *orig_installprefix,
171 const char *orig_installdir,
172 const char *curr_pathname)
174 char *curr_installdir;
175 const char *rel_installdir;
177 if (curr_pathname == NULL)
180 /* Determine the relative installation directory, relative to the prefix.
181 This is simply the difference between orig_installprefix and
183 if (strncmp (orig_installprefix, orig_installdir, strlen (orig_installprefix))
185 /* Shouldn't happen - nothing should be installed outside $(prefix). */
187 rel_installdir = orig_installdir + strlen (orig_installprefix);
189 /* Determine the current installation directory. */
191 const char *p_base = curr_pathname + FILE_SYSTEM_PREFIX_LEN (curr_pathname);
192 const char *p = curr_pathname + strlen (curr_pathname);
202 q = (char *) xmalloc (p - curr_pathname + 1);
207 memcpy (q, curr_pathname, p - curr_pathname);
208 q[p - curr_pathname] = '\0';
212 /* Compute the current installation prefix by removing the trailing
213 rel_installdir from it. */
215 const char *rp = rel_installdir + strlen (rel_installdir);
216 const char *cp = curr_installdir + strlen (curr_installdir);
217 const char *cp_base =
218 curr_installdir + FILE_SYSTEM_PREFIX_LEN (curr_installdir);
220 while (rp > rel_installdir && cp > cp_base)
223 const char *rpi = rp;
224 const char *cpi = cp;
226 while (rpi > rel_installdir && cpi > cp_base)
230 if (ISSLASH (*rpi) || ISSLASH (*cpi))
232 if (ISSLASH (*rpi) && ISSLASH (*cpi))
236 /* Do case-insensitive comparison if the file system is always or
237 often case-insensitive. It's better to accept the comparison
238 if the difference is only in case, rather than to fail. */
239 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
240 /* Win32, Cygwin, OS/2, DOS - case insignificant file system */
241 if ((*rpi >= 'a' && *rpi <= 'z' ? *rpi - 'a' + 'A' : *rpi)
242 != (*cpi >= 'a' && *cpi <= 'z' ? *cpi - 'a' + 'A' : *cpi))
251 /* The last pathname component was the same. opi and cpi now point
252 to the slash before it. */
257 if (rp > rel_installdir)
259 /* Unexpected: The curr_installdir does not end with rel_installdir. */
260 free (curr_installdir);
265 size_t curr_prefix_len = cp - curr_installdir;
268 curr_prefix = (char *) xmalloc (curr_prefix_len + 1);
270 if (curr_prefix == NULL)
272 free (curr_installdir);
276 memcpy (curr_prefix, curr_installdir, curr_prefix_len);
277 curr_prefix[curr_prefix_len] = '\0';
279 free (curr_installdir);
286 #endif /* !IN_LIBRARY || PIC */
288 #if defined PIC && defined INSTALLDIR
290 /* Full pathname of shared library, or NULL. */
291 static char *shared_library_fullname;
293 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
295 /* Determine the full pathname of the shared library when it is loaded. */
298 DllMain (HINSTANCE module_handle, DWORD event, LPVOID reserved)
302 if (event == DLL_PROCESS_ATTACH)
304 /* The DLL is being loaded into an application's address range. */
305 static char location[MAX_PATH];
307 if (!GetModuleFileName (module_handle, location, sizeof (location)))
308 /* Shouldn't happen. */
311 if (!IS_PATH_WITH_DIR (location))
312 /* Shouldn't happen. */
316 #if defined __CYGWIN__
317 /* On Cygwin, we need to convert paths coming from Win32 system calls
318 to the Unix-like slashified notation. */
319 static char location_as_posix_path[2 * MAX_PATH];
320 /* There's no error return defined for cygwin_conv_to_posix_path.
321 See cygwin-api/func-cygwin-conv-to-posix-path.html.
322 Does it overflow the buffer of expected size MAX_PATH or does it
323 truncate the path? I don't know. Let's catch both. */
324 cygwin_conv_to_posix_path (location, location_as_posix_path);
325 location_as_posix_path[MAX_PATH - 1] = '\0';
326 if (strlen (location_as_posix_path) >= MAX_PATH - 1)
327 /* A sign of buffer overflow or path truncation. */
329 shared_library_fullname = strdup (location_as_posix_path);
331 shared_library_fullname = strdup (location);
339 #else /* Unix except Cygwin */
342 find_shared_library_fullname ()
344 #if defined __linux__ && __GLIBC__ >= 2
345 /* Linux has /proc/self/maps. glibc 2 has the getline() function. */
348 /* Open the current process' maps file. It describes one VMA per line. */
349 fp = fopen ("/proc/self/maps", "r");
352 unsigned long address = (unsigned long) &find_shared_library_fullname;
355 unsigned long start, end;
358 if (fscanf (fp, "%lx-%lx", &start, &end) != 2)
360 if (address >= start && address <= end - 1)
362 /* Found it. Now see if this line contains a filename. */
363 while (c = getc (fp), c != EOF && c != '\n' && c != '/')
371 shared_library_fullname = NULL; size = 0;
372 len = getline (&shared_library_fullname, &size, fp);
375 /* Success: filled shared_library_fullname. */
376 if (len > 0 && shared_library_fullname[len - 1] == '\n')
377 shared_library_fullname[len - 1] = '\0';
382 while (c = getc (fp), c != EOF && c != '\n')
390 #endif /* (WIN32 or Cygwin) / (Unix except Cygwin) */
392 /* Return the full pathname of the current shared library.
393 Return NULL if unknown.
394 Guaranteed to work only on Linux, Cygwin and Woe32. */
396 get_shared_library_fullname ()
398 #if !(defined _WIN32 || defined __WIN32__ || defined __CYGWIN__)
399 static bool tried_find_shared_library_fullname;
400 if (!tried_find_shared_library_fullname)
402 find_shared_library_fullname ();
403 tried_find_shared_library_fullname = true;
406 return shared_library_fullname;
411 /* Returns the pathname, relocated according to the current installation
413 The returned string is either PATHNAME unmodified or a freshly allocated
414 string that you can free with free() after casting it to 'char *'. */
416 relocate (const char *pathname)
418 #if defined PIC && defined INSTALLDIR
419 static int initialized;
421 /* Initialization code for a shared library. */
424 /* At this point, orig_prefix and curr_prefix likely have already been
425 set through the main program's set_program_name_and_installdir
426 function. This is sufficient in the case that the library has
427 initially been installed in the same orig_prefix. But we can do
428 better, to also cover the cases that 1. it has been installed
429 in a different prefix before being moved to orig_prefix and (later)
430 to curr_prefix, 2. unlike the program, it has not moved away from
432 const char *orig_installprefix = INSTALLPREFIX;
433 const char *orig_installdir = INSTALLDIR;
434 char *curr_prefix_better;
437 compute_curr_prefix (orig_installprefix, orig_installdir,
438 get_shared_library_fullname ());
440 set_relocation_prefix (orig_installprefix,
441 curr_prefix_better != NULL
445 if (curr_prefix_better != NULL)
446 free (curr_prefix_better);
452 /* Note: It is not necessary to perform case insensitive comparison here,
453 even for DOS-like file systems, because the pathname argument was
454 typically created from the same Makefile variable as orig_prefix came
456 if (orig_prefix != NULL && curr_prefix != NULL
457 && strncmp (pathname, orig_prefix, orig_prefix_len) == 0)
459 if (pathname[orig_prefix_len] == '\0')
461 /* pathname equals orig_prefix. */
462 char *result = (char *) xmalloc (strlen (curr_prefix) + 1);
468 strcpy (result, curr_prefix);
472 else if (ISSLASH (pathname[orig_prefix_len]))
474 /* pathname starts with orig_prefix. */
475 const char *pathname_tail = &pathname[orig_prefix_len];
477 (char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1);
483 memcpy (result, curr_prefix, curr_prefix_len);
484 strcpy (result + curr_prefix_len, pathname_tail);
489 /* Nothing to relocate. */