Skip to content

Commit f61d372

Browse files
committed
updpatch: rust 1:1.77.2-1
Backport rust-lang/rust#123612 to fix cross-language LTO issues in advance.
1 parent d4aa46a commit f61d372

File tree

2 files changed

+302
-12
lines changed

2 files changed

+302
-12
lines changed

rust/riscv-target-abi-fix.patch

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
Index: rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/back/lto.rs
2+
===================================================================
3+
--- rustc-1.77.2-src.orig/compiler/rustc_codegen_llvm/src/back/lto.rs
4+
+++ rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/back/lto.rs
5+
@@ -607,7 +607,7 @@ pub(crate) fn run_pass_manager(
6+
"LTOPostLink".as_ptr().cast(),
7+
11,
8+
) {
9+
- llvm::LLVMRustAddModuleFlag(
10+
+ llvm::LLVMRustAddModuleFlagU32(
11+
module.module_llvm.llmod(),
12+
llvm::LLVMModFlagBehavior::Error,
13+
c"LTOPostLink".as_ptr().cast(),
14+
Index: rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/context.rs
15+
===================================================================
16+
--- rustc-1.77.2-src.orig/compiler/rustc_codegen_llvm/src/context.rs
17+
+++ rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/context.rs
18+
@@ -203,13 +203,13 @@ pub unsafe fn create_module<'ll>(
19+
// to ensure intrinsic calls don't use it.
20+
if !sess.needs_plt() {
21+
let avoid_plt = c"RtLibUseGOT".as_ptr().cast();
22+
- llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
23+
+ llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
24+
}
25+
26+
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
27+
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
28+
let canonical_jump_tables = c"CFI Canonical Jump Tables".as_ptr().cast();
29+
- llvm::LLVMRustAddModuleFlag(
30+
+ llvm::LLVMRustAddModuleFlagU32(
31+
llmod,
32+
llvm::LLVMModFlagBehavior::Override,
33+
canonical_jump_tables,
34+
@@ -220,7 +220,7 @@ pub unsafe fn create_module<'ll>(
35+
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
36+
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
37+
let enable_split_lto_unit = c"EnableSplitLTOUnit".as_ptr().cast();
38+
- llvm::LLVMRustAddModuleFlag(
39+
+ llvm::LLVMRustAddModuleFlagU32(
40+
llmod,
41+
llvm::LLVMModFlagBehavior::Override,
42+
enable_split_lto_unit,
43+
@@ -231,7 +231,7 @@ pub unsafe fn create_module<'ll>(
44+
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
45+
if sess.is_sanitizer_kcfi_enabled() {
46+
let kcfi = c"kcfi".as_ptr().cast();
47+
- llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
48+
+ llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
49+
}
50+
51+
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
52+
@@ -240,7 +240,7 @@ pub unsafe fn create_module<'ll>(
53+
CFGuard::Disabled => {}
54+
CFGuard::NoChecks => {
55+
// Set `cfguard=1` module flag to emit metadata only.
56+
- llvm::LLVMRustAddModuleFlag(
57+
+ llvm::LLVMRustAddModuleFlagU32(
58+
llmod,
59+
llvm::LLVMModFlagBehavior::Warning,
60+
c"cfguard".as_ptr() as *const _,
61+
@@ -249,7 +249,7 @@ pub unsafe fn create_module<'ll>(
62+
}
63+
CFGuard::Checks => {
64+
// Set `cfguard=2` module flag to emit metadata and checks.
65+
- llvm::LLVMRustAddModuleFlag(
66+
+ llvm::LLVMRustAddModuleFlagU32(
67+
llmod,
68+
llvm::LLVMModFlagBehavior::Warning,
69+
c"cfguard".as_ptr() as *const _,
70+
@@ -267,26 +267,26 @@ pub unsafe fn create_module<'ll>(
71+
};
72+
73+
if sess.target.arch == "aarch64" {
74+
- llvm::LLVMRustAddModuleFlag(
75+
+ llvm::LLVMRustAddModuleFlagU32(
76+
llmod,
77+
behavior,
78+
c"branch-target-enforcement".as_ptr().cast(),
79+
bti.into(),
80+
);
81+
- llvm::LLVMRustAddModuleFlag(
82+
+ llvm::LLVMRustAddModuleFlagU32(
83+
llmod,
84+
behavior,
85+
c"sign-return-address".as_ptr().cast(),
86+
pac_ret.is_some().into(),
87+
);
88+
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
89+
- llvm::LLVMRustAddModuleFlag(
90+
+ llvm::LLVMRustAddModuleFlagU32(
91+
llmod,
92+
behavior,
93+
c"sign-return-address-all".as_ptr().cast(),
94+
pac_opts.leaf.into(),
95+
);
96+
- llvm::LLVMRustAddModuleFlag(
97+
+ llvm::LLVMRustAddModuleFlagU32(
98+
llmod,
99+
behavior,
100+
c"sign-return-address-with-bkey".as_ptr().cast(),
101+
@@ -302,7 +302,7 @@ pub unsafe fn create_module<'ll>(
102+
103+
// Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang).
104+
if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
105+
- llvm::LLVMRustAddModuleFlag(
106+
+ llvm::LLVMRustAddModuleFlagU32(
107+
llmod,
108+
llvm::LLVMModFlagBehavior::Override,
109+
c"cf-protection-branch".as_ptr().cast(),
110+
@@ -310,7 +310,7 @@ pub unsafe fn create_module<'ll>(
111+
)
112+
}
113+
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
114+
- llvm::LLVMRustAddModuleFlag(
115+
+ llvm::LLVMRustAddModuleFlagU32(
116+
llmod,
117+
llvm::LLVMModFlagBehavior::Override,
118+
c"cf-protection-return".as_ptr().cast(),
119+
@@ -319,7 +319,7 @@ pub unsafe fn create_module<'ll>(
120+
}
121+
122+
if sess.opts.unstable_opts.virtual_function_elimination {
123+
- llvm::LLVMRustAddModuleFlag(
124+
+ llvm::LLVMRustAddModuleFlagU32(
125+
llmod,
126+
llvm::LLVMModFlagBehavior::Error,
127+
c"Virtual Function Elim".as_ptr().cast(),
128+
@@ -329,7 +329,7 @@ pub unsafe fn create_module<'ll>(
129+
130+
// Set module flag to enable Windows EHCont Guard (/guard:ehcont).
131+
if sess.opts.unstable_opts.ehcont_guard {
132+
- llvm::LLVMRustAddModuleFlag(
133+
+ llvm::LLVMRustAddModuleFlagU32(
134+
llmod,
135+
llvm::LLVMModFlagBehavior::Warning,
136+
c"ehcontguard".as_ptr() as *const _,
137+
@@ -354,6 +354,22 @@ pub unsafe fn create_module<'ll>(
138+
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
139+
);
140+
141+
+ // Emit RISC-V specific target-abi metadata
142+
+ // to workaround lld as the LTO plugin not
143+
+ // correctly setting target-abi for the LTO object
144+
+ // FIXME: https://github.com/llvm/llvm-project/issues/50591
145+
+ // If llvm_abiname is empty, emit nothing.
146+
+ let llvm_abiname = &sess.target.options.llvm_abiname;
147+
+ if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
148+
+ llvm::LLVMRustAddModuleFlagString(
149+
+ llmod,
150+
+ llvm::LLVMModFlagBehavior::Error,
151+
+ c"target-abi".as_ptr(),
152+
+ llvm_abiname.as_ptr().cast(),
153+
+ llvm_abiname.len(),
154+
+ );
155+
+ }
156+
+
157+
// Add module flags specified via -Z llvm_module_flag
158+
for (key, value, behavior) in &sess.opts.unstable_opts.llvm_module_flag {
159+
let key = format!("{key}\0");
160+
@@ -369,7 +385,7 @@ pub unsafe fn create_module<'ll>(
161+
// We already checked this during option parsing
162+
_ => unreachable!(),
163+
};
164+
- llvm::LLVMRustAddModuleFlag(llmod, behavior, key.as_ptr().cast(), *value)
165+
+ llvm::LLVMRustAddModuleFlagU32(llmod, behavior, key.as_ptr().cast(), *value)
166+
}
167+
168+
llmod
169+
Index: rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
170+
===================================================================
171+
--- rustc-1.77.2-src.orig/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
172+
+++ rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
173+
@@ -110,7 +110,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<
174+
.unstable_opts
175+
.dwarf_version
176+
.unwrap_or(sess.target.default_dwarf_version);
177+
- llvm::LLVMRustAddModuleFlag(
178+
+ llvm::LLVMRustAddModuleFlagU32(
179+
self.llmod,
180+
llvm::LLVMModFlagBehavior::Warning,
181+
c"Dwarf Version".as_ptr().cast(),
182+
@@ -118,7 +118,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<
183+
);
184+
} else {
185+
// Indicate that we want CodeView debug information on MSVC
186+
- llvm::LLVMRustAddModuleFlag(
187+
+ llvm::LLVMRustAddModuleFlagU32(
188+
self.llmod,
189+
llvm::LLVMModFlagBehavior::Warning,
190+
c"CodeView".as_ptr().cast(),
191+
@@ -127,7 +127,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<
192+
}
193+
194+
// Prevent bitcode readers from deleting the debug info.
195+
- llvm::LLVMRustAddModuleFlag(
196+
+ llvm::LLVMRustAddModuleFlagU32(
197+
self.llmod,
198+
llvm::LLVMModFlagBehavior::Warning,
199+
c"Debug Info Version".as_ptr().cast(),
200+
Index: rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
201+
===================================================================
202+
--- rustc-1.77.2-src.orig/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
203+
+++ rustc-1.77.2-src/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
204+
@@ -1792,12 +1792,21 @@ extern "C" {
205+
///
206+
/// In order for Rust-C LTO to work, module flags must be compatible with Clang. What
207+
/// "compatible" means depends on the merge behaviors involved.
208+
- pub fn LLVMRustAddModuleFlag(
209+
+ pub fn LLVMRustAddModuleFlagU32(
210+
M: &Module,
211+
merge_behavior: LLVMModFlagBehavior,
212+
name: *const c_char,
213+
value: u32,
214+
);
215+
+
216+
+ pub fn LLVMRustAddModuleFlagString(
217+
+ M: &Module,
218+
+ merge_behavior: LLVMModFlagBehavior,
219+
+ name: *const c_char,
220+
+ value: *const c_char,
221+
+ value_len: size_t,
222+
+ );
223+
+
224+
pub fn LLVMRustHasModuleFlag(M: &Module, name: *const c_char, len: size_t) -> bool;
225+
226+
pub fn LLVMRustDIBuilderCreate(M: &Module) -> &mut DIBuilder<'_>;
227+
Index: rustc-1.77.2-src/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
228+
===================================================================
229+
--- rustc-1.77.2-src.orig/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
230+
+++ rustc-1.77.2-src/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
231+
@@ -748,7 +748,7 @@ extern "C" uint32_t LLVMRustVersionMinor
232+
233+
extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; }
234+
235+
-extern "C" void LLVMRustAddModuleFlag(
236+
+extern "C" void LLVMRustAddModuleFlagU32(
237+
LLVMModuleRef M,
238+
Module::ModFlagBehavior MergeBehavior,
239+
const char *Name,
240+
@@ -756,6 +756,16 @@ extern "C" void LLVMRustAddModuleFlag(
241+
unwrap(M)->addModuleFlag(MergeBehavior, Name, Value);
242+
}
243+
244+
+extern "C" void LLVMRustAddModuleFlagString(
245+
+ LLVMModuleRef M,
246+
+ Module::ModFlagBehavior MergeBehavior,
247+
+ const char *Name,
248+
+ const char *Value,
249+
+ size_t ValueLen) {
250+
+ unwrap(M)->addModuleFlag(MergeBehavior, Name,
251+
+ MDString::get(unwrap(M)->getContext(), StringRef(Value, ValueLen)));
252+
+}
253+
+
254+
extern "C" bool LLVMRustHasModuleFlag(LLVMModuleRef M, const char *Name,
255+
size_t Len) {
256+
return unwrap(M)->getModuleFlag(StringRef(Name, Len)) != nullptr;
257+
Index: rustc-1.77.2-src/tests/codegen/riscv-target-abi.rs
258+
===================================================================
259+
--- /dev/null
260+
+++ rustc-1.77.2-src/tests/codegen/riscv-target-abi.rs
261+
@@ -0,0 +1,20 @@
262+
+//@ revisions:riscv64gc riscv32gc riscv32imac
263+
+
264+
+//@[riscv64gc] compile-flags: --target=riscv64gc-unknown-linux-gnu
265+
+//@[riscv64gc] needs-llvm-components: riscv
266+
+// riscv64gc: !{i32 1, !"target-abi", !"lp64d"}
267+
+
268+
+//@[riscv32gc] compile-flags: --target=riscv32gc-unknown-linux-musl
269+
+//@[riscv32gc] needs-llvm-components: riscv
270+
+// riscv32gc: !{i32 1, !"target-abi", !"ilp32d"}
271+
+
272+
+//@[riscv32imac] compile-flags: --target=riscv32imac-unknown-none-elf
273+
+//@[riscv32imac] needs-llvm-components: riscv
274+
+// riscv32imac-NOT: !"target-abi"
275+
+
276+
+#![feature(no_core, lang_items)]
277+
+#![crate_type = "lib"]
278+
+#![no_core]
279+
+
280+
+#[lang = "sized"]
281+
+trait Sized {}

rust/riscv64.patch

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
libffi
1717
lld
1818
llvm
19-
@@ -42,6 +40,7 @@ makedepends=(
20-
python
21-
rust
22-
wasi-libc
23-
+ git
24-
)
25-
checkdepends=(
26-
gdb
27-
@@ -89,9 +88,8 @@ link-shared = true
19+
@@ -80,6 +78,9 @@ prepare() {
20+
# Use our wasm-ld
21+
patch -Np1 -i ../0004-compiler-Use-wasm-ld-for-wasm-targets.patch
22+
23+
+ # Backport https://github.com/rust-lang/rust/pull/123612 to fix cross-language LTO issues
24+
+ patch -Np1 -i ../riscv-target-abi-fix.patch
25+
+
26+
cat >config.toml <<END
27+
profile = "user"
28+
change-id = 118703
29+
@@ -89,9 +90,8 @@ link-shared = true
2830

2931
[build]
3032
target = [
@@ -36,7 +38,7 @@
3638
"wasm32-unknown-unknown",
3739
"wasm32-wasi",
3840
]
39-
@@ -144,22 +142,18 @@ deny-warnings = false
41+
@@ -144,22 +144,18 @@ deny-warnings = false
4042
[dist]
4143
compression-formats = ["gz"]
4244

@@ -63,7 +65,7 @@
6365

6466
[target.wasm32-unknown-unknown]
6567
sanitizers = false
66-
@@ -198,15 +192,12 @@ build() {
68+
@@ -198,15 +194,12 @@ build() {
6769

6870
# rustbuild always installs copies of the shared libraries to /usr/lib,
6971
# overwrite them with symlinks to the per-architecture versions
@@ -81,7 +83,7 @@
8183
_pick dest-wasm usr/lib/rustlib/wasm32-*
8284
_pick dest-src usr/lib/rustlib/src
8385
}
84-
@@ -235,22 +226,6 @@ package_rust() {
86+
@@ -235,22 +228,6 @@ package_rust() {
8587
cp -a dest-rust/* "$pkgdir"
8688
}
8789

@@ -104,3 +106,10 @@
104106
package_rust-musl() {
105107
pkgdesc="Musl target for Rust"
106108
depends=(rust)
109+
@@ -285,3 +262,6 @@ package_rust-src() {
110+
}
111+
112+
# vim:set ts=2 sw=2 et:
113+
+
114+
+source+=(riscv-target-abi-fix.patch)
115+
+b2sums+=('37bb6aaaba5b25ea2d6eb17c1e0928caa13055dc105c41186ea3556e35df2332b81bd939a16172b6c18b9b0e3f7c16944f8922c2e5307f3dab8e52ec9c310d60')

0 commit comments

Comments
 (0)