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