Skip to content

Commit eb5ef81

Browse files
committed
Auto merge of #65435 - michaelwoerister:fix-issue-64153, r=alexcrichton
Fix #64153 This PR changes how the compiler detects if an object file from an upstream crate is a Rust object file or not. Instead of checking if the name starts with the crate name and ends with `.o` (which is not always the case, as described in #64153), it now just checks if the filename ends with `.rcgu.o`. This fixes #64153. However, ideally we'd clean up the code around filename generation some more. Then this check could be made more robust. r? @alexcrichton
2 parents 2dd4e73 + a63dfb3 commit eb5ef81

File tree

6 files changed

+68
-21
lines changed

6 files changed

+68
-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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-include ../tools.mk
2+
3+
# `llvm-objdump`'s output looks different on windows than on other platforms.
4+
# It should be enough to check on Unix platforms, so:
5+
# ignore-windows
6+
7+
# Staticlibs don't include Rust object files from upstream crates if the same
8+
# code was already pulled into the lib via LTO. However, the bug described in
9+
# https://github.com/rust-lang/rust/issues/64153 lead to this exclusion not
10+
# working properly if the upstream crate was compiled with an explicit filename
11+
# (via `-o`).
12+
#
13+
# This test makes sure that functions defined in the upstream crates do not
14+
# appear twice in the final staticlib when listing all the symbols from it.
15+
16+
all:
17+
$(RUSTC) --crate-type rlib upstream.rs -o $(TMPDIR)/libupstream.rlib -Ccodegen-units=1
18+
$(RUSTC) --crate-type staticlib downstream.rs -Clto -Ccodegen-units=1 -o $(TMPDIR)/libdownstream.a
19+
# Dump all the symbols from the staticlib into `syms`
20+
"$(LLVM_BIN_DIR)"/llvm-objdump -t $(TMPDIR)/libdownstream.a > $(TMPDIR)/syms
21+
# Count the global instances of `issue64153_test_function`. There'll be 2
22+
# if the `upstream` object file got erronously included twice.
23+
# The line we are testing for with the regex looks something like:
24+
# 0000000000000000 g F .text.issue64153_test_function 00000023 issue64153_test_function
25+
grep -c -e "[[:space:]]g[[:space:]]*F[[:space:]].*issue64153_test_function" $(TMPDIR)/syms > $(TMPDIR)/count
26+
[ "$$(cat $(TMPDIR)/count)" -eq "1" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate upstream;
2+
3+
#[no_mangle]
4+
pub extern "C" fn foo() {
5+
print!("1 + 1 = {}", upstream::issue64153_test_function(1));
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Make this function extern "C", public, and no-mangle, so that it gets
2+
// exported from the downstream staticlib.
3+
#[no_mangle]
4+
pub extern "C" fn issue64153_test_function(x: u32) -> u32 {
5+
x + 1
6+
}

0 commit comments

Comments
 (0)