Compiling Rust binaries for Windows 98 SE and more: a journey

A journey through PE files, linkers, and the Rust source!

https://seri.tools/blog/compiling-rust-for-legacy-windows/

11 Likes

My hat is off to you sir. That is craziness above and beyond the call of duty.

3 Likes

Implement an ANSI/Codepage based version of OsStr / OsString for Windows 9x systems, removing the need for unicows

Good luck with that.

There's an API implementation of AsRef<OsStr> for str, which means that you have to write a function with the signature fn (&'a str) -> &'a OsStr; take a reference to a UTF-8 string, and return an OsStr reference with the same lifetime. About the only way that seems possible* is if OsStr's byte representation is a superset of str's.

Are you're wondering how this works on Windows NT, where str is UTF-8 and the native wide strings are UTF-16? Rust's OsStr doesn't use the same byte-for-byte representation as native wide strings on Windows. Rust uses an encoding called WTF-8, which is a superset of UTF-8 that is also isomorphic with Windows wide string**. This means that every time you pass an OsString to a Windows API, it performs an allocation.

I guess you could basically reimplement unicows yourself, keeping the OsString representation as WTF-8 but calling the A windows functions instead of the W variants and performing the conversion within libstd. But that sounds like a lot of boring drudgery to implement it, and you wouldn't really learn much about Win32 or Rust that way.

* Short of degenerate implementations like panicking or returning the empty &'static OsStr, which would satisfy the function signature but not actually work.
** Windows wide strings are allowed content that isn't valid Unicode. WTF-8 allows the same content, because that's actually the whole point.

2 Likes

Yeah, I've looked into how wide support is implemented, and upon reading about WTF-8 I've steered clear of digging deeper and followed the unicows way only, mostly to keep myself from going crazy and losing fun with this little project :slight_smile:

That seems like a viable strategy if someone were to do that, yeah!

Thanks for the insight! :slight_smile:

1 Like