From 265939fc003074e98696323690ad5cbafde96193 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 12 Aug 2016 21:20:56 -0500 Subject: [PATCH 1/7] add weak `memcpy` et al symbols closes #28 --- src/lib.rs | 3 +++ src/mem.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/mem.rs diff --git a/src/lib.rs b/src/lib.rs index 823170a..a802d1b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,12 @@ #![allow(unused_features)] #![feature(asm)] #![feature(core_intrinsics)] +#![feature(linkage)] #![feature(naked_functions)] #![cfg_attr(not(test), no_std)] // TODO(rust-lang/rust#35021) uncomment when that PR lands // #![feature(rustc_builtins)] +#![no_builtins] // We disable #[no_mangle] for tests so that we can verify the test results // against the native compiler-rt implementations of the builtins. @@ -20,6 +22,7 @@ extern crate core; pub mod arm; pub mod udiv; +pub mod mem; pub mod mul; pub mod shift; diff --git a/src/mem.rs b/src/mem.rs new file mode 100644 index 0000000..7a94034 --- /dev/null +++ b/src/mem.rs @@ -0,0 +1,60 @@ +// NOTE Copied verbatim from the rlibc crate +// cf. https://crates.io/crates/rlibc + +#[linkage = "weak"] +#[no_mangle] +pub unsafe extern "C" 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; + } + dest +} + +#[linkage = "weak"] +#[no_mangle] +pub unsafe extern "C" 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; + } + } + dest +} + +#[linkage = "weak"] +#[no_mangle] +pub unsafe extern "C" 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; + } + s +} + +#[linkage = "weak"] +#[no_mangle] +pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 { + let mut i = 0; + while i < n { + let a = *s1.offset(i as isize); + let b = *s2.offset(i as isize); + if a != b { + return a as i32 - b as i32; + } + i += 1; + } + 0 +} From fdbb7883da44d443c6461dc0f5baf91dd93cd8ee Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 13 Aug 2016 12:16:52 -0500 Subject: [PATCH 2/7] exclude windows and macos --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a802d1b..2d1c839 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,9 @@ #![feature(linkage)] #![feature(naked_functions)] #![cfg_attr(not(test), no_std)] +#![no_builtins] // TODO(rust-lang/rust#35021) uncomment when that PR lands // #![feature(rustc_builtins)] -#![no_builtins] // We disable #[no_mangle] for tests so that we can verify the test results // against the native compiler-rt implementations of the builtins. @@ -22,6 +22,7 @@ extern crate core; pub mod arm; pub mod udiv; +#[cfg(all(not(windows), not(target_os = "macos")))] pub mod mem; pub mod mul; pub mod shift; From 5abf6b5d7c1703b3bccd7781e64b96c5284609ec Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 13 Aug 2016 12:26:58 -0500 Subject: [PATCH 3/7] check presence of weak symbols --- ci/script.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ci/script.sh b/ci/script.sh index 3b00ff6..c271456 100644 --- a/ci/script.sh +++ b/ci/script.sh @@ -9,10 +9,21 @@ build() { inspect() { $PREFIX$NM -g --defined-only target/**/debug/*.rlib + set +e $PREFIX$OBJDUMP -Cd target/**/debug/*.rlib $PREFIX$OBJDUMP -Cd target/**/release/*.rlib set -e + + # Check presence of weak symbols + case $TRAVIS_OS_NAME in + linux) + local symbols=( memcmp memcpy memmove memset ) + for symbol in "${symbols[@]}"; do + $PREFIX$NM target/**/debug/*.rlib | grep -q "W $symbol" + done + ;; + esac } run_tests() { From 8965b061c234c18ecf3a488c39764d62b7f29086 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 14 Aug 2016 21:59:48 -0500 Subject: [PATCH 4/7] use rlibc crate instead --- Cargo.toml | 6 ++++++ src/lib.rs | 5 +++-- src/mem.rs | 60 ------------------------------------------------------ 3 files changed, 9 insertions(+), 62 deletions(-) delete mode 100644 src/mem.rs diff --git a/Cargo.toml b/Cargo.toml index dfd1fde..48f6ee0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,11 @@ authors = ["Jorge Aparicio "] name = "rustc_builtins" version = "0.1.0" +[dependencies] +rlibc = { git = "https://github.com/alexcrichton/rlibc", optional = true } + [dev-dependencies] quickcheck = "0.3.1" + +[features] +default = ["rlibc/weak"] diff --git a/src/lib.rs b/src/lib.rs index 2d1c839..7b621dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,12 +18,13 @@ extern crate quickcheck; #[cfg(test)] extern crate core; +#[cfg(all(not(windows), not(target_os = "macos")))] +extern crate rlibc; + #[cfg(target_arch = "arm")] pub mod arm; pub mod udiv; -#[cfg(all(not(windows), not(target_os = "macos")))] -pub mod mem; pub mod mul; pub mod shift; diff --git a/src/mem.rs b/src/mem.rs deleted file mode 100644 index 7a94034..0000000 --- a/src/mem.rs +++ /dev/null @@ -1,60 +0,0 @@ -// NOTE Copied verbatim from the rlibc crate -// cf. https://crates.io/crates/rlibc - -#[linkage = "weak"] -#[no_mangle] -pub unsafe extern "C" 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; - } - dest -} - -#[linkage = "weak"] -#[no_mangle] -pub unsafe extern "C" 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; - } - } - dest -} - -#[linkage = "weak"] -#[no_mangle] -pub unsafe extern "C" 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; - } - s -} - -#[linkage = "weak"] -#[no_mangle] -pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 { - let mut i = 0; - while i < n { - let a = *s1.offset(i as isize); - let b = *s2.offset(i as isize); - if a != b { - return a as i32 - b as i32; - } - i += 1; - } - 0 -} From 55eb1b71a087bd071a178645188502b4a7491a04 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 14 Aug 2016 22:08:36 -0500 Subject: [PATCH 5/7] remove unused feature gates --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7b621dd..ed4916f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,4 @@ -#![allow(unused_features)] #![feature(asm)] -#![feature(core_intrinsics)] #![feature(linkage)] #![feature(naked_functions)] #![cfg_attr(not(test), no_std)] From 94ab3e09c51ac6c3f4aee5104826c1eb140cdf3c Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 15 Aug 2016 20:22:58 -0500 Subject: [PATCH 6/7] adjust test: weak symbols are now in librlibc.rlib --- ci/script.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/script.sh b/ci/script.sh index c271456..7558478 100644 --- a/ci/script.sh +++ b/ci/script.sh @@ -20,7 +20,7 @@ inspect() { linux) local symbols=( memcmp memcpy memmove memset ) for symbol in "${symbols[@]}"; do - $PREFIX$NM target/**/debug/*.rlib | grep -q "W $symbol" + $PREFIX$NM target/**/debug/deps/librlibc*.rlib | grep -q "W $symbol" done ;; esac From ac352a3512a0d5908bf0efc9550cb213f48843db Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 15 Aug 2016 21:08:04 -0500 Subject: [PATCH 7/7] add core_intrinsics feature gate --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index ed4916f..2263fda 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![feature(asm)] +#![feature(core_intrinsics)] #![feature(linkage)] #![feature(naked_functions)] #![cfg_attr(not(test), no_std)]