Skip to content

Add cygwin support #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cpp_demangle = { default-features = false, version = "0.4.0", optional = true, f
"alloc",
] }

[target.'cfg(windows)'.dependencies]
[target.'cfg(any(windows, target_os = "cygwin"))'.dependencies]
windows-targets = "0.52.6"

[target.'cfg(not(all(windows, target_env = "msvc", not(target_vendor = "uwp"))))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/as-if-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ default-features = false
optional = true
features = ['read_core', 'elf', 'macho', 'pe', 'xcoff', 'unaligned', 'archive']

[target.'cfg(windows)'.dependencies]
[target.'cfg(any(windows, target_os = "cygwin"))'.dependencies]
windows-targets = "0.52.6"

[features]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,5 @@ mod lock {
))]
mod dbghelp;
// Auto-generated by windows-bindgen/riddle
#[cfg(windows)]
#[cfg(any(windows, target_os = "cygwin"))]
mod windows_sys;
5 changes: 3 additions & 2 deletions src/symbolize/gimli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ cfg_if::cfg_if! {
target_os = "solaris",
target_os = "illumos",
target_os = "aix",
target_os = "cygwin",
))] {
#[path = "gimli/mmap_unix.rs"]
mod mmap;
Expand Down Expand Up @@ -193,7 +194,7 @@ fn mmap(path: &Path) -> Option<Mmap> {
}

cfg_if::cfg_if! {
if #[cfg(windows)] {
if #[cfg(any(windows, target_os = "cygwin"))] {
mod coff;
use self::coff::{handle_split_dwarf, Object};
} else if #[cfg(any(target_vendor = "apple"))] {
Expand All @@ -209,7 +210,7 @@ cfg_if::cfg_if! {
}

cfg_if::cfg_if! {
if #[cfg(windows)] {
if #[cfg(any(windows, target_os = "cygwin"))] {
mod libs_windows;
use libs_windows::native_libraries;
} else if #[cfg(target_vendor = "apple")] {
Expand Down
79 changes: 72 additions & 7 deletions src/symbolize/gimli/libs_windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::super::super::windows_sys::*;
use super::mystd::ffi::OsString;
use super::mystd::os::windows::prelude::*;
use super::{coff, mmap, Library, LibrarySegment};
use alloc::vec;
use alloc::vec::Vec;
Expand Down Expand Up @@ -43,13 +42,79 @@ unsafe fn add_loaded_images(ret: &mut Vec<Library>) {
}
}

// Safety: long_path should be null-terminated
#[cfg(target_os = "cygwin")]
unsafe fn get_posix_path(long_path: &[u16]) -> Option<OsString> {
use super::mystd::os::unix::ffi::OsStringExt;

unsafe extern "C" {
// Doc: https://cygwin.com/cygwin-api/func-cygwin-conv-path.html
// Src: https://github.com/cygwin/cygwin/blob/718a15ba50e0d01c79800bd658c2477f9a603540/winsup/cygwin/path.cc#L3902
// Safety:
// * `what` should be `CCP_WIN_W_TO_POSIX` here
// * `from` is null-terminated UTF-16 path
// * `to` is buffer, the buffer size is `size`.
fn cygwin_conv_path(
what: libc::c_uint,
from: *const u16,
to: *mut u8,
size: libc::size_t,
) -> libc::ssize_t;
}
const CCP_WIN_W_TO_POSIX: libc::c_uint = 3;

// If `size` is 0, returns needed buffer size, including null terminator;
// or -1 if error.
// Safety: **Confirmed from source:** If `size` is 0, `to` is not used.
let name_len = unsafe {
cygwin_conv_path(
CCP_WIN_W_TO_POSIX,
long_path.as_ptr(),
core::ptr::null_mut(),
0,
)
};
// Expect at least 1 for null terminator.
// It's not likely to return error here.
if name_len < 1 {
return None;
}
let name_len = name_len as usize;
let mut name_buffer = Vec::with_capacity(name_len);
// Safety: `name_buffer` is large enough.
let res = unsafe {
cygwin_conv_path(
CCP_WIN_W_TO_POSIX,
long_path.as_ptr(),
name_buffer.as_mut_ptr(),
name_len,
)
};
// It's not likely to return error here.
if res != 0 {
return None;
}
// Remove the null terminator.
unsafe { name_buffer.set_len(name_len - 1) };
let name = OsString::from_vec(name_buffer);
Some(name)
}

unsafe fn load_library(me: &MODULEENTRY32W) -> Option<Library> {
let pos = me
.szExePath
.iter()
.position(|i| *i == 0)
.unwrap_or(me.szExePath.len());
let name = OsString::from_wide(&me.szExePath[..pos]);
#[cfg(windows)]
let name = {
use super::mystd::os::windows::prelude::*;
let pos = me
.szExePath
.iter()
.position(|i| *i == 0)
.unwrap_or(me.szExePath.len());
OsString::from_wide(&me.szExePath[..pos])
};
#[cfg(target_os = "cygwin")]
// Safety: the path with max length MAX_PATH always contains a null
// terminator. Don't slice it.
let name = unsafe { get_posix_path(&me.szExePath[..])? };

// MinGW libraries currently don't support ASLR
// (rust-lang/rust#16514), but DLLs can still be relocated around in
Expand Down
Loading