Skip to content
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

Windows DllMain name is exported #67399

Open
MauriceKayser opened this issue Dec 18, 2019 · 4 comments
Open

Windows DllMain name is exported #67399

MauriceKayser opened this issue Dec 18, 2019 · 4 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@MauriceKayser
Copy link

On Windows, Rust exports the name of DllMain (std) / _DllMainCRTStartup (no_std), which is not standard behaviour and unwanted in most cases (that I am aware of). As far as I understand this happens because they must be marked with either #[no_mangle] or #[export_name = ".."] so the link.exe can find the symbol. The extern keyword must also be used to mark them as stdcall for 32 bit targets.

Example project:

  • Cargo.toml

    [lib]
    crate-type = ["cdylib"]
  • lib.rs (std)

    #[no_mangle]
    extern "system" fn DllMain(_: *const u8, _: u32, _: *const u8) -> u32 { 1 }
  • lib.rs (no_std)

    #![no_std]
    
    #[panic_handler]
    fn panic(_: &core::panic::PanicInfo) -> ! { loop {} }
    
    #[no_mangle]
    extern "system" fn _DllMainCRTStartup(_: *const u8, _: u32, _: *const u8) -> u32 { 1 }

dumpbin output

  • Example Windows dll file:

    Dump of file C:\Windows\System32\httpprxp.dll
    
    File Type: DLL
    
      Section contains the following exports for httpprxp.dll
    
        00000000 characteristics
            0.00 version
               1 ordinal base
               8 number of functions
               8 number of names
    
        ordinal hint RVA      name
    
              1    0 00001870 ProxyHelperGetProxyEventInformation
              2    1 00001D00 ProxyHelperProviderConnectToServer
              3    2 00001DC0 ProxyHelperProviderDisconnectFromServer
              4    3 000010E0 ProxyHelperProviderFreeMemory
              5    4 00001220 ProxyHelperProviderRegisterForEventNotification
              6    5 00001AC0 ProxyHelperProviderSetProxyConfiguration
              7    6 00001BA0 ProxyHelperProviderSetProxyCredentials
              8    7 00001620 ProxyHelperProviderUnregisterEventNotification
    
      Summary
    
            1000 .data
            1000 .didat
            1000 .pdata
            2000 .rdata
            1000 .reloc
            1000 .rsrc
            2000 .text
    
  • Rust example from above (std):

    Dump of file std.dll
    
    File Type: DLL
    
      Section contains the following exports for std.dll
    
        00000000 characteristics
            0.00 version
               1 ordinal base
               2 number of functions
               2 number of names
    
        ordinal hint RVA      name
    
              1    0 00001000 DllMain = DllMain
              2    1 00001010 rust_eh_personality = rust_eh_personality
    
      Summary
    
            1000 .data
            1000 .pdata
            1000 .rdata
            1000 .reloc
            1000 .text
    
  • Rust example from above (no_std):

    Dump of file no_std.dll
    
    File Type: DLL
    
      Section contains the following exports for no_std.dll
    
        00000000 characteristics
            0.00 version
               1 ordinal base
               1 number of functions
               1 number of names
    
        ordinal hint RVA      name
    
              1    0 00001000 _DllMainCRTStartup = _DllMainCRTStartup
    
      Summary
    
            1000 .rdata
            1000 .text
    

Expected output

DllMain and _DllMainCRTStartup should not be exported by name, just as in the first dumpbin output example of a Windows system dll.

Tested compiler version

rustc 1.41.0-nightly (c8ea4ace9 2019-12-14)
binary: rustc
commit-hash: c8ea4ace9213ae045123fdfeb59d1ac887656d31
commit-date: 2019-12-14
host: x86_64-pc-windows-msvc
release: 1.41.0-nightly
LLVM version: 9.0
@csmoe csmoe added A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows labels Dec 18, 2019
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2019
@MSxDOS
Copy link

MSxDOS commented Dec 19, 2019

I think the main issue here is that #[no_mangle] also exports the function instead of just demangling it which is unexpected and unintuitive. As it seems, the only solution we currently have is to use a custom empty DEF file. Note that even then the MSVC linker will still create an empty export directory. That's not the case with LLD-linker, though.

@MauriceKayser
Copy link
Author

I just had a short look because I wanted to try two ideas:

  1. Simply blacklist the two mentioned names and do not add them to the DEF file.
  2. Add some kind of optional #[no_mangle(export = "false")] mechanism.

To start off I tried modifying fn contains_extern_indicator(&self) -> bool in lib/rustlib/src/rust/src/librustc/hir/mod.rs to always return false and compiled with xbuild, but my change does not seem to be used by that. I guess I would need to recompile rustc, not core.

I can neither find any references to fn contains_extern_indicator(&self) -> bool, nor can I find the logic where the no_mangle attribute is actually parsed.

@ma6254
Copy link

ma6254 commented Aug 30, 2022

I desperately need some mechanism:
Perform some initialization actions (such as network, configuration files, etc.) before running the export function after the dll is loaded by the main program

@lz520520
Copy link

lz520520 commented Jan 6, 2024

Is there any solution to this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows Operating system: Windows O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants