From 1cefa1551837d5e2ffcb5187d0ca383a036f5173 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:41:22 -0700 Subject: [PATCH 1/6] 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 c0f7ed78a..88bae304d 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 3eea3d6e1cc4041ee58ad1c94896cacc2fb94dee Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:41:57 -0700 Subject: [PATCH 2/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 88bae304d..b72d4e2ac 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 54632d1ae88f2ec62b544351d73e0bc20becc125 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:42:14 -0700 Subject: [PATCH 3/6] 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 b72d4e2ac..1f7a2b13b 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 45e5c162aa4dedd27d313340f4ea6a59acdf8123 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:00:47 -0700 Subject: [PATCH 4/6] 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 1f7a2b13b..811d199e5 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 a18b64829e958b94280357b4ddfe61a74952ebc7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:01:04 -0700 Subject: [PATCH 5/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 811d199e5..0df88683c 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 a4ef1ce2d0dfbd6101659c1b2da79e0de478c0e7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 17:01:49 -0700 Subject: [PATCH 6/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 0df88683c..06010c13f 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