Skip to content

add weak memcpy et al (take 2) #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
authors = ["Jorge Aparicio <[email protected]>"]
name = "rustc_builtins"
version = "0.1.0"

[dependencies]
rlibc = { path = "rlibc" }
6 changes: 6 additions & 0 deletions rlibc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "rlibc"
version = "0.1.0"
authors = ["Jorge Aparicio <[email protected]>"]

[dependencies]
46 changes: 46 additions & 0 deletions rlibc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![feature(linkage)]
#![no_builtins]
#![no_std]

#[linkage = "weak"]
#[no_mangle]
pub unsafe extern fn memcpy(dest: *mut u8, src: *const u8,
n: usize) -> *mut u8 {
let mut i = 0;
while i < n {
*dest.offset(i as isize) = *src.offset(i as isize);
i += 1;
}
return dest;
}

#[linkage = "weak"]
#[no_mangle]
pub unsafe extern fn memmove(dest: *mut u8, src: *const u8,
n: usize) -> *mut u8 {
if src < dest as *const u8 { // copy from end
let mut i = n;
while i != 0 {
i -= 1;
*dest.offset(i as isize) = *src.offset(i as isize);
}
} else { // copy from beginning
let mut i = 0;
while i < n {
*dest.offset(i as isize) = *src.offset(i as isize);
i += 1;
}
}
return dest;
}

#[linkage = "weak"]
#[no_mangle]
pub unsafe extern fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
let mut i = 0;
while i < n {
*s.offset(i as isize) = c as u8;
i += 1;
}
return s;
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#[cfg(test)]
extern crate core;

extern crate rlibc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think an extern crate is necessary here since you're getting the symbols using extern "C".


use core::mem;

#[cfg(target_arch = "arm")]
Expand Down