relocatable.c 14 KB

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