Skip to content

Commit 7462da5

Browse files
committed
rustc: use a simpler scheme for plugin registrar symbol names.
1 parent dd6e8d4 commit 7462da5

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

src/librustc/session/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// except according to those terms.
1010

1111
use dep_graph::DepGraph;
12+
use hir::def_id::DefIndex;
13+
use hir::svh::Svh;
1214
use lint;
1315
use middle::cstore::CrateStore;
1416
use middle::dependency_format;
@@ -312,6 +314,14 @@ impl Session {
312314
pub fn nonzeroing_move_hints(&self) -> bool {
313315
self.opts.debugging_opts.enable_nonzeroing_move_hints
314316
}
317+
318+
/// Returns the symbol name for the registrar function,
319+
/// given the crate Svh and the function DefIndex.
320+
pub fn generate_plugin_registrar_symbol(&self, svh: &Svh, index: DefIndex)
321+
-> String {
322+
format!("__rustc_plugin_registrar__{}_{}", svh, index.as_usize())
323+
}
324+
315325
pub fn sysroot<'a>(&'a self) -> &'a Path {
316326
match self.opts.maybe_sysroot {
317327
Some (ref sysroot) => sysroot,

src/librustc_metadata/creader.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use cstore::{self, CStore, CrateSource, MetadataBlob};
1717
use decoder;
1818
use loader::{self, CratePaths};
1919

20+
use rustc::hir::def_id::DefIndex;
2021
use rustc::hir::svh::Svh;
2122
use rustc::dep_graph::{DepGraph, DepNode};
2223
use rustc::session::{config, Session};
@@ -610,9 +611,10 @@ impl<'a> CrateReader<'a> {
610611
macros
611612
}
612613

613-
/// Look for a plugin registrar. Returns library path and symbol name.
614+
/// Look for a plugin registrar. Returns library path, crate
615+
/// SVH and DefIndex of the registrar function.
614616
pub fn find_plugin_registrar(&mut self, span: Span, name: &str)
615-
-> Option<(PathBuf, String)> {
617+
-> Option<(PathBuf, Svh, DefIndex)> {
616618
let ekrate = self.read_extension_crate(span, &CrateInfo {
617619
name: name.to_string(),
618620
ident: name.to_string(),
@@ -630,12 +632,14 @@ impl<'a> CrateReader<'a> {
630632
span_fatal!(self.sess, span, E0456, "{}", &message[..]);
631633
}
632634

635+
let svh = decoder::get_crate_hash(ekrate.metadata.as_slice());
633636
let registrar =
634-
decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice())
635-
.map(|id| decoder::get_symbol_from_buf(ekrate.metadata.as_slice(), id));
637+
decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice());
636638

637639
match (ekrate.dylib.as_ref(), registrar) {
638-
(Some(dylib), Some(reg)) => Some((dylib.to_path_buf(), reg)),
640+
(Some(dylib), Some(reg)) => {
641+
Some((dylib.to_path_buf(), svh, reg))
642+
}
639643
(None, Some(_)) => {
640644
span_err!(self.sess, span, E0457,
641645
"plugin `{}` only found in rlib format, but must be available \

src/librustc_metadata/decoder.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,6 @@ pub fn get_symbol(cdata: Cmd, id: DefIndex) -> String {
644644
return item_symbol(cdata.lookup_item(id));
645645
}
646646

647-
/// If you have a crate_metadata, call get_symbol instead
648-
pub fn get_symbol_from_buf(data: &[u8], id: DefIndex) -> String {
649-
let index = load_index(data);
650-
let pos = index.lookup_item(data, id).unwrap();
651-
let doc = reader::doc_at(data, pos as usize).unwrap().doc;
652-
item_symbol(doc)
653-
}
654-
655647
/// Iterates over the language items in the given crate.
656648
pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
657649
F: FnMut(DefIndex, usize) -> bool,

src/librustc_plugin/load.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ impl<'a> PluginLoader<'a> {
101101
fn load_plugin(&mut self, span: Span, name: &str, args: Vec<P<ast::MetaItem>>) {
102102
let registrar = self.reader.find_plugin_registrar(span, name);
103103

104-
if let Some((lib, symbol)) = registrar {
104+
if let Some((lib, svh, index)) = registrar {
105+
let symbol = self.sess.generate_plugin_registrar_symbol(&svh, index);
105106
let fun = self.dylink_registrar(span, lib, symbol);
106107
self.plugins.push(PluginRegistrar {
107108
fun: fun,

src/librustc_trans/base.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,12 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
24432443
-> String {
24442444
let id = ccx.tcx().map.as_local_node_id(instance.def).unwrap();
24452445

2446+
if ccx.sess().plugin_registrar_fn.get() == Some(id) {
2447+
let svh = &ccx.link_meta().crate_hash;
2448+
let idx = instance.def.index;
2449+
return ccx.sess().generate_plugin_registrar_symbol(svh, idx);
2450+
}
2451+
24462452
match ccx.external_srcs().borrow().get(&id) {
24472453
Some(&did) => {
24482454
let sym = ccx.sess().cstore.item_symbol(did);

0 commit comments

Comments
 (0)