Skip to content

Commit 60bd7ac

Browse files
committed
Allow linking against crates with #[no_std]
Previously having optional lang_items caused an assertion failure at compile-time, and then once that was fixed there was a segfault at runtime of using a NULL crate-map (crates with no_std)
1 parent 3d13d4b commit 60bd7ac

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,9 @@ fn encode_lang_items(ecx: &EncodeContext, ebml_w: &mut writer::Encoder) {
14601460
ebml_w.start_tag(tag_lang_items);
14611461

14621462
for ecx.tcx.lang_items.each_item |def_id, i| {
1463+
let def_id = match def_id {
1464+
Some(id) => id, None => { loop }
1465+
};
14631466
if def_id.crate != local_crate {
14641467
loop;
14651468
}

src/librustc/middle/lang_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ impl LanguageItems {
9292
}
9393
}
9494

95-
pub fn each_item(&self, f: &fn(def_id: def_id, i: uint) -> bool) -> bool {
96-
self.items.iter().enumerate().advance(|(i, &item)| f(item.get(), i))
95+
pub fn each_item(&self, f: &fn(Option<def_id>, uint) -> bool) -> bool {
96+
self.items.iter().enumerate().advance(|(i, &item)| f(item, i))
9797
}
9898

9999
pub fn item_name(index: uint) -> &'static str {

src/rt/rust_crate_map.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class cratemap {
6868
return &reinterpret_cast<const cratemap_v0 *>(this)->
6969
m_children[0];
7070
case 1:
71-
return &m_children[1];
71+
return &m_children[0];
7272
default: assert(false && "Unknown crate map version!");
7373
return NULL; // Appease -Werror=return-type
7474
}

src/test/auxiliary/no_std_crate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[no_std];
2+
3+
pub fn foo() {}

src/test/run-pass/no-std-xcrate.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:no_std_crate.rs
13+
14+
// This tests that crates which link to std can also be linked to crates with
15+
// #[no_std] that have no lang items.
16+
17+
extern mod no_std_crate;
18+
19+
fn main() {
20+
no_std_crate::foo();
21+
}

src/test/run-pass/no-std-xcrate2.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:no_std_crate.rs
13+
14+
// This tests that libraries built with #[no_std] can be linked to crates with
15+
// #[no_std] and actually run.
16+
17+
#[no_std];
18+
19+
extern mod no_std_crate;
20+
21+
// This is an unfortunate thing to have to do on linux :(
22+
#[cfg(target_os = "linux")]
23+
#[doc(hidden)]
24+
pub mod linkhack {
25+
#[link_args="-lrustrt -lrt"]
26+
extern {}
27+
}
28+
29+
#[start]
30+
fn main(_: int, _: **u8, _: *u8) -> int {
31+
no_std_crate::foo();
32+
0
33+
}

0 commit comments

Comments
 (0)