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__
20
#ifdef ENABLE_BINRELOC
21
#include <sys/types.h>
24
#endif /* ENABLE_BINRELOC */
33
#endif /* __cplusplus */
38
* Find the canonical filename of the executable. Returns the filename
39
* (which must be freed) or NULL on error. If the parameter 'error' is
40
* not NULL, the error code will be stored there, if an error occured.
43
_br_find_exe (BrInitError *error)
45
#ifndef ENABLE_BINRELOC
47
*error = BR_INIT_ERROR_DISABLED;
50
char *path, *path2, *line, *result;
56
/* Read from /proc/self/exe (symlink) */
57
if (sizeof (path) > SSIZE_MAX)
58
buf_size = SSIZE_MAX - 1;
60
buf_size = PATH_MAX - 1;
61
path = (char *) malloc (buf_size);
63
/* Cannot allocate memory. */
65
*error = BR_INIT_ERROR_NOMEM;
68
path2 = (char *) malloc (buf_size);
70
/* Cannot allocate memory. */
72
*error = BR_INIT_ERROR_NOMEM;
77
strncpy (path2, "/proc/self/exe", buf_size - 1);
82
size = readlink (path2, path, buf_size - 1);
89
/* readlink() success. */
92
/* Check whether the symlink's target is also a symlink.
93
* We want to get the final target. */
94
i = stat (path, &stat_buf);
101
/* stat() success. */
102
if (!S_ISLNK (stat_buf.st_mode)) {
103
/* path is not a symlink. Done. */
108
/* path is a symlink. Continue loop and resolve this. */
109
strncpy (path, path2, buf_size - 1);
113
/* readlink() or stat() failed; this can happen when the program is
114
* running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
116
buf_size = PATH_MAX + 128;
117
line = (char *) realloc (path, buf_size);
119
/* Cannot allocate memory. */
122
*error = BR_INIT_ERROR_NOMEM;
126
f = fopen ("/proc/self/maps", "r");
130
*error = BR_INIT_ERROR_OPEN_MAPS;
134
/* The first entry should be the executable name. */
135
result = fgets (line, (int) buf_size, f);
136
if (result == NULL) {
140
*error = BR_INIT_ERROR_READ_MAPS;
144
/* Get rid of newline character. */
145
buf_size = strlen (line);
147
/* Huh? An empty string? */
151
*error = BR_INIT_ERROR_INVALID_MAPS;
154
if (line[buf_size - 1] == 10)
155
line[buf_size - 1] = 0;
157
/* Extract the filename; it is always an absolute path. */
158
path = strchr (line, '/');
161
if (strstr (line, " r-xp ") == NULL || path == NULL) {
165
*error = BR_INIT_ERROR_INVALID_MAPS;
169
path = strdup (path);
173
#endif /* ENABLE_BINRELOC */
178
* Find the canonical filename of the executable which owns symbol.
179
* Returns a filename which must be freed, or NULL on error.
182
_br_find_exe_for_symbol (const void *symbol, BrInitError *error)
184
#ifndef ENABLE_BINRELOC
186
*error = BR_INIT_ERROR_DISABLED;
187
return (char *) NULL;
189
#define SIZE PATH_MAX + 100
191
size_t address_string_len;
192
char *address_string, line[SIZE], *found;
195
return (char *) NULL;
197
f = fopen ("/proc/self/maps", "r");
199
return (char *) NULL;
201
address_string_len = 4;
202
address_string = (char *) malloc (address_string_len);
203
found = (char *) NULL;
206
char *start_addr, *end_addr, *end_addr_end, *file;
207
void *start_addr_p, *end_addr_p;
210
if (fgets (line, SIZE, f) == NULL)
214
if (strstr (line, " r-xp ") == NULL || strchr (line, '/') == NULL)
219
end_addr = strchr (line, '-');
220
file = strchr (line, '/');
222
/* More sanity check. */
223
if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-'))
228
end_addr_end = strchr (end_addr, ' ');
229
if (end_addr_end == NULL)
232
end_addr_end[0] = '\0';
236
if (file[len - 1] == '\n')
237
file[len - 1] = '\0';
239
/* Get rid of "(deleted)" from the filename. */
241
if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0)
242
file[len - 10] = '\0';
244
/* I don't know whether this can happen but better safe than sorry. */
245
len = strlen (start_addr);
246
if (len != strlen (end_addr))
250
/* Transform the addresses into a string in the form of 0xdeadbeef,
251
* then transform that into a pointer. */
252
if (address_string_len < len + 3) {
253
address_string_len = len + 3;
254
address_string = (char *) realloc (address_string, address_string_len);
257
memcpy (address_string, "0x", 2);
258
memcpy (address_string + 2, start_addr, len);
259
address_string[2 + len] = '\0';
260
sscanf (address_string, "%p", &start_addr_p);
262
memcpy (address_string, "0x", 2);
263
memcpy (address_string + 2, end_addr, len);
264
address_string[2 + len] = '\0';
265
sscanf (address_string, "%p", &end_addr_p);
268
if (symbol >= start_addr_p && symbol < end_addr_p) {
274
free (address_string);
278
return (char *) NULL;
280
return strdup (found);
281
#endif /* ENABLE_BINRELOC */
285
#ifndef BINRELOC_RUNNING_DOXYGEN
287
#define NULL ((void *) 0) /* typecasted as char* for C++ type safeness */
290
static char *exe = (char *) NULL;
293
/** Initialize the BinReloc library (for applications).
295
* This function must be called before using any other BinReloc functions.
296
* It attempts to locate the application's canonical filename.
298
* @note If you want to use BinReloc for a library, then you should call
299
* br_init_lib() instead.
301
* @param error If BinReloc failed to initialize, then the error code will
302
* be stored in this variable. Set to NULL if you want to
303
* ignore this. See #BrInitError for a list of error codes.
305
* @returns 1 on success, 0 if BinReloc failed to initialize.
308
br_init (BrInitError *error)
310
exe = _br_find_exe (error);
315
/** Initialize the BinReloc library (for libraries).
317
* This function must be called before using any other BinReloc functions.
318
* It attempts to locate the calling library's canonical filename.
320
* @note The BinReloc source code MUST be included in your library, or this
321
* function won't work correctly.
323
* @param error If BinReloc failed to initialize, then the error code will
324
* be stored in this variable. Set to NULL if you want to
325
* ignore this. See #BrInitError for a list of error codes.
327
* @returns 1 on success, 0 if a filename cannot be found.
330
br_init_lib (BrInitError *error)
332
exe = _br_find_exe_for_symbol ((const void *) "", error);
337
/** Find the canonical filename of the current application.
339
* @param default_exe A default filename which will be used as fallback.
340
* @returns A string containing the application's canonical filename,
341
* which must be freed when no longer necessary. If BinReloc is
342
* not initialized, or if br_init() failed, then a copy of
343
* default_exe will be returned. If default_exe is NULL, then
344
* NULL will be returned.
347
br_find_exe (const char *default_exe)
349
if (exe == (char *) NULL) {
350
/* BinReloc is not initialized. */
351
if (default_exe != (const char *) NULL)
352
return strdup (default_exe);
354
return (char *) NULL;
360
/** Locate the directory in which the current application is installed.
362
* The prefix is generated by the following pseudo-code evaluation:
367
* @param default_dir A default directory which will used as fallback.
368
* @return A string containing the directory, which must be freed when no
369
* longer necessary. If BinReloc is not initialized, or if the
370
* initialization function failed, then a copy of default_dir
371
* will be returned. If default_dir is NULL, then NULL will be
375
br_find_exe_dir (const char *default_dir)
378
/* BinReloc not initialized. */
379
if (default_dir != nullptr)
380
return strdup (default_dir);
385
return br_dirname (exe);
389
/** Locate the prefix in which the current application is installed.
391
* The prefix is generated by the following pseudo-code evaluation:
393
* dirname(dirname(exename))
396
* @param default_prefix A default prefix which will used as fallback.
397
* @return A string containing the prefix, which must be freed when no
398
* longer necessary. If BinReloc is not initialized, or if
399
* the initialization function failed, then a copy of default_prefix
400
* will be returned. If default_prefix is NULL, then NULL will be returned.
403
br_find_prefix (const char *default_prefix)
407
if (exe == (char *) NULL) {
408
/* BinReloc not initialized. */
409
if (default_prefix != (const char *) NULL)
410
return strdup (default_prefix);
412
return (char *) NULL;
415
dir1 = br_dirname (exe);
416
dir2 = br_dirname (dir1);
422
/** Locate the application's binary folder.
424
* The path is generated by the following pseudo-code evaluation:
429
* @param default_bin_dir A default path which will used as fallback.
430
* @return A string containing the bin folder's path, which must be freed when
431
* no longer necessary. If BinReloc is not initialized, or if
432
* the initialization function failed, then a copy of default_bin_dir will
433
* be returned. If default_bin_dir is NULL, then NULL will be returned.
436
br_find_bin_dir (const char *default_bin_dir)
440
prefix = br_find_prefix ((const char *) NULL);
441
if (prefix == (char *) NULL) {
442
/* BinReloc not initialized. */
443
if (default_bin_dir != (const char *) NULL)
444
return strdup (default_bin_dir);
446
return (char *) NULL;
449
dir = br_build_path (prefix, "bin");
455
/** Locate the application's superuser binary folder.
457
* The path is generated by the following pseudo-code evaluation:
462
* @param default_sbin_dir A default path which will used as fallback.
463
* @return A string containing the sbin folder's path, which must be freed when
464
* no longer necessary. If BinReloc is not initialized, or if the
465
* initialization function failed, then a copy of default_sbin_dir will
466
* be returned. If default_bin_dir is NULL, then NULL will be returned.
469
br_find_sbin_dir (const char *default_sbin_dir)
473
prefix = br_find_prefix ((const char *) NULL);
474
if (prefix == (char *) NULL) {
475
/* BinReloc not initialized. */
476
if (default_sbin_dir != (const char *) NULL)
477
return strdup (default_sbin_dir);
479
return (char *) NULL;
482
dir = br_build_path (prefix, "sbin");
488
/** Locate the application's data folder.
490
* The path is generated by the following pseudo-code evaluation:
495
* @param default_data_dir A default path which will used as fallback.
496
* @return A string containing the data folder's path, which must be freed when
497
* no longer necessary. If BinReloc is not initialized, or if the
498
* initialization function failed, then a copy of default_data_dir
499
* will be returned. If default_data_dir is NULL, then NULL will be
503
br_find_data_dir (const char *default_data_dir)
507
prefix = br_find_prefix ((const char *) NULL);
508
if (prefix == (char *) NULL) {
509
/* BinReloc not initialized. */
510
if (default_data_dir != (const char *) NULL)
511
return strdup (default_data_dir);
513
return (char *) NULL;
516
dir = br_build_path (prefix, "share");
522
/** Locate the application's localization folder.
524
* The path is generated by the following pseudo-code evaluation:
526
* prefix + "/share/locale"
529
* @param default_locale_dir A default path which will used as fallback.
530
* @return A string containing the localization folder's path, which must be freed when
531
* no longer necessary. If BinReloc is not initialized, or if the
532
* initialization function failed, then a copy of default_locale_dir will be returned.
533
* If default_locale_dir is NULL, then NULL will be returned.
536
br_find_locale_dir (const char *default_locale_dir)
538
char *data_dir, *dir;
540
data_dir = br_find_data_dir ((const char *) NULL);
541
if (data_dir == (char *) NULL) {
542
/* BinReloc not initialized. */
543
if (default_locale_dir != (const char *) NULL)
544
return strdup (default_locale_dir);
546
return (char *) NULL;
549
dir = br_build_path (data_dir, "locale");
555
/** Locate the application's library folder.
557
* The path is generated by the following pseudo-code evaluation:
562
* @param default_lib_dir A default path which will used as fallback.
563
* @return A string containing the library folder's path, which must be freed when
564
* no longer necessary. If BinReloc is not initialized, or if the initialization
565
* function failed, then a copy of default_lib_dir will be returned.
566
* If default_lib_dir is NULL, then NULL will be returned.
569
br_find_lib_dir (const char *default_lib_dir)
573
prefix = br_find_prefix ((const char *) NULL);
574
if (prefix == (char *) NULL) {
575
/* BinReloc not initialized. */
576
if (default_lib_dir != (const char *) NULL)
577
return strdup (default_lib_dir);
579
return (char *) NULL;
582
dir = br_build_path (prefix, "lib");
588
/** Locate the application's libexec folder.
590
* The path is generated by the following pseudo-code evaluation:
592
* prefix + "/libexec"
595
* @param default_libexec_dir A default path which will used as fallback.
596
* @return A string containing the libexec folder's path, which must be freed when
597
* no longer necessary. If BinReloc is not initialized, or if the initialization
598
* function failed, then a copy of default_libexec_dir will be returned.
599
* If default_libexec_dir is NULL, then NULL will be returned.
602
br_find_libexec_dir (const char *default_libexec_dir)
606
prefix = br_find_prefix ((const char *) NULL);
607
if (prefix == (char *) NULL) {
608
/* BinReloc not initialized. */
609
if (default_libexec_dir != (const char *) NULL)
610
return strdup (default_libexec_dir);
612
return (char *) NULL;
615
dir = br_build_path (prefix, "libexec");
621
/** Locate the application's configuration files folder.
623
* The path is generated by the following pseudo-code evaluation:
628
* @param default_etc_dir A default path which will used as fallback.
629
* @return A string containing the etc folder's path, which must be freed when
630
* no longer necessary. If BinReloc is not initialized, or if the initialization
631
* function failed, then a copy of default_etc_dir will be returned.
632
* If default_etc_dir is NULL, then NULL will be returned.
635
br_find_etc_dir (const char *default_etc_dir)
639
prefix = br_find_prefix ((const char *) NULL);
640
if (prefix == (char *) NULL) {
641
/* BinReloc not initialized. */
642
if (default_etc_dir != (const char *) NULL)
643
return strdup (default_etc_dir);
645
return (char *) NULL;
648
dir = br_build_path (prefix, "etc");
654
/***********************
656
***********************/
658
/** Concatenate str1 and str2 to a newly allocated string.
660
* @param str1 A string.
661
* @param str2 Another string.
662
* @returns A newly-allocated string. This string should be freed when no longer needed.
665
br_strcat (const char *str1, const char *str2)
675
len1 = strlen (str1);
676
len2 = strlen (str2);
678
result = (char *) malloc (len1 + len2 + 1);
679
memcpy (result, str1, len1);
680
memcpy (result + len1, str2, len2);
681
result[len1 + len2] = '\0';
688
br_build_path (const char *dir, const char *file)
695
if (len > 0 && dir[len - 1] != '/') {
696
dir2 = br_strcat (dir, "/");
701
result = br_strcat (dir2, file);
708
/* Emulates glibc's strndup() */
710
br_strndup (const char *str, size_t size)
712
char *result = (char *) NULL;
715
if (str == (const char *) NULL)
716
return (char *) NULL;
724
result = (char *) malloc (len + 1);
725
memcpy (result, str, size);
731
/** Extracts the directory component of a path.
733
* Similar to g_dirname() or the dirname commandline application.
737
* br_dirname ("/usr/local/foobar"); --> Returns: "/usr/local"
740
* @param path A path.
741
* @returns A directory name. This string should be freed when no longer needed.
744
br_dirname (const char *path)
751
end = (char*)strrchr (path, '/');
755
while (end > path && *end == '/')
757
result = br_strndup (path, end - path + 1);
758
if (result[0] == 0) {
768
#endif /* __cplusplus */
770
#endif /* __BINRELOC_C__ */