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/
7 * This source code is public domain. You can relicense this code
8 * under whatever license you want.
10 * NOTE: if you're using C++ and are getting "undefined reference
11 * to br_*", try renaming prefix.c to prefix.cpp
14 /* WARNING, BEFORE YOU MODIFY PREFIX.C:
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.
22 * #define BR_NAMESPACE(funcName) foobar_ ## funcName
23 * --> expands br_locate to foobar_br_locate
34 /* Change 1 to 0 if you don't want pthread support */
36 #endif /* BR_PTHREADS */
46 #endif /* __cplusplus */
50 #define NULL ((void *) 0)
53 #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
55 #define br_return_val_if_fail(expr,val) if (!(expr)) return val
59 static br_locate_fallback_func fallback_func
= NULL
;
60 static void *fallback_data
= NULL
;
63 #ifdef ENABLE_BINRELOC
64 #include <sys/types.h>
66 #include <sys/param.h>
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.
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.
86 * int main (int argc, char *argv[]) {
87 * printf ("Full path of this app: %s\n", br_locate (&argc));
92 * --> libfoo.c starts here
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 (""));
102 br_locate (void *symbol
)
108 br_return_val_if_fail (symbol
!= NULL
, NULL
);
110 f
= fopen ("/proc/self/maps", "r");
113 return fallback_func(symbol
, fallback_data
);
120 unsigned long start
, end
;
122 if (!fgets (line
, sizeof (line
), f
))
124 if (!strstr (line
, " r-xp ") || !strchr (line
, '/'))
127 sscanf (line
, "%lx-%lx ", &start
, &end
);
128 if (symbol
>= (void *) start
&& symbol
< (void *) end
)
133 /* Extract the filename; it is always an absolute path */
134 path
= strchr (line
, '/');
136 /* Get rid of the newline */
137 tmp
= strrchr (path
, '\n');
140 /* Get rid of "(deleted)" */
142 if (len
> 10 && strcmp (path
+ len
- 10, " (deleted)") == 0)
144 tmp
= path
+ len
- 10;
149 return strdup (path
);
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.
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.
168 * --> This application is located in /usr/bin/foo
169 * br_locate_prefix (&argc); --> returns: "/usr"
172 br_locate_prefix (void *symbol
)
176 br_return_val_if_fail (symbol
!= NULL
, NULL
);
178 path
= br_locate (symbol
);
179 if (!path
) return NULL
;
181 prefix
= br_extract_prefix (path
);
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
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.
198 * --> The application is /usr/bin/foo
199 * br_prepend_prefix (&argc, "/share/foo/data.png"); --> Returns "/usr/share/foo/data.png"
202 br_prepend_prefix (void *symbol
, char *path
)
206 br_return_val_if_fail (symbol
!= NULL
, NULL
);
207 br_return_val_if_fail (path
!= NULL
, NULL
);
209 tmp
= br_locate_prefix (symbol
);
210 if (!tmp
) return NULL
;
212 if (strcmp (tmp
, "/") == 0)
213 newpath
= strdup (path
);
215 newpath
= br_strcat (tmp
, path
);
217 /* Get rid of compiler warning ("br_prepend_prefix never used") */
218 if (0) br_prepend_prefix (NULL
, NULL
);
224 #endif /* ENABLE_BINRELOC */
227 /* Pthread stuff for thread safetiness */
228 #if BR_PTHREADS && defined(ENABLE_BINRELOC)
232 static pthread_key_t br_thread_key
;
233 static pthread_once_t br_thread_key_once
= PTHREAD_ONCE_INIT
;
237 br_thread_local_store_fini ()
241 specific
= (char *) pthread_getspecific (br_thread_key
);
245 pthread_setspecific (br_thread_key
, NULL
);
247 pthread_key_delete (br_thread_key
);
253 br_str_free (void *str
)
261 br_thread_local_store_init ()
263 if (pthread_key_create (&br_thread_key
, br_str_free
) == 0)
264 atexit (br_thread_local_store_fini
);
267 #else /* BR_PTHREADS */
268 #ifdef ENABLE_BINRELOC
270 static char *br_last_value
= (char *) NULL
;
273 br_free_last_value ()
276 free (br_last_value
);
279 #endif /* ENABLE_BINRELOC */
280 #endif /* BR_PTHREADS */
283 #ifdef ENABLE_BINRELOC
286 * br_thread_local_store:
287 * str: A dynamically allocated string.
288 * Returns: str. This return value must not be freed.
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:
295 * some_function( BR_DATADIR("/one.png"), BR_DATADIR("/two.png") )
299 * foo = br_thread_local_store (strdup ("hello")); --> foo == "hello"
300 * foo = br_thread_local_store (strdup ("world")); --> foo == "world"; "hello" is now freed.
303 br_thread_local_store (char *str
)
308 pthread_once (&br_thread_key_once
, br_thread_local_store_init
);
310 specific
= (char *) pthread_getspecific (br_thread_key
);
311 br_str_free (specific
);
312 pthread_setspecific (br_thread_key
, str
);
314 #else /* BR_PTHREADS */
315 static int initialized
= 0;
319 atexit (br_free_last_value
);
324 free (br_last_value
);
326 #endif /* BR_PTHREADS */
328 return (const char *) str
;
331 #endif /* ENABLE_BINRELOC */
337 * str2: Another string.
338 * Returns: A newly-allocated string. This string should be freed when no longer needed.
340 * Concatenate str1 and str2 to a newly allocated string.
343 br_strcat (const char *str1
, const char *str2
)
348 if (!str1
) str1
= "";
349 if (!str2
) str2
= "";
351 len1
= strlen (str1
);
352 len2
= strlen (str2
);
354 result
= (char *) malloc (len1
+ len2
+ 1);
355 memcpy (result
, str1
, len1
);
356 memcpy (result
+ len1
, str2
, len2
);
357 result
[len1
+ len2
] = '\0';
363 /* Emulates glibc's strndup() */
365 br_strndup (char *str
, size_t size
)
367 char *result
= (char *) NULL
;
370 br_return_val_if_fail (str
!= (char *) NULL
, (char *) NULL
);
373 if (!len
) return strdup ("");
374 if (size
> len
) size
= len
;
376 result
= (char *) calloc (sizeof (char), len
+ 1);
377 memcpy (result
, str
, size
);
385 * Returns: A directory name. This string should be freed when no longer needed.
387 * Extracts the directory component of path. Similar to g_dirname() or the dirname
388 * commandline application.
391 * br_extract_dir ("/usr/local/foobar"); --> Returns: "/usr/local"
394 br_extract_dir (const char *path
)
398 br_return_val_if_fail (path
!= (char *) NULL
, (char *) NULL
);
400 end
= strrchr (path
, '/');
401 if (!end
) return strdup (".");
403 while (end
> path
&& *end
== '/')
405 result
= br_strndup ((char *) path
, end
- path
+ 1);
417 * path: The full path of an executable or library.
418 * Returns: The prefix, or NULL on error. This string should be freed when no longer needed.
420 * Extracts the prefix from path. This function assumes that your executable
421 * or library is installed in an LSB-compatible directory structure.
424 * br_extract_prefix ("/usr/bin/gnome-panel"); --> Returns "/usr"
425 * br_extract_prefix ("/usr/local/lib/libfoo.so"); --> Returns "/usr/local"
426 * br_extract_prefix ("/usr/local/libfoo.so"); --> Returns "/usr"
429 br_extract_prefix (const char *path
)
431 char *end
, *tmp
, *result
;
433 br_return_val_if_fail (path
!= (char *) NULL
, (char *) NULL
);
435 if (!*path
) return strdup ("/");
436 end
= strrchr (path
, '/');
437 if (!end
) return strdup (path
);
439 tmp
= br_strndup ((char *) path
, end
- path
);
445 end
= strrchr (tmp
, '/');
446 if (!end
) return tmp
;
448 result
= br_strndup (tmp
, end
- tmp
);
454 result
= strdup ("/");
462 * br_set_fallback_function:
463 * func: A function to call to find the binary.
464 * data: User data to pass to func.
466 * Sets a function to call to find the path to the binary, in
467 * case "/proc/self/maps" can't be opened. The function set should
468 * return a string that is safe to free with free().
471 br_set_locate_fallback_func (br_locate_fallback_func func
, void *data
)
473 fallback_func
= func
;
474 fallback_data
= data
;
480 #endif /* __cplusplus */
482 #endif /* _PREFIX_C */