Added support for binary relocation by means of binreloc, largely based on

2005-11-06  Sven Neumann  <sven@gimp.org>

	Added support for binary relocation by means of binreloc, largely
	based on a patch by Hongli Lai:

	* m4macros/Makefile.am
	* m4macros/binreloc.m4: new file providing a macro to check for
	binreloc support.

	* acinclude.m4
	* configure.in: use the macro.

	* libgimpbase/Makefile.am
	* libgimpbase/gimpreloc.[ch]: new files providing binreloc support
	on Linux.

	* libgimpbase/gimpenv.[ch]: use binreloc, provide a function to
	initialize the environment machinery.

	* libgimpbase/gimpbase.def: updated.

	* app/Makefile.am: fiddle with the LDFLAGS for binreloc.

	* app/main.c (main): gimp_env_init(FALSE).

	* libgimp/gimp.c (gimp_main): gimp_env_init(TRUE).
This commit is contained in:
Sven Neumann 2005-11-06 01:06:41 +00:00 committed by Sven Neumann
parent 791fda957a
commit 39e866d9c0
16 changed files with 994 additions and 10 deletions

View File

@ -1,3 +1,30 @@
2005-11-06 Sven Neumann <sven@gimp.org>
Added support for binary relocation by means of binreloc, largely
based on a patch by Hongli Lai:
* m4macros/Makefile.am
* m4macros/binreloc.m4: new file providing a macro to check for
binreloc support.
* acinclude.m4
* configure.in: use the macro.
* libgimpbase/Makefile.am
* libgimpbase/gimpreloc.[ch]: new files providing binreloc support
on Linux.
* libgimpbase/gimpenv.[ch]: use binreloc, provide a function to
initialize the environment machinery.
* libgimpbase/gimpbase.def: updated.
* app/Makefile.am: fiddle with the LDFLAGS for binreloc.
* app/main.c (main): gimp_env_init(FALSE).
* libgimp/gimp.c (gimp_main): gimp_env_init(TRUE).
2005-11-06 Sven Neumann <sven@gimp.org>
* modules/controller_midi.c (midi_set_device): corrected casting.

View File

@ -3,3 +3,4 @@ m4_include([m4macros/pythondev.m4])
m4_include([m4macros/gimpprint.m4])
m4_include([m4macros/alsa.m4])
m4_include([m4macros/gtk-doc.m4])
m4_include([m4macros/binreloc.m4])

View File

@ -70,6 +70,9 @@ endif
if OS_WIN32
mwindows = -mwindows
endif
if OS_UNIX
munix = -Wl,-rpath '-Wl,$$ORIGIN/../lib'
endif
if HAVE_WINDRES
GIMPICONRC = gimprc.o
@ -86,7 +89,7 @@ INCLUDES = \
$(PANGOFT2_CFLAGS) \
-I$(includedir)
gimp_2_3_LDFLAGS = $(mwindows)
gimp_2_3_LDFLAGS = $(mwindows) $(munix)
gimp_2_3_LDADD = \
gui/libappgui.a \

View File

