Skip to content

Commit af78e23

Browse files
committed
auto merge of #7958 : kemurphy/rust/link-section, r=alexcrichton
This allows for control over the section placement of static, static mut, and fn items. One caveat is that if a static and a static mut are placed in the same section, the static is declared first, and the static mut is assigned to, the generated program crashes. For example: #[link_section=".boot"] static foo : uint = 0xdeadbeef; #[link_section=".boot"] static mut bar : uint = 0xcafebabe; Declaring bar first would mark .bootdata as writable, preventing the crash when bar is written to.
2 parents 359755a + c6c1472 commit af78e23

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/librustc/middle/trans/base.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::node_id) -> ValueRef {
24492449
let val = match item {
24502450
ast_map::node_item(i, pth) => {
24512451
let my_path = vec::append((*pth).clone(), [path_name(i.ident)]);
2452-
match i.node {
2452+
let v = match i.node {
24532453
ast::item_static(_, m, expr) => {
24542454
let typ = ty::node_id_to_type(ccx.tcx, i.id);
24552455
let s = mangle_exported_name(ccx, my_path, typ);
@@ -2481,7 +2481,16 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::node_id) -> ValueRef {
24812481
llfn
24822482
}
24832483
_ => fail!("get_item_val: weird result in table")
2484+
};
2485+
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
2486+
Some(sect) => unsafe {
2487+
do sect.as_c_str |buf| {
2488+
llvm::LLVMSetSection(v, buf);
2489+
}
2490+
},
2491+
None => ()
24842492
}
2493+
v
24852494
}
24862495
ast_map::node_trait_method(trait_method, _, pth) => {
24872496
debug!("get_item_val(): processing a node_trait_method");

src/test/run-pass/link-section.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#[cfg(not(target_os = "macos"))]
2+
#[link_section=".moretext"]
3+
fn i_live_in_more_text() -> &'static str {
4+
"knock knock"
5+
}
6+
7+
#[cfg(not(target_os = "macos"))]
8+
#[link_section=".imm"]
9+
static magic: uint = 42;
10+
11+
#[cfg(not(target_os = "macos"))]
12+
#[link_section=".mut"]
13+
static mut frobulator: uint = 0xdeadbeef;
14+
15+
#[cfg(target_os = "macos")]
16+
#[link_section="__TEXT,__moretext"]
17+
fn i_live_in_more_text() -> &'static str {
18+
"knock knock"
19+
}
20+
21+
#[cfg(target_os = "macos")]
22+
#[link_section="__RODATA,__imm"]
23+
static magic: uint = 42;
24+
25+
#[cfg(target_os = "macos")]
26+
#[link_section="__DATA,__mut"]
27+
static mut frobulator: uint = 0xdeadbeef;
28+
29+
fn main() {
30+
unsafe {
31+
frobulator = 0xcafebabe;
32+
printfln!("%? %? %?", i_live_in_more_text(), magic, frobulator);
33+
}
34+
}

0 commit comments

Comments
 (0)