Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 30 additions & 40 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,51 +1450,44 @@ fn link_natively(
let mut output = prog.stderr.clone();
output.extend_from_slice(&prog.stdout);
let escaped_output = escape_linker_output(&output, flavor);
let err = diagnostics::LinkingFailed {
linker_path: &linker_path,
exit_status: prog.status,
command: cmd,
escaped_output,
verbose: sess.opts.verbose,
sysroot_dir: sess.opts.sysroot.path().to_owned(),
};
sess.dcx().emit_err(err);

let mut unexpected_error = false;
let mut link_exe_status_stack_buffer_overrun = false;
let mut is_vs_installed = false;
let mut has_linker = false;

// If MSVC's `link.exe` was expected but the return code
// is not a Microsoft LNK error then suggest a way to fix or
// install the Visual Studio build tools.
if let Some(code) = prog.status.code() {
// All Microsoft `link.exe` linking ror codes are
// All Microsoft `link.exe` linking error codes are
// four digit numbers in the range 1000 to 9999 inclusive
if is_msvc_link_exe(sess) && (code < 1000 || code > 9999) {
let is_vs_installed = find_msvc_tools::find_vs_version().is_ok();
let has_linker =
is_vs_installed = find_msvc_tools::find_vs_version().is_ok();
has_linker =
find_msvc_tools::find_tool(sess.target.arch.desc(), "link.exe")
.is_some();

sess.dcx().emit_note(diagnostics::LinkExeUnexpectedError);
unexpected_error = true;

// STATUS_STACK_BUFFER_OVERRUN is also used for fast abnormal program termination, e.g. abort().
// Emit a special diagnostic to let people know that this most likely doesn't indicate a stack buffer overrun.
const STATUS_STACK_BUFFER_OVERRUN: i32 = 0xc0000409u32 as _;
if code == STATUS_STACK_BUFFER_OVERRUN {
sess.dcx().emit_note(diagnostics::LinkExeStatusStackBufferOverrun);
}

if is_vs_installed && has_linker {
// the linker is broken
sess.dcx().emit_note(diagnostics::RepairVSBuildTools);
sess.dcx().emit_note(diagnostics::MissingCppBuildToolComponent);
} else if is_vs_installed {
// the linker is not installed
sess.dcx().emit_note(diagnostics::SelectCppBuildToolWorkload);
} else {
// visual studio is not installed
sess.dcx().emit_note(diagnostics::VisualStudioNotInstalled);
}
link_exe_status_stack_buffer_overrun = code == STATUS_STACK_BUFFER_OVERRUN;
}
}

sess.dcx().abort_if_errors();
sess.dcx().emit_fatal(diagnostics::LinkingFailed {
linker_path: &linker_path,
exit_status: prog.status,
command: cmd,
escaped_output,
verbose: sess.opts.verbose,
sysroot_dir: sess.opts.sysroot.path().to_owned(),
unexpected_error,
link_exe_status_stack_buffer_overrun,
is_vs_installed,
has_linker,
});
}

info!("reporting linker output: flavor={flavor:?}");
Expand All @@ -1503,22 +1496,19 @@ fn link_natively(
Err(e) => {
let linker_not_found = e.kind() == io::ErrorKind::NotFound;

let err = if linker_not_found {
sess.dcx().emit_err(diagnostics::LinkerNotFound { linker_path, error: e })
if linker_not_found {
sess.dcx().emit_fatal(diagnostics::LinkerNotFound {
linker_path,
error: e,
msvc: sess.target.is_like_msvc,
})
} else {
sess.dcx().emit_err(diagnostics::UnableToExeLinker {
sess.dcx().emit_fatal(diagnostics::UnableToExeLinker {
linker_path,
error: e,
command_formatted: format!("{cmd:?}"),
})
};

if sess.target.is_like_msvc && linker_not_found {
sess.dcx().emit_note(diagnostics::MsvcMissingLinker);
sess.dcx().emit_note(diagnostics::CheckInstalledVisualStudio);
sess.dcx().emit_note(diagnostics::InsufficientVSCodeProduct);
}
err.raise_fatal();
}
}

Expand Down
Loading
Loading