-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
-Zfunction-return={keep,thunk-extern}
option
This is intended to be used for Linux kernel RETHUNK builds. With this commit (optionally backported to Rust 1.73.0), plus a patched Linux kernel to pass the flag, I get a RETHUNK build with Rust enabled that is `objtool`-warning-free and is able to boot in QEMU and load a sample Rust kernel module. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
- Loading branch information
Showing
17 changed files
with
212 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/doc/unstable-book/src/compiler-flags/function-return.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# `function-return` | ||
|
||
The tracking issue for this feature is: https://github.com/rust-lang/rust/issues/116853. | ||
|
||
------------------------ | ||
|
||
Option `-Zfunction-return` controls how function returns are converted. | ||
|
||
It is equivalent to [Clang]'s and [GCC]'s `-mfunction-return`. The Linux kernel | ||
uses it for RETHUNK builds. For details, see [LLVM commit 2240d72f15f3] ("[X86] | ||
initial -mfunction-return=thunk-extern support") which introduces the feature. | ||
|
||
Supported values for this option are: | ||
|
||
- `keep`: do not convert function returns. | ||
- `thunk-extern`: convert function returns (`ret`) to jumps (`jmp`) | ||
to an external symbol called `__x86_return_thunk`. | ||
|
||
Like in Clang, GCC's values `thunk` and `thunk-inline` are not supported. | ||
|
||
Only x86 and non-large code models are supported. | ||
|
||
[Clang]: https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mfunction-return | ||
[GCC]: https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#index-mfunction-return | ||
[LLVM commit 2240d72f15f3]: https://github.com/llvm/llvm-project/commit/2240d72f15f3b7b9d9fb65450f9bf635fd310f6f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Test that the function return is (not) converted into a jump to the thunk | ||
// when the `-Zfunction-return={keep,thunk-extern}` flag is (not) set. | ||
|
||
// revisions: unset keep thunk-extern keep-thunk-extern thunk-extern-keep | ||
// assembly-output: emit-asm | ||
// compile-flags: -O | ||
// [keep] compile-flags: -Zfunction-return=keep | ||
// [thunk-extern] compile-flags: -Zfunction-return=thunk-extern | ||
// [keep-thunk-extern] compile-flags: -Zfunction-return=keep -Zfunction-return=thunk-extern | ||
// [thunk-extern-keep] compile-flags: -Zfunction-return=thunk-extern -Zfunction-return=keep | ||
// only-x86_64 | ||
|
||
#![crate_type = "lib"] | ||
|
||
// CHECK-LABEL: foo: | ||
#[no_mangle] | ||
pub unsafe fn foo() { | ||
// unset: ret | ||
// unset-NOT: jmp __x86_return_thunk | ||
// keep: ret | ||
// keep-NOT: jmp __x86_return_thunk | ||
// thunk-extern: jmp __x86_return_thunk | ||
// thunk-extern-NOT: ret | ||
// keep-thunk-extern: jmp __x86_return_thunk | ||
// keep-thunk-extern-NOT: ret | ||
// thunk-extern-keep: ret | ||
// thunk-extern-keep-NOT: jmp __x86_return_thunk | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Test that the `fn_ret_thunk_extern` function attribute is (not) emitted when | ||
// the `-Zfunction-return={keep,thunk-extern}` flag is (not) set. | ||
|
||
// revisions: unset keep thunk-extern keep-thunk-extern thunk-extern-keep | ||
// needs-llvm-components: x86 | ||
// compile-flags: --target x86_64-unknown-linux-gnu | ||
// [keep] compile-flags: -Zfunction-return=keep | ||
// [thunk-extern] compile-flags: -Zfunction-return=thunk-extern | ||
// [keep-thunk-extern] compile-flags: -Zfunction-return=keep -Zfunction-return=thunk-extern | ||
// [thunk-extern-keep] compile-flags: -Zfunction-return=thunk-extern -Zfunction-return=keep | ||
|
||
#![crate_type = "lib"] | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
#[no_mangle] | ||
pub fn foo() { | ||
// CHECK: @foo() unnamed_addr #0 | ||
|
||
// unset-NOT: fn_ret_thunk_extern | ||
// keep-NOT: fn_ret_thunk_extern | ||
// thunk-extern: attributes #0 = { {{.*}}fn_ret_thunk_extern{{.*}} } | ||
// keep-thunk-extern: attributes #0 = { {{.*}}fn_ret_thunk_extern{{.*}} } | ||
// thunk-extern-keep-NOT: fn_ret_thunk_extern | ||
} |
4 changes: 4 additions & 0 deletions
4
tests/ui/invalid-compile-flags/function-return/requires-x86-or-x86_64.aarch64.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
error: `-Zfunction-return` (except `keep`) is only supported on x86 and x86_64 | ||
|
||
error: aborting due to previous error | ||
|
20 changes: 20 additions & 0 deletions
20
tests/ui/invalid-compile-flags/function-return/requires-x86-or-x86_64.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// revisions: x86 x86_64 aarch64 | ||
|
||
// compile-flags: -Zfunction-return=thunk-extern | ||
|
||
//[x86] check-pass | ||
//[x86] needs-llvm-components: x86 | ||
//[x86] compile-flags: --target i686-unknown-linux-gnu | ||
|
||
//[x86_64] check-pass | ||
//[x86_64] needs-llvm-components: x86 | ||
//[x86_64] compile-flags: --target x86_64-unknown-linux-gnu | ||
|
||
//[aarch64] check-fail | ||
//[aarch64] needs-llvm-components: aarch64 | ||
//[aarch64] compile-flags: --target aarch64-unknown-linux-gnu | ||
//[aarch64] error-pattern: `-Zfunction-return` (except `keep`) is only supported on x86 and x86_64 | ||
|
||
#![feature(no_core)] | ||
#![no_core] | ||
#![no_main] |
4 changes: 4 additions & 0 deletions
4
...lid-compile-flags/function-return/thunk-extern-requires-non-large-code-model.large.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
error: `-Zfunction-return=thunk-extern` is only supported on non-large code models | ||
|
||
error: aborting due to previous error | ||
|
21 changes: 21 additions & 0 deletions
21
tests/ui/invalid-compile-flags/function-return/thunk-extern-requires-non-large-code-model.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// revisions: small kernel medium large | ||
|
||
// needs-llvm-components: x86 | ||
// compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-return=thunk-extern | ||
|
||
//[small] check-pass | ||
//[small] compile-flags: -Ccode-model=small | ||
|
||
//[kernel] check-pass | ||
//[kernel] compile-flags: -Ccode-model=kernel | ||
|
||
//[medium] check-pass | ||
//[medium] compile-flags: -Ccode-model=medium | ||
|
||
//[large] check-fail | ||
//[large] compile-flags: -Ccode-model=large | ||
//[large] error-pattern: `-Zfunction-return=thunk-extern` is only supported on non-large code models | ||
|
||
#![feature(no_core)] | ||
#![no_core] | ||
#![no_main] |