relocatable.c 12 KB

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