libc: better support for c++ builds

This commit is contained in:
Mark Poliakov 2024-11-15 11:14:08 +02:00
parent 98ea969675
commit bc6a5b115c
31 changed files with 370 additions and 15 deletions

View File

@ -80,7 +80,7 @@ unsafe impl<T: Place> Place for [T] {
/// Automatically implements the [Place] interface for a struct, properly handling any nested
/// references
#[macro_export]
macro_rules! impl_place_lifetime {
macro_rules! impl_place_lifetime_struct {
(
$( #[ $struct_meta:meta ] )*
$struct_vis:vis struct $struct_name:ident<$struct_lifetime:lifetime> {

View File

@ -3,7 +3,7 @@
use core::{fmt, time::Duration};
use crate::{
impl_place_lifetime,
impl_place_lifetime_struct,
io::{FileMode, RawFd},
};
@ -54,7 +54,7 @@ pub enum ProcessInfoElement {
}
// TODO not sure if I really need #[repr(C)] ABI here
impl_place_lifetime! {
impl_place_lifetime_struct! {
#[doc = "Argument struct passed from the kernel to a spawned process"]
#[derive(Debug)]
pub struct ProgramArgumentInner<'a> {

View File

@ -50,6 +50,7 @@ fn generate_header(config_path: impl AsRef<Path>, header_output: impl AsRef<Path
.export
.rename
.extend(RENAMES.into_iter().map(|&(x, y)| (x.into(), y.into())));
config.cpp_compat = true;
cbindgen::Builder::new()
.with_config(config)

View File

@ -7,7 +7,7 @@
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
"max-atomic-width": 64,
"target-pointer-width": "64",
"features": "+sse",
"features": "+sse,-soft-float",
"disable-redzone": true,
"executables": true,

View File

@ -6,6 +6,14 @@
#define assert(expr) \
((expr) ? __ASSERT_VOID_CAST(0) : __assert_fail(__FILE__, __LINE__, #expr))
#if defined(__cplusplus)
extern "C" {
#endif
[[noreturn]] void __assert_fail(const char *file, int line, const char *message);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,8 +1,16 @@
#ifndef _YGGDRASIL_DIRENT
#define _YGGDRASIL_DIRENT 1
#if defined(__cplusplus)
extern "C" {
#endif
struct __DIR;
typedef struct __DIR DIR;
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,6 +1,14 @@
#ifndef _YGGDRASIL_ERRNO_H
#define _YGGDRASIL_ERRNO_H 1
#if defined(__cplusplus)
extern "C" {
#endif
extern int errno;
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,8 +1,16 @@
#ifndef _YGGDRASIL_FCNTL_H
#define _YGGDRASIL_FCNTL_H 1
#if defined(__cplusplus)
extern "C" {
#endif
int fcntl(int fd, int cmd, ...);
int open(const char *pathname, int opts, ...);
int openat(int atfd, const char *pathname, int opts, ...);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,7 +1,15 @@
#ifndef _YGGDRASIL_MONETARY_H
#define _YGGDRASIL_MONETARY_H 1
#if defined(__cplusplus)
extern "C" {
#endif
ssize_t strfmon(char *buf, size_t buflen, const char *fmt, ...);
ssize_t strfmon_l(char *buf, size_t buflen, locale_t locale, const char *fmt, ...);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -0,0 +1,6 @@
#ifndef _YGGDRASIL_SETJMP_H
#define _YGGDRASIL_SETJMP_H 1
#endif

View File

@ -3,6 +3,10 @@
#include <stdatomic.h>
#if defined(__cplusplus)
extern "C" {
#endif
typedef _Atomic int sig_atomic_t;
#define SIG_IGN __sig_ignore
@ -13,4 +17,8 @@ typedef _Atomic int sig_atomic_t;
#define SIG_DFL ((sig_handler_t) 0)
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,6 +1,10 @@
#ifndef _YGGDRASIL_STDIO_H
#define _YGGDRASIL_STDIO_H 1
#if defined(__cplusplus)
extern "C" {
#endif
struct __FILE;
typedef struct __FILE FILE;
@ -8,10 +12,16 @@ extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
int asprintf(char **strp, const char *fmt, ...);
int dprintf(int fd, const char *fmt, ...);
int fprintf(FILE *fp, const char *fmt, ...);
int printf(const char *fmt, ...);
int snprintf(char *buf, size_t len, const char *fmt, ...);
int sprintf(char *buf, const char *fmt, ...);
int sscanf(const char *str, const char *fmt, ...);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -0,0 +1,14 @@
#ifndef _YGGDRASIL_STDLIB_H
#define _YGGDRASIL_STDLIB_H 1
#if defined(__cplusplus)
extern "C" {
#endif
[[noreturn]] void abort(void);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,6 +1,14 @@
#ifndef _YGGDRASIL_TIME_H
#define _YGGDRASIL_TIME_H 1
#if defined(__cplusplus)
extern "C" {
#endif
typedef struct timespec __ygg_timespec_t;
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,6 +1,14 @@
#ifndef _YGGDRASIL_ULIMIT_H
#define _YGGDRASIL_ULIMIT_H 1
#if defined(__cplusplus)
extern "C" {
#endif
long ulimit(int which, ...);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,10 +1,18 @@
#ifndef _YGGDRASIL_UNISTD_H
#define _YGGDRASIL_UNISTD_H 1
#if defined(__cplusplus)
extern "C" {
#endif
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ...);
extern char **environ;
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,6 +1,10 @@
#ifndef _YGGDRASIL_WCHAR_H
#define _YGGDRASIL_WCHAR_H 1
#if defined(__cplusplus)
extern "C" {
#endif
int fwprintf(FILE *fp, const wchar_t *fmt, ...);
int fwscanf(FILE *fp, const wchar_t *fmt, ...);
int swprintf(wchar_t *buf, size_t len, const wchar_t *fmt, ...);
@ -8,4 +12,8 @@ int swscanf(const wchar_t *buf, const wchar_t *fmt, ...);
int wprintf(const wchar_t *fmt, ...);
int wscanf(const wchar_t *fmt, ...);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -0,0 +1,16 @@
#ifndef _YGGDRASIL_WCTYPE_H
#define _YGGDRASIL_WCTYPE_H 1
#if defined(__cplusplus)
extern "C" {
#endif
#if !defined(wctype_t)
typedef int wctype_t;
#endif
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -6,9 +6,17 @@
#define RTLD_GLOBAL (1 << 1)
#define RTLD_LOCAL (0 << 1)
#if defined(__cplusplus)
extern "C" {
#endif
int dlclose(void *ptr);
char *dlerror(void);
void *dlopen(const char *path, int mode);
void *dlsym(void *handle, const char *sym);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -0,0 +1,87 @@
use core::ffi::{c_int, c_void};
#[no_mangle]
unsafe extern "C" fn __cxa_begin_catch(_exception: *mut c_void) -> *mut c_void {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn __cxa_end_catch() {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn __cxa_pure_virtual() {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn __cxa_guard_acquire(_guard: *mut u64) -> c_int {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn __cxa_guard_release(_guard: *mut u64) {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn __cxa_atexit(
_destructor: extern "C" fn(*mut c_void),
_arg: *mut c_void,
_dso_handle: *mut c_void,
) -> c_int {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn __gxx_personality_v0() {
unimplemented!()
}
#[no_mangle]
unsafe extern "C" fn _Unwind_Resume(_exception: *mut c_void) {
unimplemented!()
}
// void *operator new(size_t x)
#[no_mangle]
unsafe extern "C" fn _Znwm(_x: usize) -> *mut c_void {
unimplemented!()
}
// void *operator new(size_t x, std::align_val_t y)
#[no_mangle]
unsafe extern "C" fn _ZnwmSt11align_val_t(_x: usize, _y: usize) -> *mut c_void {
unimplemented!()
}
// void *operator new[](size_t x)
#[no_mangle]
unsafe extern "C" fn _Znam(_x: usize) -> *mut c_void {
unimplemented!()
}
// void operator delete(void *x)
#[no_mangle]
unsafe extern "C" fn _ZdlPv(_x: *mut c_void) {
unimplemented!()
}
// void operator delete(void *x, size_t y)
#[no_mangle]
unsafe extern "C" fn _ZdlPvm(_x: *mut c_void, _y: usize) {
unimplemented!()
}
// void operator delete(void *x, size_t y, std::align_val_t z)
#[no_mangle]
unsafe extern "C" fn _ZdlPvmSt11align_val_t(_x: *mut c_void, _y: usize) {
unimplemented!()
}
// void operator delete[](void *x)
#[no_mangle]
unsafe extern "C" fn _ZdaPv(_x: *mut c_void) {
unimplemented!()
}

View File

@ -49,6 +49,15 @@ pub const LC_MONETARY: c_int = 3;
pub const LC_NUMERIC: c_int = 4;
pub const LC_TIME: c_int = 5;
pub const LC_COLLATE_MASK: c_int = 0;
pub const LC_CTYPE_MASK: c_int = 0;
pub const LC_MESSAGES_MASK: c_int = 0;
pub const LC_MONETARY_MASK: c_int = 0;
pub const LC_NUMERIC_MASK: c_int = 0;
pub const LC_TIME_MASK: c_int = 0;
pub const LC_ALL_MASK: c_int = 0;
pub const LC_ALL: c_int = 6;
pub const __LC_LAST: usize = 6;

View File

@ -20,6 +20,7 @@ isize_type = "ssize_t"
[export]
include = ["fpos_t"]
exclude = [
"asprintf",
"printf",
"fprintf",
"dprintf",

View File

@ -185,6 +185,16 @@ fn printf_inner<W: FmtWriter>(output: &mut W, format: &[u8], mut ap: VaList) ->
EResult::Ok(count)
}
#[no_mangle]
unsafe extern "C" fn asprintf(dst: *mut *mut c_char, fmt: *const c_char, mut args: ...) -> c_int {
vasprintf(dst, fmt, args.as_va_list())
}
#[no_mangle]
unsafe extern "C" fn vasprintf(_dst: *mut *mut c_char, _fmt: *const c_char, args: VaList) -> c_int {
todo!()
}
#[no_mangle]
unsafe extern "C" fn dprintf(fd: c_int, fmt: *const c_char, mut args: ...) -> CIntCountResult {
vdprintf(fd, fmt, args.as_va_list())

View File

@ -2,7 +2,9 @@ language = "C"
style = "Type"
sys_includes = [
"stddef.h"
"stddef.h",
"locale.h",
"bits/stdlib.h"
]
no_includes = true
@ -10,3 +12,6 @@ include_guard = "_STDLIB_H"
usize_type = "size_t"
isize_type = "ssize_t"
[export]
exclude = ["abort"]

View File

@ -3,7 +3,7 @@ use core::{
ptr::{null_mut, NonNull},
};
use crate::util::cstr_matches_insensitive;
use crate::{headers::locale::locale_t, util::cstr_matches_insensitive};
// base64
@ -84,6 +84,55 @@ unsafe extern "C" fn strtoull(
str_to_uint_ext(s, endptr, base)
}
// locale
#[no_mangle]
unsafe extern "C" fn strtoll_l(
_s: *const c_char,
_endptr: *mut *mut c_char,
_base: c_int,
_locale: locale_t,
) -> c_longlong {
todo!()
}
#[no_mangle]
unsafe extern "C" fn strtoull_l(
_s: *const c_char,
_endptr: *mut *mut c_char,
_base: c_int,
_locale: locale_t,
) -> c_ulonglong {
todo!()
}
#[no_mangle]
unsafe extern "C" fn strtof_l(
_s: *const c_char,
_endptr: *mut *mut c_char,
_locale: locale_t,
) -> c_float {
todo!()
}
#[no_mangle]
unsafe extern "C" fn strtod_l(
_s: *const c_char,
_endptr: *mut *mut c_char,
_locale: locale_t,
) -> c_double {
todo!()
}
#[no_mangle]
unsafe extern "C" fn strtold_l(
_s: *const c_char,
_endptr: *mut *mut c_char,
_locale: locale_t,
) -> c_double {
todo!()
}
#[derive(Clone, Copy, PartialEq)]
enum Radix {
Oct,

View File

@ -1,6 +1,28 @@
use core::ffi::{c_char, c_int};
// int mblen(const char *, size_t);
// size_t mbstowcs(wchar_t *restrict, const char *restrict, size_t);
// int mbtowc(wchar_t *restrict, const char *restrict, size_t);
// size_t wcstombs(char *restrict, const wchar_t *restrict, size_t);
// int wctomb(char *, wchar_t);
use crate::types::wchar_t;
#[no_mangle]
unsafe extern "C" fn mblen(_s: *const c_char, _n: usize) -> c_int {
todo!()
}
#[no_mangle]
unsafe extern "C" fn mbstowcs(_dst: *mut wchar_t, _src: *const c_char, _n: usize) -> usize {
todo!()
}
#[no_mangle]
unsafe extern "C" fn mbtowc(_dst: *mut wchar_t, _src: *const c_char, _n: usize) -> c_int {
todo!()
}
#[no_mangle]
unsafe extern "C" fn wcstombs(_dst: *mut c_char, _src: *const wchar_t, _n: usize) -> usize {
todo!()
}
#[no_mangle]
unsafe extern "C" fn wctomb(_dst: *mut c_char, _wc: wchar_t) -> c_int {
todo!()
}

View File

@ -1,4 +1,4 @@
use core::ffi::c_long;
use core::ffi::{c_int, c_long, c_void};
use super::sys_types::{suseconds_t, time_t};
@ -16,5 +16,31 @@ pub struct timespec {
pub tv_nsec: c_long,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct itimerval {
pub it_interval: timeval,
pub it_value: timeval
}
pub type __ygg_timeval_t = timeval;
pub type __ygg_timespec_t = timespec;
pub const ITIMER_REAL: c_int = 0;
pub const ITIMER_VIRTUAL: c_int = 1;
pub const ITIMER_PROF: c_int = 2;
#[no_mangle]
unsafe extern "C" fn getitimer(_timer: c_int, _value: *mut itimerval) -> c_int {
todo!()
}
#[no_mangle]
unsafe extern "C" fn gettimeofday(_time: *mut timeval, _d: *mut c_void) -> c_int {
todo!()
}
#[no_mangle]
unsafe extern "C" fn setitimer(_timer: c_int, _new: *const itimerval, _old: *mut itimerval) -> c_int {
todo!()
}

View File

@ -16,6 +16,7 @@ sys_includes = [
no_includes = true
include_guard = "_WCHAR_H"
trailer = "#include <bits/wchar.h>"
usize_type = "size_t"
isize_type = "ssize_t"

View File

@ -1,4 +1,6 @@
use core::ffi::c_void;
use core::ffi::{c_int, c_void};
use crate::types::wchar_t;
use super::wctype::wint_t;
@ -6,4 +8,9 @@ pub mod io;
pub mod multibyte;
pub mod string;
pub type mbstate_t = c_void;
pub const WEOF: wchar_t = -1;
#[repr(C)]
pub struct mbstate_t {
__dummy: c_int
}

View File

@ -2,6 +2,7 @@ language = "C"
style = "Type"
sys_includes = [
"bits/wctype.h",
"locale.h",
"ctype.h",
]
@ -13,4 +14,5 @@ usize_type = "size_t"
isize_type = "ssize_t"
[export]
include = ["wint_t", "wctype_t"]
include = ["wint_t"]
exclude = ["wctype_t"]

View File

@ -33,6 +33,7 @@ mod ssp;
mod sync;
mod types;
mod util;
mod cxxabi;
pub mod headers;