Skip to content

Commit e74e089

Browse files
authored
Merge pull request rust-lang#339 from bjorn3/fix_libstd
Fix libstd
2 parents 4bb8bfc + 130fbe2 commit e74e089

File tree

9 files changed

+68
-8
lines changed

9 files changed

+68
-8
lines changed

build_sysroot/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.0.0"
77
core = { path = "./sysroot_src/src/libcore" }
88
compiler_builtins = "0.1"
99
alloc = { path = "./sysroot_src/src/liballoc" }
10+
std = { path = "./sysroot_src/src/libstd" }
1011

1112
alloc_system = { path = "./alloc_system" }
1213

example/mini_core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ pub mod libc {
338338
#[link(name = "c")]
339339
extern "C" {
340340
pub fn puts(s: *const u8);
341+
pub fn printf(format: *const i8, ...) -> i32;
341342
pub fn malloc(size: usize) -> *mut u8;
342343
pub fn free(ptr: *mut u8);
343344
pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);

example/mini_core_hello_world.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ fn main() {
121121
//return;
122122

123123
unsafe {
124+
printf("Hello %s\n\0" as *const str as *const i8, "printf\0" as *const str as *const i8);
125+
124126
let hello: &[u8] = b"Hello\0" as &[u8; 6];
125127
let ptr: *const u8 = hello as *const [u8] as *const u8;
126128
puts(ptr);

example/std_example.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::io::Write;
2+
3+
fn main() {
4+
let _ = ::std::iter::repeat('a' as u8).take(10).collect::<Vec<_>>();
5+
let stderr = ::std::io::stderr();
6+
let mut stderr = stderr.lock();
7+
8+
writeln!(stderr, "some {} text", "<unknown>").unwrap();
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From 2bc2ef06e118c6fba0626c0e9bf24fed873405b2 Mon Sep 17 00:00:00 2001
2+
From: bjorn3 <[email protected]>
3+
Date: Sat, 29 Dec 2018 12:37:34 +0100
4+
Subject: [PATCH] Workaround for libstd crash
5+
6+
I think this is related to the use of TLS inside those functions
7+
---
8+
src/libstd/rt.rs | 2 +-
9+
1 file changed, 1 insertions(+), 1 deletions(-)
10+
11+
diff --git a/src/libstd/rt.rs b/src/libstd/rt.rs
12+
index 5ddb66b..6a0d0b5 100644
13+
--- a/src/libstd/rt.rs
14+
+++ b/src/libstd/rt.rs
15+
@@ -51,7 +51,7 @@ fn lang_start_internal(main: &(dyn Fn() -> i32 + Sync + ::panic::RefUnwindSafe),
16+
#[cfg(not(feature = "backtrace"))]
17+
let exit_code = panic::catch_unwind(move || main());
18+
19+
- sys_common::cleanup();
20+
+ //sys_common::cleanup();
21+
exit_code.unwrap_or(101) as isize
22+
}
23+
}
24+
--
25+
2.17.2 (Apple Git-113)

src/abi.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,13 @@ pub fn ty_fn_sig<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> ty::FnS
191191
pub fn get_function_name_and_sig<'a, 'tcx>(
192192
tcx: TyCtxt<'a, 'tcx, 'tcx>,
193193
inst: Instance<'tcx>,
194+
support_vararg: bool
194195
) -> (String, Signature) {
195196
assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types());
196197
let fn_ty = inst.ty(tcx);
197198
let fn_sig = ty_fn_sig(tcx, fn_ty);
198-
if fn_sig.variadic {
199-
unimpl!("Variadic functions are not yet supported");
199+
if fn_sig.variadic && !support_vararg {
200+
unimpl!("Variadic function definitions are not yet supported");
200201
}
201202
let sig = clif_sig_from_fn_sig(tcx, fn_sig);
202203
(tcx.symbol_name(inst).as_str().to_string(), sig)
@@ -208,7 +209,7 @@ pub fn import_function<'a, 'tcx: 'a>(
208209
module: &mut Module<impl Backend>,
209210
inst: Instance<'tcx>,
210211
) -> FuncId {
211-
let (name, sig) = get_function_name_and_sig(tcx, inst);
212+
let (name, sig) = get_function_name_and_sig(tcx, inst, true);
212213
module
213214
.declare_function(&name, Linkage::Import, &sig)
214215
.unwrap()
@@ -659,6 +660,23 @@ pub fn codegen_call_inner<'a, 'tcx: 'a>(
659660
fx.bcx.ins().call(func_ref, &call_args)
660661
};
661662

663+
// FIXME find a cleaner way to support varargs
664+
if fn_sig.variadic {
665+
if fn_sig.abi != Abi::C {
666+
unimpl!("Variadic call for non-C abi {:?}", fn_sig.abi);
667+
}
668+
let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
669+
let abi_params = call_args.into_iter().map(|arg| {
670+
let ty = fx.bcx.func.dfg.value_type(arg);
671+
if !ty.is_int() {
672+
// FIXME set %al to upperbound on float args once floats are supported
673+
unimpl!("Non int ty {:?} for variadic call", ty);
674+
}
675+
AbiParam::new(ty)
676+
}).collect::<Vec<AbiParam>>();
677+
fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
678+
}
679+
662680
match output_pass_mode {
663681
PassMode::NoPass => {}
664682
PassMode::ByVal(_) => {

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
6565
let mir = tcx.instance_mir(instance.def);
6666

6767
// Step 2. Declare function
68-
let (name, sig) = get_function_name_and_sig(tcx, instance);
68+
let (name, sig) = get_function_name_and_sig(tcx, instance, false);
6969
let func_id = cx.module
7070
.declare_function(&name, linkage, &sig)
7171
.unwrap();

src/main_shim.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx: 'a>(
5353

5454
let instance = Instance::mono(tcx, rust_main_def_id);
5555

56-
let (main_name, main_sig) = get_function_name_and_sig(tcx, instance);
57-
56+
let (main_name, main_sig) = get_function_name_and_sig(tcx, instance, false);
5857
let main_func_id = m
5958
.declare_function(&main_name, Linkage::Import, &main_sig)
6059
.unwrap();

test.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ echo "[BUILD+RUN] alloc_example"
2424
$RUSTC --sysroot ./build_sysroot/sysroot example/alloc_example.rs --crate-type bin
2525
./target/out/alloc_example
2626

27+
echo "[BUILD+RUN] std_example"
28+
$RUSTC --sysroot ./build_sysroot/sysroot example/std_example.rs --crate-type bin
29+
./target/out/std_example
30+
2731
echo "[BUILD] mod_bench"
2832
$RUSTC --sysroot ./build_sysroot/sysroot example/mod_bench.rs --crate-type bin
2933

30-
echo "[BUILD] sysroot in release mode"
31-
./build_sysroot/build_sysroot.sh --release
34+
# FIXME linker gives multiple definitions error on Linux
35+
#echo "[BUILD] sysroot in release mode"
36+
#./build_sysroot/build_sysroot.sh --release
3237

3338
COMPILE_MOD_BENCH_INLINE="$RUSTC --sysroot ./build_sysroot/sysroot example/mod_bench.rs --crate-type bin -Zmir-opt-level=3 -O --crate-name mod_bench_inline"
3439
COMPILE_MOD_BENCH_LLVM_0="rustc example/mod_bench.rs --crate-type bin -Copt-level=0 -o target/out/mod_bench_llvm_0 -Cpanic=abort"

0 commit comments

Comments
 (0)