@ -243,6 +243,8 @@ main (int argc,
gimp_init_malloc ();
gimp_env_init (FALSE);
gimp_init_i18n ();
g_set_application_name (_("GNU Image Manipulation Program"));

View File

@ -369,6 +369,8 @@ AC_FUNC_ALLOCA
dnl check some more funcs
AC_CHECK_FUNCS(difftime putenv mmap)
AM_BINRELOC
# _NL_MEASUREMENT_MEASUREMENT is an enum and not a define
AC_MSG_CHECKING([for _NL_MEASUREMENT_MEASUREMENT])

View File

@ -1,3 +1,7 @@
2005-11-06 Sven Neumann <sven@gimp.org>
* devel-docs/libgimpbase/Makefile.am: ignore gimpreloc.h.
2005-11-04 Sven Neumann <sven@gimp.org>
* libgimpbase/libgimpbase-sections.txt: added GimpGridStyle.

View File

@ -29,6 +29,7 @@ CFILE_GLOB = $(DOC_SOURCE_DIR)/*.c
IGNORE_HFILES = \
gimpbase.h \
gimpbase-private.h \
gimpreloc.h \
gimpwin32-io.h
# Images to copy into HTML directory

View File

@ -286,6 +286,8 @@ gimp_main (const GimpPlugInInfo *info,
return 1;
}
gimp_env_init (TRUE);
progname = argv[0];
basename = g_path_get_basename (progname);

View File

@ -46,7 +46,12 @@ AM_CPPFLAGS = \
-DLOCALEDIR=\""$(gimplocaledir)"\" \
-DPLUGINDIR=\""$(gimpplugindir)"\" \
-DSYSCONFDIR=\""$(gimpsysconfdir)"\" \
-DGIMP_PACKAGE=\""@PACKAGE@"\" \
-DGIMP_DATA_VERSION=\"$(GIMP_DATA_VERSION)\" \
-DGIMP_SYSCONF_VERSION=\"$(GIMP_SYSCONF_VERSION)\" \
-DGIMP_PLUGIN_VERSION=\"$(GIMP_PLUGIN_VERSION)\" \
-DG_LOG_DOMAIN=\"LibGimpBase\" \
@BINRELOC_CFLAGS@ \
@GTHREAD_CFLAGS@
INCLUDES = \
@ -90,6 +95,8 @@ libgimpbase_sources = \
gimpparasiteio.h \
gimpprotocol.c \
gimpprotocol.h \
gimpreloc.c \
gimpreloc.h \
gimpsignal.c \
gimpsignal.h \
gimpunit.c \

View File

@ -24,6 +24,7 @@ EXPORTS
gimp_enum_set_value_descriptions
gimp_enum_value_get_desc
gimp_enum_value_get_help
gimp_env_init
gimp_escape_uline
gimp_filename_to_utf8
gimp_flags_get_first_desc

View File

@ -34,6 +34,7 @@
#include "gimpenv.h"
#include "gimpversion.h"
#include "gimpreloc.h"
#ifdef G_OS_WIN32
@ -62,6 +63,56 @@ static gchar * gimp_env_get_dir (const gchar *gimp_env_name,
const gchar *env_dir);
/**
* gimp_env_init:
* @plug_in: must be %TRUE if this function is called from a plug-in
*
* You don't need to care about this function. It is being called for
* you automatically (by means of the MAIN() macro that every plug-in
* runs). Calling it again will cause a fatal error.
*
* Since: GIMP 2.4
*/
void
gimp_env_init (gboolean plug_in)
{
static gboolean gimp_env_initialized = FALSE;
if (gimp_env_initialized)
g_error ("gimp_env_init() must only be called once!");
gimp_env_initialized = TRUE;
#ifndef G_OS_WIN32
if (plug_in)
{
_gimp_reloc_init_lib (NULL);
}
else if (_gimp_reloc_init (NULL))
{
/* Set $LD_LIBRARY_PATH to ensure that plugins can be loaded. */
const gchar *ldpath = g_getenv ("LD_LIBRARY_PATH");
gchar *libdir = _gimp_reloc_find_lib_dir (NULL);
if (ldpath && *ldpath)
{
gchar *tmp = g_strconcat (libdir, ":", ldpath, NULL);
g_setenv ("LD_LIBRARY_PATH", tmp, TRUE);
g_free (tmp);
}
else
{
g_setenv ("LD_LIBRARY_PATH", libdir, TRUE);
}
g_free (libdir);
}
#endif
}
/**
* gimp_directory:
*
@ -182,10 +233,10 @@ gimp_personal_rc_file (const gchar *basename)
return g_build_filename (gimp_directory (), basename, NULL);
}
#ifdef G_OS_WIN32
gchar *
gimp_toplevel_directory (void)
{
#ifdef G_OS_WIN32
/* Figure it out from the executable name */
static gchar *toplevel = NULL;
@ -217,7 +268,7 @@ gimp_toplevel_directory (void)
if (filename == NULL)
g_error ("Converting module filename to UTF-8 failed");
}
/* If the executable file name is of the format
* <foobar>\bin\*.exe or
* <foobar>\lib\gimp\GIMP_API_VERSION\plug-ins\*.exe, use <foobar>.
@ -253,8 +304,11 @@ gimp_toplevel_directory (void)
toplevel = filename;
return toplevel;
}
#else
return _gimp_reloc_find_prefix (PREFIX);
#endif
}
/**
* gimp_data_directory:
@ -277,7 +331,12 @@ gimp_data_directory (void)
static gchar *gimp_data_dir = NULL;
if (! gimp_data_dir)
gimp_data_dir = gimp_env_get_dir ("GIMP2_DATADIR", DATADIR);
{
gchar *tmp = _gimp_reloc_find_data_dir (DATADIR);
gimp_data_dir = gimp_env_get_dir ("GIMP2_DATADIR", tmp);
g_free (tmp);
}
return gimp_data_dir;
}
@ -303,7 +362,12 @@ gimp_locale_directory (void)
static gchar *gimp_locale_dir = NULL;
if (! gimp_locale_dir)
gimp_locale_dir = gimp_env_get_dir ("GIMP2_LOCALEDIR", LOCALEDIR);
{
gchar *tmp = _gimp_reloc_find_locale_dir (LOCALEDIR);
gimp_locale_dir = gimp_env_get_dir ("GIMP2_LOCALEDIR", tmp);
g_free (tmp);
}
return gimp_locale_dir;
}
@ -329,7 +393,12 @@ gimp_sysconf_directory (void)
static gchar *gimp_sysconf_dir = NULL;
if (! gimp_sysconf_dir)
gimp_sysconf_dir = gimp_env_get_dir ("GIMP2_SYSCONFDIR", SYSCONFDIR);
{
gchar *tmp = _gimp_reloc_find_etc_dir (SYSCONFDIR);
gimp_sysconf_dir = gimp_env_get_dir ("GIMP2_SYSCONFDIR", tmp);
g_free (tmp);
}
return gimp_sysconf_dir;
}
@ -355,7 +424,12 @@ gimp_plug_in_directory (void)
static gchar *gimp_plug_in_dir = NULL;
if (! gimp_plug_in_dir)
gimp_plug_in_dir = gimp_env_get_dir ("GIMP2_PLUGINDIR", PLUGINDIR);
{
gchar *tmp = _gimp_reloc_find_plugin_dir (PLUGINDIR);
gimp_plug_in_dir = gimp_env_get_dir ("GIMP2_PLUGINDIR", tmp);
g_free (tmp);
}
return gimp_plug_in_dir;
}
@ -397,7 +471,9 @@ gimp_gtkrc (void)
* and *@path is replaced with a pointer to a new string with the
* run-time prefix spliced in.
*
* On Unix, does nothing.
* On Linux, it does the same thing, but only if BinReloc support is enabled.
* On other Unices, it does nothing because those platforms don't have a
* way to find out where our binary is.
*/
static void
gimp_path_runtime_fix (gchar **path)
@ -435,6 +511,20 @@ gimp_path_runtime_fix (gchar **path)
*path, NULL);
g_free (p);
}
#else
gchar *p;
if (strncmp (*path, PREFIX G_DIR_SEPARATOR_S, strlen (PREFIX G_DIR_SEPARATOR_S)) == 0)
{
/* This is a compile-time entry. Replace the path with the
* real one on this machine.
*/
p = *path;
*path = g_build_filename (gimp_toplevel_directory (),
*path + strlen (PREFIX G_DIR_SEPARATOR_S),
NULL);
g_free (p);
}
#endif
}

