Skip to content

Commit 3c2ec13

Browse files
committed
rustc: use a simpler scheme for plugin registrar symbol names.
1 parent 22ac88f commit 3c2ec13

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
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use hir::def_id::DefIndex;
12+
use hir::svh::Svh;
1113
use lint;
1214
use middle::cstore::CrateStore;
1315
use middle::dependency_format;
@@ -310,6 +312,14 @@ impl Session {
310312
pub fn nonzeroing_move_hints(&self) -> bool {
311313
self.opts.debugging_opts.enable_nonzeroing_move_hints
312314
}
315+
316+
/// Returns the symbol name for the registrar function,
317+
/// given the crate Svh and the function DefIndex.
318+
pub fn generate_plugin_registrar_symbol(&self, svh: &Svh, index: DefIndex)
319+
-> String {
320+
format!("__rustc_plugin_registrar__{}_{}", svh, index.as_usize())
321+
}
322+
313323
pub fn sysroot<'a>(&'a self) -> &'a Path {
314324
match self.opts.maybe_sysroot {
315325
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};
@@ -578,9 +579,10 @@ impl<'a> CrateReader<'a> {
578579
macros
579580
}
580581

581-
/// Look for a plugin registrar. Returns library path and symbol name.
582+
/// Look for a plugin registrar. Returns library path, crate
583+
/// SVH and DefIndex of the registrar function.
582584
pub fn find_plugin_registrar(&mut self, span: Span, name: &str)
583-
-> Option<(PathBuf, String)> {
585+
-> Option<(PathBuf, Svh, DefIndex)> {
584586
let ekrate = self.read_extension_crate(span, &CrateInfo {
585587
name: name.to_string(),
586588
ident: name.to_string(),
@@ -598,12 +600,14 @@ impl<'a> CrateReader<'a> {
598600
span_fatal!(self.sess, span, E0456, "{}", &message[..]);
599601
}
600602

603+
let svh = decoder::get_crate_hash(ekrate.metadata.as_slice());
601604
let registrar =
602-
decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice())
603-
.map(|id| decoder::get_symbol_from_buf(ekrate.metadata.as_slice(), id));
605+
decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice());
604606

605607
match (ekrate.dylib.as_ref(), registrar) {
606-
(Some(dylib), Some(reg)) => Some((dylib.to_path_buf(), reg)),
608+
(Some(dylib), Some(reg)) => {
609+
Some((dylib.to_path_buf(), svh, reg))
610+
}
607611
(None, Some(_)) => {
608612
span_err!(self.sess, span, E0457,
609613
"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
@@ -613,14 +613,6 @@ pub fn get_symbol(cdata: Cmd, id: DefIndex) -> String {
613613
return item_symbol(cdata.lookup_item(id));
614614
}
615615

616-
/// If you have a crate_metadata, call get_symbol instead
617-
pub fn get_symbol_from_buf(data: &[u8], id: DefIndex) -> String {
618-
let index = load_index(data);
619-
let pos = index.lookup_item(data, id).unwrap();
620-
let doc = reader::doc_at(data, pos as usize).unwrap().doc;
621-
item_symbol(doc)
622-
}
623-
624616
/// Iterates over the language items in the given crate.
625617
pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
626618
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
@@ -2435,6 +2435,12 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
24352435
-> String {
24362436
let id = ccx.tcx().map.as_local_node_id(instance.def).unwrap();
24372437

2438+
if ccx.sess().plugin_registrar_fn.get() == Some(id) {
2439+
let svh = &ccx.link_meta().crate_hash;
2440+
let idx = instance.def.index;
2441+
return ccx.sess().generate_plugin_registrar_symbol(svh, idx);
2442+
}
2443+
24382444
match ccx.external_srcs().borrow().get(&id) {
24392445
Some(&did) => {
24402446
let sym = ccx.sess().cstore.item_symbol(did);

0 commit comments

Comments
 (0)