Skip to content

Commit af05b23

Browse files
Fix issue #64153 by checking for .rcgu.o suffix when trying to identify Rust generated object files.
1 parent 7528234 commit af05b23

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/librustc_codegen_llvm/back/archive.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use std::str;
99

1010
use crate::llvm::archive_ro::{ArchiveRO, Child};
1111
use crate::llvm::{self, ArchiveKind};
12-
use rustc_codegen_ssa::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION};
12+
use rustc_codegen_ssa::{
13+
METADATA_FILENAME, RLIB_BYTECODE_EXTENSION, looks_like_rust_object_file
14+
};
1315
use rustc_codegen_ssa::back::archive::{ArchiveBuilder, find_library};
1416
use rustc::session::Session;
1517
use syntax::symbol::Symbol;
@@ -141,7 +143,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
141143
}
142144

143145
// Don't include Rust objects if LTO is enabled
144-
if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") {
146+
if lto && looks_like_rust_object_file(fname) {
145147
return true
146148
}
147149

src/librustc_codegen_ssa/back/link.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use rustc::session::{Session, filesearch};
55
use rustc::session::config::{
6-
self, RUST_CGU_EXT, DebugInfo, OutputFilenames, OutputType, PrintRequest, Sanitizer
6+
self, DebugInfo, OutputFilenames, OutputType, PrintRequest, Sanitizer
77
};
88
use rustc::session::search_paths::PathKind;
99
use rustc::middle::dependency_format::Linkage;
@@ -15,7 +15,8 @@ use rustc_fs_util::fix_windows_verbatim_for_gcc;
1515
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
1616
use syntax::symbol::Symbol;
1717

18-
use crate::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION, CrateInfo, CodegenResults};
18+
use crate::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION, CrateInfo,
19+
looks_like_rust_object_file, CodegenResults};
1920
use super::archive::ArchiveBuilder;
2021
use super::command::Command;
2122
use super::linker::Linker;
@@ -1549,23 +1550,9 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
15491550
let canonical = f.replace("-", "_");
15501551
let canonical_name = name.replace("-", "_");
15511552

1552-
// Look for `.rcgu.o` at the end of the filename to conclude
1553-
// that this is a Rust-related object file.
1554-
fn looks_like_rust(s: &str) -> bool {
1555-
let path = Path::new(s);
1556-
let ext = path.extension().and_then(|s| s.to_str());
1557-
if ext != Some(OutputType::Object.extension()) {
1558-
return false
1559-
}
1560-
let ext2 = path.file_stem()
1561-
.and_then(|s| Path::new(s).extension())
1562-
.and_then(|s| s.to_str());
1563-
ext2 == Some(RUST_CGU_EXT)
1564-
}
1565-
15661553
let is_rust_object =
15671554
canonical.starts_with(&canonical_name) &&
1568-
looks_like_rust(&f);
1555+
looks_like_rust_object_file(&f);
15691556

15701557
// If we've been requested to skip all native object files
15711558
// (those not generated by the rust compiler) then we can skip

src/librustc_codegen_ssa/lib.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#[macro_use] extern crate rustc;
2323
#[macro_use] extern crate syntax;
2424

25-
use std::path::PathBuf;
25+
use std::path::{Path, PathBuf};
2626
use rustc::dep_graph::WorkProduct;
27-
use rustc::session::config::{OutputFilenames, OutputType};
27+
use rustc::session::config::{OutputFilenames, OutputType, RUST_CGU_EXT};
2828
use rustc::middle::lang_items::LangItem;
2929
use rustc::hir::def_id::CrateNum;
3030
use rustc::ty::query::Providers;
@@ -62,6 +62,7 @@ pub struct ModuleCodegen<M> {
6262
pub const METADATA_FILENAME: &str = "rust.metadata.bin";
6363
pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";
6464

65+
6566
impl<M> ModuleCodegen<M> {
6667
pub fn into_compiled_module(self,
6768
emit_obj: bool,
@@ -166,3 +167,22 @@ pub fn provide_extern(providers: &mut Providers<'_>) {
166167
crate::back::symbol_export::provide_extern(providers);
167168
crate::base::provide_both(providers);
168169
}
170+
171+
/// Checks if the given filename ends with the `.rcgu.o` extension that `rustc`
172+
/// uses for the object files it generates.
173+
pub fn looks_like_rust_object_file(filename: &str) -> bool {
174+
let path = Path::new(filename);
175+
let ext = path.extension().and_then(|s| s.to_str());
176+
if ext != Some(OutputType::Object.extension()) {
177+
// The file name does not end with ".o", so it can't be an object file.
178+
return false
179+
}
180+
181+
// Strip the ".o" at the end
182+
let ext2 = path.file_stem()
183+
.and_then(|s| Path::new(s).extension())
184+
.and_then(|s| s.to_str());
185+
186+
// Check if the "inner" extension
187+
ext2 == Some(RUST_CGU_EXT)
188+
}

0 commit comments

Comments
 (0)