Merge the basics component into libc-bottom-half. (#199)

We no longer have a need to maintain basics as a separate component.
Folding it into libc-bottom-half eliminates a fair amount of redundancy.
This commit is contained in:
Dan Gohman 2020-06-01 16:44:05 -07:00 committed by GitHub
parent 84c0778bff
commit 753cc4344d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 84 additions and 298 deletions

View File

@ -12,21 +12,13 @@ INSTALL_DIR ?= /usr/local
THREAD_MODEL ?= single
# yes or no
BUILD_DLMALLOC ?= yes
BUILD_LIBC_BOTTOM_HALF ?= yes
BUILD_LIBC_TOP_HALF ?= yes
# The directory where we're store intermediate artifacts.
OBJDIR ?= $(CURDIR)/build
# Check dependencies.
ifeq ($(BUILD_LIBC_TOP_HALF),yes)
ifneq ($(BUILD_LIBC_BOTTOM_HALF),yes)
$(error BUILD_LIBC_TOP_HALF=yes depends on BUILD_LIBC_BOTTOM_HALF=yes)
endif
endif
ifeq ($(BUILD_LIBC_BOTTOM_HALF),yes)
ifneq ($(BUILD_DLMALLOC),yes)
$(error BUILD_LIBC_BOTTOM_HALF=yes depends on BUILD_DLMALLOC=yes)
endif
$(error build currently depends on BUILD_DLMALLOC=yes)
endif
# Variables from this point on are not meant to be overridable via the
@ -39,12 +31,6 @@ MULTIARCH_TRIPLE = wasm32-wasi
# These variables describe the locations of various files and directories in
# the source tree.
BASICS_DIR = $(CURDIR)/basics
BASICS_INC = $(BASICS_DIR)/include
BASICS_CRT_SOURCES = $(wildcard $(BASICS_DIR)/crt/*.c)
BASICS_SOURCES = \
$(wildcard $(BASICS_DIR)/sources/*.c) \
$(wildcard $(BASICS_DIR)/sources/math/*.c)
DLMALLOC_DIR = $(CURDIR)/dlmalloc
DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src
DLMALLOC_SOURCES = $(DLMALLOC_SRC_DIR)/dlmalloc.c
@ -209,25 +195,15 @@ WASM_CFLAGS += --sysroot="$(SYSROOT)"
# These variables describe the locations of various files and directories in
# the build tree.
objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1))
BASICS_OBJS = $(call objs,$(BASICS_SOURCES))
DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
LIBC_TOP_HALF_ALL_OBJS = $(call objs,$(LIBC_TOP_HALF_ALL_SOURCES))
LIBC_OBJS := $(BASICS_OBJS)
ifeq ($(BUILD_DLMALLOC),yes)
LIBC_OBJS += $(DLMALLOC_OBJS)
endif
ifeq ($(BUILD_LIBC_BOTTOM_HALF),yes)
# Override basics' string.o with libc-bottom-half's.
LIBC_OBJS := $(filter-out %/string.o,$(LIBC_OBJS))
# Add libc-bottom-half's objects.
LIBC_OBJS += $(LIBC_BOTTOM_HALF_ALL_OBJS)
endif
ifeq ($(BUILD_LIBC_TOP_HALF),yes)
# Override libc-bottom-half's string.o with libc-top-half's.
LIBC_OBJS := $(filter-out %/string.o,$(LIBC_OBJS))
# Override libc-bottom-half's qsort.o with libc-top-half's.
LIBC_OBJS := $(filter-out %/qsort.o,$(LIBC_OBJS))
# libc-top-half is musl.
LIBC_OBJS += $(LIBC_TOP_HALF_ALL_OBJS)
endif
@ -405,7 +381,6 @@ include_dirs:
# Install the include files.
#
mkdir -p "$(SYSROOT_INC)"
cp -r "$(BASICS_INC)" "$(SYSROOT)"
cp -r "$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)"
# Generate musl's bits/alltypes.h header.
@ -424,19 +399,13 @@ include_dirs:
# Remove selected header files.
$(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS))
ifeq ($(BUILD_LIBC_BOTTOM_HALF),no)
CRT_SOURCES = $(BASICS_CRT_SOURCES)
else
CRT_SOURCES = $(LIBC_BOTTOM_HALF_CRT_SOURCES)
endif
startup_files: include_dirs
#
# Build the startup files.
#
@mkdir -p "$(OBJDIR)"
cd "$(OBJDIR)" && \
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT_SOURCES) -MD -MP && \
"$(WASM_CC)" $(WASM_CFLAGS) -c $(LIBC_BOTTOM_HALF_CRT_SOURCES) -MD -MP && \
mkdir -p "$(SYSROOT_LIB)" && \
mv *.o "$(SYSROOT_LIB)"

View File

@ -1,23 +0,0 @@
extern void __wasm_call_ctors(void);
extern int __original_main(void);
extern void __prepare_for_exit(void);
void _Exit(int) __attribute__((noreturn));
__attribute__((export_name("_start")))
void _start(void) {
// The linker synthesizes this to call constructors.
__wasm_call_ctors();
// Call `__original_main` which will either be the application's zero-argument
// `__original_main` function or a libc routine which calls `__main_void`.
// TODO: Call `main` directly once we no longer have to support old compilers.
int r = __original_main();
// Call atexit functions, destructors, stdio cleanup, etc.
__prepare_for_exit();
// If main exited successfully, just return, otherwise call _Exit.
if (r != 0) {
_Exit(r);
}
}

View File

@ -1,6 +0,0 @@
#ifndef __wasm_basics___typedef_blksize_t_h
#define __wasm_basics___typedef_blksize_t_h
typedef long blksize_t;
#endif

View File

@ -1,6 +0,0 @@
#ifndef __wasm_basics___typedef_gid_t_h
#define __wasm_basics___typedef_gid_t_h
typedef unsigned gid_t;
#endif

View File

@ -1,6 +0,0 @@
#ifndef __wasm_basics___typedef_mode_t_h
#define __wasm_basics___typedef_mode_t_h
typedef unsigned mode_t;
#endif

View File

@ -1,6 +0,0 @@
#ifndef __wasm_basics___typedef_uid_t_h
#define __wasm_basics___typedef_uid_t_h
typedef unsigned uid_t;
#endif

View File

@ -1,6 +0,0 @@
#ifndef __wasm_basics_errno_h
#define __wasm_basics_errno_h
#include <__errno.h>
#endif

View File

@ -1,10 +0,0 @@
#ifndef __wasm_basics_stdlib_h
#define __wasm_basics_stdlib_h
/*
* Include the real implementation, which is factored into a separate file so
* that it can be reused by other libc stdlib implementations.
*/
#include <__functions_malloc.h>
#endif

View File

@ -1,10 +0,0 @@
#ifndef __wasm_basics_string_h
#define __wasm_basics_string_h
/*
* Include the real implementation, which is factored into a separate file so
* that it can be reused by other libc string implementations.
*/
#include <__functions_memcpy.h>
#endif

View File

@ -1,10 +0,0 @@
#ifndef __wasm_basics_sys_stat_h
#define __wasm_basics_sys_stat_h
#include <__struct_stat.h>
#define st_atime st_atim.tv_sec
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
#endif

View File

@ -1,15 +0,0 @@
#ifndef __wasm_basics_sys_types_h
#define __wasm_basics_sys_types_h
#define __need_size_t
#include <stddef.h>
#include <__typedef_clock_t.h>
#include <__typedef_time_t.h>
#include <__typedef_blksize_t.h>
#include <__typedef_off_t.h>
#include <__typedef_ssize_t.h>
#include <__typedef_suseconds_t.h>
#include <__typedef_nlink_t.h>
#endif

View File

@ -1,11 +0,0 @@
#ifndef __wasm_basics_time_h
#define __wasm_basics_time_h
#define __need_size_t
#define __need_NULL
#include <stddef.h>
#include <__typedef_time_t.h>
#include <__struct_timespec.h>
#endif

View File

@ -1,41 +0,0 @@
#include <string.h>
#include <stdint.h>
static void *copy_forward(void *restrict dst, const void *restrict src, size_t n) {
char *d = (char *)dst;
const char *s = (const char *)src;
while (n-- != 0) {
*d++ = *s++;
}
return dst;
}
static void *copy_backward(void *restrict dst, const void *restrict src, size_t n) {
char *d = (char *)dst;
const char *s = (const char *)src;
d += n;
s += n;
while (n-- != 0) {
*--d = *--s;
}
return dst;
}
void *memcpy(void *restrict dst, const void *restrict src, size_t n) {
return copy_forward(dst, src, n);
}
void *memmove(void *dst, const void *src, size_t n) {
if ((uintptr_t)dst - (uintptr_t)src >= n) {
return copy_forward(dst, src, n);
}
return copy_backward(dst, src, n);
}
void *memset(void *restrict dst, int c, size_t n) {
char *d = (char *)dst;
while (n-- != 0) {
*d++ = c;
}
return dst;
}

View File

@ -3021,9 +3021,12 @@
#define __wasi_libc_environ_h
#define __wasi_libc_find_relpath_h
#define __wasi_libc_h
#define __wasilibc___errno_h
#define __wasilibc___errno_values_h
#define __wasilibc___fd_set_h
#define __wasilibc___function___isatty_h
#define __wasilibc___functions_malloc_h
#define __wasilibc___functions_memcpy_h
#define __wasilibc___header_dirent_h
#define __wasilibc___header_fcntl_h
#define __wasilibc___header_netinet_in_h
@ -3036,7 +3039,9 @@
#define __wasilibc___header_sys_stat_h
#define __wasilibc___header_time_h
#define __wasilibc___header_unistd_h
#define __wasilibc___include_inttypes_h
#define __wasilibc___macro_FD_SETSIZE_h
#define __wasilibc___macro_PAGESIZE_h
#define __wasilibc___mode_t_h
#define __wasilibc___seek_h
#define __wasilibc___struct_dirent_h
@ -3051,42 +3056,37 @@
#define __wasilibc___struct_sockaddr_in_h
#define __wasilibc___struct_sockaddr_storage_h
#define __wasilibc___struct_sockaddr_un_h
#define __wasilibc___struct_stat_h
#define __wasilibc___struct_timespec_h
#define __wasilibc___struct_timeval_h
#define __wasilibc___struct_tm_h
#define __wasilibc___struct_tms_h
#define __wasilibc___typedef_DIR_h
#define __wasilibc___typedef_blkcnt_t_h
#define __wasilibc___typedef_blksize_t_h
#define __wasilibc___typedef_clock_t_h
#define __wasilibc___typedef_clockid_t_h
#define __wasilibc___typedef_dev_t_h
#define __wasilibc___typedef_fd_set_h
#define __wasilibc___typedef_gid_t_h
#define __wasilibc___typedef_in_addr_t_h
#define __wasilibc___typedef_in_port_t_h
#define __wasilibc___typedef_ino_t_h
#define __wasilibc___typedef_mode_t_h
#define __wasilibc___typedef_nfds_t_h
#define __wasilibc___typedef_nlink_t_h
#define __wasilibc___typedef_off_t_h
#define __wasilibc___typedef_sa_family_t_h
#define __wasilibc___typedef_sigset_t_h
#define __wasilibc___typedef_socklen_t_h
#define __wasilibc___typedef_ssize_t_h
#define __wasilibc___typedef_suseconds_t_h
#define __wasilibc___typedef_time_t_h
#define __wasilibc___typedef_uid_t_h
#define __wasm 1
#define __wasm32 1
#define __wasm32__ 1
#define __wasm__ 1
#define __wasm_basics___errno_h
#define __wasm_basics___functions_malloc_h
#define __wasm_basics___functions_memcpy_h
#define __wasm_basics___include_inttypes_h
#define __wasm_basics___macro_PAGESIZE_h
#define __wasm_basics___struct_stat_h
#define __wasm_basics___struct_timespec_h
#define __wasm_basics___typedef_blkcnt_t_h
#define __wasm_basics___typedef_blksize_t_h
#define __wasm_basics___typedef_clock_t_h
#define __wasm_basics___typedef_dev_t_h
#define __wasm_basics___typedef_gid_t_h
#define __wasm_basics___typedef_ino_t_h
#define __wasm_basics___typedef_mode_t_h
#define __wasm_basics___typedef_nlink_t_h
#define __wasm_basics___typedef_off_t_h
#define __wasm_basics___typedef_ssize_t_h
#define __wasm_basics___typedef_suseconds_t_h
#define __wasm_basics___typedef_time_t_h
#define __wasm_basics___typedef_uid_t_h
#define _tolower(a) ((a)|0x20)
#define _toupper(a) ((a)&0x5f)
#define acos(x) __tg_real_complex(acos, (x))

View File

@ -16,5 +16,4 @@ libc rather than to be a layer on top of libc.
[libpreopen]: https://github.com/musec/libpreopen
The WASI libc "bottom half" depends on the basics and dlmalloc components of
wasi-libc.
The WASI libc lower half currently depends on the dlmalloc component.

View File

@ -1,15 +0,0 @@
// Copyright (c) 2015 Nuxi, https://nuxi.nl/
//
// SPDX-License-Identifier: BSD-2-Clause
#include <stdlib.h>
#ifndef qsort
#error "qsort is supposed to be a macro as well"
#endif
// clang-format off
void (qsort)(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *)) {
return qsort(base, nel, width, compar);
}

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___errno_h
#define __wasm_basics___errno_h
#ifndef __wasilibc___errno_h
#define __wasilibc___errno_h
#ifdef __cplusplus
extern "C" {

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___functions_malloc_h
#define __wasm_basics___functions_malloc_h
#ifndef __wasilibc___functions_malloc_h
#define __wasilibc___functions_malloc_h
#define __need_size_t
#define __need_wchar_t

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___functions_memcpy_h
#define __wasm_basics___functions_memcpy_h
#ifndef __wasilibc___functions_memcpy_h
#define __wasilibc___functions_memcpy_h
#define __need_size_t
#define __need_NULL

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___include_inttypes_h
#define __wasm_basics___include_inttypes_h
#ifndef __wasilibc___include_inttypes_h
#define __wasilibc___include_inttypes_h
#include <stdint.h>

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___macro_PAGESIZE_h
#define __wasm_basics___macro_PAGESIZE_h
#ifndef __wasilibc___macro_PAGESIZE_h
#define __wasilibc___macro_PAGESIZE_h
/*
* The page size in WebAssembly is fixed at 64 KiB. If this ever changes,

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___struct_stat_h
#define __wasm_basics___struct_stat_h
#ifndef __wasilibc___struct_stat_h
#define __wasilibc___struct_stat_h
#include <__typedef_dev_t.h>
#include <__typedef_ino_t.h>

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___struct_timespec_h
#define __wasm_basics___struct_timespec_h
#ifndef __wasilibc___struct_timespec_h
#define __wasilibc___struct_timespec_h
#include <__typedef_time_t.h>

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_blkcnt_t_h
#define __wasm_basics___typedef_blkcnt_t_h
#ifndef __wasilibc___typedef_blkcnt_t_h
#define __wasilibc___typedef_blkcnt_t_h
/* Define these as 64-bit signed integers to support files larger than 2 GiB. */
typedef long long blkcnt_t;

View File

@ -0,0 +1,6 @@
#ifndef __wasilibc___typedef_blksize_t_h
#define __wasilibc___typedef_blksize_t_h
typedef long blksize_t;
#endif

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_clock_t_h
#define __wasm_basics___typedef_clock_t_h
#ifndef __wasilibc___typedef_clock_t_h
#define __wasilibc___typedef_clock_t_h
/* Define this as a 64-bit signed integer to avoid wraparounds. */
typedef long long clock_t;

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_dev_t_h
#define __wasm_basics___typedef_dev_t_h
#ifndef __wasilibc___typedef_dev_t_h
#define __wasilibc___typedef_dev_t_h
/* Define these as 64-bit integers to support billions of devices. */
typedef unsigned long long dev_t;

View File

@ -0,0 +1,6 @@
#ifndef __wasilibc___typedef_gid_t_h
#define __wasilibc___typedef_gid_t_h
typedef unsigned gid_t;
#endif

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_ino_t_h
#define __wasm_basics___typedef_ino_t_h
#ifndef __wasilibc___typedef_ino_t_h
#define __wasilibc___typedef_ino_t_h
/* Define these as 64-bit integers to support billions of inodes. */
typedef unsigned long long ino_t;

View File

@ -0,0 +1,6 @@
#ifndef __wasilibc___typedef_mode_t_h
#define __wasilibc___typedef_mode_t_h
typedef unsigned mode_t;
#endif

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_nlink_t_h
#define __wasm_basics___typedef_nlink_t_h
#ifndef __wasilibc___typedef_nlink_t_h
#define __wasilibc___typedef_nlink_t_h
/* Define these as 64-bit unsigned integers to support billions of links. */
typedef unsigned long long nlink_t;

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_off_t_h
#define __wasm_basics___typedef_off_t_h
#ifndef __wasilibc___typedef_off_t_h
#define __wasilibc___typedef_off_t_h
/* Define these as 64-bit signed integers to support files larger than 2 GiB. */
typedef long long off_t;

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_ssize_t_h
#define __wasm_basics___typedef_ssize_t_h
#ifndef __wasilibc___typedef_ssize_t_h
#define __wasilibc___typedef_ssize_t_h
/* This is defined to be the same size as size_t. */
typedef long ssize_t;

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_suseconds_t_h
#define __wasm_basics___typedef_suseconds_t_h
#ifndef __wasilibc___typedef_suseconds_t_h
#define __wasilibc___typedef_suseconds_t_h
/* Define this to be 64-bit as its main use is in struct timeval where the
extra space would otherwise be padding. */

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics___typedef_time_t_h
#define __wasm_basics___typedef_time_t_h
#ifndef __wasilibc___typedef_time_t_h
#define __wasilibc___typedef_time_t_h
/* Define this as a 64-bit signed integer to avoid the 2038 bug. */
typedef long long time_t;

View File

@ -0,0 +1,6 @@
#ifndef __wasilibc___typedef_uid_t_h
#define __wasilibc___typedef_uid_t_h
typedef unsigned uid_t;
#endif

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics_inttypes_h
#define __wasm_basics_inttypes_h
#ifndef __wasilibc_inttypes_h
#define __wasilibc_inttypes_h
/*
* Include the real implementation, which is factored into a separate file so

View File

@ -5,6 +5,6 @@
* Include the real implementation, which is factored into a separate file so
* that it can be reused by other libc stdlib implementations.
*/
#include <__header_stdlib.h>
#include <__functions_malloc.h>
#endif

View File

@ -1,5 +1,5 @@
#ifndef __wasm_basics_wchar_h
#define __wasm_basics_wchar_h
#ifndef __wasilibc_wchar_h
#define __wasilibc_wchar_h
#define __need_size_t
#define __need_wchar_t

View File

@ -1,5 +1,4 @@
#include <errno.h>
#include <threads.h>
// These values are used by reference-sysroot's dlmalloc.
const int __EINVAL = EINVAL;

View File

@ -1,40 +0,0 @@
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
size_t strlen(const char *str) {
const char *s = str;
while (*s) {
++s;
}
return s - str;
}
char *strdup(const char *str) {
size_t buf_len = strlen(str) + 1;
void *ptr = malloc(buf_len);
if (ptr == NULL) {
return NULL;
}
return memcpy(ptr, str, buf_len);
}
int strcmp(const char *a, const char *b) {
while (*a != '\0' && (*a == *b)) {
++a;
++b;
}
return *(const unsigned char*)a - *(const unsigned char*)b;
}
void *memchr(const void *ptr, int c, size_t len) {
const unsigned char *p = ptr;
while (len != 0 && *p != (unsigned char)c) {
++p;
--len;
}
if (len == 0) {
return NULL;
}
return (void *)p;
}