Skip to content

Commit 265939f

Browse files
author
Jorge Aparicio
committed
add weak memcpy et al symbols
closes #28
1 parent adfb1ff commit 265939f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#![allow(unused_features)]
22
#![feature(asm)]
33
#![feature(core_intrinsics)]
4+
#![feature(linkage)]
45
#![feature(naked_functions)]
56
#![cfg_attr(not(test), no_std)]
67
// TODO(rust-lang/rust#35021) uncomment when that PR lands
78
// #![feature(rustc_builtins)]
9+
#![no_builtins]
810

911
// We disable #[no_mangle] for tests so that we can verify the test results
1012
// against the native compiler-rt implementations of the builtins.
@@ -20,6 +22,7 @@ extern crate core;
2022
pub mod arm;
2123

2224
pub mod udiv;
25+
pub mod mem;
2326
pub mod mul;
2427
pub mod shift;
2528

src/mem.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// NOTE Copied verbatim from the rlibc crate
2+
// cf. https://crates.io/crates/rlibc
3+
4+
#[linkage = "weak"]
5+
#[no_mangle]
6+
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
7+
let mut i = 0;
8+
while i < n {
9+
*dest.offset(i as isize) = *src.offset(i as isize);
10+
i += 1;
11+
}
12+
dest
13+
}
14+
15+
#[linkage = "weak"]
16+
#[no_mangle]
17+
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
18+
if src < dest as *const u8 {
19+
// copy from end
20+
let mut i = n;
21+
while i != 0 {
22+
i -= 1;
23+
*dest.offset(i as isize) = *src.offset(i as isize);
24+
}
25+
} else {
26+
// copy from beginning
27+
let mut i = 0;
28+
while i < n {
29+
*dest.offset(i as isize) = *src.offset(i as isize);
30+
i += 1;
31+
}
32+
}
33+
dest
34+
}
35+
36+
#[linkage = "weak"]
37+
#[no_mangle]
38+
pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
39+
let mut i = 0;
40+
while i < n {
41+
*s.offset(i as isize) = c as u8;
42+
i += 1;
43+
}
44+
s
45+
}
46+
47+
#[linkage = "weak"]
48+
#[no_mangle]
49+
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
50+
let mut i = 0;
51+
while i < n {
52+
let a = *s1.offset(i as isize);
53+
let b = *s2.offset(i as isize);
54+
if a != b {
55+
return a as i32 - b as i32;
56+
}
57+
i += 1;
58+
}
59+
0
60+
}

0 commit comments

Comments
 (0)