View File

@ -47,6 +47,10 @@ void gimp_path_free (GList *path);
gchar * gimp_path_get_user_writable_dir (GList *path);
/* should be considered private, don't use! */
void gimp_env_init (gboolean library);
G_END_DECLS
#endif /* __GIMPENV_H__ */

706
libgimpbase/gimpreloc.c Normal file
View File

@ -0,0 +1,706 @@
/*
* 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.
*/
#ifndef _GIMPRELOC_C_
#define _GIMPRELOC_C_
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#ifdef ENABLE_BINRELOC
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif /* ENABLE_BINRELOC */
#include <glib.h>
#include "gimpreloc.h"
G_BEGIN_DECLS
/** @internal
* Find the canonical filename of the executable. Returns the filename
* (which must be freed) or NULL on error. If the parameter 'error' is
* not NULL, the error code will be stored there, if an error occured.
*/
static char *
_br_find_exe (GimpBinrelocInitError *error)
{
#ifndef ENABLE_BINRELOC
if (error)
*error = GIMP_RELOC_INIT_ERROR_DISABLED;
return NULL;
#else
char *path, *path2, *line, *result;
size_t buf_size;
ssize_t size;
struct stat stat_buf;
FILE *f;
/* Read from /proc/self/exe (symlink) */
if (sizeof (path) > SSIZE_MAX)
buf_size = SSIZE_MAX - 1;
else
buf_size = PATH_MAX - 1;
path = (char *) g_try_malloc (buf_size);
if (path == NULL) {
/* Cannot allocate memory. */
if (error)
*error = GIMP_RELOC_INIT_ERROR_NOMEM;
return NULL;
}
path2 = (char *) g_try_malloc (buf_size);
if (path2 == NULL) {
/* Cannot allocate memory. */
if (error)
*error = GIMP_RELOC_INIT_ERROR_NOMEM;
g_free (path);
return NULL;
}
strncpy (path2, "/proc/self/exe", buf_size - 1);
while (1) {
int i;
size = readlink (path2, path, buf_size - 1);
if (size == -1) {
/* Error. */
g_free (path2);
break;
}
/* readlink() success. */
path[size] = '\0';
/* 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. */
g_free (path2);
break;
}
/* stat() success. */
if (!S_ISLNK (stat_buf.st_mode)) {
/* path is not a symlink. Done. */
g_free (path2);
return path;
}
/* path is a symlink. Continue loop and resolve this. */
strncpy (path, path2, buf_size - 1);
}
/* readlink() or stat() failed; this can happen when the program is
* running in Valgrind 2.2. Read from /proc/self/maps as fallback. */
buf_size = PATH_MAX + 128;
line = (char *) g_try_realloc (path, buf_size);
if (line == NULL) {
/* Cannot allocate memory. */
g_free (path);
if (error)
*error = GIMP_RELOC_INIT_ERROR_NOMEM;
return NULL;
}
f = fopen ("/proc/self/maps", "r");
if (f == NULL) {
g_free (line);
if (error)
*error = GIMP_RELOC_INIT_ERROR_OPEN_MAPS;
return NULL;
}
/* The first entry should be the executable name. */
result = fgets (line, (int) buf_size, f);
if (result == NULL) {
fclose (f);
g_free (line);
if (error)
*error = GIMP_RELOC_INIT_ERROR_READ_MAPS;
return NULL;
}
/* Get rid of newline character. */
buf_size = strlen (line);
if (buf_size <= 0) {
/* Huh? An empty string? */
fclose (f);
g_free (line);
if (error)
*error = GIMP_RELOC_INIT_ERROR_INVALID_MAPS;
return NULL;
}
if (line[buf_size - 1] == 10)
line[buf_size - 1] = 0;
/* Extract the filename; it is always an absolute path. */
path = strchr (line, '/');
/* Sanity check. */
if (strstr (line, " r-xp ") == NULL || path == NULL) {
fclose (f);
g_free (line);
if (error)
*error = GIMP_RELOC_INIT_ERROR_INVALID_MAPS;
return NULL;
}
path = g_strdup (path);
g_free (line);
fclose (f);
return path;
#endif /* ENABLE_BINRELOC */
}
/** @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, GimpBinrelocInitError *error)
{
#ifndef ENABLE_BINRELOC
if (error)
*error = GIMP_RELOC_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 /* ENABLE_BINRELOC */
}
static gchar *exe = NULL;
static void set_gerror (GError **error, GimpBinrelocInitError errcode);
/** Initialize the BinReloc library (for applications).
*
* 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)
{
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;
}
}
/** 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.
* @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.
*/
G_GNUC_INTERNAL gboolean
_gimp_reloc_init_lib (GError **error)
{
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)
/* Success! */
return TRUE;
else {
/* Failed :-( */
set_gerror (error, errcode);
return exe != NULL;
}
}
static void
set_gerror (GError **error, GimpBinrelocInitError errcode)
{
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);
}
/** Find the canonical filename of the current application.
*
* @param default_exe A default filename which will be used as fallback.
* @returns A string containing the application's canonical filename,
* which must be freed when no longer necessary. If BinReloc is
* not initialized, or if the initialization function failed,
* then a copy of default_exe will be returned. If default_exe
* is NULL, then NULL will be returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_exe (const gchar *default_exe)
{
if (exe == NULL) {
/* BinReloc is not initialized. */
if (default_exe != NULL)
return g_strdup (default_exe);
else
return NULL;
}
return g_strdup (exe);
}
/** Locate the directory in which the current application is installed.
*
* The prefix is generated by the following pseudo-code evaluation:
* \code
* dirname(exename)
* \endcode
*
* @param default_dir A default directory which will used as fallback.
* @return A string containing the directory, which must be freed when no
* longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_dir
* will be returned. If default_dir is NULL, then NULL will be
* returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_exe_dir (const gchar *default_dir)
{
if (exe == NULL) {
/* BinReloc not initialized. */
if (default_dir != NULL)
return g_strdup (default_dir);
else
return NULL;
}
return g_path_get_dirname (exe);
}
/** Locate the prefix in which the current application is installed.
*
* 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.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_prefix (const gchar *default_prefix)
{
gchar *dir1, *dir2;
if (exe == NULL) {
/* BinReloc not initialized. */
if (default_prefix != NULL)
return g_strdup (default_prefix);
else
return NULL;
}
dir1 = g_path_get_dirname (exe);
dir2 = g_path_get_dirname (dir1);
g_free (dir1);
return dir2;
}
/** Locate the application's binary folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/bin"
* \endcode
*
* @param default_bin_dir A default path which will used as fallback.
* @return A string containing the bin folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_bin_dir will
* be returned. If default_bin_dir is NULL, then NULL will be returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_bin_dir (const gchar *default_bin_dir)
{
gchar *prefix, *dir;
prefix = _gimp_reloc_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_bin_dir != NULL)
return g_strdup (default_bin_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "bin", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's data folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/share/" + GIMP_PACKAGE + "/" + GIMP_DATA_VERSION
* \endcode
*
* @param default_data_dir A default path which will used as fallback.
* @return A string containing the data folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_data_dir
* will be returned. If default_data_dir is NULL, then NULL will be
* returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_data_dir (const gchar *default_data_dir)
{
gchar *prefix, *dir;
prefix = _gimp_reloc_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_data_dir != NULL)
return g_strdup (default_data_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "share",
GIMP_PACKAGE,
GIMP_DATA_VERSION,
NULL);
g_free (prefix);
return dir;
}
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_plugin_dir (const gchar *default_plugin_dir)
{
gchar *libdir, *dir;
libdir = _gimp_reloc_find_lib_dir (NULL);
if (libdir == NULL) {
/* BinReloc not initialized. */
if (default_plugin_dir != NULL)
return g_strdup (default_plugin_dir);
else
return NULL;
}
dir = g_build_filename (libdir,
GIMP_PACKAGE,
GIMP_PLUGIN_VERSION,
NULL);
g_free (libdir);
return dir;
}
/** Locate the application's localization folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/share/locale"
* \endcode
*
* @param default_locale_dir A default path which will used as fallback.
* @return A string containing the localization folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_locale_dir will be returned.
* If default_locale_dir is NULL, then NULL will be returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_locale_dir (const gchar *default_locale_dir)
{
gchar *data_dir, *dir;
data_dir = _gimp_reloc_find_data_dir (NULL);
if (data_dir == NULL) {
/* BinReloc not initialized. */
if (default_locale_dir != NULL)
return g_strdup (default_locale_dir);
else
return NULL;
}
dir = g_build_filename (data_dir, "locale", NULL);
g_free (data_dir);
return dir;
}
/** Locate the application's library folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/lib"
* \endcode
*
* @param default_lib_dir A default path which will used as fallback.
* @return A string containing the library folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the
* initialization function failed, then a copy of default_lib_dir will be returned.
* If default_lib_dir is NULL, then NULL will be returned.
*/
gchar *
_gimp_reloc_find_lib_dir (const gchar *default_lib_dir)
{
gchar *prefix, *dir;
prefix = _gimp_reloc_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_lib_dir != NULL)
return g_strdup (default_lib_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "lib", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's libexec folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/libexec"
* \endcode
*
* @param default_libexec_dir A default path which will used as fallback.
* @return A string containing the libexec folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the initialization
* function failed, then a copy of default_libexec_dir will be returned.
* If default_libexec_dir is NULL, then NULL will be returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_libexec_dir (const gchar *default_libexec_dir)
{
gchar *prefix, *dir;
prefix = _gimp_reloc_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_libexec_dir != NULL)
return g_strdup (default_libexec_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "libexec", NULL);
g_free (prefix);
return dir;
}
/** Locate the application's configuration files folder.
*
* The path is generated by the following pseudo-code evaluation:
* \code
* prefix + "/etc/" + GIMP_PACKAGE + "/" + GIMP_SYSCONF_VERSION
* \endcode
*
* @param default_etc_dir A default path which will used as fallback.
* @return A string containing the etc folder's path, which must be freed when
* no longer necessary. If BinReloc is not initialized, or if the initialization
* function failed, then a copy of default_etc_dir will be returned.
* If default_etc_dir is NULL, then NULL will be returned.
*/
G_GNUC_INTERNAL gchar *
_gimp_reloc_find_etc_dir (const gchar *default_etc_dir)
{
gchar *prefix, *dir;
prefix = _gimp_reloc_find_prefix (NULL);
if (prefix == NULL) {
/* BinReloc not initialized. */
if (default_etc_dir != NULL)
return g_strdup (default_etc_dir);
else
return NULL;
}
dir = g_build_filename (prefix, "etc",
GIMP_PACKAGE,
GIMP_SYSCONF_VERSION,
NULL);
g_free (prefix);
return dir;
}
G_END_DECLS
#endif /* _GIMPRELOC_C_ */

