Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d26bebd

Browse files
committedMar 17, 2016
---
yaml --- r: 282019 b: refs/heads/stable c: 6c0674e h: refs/heads/master i: 282017: dace146 282015: 255d87d
1 parent 72be1e9 commit d26bebd

File tree

9 files changed

+98
-136
lines changed

9 files changed

+98
-136
lines changed
 

‎[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: 3c795e08d6f4a532f12f3f8e1837db5e0647f8b0
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: aec63821d04872f9190c3d8606d0a58428005222
32+
refs/heads/stable: 6c0674e613a708e25d9ae92de4ae4bc5756805c4
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b
3535
refs/tags/1.2.0: f557861f822c34f07270347b94b5280de20a597e

‎branches/stable/src/librustc_trans/trans/base.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use trans::datum;
7676
use trans::debuginfo::{self, DebugLoc, ToDebugLoc};
7777
use trans::declare;
7878
use trans::expr;
79-
use trans::foreign;
8079
use trans::glue;
8180
use trans::intrinsic;
8281
use trans::machine;
@@ -2311,7 +2310,7 @@ pub fn trans_item(ccx: &CrateContext, item: &hir::Item) {
23112310
return;
23122311
}
23132312
for fi in &m.items {
2314-
let lname = foreign::link_name(fi.name, &fi.attrs).to_string();
2313+
let lname = imported_name(fi.name, &fi.attrs).to_string();
23152314
ccx.item_symbols().borrow_mut().insert(fi.id, lname);
23162315
}
23172316
}
@@ -2434,6 +2433,16 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
24342433
}
24352434
}
24362435

2436+
pub fn imported_name(name: ast::Name, attrs: &[ast::Attribute]) -> InternedString {
2437+
match attr::first_attr_value_str_by_name(attrs, "link_name") {
2438+
Some(ln) => ln.clone(),
2439+
None => match weak_lang_items::link_name(attrs) {
2440+
Some(name) => name,
2441+
None => name.as_str(),
2442+
}
2443+
}
2444+
}
2445+
24372446
fn contains_null(s: &str) -> bool {
24382447
s.bytes().any(|b| b == 0)
24392448
}

‎branches/stable/src/librustc_trans/trans/callee.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use trans::declare;
4545
use trans::expr;
4646
use trans::glue;
4747
use trans::inline;
48-
use trans::foreign;
4948
use trans::intrinsic;
5049
use trans::machine::{llalign_of_min, llsize_of_store};
5150
use trans::meth;
@@ -529,7 +528,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
529528
Some(hir_map::NodeForeignItem(&hir::ForeignItem {
530529
ref attrs, name, node: hir::ForeignItemFn(..), ..
531530
})) => {
532-
(foreign::link_name(name, attrs).to_string(), &attrs[..], None)
531+
(imported_name(name, attrs).to_string(), &attrs[..], None)
533532
}
534533

535534
None => {

‎branches/stable/src/librustc_trans/trans/consts.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use middle::def::Def;
1919
use middle::def_id::DefId;
2020
use rustc::front::map as hir_map;
2121
use trans::{abi, adt, closure, debuginfo, expr, inline, machine};
22-
use trans::base::{self, exported_name, push_ctxt};
22+
use trans::base::{self, exported_name, imported_name, push_ctxt};
2323
use trans::callee::Callee;
2424
use trans::collector::{self, TransItem};
2525
use trans::common::{type_is_sized, C_nil, const_get_elt};
@@ -28,7 +28,6 @@ use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, Vari
2828
use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null};
2929
use trans::datum::{Datum, Lvalue};
3030
use trans::declare;
31-
use trans::foreign;
3231
use trans::monomorphize::{self, Instance};
3332
use trans::type_::Type;
3433
use trans::type_of;
@@ -1029,6 +1028,7 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
10291028
}
10301029

