diff --git a/Cargo.lock b/Cargo.lock index 6b4f16075e..06c311a618 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,6 +263,7 @@ dependencies = [ name = "relibc" version = "0.1.0" dependencies = [ + "cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins.git)", "ctype 0.1.0", "errno 0.1.0", diff --git a/Cargo.toml b/Cargo.toml index 3cfc826fad..516799ba8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ crate-type = ["staticlib"] [workspace] members = ["src/crt0"] +[build-dependencies] +cc = "1.0" + [dependencies] compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] } ctype = { path = "src/ctype" } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000000..d87317ad56 --- /dev/null +++ b/build.rs @@ -0,0 +1,18 @@ +extern crate cc; + +use std::env; + +fn main() { + let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); + + cc::Build::new() + .flag("-nostdinc") + .flag("-nostdlib") + .flag("-I").flag(&format!("{}/include", crate_dir)) + .flag("-fno-stack-protector") + .file("src/c/fcntl.c") + .file("src/c/stdio.c") + .compile("relibc_c"); + + println!("cargo:rustc-link-lib=static=relibc_c"); +} diff --git a/include/assert.h b/include/assert.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/include/bits/fcntl.h b/include/bits/fcntl.h index 9c4a13b73f..fff8987b23 100644 --- a/include/bits/fcntl.h +++ b/include/bits/fcntl.h @@ -1,22 +1,7 @@ #ifndef _BITS_FCNTL_H #define _BITS_FCNTL_H -int open(const char* filename, int flags, ...) { - mode_t mode = 0; - va_list ap; - va_start(ap, flags); - mode = va_arg(ap, mode_t); - va_end(ap); - return sys_open(filename, flags, mode); -} - -int fcntl(int fildes, int cmd, ...) { - int args = 0; - va_list ap; - va_start(ap, cmd); - args = va_arg(ap, int); - va_end(ap); - return sys_fcntl(fildes, cmd, args); -} +int open(const char* filename, int flags, ...); +int fcntl(int fildes, int cmd, ...); #endif diff --git a/include/bits/stdio.h b/include/bits/stdio.h index d990dae61b..03253c3fd2 100644 --- a/include/bits/stdio.h +++ b/include/bits/stdio.h @@ -1,40 +1,9 @@ #ifndef _BITS_STDIO_H #define _BITS_STDIO_H -int fprintf(FILE * stream, const char * fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = vfprintf(stream, fmt, ap); - va_end(ap); - return ret; -} - -int printf(const char * fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = vprintf(fmt, ap); - va_end(ap); - return ret; -} - -int snprintf(char *s, size_t n, const char * fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = vsnprintf(s, n, fmt, ap); - va_end(ap); - return ret; -} - -int sprintf(char *s, const char * fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = vsprintf(s, fmt, ap); - va_end(ap); - return ret; -} +int fprintf(FILE * stream, const char * fmt, ...); +int printf(const char * fmt, ...); +int snprintf(char *s, size_t n, const char * fmt, ...); +int sprintf(char *s, const char * fmt, ...); #endif /* _BITS_STDIO_H */ diff --git a/include/bits/string.h b/include/bits/string.h index f3a5b16843..c62f315faa 100644 --- a/include/bits/string.h +++ b/include/bits/string.h @@ -1,9 +1,9 @@ -#ifndef _BITS_STDIO_H -#define _BITS_STDIO_H +#ifndef _BITS_STRING_H +#define _BITS_STRING_H int memcmp(const void *s1, const void *s2, size_t n); void *memcpy(void *dest, const void *src, size_t n); void *memmove(void *dest, const void *src, size_t n); void *memset(void *s, int c, size_t n); -#endif /* _BITS_STDIO_H */ +#endif /* _BITS_STRING_H */ diff --git a/src/c/fcntl.c b/src/c/fcntl.c new file mode 100644 index 0000000000..9aea06f027 --- /dev/null +++ b/src/c/fcntl.c @@ -0,0 +1,24 @@ +#include +#include + +int sys_open(const char* filename, int flags, mode_t mode); + +int open(const char* filename, int flags, ...) { + mode_t mode = 0; + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + return sys_open(filename, flags, mode); +} + +int sys_fcntl(int fildes, int cmd, int args); + +int fcntl(int fildes, int cmd, ...) { + int args = 0; + va_list ap; + va_start(ap, cmd); + args = va_arg(ap, int); + va_end(ap); + return sys_fcntl(fildes, cmd, args); +} diff --git a/src/c/stdio.c b/src/c/stdio.c new file mode 100644 index 0000000000..9e0bc9a251 --- /dev/null +++ b/src/c/stdio.c @@ -0,0 +1,48 @@ +#include +#include + +typedef struct FILE FILE; + +int vfprintf(FILE * stream, const char * fmt, va_list ap); + +int fprintf(FILE * stream, const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vfprintf(stream, fmt, ap); + va_end(ap); + return ret; +} + +int vprintf(const char * fmt, va_list ap); + +int printf(const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vprintf(fmt, ap); + va_end(ap); + return ret; +} + +int vsnprintf(char * s, size_t n, const char * fmt, va_list ap); + +int snprintf(char * s, size_t n, const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsnprintf(s, n, fmt, ap); + va_end(ap); + return ret; +} + +int vsprintf(char * s, const char * fmt, va_list ap); + +int sprintf(char *s, const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsprintf(s, fmt, ap); + va_end(ap); + return ret; +}