From c10c30820608fe7ccc81807fe1144b8d62c01ffe Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:41:57 -0700 Subject: [PATCH 1/6] Linkify attribute --- src/abi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abi.md b/src/abi.md index cf6f32c90e..a9a2a851fc 100644 --- a/src/abi.md +++ b/src/abi.md @@ -86,10 +86,10 @@ 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. r[abi.link_section.syntax] The `link_section` attribute uses the [MetaNameValueStr] syntax to specify the section name. +The *`link_section` [attribute]* specifies the section of the object file that a [function] or [static]'s content will be placed into. ```rust,no_run From 3e23917523a46ea6cb309ca45a5034b9c03c7fc9 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:42:14 -0700 Subject: [PATCH 2/6] Move link_section example to the intro --- src/abi.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/abi.md b/src/abi.md index a9a2a851fc..3608196848 100644 --- a/src/abi.md +++ b/src/abi.md @@ -86,19 +86,20 @@ 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. + +> [!EXAMPLE] +> +> ```rust,no_run +> # #[cfg(target_os = "linux")] { +> #[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. -The *`link_section` [attribute]* specifies the section of the object file that a [function] or [static]'s content will be placed into. - - -```rust,no_run -# #[cfg(target_os = "linux")] { -#[unsafe(no_mangle)] -#[unsafe(link_section = ".example_section")] -pub static VAR1: u32 = 1; -# } -``` 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. From 1206162b031df1f011585cc3c892e9c1fb49cf3c Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:00:47 -0700 Subject: [PATCH 3/6] Add link_section attribute template rules --- src/abi.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/abi.md b/src/abi.md index 3608196848..96bd356f2b 100644 --- a/src/abi.md +++ b/src/abi.md @@ -101,8 +101,17 @@ The *`link_section` [attribute]* specifies the section of the object file that a r[abi.link_section.syntax] The `link_section` attribute uses the [MetaNameValueStr] syntax to specify the section name. -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. +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 first use of `link_section` on an item has effect. @@ -110,6 +119,9 @@ Only the first use of `link_section` on an item has effect. > [!NOTE] > `rustc` lints against any use following the first with a future-compatibility warning. 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. + r[abi.link_section.edition2024] > [!EDITION-2024] > Before the 2024 edition it is allowed to use the `link_section` attribute without the `unsafe` qualification. From fd01419be4a44e72cdb69e7dede6caf189c5a854 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:01:04 -0700 Subject: [PATCH 4/6] Reword abi.link_section.unsafe to match attribute template --- src/abi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abi.md b/src/abi.md index 96bd356f2b..d0960dffd9 100644 --- a/src/abi.md +++ b/src/abi.md @@ -120,7 +120,7 @@ Only the first use of `link_section` on an item has effect. > `rustc` lints against any use following the first with a future-compatibility warning. 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] From 495a9b15f92a85617c10f9c6e9127409dc819846 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:01:49 -0700 Subject: [PATCH 5/6] Add abi.link_section.null This is checked here: https://github.com/rust-lang/rust/blob/5d707b07e42766c080c5012869c9988a18dcbb83/compiler/rustc_codegen_ssa/src/codegen_attrs.rs#L331-L340 --- src/abi.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/abi.md b/src/abi.md index d0960dffd9..e5d1d30462 100644 --- a/src/abi.md +++ b/src/abi.md @@ -126,6 +126,9 @@ 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 From 0230a016ee778c3f8d1573d3db68cd3e2409b772 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 22 Sep 2025 13:13:59 -0700 Subject: [PATCH 6/6] Minor update of `link_section` More closely align with the template, and some minor word tweaks. --- src/abi.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/abi.md b/src/abi.md index e5d1d30462..19e8a034f5 100644 --- a/src/abi.md +++ b/src/abi.md @@ -82,6 +82,7 @@ r[abi.no_mangle.edition2024] > [!EDITION-2024] > Before the 2024 edition it is allowed to use the `no_mangle` attribute without the `unsafe` qualification. + r[abi.link_section] ## The `link_section` attribute @@ -111,7 +112,7 @@ The `link_section` attribute may only be applied to: - [Trait definition functions][items.traits] with a body > [!NOTE] -> `rustc` currently warns in other positions, but this may be rejected in the future. +> `rustc` ignores use in other positions but lints against it. This may become an error in the future. r[abi.link_section.duplicates] Only the first use of `link_section` on an item has effect.