2005-11-06 09:06:41 +08:00
|
|
|
/*
|
|
|
|
* BinReloc - a library for creating relocatable executables
|
|
|
|
* Written by: Hongli Lai <h.lai@chello.nl>
|
|
|
|
* http://autopackage.org/
|
|
|
|
*
|
|
|
|
* This source code is public domain. You can relicense this code
|
|
|
|
* under whatever license you want.
|
|
|
|
*
|
|
|
|
* See http://autopackage.org/docs/binreloc/ for
|
|
|
|
* more information and how to use this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-02-05 21:21:23 +08:00
|
|
|
#if defined(ENABLE_RELOCATABLE_RESOURCES) && ! defined(G_OS_WIN32)
|
2005-11-06 09:06:41 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2019-02-05 21:21:23 +08:00
|
|
|
#endif /* ENABLE_RELOCATABLE_RESOURCES && ! G_OS_WIN32 */
|
2005-11-06 09:06:41 +08:00
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
#include <gio/gio.h>
|
2005-11-06 09:06:41 +08:00
|
|
|
#include <glib.h>
|
2005-11-16 20:03:36 +08:00
|
|
|
#include <glib/gstdio.h>
|
2005-11-06 09:06:41 +08:00
|
|
|
|
|
|
|
#include "gimpreloc.h"
|
|
|
|
|
|
|
|
|
2005-12-14 21:36:12 +08:00
|
|
|
/*
|
2005-11-06 09:06:41 +08:00
|
|
|
* Find the canonical filename of the executable. Returns the filename
|
|
|
|
* (which must be freed) or NULL on error. If the parameter 'error' is
|
2013-01-27 23:52:38 +08:00
|
|
|
* not NULL, the error code will be stored there, if an error occurred.
|
2005-11-06 09:06:41 +08:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
_br_find_exe (GimpBinrelocInitError *error)
|
|
|
|
{
|
2021-10-11 06:16:08 +08:00
|
|
|
#if ! defined(ENABLE_RELOCATABLE_RESOURCES) || defined(G_OS_WIN32) || defined(__APPLE__)
|
2019-02-05 22:00:44 +08:00
|
|
|
if (error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_DISABLED;
|
|
|
|
return NULL;
|
2005-11-06 09:06:41 +08:00
|
|
|
#else
|
2021-03-21 01:40:32 +08:00
|
|
|
GDataInputStream *data_input;
|
|
|
|
GInputStream *input;
|
|
|
|
GFile *file;
|
|
|
|
GError *gerror = NULL;
|
|
|
|
gchar *path;
|
|
|
|
gchar *sym_path;
|
|
|
|
gchar *maps_line;
|
2019-02-05 22:00:44 +08:00
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
sym_path = g_strdup ("/proc/self/exe");
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2021-03-21 01:40:32 +08:00
|
|
|
struct stat stat_buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Do not use readlink() with a buffer of size PATH_MAX because
|
|
|
|
* some systems actually allow paths of bigger size. Thus this
|
|
|
|
* macro is kind of bogus. Some systems like Hurd will not even
|
|
|
|
* define it (see MR !424).
|
|
|
|
* g_file_read_link() on the other hand will return a size of
|
|
|
|
* appropriate size, with newline removed and NUL terminator
|
|
|
|
* added.
|
|
|
|
*/
|
|
|
|
path = g_file_read_link (sym_path, &gerror);
|
|
|
|
g_free (sym_path);
|
|
|
|
if (! path)
|
2019-02-05 22:00:44 +08:00
|
|
|
{
|
2021-03-21 01:40:32 +08:00
|
|
|
/* Read link fails but we can try reading /proc/self/maps as
|
|
|
|
* an alternate method.
|
|
|
|
*/
|
|
|
|
g_printerr ("%s: %s\n", G_STRFUNC, gerror->message);
|
2021-10-03 08:29:15 +08:00
|
|
|
g_clear_error (&gerror);
|
2021-03-21 01:40:32 +08:00
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
break;
|
2005-12-14 21:36:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
/* Check whether the symlink's target is also a symlink.
|
|
|
|
* We want to get the final target. */
|
|
|
|
i = stat (path, &stat_buf);
|
|
|
|
if (i == -1)
|
|
|
|
{
|
|
|
|
/* Error. */
|
|
|
|
break;
|
2005-12-14 21:36:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
/* stat() success. */
|
2021-03-21 01:40:32 +08:00
|
|
|
if (! S_ISLNK (stat_buf.st_mode))
|
2019-02-05 22:00:44 +08:00
|
|
|
{
|
|
|
|
/* path is not a symlink. Done. */
|
|
|
|
return path;
|
2005-12-14 21:36:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
/* path is a symlink. Continue loop and resolve this. */
|
2021-03-21 01:40:32 +08:00
|
|
|
sym_path = path;
|
2019-02-05 22:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readlink() or stat() failed; this can happen when the program is
|
|
|
|
* running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
|
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
file = g_file_new_for_path ("/proc/self/maps");
|
|
|
|
input = G_INPUT_STREAM (g_file_read (file, NULL, &gerror));
|
|
|
|
g_object_unref (file);
|
|
|
|
if (! input)
|
2019-02-05 22:00:44 +08:00
|
|
|
{
|
2021-03-21 01:40:32 +08:00
|
|
|
g_printerr ("%s: %s", G_STRFUNC, gerror->message);
|
2021-10-03 08:29:15 +08:00
|
|
|
g_clear_error (&gerror);
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_OPEN_MAPS;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
data_input = g_data_input_stream_new (input);
|
|
|
|
g_object_unref (input);
|
|
|
|
|
|
|
|
/* The first entry with r-xp permission should be the executable name. */
|
|
|
|
while ((maps_line = g_data_input_stream_read_line (data_input, NULL, NULL, &gerror)))
|
2019-02-05 22:00:44 +08:00
|
|
|
{
|
2021-03-21 01:40:32 +08:00
|
|
|
if (maps_line == NULL)
|
|
|
|
{
|
|
|
|
if (gerror)
|
|
|
|
{
|
|
|
|
g_printerr ("%s: %s\n", G_STRFUNC, gerror->message);
|
|
|
|
g_error_free (gerror);
|
|
|
|
}
|
|
|
|
g_object_unref (data_input);
|
2019-02-05 22:00:44 +08:00
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
if (error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_READ_MAPS;
|
2019-02-05 22:00:44 +08:00
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the filename; it is always an absolute path. */
|
|
|
|
path = strchr (maps_line, '/');
|
|
|
|
|
|
|
|
/* Sanity check. */
|
|
|
|
if (path && strstr (maps_line, " r-xp "))
|
|
|
|
{
|
|
|
|
/* We found the executable name. */
|
|
|
|
path = g_strdup (path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (maps_line);
|
|
|
|
maps_line = NULL;
|
|
|
|
path = NULL;
|
2019-02-05 22:00:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-21 01:40:32 +08:00
|
|
|
if (path == NULL && error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_INVALID_MAPS;
|
|
|
|
|
|
|
|
g_object_unref (data_input);
|
|
|
|
g_free (maps_line);
|
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
return path;
|
2019-02-05 21:21:23 +08:00
|
|
|
#endif /* ! ENABLE_RELOCATABLE_RESOURCES || G_OS_WIN32 */
|
2005-11-06 09:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-14 21:36:12 +08:00
|
|
|
/*
|
2005-11-06 09:06:41 +08:00
|
|
|
* 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, GimpBinrelocInitError *error)
|
|
|
|
{
|
2021-10-10 00:32:58 +08:00
|
|
|
#if ! defined(ENABLE_RELOCATABLE_RESOURCES) || defined(G_OS_WIN32) || defined(__APPLE__)
|
2019-02-05 22:00:44 +08:00
|
|
|
if (error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_DISABLED;
|
|
|
|
return (char *) NULL;
|
2005-11-06 09:06:41 +08:00
|
|
|
#else
|
2021-03-21 04:15:15 +08:00
|
|
|
GDataInputStream *data_input;
|
|
|
|
GInputStream *input;
|
|
|
|
GFile *file;
|
|
|
|
GError *gerror = NULL;
|
|
|
|
gchar *maps_line;
|
|
|
|
char *found = NULL;
|
|
|
|
char *address_string;
|
|
|
|
size_t address_string_len;
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
if (symbol == NULL)
|
|
|
|
return (char *) NULL;
|
|
|
|
|
2021-03-21 04:15:15 +08:00
|
|
|
file = g_file_new_for_path ("/proc/self/maps");
|
|
|
|
input = G_INPUT_STREAM (g_file_read (file, NULL, &gerror));
|
|
|
|
g_object_unref (file);
|
|
|
|
if (! input)
|
|
|
|
{
|
|
|
|
g_printerr ("%s: %s", G_STRFUNC, gerror->message);
|
|
|
|
g_error_free (gerror);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_OPEN_MAPS;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_input = g_data_input_stream_new (input);
|
|
|
|
g_object_unref (input);
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
address_string_len = 4;
|
|
|
|
address_string = g_try_new (char, address_string_len);
|
|
|
|
|
2021-03-21 04:15:15 +08:00
|
|
|
while ((maps_line = g_data_input_stream_read_line (data_input, NULL, NULL, &gerror)))
|
2019-02-05 22:00:44 +08:00
|
|
|
{
|
2021-03-21 04:15:15 +08:00
|
|
|
char *start_addr, *end_addr, *end_addr_end;
|
|
|
|
char *path;
|
|
|
|
void *start_addr_p, *end_addr_p;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (maps_line == NULL)
|
|
|
|
{
|
|
|
|
if (gerror)
|
|
|
|
{
|
|
|
|
g_printerr ("%s: %s\n", G_STRFUNC, gerror->message);
|
|
|
|
g_error_free (gerror);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = GIMP_RELOC_INIT_ERROR_READ_MAPS;
|
2019-02-05 22:00:44 +08:00
|
|
|
|
2021-03-21 04:15:15 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
/* Sanity check. */
|
2021-03-21 03:36:59 +08:00
|
|
|
/* XXX Early versions of this code would check that the mapped
|
|
|
|
* region was with r-xp permission. It might have been true at
|
|
|
|
* some point in time, but last I tested, the searched pointer was
|
|
|
|
* in a r--p region for libgimpbase. Thus _br_find_exe_for_symbol()
|
|
|
|
* would fail to find the executable's path.
|
|
|
|
* So now we don't test the region's permission anymore.
|
|
|
|
*/
|
2021-03-21 04:15:15 +08:00
|
|
|
if (strchr (maps_line, '/') == NULL)
|
|
|
|
{
|
|
|
|
g_free (maps_line);
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
/* Parse line. */
|
2021-03-21 04:15:15 +08:00
|
|
|
start_addr = maps_line;
|
|
|
|
end_addr = strchr (maps_line, '-');
|
|
|
|
path = strchr (maps_line, '/');
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
/* More sanity check. */
|
2021-03-21 04:15:15 +08:00
|
|
|
if (!(path > end_addr && end_addr != NULL && end_addr[0] == '-'))
|
|
|
|
{
|
|
|
|
g_free (maps_line);
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
end_addr[0] = '\0';
|
|
|
|
end_addr++;
|
|
|
|
end_addr_end = strchr (end_addr, ' ');
|
|
|
|
if (end_addr_end == NULL)
|
2021-03-21 04:15:15 +08:00
|
|
|
{
|
|
|
|
g_free (maps_line);
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
end_addr_end[0] = '\0';
|
2021-03-21 04:15:15 +08:00
|
|
|
len = strlen (path);
|
2019-02-05 22:00:44 +08:00
|
|
|
if (len == 0)
|
2021-03-21 04:15:15 +08:00
|
|
|
{
|
|
|
|
g_free (maps_line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (path[len - 1] == '\n')
|
|
|
|
path[len - 1] = '\0';
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
/* Get rid of "(deleted)" from the filename. */
|
2021-03-21 04:15:15 +08:00
|
|
|
len = strlen (path);
|
|
|
|
if (len > 10 && strcmp (path + len - 10, " (deleted)") == 0)
|
|
|
|
path[len - 10] = '\0';
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
/* I don't know whether this can happen but better safe than sorry. */
|
|
|
|
len = strlen (start_addr);
|
|
|
|
if (len != strlen (end_addr))
|
2021-03-21 04:15:15 +08:00
|
|
|
{
|
|
|
|
g_free (maps_line);
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2021-03-21 04:15:15 +08:00
|
|
|
found = g_strdup (path);
|
|
|
|
g_free (maps_line);
|
2019-02-05 22:00:44 +08:00
|
|
|
break;
|
2005-12-14 21:36:12 +08:00
|
|
|
}
|
2021-03-21 04:15:15 +08:00
|
|
|
|
|
|
|
g_free (maps_line);
|
2019-02-05 22:00:44 +08:00
|
|
|
}
|
2005-12-14 21:36:12 +08:00
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
g_free (address_string);
|
2021-03-21 04:15:15 +08:00
|
|
|
g_object_unref (data_input);
|
2005-12-14 21:36:12 +08:00
|
|
|
|
2021-03-21 04:15:15 +08:00
|
|
|
return found;
|
2019-02-05 21:21:23 +08:00
|
|
|
#endif /* ! ENABLE_RELOCATABLE_RESOURCES || G_OS_WIN32 */
|
2005-11-06 09:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gchar *exe = NULL;
|
|
|
|
|
|
|
|
static void set_gerror (GError **error, GimpBinrelocInitError errcode);
|
|
|
|
|
|
|
|
|
2005-12-14 21:36:12 +08:00
|
|
|
/* Initialize the BinReloc library (for applications).
|
2005-11-06 09:06:41 +08:00
|
|
|
*
|
|
|
|
* This function must be called before using any other BinReloc functions.
|
|
|
|
* It attempts to locate the application's canonical filename.
|
|
|
|
*
|
|
|
|
* @note If you want to use BinReloc for a library, then you should call
|
|
|
|
* _gimp_reloc_init_lib() instead.
|
|
|
|
* @note Initialization failure is not fatal. BinReloc functions will just
|
|
|
|
* fallback to the supplied default path.
|
|
|
|
*
|
|
|
|
* @param error If BinReloc failed to initialize, then the error report will
|
|
|
|
* be stored in this variable. Set to NULL if you don't want an
|
|
|
|
* error report. See the #GimpBinrelocInitError for a list of error
|
|
|
|
* codes.
|
|
|
|
*
|
|
|
|
* @returns TRUE on success, FALSE if BinReloc failed to initialize.
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
_gimp_reloc_init (GError **error)
|
|
|
|
{
|
2019-02-05 22:00:44 +08:00
|
|
|
GimpBinrelocInitError errcode;
|
|
|
|
|
|
|
|
/* Shut up compiler warning about uninitialized variable. */
|
|
|
|
errcode = GIMP_RELOC_INIT_ERROR_NOMEM;
|
|
|
|
|
|
|
|
/* Locate the application's filename. */
|
|
|
|
exe = _br_find_exe (&errcode);
|
|
|
|
if (exe != NULL)
|
|
|
|
/* Success! */
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Failed :-( */
|
|
|
|
set_gerror (error, errcode);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-11-06 09:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-14 21:36:12 +08:00
|
|
|
/* Initialize the BinReloc library (for libraries).
|
2005-11-06 09:06:41 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* @note Initialization failure is not fatal. BinReloc functions will just
|
|
|
|
* fallback to the supplied default path.
|
|
|
|
*
|
|
|
|
* @returns TRUE on success, FALSE if a filename cannot be found.
|
|
|
|
*/
|
2005-11-16 20:03:36 +08:00
|
|
|
gboolean
|
2005-11-06 09:06:41 +08:00
|
|
|
_gimp_reloc_init_lib (GError **error)
|
|
|
|
{
|
2019-02-05 22:00:44 +08:00
|
|
|
GimpBinrelocInitError errcode;
|
|
|
|
|
|
|
|
/* Shut up compiler warning about uninitialized variable. */
|
|
|
|
errcode = GIMP_RELOC_INIT_ERROR_NOMEM;
|
|
|
|
|
|
|
|
exe = _br_find_exe_for_symbol ((const void *) "", &errcode);
|
|
|
|
if (exe != NULL)
|
2021-03-21 04:15:15 +08:00
|
|
|
{
|
|
|
|
/* Success! */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-02-05 22:00:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Failed :-( */
|
|
|
|
set_gerror (error, errcode);
|
|
|
|
return exe != NULL;
|
|
|
|
}
|
2005-11-06 09:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_gerror (GError **error, GimpBinrelocInitError errcode)
|
|
|
|
{
|
2019-02-05 22:00:44 +08:00
|
|
|
const gchar *error_message;
|
|
|
|
|
|
|
|
if (error == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case GIMP_RELOC_INIT_ERROR_NOMEM:
|
|
|
|
error_message = "Cannot allocate memory.";
|
|
|
|
break;
|
|
|
|
case GIMP_RELOC_INIT_ERROR_OPEN_MAPS:
|
|
|
|
error_message = "Unable to open /proc/self/maps for reading.";
|
|
|
|
break;
|
|
|
|
case GIMP_RELOC_INIT_ERROR_READ_MAPS:
|
|
|
|
error_message = "Unable to read from /proc/self/maps.";
|
|
|
|
break;
|
|
|
|
case GIMP_RELOC_INIT_ERROR_INVALID_MAPS:
|
|
|
|
error_message = "The file format of /proc/self/maps is invalid.";
|
|
|
|
break;
|
|
|
|
case GIMP_RELOC_INIT_ERROR_DISABLED:
|
|
|
|
error_message = "Binary relocation support is disabled.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_message = "Unknown error.";
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
g_set_error (error, g_quark_from_static_string ("GBinReloc"),
|
|
|
|
errcode, "%s", error_message);
|
2005-11-06 09:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-14 21:36:12 +08:00
|
|
|
/* Locate the prefix in which the current application is installed.
|
2005-11-06 09:06:41 +08:00
|
|
|
*
|
|
|
|
* The prefix is generated by the following pseudo-code evaluation:
|
|
|
|
* \code
|
|
|
|
* dirname(dirname(exename))
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* @param default_prefix A default prefix which will used as fallback.
|
|
|
|
* @return A string containing the prefix, which must be freed when no
|
|
|
|
* longer necessary. If BinReloc is not initialized, or if the
|
|
|
|
* initialization function failed, then a copy of default_prefix
|
|
|
|
* will be returned. If default_prefix is NULL, then NULL will be
|
|
|
|
* returned.
|
|
|
|
*/
|
2005-11-16 20:03:36 +08:00
|
|
|
gchar *
|
2005-11-06 09:06:41 +08:00
|
|
|
_gimp_reloc_find_prefix (const gchar *default_prefix)
|
|
|
|
{
|
2019-02-05 22:00:44 +08:00
|
|
|
gchar *dir1, *dir2;
|
2024-04-05 01:49:30 +08:00
|
|
|
gchar *exe_dir;
|
2019-02-05 22:00:44 +08:00
|
|
|
|
|
|
|
if (exe == NULL)
|
|
|
|
{
|
|
|
|
/* BinReloc not initialized. */
|
|
|
|
if (default_prefix != NULL)
|
|
|
|
return g_strdup (default_prefix);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-12-14 21:36:12 +08:00
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
dir1 = g_path_get_dirname (exe);
|
|
|
|
dir2 = g_path_get_dirname (dir1);
|
2024-04-05 01:49:30 +08:00
|
|
|
|
|
|
|
exe_dir = g_path_get_basename (dir1);
|
|
|
|
if (g_strcmp0 (exe_dir, "bin") != 0 && ! g_str_has_prefix (exe_dir, "lib"))
|
|
|
|
{
|
|
|
|
g_free (exe_dir);
|
|
|
|
exe_dir = g_path_get_basename (dir2);
|
|
|
|
if (g_str_has_prefix (exe_dir, "lib"))
|
|
|
|
{
|
|
|
|
/* Supporting multiarch folders, such as lib/x86_64-linux-gnu/ */
|
|
|
|
gchar *dir3 = g_path_get_dirname (dir2);
|
|
|
|
|
|
|
|
g_free (dir2);
|
|
|
|
dir2 = dir3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
g_free (dir1);
|
2024-04-05 01:49:30 +08:00
|
|
|
g_free (exe_dir);
|
|
|
|
|
2019-02-05 22:00:44 +08:00
|
|
|
return dir2;
|
2005-11-06 09:06:41 +08:00
|
|
|
}
|