Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f346fb0

Browse files
committed
Auto merge of rust-lang#108792 - Amanieu:ohos, r=petrochenkov
Add OpenHarmony targets - `aarch64-unknown-linux-ohos` - `armv7-unknown-linux-ohos` Compiler team MCP: rust-lang/compiler-team#568
2 parents 40cd031 + e3968be commit f346fb0

File tree

15 files changed

+238
-7
lines changed

15 files changed

+238
-7
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,9 @@ dependencies = [
868868

869869
[[package]]
870870
name = "compiler_builtins"
871-
version = "0.1.87"
871+
version = "0.1.89"
872872
source = "registry+https://github.com/rust-lang/crates.io-index"
873-
checksum = "f867ce54c09855ccd135ad4a50c777182a0c7af5ff20a8f537617bd648b10d50"
873+
checksum = "9fc9c2080d347a2c316518840ac9194644a9993dfa1e9778ef38979a339f5d8b"
874874
dependencies = [
875875
"cc",
876876
"rustc-std-workspace-core",
@@ -2879,9 +2879,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
28792879

28802880
[[package]]
28812881
name = "libc"
2882-
version = "0.2.139"
2882+
version = "0.2.140"
28832883
source = "registry+https://github.com/rust-lang/crates.io-index"
2884-
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
2884+
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
28852885
dependencies = [
28862886
"rustc-std-workspace-core",
28872887
]

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ pub fn target_machine_factory(
214214

215215
let path_mapping = sess.source_map().path_mapping().clone();
216216

217+
let force_emulated_tls = sess.target.force_emulated_tls;
218+
217219
Arc::new(move |config: TargetMachineFactoryConfig| {
218220
let split_dwarf_file =
219221
path_mapping.map_prefix(config.split_dwarf_file.unwrap_or_default()).0;
@@ -239,6 +241,7 @@ pub fn target_machine_factory(
239241
relax_elf_relocations,
240242
use_init_array,
241243
split_dwarf_file.as_ptr(),
244+
force_emulated_tls,
242245
)
243246
};
244247

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,7 @@ extern "C" {
22572257
RelaxELFRelocations: bool,
22582258
UseInitArray: bool,
22592259
SplitDwarfFile: *const c_char,
2260+
ForceEmulatedTls: bool,
22602261
) -> Option<&'static mut TargetMachine>;
22612262
pub fn LLVMRustDisposeTargetMachine(T: &'static mut TargetMachine);
22622263
pub fn LLVMRustAddLibraryInfo<'a>(

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
368368
bool EmitStackSizeSection,
369369
bool RelaxELFRelocations,
370370
bool UseInitArray,
371-
const char *SplitDwarfFile) {
371+
const char *SplitDwarfFile,
372+
bool ForceEmulatedTls) {
372373

373374
auto OptLevel = fromRust(RustOptLevel);
374375
auto RM = fromRust(RustReloc);
@@ -400,6 +401,10 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
400401
}
401402
Options.RelaxELFRelocations = RelaxELFRelocations;
402403
Options.UseInitArray = UseInitArray;
404+
if (ForceEmulatedTls) {
405+
Options.ExplicitEmulatedTLS = true;
406+
Options.EmulatedTLS = true;
407+
}
403408

404409
if (TrapUnreachable) {
405410
// Tell LLVM to codegen `unreachable` into an explicit trap instruction.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::spec::{Target, TargetOptions};
2+
3+
use super::SanitizerSet;
4+
5+
pub fn target() -> Target {
6+
let mut base = super::linux_musl_base::opts();
7+
base.env = "ohos".into();
8+
base.crt_static_default = false;
9+
base.max_atomic_width = Some(128);
10+
11+
Target {
12+
// LLVM 15 doesn't support OpenHarmony yet, use a linux target instead.
13+
llvm_target: "aarch64-unknown-linux-musl".into(),
14+
pointer_width: 64,
15+
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
16+
arch: "aarch64".into(),
17+
options: TargetOptions {
18+
features: "+reserve-x18".into(),
19+
mcount: "\u{1}_mcount".into(),
20+
force_emulated_tls: true,
21+
supported_sanitizers: SanitizerSet::ADDRESS
22+
| SanitizerSet::CFI
23+
| SanitizerSet::LEAK
24+
| SanitizerSet::MEMORY
25+
| SanitizerSet::MEMTAG
26+
| SanitizerSet::THREAD
27+
| SanitizerSet::HWADDRESS,
28+
..base
29+
},
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::spec::{Target, TargetOptions};
2+
3+
// This target is for OpenHarmony on ARMv7 Linux with thumb-mode, but no NEON or
4+
// hardfloat.
5+
6+
pub fn target() -> Target {
7+
// Most of these settings are copied from the armv7_unknown_linux_musleabi
8+
// target.
9+
Target {
10+
// LLVM 15 doesn't support OpenHarmony yet, use a linux target instead.
11+
llvm_target: "armv7-unknown-linux-gnueabi".into(),
12+
pointer_width: 32,
13+
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".into(),
14+
arch: "arm".into(),
15+
16+
options: TargetOptions {
17+
abi: "eabi".into(),
18+
features: "+v7,+thumb2,+soft-float,-neon".into(),
19+
max_atomic_width: Some(64),
20+
env: "ohos".into(),
21+
crt_static_default: false,
22+
mcount: "\u{1}mcount".into(),
23+
force_emulated_tls: true,
24+
..super::linux_musl_base::opts()
25+
},
26+
}
27+
}

compiler/rustc_target/src/spec/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,9 @@ supported_targets! {
12611261

12621262
("aarch64-unknown-nto-qnx710", aarch64_unknown_nto_qnx_710),
12631263
("x86_64-pc-nto-qnx710", x86_64_pc_nto_qnx710),
1264+
1265+
("aarch64-unknown-linux-ohos", aarch64_unknown_linux_ohos),
1266+
("armv7-unknown-linux-ohos", armv7_unknown_linux_ohos),
12641267
}
12651268

12661269
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
@@ -1734,6 +1737,9 @@ pub struct TargetOptions {
17341737

17351738
/// Whether the target supports XRay instrumentation.
17361739
pub supports_xray: bool,
1740+
1741+
/// Forces the use of emulated TLS (__emutls_get_address)
1742+
pub force_emulated_tls: bool,
17371743
}
17381744

17391745
/// Add arguments for the given flavor and also for its "twin" flavors
@@ -1954,6 +1960,7 @@ impl Default for TargetOptions {
19541960
entry_name: "main".into(),
19551961
entry_abi: Conv::C,
19561962
supports_xray: false,
1963+
force_emulated_tls: false,
19571964
}
19581965
}
19591966
}
@@ -2605,6 +2612,7 @@ impl Target {
26052612
key!(entry_name);
26062613
key!(entry_abi, Conv)?;
26072614
key!(supports_xray, bool);
2615+
key!(force_emulated_tls, bool);
26082616

26092617
if base.is_builtin {
26102618
// This can cause unfortunate ICEs later down the line.
@@ -2859,6 +2867,7 @@ impl ToJson for Target {
28592867
target_option_val!(entry_name);
28602868
target_option_val!(entry_abi);
28612869
target_option_val!(supports_xray);
2870+
target_option_val!(force_emulated_tls);
28622871

28632872
if let Some(abi) = self.default_adjusted_cabi {
28642873
d.insert("default-adjusted-cabi".into(), Abi::name(abi).to_json());

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
1515
panic_unwind = { path = "../panic_unwind", optional = true }
1616
panic_abort = { path = "../panic_abort" }
1717
core = { path = "../core" }
18-
libc = { version = "0.2.139", default-features = false, features = ['rustc-dep-of-std'] }
18+
libc = { version = "0.2.140", default-features = false, features = ['rustc-dep-of-std'] }
1919
compiler_builtins = { version = "0.1.87" }
2020
profiler_builtins = { path = "../profiler_builtins", optional = true }
2121
unwind = { path = "../unwind" }

library/std/src/sys/unix/os.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ pub fn set_errno(e: i32) {
115115
/// Gets a detailed string description for the given error number.
116116
pub fn error_string(errno: i32) -> String {
117117
extern "C" {
118-
#[cfg_attr(any(target_os = "linux", target_env = "newlib"), link_name = "__xpg_strerror_r")]
118+
#[cfg_attr(
119+
all(any(target_os = "linux", target_env = "newlib"), not(target_env = "ohos")),
120+
link_name = "__xpg_strerror_r"
121+
)]
119122
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t) -> c_int;
120123
}
121124

library/unwind/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ cfg_if::cfg_if! {
5454
}
5555
}
5656

57+
// This is the same as musl except that we default to using the system libunwind
58+
// instead of libgcc.
59+
#[cfg(target_env = "ohos")]
60+
cfg_if::cfg_if! {
61+
if #[cfg(all(feature = "llvm-libunwind", feature = "system-llvm-libunwind"))] {
62+
compile_error!("`llvm-libunwind` and `system-llvm-libunwind` cannot be enabled at the same time");
63+
} else if #[cfg(feature = "llvm-libunwind")] {
64+
#[link(name = "unwind", kind = "static", modifiers = "-bundle")]
65+
extern "C" {}
66+
} else {
67+
#[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
68+
#[link(name = "unwind", cfg(not(target_feature = "crt-static")))]
69+
extern "C" {}
70+
}
71+
}
72+
5773
#[cfg(target_os = "android")]
5874
cfg_if::cfg_if! {
5975
if #[cfg(feature = "llvm-libunwind")] {

0 commit comments

Comments
 (0)