Skip to content

Commit fd83b2b

Browse files
committed
auto merge of #12408 : alexcrichton/rust/manual-crate-map, r=brson
Apparently weak linkage and dlopen aren't quite working out for applications like servo on android. There appears to be a bug or two in how android loads dynamic libraries and for some reason libservo.so isn't being found. As a temporary solution, add an extern "C" function to libstd which can be called if you have a handle to the crate map manually. When crawling the crate map, we then check this manual symbol before falling back to the old solutions. cc #11731
2 parents 3276090 + 1b3b273 commit fd83b2b

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

src/libstd/rt/crate_map.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use cast;
1112
use cmp::TotalOrd;
1213
use container::MutableSet;
1314
use iter::Iterator;
@@ -35,27 +36,61 @@ pub struct CrateMap<'a> {
3536
event_loop_factory: Option<fn() -> ~EventLoop>,
3637
}
3738

39+
// When working on android, apparently weak symbols don't work so well for
40+
// finding the crate map, and neither does dlopen + dlsym. This is mainly a
41+
// problem when integrating a shared library with an existing application.
42+
// Standalone binaries do not appear to have this problem. The reasons are a
43+
// little mysterious, and more information can be found in #11731.
44+
//
45+
// For now we provide a way to tell libstd about the crate map manually that's
46+
// checked before the normal weak symbol/dlopen paths. In theory this is useful
47+
// on other platforms where our dlopen/weak linkage strategy mysteriously fails
48+
// but the crate map can be specified manually.
49+
static mut MANUALLY_PROVIDED_CRATE_MAP: *CrateMap<'static> =
50+
0 as *CrateMap<'static>;
51+
#[no_mangle]
52+
#[cfg(not(test))]
53+
pub extern fn rust_set_crate_map(map: *CrateMap<'static>) {
54+
unsafe { MANUALLY_PROVIDED_CRATE_MAP = map; }
55+
}
56+
57+
fn manual_crate_map() -> Option<&'static CrateMap<'static>> {
58+
unsafe {
59+
if MANUALLY_PROVIDED_CRATE_MAP.is_null() {
60+
None
61+
} else {
62+
Some(cast::transmute(MANUALLY_PROVIDED_CRATE_MAP))
63+
}
64+
}
65+
}
66+
3867
#[cfg(not(windows))]
3968
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
4069
extern {
4170
#[crate_map]
4271
static CRATE_MAP: CrateMap<'static>;
4372
}
4473

45-
let ptr: (*CrateMap) = &'static CRATE_MAP;
46-
if ptr.is_null() {
47-
return None;
48-
} else {
49-
return Some(&'static CRATE_MAP);
50-
}
74+
manual_crate_map().or_else(|| {
75+
let ptr: (*CrateMap) = &'static CRATE_MAP;
76+
if ptr.is_null() {
77+
None
78+
} else {
79+
Some(&'static CRATE_MAP)
80+
}
81+
})
5182
}
5283

5384
#[cfg(windows)]
5485
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
55-
use cast::transmute;
5686
use c_str::ToCStr;
5787
use unstable::dynamic_lib::dl;
5888

89+
match manual_crate_map() {
90+
Some(cm) => return Some(cm),
91+
None => {}
92+
}
93+
5994
let sym = unsafe {
6095
let module = dl::open_internal();
6196
let rust_crate_map_toplevel = if cfg!(target_arch = "x86") {
@@ -74,7 +109,7 @@ pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
74109
return None;
75110
} else {
76111
unsafe {
77-
return Some(transmute(sym));
112+
return Some(cast::transmute(sym));
78113
}
79114
}
80115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) lib.rs -C gen-crate-map
5+
ln -nsf $(call DYLIB,boot-*) $(call DYLIB,boot)
6+
$(CC) main.c -o $(call RUN,main) -lboot -Wl,-rpath,$(TMPDIR)
7+
RUST_LOG=boot $(call RUN,main)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2014 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+
#[crate_id="boot#0.1"];
12+
#[crate_type="dylib"];
13+
#[no_uv];
14+
15+
extern crate rustuv;
16+
extern crate green;
17+
18+
use std::rt::crate_map::{CrateMap, rust_set_crate_map};
19+
20+
// pull in this symbol from libstd into this crate (for convenience)
21+
#[no_mangle]
22+
pub static set_crate_map: extern "C" fn(*CrateMap<'static>) = rust_set_crate_map;
23+
24+
#[no_mangle] // this needs to get called from C
25+
pub extern "C" fn foo(argc: int, argv: **u8) -> int {
26+
green::start(argc, argv, proc() {
27+
if log_enabled!(std::logging::DEBUG) { return }
28+
fail!()
29+
})
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2014 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+
// this is the rust entry point that we're going to call.
12+
int foo(int argc, char *argv[]);
13+
14+
extern void (*set_crate_map)(void *map);
15+
extern int _rust_crate_map_toplevel;
16+
17+
int main(int argc, char *argv[]) {
18+
set_crate_map(&_rust_crate_map_toplevel);
19+
return foo(argc, argv);
20+
}

0 commit comments

Comments
 (0)