From: Norbert Lange Date: Mon, 23 Apr 2018 16:24:18 +0200 Message-Id: <20180423142420.5429-1-norbert.lange@andritz.com> Subject: [Xenomai] [PATCH 1/3] add an flexible bootstrap template header List-Id: Discussions about the Xenomai project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: xenomai@xenomai.org this adds a new file 'include/xenomai/bootstrap-template.h', replicating the functionality of the previous bootstrap code, but allowing this functionality to be controlled with macros. Intended purpose is to allow applications to easily compile their own bootstrap, without depending on precompiled objects or linker wrapping magic. Signed-off-by: Norbert Lange --- include/xenomai/Makefile.am | 7 +- include/xenomai/bootstrap-template.h | 270 +++++++++++++++++++++++++++ include/xenomai/init.h | 2 + lib/boilerplate/init/Makefile.am | 3 +- lib/boilerplate/init/bootstrap-pic.c | 2 + lib/boilerplate/init/bootstrap.c | 175 +---------------- 6 files changed, 282 insertions(+), 177 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..79197db4b 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..d9fb0b313 --- /dev/null +++ b/include/xenomai/bootstrap-template.h @@ -0,0 +1,270 @@ +/* + * 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 + */ + +#define _XENOMAI_BOOTSTRAP_GLIBC_CONSTRUCTORS + +/* 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 + +#ifndef __setup_section +#define __setup_section __attribute__ ((section(".text.startup.xenomai"))) +#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. + */ +__setup_section int xenomai_bootstrap_getargv(int *argc, char *const** argv) +{ + if (early_argc) + { + *argc = early_argc; + *argv = early_argv; + return 1; + } + return 0; +} + +#ifdef __cplusplus +} +#endif + +/* if needed, 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 ) +#include +#include +#include +#include +#include + +__setup_section 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 + +#ifdef __cplusplus +extern "C" { +#endif + +#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[]); + +__setup_section int xenomai_main(int argc, char *const argv[]) +{ + if (!early_argc) + { + xenomai_init(&argc, &argv); + /* State should be identical to using the constructor function */ + early_argc = argc; + early_argv = argv; + } + + return _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER(early_argc, early_argv); +} +#endif /* ifdef _XENOMAI_BOOTSTRAP_DEFINE_MAINWRAPPER */ + +/** + * 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; +} + +#ifdef __cplusplus +} +#endif diff --git a/include/xenomai/init.h b/include/xenomai/init.h index 598bf5329..1cffa6f17 100644 --- a/include/xenomai/init.h +++ b/include/xenomai/init.h @@ -29,6 +29,8 @@ void xenomai_init(int *argcp, char *const **argvp); void xenomai_init_dso(int *argcp, char *const **argvp); +int xenomai_bootstrap_getargv(int *argc, char *const** argv); + int xenomai_main(int argc, char *const argv[]); void xenomai_usage(void); 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 64a11c2d2..9fb38a715 100644 --- a/lib/boilerplate/init/bootstrap.c +++ b/lib/boilerplate/init/bootstrap.c @@ -1,172 +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 -#include -#include -#include -#include -#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__ */ - -__bootstrap_ctor static void xenomai_bootstrap(void) -{ - char *arglist, *argend, *p, **v, *const *argv; - ssize_t len, ret; - int fd, n, argc; - - len = 1024; - - for (;;) { - fd = __STD(open("/proc/self/cmdline", O_RDONLY)); - if (fd < 0) - return; - - arglist = __STD(malloc(len)); - if (arglist == NULL) { - __STD(close(fd)); - return; - } - - ret = __STD(read(fd, arglist, len)); - __STD(close(fd)); - - if (ret < 0) { - __STD(free(arglist)); - return; - } - - 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; - } - - p = arglist; - n = 0; - while (p < argend) { - v[n++] = p; - p += strlen(p) + 1; - } - - v[n] = NULL; - argv = v; - argc = n; - - 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.17.0