From 01a9779fc63d59335dfdcddb694176b1d7b2124f Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 30 Apr 2016 18:10:49 +0200 Subject: [PATCH] Compile libc with -ffreestanding. This properly avoids problems where the compiler is unaware that this is the implementation and assumes it can rely on the implementation. For instance, it might implement calloc using a call to calloc. Restructure the code that wrongly assumed __STDC_HOSTED__ meant userspace. --- kernel/include/sortix/sigevent.h | 8 ++++---- kernel/kernel.cpp | 1 - libc/Makefile | 4 ++-- libc/c++/op-new.c++ | 2 +- libc/include/errno.h | 4 ++-- libc/include/malloc.h | 14 +++++++------- libc/include/msr.h | 4 ++-- libc/include/signal.h | 6 +++--- libc/include/sys/syscall.h | 4 ++-- libc/include/sys/types.h | 6 +++--- 10 files changed, 26 insertions(+), 27 deletions(-) diff --git a/kernel/include/sortix/sigevent.h b/kernel/include/sortix/sigevent.h index 81243326..0a54cbdb 100644 --- a/kernel/include/sortix/sigevent.h +++ b/kernel/include/sortix/sigevent.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2013, 2014, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,7 +22,7 @@ #include -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) #include <__/pthread.h> #endif @@ -32,7 +32,7 @@ extern "C" { #endif -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) #ifndef __pthread_attr_t_defined #define __pthread_attr_t_defined typedef __pthread_attr_t pthread_attr_t; @@ -49,7 +49,7 @@ struct sigevent int sigev_signo; union sigval sigev_value; void (*sigev_notify_function)(union sigval); -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) pthread_attr_t* sigev_notify_attributes; #else void* sigev_notify_attributes; diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index e20135b6..f9301347 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/libc/Makefile b/libc/Makefile index e528be6e..db67020d 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -10,7 +10,7 @@ CPUDIR:=$(CPU) CPPINCLUDES=-Iinclude CPPFLAGS=-D__is_sortix_libc $(CPPINCLUDES) -FLAGS=-Wall -Wextra $(OPTLEVEL) +FLAGS=-Wall -Wextra -ffreestanding $(OPTLEVEL) CFLAGS=-std=gnu11 -Wstrict-prototypes -Werror=implicit-function-declaration CXXFLAGS=-std=gnu++11 -fno-exceptions -fno-rtti ASFLAGS= @@ -740,7 +740,7 @@ HEADERS:=$(shell find include -type f) LIBK_OBJS:=$(FREEOBJS:.o=.libk.o) LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_sortix_libk -LIBK_FLAGS:=$(FLAGS) -ffreestanding +LIBK_FLAGS:=$(FLAGS) LIBK_CFLAGS:=$(CFLAGS) LIBK_CXXFLAGS:=$(CXXFLAGS) ifeq ($(HOST),x86_64-sortix) diff --git a/libc/c++/op-new.c++ b/libc/c++/op-new.c++ index 419bf28c..c00367ef 100644 --- a/libc/c++/op-new.c++ +++ b/libc/c++/op-new.c++ @@ -20,7 +20,7 @@ #include #include -#if __STDC_HOSTED__ +#ifndef __is_sortix_libk __attribute__((weak)) void* operator new(size_t size) diff --git a/libc/include/errno.h b/libc/include/errno.h index 2acfb32e..7894a0ac 100644 --- a/libc/include/errno.h +++ b/libc/include/errno.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -117,7 +117,7 @@ extern "C" { #define EOPNOTSUPP ENOTSUP -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) extern __thread int errno; #define errno errno diff --git a/libc/include/malloc.h b/libc/include/malloc.h index 93a6f697..faa05582 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, 2014, 2015 Jonas 'Sortie' Termansen. + * Copyright (c) 2012, 2013, 2014, 2015, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -74,17 +74,17 @@ int heap_get_paranoia(void); #endif #endif -#if !defined(HEAP_GUARD_DEBUG) && \ - defined(__is_sortix_kernel) && defined(HEAP_GUARD_DEBUG_KERNEL) +#ifdef __is_sortix_libk +#if !defined(HEAP_GUARD_DEBUG) && defined(HEAP_GUARD_DEBUG_KERNEL) #define HEAP_GUARD_DEBUG #endif - -#if !defined(MALLOC_GUARD_DEBUG) && \ - __STDC_HOSTED__ && defined(HEAP_GUARD_DEBUG_USERLAND) +#else +#if !defined(MALLOC_GUARD_DEBUG) && defined(HEAP_GUARD_DEBUG_USERLAND) #define HEAP_GUARD_DEBUG #endif +#endif -#if defined(HEAP_GUARD_DEBUG) && !defined(__is_sortix_kernel) +#if defined(HEAP_GUARD_DEBUG) && !defined(__is_sortix_libk) struct heap_alloc { uintptr_t from; diff --git a/libc/include/msr.h b/libc/include/msr.h index e7442b73..d5647418 100644 --- a/libc/include/msr.h +++ b/libc/include/msr.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Jonas 'Sortie' Termansen. + * Copyright (c) 2013, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -52,7 +52,7 @@ static __inline uint64_t wrmsr_instruction(uint32_t msrid, uint64_t value) return value; } -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) uint64_t rdmsr(uint32_t msrid); uint64_t wrmsr(uint32_t msrid, uint64_t value); diff --git a/libc/include/signal.h b/libc/include/signal.h index 61508f7d..373ab257 100644 --- a/libc/include/signal.h +++ b/libc/include/signal.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2011, 2012, 2013, 2014, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -24,7 +24,7 @@ #include -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) #include <__/pthread.h> #endif @@ -60,7 +60,7 @@ typedef __pid_t pid_t; #include #endif -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) #ifndef __pthread_attr_t_defined #define __pthread_attr_t_defined diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h index 214c8906..20c80827 100644 --- a/libc/include/sys/syscall.h +++ b/libc/include/sys/syscall.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, 2013 Jonas 'Sortie' Termansen. + * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,7 +20,7 @@ #ifndef INCLUDE_SYS_SYSCALL_H #define INCLUDE_SYS_SYSCALL_H -#if !__STDC_HOSTED__ +#ifdef __is_sortix_libk #error "This file is part of user-space and should not be built into libk" #endif diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h index 9d0f40d9..47992a28 100644 --- a/libc/include/sys/types.h +++ b/libc/include/sys/types.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, 2013, 2014 Jonas 'Sortie' Termansen. + * Copyright (c) 2011, 2012, 2013, 2014, 2016 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,7 +23,7 @@ #include #include -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) #include <__/pthread.h> #endif @@ -459,7 +459,7 @@ typedef __uid_t uid_t; #define SCNxUID __SCNxUID #endif -#if __STDC_HOSTED__ +#if !defined(__is_sortix_libk) && !defined(__is_sortix_kernel) #ifndef __pthread_attr_t_defined #define __pthread_attr_t_defined