From 6125220a26b82e7e0c68e10e04162a08a694c4f7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:41:22 -0700 Subject: [PATCH 1/7] Unwrap link_section --- src/abi.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/abi.md b/src/abi.md index 750d7814b1..ea10ff8c31 100644 --- a/src/abi.md +++ b/src/abi.md @@ -90,8 +90,7 @@ 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. r[abi.link_section.syntax] The `link_section` attribute uses the [MetaNameValueStr] syntax to specify the section name. @@ -106,8 +105,7 @@ 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. +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.duplicates] Only the first use of `link_section` on an item has effect. From c8daf769da0c3d84f1b33e9cf51aebffa5f77b00 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:41:57 -0700 Subject: [PATCH 2/7] Linkify attribute --- src/abi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abi.md b/src/abi.md index ea10ff8c31..17f535c3c7 100644 --- a/src/abi.md +++ b/src/abi.md @@ -90,10 +90,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 ad0d95e55cbdb2b8d42126ff9fd424988501baa3 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:42:14 -0700 Subject: [PATCH 3/7] 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 17f535c3c7..2ec57751d9 100644 --- a/src/abi.md +++ b/src/abi.md @@ -90,19 +90,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 675bbf41d416ea4d4f586cb37adea4774b9bf3cb Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:00:47 -0700 Subject: [PATCH 4/7] 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 2ec57751d9..af97e62e19 100644 --- a/src/abi.md +++ b/src/abi.md @@ -105,8 +105,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. @@ -114,6 +123,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 2d9c1ed7a367f52ecb879a89c562ea067a93f11a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:01:04 -0700 Subject: [PATCH 5/7] 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 af97e62e19..4a997d5fc5 100644 --- a/src/abi.md +++ b/src/abi.md @@ -124,7 +124,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 20ee4ebc2bc7701162c22af5026b7dbb1009382d Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:01:49 -0700 Subject: [PATCH 6/7] 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 4a997d5fc5..00f6a265dd 100644 --- a/src/abi.md +++ b/src/abi.md @@ -130,6 +130,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 80305a4bfa4ccd0143ef42a8b12ca9c797fd5683 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 22 Sep 2025 13:13:59 -0700 Subject: [PATCH 7/7] 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 00f6a265dd..bc6bccf479 100644 --- a/src/abi.md +++ b/src/abi.md @@ -86,6 +86,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 @@ -115,7 +116,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.