Create an .exe that exports symbols

I am trying to get rust to create a windows .exe executable, that exports some extern "C" functions. This has turned into a fight with the linker.

The following is how you'd do this in zig

export fn look_at_me() u64 {
    return 42;
}

pub fn main() !void {
    return;
}

then run zig build-exe main.zig -rdynamic, then objdump -x main.exe gives

Export Address Table -- Ordinal Base 0
	[   1] +base[   1] 64000 Export RVA
	[   2] +base[   2] 64008 Export RVA
	[   3] +base[   3] 65010 Export RVA
	[   4] +base[   4] 5f008 Export RVA
	[   5] +base[   5] 65000 Export RVA
	[   6] +base[   6] 5bbb0 Export RVA
	[   7] +base[   7] 1bb0 Export RVA
	[   8] +base[   8] 1cf0 Export RVA

[Ordinal/Name Pointer] Table
	[   1] __xl_a
	[   2] __xl_z
	[   3] _tls_end
	[   4] _tls_index
	[   5] _tls_start
	[   6] _tls_used
	[   7] look_at_me
	[   8] wWinMainCRTStartup

So we have an executable, with an export address table, that provides the exposed look_at_me function. This is my goal, but now with rust.

#[no_mangle]
pub extern "C" fn look_at_me() -> usize {
    42
}

fn main() {
    println!("Hello, world!");
}

I prepared a repository here: GitHub - folkertdev/rust-rdynamic: experiments with the `-rdynamic` linker flag and rust. On windows, the rdynamic flag is not available:

note: x86_64-w64-mingw32-gcc: error: unrecognized command line option '-rdynamic'

An alternative I found is -Wl,--expose-all-symbols. I got that to work while cross-compiling. Now I want to do this on an actual windows machine, and that is where the trouble starts:

  • On the default target, x86_64-pc-windows-msvc, the -Wl,--expose-all-symbols seems to just get ignored? in fact all linker arguments seem to be ignored.

  • Using the x86_64-pc-windows-gnu requires installing some gcc compiler. I'd rather not do this manually. Why can rustup not install this for me?

For some reason I can build my actual program with x86_64-pc-windows-gnu when I juse a Command from a rust process. I guess that makes the gcc binary available somehow because it's a mingw environment? it's not available in powershell by default. In any case, this Command fails with

crypto/fipsmodule/aes/aes_nohw.c:1:0: sorry, unimplemented: 64-bit mode not compiled in

See also https://stackoverflow.com/questions/68149955/rust-sorry-unimplemented-64-bit-mode-not-compiled-in

So, does anyone know what magic incantation do I need to provide the linker to achieve what I want here?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.