Added a simple crti/n/1 to fix clang compat issues

This commit is contained in:
Ali Mashtizadeh 2015-02-27 15:57:07 -08:00
parent 6a7fd630ce
commit 2809d233b5
12 changed files with 227 additions and 5 deletions

View File

@ -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

View File

@ -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'])

View File

@ -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'])

View File

@ -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'])

View File

@ -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")

116
lib/libc/crt1.c Normal file
View File

@ -0,0 +1,116 @@
/* LINTLIBRARY */
/*-
* Copyright 1996-1998 John D. Polstra.
* Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
* 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 <stdlib.h>
#include <sys/cdefs.h>
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));
}

40
lib/libc/crti.S Normal file
View File

@ -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

34
lib/libc/crtn.S Normal file
View File

@ -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

View File

@ -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'])

View File

@ -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'])

View File

@ -14,5 +14,7 @@
#define ROUNDUP(_x, _n) (((_x) + (_n) - 1) & ~((_n) - 1))
#define __hidden __attribute__((__visibility__("hidden")))
#endif /* __CDEFS_H__ */

View File

@ -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)