2 * BinReloc - a library for creating relocatable executables
3 * Written by: Hongli Lai <h.lai@chello.nl>
4 * http://autopackage.org/
6 * This source code is public domain. You can relicense this code
7 * under whatever license you want.
9 * See http://autopackage.org/docs/binreloc/ for
10 * more information and how to use this.
13 #ifndef __BINRELOC_C__
14 #define __BINRELOC_C__
16 #ifdef ENABLE_BINRELOC
17 #include <sys/types.h>
20 #endif /* ENABLE_BINRELOC */
31 * Find the canonical filename of the executable. Returns the filename
32 * (which must be freed) or NULL on error. If the parameter 'error' is
33 * not NULL, the error code will be stored there, if an error occured.
36 _br_find_exe (GbrInitError *error)
38 #ifndef ENABLE_BINRELOC
40 *error = GBR_INIT_ERROR_DISABLED;
43 char *path, *path2, *line, *result;
49 /* Read from /proc/self/exe (symlink) */
50 if (sizeof (path) > SSIZE_MAX)
51 buf_size = SSIZE_MAX - 1;
53 buf_size = PATH_MAX - 1;
54 path = (char *) g_try_malloc (buf_size);
56 /* Cannot allocate memory. */
58 *error = GBR_INIT_ERROR_NOMEM;
61 path2 = (char *) g_try_malloc (buf_size);
63 /* Cannot allocate memory. */
65 *error = GBR_INIT_ERROR_NOMEM;
70 strncpy (path2, "/proc/self/exe", buf_size - 1);
75 size = readlink (path2, path, buf_size - 1);
82 /* readlink() success. */
85 /* Check whether the symlink's target is also a symlink.
86 * We want to get the final target. */
87 i = stat (path, &stat_buf);
95 if (!S_ISLNK (stat_buf.st_mode)) {
96 /* path is not a symlink. Done. */
101 /* path is a symlink. Continue loop and resolve this. */
102 strncpy (path, path2, buf_size - 1);
106 /* readlink() or stat() failed; this can happen when the program is
107 * running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
109 buf_size = PATH_MAX + 128;
110 line = (char *) g_try_realloc (path, buf_size);
112 /* Cannot allocate memory. */
115 *error = GBR_INIT_ERROR_NOMEM;
119 f = fopen ("/proc/self/maps", "r");
123 *error = GBR_INIT_ERROR_OPEN_MAPS;
127 /* The first entry should be the executable name. */
128 result = fgets (line, (int) buf_size, f);
129 if (result == NULL) {
133 *error = GBR_INIT_ERROR_READ_MAPS;
137 /* Get rid of newline character. */
138 buf_size = strlen (line);
140 /* Huh? An empty string? */
144 *error = GBR_INIT_ERROR_INVALID_MAPS;
147 if (line[buf_size - 1] == 10)
148 line[buf_size - 1] = 0;
150 /* Extract the filename; it is always an absolute path. */
151 path = strchr (line, '/');
154 if (strstr (line, " r-xp ") == NULL || path == NULL) {
158 *error = GBR_INIT_ERROR_INVALID_MAPS;
162 path = g_strdup (path);
166 #endif /* ENABLE_BINRELOC */
171 * Find the canonical filename of the executable which owns symbol.
172 * Returns a filename which must be freed, or NULL on error.
175 _br_find_exe_for_symbol (const void *symbol, GbrInitError *error)
177 #ifndef ENABLE_BINRELOC
179 *error = GBR_INIT_ERROR_DISABLED;
180 return (char *) NULL;
182 #define SIZE PATH_MAX + 100
184 size_t address_string_len;
185 char *address_string, line[SIZE], *found;
188 return (char *) NULL;
190 f = fopen ("/proc/self/maps", "r");
192 return (char *) NULL;
194 address_string_len = 4;
195 address_string = (char *) g_try_malloc (address_string_len);
196 found = (char *) NULL;
199 char *start_addr, *end_addr, *end_addr_end, *file;
200 void *start_addr_p, *end_addr_p;
203 if (fgets (line, SIZE, f) == NULL)
207 if (strstr (line, " r-xp ") == NULL || strchr (line, '/') == NULL)
212 end_addr = strchr (line, '-');
213 file = strchr (line, '/');
215 /* More sanity check. */
216 if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-'))
221 end_addr_end = strchr (end_addr, ' ');
222 if (end_addr_end == NULL)
225 end_addr_end[0] = '\0';
229 if (file[len - 1] == '\n')
230 file[len - 1] = '\0';
232 /* Get rid of "(deleted)" from the filename. */
234 if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0)
235 file[len - 10] = '\0';
237 /* I don't know whether this can happen but better safe than sorry. */
238 len = strlen (start_addr);
239 if (len != strlen (end_addr))
243 /* Transform the addresses into a string in the form of 0xdeadbeef,
244 * then transform that into a pointer. */
245 if (address_string_len < len + 3) {
246 address_string_len = len + 3;
247 address_string = (char *) g_try_realloc (address_string, address_string_len);
250 memcpy (address_string, "0x", 2);
251 memcpy (address_string + 2, start_addr, len);
252 address_string[2 + len] = '\0';
253 sscanf (address_string, "%p", &start_addr_p);
255 memcpy (address_string, "0x", 2);
256 memcpy (address_string + 2, end_addr, len);
257 address_string[2 + len] = '\0';
258 sscanf (address_string, "%p", &end_addr_p);
261 if (symbol >= start_addr_p && symbol < end_addr_p) {
267 g_free (address_string);
271 return (char *) NULL;
273 return g_strdup (found);
274 #endif /* ENABLE_BINRELOC */
278 static gchar *exe = NULL;
280 static void set_gerror (GError **error, GbrInitError errcode);
283 /** Initialize the BinReloc library (for applications).
285 * This function must be called before using any other BinReloc functions.
286 * It attempts to locate the application's canonical filename.
288 * @note If you want to use BinReloc for a library, then you should call
289 * gbr_init_lib() instead.
291 * @param error If BinReloc failed to initialize, then the error report will
292 * be stored in this variable. Set to NULL if you don't want an
293 * error report. See the #GbrInitError for a list of error
296 * @returns TRUE on success, FALSE if BinReloc failed to initialize.
299 gbr_init (GError **error)
301 GbrInitError errcode;
303 /* Locate the application's filename. */
304 exe = _br_find_exe (&errcode);
310 set_gerror (error, errcode);
316 /** Initialize the BinReloc library (for libraries).
318 * This function must be called before using any other BinReloc functions.
319 * It attempts to locate the calling library's canonical filename.
321 * @note The BinReloc source code MUST be included in your library, or this
322 * function won't work correctly.
324 * @returns TRUE on success, FALSE if a filename cannot be found.
327 gbr_init_lib (GError **error)
329 GbrInitError errcode;
331 exe = _br_find_exe_for_symbol ((const void *) "", &errcode);
337 set_gerror (error, errcode);
344 set_gerror (GError **error, GbrInitError errcode)
346 const gchar *error_message;
352 case GBR_INIT_ERROR_NOMEM:
353 error_message = "Cannot allocate memory.";
355 case GBR_INIT_ERROR_OPEN_MAPS:
356 error_message = "Unable to open /proc/self/maps for reading.";
358 case GBR_INIT_ERROR_READ_MAPS:
359 error_message = "Unable to read from /proc/self/maps.";
361 case GBR_INIT_ERROR_INVALID_MAPS:
362 error_message = "The file format of /proc/self/maps is invalid.";
364 case GBR_INIT_ERROR_DISABLED:
365 error_message = "Binary relocation support is disabled.";
368 error_message = "Unknown error.";
371 g_set_error (error, g_quark_from_static_string ("GBinReloc"),
372 errcode, "%s", error_message);
376 /** Find the canonical filename of the current application.
378 * @param default_exe A default filename which will be used as fallback.
379 * @returns A string containing the application's canonical filename,
380 * which must be freed when no longer necessary. If BinReloc is
381 * not initialized, or if the initialization function failed,
382 * then a copy of default_exe will be returned. If default_exe
383 * is NULL, then NULL will be returned.
386 gbr_find_exe (const gchar *default_exe)
389 /* BinReloc is not initialized. */
390 if (default_exe != NULL)
391 return g_strdup (default_exe);
395 return g_strdup (exe);
399 /** Locate the directory in which the current application is installed.
401 * The prefix is generated by the following pseudo-code evaluation:
406 * @param default_dir A default directory which will used as fallback.
407 * @return A string containing the directory, which must be freed when no
408 * longer necessary. If BinReloc is not initialized, or if the
409 * initialization function failed, then a copy of default_dir
410 * will be returned. If default_dir is NULL, then NULL will be
414 gbr_find_exe_dir (const gchar *default_dir)
417 /* BinReloc not initialized. */
418 if (default_dir != NULL)
419 return g_strdup (default_dir);
424 return g_path_get_dirname (exe);
428 /** Locate the prefix in which the current application is installed.
430 * The prefix is generated by the following pseudo-code evaluation:
432 * dirname(dirname(exename))
435 * @param default_prefix A default prefix which will used as fallback.
436 * @return A string containing the prefix, which must be freed when no
437 * longer necessary. If BinReloc is not initialized, or if the
438 * initialization function failed, then a copy of default_prefix
439 * will be returned. If default_prefix is NULL, then NULL will be
443 gbr_find_prefix (const gchar *default_prefix)
448 /* BinReloc not initialized. */
449 if (default_prefix != NULL)
450 return g_strdup (default_prefix);
455 dir1 = g_path_get_dirname (exe);
456 dir2 = g_path_get_dirname (dir1);
462 /** Locate the application's binary folder.
464 * The path is generated by the following pseudo-code evaluation:
469 * @param default_bin_dir A default path which will used as fallback.
470 * @return A string containing the bin folder's path, which must be freed when
471 * no longer necessary. If BinReloc is not initialized, or if the
472 * initialization function failed, then a copy of default_bin_dir will
473 * be returned. If default_bin_dir is NULL, then NULL will be returned.
476 gbr_find_bin_dir (const gchar *default_bin_dir)
480 prefix = gbr_find_prefix (NULL);
481 if (prefix == NULL) {
482 /* BinReloc not initialized. */
483 if (default_bin_dir != NULL)
484 return g_strdup (default_bin_dir);
489 dir = g_build_filename (prefix, "bin", NULL);
495 /** Locate the application's superuser binary folder.
497 * The path is generated by the following pseudo-code evaluation:
502 * @param default_sbin_dir A default path which will used as fallback.
503 * @return A string containing the sbin folder's path, which must be freed when
504 * no longer necessary. If BinReloc is not initialized, or if the
505 * initialization function failed, then a copy of default_sbin_dir will
506 * be returned. If default_bin_dir is NULL, then NULL will be returned.
509 gbr_find_sbin_dir (const gchar *default_sbin_dir)
513 prefix = gbr_find_prefix (NULL);
514 if (prefix == NULL) {
515 /* BinReloc not initialized. */
516 if (default_sbin_dir != NULL)
517 return g_strdup (default_sbin_dir);
522 dir = g_build_filename (prefix, "sbin", NULL);
528 /** Locate the application's data folder.
530 * The path is generated by the following pseudo-code evaluation:
535 * @param default_data_dir A default path which will used as fallback.
536 * @return A string containing the data folder's path, which must be freed when
537 * no longer necessary. If BinReloc is not initialized, or if the
538 * initialization function failed, then a copy of default_data_dir
539 * will be returned. If default_data_dir is NULL, then NULL will be
543 gbr_find_data_dir (const gchar *default_data_dir)
547 prefix = gbr_find_prefix (NULL);
548 if (prefix == NULL) {
549 /* BinReloc not initialized. */
550 if (default_data_dir != NULL)
551 return g_strdup (default_data_dir);
556 dir = g_build_filename (prefix, "share", NULL);
562 /** Locate the application's localization folder.
564 * The path is generated by the following pseudo-code evaluation:
566 * prefix + "/share/locale"
569 * @param default_locale_dir A default path which will used as fallback.
570 * @return A string containing the localization folder's path, which must be freed when
571 * no longer necessary. If BinReloc is not initialized, or if the
572 * initialization function failed, then a copy of default_locale_dir will be returned.
573 * If default_locale_dir is NULL, then NULL will be returned.
576 gbr_find_locale_dir (const gchar *default_locale_dir)
578 gchar *data_dir, *dir;
580 data_dir = gbr_find_data_dir (NULL);
581 if (data_dir == NULL) {
582 /* BinReloc not initialized. */
583 if (default_locale_dir != NULL)
584 return g_strdup (default_locale_dir);
589 dir = g_build_filename (data_dir, "locale", NULL);
595 /** Locate the application's library folder.
597 * The path is generated by the following pseudo-code evaluation:
602 * @param default_lib_dir A default path which will used as fallback.
603 * @return A string containing the library folder's path, which must be freed when
604 * no longer necessary. If BinReloc is not initialized, or if the
605 * initialization function failed, then a copy of default_lib_dir will be returned.
606 * If default_lib_dir is NULL, then NULL will be returned.
609 gbr_find_lib_dir (const gchar *default_lib_dir)
613 prefix = gbr_find_prefix (NULL);
614 if (prefix == NULL) {
615 /* BinReloc not initialized. */
616 if (default_lib_dir != NULL)
617 return g_strdup (default_lib_dir);
622 dir = g_build_filename (prefix, "lib", NULL);
628 /** Locate the application's libexec folder.
630 * The path is generated by the following pseudo-code evaluation:
632 * prefix + "/libexec"
635 * @param default_libexec_dir A default path which will used as fallback.
636 * @return A string containing the libexec folder's path, which must be freed when
637 * no longer necessary. If BinReloc is not initialized, or if the initialization
638 * function failed, then a copy of default_libexec_dir will be returned.
639 * If default_libexec_dir is NULL, then NULL will be returned.
642 gbr_find_libexec_dir (const gchar *default_libexec_dir)
646 prefix = gbr_find_prefix (NULL);
647 if (prefix == NULL) {
648 /* BinReloc not initialized. */
649 if (default_libexec_dir != NULL)
650 return g_strdup (default_libexec_dir);
655 dir = g_build_filename (prefix, "libexec", NULL);
661 /** Locate the application's configuration files folder.
663 * The path is generated by the following pseudo-code evaluation:
668 * @param default_etc_dir A default path which will used as fallback.
669 * @return A string containing the etc folder's path, which must be freed when
670 * no longer necessary. If BinReloc is not initialized, or if the initialization
671 * function failed, then a copy of default_etc_dir will be returned.
672 * If default_etc_dir is NULL, then NULL will be returned.
675 gbr_find_etc_dir (const gchar *default_etc_dir)
679 prefix = gbr_find_prefix (NULL);
680 if (prefix == NULL) {
681 /* BinReloc not initialized. */
682 if (default_etc_dir != NULL)
683 return g_strdup (default_etc_dir);
688 dir = g_build_filename (prefix, "etc", NULL);
696 #endif /* __BINRELOC_C__ */