relocatable.c 15 KB

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