rust/library
bors a8a88fe524 Auto merge of #122268 - ChrisDenton:no-libc, r=Mark-Simulacrum
Link MSVC default lib in core

## The Problem

On Windows MSVC, Rust invokes the linker directly. This means only the objects and libraries Rust explicitly passes to the linker are used. In short, this is equivalent to passing `-nodefaultlibs`, `-nostartfiles`, etc for gnu compilers.

To compensate for this [the libc crate links to the necessary libraries](a0f5b4b213/src/windows/mod.rs (L258-L261)). The libc crate is then linked from std, thus when you use std you get the defaults back.or integrate with C/C++.

However, this has a few problems:

- For `no_std`, users are left to manually pass the default lib to the linker
- Whereas `std` has the opposite problem, using [`/nodefaultlib`](https://learn.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?view=msvc-170) doesn't work as expected because Rust treats them as normal libs. This is a particular problem when you want to use e.g. the debug CRT libraries in their place or integrate with C/C++..

## The solution

This PR fixes this in two ways:

- moves linking the default lib into `core`
- passes the lib to the linker using [`/defaultlib`](https://learn.microsoft.com/en-us/cpp/build/reference/defaultlib-specify-default-library?view=msvc-170). This allows users to override it in the normal way (i.e. with [`/nodefaultlib`](https://learn.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?view=msvc-170)).

This is more or less equivalent to what the MSVC C compiler does. You can see what this looks like in my second commit, which I'll reproduce here for convenience:

```rust
// In library/core
#[cfg(all(windows, target_env = "msvc"))]
#[link(
    name = "/defaultlib:msvcrt",
    modifiers = "+verbatim",
    cfg(not(target_feature = "crt-static"))
)]
#[link(name = "/defaultlib:libcmt", modifiers = "+verbatim", cfg(target_feature = "crt-static"))]
extern "C" {}
```

## Alternatives

- Add the above to `unwind` and `std` but not `core`
- The status quo
- Some other kind of compiler magic maybe

This bares some discussion so I've t-libs nominated it.
2024-04-14 13:28:21 +00:00
..
alloc Rollup merge of #123868 - eduardosm:stabilize-slice_ptr_len, r=jhpratt 2024-04-13 00:18:46 -04:00
backtrace@e151306182 Update backtrace submodule 2024-04-12 16:28:19 -07:00
core Auto merge of #122268 - ChrisDenton:no-libc, r=Mark-Simulacrum 2024-04-14 13:28:21 +00:00
panic_abort Replace libc::c_int with core::ffi::c_int 2024-04-14 07:11:51 +00:00
panic_unwind Replace libc::c_int with core::ffi::c_int 2024-04-14 07:11:51 +00:00
portable-simd rename expose_addr to expose_provenance 2024-04-03 16:00:38 +02:00
proc_macro Call the panic hook for non-unwind panics in proc-macros 2024-04-11 17:46:12 -04:00
profiler_builtins Update version of cc crate 2024-03-14 16:42:15 +00:00
rtstartup library: Fix warnings in rtstartup 2024-01-06 01:32:03 +03:00
rustc-std-workspace-alloc Replace libstd, libcore, liballoc in line comments. 2022-12-30 14:00:42 +01:00
rustc-std-workspace-core
rustc-std-workspace-std
std Auto merge of #122268 - ChrisDenton:no-libc, r=Mark-Simulacrum 2024-04-14 13:28:21 +00:00
stdarch@7df81ba8c3 Update stdarch submodule 2024-04-11 16:26:02 -07:00
sysroot Expose compiler-builtins-weak-intrinsics feature for -Zbuild-std 2023-06-23 11:15:34 +01:00
test Replace libc::c_int with core::ffi::c_int 2024-04-14 07:11:51 +00:00
unwind Replace libc::c_int with core::ffi::c_int 2024-04-14 07:11:51 +00:00