Code cleanup in binreloc: Remove unused init_lib function. Add possibility to set the looked-up exe path from somewhere else.

Remove symbol mangling because r18940 already added the gnc_* prefix,
hence the symbol names are unique to gnucash anyway.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18972 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2010-03-27 22:08:17 +00:00
parent 50163126ea
commit bc411c57db
3 changed files with 22 additions and 174 deletions

View File

@ -227,129 +227,23 @@ _br_find_exe (Gnc_GbrInitError *error)
}
/** @internal
* Find the canonical filename of the executable which owns symbol.
* Returns a filename which must be freed, or NULL on error.
*/
static char *
_br_find_exe_for_symbol (const void *symbol, Gnc_GbrInitError *error)
{
#ifndef ENABLE_BINRELOC
if (error)
*error = GNC_GBR_INIT_ERROR_DISABLED;
return (char *) NULL;
#else
#if defined G_OS_WIN32
g_warning ("_br_find_exe_for_symbol not implemented on win32.");
if (error)
*error = GNC_GBR_INIT_ERROR_DISABLED;
return (char *) NULL;
#else
#define SIZE PATH_MAX + 100
FILE *f;
size_t address_string_len;
char *address_string, line[SIZE], *found;
if (symbol == NULL)
return (char *) NULL;
f = fopen ("/proc/self/maps", "r");
if (f == NULL)
return (char *) NULL;
address_string_len = 4;
address_string = (char *) g_try_malloc (address_string_len);
found = (char *) NULL;
while (!feof (f))
{
char *start_addr, *end_addr, *end_addr_end, *file;
void *start_addr_p, *end_addr_p;
size_t len;
if (fgets (line, SIZE, f) == NULL)
break;
/* Sanity check. */
if (strstr (line, " r-xp ") == NULL || strchr (line, '/') == NULL)
continue;
/* Parse line. */
start_addr = line;
end_addr = strchr (line, '-');
file = strchr (line, '/');
/* More sanity check. */
if (!(file > end_addr && end_addr != NULL && end_addr[0] == '-'))
continue;
end_addr[0] = '\0';
end_addr++;
end_addr_end = strchr (end_addr, ' ');
if (end_addr_end == NULL)
continue;
end_addr_end[0] = '\0';
len = strlen (file);
if (len == 0)
continue;
if (file[len - 1] == '\n')
file[len - 1] = '\0';
/* Get rid of "(deleted)" from the filename. */
len = strlen (file);
if (len > 10 && strcmp (file + len - 10, " (deleted)") == 0)
file[len - 10] = '\0';
/* I don't know whether this can happen but better safe than sorry. */
len = strlen (start_addr);
if (len != strlen (end_addr))
continue;
/* Transform the addresses into a string in the form of 0xdeadbeef,
* then transform that into a pointer. */
if (address_string_len < len + 3)
{
address_string_len = len + 3;
address_string = (char *) g_try_realloc (address_string, address_string_len);
}
memcpy (address_string, "0x", 2);
memcpy (address_string + 2, start_addr, len);
address_string[2 + len] = '\0';
sscanf (address_string, "%p", &start_addr_p);
memcpy (address_string, "0x", 2);
memcpy (address_string + 2, end_addr, len);
address_string[2 + len] = '\0';
sscanf (address_string, "%p", &end_addr_p);
if (symbol >= start_addr_p && symbol < end_addr_p)
{
found = file;
break;
}
}
g_free (address_string);
fclose (f);
if (found == NULL)
return (char *) NULL;
else
return g_strdup (found);
#endif /* G_OS_WIN32 */
#endif /* ENABLE_BINRELOC */
}
static gchar *exe = NULL;
static void set_gerror (GError **error, Gnc_GbrInitError errcode);
void gnc_gbr_set_exe (const gchar* default_exe)
{
if (exe != NULL)
g_free(exe);
exe = NULL;
if (default_exe != NULL)
exe = g_strdup(default_exe);
}
/** Initialize the BinReloc library (for applications).
*
* This function must be called before using any other BinReloc functions.
@ -384,34 +278,6 @@ gnc_gbr_init (GError **error)
}
/** Initialize the BinReloc library (for libraries).
*
* This function must be called before using any other BinReloc functions.
* It attempts to locate the calling library's canonical filename.
*
* @note The BinReloc source code MUST be included in your library, or this
* function won't work correctly.
*
* @returns TRUE on success, FALSE if a filename cannot be found.
*/
gboolean
gnc_gbr_init_lib (GError **error)
{
Gnc_GbrInitError errcode = 0;
exe = _br_find_exe_for_symbol ((const void *) "", &errcode);
if (exe != NULL)
/* Success! */
return TRUE;
else
{
/* Failed :-( */
set_gerror (error, errcode);
return exe != NULL;
}
}
static void
set_gerror (GError **error, Gnc_GbrInitError errcode)
{

View File

@ -18,7 +18,7 @@
G_BEGIN_DECLS
/** These error codes can be returned by br_init(), br_init_lib(), gnc_gbr_init() or gnc_gbr_init_lib(). */
/** These error codes can be returned by gnc_gbr_init(). */
typedef enum
{
/** Cannot allocate memory. */
@ -38,22 +38,7 @@ typedef enum
} Gnc_GbrInitError;
#ifndef BINRELOC_RUNNING_DOXYGEN
/* Mangle symbol names to avoid symbol collisions with other ELF objects. */
#define gnc_gbr_find_exe ffEt66859784967989_gnc_gbr_find_exe
#define gnc_gbr_find_exe_dir ffEt66859784967989_gnc_gbr_find_exe_dir
#define gnc_gbr_find_prefix ffEt66859784967989_gnc_gbr_find_prefix
#define gnc_gbr_find_bin_dir ffEt66859784967989_gnc_gbr_find_bin_dir
#define gnc_gbr_find_sbin_dir ffEt66859784967989_gnc_gbr_find_sbin_dir
#define gnc_gbr_find_data_dir ffEt66859784967989_gnc_gbr_find_data_dir
#define gnc_gbr_find_lib_dir ffEt66859784967989_gnc_gbr_find_lib_dir
#define gnc_gbr_find_libexec_dir ffEt66859784967989_gnc_gbr_find_libexec_dir
#define gnc_gbr_find_etc_dir ffEt66859784967989_gnc_gbr_find_etc_dir
#endif
gboolean gnc_gbr_init (GError **error);
gboolean gnc_gbr_init_lib (GError **error);
gchar *gnc_gbr_find_exe (const gchar *default_exe);
gchar *gnc_gbr_find_exe_dir (const gchar *default_dir);
@ -65,6 +50,12 @@ gchar *gnc_gbr_find_lib_dir (const gchar *default_lib_dir);
gchar *gnc_gbr_find_libexec_dir (const gchar *default_libexec_dir);
gchar *gnc_gbr_find_etc_dir (const gchar *default_etc_dir);
/** Sets the executable path to the given value. This is useful if the
* binreloc lookup code will not be used, but instead the executable
* location is obtained from somewhere else (e.g. qt) but the gnucash
* code should nevertheless use this path internally. */
void gnc_gbr_set_exe (const gchar* default_exe);
G_END_DECLS

View File

@ -148,18 +148,10 @@ main(int argc, char ** argv)
#endif
g_thread_init(NULL);
#ifdef ENABLE_BINRELOC
{
GError *binreloc_error = NULL;
if (!gnc_gbr_init(&binreloc_error))
{
g_print("main: Error on gnc_gbr_init: %s\n", binreloc_error->message);
g_error_free(binreloc_error);
}
}
#else
//g_message("main: binreloc relocation support was disabled at configure time.\n");
#endif
QApplication app(argc, argv);
// Binreloc is initialized by the Qt exe path lookup.
gnc_gbr_set_exe(QCoreApplication::applicationFilePath().toUtf8());
#ifdef HAVE_GETTEXT
{
@ -194,7 +186,6 @@ main(int argc, char ** argv)
int r;
{
// From here on the new C++ code
QApplication app(argc, argv);
gnc::MainWindow mainWin;
mainWin.show();