gwenhywfar  4.99.25rc9
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  char *tmp_address_string;
259 
260  address_string_len = len + 3;
261  tmp_address_string = (char *) realloc(address_string, address_string_len);
262  if (tmp_address_string==NULL)
263  free(address_string);
264  address_string = tmp_address_string;
265  }
266 
267  memcpy(address_string, "0x", 2);
268  memcpy(address_string + 2, start_addr, len);
269  address_string[2 + len] = '\0';
270  sscanf(address_string, "%p", &start_addr_p);
271 
272  memcpy(address_string, "0x", 2);
273  memcpy(address_string + 2, end_addr, len);
274  address_string[2 + len] = '\0';
275  sscanf(address_string, "%p", &end_addr_p);
276 
277 
278  if (symbol >= start_addr_p && symbol < end_addr_p) {
279  found = file;
280  break;
281  }
282  }
283 
284  free(address_string);
285  fclose(f);
286 
287  if (found == NULL)
288  return (char *) NULL;
289  else
290  return strdup(found);
291 #endif /* ENABLE_BINRELOC */
292 }
293 
294 
295 #ifndef BINRELOC_RUNNING_DOXYGEN
296 #undef NULL
297 #define NULL ((void *) 0) /* typecasted as char* for C++ type safeness */
298 #endif
299 
300 static char *exe = (char *) NULL;
301 
302 
317 int
319 {
320  exe = _br_find_exe(error);
321  return exe != NULL;
322 }
323 
324 
339 int
341 {
342  exe = _br_find_exe_for_symbol((const void *) "", error);
343  return exe != NULL;
344 }
345 
346 
356 char *
357 br_find_exe(const char *default_exe)
358 {
359  if (exe == (char *) NULL) {
360  /* BinReloc is not initialized. */
361  if (default_exe != (const char *) NULL)
362  return strdup(default_exe);
363  else
364  return (char *) NULL;
365  }
366  return strdup(exe);
367 }
368 
369 
384 char *
385 br_find_exe_dir(const char *default_dir)
386 {
387  if (exe == NULL) {
388  /* BinReloc not initialized. */
389  if (default_dir != NULL)
390  return strdup(default_dir);
391  else
392  return NULL;
393  }
394 
395  return br_dirname(exe);
396 }
397 
398 
412 char *
413 br_find_prefix(const char *default_prefix)
414 {
415  char *dir1, *dir2;
416 
417  if (exe == (char *) NULL) {
418  /* BinReloc not initialized. */
419  if (default_prefix != (const char *) NULL)
420  return strdup(default_prefix);
421  else
422  return (char *) NULL;
423  }
424 
425  dir1 = br_dirname(exe);
426  dir2 = br_dirname(dir1);
427  free(dir1);
428  return dir2;
429 }
430 
431 
445 char *
446 br_find_bin_dir(const char *default_bin_dir)
447 {
448  char *prefix, *dir;
449 
450  prefix = br_find_prefix((const char *) NULL);
451  if (prefix == (char *) NULL) {
452  /* BinReloc not initialized. */
453  if (default_bin_dir != (const char *) NULL)
454  return strdup(default_bin_dir);
455  else
456  return (char *) NULL;
457  }
458 
459  dir = br_build_path(prefix, "bin");
460  free(prefix);
461  return dir;
462 }
463 
464 
478 char *
479 br_find_sbin_dir(const char *default_sbin_dir)
480 {
481  char *prefix, *dir;
482 
483  prefix = br_find_prefix((const char *) NULL);
484  if (prefix == (char *) NULL) {
485  /* BinReloc not initialized. */
486  if (default_sbin_dir != (const char *) NULL)
487  return strdup(default_sbin_dir);
488  else
489  return (char *) NULL;
490  }
491 
492  dir = br_build_path(prefix, "sbin");
493  free(prefix);
494  return dir;
495 }
496 
497 
512 char *
513 br_find_data_dir(const char *default_data_dir)
514 {
515  char *prefix, *dir;
516 
517  prefix = br_find_prefix((const char *) NULL);
518  if (prefix == (char *) NULL) {
519  /* BinReloc not initialized. */
520  if (default_data_dir != (const char *) NULL)
521  return strdup(default_data_dir);
522  else
523  return (char *) NULL;
524  }
525 
526  dir = br_build_path(prefix, "share");
527  free(prefix);
528  return dir;
529 }
530 
531 
545 char *
546 br_find_locale_dir(const char *default_locale_dir)
547 {
548  char *data_dir, *dir;
549 
550  data_dir = br_find_data_dir((const char *) NULL);
551  if (data_dir == (char *) NULL) {
552  /* BinReloc not initialized. */
553  if (default_locale_dir != (const char *) NULL)
554  return strdup(default_locale_dir);
555  else
556  return (char *) NULL;
557  }
558 
559  dir = br_build_path(data_dir, "locale");
560  free(data_dir);
561  return dir;
562 }
563 
564 
578 char *
579 br_find_lib_dir(const char *default_lib_dir)
580 {
581  char *prefix, *dir;
582 
583  prefix = br_find_prefix((const char *) NULL);
584  if (prefix == (char *) NULL) {
585  /* BinReloc not initialized. */
586  if (default_lib_dir != (const char *) NULL)
587  return strdup(default_lib_dir);
588  else
589  return (char *) NULL;
590  }
591 
592  dir = br_build_path(prefix, "lib");
593  free(prefix);
594  return dir;
595 }
596 
597 
611 char *
612 br_find_libexec_dir(const char *default_libexec_dir)
613 {
614  char *prefix, *dir;
615 
616  prefix = br_find_prefix((const char *) NULL);
617  if (prefix == (char *) NULL) {
618  /* BinReloc not initialized. */
619  if (default_libexec_dir != (const char *) NULL)
620  return strdup(default_libexec_dir);
621  else
622  return (char *) NULL;
623  }
624 
625  dir = br_build_path(prefix, "libexec");
626  free(prefix);
627  return dir;
628 }
629 
630 
644 char *
645 br_find_etc_dir(const char *default_etc_dir)
646 {
647  char *prefix, *dir;
648 
649  prefix = br_find_prefix((const char *) NULL);
650  if (prefix == (char *) NULL) {
651  /* BinReloc not initialized. */
652  if (default_etc_dir != (const char *) NULL)
653  return strdup(default_etc_dir);
654  else
655  return (char *) NULL;
656  }
657 
658  dir = br_build_path(prefix, "etc");
659  free(prefix);
660  return dir;
661 }
662 
663 
664 /***********************
665  * Utility functions
666  ***********************/
667 
674 char *
675 br_strcat(const char *str1, const char *str2)
676 {
677  char *result;
678  size_t len1, len2;
679 
680  if (str1 == NULL)
681  str1 = "";
682  if (str2 == NULL)
683  str2 = "";
684 
685  len1 = strlen(str1);
686  len2 = strlen(str2);
687 
688  result = (char *) malloc(len1 + len2 + 1);
689  memcpy(result, str1, len1);
690  memcpy(result + len1, str2, len2);
691  result[len1 + len2] = '\0';
692 
693  return result;
694 }
695 
696 
697 char *
698 br_build_path(const char *dir, const char *file)
699 {
700  char *dir2, *result;
701  size_t len;
702  int must_free = 0;
703 
704  len = strlen(dir);
705  if (len > 0 && dir[len - 1] != DIRSEP_C) {
706  dir2 = br_strcat(dir, DIRSEP);
707  must_free = 1;
708  }
709  else
710  dir2 = (char *) dir;
711 
712  result = br_strcat(dir2, file);
713  if (must_free)
714  free(dir2);
715  return result;
716 }
717 
718 
719 /* Emulates glibc's strndup() */
720 static char *
721 br_strndup(const char *str, size_t size)
722 {
723  char *result = (char *) NULL;
724  size_t len;
725 
726  if (str == (const char *) NULL)
727  return (char *) NULL;
728 
729  len = strlen(str);
730  if (len == 0)
731  return strdup("");
732  if (size > len)
733  size = len;
734 
735  result = (char *) malloc(len + 1);
736  memcpy(result, str, size);
737  result[size] = '\0';
738  return result;
739 }
740 
741 
754 char *
755 br_dirname(const char *path)
756 {
757  char *end, *result;
758 
759  if (path == (const char *) NULL)
760  return (char *) NULL;
761 
762  end = strrchr(path, DIRSEP_C);
763  if (end == (const char *) NULL)
764  return strdup(".");
765 
766  while (end > path && *end == DIRSEP_C)
767  end--;
768  result = br_strndup(path, end - path + 1);
769  if (result[0] == 0) {
770  free(result);
771  return strdup(DIRSEP);
772  }
773  else
774  return result;
775 }
776 
777 
778 #ifdef __cplusplus
779 }
780 #endif /* __cplusplus */
781 
782 #endif /* __BINRELOC_C__ */
char * br_find_bin_dir(const char *default_bin_dir)
Definition: binreloc.c:446
char * br_find_exe_dir(const char *default_dir)
Definition: binreloc.c:385
char * br_dirname(const char *path)
Definition: binreloc.c:755
char * br_find_exe(const char *default_exe)
Definition: binreloc.c:357
#define NULL
Definition: binreloc.c:297
char * br_find_locale_dir(const char *default_locale_dir)
Definition: binreloc.c:546
char * br_find_libexec_dir(const char *default_libexec_dir)
Definition: binreloc.c:612
static char * exe
Definition: binreloc.c:300
int br_init_lib(BrInitError *error)
Definition: binreloc.c:340
#define DIRSEP_C
Definition: binreloc.c:39
char * br_find_etc_dir(const char *default_etc_dir)
Definition: binreloc.c:645
static char * br_strndup(const char *str, size_t size)
Definition: binreloc.c:721
static char * _br_find_exe(BrInitError *error)
Definition: binreloc.c:48
char * br_find_data_dir(const char *default_data_dir)
Definition: binreloc.c:513
BrInitError
Definition: binreloc.h:22
char * br_find_lib_dir(const char *default_lib_dir)
Definition: binreloc.c:579
char * br_find_prefix(const char *default_prefix)
Definition: binreloc.c:413
char * br_build_path(const char *dir, const char *file)
Definition: binreloc.c:698
char * br_find_sbin_dir(const char *default_sbin_dir)
Definition: binreloc.c:479
#define DIRSEP
Definition: binreloc.c:38
static char * _br_find_exe_for_symbol(const void *symbol, BrInitError *error)
Definition: binreloc.c:187
int br_init(BrInitError *error)
Definition: binreloc.c:318
char * br_strcat(const char *str1, const char *str2)
Definition: binreloc.c:675