-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] Add #[export_ordinal(n)]
attribute
#3641
base: master
Are you sure you want to change the base?
[RFC] Add #[export_ordinal(n)]
attribute
#3641
Conversation
you should probably specify that the attribute is ignored everywhere other than Windows (or similar) targets. |
@programmerjake Thanks, I wasn't sure if ordinals were a thing on other targets. I'll update it accordingly. |
Ordinals are a specific feature in Windows DLLs, and are not present on other targets.
The attribute should not cause an error on non-windows targets. Instead, it should do nothing.
@rfcbot merge |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
Can we reuse Should this attribute be unsafe once unsafe attributes are implemented? I'm unsure if we can guarantee there will be no collisions with anything else linked into the program (including native libraries). |
I think this could be a good idea if there is no ambiguity. It could be confusing at a glance if a project has multiple
I'm not sure about how export_ordinal would be implemented, but if there's the chance of collisions, then I agree it should be marked as unsafe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the attribute itself modify symbol attributes such that the symbol is exported, or does it only modify the ordinal field? I'm having a hard time finding the docs for the rules determining when a function is exported are not. I think it has to do with whether they are public, which seems like it would have some drawbacks. I'm surprised by the example in the RFC:
#[link(name = "external_library")]
extern "C" {
#[export_ordinal(2)]
pub fn external_function();
}
In this case is the externally-declared function somehow being re-exported? Then it seems like we might need both #[link_ordinal(n)]
and #[export_ordinal(n)]
on the same function.
I'm not sure about how export_ordinal would be implemented, but if there's the chance of collisions, then I agree it should be marked as unsafe.
Can you add this as an unresolved question to the RFC?
- Removed a statement that should be an unresolved question instead. - Add unresolved question for whether export_ordinal should be marked unsafe when RFC3325 (Unsafe Attributes) is implemented.
@tmandry I took into consideration that some libraries could possibly be statically linked, and the possibility that someone may want to re-export a function. If this isn't a valid concern I'm happy to remove it - I just tried to think of every possibility. |
Are reexports even possible? |
I don't think that's true; if I try to use it on an export I get a warning: #[link_name = "actual_symbol_name"]
pub extern "C" fn name_in_rust() {}
Regardless, agreed that we should be consistent: Either |
Seems like I misremembered... I thought there was a way to set the link name independent of the item name. Maybe there isn't. |
Were you thinking of #[export_name = "exported_symbol_name"]
pub extern "C" fn name_in_rust() {} |
If we already use |
Yeah, that makes sense.
|
@rfcbot reviewed Agreed that we should stick with |
I noticed that the edition guide for unsafe attributes got merged. It says that Should I update the RFC to remove it from the unanswered questions, and clarify that in 2024 edition it should be marked as unsafe? Edit: I don't entirely understand how editions would work - if |
If it has the same risks as |
It's entirely possible that a dependency could mark something as a duplicate ordinal, so I believe the same risks apply. I'm not sure whether to word it as "in 2024 edition" or not though. |
I don't see a reason to make this unsafe only in some editions. We do that for old attributes solely due to backwards compatibility. New things should be properly unsafe from the start. |
Thanks for the clarification. I'll work on the changes in a bit. |
I have updated the RFC to mark I have also removed the section on re-exporting bindings, as after testing, I've confirmed that this is not valid code. |
This RFC adds an attribute that allows specifying the ordinal of an exported function when creating cdylibs on windows.
We already have a
#[link_ordinal(n)]
attribute which allows importing functions from DLLs by its ordinal, but we don't have any way of doing the opposite - specifying the ordinal of a function when we're making our own DLLs.Currently, you need to create a
lib.def
file and specify the ordinals, and then link it withcargo:rustc-cdylib-link-arg=/DEF
.The biggest downside of the current method is that once you specify a
.def
file, you will have to specify an ordinal for every function that you want to export from the DLL, or else it won't be present in the generated.lib
file. This can become very overwhelming if you have a lot of exported functions.This RFC would allow you to specify exported function ordinals like so:
Rendered