Skip to content

Commit a0480e2

Browse files
committed
rustc: Always name bytecode based on crate ids
When performing LTO, bytecode is loaded from an archive by using the crate id's name. If the -o flag was specified when building the original library, it previously also mangled the name of the bytecode. This switches to always using the crate id's name when naming the bytecode file, ensuring that it can always be loaded at a later date, regardless of -o. Closes rust-lang#13317
1 parent 46e6194 commit a0480e2

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/librustc/back/link.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ fn link_binary_output(sess: &Session,
846846

847847
match crate_type {
848848
session::CrateTypeRlib => {
849-
link_rlib(sess, Some(trans), &obj_filename, &out_filename);
849+
link_rlib(sess, Some((trans, id)), &obj_filename, &out_filename);
850850
}
851851
session::CrateTypeStaticlib => {
852852
link_staticlib(sess, &obj_filename, &out_filename);
@@ -869,7 +869,8 @@ fn link_binary_output(sess: &Session,
869869
// all of the object files from native libraries. This is done by unzipping
870870
// native libraries and inserting all of the contents into this archive.
871871
fn link_rlib<'a>(sess: &'a Session,
872-
trans: Option<&CrateTranslation>, // None == no metadata/bytecode
872+
// None == no metadata/bytecode
873+
trans: Option<(&CrateTranslation, &CrateId)>,
873874
obj_filename: &Path,
874875
out_filename: &Path) -> Archive<'a> {
875876
let mut a = Archive::create(sess, out_filename, obj_filename);
@@ -905,7 +906,7 @@ fn link_rlib<'a>(sess: &'a Session,
905906
// Basically, all this means is that this code should not move above the
906907
// code above.
907908
match trans {
908-
Some(trans) => {
909+
Some((trans, id)) => {
909910
// Instead of putting the metadata in an object file section, rlibs
910911
// contain the metadata in a separate file. We use a temp directory
911912
// here so concurrent builds in the same directory don't try to use
@@ -925,11 +926,15 @@ fn link_rlib<'a>(sess: &'a Session,
925926
remove(sess, &metadata);
926927

927928
// For LTO purposes, the bytecode of this library is also inserted
928-
// into the archive.
929+
// into the archive. Note that we ensure that the bytecode has the
930+
// same name as the crate id's name because it's how we'll search
931+
// for the bytecode later on.
929932
let bc = obj_filename.with_extension("bc");
930-
let bc_deflated = obj_filename.with_extension("bc.deflate");
933+
let bc_deflated = tmpdir.path().join(id.name.as_slice())
934+
.with_extension("bc.deflate");
931935
match fs::File::open(&bc).read_to_end().and_then(|data| {
932-
fs::File::create(&bc_deflated).write(flate::deflate_bytes(data).as_slice())
936+
let bytes = flate::deflate_bytes(data);
937+
fs::File::create(&bc_deflated).write(bytes.as_slice())
933938
}) {
934939
Ok(()) => {}
935940
Err(e) => {
@@ -938,7 +943,6 @@ fn link_rlib<'a>(sess: &'a Session,
938943
}
939944
}
940945
a.add_file(&bc_deflated, false);
941-
remove(sess, &bc_deflated);
942946
if !sess.opts.cg.save_temps &&
943947
!sess.opts.output_types.contains(&OutputTypeBitcode) {
944948
remove(sess, &bc);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) foo.rs -o $(TMPDIR)/libfoo-somehash-0.0.rlib
5+
$(RUSTC) main.rs -Zlto
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_id = "foo"]
2+
#![crate_type = "rlib"]
3+
4+
pub fn foo() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extern crate foo;
2+
3+
fn main() {
4+
}

0 commit comments

Comments
 (0)