Skip to content

Commit

Permalink
Define functions in libc named __attribute__((weak))
Browse files Browse the repository at this point in the history
  • Loading branch information
elliott10 committed May 10, 2024
1 parent b6e4d01 commit 084cf16
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 10 additions & 1 deletion c/ulibc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define HIGHS (ONES * (UCHAR_MAX / 2 + 1))
#define HASZERO(x) (((x) - ONES) & ~(x) & HIGHS)

__attribute__((weak))
char *__stpcpy(char *restrict d, const char *restrict s)
{
#ifdef __GNUC__
Expand All @@ -36,19 +37,22 @@ char *__stpcpy(char *restrict d, const char *restrict s)
return d;
}

__attribute__((weak))
char *strcpy(char *restrict dest, const char *restrict src)
{
__stpcpy(dest, src);
return dest;
}

__attribute__((weak))
int strcmp(const char *l, const char *r)
{
for (; *l == *r && *l; l++, r++)
;
return *(unsigned char *)l - *(unsigned char *)r;
}

__attribute__((weak))
int strncmp(const char *_l, const char *_r, size_t n)
{
const unsigned char *l = (void *)_l, *r = (void *)_r;
Expand All @@ -60,8 +64,10 @@ int strncmp(const char *_l, const char *_r, size_t n)
}

// fix me
__attribute__((weak))
FILE *const stdout = NULL;

__attribute__((weak))
int fflush(FILE *f)
{
// printf("fflush() is not implemented !\n");
Expand All @@ -70,6 +76,7 @@ int fflush(FILE *f)

// +++++++++ uClibc +++++++++

__attribute__((weak))
void *memset(void *s, int c, size_t n)
{
register unsigned char *p = (unsigned char *)s;
Expand All @@ -84,6 +91,7 @@ void *memset(void *s, int c, size_t n)
// musl, typedef int (*cmpfun)(const void *, const void *);
typedef int (*__compar_fn_t)(const void *, const void *);
typedef int (*__compar_d_fn_t)(const void *, const void *, void *);
__attribute__((weak))
void qsort_r(void *base,
size_t nel,
size_t width,
Expand Down Expand Up @@ -140,10 +148,11 @@ void qsort_r(void *base,
}
}

__attribute__((weak))
void qsort(void *base,
size_t nel,
size_t width,
__compar_fn_t comp)
{
return qsort_r(base, nel, width, (__compar_d_fn_t)comp, NULL);
}
}
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![no_std]
#![feature(linkage)]
#![feature(c_variadic, c_size_t)]
#![feature(associated_type_defaults)]

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

#![feature(c_variadic, c_size_t)]
#![feature(associated_type_defaults)]

extern crate alloc;

#[macro_use]
Expand Down
6 changes: 6 additions & 0 deletions src/ulibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::cmp::min;
use core::ffi::{c_char, c_int, c_size_t, c_void};

#[cfg(feature = "print")]
#[linkage = "weak"]
#[no_mangle]
unsafe extern "C" fn printf(str: *const c_char, mut args: ...) -> c_int {
// extern "C" { pub fn printf(arg1: *const c_char, ...) -> c_int; }
Expand All @@ -19,6 +20,7 @@ unsafe extern "C" fn printf(str: *const c_char, mut args: ...) -> c_int {
}

#[cfg(not(feature = "print"))]
#[linkage = "weak"]
#[no_mangle]
unsafe extern "C" fn printf(str: *const c_char, mut args: ...) -> c_int {
use core::ffi::CStr;
Expand All @@ -34,6 +36,7 @@ pub extern "C" fn ext4_user_malloc(size: c_size_t) -> *mut c_void {
malloc(size)
}

#[linkage = "weak"]
#[no_mangle]
pub extern "C" fn calloc(m: c_size_t, n: c_size_t) -> *mut c_void {
let mem = malloc(m * n);
Expand All @@ -44,6 +47,7 @@ pub extern "C" fn calloc(m: c_size_t, n: c_size_t) -> *mut c_void {
unsafe { memset(mem, 0, m * n) }
}

#[linkage = "weak"]
#[no_mangle]
pub extern "C" fn realloc(memblock: *mut c_void, size: c_size_t) -> *mut c_void {
if memblock.is_null() {
Expand Down Expand Up @@ -78,6 +82,7 @@ struct MemoryControlBlock {
const CTRL_BLK_SIZE: usize = core::mem::size_of::<MemoryControlBlock>();

/// Allocate size bytes memory and return the memory address.
#[linkage = "weak"]
#[no_mangle]
pub extern "C" fn malloc(size: c_size_t) -> *mut c_void {
// Allocate `(actual length) + 8`. The lowest 8 Bytes are stored in the actual allocated space size.
Expand All @@ -94,6 +99,7 @@ pub extern "C" fn malloc(size: c_size_t) -> *mut c_void {
}

/// Deallocate memory at ptr address
#[linkage = "weak"]
#[no_mangle]
pub extern "C" fn free(ptr: *mut c_void) {
if ptr.is_null() {
Expand Down

0 comments on commit 084cf16

Please sign in to comment.