gwenhywfar  4.6.0beta
binreloc.c
Go to the documentation of this file.
1 /*
2  * BinReloc - a library for creating relocatable executables
3  * Written by: Hongli Lai <h.lai@chello.nl>
4  * http://autopackage.org/
5  *
6  * This source code is public domain. You can relicense this code
7  * under whatever license you want.
8  *
9  * See http://autopackage.org/docs/binreloc/ for
10  * more information and how to use this.
11  */
12 
13 #ifndef __BINRELOC_C__
14 #define __BINRELOC_C__
15 
16 #include "config.h"
17 
18 #ifdef ENABLE_BINRELOC
19  #include <sys/types.h>
20  #include <sys/stat.h>
21  #include <unistd.h>
22 #endif /* ENABLE_BINRELOC */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <string.h>
27 #include "binreloc.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif /* __cplusplus */
32 
33 
34 #ifdef OS_WIN32
35 # define DIRSEP "\\"
36 # define DIRSEP_C '\\'
37 #else
38 # define DIRSEP "/"
39 # define DIRSEP_C '/'
40 #endif
41 
47 static char *
49 {
50 #ifndef ENABLE_BINRELOC
51  if (error)
52  *error = BR_INIT_ERROR_DISABLED;
53  return NULL;
54 #else
55  char *path, *path2, *line, *result;
56  size_t buf_size;
57  ssize_t size;
58  struct stat stat_buf;
59  FILE *f;
60 
61  /* Read from /proc/self/exe (symlink) */
62  if (sizeof (path) > SSIZE_MAX)
63  buf_size = SSIZE_MAX - 1;
64  else
65  buf_size = PATH_MAX - 1;
66  path = (char *) malloc (buf_size);
67  if (path == NULL) {
68  /* Cannot allocate memory. */
69  if (error)
70  *error = BR_INIT_ERROR_NOMEM;
71  return NULL;
72  }
73  path2 = (char *) malloc (buf_size);
74  if (path2 == NULL) {
75  /* Cannot allocate memory. */
76  if (error)
77  *error = BR_INIT_ERROR_NOMEM;
78  free (path);
79  return NULL;
80  }
81 
82  strncpy (path2, "/proc/self/exe", buf_size - 1);
83 
84  while (1) {
85  int i;
86 
87  size = readlink (path2, path, buf_size - 1);
88  if (size == -1) {
89  /* Error. */
90  free (path2);
91  break;
92  }
93 
94  /* readlink() success. */
95  path[size] = '\0';
96 
97  /* Check whether the symlink's target is also a symlink.
98  * We want to get the final target. */
99  i = stat (path, &stat_buf);
100  if (i == -1) {
101  /* Error. */
102  free (path2);
103  break;
104  }
105 
106  /* stat() success. */
107  if (!S_ISLNK (stat_buf.st_mode)) {
108  /* path is not a symlink. Done. */
109  free (path2);
110  return path;
111  }
112 
113  /* path is a symlink. Continue loop and resolve this. */
114  strncpy (path, path2, buf_size - 1);
115  }
116 
117 
118  /* readlink() or stat() failed; this can happen when the program is
119  * running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
120 
121  buf_size = PATH_MAX + 128;
122  line = (char *) realloc (path, buf_size);
123  if (line == NULL) {
124  /* Cannot allocate memory. */
125  free (path);
126  if (error)
127  *error = BR_INIT_ERROR_NOMEM;
128  return NULL;
129  }
130 
131  f = fopen ("/proc/self/maps", "r");
132  if (f == NULL) {
133  free (line);
134  if (error)
135  *error = BR_INIT_ERROR_OPEN_MAPS;
136  return NULL;
137  }
138 
139  /* The first entry should be the executable name. */
140  result = fgets (line, (int) buf_size, f);
141  if (result == NULL) {
142  fclose (f);
143  free (line);
144  if (error)
145  *error = BR_INIT_ERROR_READ_MAPS;
146  return NULL;
147  }
148 
149  /* Get rid of newline character. */
150  buf_size = strlen (line);
151  if (buf_size <= 0) {
152  /* Huh? An empty string? */
153  fclose (f);
154  free (line);
155  if (error)
157  return NULL;
158  }
159  if (line[buf_size - 1] == 10)
160  line[buf_size - 1] = 0;
161 
162  /* Extract the filename; it is always an absolute path. */
163  path = strchr (line, DIRSEP_C);
164 
165  /* Sanity check. */
166  if (strstr (line, " r-xp ") == NULL || path == NULL) {
167  fclose (f);
168  free (line);
169  if (error)
171  return NULL;
172  }
173 
174  path = strdup (path);
175  free (line);
176  fclose (f);
177  return path;
178 #endif /* ENABLE_BINRELOC */
179 }
180 
181 
186 static char *
187 _br_find_exe_for_symbol (const void *symbol, BrInitError *error)
188 {
189 #ifndef ENABLE_BINRELOC
190  if (error)
191  *error = BR_INIT_ERROR_DISABLED;
192  return (char *) NULL;
193 #else
194  #define SIZE PATH_MAX + 100
195  FILE *f;
196  size_t address_string_len;
197  char *address_string, line[SIZE], *found;
198 
199  if (symbol == NULL)
200  return (char *) NULL;
201 
202  f = fopen ("/proc/self/maps", "r");
203  if (f == NULL)
204  return (char *) NULL;
205 
206  address_string_len = 4;
207  address_string = (char *) malloc (address_string_len);
208  found = (char *) NULL;
209 
210  while (!feof (f)) {
211  char *start_addr, *end_addr, *end_addr_end, *file;
212  void *start_addr_p, *end_addr_p;
213  size_t len;
214 
215  if (fgets (line, SIZE, f) == NULL)
216  break;
217 
218  /* Sanity check. */
219  if (strstr (line, " r-xp ") == NULL || strchr (line, '/') == NULL)
220  continue;
221 
222  /* Parse line. */
223  start_addr = line;
224  end_addr = strchr (line, '-');
225  file = strchr (line, '/');
226 
227  /* More sanity check. */
228  if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-'))
229  continue;
230 
231  end_addr[0] = '\0';
232  end_addr++;
233  end_addr_end = strchr (end_addr, ' ');
234  if (end_addr_end == NULL)
235  continue;
236 
237  end_addr_end[0] = '\0';
238  len = strlen (file);
239  if (len == 0)
240  continue;
241  if (file[len - 1] == '\n')
242  file[len - 1] = '\0';
243 
244  /* Get rid of "(deleted)" from the filename. */
245  len = strlen (file);
246  if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0)
247  file[len - 10] = '\0';
248 
249  /* I don't know whether this can happen but better safe than sorry. */
250  len = strlen (start_addr);
251  if (len != strlen (end_addr))
252  continue;
253 
254 
255  /* Transform the addresses into a string in the form of 0xdeadbeef,
256  * then transform that into a pointer. */
257  if (address_string_len < len + 3) {
258  address_string_len = len + 3;
259  address_string = (char *) realloc (address_string, address_string_len);
260  }
261 
262  memcpy (address_string, "0x", 2);
263  memcpy (address_string + 2, start_addr, len);
264  address_string[2 + len] = '\0';
265  sscanf (address_string, "%p", &start_addr_p);
266 
267  memcpy (address_string, "0x", 2);
268  memcpy (address_string + 2, end_addr, len);
269  address_string[2 + len] = '\0';
270  sscanf (address_string, "%p", &end_addr_p);
271 
272 
273  if (symbol >= start_addr_p && symbol < end_addr_p) {
274  found = file;
275  break;
276  }
277  }
278 
279  free (address_string);
280  fclose (f);
281 
282  if (found == NULL)
283  return (char *) NULL;
284  else
285  return strdup (found);
286 #endif /* ENABLE_BINRELOC */
287 }
288 
289 
290 #ifndef BINRELOC_RUNNING_DOXYGEN
291  #undef NULL
292  #define NULL ((void *) 0) /* typecasted as char* for C++ type safeness */
293 #endif
294 
295 static char *exe = (char *) NULL;
296 
297 
312 int
314 {
315  exe = _br_find_exe (error);
316  return exe != NULL;
317 }
318 
319 
334 int
336 {
337  exe = _br_find_exe_for_symbol ((const void *) "", error);
338  return exe != NULL;
339 }
340 
341 
351 char *
352 br_find_exe (const char *default_exe)
353 {
354  if (exe == (char *) NULL) {
355  /* BinReloc is not initialized. */
356  if (default_exe != (const char *) NULL)
357  return strdup (default_exe);
358  else
359  return (char *) NULL;
360  }
361  return strdup (exe);
362 }
363 
364 
379 char *
380 br_find_exe_dir (const char *default_dir)
381 {
382  if (exe == NULL) {
383  /* BinReloc not initialized. */
384  if (default_dir != NULL)
385  return strdup (default_dir);
386  else
387  return NULL;
388  }
389 
390  return br_dirname (exe);
391 }
392 
393 
407 char *
408 br_find_prefix (const char *default_prefix)
409 {
410  char *dir1, *dir2;
411 
412  if (exe == (char *) NULL) {
413  /* BinReloc not initialized. */
414  if (default_prefix != (const char *) NULL)
415  return strdup (default_prefix);
416  else
417  return (char *) NULL;
418  }
419 
420  dir1 = br_dirname (exe);
421  dir2 = br_dirname (dir1);
422  free (dir1);
423  return dir2;
424 }
425 
426 
440 char *
441 br_find_bin_dir (const char *default_bin_dir)
442 {
443  char *prefix, *dir;
444 
445  prefix = br_find_prefix ((const char *) NULL);
446  if (prefix == (char *) NULL) {
447  /* BinReloc not initialized. */
448  if (default_bin_dir != (const char *) NULL)
449  return strdup (default_bin_dir);
450  else
451  return (char *) NULL;
452  }
453 
454  dir = br_build_path (prefix, "bin");
455  free (prefix);
456  return dir;
457 }
458 
459 
473 char *
474 br_find_sbin_dir (const char *default_sbin_dir)
475 {
476  char *prefix, *dir;
477 
478  prefix = br_find_prefix ((const char *) NULL);
479  if (prefix == (char *) NULL) {
480  /* BinReloc not initialized. */
481  if (default_sbin_dir != (const char *) NULL)
482  return strdup (default_sbin_dir);
483  else
484  return (char *) NULL;
485  }
486 
487  dir = br_build_path (prefix, "sbin");
488  free (prefix);
489  return dir;
490 }
491 
492 
507 char *
508 br_find_data_dir (const char *default_data_dir)
509 {
510  char *prefix, *dir;
511 
512  prefix = br_find_prefix ((const char *) NULL);
513  if (prefix == (char *) NULL) {
514  /* BinReloc not initialized. */
515  if (default_data_dir != (const char *) NULL)
516  return strdup (default_data_dir);
517  else
518  return (char *) NULL;
519  }
520 
521  dir = br_build_path (prefix, "share");
522  free (prefix);
523  return dir;
524 }
525 
526 
540 char *
541 br_find_locale_dir (const char *default_locale_dir)
542 {
543  char *data_dir, *dir;
544 
545  data_dir = br_find_data_dir ((const char *) NULL);
546  if (data_dir == (char *) NULL) {
547  /* BinReloc not initialized. */
548  if (default_locale_dir != (const char *) NULL)
549  return strdup (default_locale_dir);
550  else
551  return (char *) NULL;
552  }
553 
554  dir = br_build_path (data_dir, "locale");
555  free (data_dir);
556  return dir;
557 }
558 
559 
573 char *
574 br_find_lib_dir (const char *default_lib_dir)
575 {
576  char *prefix, *dir;
577 
578  prefix = br_find_prefix ((const char *) NULL);
579  if (prefix == (char *) NULL) {
580  /* BinReloc not initialized. */
581  if (default_lib_dir != (const char *) NULL)
582  return strdup (default_lib_dir);
583  else
584  return (char *) NULL;
585  }
586 
587  dir = br_build_path (prefix, "lib");
588  free (prefix);
589  return dir;
590 }
591 
592 
606 char *
607 br_find_libexec_dir (const char *default_libexec_dir)
608 {
609  char *prefix, *dir;
610 
611  prefix = br_find_prefix ((const char *) NULL);
612  if (prefix == (char *) NULL) {
613  /* BinReloc not initialized. */
614  if (default_libexec_dir != (const char *) NULL)
615  return strdup (default_libexec_dir);
616  else
617  return (char *) NULL;
618  }
619 
620  dir = br_build_path (prefix, "libexec");
621  free (prefix);
622  return dir;
623 }
624 
625 
639 char *
640 br_find_etc_dir (const char *default_etc_dir)
641 {
642  char *prefix, *dir;
643 
644  prefix = br_find_prefix ((const char *) NULL);
645  if (prefix == (char *) NULL) {
646  /* BinReloc not initialized. */
647  if (default_etc_dir != (const char *) NULL)
648  return strdup (default_etc_dir);
649  else
650  return (char *) NULL;
651  }
652 
653  dir = br_build_path (prefix, "etc");
654  free (prefix);
655  return dir;
656 }
657 
658 
659 /***********************
660  * Utility functions
661  ***********************/
662 
669 char *
670 br_strcat (const char *str1, const char *str2)
671 {
672  char *result;
673  size_t len1, len2;
674 
675  if (str1 == NULL)
676  str1 = "";
677  if (str2 == NULL)
678  str2 = "";
679 
680  len1 = strlen (str1);
681  len2 = strlen (str2);
682 
683  result = (char *) malloc (len1 + len2 + 1);
684  memcpy (result, str1, len1);
685  memcpy (result + len1, str2, len2);
686  result[len1 + len2] = '\0';
687 
688  return result;
689 }
690 
691 
692 char *
693 br_build_path (const char *dir, const char *file)
694 {
695  char *dir2, *result;
696  size_t len;
697  int must_free = 0;
698 
699  len = strlen (dir);
700  if (len > 0 && dir[len - 1] != DIRSEP_C) {
701  dir2 = br_strcat (dir, DIRSEP );
702  must_free = 1;
703  } else
704  dir2 = (char *) dir;
705 
706  result = br_strcat (dir2, file);
707  if (must_free)
708  free (dir2);
709  return result;
710 }
711 
712 
713 /* Emulates glibc's strndup() */
714 static char *
715 br_strndup (const char *str, size_t size)
716 {
717  char *result = (char *) NULL;
718  size_t len;
719 
720  if (str == (const char *) NULL)
721  return (char *) NULL;
722 
723  len = strlen (str);
724  if (len == 0)
725  return strdup ("");
726  if (size > len)
727  size = len;
728 
729  result = (char *) malloc (len + 1);
730  memcpy (result, str, size);
731  result[size] = '\0';
732  return result;
733 }
734 
735 
748 char *
749 br_dirname (const char *path)
750 {
751  char *end, *result;
752 
753  if (path == (const char *) NULL)
754  return (char *) NULL;
755 
756  end = strrchr (path, DIRSEP_C);
757  if (end == (const char *) NULL)
758  return strdup (".");
759 
760  while (end > path && *end == DIRSEP_C)
761  end--;
762  result = br_strndup (path, end - path + 1);
763  if (result[0] == 0) {
764  free (result);
765  return strdup (DIRSEP);
766  } else
767  return result;
768 }
769 
770 
771 #ifdef __cplusplus
772 }
773 #endif /* __cplusplus */
774 
775 #endif /* __BINRELOC_C__ */