From bb6b8bf7e0e6bbcbecd8dc8abbbb500540420998 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 c0f7ed78a6..88bae304d2 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 24fedea9987b67985be530e47f9362657ac48d9b 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 88bae304d2..b72d4e2acf 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 f81f9f89e75e037aaed910de4f05122e731d7fec 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 b72d4e2acf..1f7a2b13bb 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 0a7306d10b5e88cc76134dfddaad940596dce2a3 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 1f7a2b13bb..811d199e5a 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 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. From e9abfb7a56203f96b8ec669e1eb94eac712473da 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 811d199e5a..0df88683c1 100644 --- a/src/abi.md +++ b/src/abi.md @@ -122,7 +122,7 @@ Only the last instance of `link_section` on an item is honored. Previous `link_s > `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] From f564619d2776e573910fd6ca45b402a1cbf6ad28 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 0df88683c1..06010c13f4 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 f629545659717fcec97355b40297d8843bc23575 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 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/abi.md b/src/abi.md index 06010c13f4..04d03fdfb5 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,13 +114,13 @@ 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 last instance of `link_section` on an item is honored. Previous `link_section` attributes are ignored. +Only the last use of `link_section` on an item has effect. > [!NOTE] -> `rustc` currently warns on preceding duplicate `link_section` attributes. This may become an error in the future. +> `rustc` lints against any use preceding the last. This may become an error in the future. r[abi.link_section.unsafe] 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.