From 2809d233b5b46312ea29b0d7d378e99d160212ad Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Fri, 27 Feb 2015 15:57:07 -0800 Subject: [PATCH] Added a simple crti/n/1 to fix clang compat issues --- SConstruct | 4 ++ bin/ethdump/SConscript | 2 + bin/ethinject/SConscript | 2 + bin/shell/SConscript | 2 + lib/libc/SConscript | 5 +- lib/libc/crt1.c | 116 +++++++++++++++++++++++++++++++++++++++ lib/libc/crti.S | 40 ++++++++++++++ lib/libc/crtn.S | 34 ++++++++++++ sbin/ifconfig/SConscript | 2 + sbin/init/SConscript | 4 +- sys/include/cdefs.h | 2 + tests/SConscript | 19 ++++++- 12 files changed, 227 insertions(+), 5 deletions(-) create mode 100644 lib/libc/crt1.c create mode 100644 lib/libc/crti.S create mode 100644 lib/libc/crtn.S diff --git a/SConstruct b/SConstruct index 3ed4ffa..e5bd95d 100644 --- a/SConstruct +++ b/SConstruct @@ -154,6 +154,10 @@ conf.Finish() Export('env') +# Program start/end +env["CRTBEGIN"] = [ "#build/lib/libc/crti.o", "#build/lib/libc/crt1.o" ] +env["CRTEND"] = [ "#build/lib/libc/crtn.o" ] + # Debugging Tools # Create include tree diff --git a/bin/ethdump/SConscript b/bin/ethdump/SConscript index 9a1216d..5ca1cc2 100644 --- a/bin/ethdump/SConscript +++ b/bin/ethdump/SConscript @@ -10,7 +10,9 @@ src_common = [ "ethdump.c" ] +src.append(env["CRTBEGIN"]) src.append(src_common) +src.append(env["CRTEND"]) ethdump_env.Append(LINKFLAGS = ['-nostdlib']) ethdump_env.Append(CPPFLAGS = ['-nostdinc']) diff --git a/bin/ethinject/SConscript b/bin/ethinject/SConscript index d94ae60..ff84b5f 100644 --- a/bin/ethinject/SConscript +++ b/bin/ethinject/SConscript @@ -10,7 +10,9 @@ src_common = [ "ethinject.c" ] +src.append(env["CRTBEGIN"]) src.append(src_common) +src.append(env["CRTEND"]) ethinject_env.Append(LINKFLAGS = ['-nostdlib']) ethinject_env.Append(CPPFLAGS = ['-nostdinc']) diff --git a/bin/shell/SConscript b/bin/shell/SConscript index 2c6d898..0cca10e 100644 --- a/bin/shell/SConscript +++ b/bin/shell/SConscript @@ -10,7 +10,9 @@ src_common = [ "shell.c" ] +src.append(env["CRTBEGIN"]) src.append(src_common) +src.append(env["CRTEND"]) init_env.Append(LINKFLAGS = ['-nostdlib']) init_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc']) diff --git a/lib/libc/SConscript b/lib/libc/SConscript index 9349fac..d8e511c 100644 --- a/lib/libc/SConscript +++ b/lib/libc/SConscript @@ -24,7 +24,7 @@ src_common = [ ] src_amd64 = [ - "amd64/entry.S", +# "amd64/entry.S", "amd64/syscall.S", ] @@ -36,4 +36,7 @@ libc_env.Append(CPPFLAGS = ['-nostdinc']) libc_env.Append(CPPPATH = ['#build/include']) libc_env.StaticLibrary("libc", src) +libc_env.StaticObject("crt1", "crt1.c") +libc_env.StaticObject("crti", "crti.S") +libc_env.StaticObject("crtn", "crtn.S") diff --git a/lib/libc/crt1.c b/lib/libc/crt1.c new file mode 100644 index 0000000..5d78693 --- /dev/null +++ b/lib/libc/crt1.c @@ -0,0 +1,116 @@ +/* LINTLIBRARY */ +/*- + * Copyright 1996-1998 John D. Polstra. + * Copyright 2012 Konstantin Belousov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include + +extern int main(int, char **); + +extern void (*__preinit_array_start[])(int, char **, char **) __hidden; +extern void (*__preinit_array_end[])(int, char **, char **) __hidden; +extern void (*__init_array_start[])(int, char **, char **) __hidden; +extern void (*__init_array_end[])(int, char **, char **) __hidden; +extern void (*__fini_array_start[])(void) __hidden; +extern void (*__fini_array_end[])(void) __hidden; +extern void _fini(void) __hidden; +extern void _init(void) __hidden; + +char **environ; +const char *__progname = ""; + +static void +finalizer(void) +{ + void (*fn)(void); + size_t array_size, n; + + array_size = __fini_array_end - __fini_array_start; + for (n = array_size; n > 0; n--) { + fn = __fini_array_start[n - 1]; + if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) + (fn)(); + } + _fini(); +} + +static inline void +handle_static_init(int argc, char **argv, char **env) +{ + void (*fn)(int, char **, char **); + size_t array_size, n; + + atexit(finalizer); + + array_size = __preinit_array_end - __preinit_array_start; + for (n = 0; n < array_size; n++) { + fn = __preinit_array_start[n]; + if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) + fn(argc, argv, env); + } + _init(); + array_size = __init_array_end - __init_array_start; + for (n = 0; n < array_size; n++) { + fn = __init_array_start[n]; + if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) + fn(argc, argv, env); + } +} + +static inline void +handle_argv(int argc, char *argv[], char **env) +{ + const char *s; + + if (environ == NULL) + environ = env; + if (argc > 0 && argv[0] != NULL) { + __progname = argv[0]; + for (s = __progname; *s != '\0'; s++) { + if (*s == '/') + __progname = s + 1; + } + } +} + +void +_start(char **ap) +{ + int argc; + char **argv; + char **env; + + //argc = *(long *)(void *)ap; + //argv = ap + 1; + //env = ap + 2 + argc; + //handle_argv(argc, argv, env); + + //handle_static_init(argc, argv, env); + + exit(main(argc, argv)); +} + diff --git a/lib/libc/crti.S b/lib/libc/crti.S new file mode 100644 index 0000000..74653ed --- /dev/null +++ b/lib/libc/crti.S @@ -0,0 +1,40 @@ +/*- + * Copyright 1996, 1997, 1998, 2000 John D. Polstra. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + .section .init,"ax",@progbits + .align 4 + .globl _init + .type _init,@function +_init: + subq $8,%rsp + + .section .fini,"ax",@progbits + .align 4 + .globl _fini + .type _fini,@function +_fini: + subq $8,%rsp + + .section .note.GNU-stack,"",%progbits diff --git a/lib/libc/crtn.S b/lib/libc/crtn.S new file mode 100644 index 0000000..fe50bc7 --- /dev/null +++ b/lib/libc/crtn.S @@ -0,0 +1,34 @@ +/*- + * Copyright 1996, 1997, 1998, 2000 John D. Polstra. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + .section .init,"ax",@progbits + addq $8,%rsp + ret + + .section .fini,"ax",@progbits + addq $8,%rsp + ret + + .section .note.GNU-stack,"",%progbits diff --git a/sbin/ifconfig/SConscript b/sbin/ifconfig/SConscript index 7328d30..0a76eb9 100644 --- a/sbin/ifconfig/SConscript +++ b/sbin/ifconfig/SConscript @@ -10,7 +10,9 @@ src_common = [ "ifconfig.c" ] +src.append(env["CRTBEGIN"]) src.append(src_common) +src.append(env["CRTEND"]) ifconfig_env.Append(LINKFLAGS = ['-nostdlib']) ifconfig_env.Append(CPPFLAGS = ['-nostdinc']) diff --git a/sbin/init/SConscript b/sbin/init/SConscript index ea4e5e3..1a4ffbe 100644 --- a/sbin/init/SConscript +++ b/sbin/init/SConscript @@ -7,10 +7,12 @@ init_env = env.Clone() src = [ ] src_common = [ - "init.c" + "init.c", ] +src.append(env["CRTBEGIN"]) src.append(src_common) +src.append(env["CRTEND"]) init_env.Append(LINKFLAGS = ['-nostdlib']) init_env.Append(CPPFLAGS = ['-nostdinc']) diff --git a/sys/include/cdefs.h b/sys/include/cdefs.h index f947ad4..85ec7ed 100644 --- a/sys/include/cdefs.h +++ b/sys/include/cdefs.h @@ -14,5 +14,7 @@ #define ROUNDUP(_x, _n) (((_x) + (_n) - 1) & ~((_n) - 1)) +#define __hidden __attribute__((__visibility__("hidden"))) + #endif /* __CDEFS_H__ */ diff --git a/tests/SConscript b/tests/SConscript index 24ac172..38d21b9 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -9,8 +9,17 @@ test_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc']) test_env.Append(CPPPATH = ['#build/include']) test_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c']) -test_env.Program("threadtest", ["threadtest.c"]) -test_env.Program("pthreadtest", ["pthreadtest.c"]) +threadtest_src = [] +threadtest_src.append(env["CRTBEGIN"]) +threadtest_src.append(["threadtest.c"]) +threadtest_src.append(env["CRTEND"]) +test_env.Program("threadtest", threadtest_src) + +pthreadtest_src = [] +pthreadtest_src.append(env["CRTBEGIN"]) +pthreadtest_src.append(["threadtest.c"]) +pthreadtest_src.append(env["CRTEND"]) +test_env.Program("pthreadtest", pthreadtest_src) test_netenv = env.Clone() @@ -20,5 +29,9 @@ test_netenv.Append(CPPPATH = ['#build/include', '#build/include/ipv4']) test_netenv.Append(LIBPATH = ['#build/lib/libc', '#build/lib/liblwip'], LIBS = ['lwip', 'c']) -test_netenv.Program("lwiptest", ["lwiptest.c"]) +lwiptest_src = [] +lwiptest_src.append(env["CRTBEGIN"]) +lwiptest_src.append(["threadtest.c"]) +lwiptest_src.append(env["CRTEND"]) +test_netenv.Program("lwiptest", lwiptest_src)