From 86490eb9bceb4cbbed749a1f3c27d048803d97d6 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 48cc067884..07334d6d3d 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. @@ -104,8 +103,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.edition2024] > [!EDITION-2024] From eafffb5dea53a723dae8d7b9b45a988f5ef3b15c 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 07334d6d3d..fec75bddda 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 5817070a0e0ec60650f6e8a73425c4cba42cc3e4 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 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/abi.md b/src/abi.md index fec75bddda..2460a12831 100644 --- a/src/abi.md +++ b/src/abi.md @@ -90,17 +90,18 @@ 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 +> #[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 -#[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 a51b1d1c395e6a250c7d4255f197f1237b66d396 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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/abi.md b/src/abi.md index 2460a12831..7e6b6dfbe1 100644 --- a/src/abi.md +++ b/src/abi.md @@ -103,6 +103,24 @@ 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.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. + +> [!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. From d078497b5b8d359cbb7ab8a85270abefd2b3fbd6 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 7e6b6dfbe1..bf263c6de3 100644 --- a/src/abi.md +++ b/src/abi.md @@ -122,7 +122,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 5520f98480f4b4790fe25d0923b57bbb3562b763 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 bf263c6de3..9e426081ec 100644 --- a/src/abi.md +++ b/src/abi.md @@ -128,6 +128,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 c2cdd82f16cabb98a54945dcf67e14f612863639 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 9e426081ec..348337810d 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 @@ -113,7 +114,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.