@@ -51,6 +51,80 @@ r[attributes.codegen.cold]
5151The * ` cold ` [ attribute] * suggests that the attributed function is unlikely to
5252be called.
5353
54+ ## The ` naked ` attribute
55+
56+ The * ` naked ` [ attribute] * may be applied to a function in order to prevent the compiler
57+ from emitting a function prologue.
58+
59+ ### Requirements
60+
61+ Any function marked with the ` naked ` attribute must meet the following requirements;
62+ failure to do so will result in a compiler error.
63+
64+ * The [ function body] must consist of exactly one [ ` asm! ` ] macro invocation,
65+ which may be enclosed within an [ unsafe block] .
66+ * This ` asm! ` invocation must not contain any [ operands]
67+ except for ` const ` and ` sym ` operands.
68+ * This ` asm! ` invocation must specify the ` noreturn ` [ option] ,
69+ and must not specify any other options except for ` att_syntax ` .
70+ * The function must not be marked with the [ ` inline ` ] attribute.
71+
72+ ### Recommendations
73+
74+ Any function marked with the ` naked ` attribute should adhere to the following recommendations;
75+ failure to do so will result in a compiler warning.
76+
77+ * The function should feature an [ extern function qualifier] that is not ` extern "Rust" ` .
78+ * All arguments and return types of the function should be [ FFI-safe] .
79+
80+ ### Effects
81+
82+ Marking a function with the ` naked ` attribute has the following effects:
83+
84+ * The compiler will not generate a prologue for this function.
85+ Within the function, all registers will remain precisely as they were set up
86+ by its caller.
87+ * The compiler will suppress the [ ` unused_variables ` ] lint for this function.
88+
89+ ### Notes
90+
91+ * The [ rules for inline assembly] ordinarily consider it undefined behavior to
92+ refer to registers not specified as input operands, or to modify
93+ registers not specified as output operands.
94+ The reason for this is because ordinarily an ` asm! ` invocation cannot guarantee
95+ the state of the registers surrounding the assembly block.
96+ However, in naked functions the state of the registers is guaranteed
97+ by adherence to the specified calling convention.
98+ Therefore, it is not undefined behavior for the ` asm! ` invocation in a naked function
99+ to refer to registers without specifying them as operands.
100+ * A naked function that makes use of registers in a way that does not conform
101+ to the specified calling convention imposes additional safety invariants on its caller,
102+ and therefore must be marked as an [ unsafe function] .
103+ * Implementations may assume that naked functions never unwind.
104+ Unwinding through a naked function is undefined behavior.
105+ * The semantics of naked functions require implementations to set up the call stack
106+ according to the specified calling convention before executing a naked function,
107+ even in contexts where setting up the call stack would ordinarily be unnecessary,
108+ such as when the function is inlined.
109+ An implementation can fulfill this requirement by guaranteeing that naked functions
110+ are never inlined.
111+ However, implementations are not currently required to guarantee that naked functions
112+ are never inlined.
113+ In the future it may become a requirement for implementations to guarantee that
114+ naked functions are never inlined;
115+ users must not rely on any observable behavior that may result from inlining.
116+ * Although implementations are prohibited from generating code for a naked function that
117+ contains any instructions that precede the naked function's ` asm! ` block,
118+ under some circumstances, implementations may generate code that contains instructions
119+ * after* a naked function's ` asm! ` block.
120+ In the future it may become a requirement for implementations to guarantee
121+ the absence of any instructions following a naked function's ` asm! ` block;
122+ users must not rely on the presence of any trailing instructions.
123+ If a user of the ` naked ` attribute relies on the absence of trailing instructions
124+ for correctness, for the time being it is the user's responsibility to ensure that
125+ the instructions truly are absent,
126+ for example by passing any necessary code generation flags to the compiler.
127+
54128## The ` no_builtins ` attribute
55129
56130r[ attributes.codegen.no_builtins]
@@ -469,14 +543,24 @@ trait object whose methods are attributed.
469543[ `-C target-feature` ] : ../../rustc/codegen-options/index.html#target-feature
470544[ `is_x86_feature_detected` ] : ../../std/arch/macro.is_x86_feature_detected.html
471545[ `is_aarch64_feature_detected` ] : ../../std/arch/macro.is_aarch64_feature_detected.html
546+ [ `naked_asm!` ] : ../inline-assembly.md
547+ [ `inline` ] : #the-inline-attribute
472548[ `target_feature` conditional compilation option ] : ../conditional-compilation.md#target_feature
549+ [ `unused_variables` ] : ../../rustc/lints/listing/warn-by-default.html#unused-variables
473550[ attribute ] : ../attributes.md
474551[ attributes ] : ../attributes.md
552+ [ extern function qualifier ] : ../items/functions.md#extern-function-qualifier
553+ [ FFI-safe ] : ../../rustc/lints/listing/warn-by-default.html#improper-ctypes-definitions
554+ [ function body ] : ../items/functions.md#function-body
475555[ functions ] : ../items/functions.md
556+ [ operands ] : ../inline-assembly.md#operand-type
557+ [ option ] : ../inline-assembly.md#options
558+ [ rules for inline assembly ] : ../inline-assembly.md#rules-for-inline-assembly
476559[ target architecture ] : ../conditional-compilation.md#target_arch
477560[ trait ] : ../items/traits.md
478561[ undefined behavior ] : ../behavior-considered-undefined.md
479- [ unsafe function ] : ../unsafe-keyword.md
562+ [ unsafe block ] : ../unsafe-blocks.md
563+ [ unsafe function ] : ../unsafe-functions.md
480564[ rust-abi ] : ../items/external-blocks.md#abi
481565[ `Location` ] : core::panic::Location
482566
0 commit comments