Skip to content

Commit

Permalink
Rollup merge of rust-lang#102327 - ChrisDenton:std-raw-dylib, r=thomcc
Browse files Browse the repository at this point in the history
Use `raw-dylib` in the std for non-x86 Windows

The [`raw_dylib`](rust-lang#58713) feature was recently [stabilized for non-x86 Windows targets](rust-lang#99916). This is now in beta so std start to use it without feature flags. Eventually this may allowing linking Rust programs without needing import libraries.

I think the standard library is best placed to dog food before the beta becomes the stable the release. There hopefully shouldn't be any issues but if there are it'd be good to catch them early.
  • Loading branch information
notriddle authored Oct 30, 2022
2 parents c0ee7a1 + 4c05e3a commit d70f59d
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions library/std/src/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ pub const STATUS_INVALID_PARAMETER: NTSTATUS = 0xc000000d_u32 as _;

pub const STATUS_PENDING: NTSTATUS = 0x103 as _;
pub const STATUS_END_OF_FILE: NTSTATUS = 0xC0000011_u32 as _;
#[cfg(target_arch = "x86")]
pub const STATUS_NOT_IMPLEMENTED: NTSTATUS = 0xC0000002_u32 as _;

// Equivalent to the `NT_SUCCESS` C preprocessor macro.
Expand Down Expand Up @@ -843,7 +844,8 @@ if #[cfg(not(target_vendor = "uwp"))] {
) -> BOOL;
}

#[link(name = "userenv")]
#[cfg_attr(not(target_arch = "x86"), link(name = "userenv", kind = "raw-dylib"))]
#[cfg_attr(target_arch = "x86", link(name = "userenv"))]
extern "system" {
// Allowed but unused by UWP
pub fn GetUserProfileDirectoryW(
Expand Down Expand Up @@ -1158,7 +1160,8 @@ extern "system" {
pub fn GetFileAttributesW(lpFileName: LPCWSTR) -> DWORD;
}

#[link(name = "ws2_32")]
#[cfg_attr(not(target_arch = "x86"), link(name = "ws2_32", kind = "raw-dylib"))]
#[cfg_attr(target_arch = "x86", link(name = "ws2_32"))]
extern "system" {
pub fn WSAStartup(wVersionRequested: WORD, lpWSAData: LPWSADATA) -> c_int;
pub fn WSACleanup() -> c_int;
Expand Down Expand Up @@ -1251,7 +1254,8 @@ extern "system" {
) -> c_int;
}

#[link(name = "bcrypt")]
#[cfg_attr(not(target_arch = "x86"), link(name = "bcrypt", kind = "raw-dylib"))]
#[cfg_attr(target_arch = "x86", link(name = "bcrypt"))]
extern "system" {
// >= Vista / Server 2008
// https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
Expand All @@ -1270,6 +1274,47 @@ extern "system" {
pub fn BCryptCloseAlgorithmProvider(hAlgorithm: BCRYPT_ALG_HANDLE, dwFlags: ULONG) -> NTSTATUS;
}

#[cfg(not(target_arch = "x86"))]
#[link(name = "ntdll", kind = "raw-dylib")]
extern "system" {
pub fn NtCreateFile(
FileHandle: *mut HANDLE,
DesiredAccess: ACCESS_MASK,
ObjectAttributes: *const OBJECT_ATTRIBUTES,
IoStatusBlock: *mut IO_STATUS_BLOCK,
AllocationSize: *mut i64,
FileAttributes: ULONG,
ShareAccess: ULONG,
CreateDisposition: ULONG,
CreateOptions: ULONG,
EaBuffer: *mut c_void,
EaLength: ULONG,
) -> NTSTATUS;
pub fn NtReadFile(
FileHandle: BorrowedHandle<'_>,
Event: HANDLE,
ApcRoutine: Option<IO_APC_ROUTINE>,
ApcContext: *mut c_void,
IoStatusBlock: &mut IO_STATUS_BLOCK,
Buffer: *mut crate::mem::MaybeUninit<u8>,
Length: ULONG,
ByteOffset: Option<&LARGE_INTEGER>,
Key: Option<&ULONG>,
) -> NTSTATUS;
pub fn NtWriteFile(
FileHandle: BorrowedHandle<'_>,
Event: HANDLE,
ApcRoutine: Option<IO_APC_ROUTINE>,
ApcContext: *mut c_void,
IoStatusBlock: &mut IO_STATUS_BLOCK,
Buffer: *const u8,
Length: ULONG,
ByteOffset: Option<&LARGE_INTEGER>,
Key: Option<&ULONG>,
) -> NTSTATUS;
pub fn RtlNtStatusToDosError(Status: NTSTATUS) -> ULONG;
}

// Functions that aren't available on every version of Windows that we support,
// but we still use them and just provide some form of a fallback implementation.
compat_fn_with_fallback! {
Expand Down Expand Up @@ -1310,6 +1355,7 @@ compat_fn_optional! {
compat_fn_with_fallback! {
pub static NTDLL: &CStr = ansi_str!("ntdll");

#[cfg(target_arch = "x86")]
pub fn NtCreateFile(
FileHandle: *mut HANDLE,
DesiredAccess: ACCESS_MASK,
Expand All @@ -1325,6 +1371,7 @@ compat_fn_with_fallback! {
) -> NTSTATUS {
STATUS_NOT_IMPLEMENTED
}
#[cfg(target_arch = "x86")]
pub fn NtReadFile(
FileHandle: BorrowedHandle<'_>,
Event: HANDLE,
Expand All @@ -1338,6 +1385,7 @@ compat_fn_with_fallback! {
) -> NTSTATUS {
STATUS_NOT_IMPLEMENTED
}
#[cfg(target_arch = "x86")]
pub fn NtWriteFile(
FileHandle: BorrowedHandle<'_>,
Event: HANDLE,
Expand All @@ -1351,6 +1399,7 @@ compat_fn_with_fallback! {
) -> NTSTATUS {
STATUS_NOT_IMPLEMENTED
}
#[cfg(target_arch = "x86")]
pub fn RtlNtStatusToDosError(
Status: NTSTATUS
) -> ULONG {
Expand Down

0 comments on commit d70f59d

Please sign in to comment.