2022-03-14 10:28:34 +00:00
|
|
|
use rustc_ast::{NestedMetaItem, CRATE_NODE_ID};
|
2020-01-11 12:15:20 +00:00
|
|
|
use rustc_attr as attr;
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2023-07-15 10:17:37 +00:00
|
|
|
use rustc_middle::query::LocalCrate;
|
2021-06-08 20:56:06 +00:00
|
|
|
use rustc_middle::ty::{List, ParamEnv, ParamEnvAnd, Ty, TyCtxt};
|
2022-08-24 10:10:40 +00:00
|
|
|
use rustc_session::config::CrateType;
|
2023-07-15 10:17:37 +00:00
|
|
|
use rustc_session::cstore::{
|
|
|
|
DllCallingConvention, DllImport, ForeignModule, NativeLib, PeImportNameType,
|
|
|
|
};
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2022-08-24 10:10:40 +00:00
|
|
|
use rustc_session::search_paths::PathKind;
|
2020-05-17 21:18:50 +00:00
|
|
|
use rustc_session::utils::NativeLibKind;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::Session;
|
2023-07-15 10:17:37 +00:00
|
|
|
use rustc_span::def_id::{DefId, LOCAL_CRATE};
|
2022-03-14 10:28:34 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2018-04-25 16:30:39 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-11-11 21:46:56 +00:00
|
|
|
|
2023-02-05 00:58:22 +00:00
|
|
|
use crate::errors;
|
2022-08-23 22:33:28 +00:00
|
|
|
|
2022-08-24 10:10:40 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
pub fn find_native_static_library(
|
|
|
|
name: &str,
|
2022-11-13 10:03:01 +00:00
|
|
|
verbatim: bool,
|
2022-08-24 10:10:40 +00:00
|
|
|
search_paths: &[PathBuf],
|
|
|
|
sess: &Session,
|
|
|
|
) -> PathBuf {
|
2022-11-13 10:03:01 +00:00
|
|
|
let formats = if verbatim {
|
2022-09-20 07:10:17 +00:00
|
|
|
vec![("".into(), "".into())]
|
2022-08-24 10:10:40 +00:00
|
|
|
} else {
|
2022-09-20 07:10:17 +00:00
|
|
|
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
|
|
|
|
// On Windows, static libraries sometimes show up as libfoo.a and other
|
|
|
|
// times show up as foo.lib
|
|
|
|
let unix = ("lib".into(), ".a".into());
|
|
|
|
if os == unix { vec![os] } else { vec![os, unix] }
|
2022-08-24 10:10:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for path in search_paths {
|
2022-09-20 07:10:17 +00:00
|
|
|
for (prefix, suffix) in &formats {
|
2022-12-19 09:31:55 +00:00
|
|
|
let test = path.join(format!("{prefix}{name}{suffix}"));
|
2022-08-24 10:10:40 +00:00
|
|
|
if test.exists() {
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 07:10:17 +00:00
|
|
|
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_fatal(errors::MissingNativeLibrary::new(name, verbatim));
|
2022-08-24 10:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find_bundled_library(
|
2023-03-17 01:23:46 +00:00
|
|
|
name: Symbol,
|
2022-08-24 10:10:40 +00:00
|
|
|
verbatim: Option<bool>,
|
|
|
|
kind: NativeLibKind,
|
2023-01-26 14:10:13 +00:00
|
|
|
has_cfg: bool,
|
2022-08-24 10:10:40 +00:00
|
|
|
sess: &Session,
|
|
|
|
) -> Option<Symbol> {
|
2023-01-26 14:10:13 +00:00
|
|
|
if let NativeLibKind::Static { bundle: Some(true) | None, whole_archive } = kind
|
|
|
|
&& sess.crate_types().iter().any(|t| matches!(t, &CrateType::Rlib | CrateType::Staticlib))
|
|
|
|
&& (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true))
|
|
|
|
{
|
|
|
|
let verbatim = verbatim.unwrap_or(false);
|
|
|
|
let search_paths = &sess.target_filesearch(PathKind::Native).search_path_dirs();
|
2023-03-16 23:59:08 +00:00
|
|
|
return find_native_static_library(name.as_str(), verbatim, search_paths, sess)
|
2023-01-26 14:10:13 +00:00
|
|
|
.file_name()
|
|
|
|
.and_then(|s| s.to_str())
|
|
|
|
.map(Symbol::intern);
|
2022-08-24 10:10:40 +00:00
|
|
|
}
|
2023-01-26 14:10:13 +00:00
|
|
|
None
|
2022-08-24 10:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> Vec<NativeLib> {
|
2017-08-30 21:48:57 +00:00
|
|
|
let mut collector = Collector { tcx, libs: Vec::new() };
|
2023-07-15 10:17:37 +00:00
|
|
|
if tcx.sess.opts.unstable_opts.link_directives {
|
|
|
|
for module in tcx.foreign_modules(LOCAL_CRATE).values() {
|
|
|
|
collector.process_module(module);
|
|
|
|
}
|
2022-04-06 23:04:15 +00:00
|
|
|
}
|
2017-08-30 21:48:57 +00:00
|
|
|
collector.process_command_line();
|
2020-03-20 14:03:11 +00:00
|
|
|
collector.libs
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
|
2017-08-30 21:48:57 +00:00
|
|
|
match lib.cfg {
|
2022-02-27 21:26:24 +00:00
|
|
|
Some(ref cfg) => attr::cfg_matches(cfg, &sess.parse_sess, CRATE_NODE_ID, None),
|
2017-08-30 21:48:57 +00:00
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct Collector<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-05-17 21:18:50 +00:00
|
|
|
libs: Vec<NativeLib>,
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:29:57 +00:00
|
|
|
impl<'tcx> Collector<'tcx> {
|
2023-07-15 10:17:37 +00:00
|
|
|
fn process_module(&mut self, module: &ForeignModule) {
|
|
|
|
let ForeignModule { def_id, abi, ref foreign_items } = *module;
|
|
|
|
let def_id = def_id.expect_local();
|
2022-04-07 16:29:57 +00:00
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
let sess = self.tcx.sess;
|
2017-08-30 21:48:57 +00:00
|
|
|
|
2023-01-30 12:01:09 +00:00
|
|
|
if matches!(abi, Abi::Rust | Abi::RustIntrinsic | Abi::PlatformIntrinsic) {
|
2017-08-30 21:48:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process all of the #[link(..)]-style arguments
|
2022-03-14 10:28:34 +00:00
|
|
|
let features = self.tcx.features();
|
2023-02-04 23:07:41 +00:00
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
for m in self.tcx.get_attrs(def_id, sym::link) {
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(items) = m.meta_item_list() else {
|
|
|
|
continue;
|
2017-08-30 21:48:57 +00:00
|
|
|
};
|
2018-07-16 18:31:14 +00:00
|
|
|
|
2022-03-14 10:28:34 +00:00
|
|
|
let mut name = None;
|
|
|
|
let mut kind = None;
|
|
|
|
let mut modifiers = None;
|
|
|
|
let mut cfg = None;
|
|
|
|
let mut wasm_import_module = None;
|
2022-07-12 20:52:35 +00:00
|
|
|
let mut import_name_type = None;
|
2018-07-16 18:31:14 +00:00
|
|
|
for item in items.iter() {
|
2022-03-14 10:28:34 +00:00
|
|
|
match item.name_or_empty() {
|
|
|
|
sym::name => {
|
|
|
|
if name.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleNamesInLink { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
2021-03-25 04:45:09 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
let Some(link_name) = item.value_str() else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkNameForm { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let span = item.name_value_literal_span().unwrap();
|
|
|
|
if link_name.is_empty() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::EmptyLinkName { span });
|
2018-07-16 18:31:14 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
name = Some((link_name, span));
|
2018-07-16 18:31:14 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
sym::kind => {
|
|
|
|
if kind.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleKindsInLink { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
2018-07-16 18:31:14 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
let Some(link_kind) = item.value_str() else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkKindForm { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
2021-03-25 04:45:09 +00:00
|
|
|
};
|
|
|
|
|
2022-03-14 10:28:34 +00:00
|
|
|
let span = item.name_value_literal_span().unwrap();
|
|
|
|
let link_kind = match link_kind.as_str() {
|
|
|
|
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
|
|
|
|
"dylib" => NativeLibKind::Dylib { as_needed: None },
|
|
|
|
"framework" => {
|
|
|
|
if !sess.target.is_like_osx {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkFrameworkApple { span });
|
2022-02-11 07:08:35 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
NativeLibKind::Framework { as_needed: None }
|
2021-03-25 04:45:09 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
"raw-dylib" => {
|
|
|
|
if !sess.target.is_like_windows {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::FrameworkOnlyWindows { span });
|
2022-02-11 07:08:35 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
NativeLibKind::RawDylib
|
2021-03-25 04:45:09 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
kind => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::UnknownLinkKind { span, kind });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
2022-01-23 00:49:12 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
};
|
|
|
|
kind = Some(link_kind);
|
|
|
|
}
|
|
|
|
sym::modifiers => {
|
|
|
|
if modifiers.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleLinkModifiers { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let Some(link_modifiers) = item.value_str() else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkModifiersForm { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
modifiers = Some((link_modifiers, item.name_value_literal_span().unwrap()));
|
|
|
|
}
|
|
|
|
sym::cfg => {
|
|
|
|
if cfg.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleCfgs { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let Some(link_cfg) = item.meta_item_list() else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkCfgForm { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let [NestedMetaItem::MetaItem(link_cfg)] = link_cfg else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkCfgSinglePredicate { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if !features.link_cfg {
|
|
|
|
feature_err(
|
|
|
|
&sess.parse_sess,
|
|
|
|
sym::link_cfg,
|
|
|
|
item.span(),
|
|
|
|
"link cfg is unstable",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
cfg = Some(link_cfg.clone());
|
|
|
|
}
|
|
|
|
sym::wasm_import_module => {
|
|
|
|
if wasm_import_module.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleWasmImport { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
2021-03-25 04:45:09 +00:00
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
let Some(link_wasm_import_module) = item.value_str() else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::WasmImportForm { span: item.span() });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
wasm_import_module = Some((link_wasm_import_module, item.span()));
|
2021-03-25 04:45:09 +00:00
|
|
|
}
|
2022-07-12 20:52:35 +00:00
|
|
|
sym::import_name_type => {
|
|
|
|
if import_name_type.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleImportNameType { span: item.span() });
|
2022-07-12 20:52:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let Some(link_import_name_type) = item.value_str() else {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::ImportNameTypeForm { span: item.span() });
|
2022-07-12 20:52:35 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if self.tcx.sess.target.arch != "x86" {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::ImportNameTypeX86 { span: item.span() });
|
2022-07-12 20:52:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let link_import_name_type = match link_import_name_type.as_str() {
|
|
|
|
"decorated" => PeImportNameType::Decorated,
|
|
|
|
"noprefix" => PeImportNameType::NoPrefix,
|
|
|
|
"undecorated" => PeImportNameType::Undecorated,
|
|
|
|
import_name_type => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::UnknownImportNameType {
|
2022-08-27 23:50:11 +00:00
|
|
|
span: item.span(),
|
|
|
|
import_name_type,
|
|
|
|
});
|
2022-07-12 20:52:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
import_name_type = Some((link_import_name_type, item.span()));
|
|
|
|
}
|
2022-03-14 10:28:34 +00:00
|
|
|
_ => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::UnexpectedLinkArg { span: item.span() });
|
2022-02-11 07:08:35 +00:00
|
|
|
}
|
2021-03-25 04:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:28:34 +00:00
|
|
|
// Do this outside the above loop so we don't depend on modifiers coming after kinds
|
|
|
|
let mut verbatim = None;
|
|
|
|
if let Some((modifiers, span)) = modifiers {
|
|
|
|
for modifier in modifiers.as_str().split(',') {
|
|
|
|
let (modifier, value) = match modifier.strip_prefix(&['+', '-']) {
|
|
|
|
Some(m) => (m, modifier.starts_with('+')),
|
|
|
|
None => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::InvalidLinkModifier { span });
|
2022-03-14 10:28:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
macro report_unstable_modifier($feature: ident) {
|
|
|
|
if !features.$feature {
|
|
|
|
feature_err(
|
|
|
|
&sess.parse_sess,
|
|
|
|
sym::$feature,
|
|
|
|
span,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 03:26:58 +00:00
|
|
|
format!("linking modifier `{modifier}` is unstable"),
|
2022-03-14 10:28:34 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let assign_modifier = |dst: &mut Option<bool>| {
|
|
|
|
if dst.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::MultipleModifiers { span, modifier });
|
2022-03-14 10:28:34 +00:00
|
|
|
} else {
|
|
|
|
*dst = Some(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match (modifier, &mut kind) {
|
|
|
|
("bundle", Some(NativeLibKind::Static { bundle, .. })) => {
|
|
|
|
assign_modifier(bundle)
|
|
|
|
}
|
|
|
|
("bundle", _) => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::BundleNeedsStatic { span });
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 10:03:01 +00:00
|
|
|
("verbatim", _) => assign_modifier(&mut verbatim),
|
2022-03-14 10:28:34 +00:00
|
|
|
|
|
|
|
("whole-archive", Some(NativeLibKind::Static { whole_archive, .. })) => {
|
|
|
|
assign_modifier(whole_archive)
|
|
|
|
}
|
|
|
|
("whole-archive", _) => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::WholeArchiveNeedsStatic { span });
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
("as-needed", Some(NativeLibKind::Dylib { as_needed }))
|
|
|
|
| ("as-needed", Some(NativeLibKind::Framework { as_needed })) => {
|
|
|
|
report_unstable_modifier!(native_link_modifiers_as_needed);
|
|
|
|
assign_modifier(as_needed)
|
|
|
|
}
|
|
|
|
("as-needed", _) => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::AsNeededCompatibility { span });
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::UnknownLinkModifier { span, modifier });
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 07:08:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 10:28:34 +00:00
|
|
|
if let Some((_, span)) = wasm_import_module {
|
|
|
|
if name.is_some() || kind.is_some() || modifiers.is_some() || cfg.is_some() {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::IncompatibleWasmLink { span });
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
2018-07-16 18:31:14 +00:00
|
|
|
}
|
2021-03-08 20:42:54 +00:00
|
|
|
|
2023-03-17 01:23:46 +00:00
|
|
|
if wasm_import_module.is_some() {
|
|
|
|
(name, kind) = (wasm_import_module, Some(NativeLibKind::WasmImportModule));
|
|
|
|
}
|
|
|
|
let Some((name, name_span)) = name else {
|
|
|
|
sess.emit_err(errors::LinkRequiresName { span: m.span });
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2022-07-12 20:52:35 +00:00
|
|
|
// Do this outside of the loop so that `import_name_type` can be specified before `kind`.
|
|
|
|
if let Some((_, span)) = import_name_type {
|
|
|
|
if kind != Some(NativeLibKind::RawDylib) {
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::ImportNameTypeRaw { span });
|
2022-07-12 20:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 10:28:34 +00:00
|
|
|
let dll_imports = match kind {
|
|
|
|
Some(NativeLibKind::RawDylib) => {
|
2023-03-17 01:23:46 +00:00
|
|
|
if name.as_str().contains('\0') {
|
|
|
|
sess.emit_err(errors::RawDylibNoNul { span: name_span });
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
2023-07-15 10:17:37 +00:00
|
|
|
foreign_items
|
2021-03-08 20:42:54 +00:00
|
|
|
.iter()
|
2023-07-15 10:17:37 +00:00
|
|
|
.map(|&child_item| {
|
2022-07-12 20:52:35 +00:00
|
|
|
self.build_dll_import(
|
|
|
|
abi,
|
|
|
|
import_name_type.map(|(import_name_type, _)| import_name_type),
|
|
|
|
child_item,
|
|
|
|
)
|
|
|
|
})
|
2022-03-14 10:28:34 +00:00
|
|
|
.collect()
|
2022-01-23 00:49:12 +00:00
|
|
|
}
|
2022-08-02 17:33:27 +00:00
|
|
|
_ => {
|
2023-07-15 10:17:37 +00:00
|
|
|
for &child_item in foreign_items {
|
|
|
|
if self.tcx.def_kind(child_item).has_codegen_attrs()
|
|
|
|
&& self.tcx.codegen_fn_attrs(child_item).link_ordinal.is_some()
|
2022-08-02 17:33:27 +00:00
|
|
|
{
|
2023-07-15 10:17:37 +00:00
|
|
|
let link_ordinal_attr =
|
|
|
|
self.tcx.get_attr(child_item, sym::link_ordinal).unwrap();
|
2023-02-05 00:58:22 +00:00
|
|
|
sess.emit_err(errors::LinkOrdinalRawDylib {
|
|
|
|
span: link_ordinal_attr.span,
|
|
|
|
});
|
2022-08-02 17:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec::new()
|
|
|
|
}
|
2021-03-08 20:42:54 +00:00
|
|
|
};
|
2022-08-24 10:10:40 +00:00
|
|
|
|
|
|
|
let kind = kind.unwrap_or(NativeLibKind::Unspecified);
|
2023-01-26 14:10:13 +00:00
|
|
|
let filename = find_bundled_library(name, verbatim, kind, cfg.is_some(), sess);
|
2022-03-14 10:28:34 +00:00
|
|
|
self.libs.push(NativeLib {
|
2022-08-24 10:10:40 +00:00
|
|
|
name,
|
|
|
|
filename,
|
|
|
|
kind,
|
2022-03-14 10:28:34 +00:00
|
|
|
cfg,
|
2023-07-15 10:17:37 +00:00
|
|
|
foreign_module: Some(def_id.to_def_id()),
|
2022-03-14 10:28:34 +00:00
|
|
|
verbatim,
|
|
|
|
dll_imports,
|
|
|
|
});
|
2019-08-27 14:42:44 +00:00
|
|
|
}
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process libs passed on the command line
|
|
|
|
fn process_command_line(&mut self) {
|
|
|
|
// First, check for errors
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut renames = FxHashSet::default();
|
2021-03-25 04:45:09 +00:00
|
|
|
for lib in &self.tcx.sess.opts.libs {
|
2022-03-14 10:28:34 +00:00
|
|
|
if let NativeLibKind::Framework { .. } = lib.kind && !self.tcx.sess.target.is_like_osx {
|
|
|
|
// Cannot check this when parsing options because the target is not yet available.
|
2023-02-05 00:58:22 +00:00
|
|
|
self.tcx.sess.emit_err(errors::LibFrameworkApple);
|
2022-03-14 10:28:34 +00:00
|
|
|
}
|
2021-03-25 04:45:09 +00:00
|
|
|
if let Some(ref new_name) = lib.new_name {
|
2023-03-17 01:23:46 +00:00
|
|
|
let any_duplicate = self.libs.iter().any(|n| n.name.as_str() == lib.name);
|
2017-08-30 21:48:57 +00:00
|
|
|
if new_name.is_empty() {
|
2023-02-05 00:58:22 +00:00
|
|
|
self.tcx.sess.emit_err(errors::EmptyRenamingTarget { lib_name: &lib.name });
|
2018-07-16 18:31:14 +00:00
|
|
|
} else if !any_duplicate {
|
2023-02-05 00:58:22 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RenamingNoLink { lib_name: &lib.name });
|
2021-03-25 04:45:09 +00:00
|
|
|
} else if !renames.insert(&lib.name) {
|
2023-02-05 00:58:22 +00:00
|
|
|
self.tcx.sess.emit_err(errors::MultipleRenamings { lib_name: &lib.name });
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 13:30:23 +00:00
|
|
|
// Update kind and, optionally, the name of all native libraries
|
2022-11-16 20:34:16 +00:00
|
|
|
// (there may be more than one) with the specified name. If any
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
// library is mentioned more than once, keep the latest mention
|
|
|
|
// of it, so that any possible dependent libraries appear before
|
2022-11-16 20:34:16 +00:00
|
|
|
// it. (This ensures that the linker is able to see symbols from
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
// all possible dependent libraries before linking in the library
|
|
|
|
// in question.)
|
2021-03-25 04:45:09 +00:00
|
|
|
for passed_lib in &self.tcx.sess.opts.libs {
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
// If we've already added any native libraries with the same
|
2018-12-20 21:19:55 +00:00
|
|
|
// name, they will be pulled out into `existing`, so that we
|
|
|
|
// can move them to the end of the list below.
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
let mut existing = self
|
|
|
|
.libs
|
2023-04-08 22:37:21 +00:00
|
|
|
.extract_if(|lib| {
|
2023-03-17 01:23:46 +00:00
|
|
|
if lib.name.as_str() == passed_lib.name {
|
|
|
|
// FIXME: This whole logic is questionable, whether modifiers are
|
|
|
|
// involved or not, library reordering and kind overriding without
|
|
|
|
// explicit `:rename` in particular.
|
|
|
|
if lib.has_modifiers() || passed_lib.has_modifiers() {
|
|
|
|
match lib.foreign_module {
|
|
|
|
Some(def_id) => self.tcx.sess.emit_err(errors::NoLinkModOverride {
|
|
|
|
span: Some(self.tcx.def_span(def_id)),
|
|
|
|
}),
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.emit_err(errors::NoLinkModOverride { span: None })
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if passed_lib.kind != NativeLibKind::Unspecified {
|
|
|
|
lib.kind = passed_lib.kind;
|
|
|
|
}
|
|
|
|
if let Some(new_name) = &passed_lib.new_name {
|
|
|
|
lib.name = Symbol::intern(new_name);
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
}
|
2023-03-17 01:23:46 +00:00
|
|
|
lib.verbatim = passed_lib.verbatim;
|
|
|
|
return true;
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
false
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if existing.is_empty() {
|
2017-08-30 21:48:57 +00:00
|
|
|
// Add if not found
|
2021-10-07 21:18:39 +00:00
|
|
|
let new_name: Option<&str> = passed_lib.new_name.as_deref();
|
2023-03-17 01:23:46 +00:00
|
|
|
let name = Symbol::intern(new_name.unwrap_or(&passed_lib.name));
|
2022-08-24 10:10:40 +00:00
|
|
|
let sess = self.tcx.sess;
|
|
|
|
let filename =
|
2023-01-26 14:10:13 +00:00
|
|
|
find_bundled_library(name, passed_lib.verbatim, passed_lib.kind, false, sess);
|
2022-03-14 10:28:34 +00:00
|
|
|
self.libs.push(NativeLib {
|
2022-08-24 10:10:40 +00:00
|
|
|
name,
|
|
|
|
filename,
|
2021-03-25 04:45:09 +00:00
|
|
|
kind: passed_lib.kind,
|
2017-08-30 21:48:57 +00:00
|
|
|
cfg: None,
|
2018-02-10 22:28:17 +00:00
|
|
|
foreign_module: None,
|
2021-03-25 04:45:09 +00:00
|
|
|
verbatim: passed_lib.verbatim,
|
2021-03-08 20:42:54 +00:00
|
|
|
dll_imports: Vec::new(),
|
2022-03-14 10:28:34 +00:00
|
|
|
});
|
Keep last redundant linker flag, not first
When a library (L1) is passed to the linker multiple times, this is
sometimes purposeful: there might be several other libraries in the
linker command (L2 and L3) that all depend on L1. You'd end up with a
(simplified) linker command that looks like:
-l2 -l1 -l3 -l1
With the previous behavior, when rustc encountered a redundant library,
it would keep the first instance, and remove the later ones, resulting
in:
-l2 -l1 -l3
This can cause a linker error, because on some platforms (e.g. Linux),
the linker will only include symbols from L1 that are needed *at the
point it's referenced in the command line*. So if L3 depends on
additional symbols from L1, which aren't needed by L2, the linker won't
know to include them, and you'll end up with "undefined symbols" errors.
A better behavior is to keep the *last* instance of the library:
-l2 -l3 -l1
This ensures that all "downstream" libraries have been included in the
linker command before the "upstream" library is referenced.
Fixes rust-lang#47989
2018-12-20 20:46:42 +00:00
|
|
|
} else {
|
|
|
|
// Move all existing libraries with the same name to the
|
|
|
|
// end of the command line.
|
|
|
|
self.libs.append(&mut existing);
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 20:56:06 +00:00
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
fn i686_arg_list_size(&self, item: DefId) -> usize {
|
2021-06-08 20:56:06 +00:00
|
|
|
let argument_types: &List<Ty<'_>> = self.tcx.erase_late_bound_regions(
|
|
|
|
self.tcx
|
2023-07-15 10:17:37 +00:00
|
|
|
.type_of(item)
|
2023-07-11 21:35:29 +00:00
|
|
|
.instantiate_identity()
|
2021-06-08 20:56:06 +00:00
|
|
|
.fn_sig(self.tcx)
|
|
|
|
.inputs()
|
2023-02-17 03:33:08 +00:00
|
|
|
.map_bound(|slice| self.tcx.mk_type_list(slice)),
|
2021-06-08 20:56:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
argument_types
|
|
|
|
.iter()
|
|
|
|
.map(|ty| {
|
|
|
|
let layout = self
|
|
|
|
.tcx
|
|
|
|
.layout_of(ParamEnvAnd { param_env: ParamEnv::empty(), value: ty })
|
|
|
|
.expect("layout")
|
|
|
|
.layout;
|
|
|
|
// In both stdcall and fastcall, we always round up the argument size to the
|
|
|
|
// nearest multiple of 4 bytes.
|
2022-03-04 02:46:56 +00:00
|
|
|
(layout.size().bytes_usize() + 3) & !3
|
2021-06-08 20:56:06 +00:00
|
|
|
})
|
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
|
2022-07-12 20:52:35 +00:00
|
|
|
fn build_dll_import(
|
|
|
|
&self,
|
|
|
|
abi: Abi,
|
|
|
|
import_name_type: Option<PeImportNameType>,
|
2023-07-15 10:17:37 +00:00
|
|
|
item: DefId,
|
2022-07-12 20:52:35 +00:00
|
|
|
) -> DllImport {
|
2023-07-15 10:17:37 +00:00
|
|
|
let span = self.tcx.def_span(item);
|
|
|
|
|
2021-06-08 20:56:06 +00:00
|
|
|
let calling_convention = if self.tcx.sess.target.arch == "x86" {
|
|
|
|
match abi {
|
2022-02-01 17:53:45 +00:00
|
|
|
Abi::C { .. } | Abi::Cdecl { .. } => DllCallingConvention::C,
|
2021-06-08 20:56:06 +00:00
|
|
|
Abi::Stdcall { .. } | Abi::System { .. } => {
|
|
|
|
DllCallingConvention::Stdcall(self.i686_arg_list_size(item))
|
|
|
|
}
|
2022-02-01 17:53:45 +00:00
|
|
|
Abi::Fastcall { .. } => {
|
|
|
|
DllCallingConvention::Fastcall(self.i686_arg_list_size(item))
|
|
|
|
}
|
2022-07-19 17:40:26 +00:00
|
|
|
Abi::Vectorcall { .. } => {
|
|
|
|
DllCallingConvention::Vectorcall(self.i686_arg_list_size(item))
|
|
|
|
}
|
2021-06-08 20:56:06 +00:00
|
|
|
_ => {
|
2023-07-15 10:17:37 +00:00
|
|
|
self.tcx.sess.emit_fatal(errors::UnsupportedAbiI686 { span });
|
2021-06-08 20:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match abi {
|
2022-02-01 17:53:45 +00:00
|
|
|
Abi::C { .. } | Abi::Win64 { .. } | Abi::System { .. } => DllCallingConvention::C,
|
2021-06-08 20:56:06 +00:00
|
|
|
_ => {
|
2023-07-15 10:17:37 +00:00
|
|
|
self.tcx.sess.emit_fatal(errors::UnsupportedAbi { span });
|
2021-06-08 20:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-09-11 00:34:09 +00:00
|
|
|
|
2023-07-15 10:17:37 +00:00
|
|
|
let codegen_fn_attrs = self.tcx.codegen_fn_attrs(item);
|
2022-09-12 21:03:19 +00:00
|
|
|
let import_name_type = codegen_fn_attrs
|
2022-07-12 20:52:35 +00:00
|
|
|
.link_ordinal
|
|
|
|
.map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));
|
|
|
|
|
2021-09-11 00:34:09 +00:00
|
|
|
DllImport {
|
2023-07-15 10:17:37 +00:00
|
|
|
name: codegen_fn_attrs.link_name.unwrap_or(self.tcx.item_name(item)),
|
2022-07-12 20:52:35 +00:00
|
|
|
import_name_type,
|
2021-09-11 00:34:09 +00:00
|
|
|
calling_convention,
|
2023-07-15 10:17:37 +00:00
|
|
|
span,
|
|
|
|
is_fn: self.tcx.def_kind(item).is_fn_like(),
|
2021-09-11 00:34:09 +00:00
|
|
|
}
|
2021-06-08 20:56:06 +00:00
|
|
|
}
|
2017-08-30 21:48:57 +00:00
|
|
|
}
|