-
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
10 changed files
with
125 additions
and
4 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
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 is 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 | ||
} |