1 /*
2 * BinReloc - a library for creating relocatable executables
3 * Written by: Mike Hearn <mike@theoretic.com>
4 * Hongli Lai <h.lai@chello.nl>
5 * http://autopackage.org/
6 *
7 * This source code is public domain. You can relicense this code
8 * under whatever license you want.
9 *
10 * NOTE: if you're using C++ and are getting "undefined reference
11 * to br_*", try renaming prefix.c to prefix.cpp
12 */
13
14 /* WARNING, BEFORE YOU MODIFY PREFIX.C:
15 *
16 * If you make changes to any of the functions in prefix.c, you MUST
17 * change the BR_NAMESPACE macro (in prefix.h).
18 * This way you can avoid symbol table conflicts with other libraries
19 * that also happen to use BinReloc.
20 *
21 * Example:
22 * #define BR_NAMESPACE(funcName) foobar_ ## funcName
23 * --> expands br_locate to foobar_br_locate
24 */
25
26 #ifndef _PREFIX_C_
27 #define _PREFIX_C_
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #ifndef BR_PTHREADS
34 /* Change 1 to 0 if you don't want pthread support */
35 #define BR_PTHREADS 1
36 #endif /* BR_PTHREADS */
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <limits.h>
41 #include <string.h>
42 #include "prefix.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif /* __cplusplus */
47
48
49 #undef NULL
50 #define NULL ((void *) 0)
51
52 #ifdef __GNUC__
53 #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
54 #else
55 #define br_return_val_if_fail(expr,val) if (!(expr)) return val
56 #endif /* __GNUC__ */
57
58
59 static br_locate_fallback_func fallback_func = (br_locate_fallback_func) NULL;
60 static void *fallback_data = NULL;
61
62
63 #ifdef ENABLE_BINRELOC
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #include <sys/param.h>
67 #include <unistd.h>
68
69
70 /**
71 * br_locate:
72 * symbol: A symbol that belongs to the app/library you want to locate.
73 * Returns: A newly allocated string containing the full path of the
74 * app/library that func belongs to, or NULL on error. This
75 * string should be freed when not when no longer needed.
76 *
77 * Finds out to which application or library symbol belongs, then locate
78 * the full path of that application or library.
79 * Note that symbol cannot be a pointer to a function. That will not work.
80 *
81 * Example:
82 * --> main.c
83 * #include "prefix.h"
84 * #include "libfoo.h"
85 *
86 * int main (int argc, char *argv[]) {
87 * printf ("Full path of this app: %s\n", br_locate (&argc));
88 * libfoo_start ();
89 * return 0;
90 * }
91 *
92 * --> libfoo.c starts here
93 * #include "prefix.h"
94 *
95 * void libfoo_start () {
96 * --> "" is a symbol that belongs to libfoo (because it's called
97 * --> from libfoo_start()); that's why this works.
98 * printf ("libfoo is located in: %s\n", br_locate (""));
99 * }
100 */
101 char *
br_locate(void * symbol)102 br_locate (void *symbol)
103 {
104 char line[5000];
105 FILE *f;
106 char *path;
107
108 br_return_val_if_fail (symbol != NULL, NULL);
109
110 f = fopen ("/proc/self/maps", "r");
111 if (!f) {
112 if (fallback_func)
113 return fallback_func(symbol, fallback_data);
114 else
115 return NULL;
116 }
117
118 while (!feof (f))
119 {
120 unsigned long start, end;
121
122 if (!fgets (line, sizeof (line), f))
123 continue;
124 if (!strstr (line, " r-xp ") || !strchr (line, '/'))
125 continue;
126
127 sscanf (line, "%lx-%lx ", &start, &end);
128 if (symbol >= (void *) start && symbol < (void *) end)
129 {
130 char *tmp;
131 size_t len;
132
133 /* Extract the filename; it is always an absolute path */
134 path = strchr (line, '/');
135
136 /* Get rid of the newline */
137 tmp = strrchr (path, '\n');
138 if (tmp) *tmp = 0;
139
140 /* Get rid of "(deleted)" */
141 len = strlen (path);
142 if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
143 {
144 tmp = path + len - 10;
145 *tmp = 0;
146 }
147
148 fclose(f);
149 return strdup (path);
150 }
151 }
152
153 fclose (f);
154 return NULL;
155 }
156
157
158 /**
159 * br_locate_prefix:
160 * symbol: A symbol that belongs to the app/library you want to locate.
161 * Returns: A prefix. This string should be freed when no longer needed.
162 *
163 * Locates the full path of the app/library that symbol belongs to, and return
164 * the prefix of that path, or NULL on error.
165 * Note that symbol cannot be a pointer to a function. That will not work.
166 *
167 * Example:
168 * --> This application is located in /usr/bin/foo
169 * br_locate_prefix (&argc); --> returns: "/usr"
170 */
171 char *
br_locate_prefix(void * symbol)172 br_locate_prefix (void *symbol)
173 {
174 char *path, *prefix;
175
176 br_return_val_if_fail (symbol != NULL, NULL);
177
178 path = br_locate (symbol);
179 if (!path) return NULL;
180
181 prefix = br_extract_prefix (path);
182 free (path);
183 return prefix;
184 }
185
186
187 /**
188 * br_prepend_prefix:
189 * symbol: A symbol that belongs to the app/library you want to locate.
190 * path: The path that you want to prepend the prefix to.
191 * Returns: The new path, or NULL on error. This string should be freed when no
192 * longer needed.
193 *
194 * Gets the prefix of the app/library that symbol belongs to. Prepend that prefix to path.
195 * Note that symbol cannot be a pointer to a function. That will not work.
196 *
197 * Example:
198 * --> The application is /usr/bin/foo
199 * br_prepend_prefix (&argc, "/share/foo/data.png"); --> Returns "/usr/share/foo/data.png"
200 */
201 char *
br_prepend_prefix(void * symbol,char * path)202 br_prepend_prefix (void *symbol, char *path)
203 {
204 char *tmp, *newpath;
205
206 br_return_val_if_fail (symbol != NULL, NULL);
207 br_return_val_if_fail (path != NULL, NULL);
208
209 tmp = br_locate_prefix (symbol);
210 if (!tmp) return NULL;
211
212 if (strcmp (tmp, "/") == 0)
213 newpath = strdup (path);
214 else
215 newpath = br_strcat (tmp, path);
216
217 /* Get rid of compiler warning ("br_prepend_prefix never used") */
218 if (0) br_prepend_prefix (NULL, NULL);
219
220 free (tmp);
221 return newpath;
222 }
223
224 #endif /* ENABLE_BINRELOC */
225
226
227 /* Pthread stuff for thread safetiness */
228 #if BR_PTHREADS && defined(ENABLE_BINRELOC)
229
230 #include <pthread.h>
231
232 static pthread_key_t br_thread_key;
233 static pthread_once_t br_thread_key_once = PTHREAD_ONCE_INIT;
234
235
236 static void
br_thread_local_store_fini()237 br_thread_local_store_fini ()
238 {
239 char *specific;
240
241 specific = (char *) pthread_getspecific (br_thread_key);
242 if (specific)
243 {
244 free (specific);
245 pthread_setspecific (br_thread_key, NULL);
246 }
247 pthread_key_delete (br_thread_key);
248 br_thread_key = 0;
249 }
250
251
252 static void
br_str_free(void * str)253 br_str_free (void *str)
254 {
255 if (str)
256 free (str);
257 }
258
259
260 static void
br_thread_local_store_init()261 br_thread_local_store_init ()
262 {
263 if (pthread_key_create (&br_thread_key, br_str_free) == 0)
264 atexit (br_thread_local_store_fini);
265 }
266
267 #else /* BR_PTHREADS */
268 #ifdef ENABLE_BINRELOC
269
270 static char *br_last_value = (char *) NULL;
271
272 static void
br_free_last_value()273 br_free_last_value ()
274 {
275 if (br_last_value)
276 free (br_last_value);
277 }
278
279 #endif /* ENABLE_BINRELOC */
280 #endif /* BR_PTHREADS */
281
282
283 #ifdef ENABLE_BINRELOC
284
285 /**
286 * br_thread_local_store:
287 * str: A dynamically allocated string.
288 * Returns: str. This return value must not be freed.
289 *
290 * Store str in a thread-local variable and return str. The next
291 * you run this function, that variable is freed too.
292 * This function is created so you don't have to worry about freeing
293 * strings. Just be careful about doing this sort of thing:
294 *
295 * some_function( BR_DATADIR("/one.png"), BR_DATADIR("/two.png") )
296 *
297 * Examples:
298 * char *foo;
299 * foo = br_thread_local_store (strdup ("hello")); --> foo == "hello"
300 * foo = br_thread_local_store (strdup ("world")); --> foo == "world"; "hello" is now freed.
301 */
302 const char *
br_thread_local_store(char * str)303 br_thread_local_store (char *str)
304 {
305 #if BR_PTHREADS
306 char *specific;
307
308 pthread_once (&br_thread_key_once, br_thread_local_store_init);
309
310 specific = (char *) pthread_getspecific (br_thread_key);
311 br_str_free (specific);
312 pthread_setspecific (br_thread_key, str);
313
314 #else /* BR_PTHREADS */
315 static int initialized = 0;
316
317 if (!initialized)
318 {
319 atexit (br_free_last_value);
320 initialized = 1;
321 }
322
323 if (br_last_value)
324 free (br_last_value);
325 br_last_value = str;
326 #endif /* BR_PTHREADS */
327
328 return (const char *) str;
329 }
330
331 #endif /* ENABLE_BINRELOC */
332
333
334 /**
335 * br_strcat:
336 * str1: A string.
337 * str2: Another string.
338 * Returns: A newly-allocated string. This string should be freed when no longer needed.
339 *
340 * Concatenate str1 and str2 to a newly allocated string.
341 */
342 char *
br_strcat(const char * str1,const char * str2)343 br_strcat (const char *str1, const char *str2)
344 {
345 char *result;
346 size_t len1, len2;
347
348 if (!str1) str1 = "";
349 if (!str2) str2 = "";
350
351 len1 = strlen (str1);
352 len2 = strlen (str2);
353
354 result = (char *) malloc (len1 + len2 + 1);
355 memcpy (result, str1, len1);
356 memcpy (result + len1, str2, len2);
357 result[len1 + len2] = '\0';
358
359 return result;
360 }
361
362
363 /* Emulates glibc's strndup() */
364 static char *
br_strndup(char * str,size_t size)365 br_strndup (char *str, size_t size)
366 {
367 char *result = (char *) NULL;
368 size_t len;
369
370 br_return_val_if_fail (str != (char *) NULL, (char *) NULL);
371
372 len = strlen (str);
373 if (!len) return strdup ("");
374 if (size > len) size = len;
375
376 result = (char *) calloc (sizeof (char), len + 1);
377 memcpy (result, str, size);
378 return result;
379 }
380
381
382 /**
383 * br_extract_dir:
384 * path: A path.
385 * Returns: A directory name. This string should be freed when no longer needed.
386 *
387 * Extracts the directory component of path. Similar to g_dirname() or the dirname
388 * commandline application.
389 *
390 * Example:
391 * br_extract_dir ("/usr/local/foobar"); --> Returns: "/usr/local"
392 */
393 char *
br_extract_dir(const char * path)394 br_extract_dir (const char *path)
395 {
396 const char *end;
397 char *result;
398
399 br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
400
401 end = strrchr (path, '/');
402 if (!end) return strdup (".");
403
404 while (end > path && *end == '/')
405 end--;
406 result = br_strndup ((char *) path, end - path + 1);
407 if (!*result)
408 {
409 free (result);
410 return strdup ("/");
411 } else
412 return result;
413 }
414
415
416 /**
417 * br_extract_prefix:
418 * path: The full path of an executable or library.
419 * Returns: The prefix, or NULL on error. This string should be freed when no longer needed.
420 *
421 * Extracts the prefix from path. This function assumes that your executable
422 * or library is installed in an LSB-compatible directory structure.
423 *
424 * Example:
425 * br_extract_prefix ("/usr/bin/gnome-panel"); --> Returns "/usr"
426 * br_extract_prefix ("/usr/local/lib/libfoo.so"); --> Returns "/usr/local"
427 * br_extract_prefix ("/usr/local/libfoo.so"); --> Returns "/usr"
428 */
429 char *
br_extract_prefix(const char * path)430 br_extract_prefix (const char *path)
431 {
432 const char *end;
433 char *tmp, *result;
434
435 br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
436
437 if (!*path) return strdup ("/");
438 end = strrchr (path, '/');
439 if (!end) return strdup (path);
440
441 tmp = br_strndup ((char *) path, end - path);
442 if (!*tmp)
443 {
444 free (tmp);
445 return strdup ("/");
446 }
447 end = strrchr (tmp, '/');
448 if (!end) return tmp;
449
450 result = br_strndup (tmp, end - tmp);
451 free (tmp);
452
453 if (!*result)
454 {
455 free (result);
456 result = strdup ("/");
457 }
458
459 return result;
460 }
461
462
463 /**
464 * br_set_fallback_function:
465 * func: A function to call to find the binary.
466 * data: User data to pass to func.
467 *
468 * Sets a function to call to find the path to the binary, in
469 * case "/proc/self/maps" can't be opened. The function set should
470 * return a string that is safe to free with free().
471 */
472 void
br_set_locate_fallback_func(br_locate_fallback_func func,void * data)473 br_set_locate_fallback_func (br_locate_fallback_func func, void *data)
474 {
475 fallback_func = func;
476 fallback_data = data;
477 }
478
479
480 #ifdef __cplusplus
481 }
482 #endif /* __cplusplus */
483
484 #endif /* _PREFIX_C */
485