How to make rustc link to my version of memcpy, memset, etc

my crate type is staticlib. currently rustc links to glibc's function but i have my own. how do i link to them?

extern "C" {
    pub fn memcpy(dst: *mut void, src: *const void, len: usize) -> *const void;
    pub fn memset(str: *mut void, c: u8, len: usize) -> *const void;
    pub fn strchr(str: *const u8, c: u8) -> *const u8;
    pub fn strcmp(s1: *const u8, s2: *const u8) -> i32;
    pub fn strlen(str: *const u8) -> usize;
    pub fn strncmp(s1: *const u8, s2: *const u8, len: usize) -> i32;
}

These names are global, and I don't think you'll have enough fine-grained control over linker invocation to make yours take precedence.

Also LLVM knows about these functions and treats them as special, and may completely remove calls to these functions.

Your best bet is to use different names for these functions. You can use #[link_name = "my_memcpy] if you want Rust names to stay as-is, but change the C/linked names.

If you want to override these functions in some C code, use a #define that renames them.

whoopsies. i just realized rust have these in core already. i just spent a few learning asm and implementing these. damn

1 Like

Usually linkers favour first found and favour OBJs over LIBs.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.