summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillem Jover <guillem@hadrons.org>2013-05-29 02:23:56 +0200
committerGuillem Jover <guillem@hadrons.org>2013-06-08 18:09:29 +0200
commit6faea4d2a0eb7782187dd6d92749a0813e7405bd (patch)
treebf875ace464f8ec480290184d2d7a52211b25379
parent367e036537109f8955221123af0569329868edf1 (diff)
Force setproctitle() into .init_array section
The GNU .init_array support is an extension over the standard System V ABI .init_array support, which passes the main() arguments to the init function. This support comes in three parts. First the dynamic linker (from glibc) needs to support it. Then function pointers need to be placed in the section, for example by using __attribute__((constructor)), that the compiler (gcc or clang for example) might place in section .ctors and the linker (from binutils) will move to .init_array on the output object, or by placing them directly into .init_array by the compiler when compiling. If this does not happen and the function pointers end up in .ctors, then they will not get passed the main() arguments, which we do really need in this case. But this relies on recent binutils or gcc having native .init_array support, and not having it disabled through --disable-initfini-array. To guarantee we get the correct behaviour, let's just place the function pointer in the .init_array section directly, so we only require a recent enough glibc. Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=65029
-rw-r--r--configure.ac45
-rw-r--r--src/setproctitle.c12
2 files changed, 55 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index bdadc6e..e63ebfe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -50,12 +50,57 @@ AC_CHECK_DECL([F_CLOSEM],
[AC_DEFINE([HAVE_FCNTL_CLOSEM], [1],
[Define to 1 if you have fcntl(F_CLOSEM)])],
[],
[#include <limits.h>
#include <fcntl.h>])
+AC_CACHE_CHECK(
+ [for GNU .init_array section support],
+ [libbsd_cv_gnu_init_array_support],
+ [AC_RUN_IFELSE(
+ [AC_LANG_SOURCE(
+[[
+static int rc = 1;
+static void init(int argc) { if (argc == 1) rc = 0; }
+void (*init_func)(int argc) __attribute__((section(".init_array"))) = init;
+int main() { return rc; }
+]]
+ )],
+ [libbsd_cv_gnu_init_array_support=yes],
+ [libbsd_cv_gnu_init_array_support=no],
+ [AC_PREPROC_IFELSE(
+ [AC_LANG_SOURCE(
+[[
+/* Look for a known libc that supports .init_array with the GNU extension
+ * to pass main() arguments to the init functions. */
+#include <stdlib.h>
+#if defined __GLIBC_PREREQ
+# if __GLIBC_PREREQ(2, 4)
+/* glibc supports GNU .init_array since 2.4. */
+# else
+# error glibc does not support GNU .init_array
+# endif
+#else
+/*
+ * Basic SysV ABI .init_array support, init functions do not get arguments:
+ * - Bionic since its inception.
+ * - uClibc since 0.9.29.
+ */
+# error unknown whether libc supports GNU .init_array
+#endif
+]]
+ )],
+ [libbsd_cv_gnu_init_array_support=yes],
+ [libbsd_cv_gnu_init_array_support=no])
+ ]
+ )]
+)
+if test "$libbsd_cv_gnu_init_array_support" = no; then
+ AC_MSG_ERROR([missing required GNU .init_array section support])
+fi
+
# Checks for library functions.
AC_MSG_CHECKING([for program_invocation_short_name])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[#include <errno.h>]],
[[const char *p = program_invocation_short_name;]])],
[AC_DEFINE([HAVE_PROGRAM_INVOCATION_SHORT_NAME], [1],
diff --git a/src/setproctitle.c b/src/setproctitle.c
index 60b6484..2137190 100644
--- a/src/setproctitle.c
+++ b/src/setproctitle.c
@@ -1,9 +1,9 @@
/*
* Copyright © 2010 William Ahern
- * Copyright © 2012 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2012-2013 Guillem Jover <guillem@hadrons.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
@@ -126,13 +126,13 @@ spt_copyargs(int argc, char *argv[])
argv[i] = tmp;
}
return 0;
}
-static void __attribute__((constructor))
+static void
spt_init(int argc, char *argv[], char *envp[])
{
char *base, *end, *nul, *tmp;
int i, error;
base = argv[0];
@@ -183,12 +183,20 @@ spt_init(int argc, char *argv[], char *envp[])
SPT.nul = nul;
SPT.base = base;
SPT.end = end;
}
+/*
+ * Force spt_init() function into the .init_array section instead of expecting
+ * either the compiler to place constructors there or the linker to move them
+ * from .ctors to .init_array.
+ */
+void (*spt_init_func)(int argc, char *argv[], char *envp[])
+ __attribute__((section(".init_array"))) = spt_init;
+
#ifndef SPT_MAXTITLE
#define SPT_MAXTITLE 255
#endif
void
setproctitle_impl(const char *fmt, ...)