Skip to content

Private #[no_mangle] symbols are exported from a cdylib #98449

Open
@nxtn-staged

Description

@nxtn-staged

In C++ (MSVC) un-mangling and exporting are controlled via extern "C" and __declspec(dllexport) respectively.

extern "C" {
    namespace to_export {
        __declspec(dllexport) int64_t foo() {
            int64_t bar();
            return bar();
        }
    }
    namespace not_to_export {
        int32_t bar() {
            return 42;
        }
    }
}
    00000000 characteristics
    FFFFFFFF time date stamp
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00001000 foo = foo

In Rust, however, all #[no_mangle] functions are exported from the final cdylib.

pub mod to_export {
    #[no_mangle]
    pub unsafe extern "system" fn foo() -> i64 {
        extern "system" {
            fn bar() -> i64;
        }
        bar()
    }
}
mod not_to_export {
    #[no_mangle]
    extern "system" fn bar() -> i32 {
        42
    }
}
    00000000 characteristics
    FFFFFFFF time date stamp
        0.00 version
           1 ordinal base
           2 number of functions
           2 number of names

    ordinal hint RVA      name

          1    0 00001000 bar = bar
          2    1 00001010 foo = foo

It seems that it is not possible to define an un-mangled and un-exported symbol in Rust.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions