Explicitly pass in which crate type to use to each_linked_rlib

Otherwise we may pick the dependency formats for say a dylib when
linking a staticlib.
This commit is contained in:
bjorn3 2022-12-28 17:45:06 +00:00
parent 0b54782cc8
commit ed77a61901
2 changed files with 75 additions and 75 deletions

View File

@ -11,7 +11,7 @@
use rustc_metadata::fs::{emit_wrapper_file, METADATA_FILENAME};
use rustc_middle::middle::dependency_format::Linkage;
use rustc_middle::middle::exported_symbols::SymbolExportKind;
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Lto, Strip};
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip};
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest, SplitDwarfKind};
use rustc_session::cstore::DllImport;
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
@ -208,16 +208,16 @@ pub fn link_binary<'a>(
Ok(())
}
// Crate type is not passed when calculating the dylibs to include for LTO. In that case all
// crate types must use the same dependency formats.
pub fn each_linked_rlib(
sess: &Session,
info: &CrateInfo,
crate_type: Option<CrateType>,
f: &mut dyn FnMut(CrateNum, &Path),
) -> Result<(), errors::LinkRlibError> {
let crates = info.used_crates.iter();
let mut fmts = None;
let lto_active = matches!(sess.lto(), Lto::Fat | Lto::Thin);
if lto_active {
let fmts = if crate_type.is_none() {
for combination in info.dependency_formats.iter().combinations(2) {
let (ty1, list1) = &combination[0];
let (ty2, list2) = &combination[1];
@ -230,27 +230,20 @@ pub fn each_linked_rlib(
});
}
}
}
&info.dependency_formats[0].1
} else {
let fmts = info
.dependency_formats
.iter()
.find_map(|&(ty, ref list)| if Some(ty) == crate_type { Some(list) } else { None });
for (ty, list) in info.dependency_formats.iter() {
match ty {
CrateType::Executable
| CrateType::Staticlib
| CrateType::Cdylib
| CrateType::ProcMacro => {
fmts = Some(list);
break;
}
CrateType::Dylib if lto_active => {
fmts = Some(list);
break;
}
_ => {}
}
}
let Some(fmts) = fmts else {
return Err(errors::LinkRlibError::MissingFormat);
};
fmts
};
for &cnum in crates {
match fmts.get(cnum.as_usize() - 1) {
Some(&Linkage::NotLinked | &Linkage::Dynamic | &Linkage::IncludedFromDylib) => continue,
@ -516,7 +509,10 @@ fn link_staticlib<'a>(
)?;
let mut all_native_libs = vec![];
let res = each_linked_rlib(sess, &codegen_results.crate_info, &mut |cnum, path| {
let res = each_linked_rlib(
&codegen_results.crate_info,
Some(CrateType::Staticlib),
&mut |cnum, path| {
let name = codegen_results.crate_info.crate_name[&cnum];
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
@ -562,7 +558,9 @@ fn link_staticlib<'a>(
// Otherwise if this is *not* a rust object and we're skipping
// objects then skip this file
if skip_object_files && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
if skip_object_files
&& (!fname.starts_with(&obj_start) || !fname.ends_with(".o"))
{
return true;
}
@ -572,8 +570,10 @@ fn link_staticlib<'a>(
)
.unwrap();
all_native_libs.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned());
});
all_native_libs
.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned());
},
);
if let Err(e) = res {
sess.emit_fatal(e);
}

View File

@ -1002,7 +1002,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
let sess = tcx.sess;
let mut each_linked_rlib_for_lto = Vec::new();
drop(link::each_linked_rlib(sess, crate_info, &mut |cnum, path| {
drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
if link::ignored_for_lto(sess, crate_info, cnum) {
return;
}