2003-10-25 18:30:04 +02:00
|
|
|
/* Provide relocatable packages.
|
2015-02-26 19:01:30 +01:00
|
|
|
Copyright (C) 2003-2006, 2008-2011 Free Software Foundation, Inc.
|
2003-10-25 18:30:04 +02:00
|
|
|
Written by Bruno Haible <bruno@clisp.org>, 2003.
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-02-26 19:01:30 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
2003-10-25 18:30:04 +02:00
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Tell glibc's <stdio.h> to provide a prototype for getline().
|
|
|
|
This must come before <config.h> because <config.h> may include
|
|
|
|
<features.h>, and once <features.h> has been included, it's too late. */
|
|
|
|
#ifndef _GNU_SOURCE
|
2012-12-14 16:08:23 +01:00
|
|
|
# define _GNU_SOURCE 1
|
2003-10-25 18:30:04 +02:00
|
|
|
#endif
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#define _GL_USE_STDLIB_ALLOC 1
|
2007-10-23 12:48:09 +02:00
|
|
|
#include <config.h>
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
/* Specification. */
|
|
|
|
#include "relocatable.h"
|
|
|
|
|
|
|
|
#if ENABLE_RELOCATABLE
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef NO_XMALLOC
|
|
|
|
# define xmalloc malloc
|
|
|
|
#else
|
2004-07-27 17:12:00 +02:00
|
|
|
# include "xalloc.h"
|
|
|
|
#endif
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
|
2004-07-27 17:12:00 +02:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
2003-10-25 18:30:04 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEPENDS_ON_LIBCHARSET
|
|
|
|
# include <libcharset.h>
|
|
|
|
#endif
|
|
|
|
#if DEPENDS_ON_LIBICONV && HAVE_ICONV
|
|
|
|
# include <iconv.h>
|
|
|
|
#endif
|
|
|
|
#if DEPENDS_ON_LIBINTL && ENABLE_NLS
|
|
|
|
# include <libintl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Faked cheap 'bool'. */
|
|
|
|
#undef bool
|
|
|
|
#undef false
|
|
|
|
#undef true
|
|
|
|
#define bool int
|
|
|
|
#define false 0
|
|
|
|
#define true 1
|
|
|
|
|
|
|
|
/* Pathname support.
|
|
|
|
ISSLASH(C) tests whether C is a directory separator character.
|
|
|
|
IS_PATH_WITH_DIR(P) tests whether P contains a directory specification.
|
|
|
|
*/
|
2015-02-26 19:01:30 +01:00
|
|
|
#if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __EMX__ || defined __DJGPP__
|
|
|
|
/* Native Windows, OS/2, DOS */
|
2003-10-25 18:30:04 +02:00
|
|
|
# define ISSLASH(C) ((C) == '/' || (C) == '\\')
|
|
|
|
# define HAS_DEVICE(P) \
|
|
|
|
((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
|
|
|
|
&& (P)[1] == ':')
|
|
|
|
# define IS_PATH_WITH_DIR(P) \
|
|
|
|
(strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
|
2007-10-23 12:48:09 +02:00
|
|
|
# define FILE_SYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
|
2003-10-25 18:30:04 +02:00
|
|
|
#else
|
|
|
|
/* Unix */
|
|
|
|
# define ISSLASH(C) ((C) == '/')
|
|
|
|
# define IS_PATH_WITH_DIR(P) (strchr (P, '/') != NULL)
|
2007-10-23 12:48:09 +02:00
|
|
|
# define FILE_SYSTEM_PREFIX_LEN(P) 0
|
2003-10-25 18:30:04 +02:00
|
|
|
#endif
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
/* Whether to enable the more costly support for relocatable libraries.
|
|
|
|
It allows libraries to be have been installed with a different original
|
|
|
|
prefix than the program. But it is quite costly, especially on Cygwin
|
|
|
|
platforms, see below. Therefore we enable it by default only on native
|
|
|
|
Windows platforms. */
|
|
|
|
#ifndef ENABLE_COSTLY_RELOCATABLE
|
|
|
|
# if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
|
|
|
|
# define ENABLE_COSTLY_RELOCATABLE 1
|
|
|
|
# else
|
|
|
|
# define ENABLE_COSTLY_RELOCATABLE 0
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2003-10-25 18:30:04 +02:00
|
|
|
/* Original installation prefix. */
|
|
|
|
static char *orig_prefix;
|
|
|
|
static size_t orig_prefix_len;
|
|
|
|
/* Current installation prefix. */
|
|
|
|
static char *curr_prefix;
|
|
|
|
static size_t curr_prefix_len;
|
|
|
|
/* These prefixes do not end in a slash. Anything that will be concatenated
|
|
|
|
to them must start with a slash. */
|
|
|
|
|
|
|
|
/* Sets the original and the current installation prefix of this module.
|
|
|
|
Relocation simply replaces a pathname starting with the original prefix
|
|
|
|
by the corresponding pathname with the current prefix instead. Both
|
|
|
|
prefixes should be directory names without trailing slash (i.e. use ""
|
|
|
|
instead of "/"). */
|
|
|
|
static void
|
|
|
|
set_this_relocation_prefix (const char *orig_prefix_arg,
|
2012-12-14 16:08:23 +01:00
|
|
|
const char *curr_prefix_arg)
|
2003-10-25 18:30:04 +02:00
|
|
|
{
|
|
|
|
if (orig_prefix_arg != NULL && curr_prefix_arg != NULL
|
|
|
|
/* Optimization: if orig_prefix and curr_prefix are equal, the
|
2012-12-14 16:08:23 +01:00
|
|
|
relocation is a nop. */
|
2003-10-25 18:30:04 +02:00
|
|
|
&& strcmp (orig_prefix_arg, curr_prefix_arg) != 0)
|
|
|
|
{
|
|
|
|
/* Duplicate the argument strings. */
|
|
|
|
char *memory;
|
|
|
|
|
|
|
|
orig_prefix_len = strlen (orig_prefix_arg);
|
|
|
|
curr_prefix_len = strlen (curr_prefix_arg);
|
|
|
|
memory = (char *) xmalloc (orig_prefix_len + 1 + curr_prefix_len + 1);
|
|
|
|
#ifdef NO_XMALLOC
|
|
|
|
if (memory != NULL)
|
|
|
|
#endif
|
2012-12-14 16:08:23 +01:00
|
|
|
{
|
|
|
|
memcpy (memory, orig_prefix_arg, orig_prefix_len + 1);
|
|
|
|
orig_prefix = memory;
|
|
|
|
memory += orig_prefix_len + 1;
|
|
|
|
memcpy (memory, curr_prefix_arg, curr_prefix_len + 1);
|
|
|
|
curr_prefix = memory;
|
|
|
|
return;
|
|
|
|
}
|
2003-10-25 18:30:04 +02:00
|
|
|
}
|
|
|
|
orig_prefix = NULL;
|
|
|
|
curr_prefix = NULL;
|
|
|
|
/* Don't worry about wasted memory here - this function is usually only
|
|
|
|
called once. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets the original and the current installation prefix of the package.
|
|
|
|
Relocation simply replaces a pathname starting with the original prefix
|
|
|
|
by the corresponding pathname with the current prefix instead. Both
|
|
|
|
prefixes should be directory names without trailing slash (i.e. use ""
|
|
|
|
instead of "/"). */
|
|
|
|
void
|
|
|
|
set_relocation_prefix (const char *orig_prefix_arg, const char *curr_prefix_arg)
|
|
|
|
{
|
|
|
|
set_this_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
|
|
|
|
|
|
|
|
/* Now notify all dependent libraries. */
|
|
|
|
#if DEPENDS_ON_LIBCHARSET
|
|
|
|
libcharset_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
|
|
|
|
#endif
|
|
|
|
#if DEPENDS_ON_LIBICONV && HAVE_ICONV && _LIBICONV_VERSION >= 0x0109
|
|
|
|
libiconv_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
|
|
|
|
#endif
|
|
|
|
#if DEPENDS_ON_LIBINTL && ENABLE_NLS && defined libintl_set_relocation_prefix
|
|
|
|
libintl_set_relocation_prefix (orig_prefix_arg, curr_prefix_arg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#if !defined IN_LIBRARY || (defined PIC && defined INSTALLDIR && ENABLE_COSTLY_RELOCATABLE)
|
2004-07-27 17:12:00 +02:00
|
|
|
|
2003-10-25 18:30:04 +02:00
|
|
|
/* Convenience function:
|
|
|
|
Computes the current installation prefix, based on the original
|
|
|
|
installation prefix, the original installation directory of a particular
|
2012-12-14 16:08:23 +01:00
|
|
|
file, and the current pathname of this file.
|
|
|
|
Returns it, freshly allocated. Returns NULL upon failure. */
|
2003-10-25 18:30:04 +02:00
|
|
|
#ifdef IN_LIBRARY
|
|
|
|
#define compute_curr_prefix local_compute_curr_prefix
|
|
|
|
static
|
|
|
|
#endif
|
2012-12-14 16:08:23 +01:00
|
|
|
char *
|
2003-10-25 18:30:04 +02:00
|
|
|
compute_curr_prefix (const char *orig_installprefix,
|
2012-12-14 16:08:23 +01:00
|
|
|
const char *orig_installdir,
|
|
|
|
const char *curr_pathname)
|
2003-10-25 18:30:04 +02:00
|
|
|
{
|
2012-12-14 16:08:23 +01:00
|
|
|
char *curr_installdir;
|
2003-10-25 18:30:04 +02:00
|
|
|
const char *rel_installdir;
|
|
|
|
|
|
|
|
if (curr_pathname == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Determine the relative installation directory, relative to the prefix.
|
|
|
|
This is simply the difference between orig_installprefix and
|
|
|
|
orig_installdir. */
|
|
|
|
if (strncmp (orig_installprefix, orig_installdir, strlen (orig_installprefix))
|
|
|
|
!= 0)
|
|
|
|
/* Shouldn't happen - nothing should be installed outside $(prefix). */
|
|
|
|
return NULL;
|
|
|
|
rel_installdir = orig_installdir + strlen (orig_installprefix);
|
|
|
|
|
|
|
|
/* Determine the current installation directory. */
|
|
|
|
{
|
2007-10-23 12:48:09 +02:00
|
|
|
const char *p_base = curr_pathname + FILE_SYSTEM_PREFIX_LEN (curr_pathname);
|
2003-10-25 18:30:04 +02:00
|
|
|
const char *p = curr_pathname + strlen (curr_pathname);
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
while (p > p_base)
|
|
|
|
{
|
2012-12-14 16:08:23 +01:00
|
|
|
p--;
|
|
|
|
if (ISSLASH (*p))
|
|
|
|
break;
|
2003-10-25 18:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
q = (char *) xmalloc (p - curr_pathname + 1);
|
|
|
|
#ifdef NO_XMALLOC
|
|
|
|
if (q == NULL)
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
memcpy (q, curr_pathname, p - curr_pathname);
|
|
|
|
q[p - curr_pathname] = '\0';
|
|
|
|
curr_installdir = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the current installation prefix by removing the trailing
|
|
|
|
rel_installdir from it. */
|
|
|
|
{
|
|
|
|
const char *rp = rel_installdir + strlen (rel_installdir);
|
|
|
|
const char *cp = curr_installdir + strlen (curr_installdir);
|
|
|
|
const char *cp_base =
|
2007-10-23 12:48:09 +02:00
|
|
|
curr_installdir + FILE_SYSTEM_PREFIX_LEN (curr_installdir);
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
while (rp > rel_installdir && cp > cp_base)
|
|
|
|
{
|
2012-12-14 16:08:23 +01:00
|
|
|
bool same = false;
|
|
|
|
const char *rpi = rp;
|
|
|
|
const char *cpi = cp;
|
|
|
|
|
|
|
|
while (rpi > rel_installdir && cpi > cp_base)
|
|
|
|
{
|
|
|
|
rpi--;
|
|
|
|
cpi--;
|
|
|
|
if (ISSLASH (*rpi) || ISSLASH (*cpi))
|
|
|
|
{
|
|
|
|
if (ISSLASH (*rpi) && ISSLASH (*cpi))
|
|
|
|
same = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Do case-insensitive comparison if the file system is always or
|
|
|
|
often case-insensitive. It's better to accept the comparison
|
|
|
|
if the difference is only in case, rather than to fail. */
|
2007-10-23 12:48:09 +02:00
|
|
|
#if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
|
2015-02-26 19:01:30 +01:00
|
|
|
/* Native Windows, Cygwin, OS/2, DOS - case insignificant file system */
|
2012-12-14 16:08:23 +01:00
|
|
|
if ((*rpi >= 'a' && *rpi <= 'z' ? *rpi - 'a' + 'A' : *rpi)
|
|
|
|
!= (*cpi >= 'a' && *cpi <= 'z' ? *cpi - 'a' + 'A' : *cpi))
|
|
|
|
break;
|
2003-10-25 18:30:04 +02:00
|
|
|
#else
|
2012-12-14 16:08:23 +01:00
|
|
|
if (*rpi != *cpi)
|
|
|
|
break;
|
2003-10-25 18:30:04 +02:00
|
|
|
#endif
|
2012-12-14 16:08:23 +01:00
|
|
|
}
|
|
|
|
if (!same)
|
|
|
|
break;
|
|
|
|
/* The last pathname component was the same. opi and cpi now point
|
|
|
|
to the slash before it. */
|
|
|
|
rp = rpi;
|
|
|
|
cp = cpi;
|
2003-10-25 18:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rp > rel_installdir)
|
2012-12-14 16:08:23 +01:00
|
|
|
{
|
|
|
|
/* Unexpected: The curr_installdir does not end with rel_installdir. */
|
|
|
|
free (curr_installdir);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
size_t curr_prefix_len = cp - curr_installdir;
|
|
|
|
char *curr_prefix;
|
|
|
|
|
|
|
|
curr_prefix = (char *) xmalloc (curr_prefix_len + 1);
|
|
|
|
#ifdef NO_XMALLOC
|
|
|
|
if (curr_prefix == NULL)
|
2012-12-14 16:08:23 +01:00
|
|
|
{
|
|
|
|
free (curr_installdir);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-10-25 18:30:04 +02:00
|
|
|
#endif
|
|
|
|
memcpy (curr_prefix, curr_installdir, curr_prefix_len);
|
|
|
|
curr_prefix[curr_prefix_len] = '\0';
|
|
|
|
|
2012-12-14 16:08:23 +01:00
|
|
|
free (curr_installdir);
|
|
|
|
|
2003-10-25 18:30:04 +02:00
|
|
|
return curr_prefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-27 17:12:00 +02:00
|
|
|
#endif /* !IN_LIBRARY || PIC */
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#if defined PIC && defined INSTALLDIR && ENABLE_COSTLY_RELOCATABLE
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
/* Full pathname of shared library, or NULL. */
|
|
|
|
static char *shared_library_fullname;
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
|
|
|
|
/* Native Windows only.
|
|
|
|
On Cygwin, it is better to use the Cygwin provided /proc interface, than
|
|
|
|
to use native Windows API and cygwin_conv_to_posix_path, because it
|
|
|
|
supports longer file names
|
|
|
|
(see <http://cygwin.com/ml/cygwin/2011-01/msg00410.html>). */
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
/* Determine the full pathname of the shared library when it is loaded. */
|
|
|
|
|
|
|
|
BOOL WINAPI
|
|
|
|
DllMain (HINSTANCE module_handle, DWORD event, LPVOID reserved)
|
|
|
|
{
|
|
|
|
(void) reserved;
|
|
|
|
|
|
|
|
if (event == DLL_PROCESS_ATTACH)
|
|
|
|
{
|
|
|
|
/* The DLL is being loaded into an application's address range. */
|
|
|
|
static char location[MAX_PATH];
|
|
|
|
|
|
|
|
if (!GetModuleFileName (module_handle, location, sizeof (location)))
|
2012-12-14 16:08:23 +01:00
|
|
|
/* Shouldn't happen. */
|
|
|
|
return FALSE;
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
if (!IS_PATH_WITH_DIR (location))
|
2012-12-14 16:08:23 +01:00
|
|
|
/* Shouldn't happen. */
|
|
|
|
return FALSE;
|
2003-10-25 18:30:04 +02:00
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
shared_library_fullname = strdup (location);
|
2003-10-25 18:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#else /* Unix */
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
find_shared_library_fullname ()
|
|
|
|
{
|
2015-02-26 19:01:30 +01:00
|
|
|
#if (defined __linux__ && (__GLIBC__ >= 2 || defined __UCLIBC__)) || defined __CYGWIN__
|
|
|
|
/* Linux has /proc/self/maps. glibc 2 and uClibc have the getline()
|
|
|
|
function.
|
|
|
|
Cygwin >= 1.5 has /proc/self/maps and the getline() function too.
|
|
|
|
But it is costly: ca. 0.3 ms on Linux, 3 ms on Cygwin 1.5, and 5 ms on
|
|
|
|
Cygwin 1.7. */
|
2003-10-25 18:30:04 +02:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
/* Open the current process' maps file. It describes one VMA per line. */
|
|
|
|
fp = fopen ("/proc/self/maps", "r");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
unsigned long address = (unsigned long) &find_shared_library_fullname;
|
|
|
|
for (;;)
|
2012-12-14 16:08:23 +01:00
|
|
|
{
|
|
|
|
unsigned long start, end;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (fscanf (fp, "%lx-%lx", &start, &end) != 2)
|
|
|
|
break;
|
|
|
|
if (address >= start && address <= end - 1)
|
|
|
|
{
|
|
|
|
/* Found it. Now see if this line contains a filename. */
|
|
|
|
while (c = getc (fp), c != EOF && c != '\n' && c != '/')
|
|
|
|
continue;
|
|
|
|
if (c == '/')
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
ungetc (c, fp);
|
|
|
|
shared_library_fullname = NULL; size = 0;
|
|
|
|
len = getline (&shared_library_fullname, &size, fp);
|
|
|
|
if (len >= 0)
|
|
|
|
{
|
|
|
|
/* Success: filled shared_library_fullname. */
|
|
|
|
if (len > 0 && shared_library_fullname[len - 1] == '\n')
|
|
|
|
shared_library_fullname[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (c = getc (fp), c != EOF && c != '\n')
|
|
|
|
continue;
|
|
|
|
}
|
2003-10-25 18:30:04 +02:00
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:01:30 +01:00
|
|
|
#endif /* Native Windows / Unix */
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
/* Return the full pathname of the current shared library.
|
|
|
|
Return NULL if unknown.
|
2015-02-26 19:01:30 +01:00
|
|
|
Guaranteed to work only on Linux, Cygwin, and native Windows. */
|
2003-10-25 18:30:04 +02:00
|
|
|
static char *
|
|
|
|
get_shared_library_fullname ()
|
|
|
|
{
|
2015-02-26 19:01:30 +01:00
|
|
|
#if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
|
2003-10-25 18:30:04 +02:00
|
|
|
static bool tried_find_shared_library_fullname;
|
|
|
|
if (!tried_find_shared_library_fullname)
|
|
|
|
{
|
|
|
|
find_shared_library_fullname ();
|
|
|
|
tried_find_shared_library_fullname = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return shared_library_fullname;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* PIC */
|
|
|
|
|
|
|
|
/* Returns the pathname, relocated according to the current installation
|
2012-12-14 16:08:23 +01:00
|
|
|
directory.
|
|
|
|
The returned string is either PATHNAME unmodified or a freshly allocated
|
|
|
|
string that you can free with free() after casting it to 'char *'. */
|
2003-10-25 18:30:04 +02:00
|
|
|
const char *
|
|
|
|
relocate (const char *pathname)
|
|
|
|
{
|
2015-02-26 19:01:30 +01:00
|
|
|
#if defined PIC && defined INSTALLDIR && ENABLE_COSTLY_RELOCATABLE
|
2003-10-25 18:30:04 +02:00
|
|
|
static int initialized;
|
|
|
|
|
|
|
|
/* Initialization code for a shared library. */
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
/* At this point, orig_prefix and curr_prefix likely have already been
|
2012-12-14 16:08:23 +01:00
|
|
|
set through the main program's set_program_name_and_installdir
|
|
|
|
function. This is sufficient in the case that the library has
|
|
|
|
initially been installed in the same orig_prefix. But we can do
|
|
|
|
better, to also cover the cases that 1. it has been installed
|
|
|
|
in a different prefix before being moved to orig_prefix and (later)
|
|
|
|
to curr_prefix, 2. unlike the program, it has not moved away from
|
|
|
|
orig_prefix. */
|
2003-10-25 18:30:04 +02:00
|
|
|
const char *orig_installprefix = INSTALLPREFIX;
|
|
|
|
const char *orig_installdir = INSTALLDIR;
|
2012-12-14 16:08:23 +01:00
|
|
|
char *curr_prefix_better;
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
curr_prefix_better =
|
2012-12-14 16:08:23 +01:00
|
|
|
compute_curr_prefix (orig_installprefix, orig_installdir,
|
|
|
|
get_shared_library_fullname ());
|
2003-10-25 18:30:04 +02:00
|
|
|
|
2012-12-14 16:08:23 +01:00
|
|
|
set_relocation_prefix (orig_installprefix,
|
|
|
|
curr_prefix_better != NULL
|
|
|
|
? curr_prefix_better
|
|
|
|
: curr_prefix);
|
|
|
|
|
|
|
|
if (curr_prefix_better != NULL)
|
|
|
|
free (curr_prefix_better);
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Note: It is not necessary to perform case insensitive comparison here,
|
2012-12-14 16:08:23 +01:00
|
|
|
even for DOS-like file systems, because the pathname argument was
|
2003-10-25 18:30:04 +02:00
|
|
|
typically created from the same Makefile variable as orig_prefix came
|
|
|
|
from. */
|
|
|
|
if (orig_prefix != NULL && curr_prefix != NULL
|
|
|
|
&& strncmp (pathname, orig_prefix, orig_prefix_len) == 0)
|
|
|
|
{
|
|
|
|
if (pathname[orig_prefix_len] == '\0')
|
2012-12-14 16:08:23 +01:00
|
|
|
{
|
|
|
|
/* pathname equals orig_prefix. */
|
|
|
|
char *result = (char *) xmalloc (strlen (curr_prefix) + 1);
|
|
|
|
|
|
|
|
#ifdef NO_XMALLOC
|
|
|
|
if (result != NULL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
strcpy (result, curr_prefix);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ISSLASH (pathname[orig_prefix_len]))
|
|
|
|
{
|
|
|
|
/* pathname starts with orig_prefix. */
|
|
|
|
const char *pathname_tail = &pathname[orig_prefix_len];
|
|
|
|
char *result =
|
|
|
|
(char *) xmalloc (curr_prefix_len + strlen (pathname_tail) + 1);
|
2003-10-25 18:30:04 +02:00
|
|
|
|
|
|
|
#ifdef NO_XMALLOC
|
2012-12-14 16:08:23 +01:00
|
|
|
if (result != NULL)
|
2003-10-25 18:30:04 +02:00
|
|
|
#endif
|
2012-12-14 16:08:23 +01:00
|
|
|
{
|
|
|
|
memcpy (result, curr_prefix, curr_prefix_len);
|
|
|
|
strcpy (result + curr_prefix_len, pathname_tail);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2003-10-25 18:30:04 +02:00
|
|
|
}
|
|
|
|
/* Nothing to relocate. */
|
|
|
|
return pathname;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|