54
libgimpbase/gimpreloc.h Normal file
View File

@ -0,0 +1,54 @@
/*
* 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.
*/
#ifndef __GIMP_RELOC_H__
#define __GIMP_RELOC_H__
G_BEGIN_DECLS
/* These error codes can be returned from _gimp_reloc_init() or
* _gimp_reloc_init_lib().
*/
typedef enum {
/** Cannot allocate memory. */
GIMP_RELOC_INIT_ERROR_NOMEM,
/** Unable to open /proc/self/maps; see errno for details. */
GIMP_RELOC_INIT_ERROR_OPEN_MAPS,
/** Unable to read from /proc/self/maps; see errno for details. */
GIMP_RELOC_INIT_ERROR_READ_MAPS,
/** The file format of /proc/self/maps is invalid; kernel bug? */
GIMP_RELOC_INIT_ERROR_INVALID_MAPS,
/** BinReloc is disabled (the ENABLE_BINRELOC macro is not defined). */
GIMP_RELOC_INIT_ERROR_DISABLED
} GimpBinrelocInitError;
gboolean _gimp_reloc_init (GError **error) G_GNUC_INTERNAL;
gboolean _gimp_reloc_init_lib (GError **error) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_exe (const gchar *default_exe) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_exe_dir (const gchar *default_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_prefix (const gchar *default_prefix) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_bin_dir (const gchar *default_bin_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_data_dir (const gchar *default_data_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_plugin_dir (const gchar *default_plugin_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_locale_dir (const gchar *default_locale_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_lib_dir (const gchar *default_lib_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_libexec_dir (const gchar *default_libexec_dir) G_GNUC_INTERNAL;
gchar * _gimp_reloc_find_etc_dir (const gchar *default_etc_dir) G_GNUC_INTERNAL;
G_END_DECLS
#endif /* _GIMPRELOC_H_ */

View File

@ -2,7 +2,13 @@
installed_m4 = gimp-2.0.m4
uinstalled_m4 = alsa.m4 gimpprint.m4 gtk-doc.m4 pythondev.m4 detectcflags.m4
uinstalled_m4 = \
alsa.m4 \
binreloc.m4 \
detectcflags.m4 \
gimpprint.m4 \
gtk-doc.m4 \
pythondev.m4
EXTRA_DIST = $(installed_m4) $(uninstalled_m4)

74
m4macros/binreloc.m4 Normal file
View File

@ -0,0 +1,74 @@
# Check for binary relocation support.
# Written by Hongli Lai
# http://autopackage.org/
AC_DEFUN([AM_BINRELOC],
[
AC_ARG_ENABLE(binreloc,
[ --enable-binreloc compile with binary relocation support
(default=enable when available)],
enable_binreloc=$enableval,enable_binreloc=auto)
AC_ARG_ENABLE(binreloc-threads,
[ --enable-binreloc-threads compile binary relocation with threads support
(default=yes)],
enable_binreloc_threads=$enableval,enable_binreloc_threads=yes)
BINRELOC_CFLAGS=
BINRELOC_LIBS=
if test "x$enable_binreloc" = "xauto"; then
AC_CHECK_FILE([/proc/self/maps])
AC_CACHE_CHECK([whether everything is installed to the same prefix],
[br_cv_valid_prefixes], [
if test "$bindir" = '${exec_prefix}/bin' -a "$sbindir" = '${exec_prefix}/sbin' -a \
"$datadir" = '${prefix}/share' -a "$libdir" = '${exec_prefix}/lib' -a \
"$libexecdir" = '${exec_prefix}/libexec' -a "$sysconfdir" = '${prefix}/etc'
then
br_cv_valid_prefixes=yes
else
br_cv_valid_prefixes=no
fi
])
fi
AC_CACHE_CHECK([whether binary relocation support should be enabled],
[br_cv_binreloc],
[if test "x$enable_binreloc" = "xyes"; then
br_cv_binreloc=yes
elif test "x$enable_binreloc" = "xauto"; then
if test "x$br_cv_valid_prefixes" = "xyes" -a \
"x$ac_cv_file__proc_self_maps" = "xyes"; then
br_cv_binreloc=yes
else
br_cv_binreloc=no
fi
else
br_cv_binreloc=no
fi])
if test "x$br_cv_binreloc" = "xyes"; then
BINRELOC_CFLAGS="-DENABLE_BINRELOC"
if test "x$enable_binreloc_threads" = "xyes"; then
AC_CHECK_LIB([pthread], [pthread_getspecific])
fi
AC_CACHE_CHECK([whether binary relocation should use threads],
[br_cv_binreloc_threads],
[if test "x$enable_binreloc_threads" = "xyes"; then
if test "x$ac_cv_lib_pthread_pthread_getspecific" = "xyes"; then
br_cv_binreloc_threads=yes
else
br_cv_binreloc_threads=no
fi
else
br_cv_binreloc_threads=no
fi])
if test "x$br_cv_binreloc_threads" = "xyes"; then
BINRELOC_LIBS="-lpthread"
else
BINRELOC_CFLAGS="$BINRELOC_CFLAGS -DBR_PTHREADS=0"
fi
fi
AC_SUBST(BINRELOC_CFLAGS)
AC_SUBST(BINRELOC_LIBS)
])