10311030
let g = if let Some(id) = ccx.tcx().map.as_local_node_id(def_id) {
1031+
let llty = type_of::type_of(ccx, ty);
10321032
match ccx.tcx().map.get(id) {
10331033
hir_map::NodeItem(&hir::Item {
10341034
ref attrs, span, node: hir::ItemStatic(..), ..
@@ -1042,7 +1042,6 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
10421042

10431043
// Create the global before evaluating the initializer;
10441044
// this is necessary to allow recursive statics.
1045-
let llty = type_of::type_of(ccx, ty);
10461045
let g = declare::define_global(ccx, &sym, llty).unwrap_or_else(|| {
10471046
ccx.sess().span_fatal(span,
10481047
&format!("symbol `{}` is already defined", sym))
@@ -1052,9 +1051,63 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
10521051
g
10531052
}
10541053

1055-
hir_map::NodeForeignItem(ni @ &hir::ForeignItem {
1056-
node: hir::ForeignItemStatic(..), ..
1057-
}) => foreign::register_static(ccx, ni),
1054+
hir_map::NodeForeignItem(&hir::ForeignItem {
1055+
ref attrs, name, span, node: hir::ForeignItemStatic(..), ..
1056+
}) => {
1057+
let ident = imported_name(name, attrs);
1058+
let g = if let Some(name) =
1059+
attr::first_attr_value_str_by_name(&attrs, "linkage") {
1060+
// If this is a static with a linkage specified, then we need to handle
1061+
// it a little specially. The typesystem prevents things like &T and
1062+
// extern "C" fn() from being non-null, so we can't just declare a
1063+
// static and call it a day. Some linkages (like weak) will make it such
1064+
// that the static actually has a null value.
1065+
let linkage = match base::llvm_linkage_by_name(&name) {
1066+
Some(linkage) => linkage,
1067+
None => {
1068+
ccx.sess().span_fatal(span, "invalid linkage specified");
1069+
}
1070+
};
1071+
let llty2 = match ty.sty {
1072+
ty::TyRawPtr(ref mt) => type_of::type_of(ccx, mt.ty),
1073+
_ => {
1074+
ccx.sess().span_fatal(span, "must have type `*const T` or `*mut T`");
1075+
}
1076+
};
1077+
unsafe {
1078+
// Declare a symbol `foo` with the desired linkage.
1079+
let g1 = declare::declare_global(ccx, &ident, llty2);
1080+
llvm::SetLinkage(g1, linkage);
1081+
1082+
// Declare an internal global `extern_with_linkage_foo` which
1083+
// is initialized with the address of `foo`. If `foo` is
1084+
// discarded during linking (for example, if `foo` has weak
1085+
// linkage and there are no definitions), then
1086+
// `extern_with_linkage_foo` will instead be initialized to
1087+
// zero.
1088+
let mut real_name = "_rust_extern_with_linkage_".to_string();
1089+
real_name.push_str(&ident);
1090+
let g2 = declare::define_global(ccx, &real_name, llty).unwrap_or_else(||{
1091+
ccx.sess().span_fatal(span,
1092+
&format!("symbol `{}` is already defined", ident))
1093+
});
1094+
llvm::SetLinkage(g2, llvm::InternalLinkage);
1095+
llvm::LLVMSetInitializer(g2, g1);
1096+
g2
1097+
}
1098+
} else {
1099+
// Generate an external declaration.
1100+
declare::declare_global(ccx, &ident, llty)
1101+
};
1102+
1103+
for attr in attrs {
1104+
if attr.check_name("thread_local") {
1105+
llvm::set_thread_local(g, true);
1106+
}
1107+
}
1108+
1109+
g
1110+
}
10581111

10591112
item => unreachable!("get_static: expected static, found {:?}", item)
10601113
}

‎branches/stable/src/librustc_trans/trans/foreign.rs

Lines changed: 0 additions & 100 deletions
This file was deleted.

‎branches/stable/src/librustc_trans/trans/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ mod debuginfo;
4949
mod declare;
5050
mod disr;
5151
mod expr;
52-
mod foreign;
5352
mod glue;
5453
mod inline;
5554
mod intrinsic;

‎branches/stable/src/test/compile-fail/dupe-symbols-8.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎branches/stable/src/test/compile-fail/linkage2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
extern {
1414
#[linkage = "extern_weak"] static foo: i32;
15-
//~^ ERROR: must have type `*T`
15+
//~^ ERROR: must have type `*const T` or `*mut T`
1616
}
1717

1818
fn main() {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2016 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+
// We used to have a __rust_abi shim that resulted in duplicated symbols
12+
// whenever the item path wasn't enough to disambiguate between them.
13+
fn main() {
14+
let a = {
15+
extern fn good() -> i32 { return 0; }
16+
good as extern fn() -> i32
17+
};
18+
let b = {
19+
extern fn good() -> i32 { return 5; }
20+
good as extern fn() -> i32
21+
};
22+
23+
assert!(a != b);
24+
assert_eq!((a(), b()), (0, 5));
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.