From 084cf16f70effba7836fc72dac4df0b4792ea374 Mon Sep 17 00:00:00 2001 From: elliott10 Date: Fri, 10 May 2024 11:44:53 +0800 Subject: [PATCH] Define functions in libc named __attribute__((weak)) --- c/ulibc.c | 11 ++++++++++- src/lib.rs | 7 ++++--- src/ulibc.rs | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/c/ulibc.c b/c/ulibc.c index 298ac91..4a0d985 100644 --- a/c/ulibc.c +++ b/c/ulibc.c @@ -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__ @@ -36,12 +37,14 @@ 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++) @@ -49,6 +52,7 @@ int strcmp(const char *l, const char *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; @@ -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"); @@ -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; @@ -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, @@ -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); -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index b620d62..de97b91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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] diff --git a/src/ulibc.rs b/src/ulibc.rs index 49c816b..057ff40 100644 --- a/src/ulibc.rs +++ b/src/ulibc.rs @@ -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; } @@ -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; @@ -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); @@ -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() { @@ -78,6 +82,7 @@ struct MemoryControlBlock { const CTRL_BLK_SIZE: usize = core::mem::size_of::(); /// 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. @@ -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() {