59 #define NULL ((void *) 0) 62 #define br_return_val_if_fail(expr,val) if (!(expr)) {fprintf (stderr, "** BinReloc (%s): assertion %s failed\n", __PRETTY_FUNCTION__, #expr); return val;} 64 #define br_return_val_if_fail(expr,val) if (!(expr)) return val 68 #ifdef ENABLE_BINRELOC 69 #include <sys/types.h> 71 #include <sys/param.h> 107 br_locate (
void *symbol)
115 f = fopen (
"/proc/self/maps",
"r");
121 unsigned long start, end;
123 if (!fgets (line,
sizeof (line), f))
125 if (!strstr (line,
" r-xp ") || !strchr (line,
'/'))
128 sscanf (line,
"%lx-%lx ", &start, &end);
129 if (symbol >= (
void *) start && symbol < (
void *) end)
135 path = strchr (line,
'/');
138 tmp = strrchr (path,
'\n');
143 if (len > 10 && strcmp (path + len - 10,
" (deleted)") == 0)
145 tmp = path + len - 10;
150 return strdup (path);
173 br_locate_prefix (
void *symbol)
179 path = br_locate (symbol);
180 if (!path)
return (
char*)
NULL;
203 br_prepend_prefix (
void *symbol,
char *path)
208 char* tmp = br_locate_prefix (symbol);
209 if (!tmp)
return (
char*)0;
212 if (strcmp (tmp,
"/") == 0)
213 newpath = strdup (path);
218 if (0) br_prepend_prefix (0, (
char*)0);
236 static pthread_once_t br_thread_key_once = PTHREAD_ONCE_INIT;
242 char* specific = (
char *) pthread_getspecific (br_thread_key);
246 pthread_setspecific (br_thread_key,
NULL);
248 pthread_key_delete (br_thread_key);
264 if (pthread_key_create (&br_thread_key,
br_str_free) == 0)
270 static char *br_last_value = (
char *)
NULL;
273 br_free_last_value ()
276 free (br_last_value);
303 char* specific = (
char *) pthread_getspecific (br_thread_key);
305 pthread_setspecific (br_thread_key, str);
308 static int initialized = 0;
312 atexit (br_free_last_value);
317 free (br_last_value);
321 return (
const char *) str;
336 if (!str1) str1 =
"";
337 if (!str2) str2 =
"";
339 size_t len1 = strlen (str1);
340 size_t len2 = strlen (str2);
342 char* result = (
char *) malloc (len1 + len2 + 1);
343 memcpy (result, str1, len1);
344 memcpy (result + len1, str2, len2);
345 result[len1 + len2] =
'\0';
357 size_t len = strlen (str);
358 if (!len)
return strdup (
"");
359 if (size > len) size = len;
361 char* result = (
char *) calloc (
sizeof (
char), len + 1);
362 memcpy (result, str, size);
383 const char* end = strrchr (path,
'/');
384 if (!end)
return strdup (
".");
386 while (end > path && *end ==
'/')
390 char* result =
br_strndup ((
char *) path, end - path + 1);
418 if (!*path)
return strdup (
"/");
419 const char* end = strrchr (path,
'/');
420 if (!end)
return strdup (path);
422 char* tmp =
br_strndup ((
char *) path, end - path);
428 end = strrchr (tmp,
'/');
429 if (!end)
return tmp;
437 result = strdup (
"/");
static pthread_once_t br_thread_key_once
static pthread_key_t br_thread_key
#define br_return_val_if_fail(expr, val)
static void br_thread_local_store_init()
static void br_thread_local_store_fini()
static void br_str_free(void *str)
static char * br_strndup(char *str, size_t size)
char * br_strcat(const char *str1, const char *str2)
br_strcat: str1: A string.
const char * br_thread_local_store(char *str)
br_thread_local_store: str: A dynamically allocated string.