use rustc_hir::def_id::CrateNum;
use rustc_middle::middle::cstore::{EncodedMetadata, LibSource, NativeLibrary, NativeLibraryKind};
use rustc_middle::middle::dependency_format::Linkage;
-use rustc_session::config::{
- self, CFGuard, DebugInfo, OutputFilenames, OutputType, PrintRequest, Sanitizer,
-};
+use rustc_session::config::{self, CFGuard, CrateType, DebugInfo};
+use rustc_session::config::{OutputFilenames, OutputType, PrintRequest, Sanitizer};
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_session::search_paths::PathKind;
/// For all the linkers we support, and information they might
use super::archive::ArchiveBuilder;
use super::command::Command;
-use super::linker::Linker;
+use super::linker::{self, Linker};
use super::rpath::{self, RPathConfig};
use crate::{looks_like_rust_object_file, CodegenResults, CrateInfo, METADATA_FILENAME};
// Ignore executable crates if we have -Z no-codegen, as they will error.
if (sess.opts.debugging_opts.no_codegen || !sess.opts.output_types.should_codegen())
&& !output_metadata
- && crate_type == config::CrateType::Executable
+ && crate_type == CrateType::Executable
{
continue;
}
if outputs.outputs.should_codegen() {
let out_filename = out_filename(sess, crate_type, outputs, crate_name);
match crate_type {
- config::CrateType::Rlib => {
+ CrateType::Rlib => {
let _timer = sess.timer("link_rlib");
link_rlib::<B>(
sess,
)
.build();
}
- config::CrateType::Staticlib => {
+ CrateType::Staticlib => {
link_staticlib::<B>(sess, codegen_results, &out_filename, &tmpdir);
}
_ => {
let mut fmts = None;
for (ty, list) in info.dependency_formats.iter() {
match ty {
- config::CrateType::Executable
- | config::CrateType::Staticlib
- | config::CrateType::Cdylib
- | config::CrateType::ProcMacro => {
+ CrateType::Executable
+ | CrateType::Staticlib
+ | CrateType::Cdylib
+ | CrateType::ProcMacro => {
fmts = Some(list);
break;
}
// links to all upstream files as well.
fn link_natively<'a, B: ArchiveBuilder<'a>>(
sess: &'a Session,
- crate_type: config::CrateType,
+ crate_type: CrateType,
out_filename: &Path,
codegen_results: &CodegenResults,
tmpdir: &Path,
target_cpu,
);
+ linker::disable_localization(&mut cmd);
+
for &(ref k, ref v) in &sess.target.target.options.link_env {
cmd.env(k, v);
}
}
}
-fn link_sanitizer_runtime(sess: &Session, crate_type: config::CrateType, linker: &mut dyn Linker) {
+fn link_sanitizer_runtime(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {
let sanitizer = match &sess.opts.debugging_opts.sanitizer {
Some(s) => s,
None => return,
};
- if crate_type != config::CrateType::Executable {
+ if crate_type != CrateType::Executable {
return;
}
.crate_types
.borrow()
.iter()
- .any(|&x| x != config::CrateType::Rlib && x != config::CrateType::Staticlib);
+ .any(|&x| x != CrateType::Rlib && x != CrateType::Staticlib);
if !output_linked {
return false;
}
}
/// Add begin object files defined by the target spec.
-fn add_pre_link_objects(cmd: &mut dyn Linker, sess: &Session, crate_type: config::CrateType) {
- let pre_link_objects = if crate_type == config::CrateType::Executable {
+fn add_pre_link_objects(cmd: &mut dyn Linker, sess: &Session, crate_type: CrateType) {
+ let pre_link_objects = if crate_type == CrateType::Executable {
&sess.target.target.options.pre_link_objects_exe
} else {
&sess.target.target.options.pre_link_objects_dll
cmd.add_object(&get_object_file_path(sess, obj));
}
- if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
+ if crate_type == CrateType::Executable && sess.crt_static(Some(crate_type)) {
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
cmd.add_object(&get_object_file_path(sess, obj));
}
}
/// Add end object files defined by the target spec.
-fn add_post_link_objects(cmd: &mut dyn Linker, sess: &Session, crate_type: config::CrateType) {
+fn add_post_link_objects(cmd: &mut dyn Linker, sess: &Session, crate_type: CrateType) {
for obj in &sess.target.target.options.post_link_objects {
cmd.add_object(&get_object_file_path(sess, obj));
}
cmd: &mut dyn Linker,
sess: &Session,
flavor: LinkerFlavor,
- crate_type: config::CrateType,
+ crate_type: CrateType,
) {
if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) {
cmd.args(args);
cmd.args(&sess.opts.debugging_opts.pre_link_args);
}
+/// Add a link script embedded in the target, if applicable.
+fn add_link_script(
+ cmd: &mut dyn Linker,
+ sess: &Session,
+ tmpdir: &Path,
+ crate_type: CrateType,
+) {
+ match (crate_type, &sess.target.target.options.link_script) {
+ (CrateType::Cdylib | CrateType::Executable, Some(script)) => {
+ if !sess.target.target.options.linker_is_gnu {
+ sess.fatal("can only use link script when linking with GNU-like linker");
+ }
+
+ let file_name = ["rustc", &sess.target.target.llvm_target, "linkfile.ld"].join("-");
+
+ let path = tmpdir.join(file_name);
+ if let Err(e) = fs::write(&path, script) {
+ sess.fatal(&format!("failed to write link script to {}: {}", path.display(), e));
+ }
+
+ cmd.arg("--script");
+ cmd.arg(path);
+ }
+ _ => {}
+ }
+}
+
/// Add arbitrary "user defined" args defined from command line and by `#[link_args]` attributes.
/// FIXME: Determine where exactly these args need to be inserted.
fn add_user_defined_link_args(
cmd: &mut dyn Linker,
sess: &Session,
flavor: LinkerFlavor,
- crate_type: config::CrateType,
+ crate_type: CrateType,
codegen_results: &CodegenResults,
) {
if let Some(args) = sess.target.target.options.late_link_args.get(&flavor) {
cmd.args(args);
}
- let any_dynamic_crate = crate_type == config::CrateType::Dylib
+ let any_dynamic_crate = crate_type == CrateType::Dylib
|| codegen_results.crate_info.dependency_formats.iter().any(|(ty, list)| {
*ty == crate_type && list.iter().any(|&linkage| linkage == Linkage::Dynamic)
});
/// Add object files containing metadata for the current crate.
fn add_local_crate_metadata_objects(
cmd: &mut dyn Linker,
- crate_type: config::CrateType,
+ crate_type: CrateType,
codegen_results: &CodegenResults,
) {
// When linking a dynamic library, we put the metadata into a section of the
// executable. This metadata is in a separate object file from the main
// object file, so we link that in here.
- if crate_type == config::CrateType::Dylib || crate_type == config::CrateType::ProcMacro {
+ if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
if let Some(obj) = codegen_results.metadata_module.as_ref().and_then(|m| m.object.as_ref())
{
cmd.add_object(obj);
fn link_local_crate_native_libs_and_dependent_crate_libs<'a, B: ArchiveBuilder<'a>>(
cmd: &mut dyn Linker,
sess: &'a Session,
- crate_type: config::CrateType,
+ crate_type: CrateType,
codegen_results: &CodegenResults,
tmpdir: &Path,
) {
cmd: &mut dyn Linker,
sess: &Session,
flavor: LinkerFlavor,
- crate_type: config::CrateType,
+ crate_type: CrateType,
codegen_results: &CodegenResults,
) {
- if crate_type != config::CrateType::Executable {
+ if crate_type != CrateType::Executable {
return;
}
path: &Path,
flavor: LinkerFlavor,
sess: &'a Session,
- crate_type: config::CrateType,
+ crate_type: CrateType,
tmpdir: &Path,
out_filename: &Path,
codegen_results: &CodegenResults,
// NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
add_pre_link_args(cmd, sess, flavor, crate_type);
+ // NO-OPT-OUT
+ add_link_script(cmd, sess, tmpdir, crate_type);
+
// NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
if sess.target.target.options.is_like_fuchsia {
let prefix = match sess.opts.debugging_opts.sanitizer {
cmd.output_filename(out_filename);
// OBJECT-FILES-NO, AUDIT-ORDER
- if crate_type == config::CrateType::Executable && sess.target.target.options.is_like_windows {
+ if crate_type == CrateType::Executable && sess.target.target.options.is_like_windows {
if let Some(ref s) = codegen_results.windows_subsystem {
cmd.subsystem(s);
}
// Try to strip as much out of the generated object by removing unused
// sections if possible. See more comments in linker.rs
if !sess.opts.cg.link_dead_code {
- let keep_metadata = crate_type == config::CrateType::Dylib;
+ let keep_metadata = crate_type == CrateType::Dylib;
cmd.gc_sections(keep_metadata);
}
// NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
// Tell the linker what we're doing.
- if crate_type != config::CrateType::Executable {
+ if crate_type != CrateType::Executable {
cmd.build_dylib(out_filename);
}
- if crate_type == config::CrateType::Executable && sess.crt_static(Some(crate_type)) {
+ if crate_type == CrateType::Executable && sess.crt_static(Some(crate_type)) {
cmd.build_static_executable();
}
cmd: &mut dyn Linker,
sess: &'a Session,
codegen_results: &CodegenResults,
- crate_type: config::CrateType,
+ crate_type: CrateType,
tmpdir: &Path,
) {
// All of the heavy lifting has previously been accomplished by the
sess: &'a Session,
codegen_results: &CodegenResults,
tmpdir: &Path,
- crate_type: config::CrateType,
+ crate_type: CrateType,
cnum: CrateNum,
) {
let src = &codegen_results.crate_info.used_crate_source[&cnum];
if (!are_upstream_rust_objects_already_included(sess)
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum))
- && crate_type != config::CrateType::Dylib
+ && crate_type != CrateType::Dylib
&& !skip_native
{
cmd.link_rlib(&fix_windows_verbatim_for_gcc(cratepath));
// Note, though, that we don't want to include the whole of a
// compiler-builtins crate (e.g., compiler-rt) because it'll get
// repeatedly linked anyway.
- if crate_type == config::CrateType::Dylib
+ if crate_type == CrateType::Dylib
&& codegen_results.crate_info.compiler_builtins != Some(cnum)
{
cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst));
cmd: &mut dyn Linker,
sess: &Session,
codegen_results: &CodegenResults,
- crate_type: config::CrateType,
+ crate_type: CrateType,
) {
// Be sure to use a topological sorting of crates because there may be
// interdependencies between native libraries. When passing -nodefaultlibs,