I/O without `std`, `asm!`, and `extern`

Suppose we have a no_std crate without dependencies that neither uses core::arch::asm! nor calls extern functions. However, the crate may use unsafe.
Can this crate perform I/O (reading/writing files etc.) at runtime?

unsafe can do anything, specifically, it can use platform specific knowledge to load ffi symbols from shared libraries.

for example, on Windows, it can search the PE header of the current module, and parse the PE header to get the import table, then it can locate the address of system library kernel32 (or even ntdll), then it can simply resolve LoadLibraryA and GetProcAddress from the export table of kernel32.

EDIT: you need unsafe to deref/load raw bytes from the calculated address, and you need unsafe to call the resolved function pointers.

4 Likes

Thanks a lot for your explanation, nerditation!

Embedded systems often use memory-mapped IO.

3 Likes

I see, that makes sense that you can then do I/O. :slight_smile: