From: Norbert Lange Subject: [PATCH 2/2] add a header to create flexible bootstrap code Date: Tue, 23 Oct 2018 17:16:47 +0200 Message-Id: <20181023151647.25508-3-norbert.lange@andritz.com> In-Reply-To: <20181023151647.25508-1-norbert.lange@andritz.com> References: <20181023151647.25508-1-norbert.lange@andritz.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Discussions about the Xenomai project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: xenomai@xenomai.org The previously existing bootstrap objects will be created by including said header. The functionality is configurable with macros, the minimum is an early constructor calling xenomai_init and a function to retrieve the modified commandline arguments. Further the functions and symbols can be created, that will be needed for the ld wrapping trick Signed-off-by: Norbert Lange --- include/xenomai/Makefile.am | 7 +- include/xenomai/bootstrap-template.h | 274 +++++++++++++++++++++++++++ include/xenomai/init.h | 2 + lib/boilerplate/init/Makefile.am | 3 +- lib/boilerplate/init/bootstrap-pic.c | 2 + lib/boilerplate/init/bootstrap.c | 189 +----------------- 6 files changed, 286 insertions(+), 191 deletions(-) create mode 100644 include/xenomai/bootstrap-template.h create mode 100644 lib/boilerplate/init/bootstrap-pic.c diff --git a/include/xenomai/Makefile.am b/include/xenomai/Makefile.am index f4d0c162d..c299c74ac 100644 --- a/include/xenomai/Makefile.am +++ b/include/xenomai/Makefile.am @@ -1,6 +1,7 @@ includesubdir = $(includedir)/xenomai -includesub_HEADERS = \ - init.h \ - tunables.h \ +includesub_HEADERS = \ + bootstrap-template.h \ + init.h \ + tunables.h \ version.h diff --git a/include/xenomai/bootstrap-template.h b/include/xenomai/bootstrap-template.h new file mode 100644 index 000000000..990c90d7c --- /dev/null +++ b/include/xenomai/bootstrap-template.h @@ -0,0 +1,274 @@ +/* + * The source code in this particular file is released using a dual + * license scheme. You can choose the licence that better fits your + * requirements. + * + * ----------------------------------------------------------------------- + * + * Copyright (C) 2017 Philippe Gerum + * + * 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 persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- + * + * Copyright (C) 2013 Philippe Gerum . + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#include + +/* @file bootstrap-template.h + * @brief Template header for bootstrap code + * + * The bootstrap code is configurable and is supposed to + * create a small routine that is called before other + * automatic GCC/C++ constructors are executed, + * as well as the regular main() routine. + * + * At its minimum it defines a constructor that calls + * a xenomai_init variant, + * and a function to retrieve the modified argv vector. + * + * Additionally a wrapper for the main() routine can be defined. + * Related Macros allow to specify the name of the wrapper. + * + * Look into lib/boilerplate/init for usecases covering + * shared libraries and executables usind the auto-init feature. + * + * Special handling for glibc can be enabled. + * This library will pass the argv vector to constructor + * function, thus allowing easy access to the commandline. + * + * Macros affecting the defined functionality: + * + * _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS + * Use glibc constructor signature (if glibc detected) + * _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL + * *Force* glibc constructor signature on/off (value 1/0) + * _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER + * Define a main function, calling the macro expression + * _XENOMAI_BOOTSTRAP_WEAKREF_MAINWRAPPER + * Set a weak reference to the defined main function + * _XENOMAI_BOOTSTRAP_DSO + * Should be defined when building shared libraries + */ + +/* if requested by _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS, + * test if macros for glibc are defined and + * define _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL accordingly */ +#if !defined(_XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL) \ + && defined(_XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS) +#if defined(__GLIBC__) && !defined(__UCLIBC__) +#define _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL 1 +#endif +#endif + +#if !defined(_XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL) \ + || _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL != 1 +#define _XENOMAI_BOOTSTRAP_DEFINE_GETARGV +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +static int early_argc; +static char *const *early_argv; + +/* @brief get the potentially argv vector + * + * The xenomai init code modifies the argv vector, + * this function allows to retrive this vector later. + */ +int xenomai_bootstrap_getargv(int *argc, char *const** argv) +{ + if (early_argc) + { + *argc = early_argc; + *argv = early_argv; + return 1; + } + return 0; +} + +/* if needed and not in the DSO, then + * define a function for fetching the commandline arguments + */ +#if !defined(_XENOMAI_INIT_HASFETCHARGV) && \ + ( !defined(_XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL) \ + || _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL != 1 ) + +#ifdef __cplusplus +} +#endif + +#include +#include +#include +#include +#include + +static int xenomai_init_fetchargv(int *argcp, char *const **argvp) +{ + char *arglist, *argend, *p, **v; + ssize_t len, ret; + int fd, n; + + len = 1024; + + for (;;) { + fd = __STD(open("/proc/self/cmdline", O_RDONLY)); + if (fd < 0) + return -1; + + arglist = __STD(malloc(len)); + if (arglist == NULL) { + __STD(close(fd)); + return -1; + } + + ret = __STD(read(fd, arglist, len)); + __STD(close(fd)); + + if (ret < 0) { + __STD(free(arglist)); + return -1; + } + + if (ret < len) + break; + + __STD(free(arglist)); + len <<= 1; + } + + argend = arglist + ret; + p = arglist; + n = 0; + while (p < argend) { + n++; + p += strlen(p) + 1; + } + + v = __STD(malloc((n + 1) * sizeof(char *))); + if (v == NULL) { + __STD(free(arglist)); + return -1; + } + + p = arglist; + n = 0; + while (p < argend) { + v[n++] = p; + p += strlen(p) + 1; + } + + v[n] = NULL; + *argcp = n; + *argvp = v; + return 0; +} + +#ifdef __cplusplus +extern "C" { +#endif +#endif + +/** Bootstrap: handle commandline args and call xenomai's init + * + * glibc calls constructors/destructors with the argv vector, + * which is nice as this avoids some code. + * Other C-libraries don't, and worse: define __GLIBC__ + * This probably should be an explicit opt-in + */ +#if _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL == 1 +__bootstrap_ctor static void xenomai_bootstrap(int argc, char *const argv[], char *const envp[]) +#else +__bootstrap_ctor static void xenomai_bootstrap(void) +#endif +{ +#if _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS_REAL == 1 + (void)envp; +#else + char *const *argv; + int argc; + if (xenomai_init_fetchargv(&argc, &argv) != 0) + return; +#endif + +#ifdef _XENOMAI_BOOTSTRAP_DSO + xenomai_init_dso(&argc, &argv); +#else + xenomai_init(&argc, &argv); +#endif + early_argc = argc; + early_argv = argv; +} + +/* If requested, we define the main funciton, + and weak refs */ +#ifdef _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER +#ifdef _XENOMAI_BOOTSTRAP_DSO +#error "Main wrapper is not allowed for shared libraries" +#endif + +#ifdef _XENOMAI_BOOTSTRAP_WEAKREF_MAINWRAPPER +int _XENOMAI_BOOTSTRAP_WEAKREF_MAINWRAPPER(int argc, char *const argv[]) +__attribute__((alias("xenomai_main"), weak)); +#endif + +int _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER(int argc, char *const argv[]); + +int xenomai_main(int argc, char *const argv[]) +{ +#ifdef trace_me + trace_me("xenomai_main entered"); +#endif + if (!early_argc) + { + xenomai_init(&argc, &argv); + /* State should be identical to using the constructor function */ + early_argc = argc; + early_argv = argv; + } + +#if defined(trace_me) && defined(__stringify) + trace_me("xenomai_main call %s", __stringify(_XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER)); +#endif + return _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER(early_argc, early_argv); +} +#endif /* ifdef _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER */ + +#ifdef __cplusplus +} +#endif diff --git a/include/xenomai/init.h b/include/xenomai/init.h index 7ab5c60bb..77507a7d5 100644 --- a/include/xenomai/init.h +++ b/include/xenomai/init.h @@ -29,6 +29,8 @@ extern "C" { int xenomai_init_fetchargv(int *argcp, char *const **argvp); +int xenomai_bootstrap_getargv(int *argc, char *const** argv); + void xenomai_init(int *argcp, char *const **argvp); void xenomai_init_dso(int *argcp, char *const **argvp); diff --git a/lib/boilerplate/init/Makefile.am b/lib/boilerplate/init/Makefile.am index b78dd61d1..7f12976a9 100644 --- a/lib/boilerplate/init/Makefile.am +++ b/lib/boilerplate/init/Makefile.am @@ -9,10 +9,9 @@ libbootstrap_a_CPPFLAGS = \ noinst_LTLIBRARIES = libbootstrap-pic.la -libbootstrap_pic_la_SOURCES = bootstrap.c +libbootstrap_pic_la_SOURCES = bootstrap-pic.c libbootstrap_pic_la_CPPFLAGS = \ - -D__BOOTSTRAP_DSO__ \ @XENO_USER_CFLAGS@ \ -I$(top_srcdir)/include \ -I$(top_srcdir)/lib diff --git a/lib/boilerplate/init/bootstrap-pic.c b/lib/boilerplate/init/bootstrap-pic.c new file mode 100644 index 000000000..9b7c54e17 --- /dev/null +++ b/lib/boilerplate/init/bootstrap-pic.c @@ -0,0 +1,2 @@ +#define _XENOMAI_BOOTSTRAP_DSO +#include diff --git a/lib/boilerplate/init/bootstrap.c b/lib/boilerplate/init/bootstrap.c index 8e4fc93d8..9fb38a715 100644 --- a/lib/boilerplate/init/bootstrap.c +++ b/lib/boilerplate/init/bootstrap.c @@ -1,186 +1,3 @@ -/* - * The source code in this particular file is released using a dual - * license scheme. You can choose the licence that better fits your - * requirements. - * - * ----------------------------------------------------------------------- - * - * Copyright (C) 2017 Philippe Gerum - * - * 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 persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * ----------------------------------------------------------------------- - * - * Copyright (C) 2013 Philippe Gerum . - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include - -static int early_argc; - -static char *const *early_argv; - -/* - * The bootstrap module object is built in two forms: - * - * - in static object form, to be glued to the main executable, which - * should include a wrapper interposing on the main() routine for - * auto-init purpose. Such wrapper is activated when symbol wrapping - * is enabled at link time (--wrap). - * - * - in dynamic object form, to be included in a shared library target - * which enables the auto-init feature. This form should not include - * any wrapper to a main() routine - which does not exist - but only - * a constructor routine performing the inits. - * - * The macro __BOOTSTRAP_DSO__ tells us whether we are building the - * bootstrap module to be glued into a dynamic shared object. If not, - * the main() interception code should be present in the relocatable - * object. - */ - -#ifdef __BOOTSTRAP_DSO__ - -static inline void call_init(int *argcp, char *const **argvp) -{ - xenomai_init_dso(argcp, argvp); -} - -#else - -const int xenomai_auto_bootstrap = 1; - -int __real_main(int argc, char *const argv[]); - -int __wrap_main(int argc, char *const argv[]) -__attribute__((alias("xenomai_main"), weak)); - -int xenomai_main(int argc, char *const argv[]) -{ - if (early_argc) - return __real_main(early_argc, early_argv); - - xenomai_init(&argc, &argv); - - return __real_main(argc, argv); -} - -static inline void call_init(int *argcp, char *const **argvp) -{ - xenomai_init(argcp, argvp); -} - -#endif /* !__BOOTSTRAP_DSO__ */ - -#if !defined(_XENOMAI_INIT_HASFETCHARGV) -#include -#include -#include -#include -#include - -static int xenomai_init_fetchargv(int *argcp, char *const **argvp) -{ - char *arglist, *argend, *p, **v; - ssize_t len, ret; - int fd, n; - - len = 1024; - - for (;;) { - fd = __STD(open("/proc/self/cmdline", O_RDONLY)); - if (fd < 0) - return -1; - - arglist = __STD(malloc(len)); - if (arglist == NULL) { - __STD(close(fd)); - return -1; - } - - ret = __STD(read(fd, arglist, len)); - __STD(close(fd)); - - if (ret < 0) { - __STD(free(arglist)); - return -1; - } - - if (ret < len) - break; - - __STD(free(arglist)); - len <<= 1; - } - - argend = arglist + ret; - p = arglist; - n = 0; - while (p < argend) { - n++; - p += strlen(p) + 1; - } - - v = __STD(malloc((n + 1) * sizeof(char *))); - if (v == NULL) { - __STD(free(arglist)); - return -1; - } - - p = arglist; - n = 0; - while (p < argend) { - v[n++] = p; - p += strlen(p) + 1; - } - - v[n] = NULL; - *argcp = n; - *argvp = v; - return 0; -} -#endif - - -__bootstrap_ctor static void xenomai_bootstrap(void) -{ - char *const *argv; - int argc; - - if (xenomai_init_fetchargv(&argc, &argv) != 0) - return; - - call_init(&argc, &argv); - early_argc = argc; - early_argv = argv; -} +#define _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER __real_main +#define _XENOMAI_BOOTSTRAP_WEAKREF_MAINWRAPPER __wrap_main +#include -- 2.19.1