git » libfiu » commit 7687302

preload: Do not use constructor priorities when they're not supported

author Alberto Bertogli
2009-07-02 22:58:07 UTC
committer Alberto Bertogli
2009-07-02 22:58:07 UTC
parent a9ffe90f61a87d4c244fb8e1296831cbfc417a5f

preload: Do not use constructor priorities when they're not supported

Constructor priorities are supported only from gcc 4.3 on, and only on some
platforms.

Since we don't rely on them, this patch avoids using them when building on
an unsupported system.

Note that clang defines __GNUC__, so when it gets constructor priorities
support, it should work fine without needing a special case.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

preload/posix/codegen.c +8 -2
preload/posix/codegen.h +12 -1
preload/posix/modules/libc.mm.custom.c +1 -1
preload/posix/modules/posix.custom.c +1 -1
preload/run/run.c +1 -3

diff --git a/preload/posix/codegen.c b/preload/posix/codegen.c
index e783861..0f078e8 100644
--- a/preload/posix/codegen.c
+++ b/preload/posix/codegen.c
@@ -11,7 +11,13 @@ void *_fiu_libc;
 /* Recursion counter, per-thread */
 int __thread _fiu_called;
 
-static void __attribute__((constructor(200))) _fiu_init(void)
+/* Let the user know if there is no constructor priorities support, just in
+ * case there are bugs when building/running without them */
+#ifdef NO_CONSTRUCTOR_PRIORITIES
+#warning "Building without using constructor priorities"
+#endif
+
+static void constructor_attr(200) _fiu_init(void)
 {
 	_fiu_called = 0;
 
@@ -25,7 +31,7 @@ static void __attribute__((constructor(200))) _fiu_init(void)
 }
 
 /* this runs after all function-specific constructors */
-static void __attribute__((constructor(250))) _fiu_init_final(void)
+static void constructor_attr(250) _fiu_init_final(void)
 {
 	struct timeval tv;
 
diff --git a/preload/posix/codegen.h b/preload/posix/codegen.h
index bcae5d9..04d00ee 100644
--- a/preload/posix/codegen.h
+++ b/preload/posix/codegen.h
@@ -12,6 +12,17 @@ extern void *_fiu_libc;
 /* Recursion counter, per-thread */
 extern int __thread _fiu_called;
 
+/* GCC >= 4.3 supports constructor priorities only on some platforms. Since we
+ * don't rely on them, but use them for clarity purposes, use a macro so
+ * libfiu builds on systems where they're not supported. */
+#if (defined __linux__) && (defined __GNUC__) \
+	&& __GNUC__ >= 4 && __GNUC_MINOR__ >= 3
+  #define constructor_attr(prio) __attribute__((constructor(prio)))
+#else
+  #define NO_CONSTRUCTOR_PRIORITIES 1
+  #define constructor_attr(prio) __attribute__((constructor))
+#endif
+
 /* Useful macros for recursion and debugging */
 #if 1
 	#define rec_inc() do { _fiu_called++; } while(0)
@@ -59,7 +70,7 @@ extern int __thread _fiu_called;
 #define mkwrap_top(RTYPE, NAME, PARAMS, PARAMSN, PARAMST)	\
 	static RTYPE (*_fiu_orig_##NAME) PARAMS = NULL;		\
 								\
-	static void __attribute__((constructor(201))) _fiu_init_##NAME(void) \
+	static void constructor_attr(201) _fiu_init_##NAME(void) \
 	{							\
 		rec_inc();					\
 		_fiu_orig_##NAME = (RTYPE (*) PARAMST)		\
diff --git a/preload/posix/modules/libc.mm.custom.c b/preload/posix/modules/libc.mm.custom.c
index 8e0210b..9878ea3 100644
--- a/preload/posix/modules/libc.mm.custom.c
+++ b/preload/posix/modules/libc.mm.custom.c
@@ -34,7 +34,7 @@ static void *fiu_realloc_hook(void *ptr, size_t size, const void *caller);
  * will run after them just to make things tidier (NOT because it is
  * necessary). */
 
-static void __attribute__((constructor(202))) fiu_init_malloc(void)
+static void constructor_attr(202) fiu_init_malloc(void)
 {
 	/* Save original hooks, used in ours to prevent unwanted recursion */
 	old_malloc_hook = __malloc_hook;
diff --git a/preload/posix/modules/posix.custom.c b/preload/posix/modules/posix.custom.c
index e85adee..55329fa 100644
--- a/preload/posix/modules/posix.custom.c
+++ b/preload/posix/modules/posix.custom.c
@@ -18,7 +18,7 @@
  * of arguments */
 static int (*_fiu_orig_open) (const char *pathname, int flags, ...) = NULL;
 
-static void __attribute__((constructor(201))) _fiu_init_open(void)
+static void constructor_attr(201) _fiu_init_open(void)
 {
 	rec_inc();
 	_fiu_orig_open = (int (*) (const char *, int, ...))
diff --git a/preload/run/run.c b/preload/run/run.c
index cf0dfa6..19ec8cc 100644
--- a/preload/run/run.c
+++ b/preload/run/run.c
@@ -71,9 +71,7 @@ static void parse_enable(const char *s, struct enable_option *enopt)
 	enopt->failinfo = atol(tok);
 }
 
-/* We set the constructor with priority 300 so it runs after the other
- * libfiu preloaders (if they're enabled), which use the 200 range */
-static void __attribute__((constructor(300))) fiu_run_init(void)
+static void __attribute__((constructor)) fiu_run_init(void)
 {
 	int r;
 	struct enable_option enopt;