src/thirdparty/binreloc/prefix.c

Go to the documentation of this file.
00001 /*
00002  * BinReloc - a library for creating relocatable executables
00003  * Written by: Mike Hearn <mike@theoretic.com>
00004  *             Hongli Lai <h.lai@chello.nl>
00005  * http://autopackage.org/
00006  * 
00007  * This source code is public domain. You can relicense this code
00008  * under whatever license you want.
00009  *
00010  * NOTE: if you're using C++ and are getting "undefined reference
00011  * to br_*", try renaming prefix.c to prefix.cpp
00012  */
00013 
00014 /* WARNING, BEFORE YOU MODIFY PREFIX.C:
00015  *
00016  * If you make changes to any of the functions in prefix.c, you MUST
00017  * change the BR_NAMESPACE macro (in prefix.h).
00018  * This way you can avoid symbol table conflicts with other libraries
00019  * that also happen to use BinReloc.
00020  *
00021  * Example:
00022  * #define BR_NAMESPACE(funcName) foobar_ ## funcName
00023  * --> expands br_locate to foobar_br_locate
00024  */
00025 
00026 #ifndef _PREFIX_C_
00027 #define _PREFIX_C_
00028 
00029 #ifdef HAVE_CONFIG_H
00030         #include "aa_config.h"
00031 #endif
00032 
00033 #ifndef BR_PTHREADS
00034         /* Change 1 to 0 if you don't want pthread support */
00035         #define BR_PTHREADS 1
00036 #endif /* BR_PTHREADS */
00037 
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <limits.h>
00041 #include <string.h>
00042 #include "prefix.h"
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif /* __cplusplus */
00047 
00048 
00049 #undef NULL
00050 #define NULL ((void *) 0)
00051 
00052 #ifdef __GNUC__
00053         #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;}
00054 #else
00055         #define br_return_val_if_fail(expr,val) if (!(expr)) return val
00056 #endif /* __GNUC__ */
00057 
00058 
00059 static br_locate_fallback_func fallback_func = (br_locate_fallback_func) NULL;
00060 static void *fallback_data = NULL;
00061 
00062 
00063 #ifdef ENABLE_BINRELOC
00064 #include <sys/types.h>
00065 #include <sys/stat.h>
00066 #include <sys/param.h>
00067 #include <unistd.h>
00068 
00069 
00101 char *
00102 br_locate (void *symbol)
00103 {
00104         char line[5000];
00105         FILE *f;
00106         char *path;
00107 
00108         br_return_val_if_fail (symbol != NULL, NULL);
00109 
00110         f = fopen ("/proc/self/maps", "r");
00111         if (!f) {
00112                 if (fallback_func)
00113                         return fallback_func(symbol, fallback_data);
00114                 else
00115                         return NULL;
00116         }
00117 
00118         while (!feof (f))
00119         {
00120                 unsigned long start, end;
00121 
00122                 if (!fgets (line, sizeof (line), f))
00123                         continue;
00124                 if (!strstr (line, " r-xp ") || !strchr (line, '/'))
00125                         continue;
00126 
00127                 sscanf (line, "%lx-%lx ", &start, &end);
00128                 if (symbol >= (void *) start && symbol < (void *) end)
00129                 {
00130                         char *tmp;
00131                         size_t len;
00132 
00133                         /* Extract the filename; it is always an absolute path */
00134                         path = strchr (line, '/');
00135 
00136                         /* Get rid of the newline */
00137                         tmp = strrchr (path, '\n');
00138                         if (tmp) *tmp = 0;
00139 
00140                         /* Get rid of "(deleted)" */
00141                         len = strlen (path);
00142                         if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
00143                         {
00144                                 tmp = path + len - 10;
00145                                 *tmp = 0;
00146                         }
00147 
00148                         fclose(f);
00149                         return strdup (path);
00150                 }
00151         }
00152 
00153         fclose (f);
00154         return NULL;
00155 }
00156 
00157 
00171 char *
00172 br_locate_prefix (void *symbol)
00173 {
00174         char *path, *prefix;
00175 
00176         br_return_val_if_fail (symbol != NULL, NULL);
00177 
00178         path = br_locate (symbol);
00179         if (!path) return NULL;
00180 
00181         prefix = br_extract_prefix (path);
00182         free (path);
00183         return prefix;
00184 }
00185 
00186 
00201 char *
00202 br_prepend_prefix (void *symbol, char *path)
00203 {
00204         char *tmp, *newpath;
00205 
00206         br_return_val_if_fail (symbol != NULL, NULL);
00207         br_return_val_if_fail (path != NULL, NULL);
00208 
00209         tmp = br_locate_prefix (symbol);
00210         if (!tmp) return NULL;
00211 
00212         if (strcmp (tmp, "/") == 0)
00213                 newpath = strdup (path);
00214         else
00215                 newpath = br_strcat (tmp, path);
00216 
00217         /* Get rid of compiler warning ("br_prepend_prefix never used") */
00218         if (0) br_prepend_prefix (NULL, NULL);
00219 
00220         free (tmp);
00221         return newpath;
00222 }
00223 
00224 #endif /* ENABLE_BINRELOC */
00225 
00226 
00227 /* Pthread stuff for thread safetiness */
00228 #if BR_PTHREADS && defined(ENABLE_BINRELOC)
00229 
00230 #include <pthread.h>
00231 
00232 static pthread_key_t br_thread_key;
00233 static pthread_once_t br_thread_key_once = PTHREAD_ONCE_INIT;
00234 
00235 
00236 static void
00237 br_thread_local_store_fini ()
00238 {
00239         char *specific;
00240 
00241         specific = (char *) pthread_getspecific (br_thread_key);
00242         if (specific)
00243         {
00244                 free (specific);
00245                 pthread_setspecific (br_thread_key, NULL);
00246         }
00247         pthread_key_delete (br_thread_key);
00248         br_thread_key = 0;
00249 }
00250 
00251 
00252 static void
00253 br_str_free (void *str)
00254 {
00255         if (str)
00256                 free (str);
00257 }
00258 
00259 
00260 static void
00261 br_thread_local_store_init ()
00262 {
00263         if (pthread_key_create (&br_thread_key, br_str_free) == 0)
00264                 atexit (br_thread_local_store_fini);
00265 }
00266 
00267 #else /* BR_PTHREADS */
00268 #ifdef ENABLE_BINRELOC
00269 
00270 static char *br_last_value = (char *) NULL;
00271 
00272 static void
00273 br_free_last_value ()
00274 {
00275         if (br_last_value)
00276                 free (br_last_value);
00277 }
00278 
00279 #endif /* ENABLE_BINRELOC */
00280 #endif /* BR_PTHREADS */
00281 
00282 
00283 #ifdef ENABLE_BINRELOC
00284 
00302 const char *
00303 br_thread_local_store (char *str)
00304 {
00305         #if BR_PTHREADS
00306                 char *specific;
00307 
00308                 pthread_once (&br_thread_key_once, br_thread_local_store_init);
00309 
00310                 specific = (char *) pthread_getspecific (br_thread_key);
00311                 br_str_free (specific);
00312                 pthread_setspecific (br_thread_key, str);
00313 
00314         #else /* BR_PTHREADS */
00315                 static int initialized = 0;
00316 
00317                 if (!initialized)
00318                 {
00319                         atexit (br_free_last_value);
00320                         initialized = 1;
00321                 }
00322 
00323                 if (br_last_value)
00324                         free (br_last_value);
00325                 br_last_value = str;
00326         #endif /* BR_PTHREADS */
00327 
00328         return (const char *) str;
00329 }
00330 
00331 #endif /* ENABLE_BINRELOC */
00332 
00333 
00342 char *
00343 br_strcat (const char *str1, const char *str2)
00344 {
00345         char *result;
00346         size_t len1, len2;
00347 
00348         if (!str1) str1 = "";
00349         if (!str2) str2 = "";
00350 
00351         len1 = strlen (str1);
00352         len2 = strlen (str2);
00353 
00354         result = (char *) malloc (len1 + len2 + 1);
00355         memcpy (result, str1, len1);
00356         memcpy (result + len1, str2, len2);
00357         result[len1 + len2] = '\0';
00358 
00359         return result;
00360 }
00361 
00362 
00363 /* Emulates glibc's strndup() */
00364 static char *
00365 br_strndup (char *str, size_t size)
00366 {
00367         char *result = (char *) NULL;
00368         size_t len;
00369 
00370         br_return_val_if_fail (str != (char *) NULL, (char *) NULL);
00371 
00372         len = strlen (str);
00373         if (!len) return strdup ("");
00374         if (size > len) size = len;
00375 
00376         result = (char *) calloc (sizeof (char), len + 1);
00377         memcpy (result, str, size);
00378         return result;
00379 }
00380 
00381 
00393 char *
00394 br_extract_dir (const char *path)
00395 {
00396         char *end, *result;
00397 
00398         br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
00399 
00400         end = strrchr (path, '/');
00401         if (!end) return strdup (".");
00402 
00403         while (end > path && *end == '/')
00404                 end--;
00405         result = br_strndup ((char *) path, end - path + 1);
00406         if (!*result)
00407         {
00408                 free (result);
00409                 return strdup ("/");
00410         } else
00411                 return result;
00412 }
00413 
00414 
00428 char *
00429 br_extract_prefix (const char *path)
00430 {
00431         char *end, *tmp, *result;
00432 
00433         br_return_val_if_fail (path != (char *) NULL, (char *) NULL);
00434 
00435         if (!*path) return strdup ("/");
00436         end = strrchr (path, '/');
00437         if (!end) return strdup (path);
00438 
00439         tmp = br_strndup ((char *) path, end - path);
00440         if (!*tmp)
00441         {
00442                 free (tmp);
00443                 return strdup ("/");
00444         }
00445         end = strrchr (tmp, '/');
00446         if (!end) return tmp;
00447 
00448         result = br_strndup (tmp, end - tmp);
00449         free (tmp);
00450 
00451         if (!*result)
00452         {
00453                 free (result);
00454                 result = strdup ("/");
00455         }
00456 
00457         return result;
00458 }
00459 
00460 
00470 void
00471 br_set_locate_fallback_func (br_locate_fallback_func func, void *data)
00472 {
00473         fallback_func = func;
00474         fallback_data = data;
00475 }
00476 
00477 
00478 #ifdef __cplusplus
00479 }
00480 #endif /* __cplusplus */
00481 
00482 #endif /* _PREFIX_C */

Generated on Sat Mar 15 22:55:56 2008 for Armagetron Advanced by  doxygen 1.5.4