Skip to content

Update link_section to use the attribute template #1903

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,47 @@ r[abi.link_section]
## The `link_section` attribute

r[abi.link_section.intro]
The *`link_section` attribute* specifies the section of the object file that a
[function] or [static]'s content will be placed into.
The *`link_section` [attribute]* specifies the section of the object file that a [function] or [static]'s content will be placed into.

> [!EXAMPLE]
> <!-- no_run: don't link. The format of the section name is platform-specific. -->
> ```rust,no_run
> #[unsafe(no_mangle)]
> #[unsafe(link_section = ".example_section")]
> pub static VAR1: u32 = 1;
> ```

r[abi.link_section.syntax]
The `link_section` attribute uses the [MetaNameValueStr] syntax to specify the section name.

<!-- no_run: don't link. The format of the section name is platform-specific. -->
```rust,no_run
#[unsafe(no_mangle)]
#[unsafe(link_section = ".example_section")]
pub static VAR1: u32 = 1;
```
r[abi.link_section.allowed-positions]
The `link_section` attribute may only be applied to:

- [Static items][items.static]
- [Free functions][items.fn]
- [Inherent associated functions][items.associated.fn]
- [Trait impl functions][items.impl.trait]
- [Trait definition functions][items.traits] with a body

> [!NOTE]
> `rustc` currently warns in other positions, but this may be rejected in the future.

r[abi.link_section.duplicates]
Only the last instance of `link_section` on an item is honored. Previous `link_section` attributes are ignored.

> [!NOTE]
> `rustc` currently warns on preceding duplicate `link_section` attributes. This may become an error in the future.

r[abi.link_section.unsafe]
This attribute is unsafe as it allows users to place data and code into sections
of memory not expecting them, such as mutable data into read-only areas.
The `link_section` attribute must be marked with [`unsafe`][attributes.safety] because it allows users to place data and code into sections of memory not expecting them, such as mutable data into read-only areas.

r[abi.link_section.edition2024]
> [!EDITION-2024]
> Before the 2024 edition it is allowed to use the `link_section` attribute without the `unsafe` qualification.

r[abi.link_section.null]
The section name must not contain a [NUL] character.

r[abi.export_name]
## The `export_name` attribute

Expand Down