Skip to content

Commit 0964586

Browse files
authored
Unrolled build for rust-lang#117601
Rollup merge of rust-lang#117601 - androm3da:hexagon_unknown_none_elf, r=petrochenkov Add support for hexagon-unknown-none-elf as target Still TODO: document usage details for new target
2 parents 208dd20 + cc34942 commit 0964586

File tree

6 files changed

+298
-1
lines changed

6 files changed

+298
-1
lines changed

compiler/rustc_target/src/spec/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,7 @@ supported_targets! {
14931493
("mips64-unknown-linux-muslabi64", mips64_unknown_linux_muslabi64),
14941494
("mips64el-unknown-linux-muslabi64", mips64el_unknown_linux_muslabi64),
14951495
("hexagon-unknown-linux-musl", hexagon_unknown_linux_musl),
1496+
("hexagon-unknown-none-elf", hexagon_unknown_none_elf),
14961497

14971498
("mips-unknown-linux-uclibc", mips_unknown_linux_uclibc),
14981499
("mipsel-unknown-linux-uclibc", mipsel_unknown_linux_uclibc),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::spec::{PanicStrategy, Target, TargetOptions};
2+
3+
pub fn target() -> Target {
4+
Target {
5+
llvm_target: "hexagon-unknown-none-elf".into(),
6+
pointer_width: 32,
7+
data_layout: concat!(
8+
"e-m:e-p:32:32:32-a:0-n16:32-i64:64:64-i32:32",
9+
":32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-v32",
10+
":32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048",
11+
":2048:2048"
12+
)
13+
.into(),
14+
arch: "hexagon".into(),
15+
16+
options: TargetOptions {
17+
cpu: "hexagonv60".into(),
18+
panic_strategy: PanicStrategy::Abort,
19+
dynamic_linking: true,
20+
features: "-small-data,+hvx-length128b".into(),
21+
max_atomic_width: Some(32),
22+
emit_debug_gdb_scripts: false,
23+
c_enum_min_bits: Some(8),
24+
..Default::default()
25+
},
26+
}
27+
}

compiler/rustc_target/src/spec/tests/tests_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ impl Target {
116116

117117
// Check dynamic linking stuff
118118
// BPF: when targeting user space vms (like rbpf), those can load dynamic libraries.
119-
if self.os == "none" && self.arch != "bpf" {
119+
// hexagon: when targeting QuRT, that OS can load dynamic libraries.
120+
if self.os == "none" && (self.arch != "bpf" && self.arch != "hexagon") {
120121
assert!(!self.dynamic_linking);
121122
}
122123
if self.only_cdylib

src/doc/rustc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [\*-unknown-fuchsia](platform-support/fuchsia.md)
3939
- [\*-kmc-solid_\*](platform-support/kmc-solid.md)
4040
- [csky-unknown-linux-gnuabiv2\*](platform-support/csky-unknown-linux-gnuabiv2.md)
41+
- [hexagon-unknown-none-elf](platform-support/hexagon-unknown-none-elf.md)
4142
- [loongarch\*-unknown-linux-\*](platform-support/loongarch-linux.md)
4243
- [loongarch\*-unknown-none\*](platform-support/loongarch-none.md)
4344
- [m68k-unknown-linux-gnu](platform-support/m68k-unknown-linux-gnu.md)

src/doc/rustc/src/platform-support.md

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ target | std | host | notes
265265
`bpfel-unknown-none` | * | | BPF (little endian)
266266
`csky-unknown-linux-gnuabiv2` | ✓ | | C-SKY abiv2 Linux (little endian)
267267
`csky-unknown-linux-gnuabiv2hf` | ✓ | | C-SKY abiv2 Linux, hardfloat (little endian)
268+
[`hexagon-unknown-none-elf`](platform-support/hexagon-unknown-none-elf.md)| * | Bare Hexagon (v60+, HVX)
268269
`hexagon-unknown-linux-musl` | ? | |
269270
`i386-apple-ios` | ✓ | | 32-bit x86 iOS [^x86_32-floats-return-ABI]
270271
[`i586-pc-nto-qnx700`](platform-support/nto-qnx.md) | * | | 32-bit x86 QNX Neutrino 7.0 RTOS [^x86_32-floats-return-ABI]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# `hexagon-unknown-none-elf`
2+
3+
**Tier: 3**
4+
5+
Rust for baremetal Hexagon DSPs.
6+
7+
| Target | Descriptions |
8+
| ------------------------ | ----------------------------------------- |
9+
| hexagon-unknown-none-elf | Hexagon 32-bit (freestanding, hardfloat) |
10+
11+
## Target maintainers
12+
13+
- [Brian Cain](https://github.com/androm3da), `[email protected]`
14+
15+
## Requirements
16+
17+
This target is cross-compiled. There is no support for `std`. There is no
18+
default allocator, but it's possible to use `alloc` by supplying an allocator.
19+
20+
By default, code generated with this target should run on Hexagon DSP hardware.
21+
22+
- `-Ctarget-cpu=hexagonv73` adds support for instructions defined up to Hexagon V73.
23+
24+
Functions marked `extern "C"` use the [Hexagon architecture calling convention](https://lists.llvm.org/pipermail/llvm-dev/attachments/20190916/21516a52/attachment-0001.pdf).
25+
26+
This target generates PIC ELF binaries.
27+
28+
## Building the target
29+
30+
You can build Rust with support for the target by adding it to the `target`
31+
list in `config.toml`:
32+
33+
```toml
34+
[build]
35+
build-stage = 1
36+
host = ["<target for your host>"]
37+
target = ["<target for your host>", "hexagon-unknown-none-elf"]
38+
39+
[target.hexagon-unknown-none-elf]
40+
41+
cc = "hexagon-unknown-none-elf-clang"
42+
cxx = "hexagon-unknown-none-elf-clang++"
43+
linker = "hexagon-unknown-none-elf-clang"
44+
llvm-libunwind = 'in-tree'
45+
```
46+
47+
Replace `<target for your host>` with `x86_64-unknown-linux-gnu` or whatever
48+
else is appropriate for your host machine.
49+
50+
## Building Rust programs
51+
52+
Rust does not yet ship pre-compiled artifacts for this target. To compile for
53+
this target, you will either need to build Rust with the target enabled (see
54+
"Building the target" above), or build your own copy of `core` by using
55+
`build-std` or similar.
56+
57+
## Testing
58+
59+
Since `hexagon-unknown-none-elf` supports a variety of different environments and
60+
does not support `std`, this target does not support running the Rust test suite.
61+
62+
## Cross-compilation toolchains and C code
63+
64+
This target has been tested using `qemu-system-hexagon`.
65+
66+
A common use case for `hexagon-unknown-none-elf` is building libraries that
67+
link against C code and can be used in emulation or on a device with a
68+
Hexagon DSP.
69+
70+
The Hexagon SDK has libraries which are useful to link against when running
71+
on a device.
72+
73+
74+
# Standalone OS
75+
76+
The script below will build an executable against "hexagon standalone OS"
77+
which is suitable for emulation or bare-metal on-device testing.
78+
79+
First, run `cargo new --bin demo1_hexagon` then add the source below as
80+
`src/main.rs`. This program demonstrates the console output via semihosting.
81+
82+
```rust,ignore (platform-specific,eh-personality-is-unstable)
83+
#![no_std]
84+
#![no_main]
85+
86+
extern "C" {
87+
fn putchar(ch: i32);
88+
fn _exit(code: i32) -> !;
89+
}
90+
91+
#[no_mangle]
92+
extern "C" fn main() -> i32 {
93+
let message = "Hello, this is Rust!";
94+
for b in message.bytes() {
95+
unsafe {
96+
putchar(b as i32);
97+
}
98+
}
99+
0
100+
}
101+
102+
#[panic_handler]
103+
fn panic(_panic: &core::panic::PanicInfo) -> ! {
104+
unsafe {
105+
_exit(1);
106+
}
107+
}
108+
109+
```
110+
111+
Next, save the script below as `build.sh` and edit it to suit your
112+
environment.
113+
114+
* `hex_toolchain` below refers to the [hexagon toolchain using exclusively
115+
public open source repos](https://github.com/quic/toolchain_for_hexagon/releases).
116+
* `cc` below refers to clang. You can use `clang` from your distribution, as
117+
long as it's at least `clang-17`. Or you can use
118+
`hexagon-unknown-none-elf-clang` from one of the [hexagon open source toolchain
119+
releases](https://github.com/quic/toolchain_for_hexagon/releases).
120+
121+
```sh
122+
# Hexagon SDK, required for target libraries:
123+
hex_sdk_root=/local/mnt/workspace/Qualcomm/Hexagon_SDK/5.3.0.0
124+
hex_sdk_toolchain=${hex_sdk_root}/tools/HEXAGON_Tools/8.6.06
125+
126+
sdk_libs=${hex_sdk_toolchain}/Tools/target/hexagon/lib
127+
q6_arch=v65
128+
g0_lib_path=${sdk_libs}/${q6_arch}/G0
129+
pic_lib_path=${sdk_libs}/${q6_arch}/G0/pic
130+
131+
cargo build --target=hexagon-unknown-none-elf -Zbuild-std
132+
133+
# Builds an executable against "hexagon standalone OS" suitable for emulation:
134+
${cc} --target=hexagon-unknown-none-elf -o testit \
135+
-fuse-ld=lld \
136+
-m${q6_arch} \
137+
-nodefaultlibs \
138+
-nostartfiles \
139+
${g0_lib_path}/crt0_standalone.o \
140+
${g0_lib_path}/crt0.o \
141+
${g0_lib_path}/init.o \
142+
-L${sdk_libs}/${q6_arch}/ \
143+
-L${sdk_libs}/ \
144+
testit.c \
145+
target/hexagon-unknown-none-elf/debug/libmin_ex_lib_lin.rlib \
146+
target/hexagon-unknown-none-elf/debug/deps/libcore-*.rlib \
147+
target/hexagon-unknown-none-elf/debug/deps/libcompiler_builtins-*.rlib \
148+
-Wl,--start-group \
149+
-Wl,--defsym,_SDA_BASE_=0,--defsym,__sbss_start=0,--defsym,__sbss_end=0 \
150+
-lstandalone \
151+
${g0_lib_path}/libc.a \
152+
-lgcc \
153+
-lc_eh \
154+
-Wl,--end-group \
155+
${g0_lib_path}/fini.o \
156+
157+
${hex_toolchain}/x86_64-linux-gnu/bin/qemu-system-hexagon -monitor none -display none -kernel ./testit
158+
```
159+
160+
# QuRT OS
161+
162+
First, run `cargo new --lib demo2_hexagon` then add the source below as
163+
`src/lib.rs`. This program demonstrates inline assembly and console output
164+
via semihosting.
165+
166+
```rust,ignore (platform-specific,eh-personality-is-unstable)
167+
#![no_std]
168+
#![no_main]
169+
#![feature(lang_items)]
170+
#![feature(asm_experimental_arch)]
171+
172+
use core::arch::asm;
173+
174+
extern "C" {
175+
fn putchar(ch: i32);
176+
fn _exit(code: i32) -> !;
177+
}
178+
179+
fn hexagon_specific() {
180+
let mut buffer = [0_u8; 128];
181+
182+
unsafe {
183+
let mut x = &buffer;
184+
asm!(
185+
"{{\n\t",
186+
" v0=vmem({addr}+#0)\n\t",
187+
" {tmp} = and({tmp}, #1)\n\t",
188+
"}}\n\t",
189+
addr = in(reg) x,
190+
tmp = out(reg) _,
191+
);
192+
}
193+
}
194+
195+
#[no_mangle]
196+
extern "C" fn hello() -> i32 {
197+
let message = "Hello, this is Rust!\n";
198+
for b in message.bytes() {
199+
unsafe {
200+
putchar(b as i32);
201+
}
202+
}
203+
hexagon_specific();
204+
0
205+
}
206+
207+
#[panic_handler]
208+
fn panic(_panic: &core::panic::PanicInfo) -> ! {
209+
unsafe {
210+
_exit(1);
211+
}
212+
}
213+
214+
#[lang = "eh_personality"]
215+
fn rust_eh_personality() {}
216+
217+
```
218+
219+
Next, save the script below as `build.sh` and edit it to suit your
220+
environment. The script below will build a shared object against the QuRT
221+
RTOS which is suitable for emulation or on-device testing when loaded via
222+
the fastrpc-shell.
223+
224+
225+
```sh
226+
# Hexagon SDK, required for target libraries:
227+
hex_sdk_root=/local/mnt/workspace/Qualcomm/Hexagon_SDK/5.3.0.0
228+
hex_sdk_toolchain=${hex_sdk_root}/tools/HEXAGON_Tools/8.6.06
229+
230+
sdk_libs=${hex_sdk_toolchain}/Tools/target/hexagon/lib
231+
q6_arch=v65
232+
g0_lib_path=${sdk_libs}/${q6_arch}/G0
233+
pic_lib_path=${sdk_libs}/${q6_arch}/G0/pic
234+
runelf=${hex_sdk_root}/rtos/qurt/computev65/sdksim_bin/runelf.pbn
235+
rmohs=${hex_sdk_root}/libs/run_main_on_hexagon/ship/hexagon_toolv86_${q6_arch}/run_main_on_hexagon_sim
236+
237+
# Builds a library suitable for loading into "run_main_on_hexagon_sim" for
238+
# emulation or frpc shell on real target:
239+
${cc} --target=hexagon-unknown-none-elf -o testit.so \
240+
-fuse-ld=lld \
241+
-fPIC -shared \
242+
-nostdlib \
243+
-Wl,-Bsymbolic \
244+
-Wl,--wrap=malloc \
245+
-Wl,--wrap=calloc \
246+
-Wl,--wrap=free \
247+
-Wl,--wrap=realloc \
248+
-Wl,--wrap=memalign \
249+
-m${q6_arch} \
250+
testit.c \
251+
target/hexagon-unknown-none-elf/debug/libmin_ex_lib_lin.rlib \
252+
target/hexagon-unknown-none-elf/debug/deps/libcore-*.rlib \
253+
target/hexagon-unknown-none-elf/debug/deps/libcompiler_builtins-*.rlib \
254+
-Wl,-soname=testit \
255+
${pic_lib_path}/libc.so
256+
257+
# -Bsymbolic above for memory alloc funcs is necessary to access the heap on
258+
# target, but otherwise not required.
259+
260+
# multi-stage loader: runelf => run_main_on_hexagon_sim => testit.so{`main`}
261+
${hex_toolchain}/x86_64-linux-gnu/bin/qemu-system-hexagon \
262+
-monitor none \
263+
-display none \
264+
-kernel ${runelf} \
265+
-append "${rmohs} -- ./testit.so"
266+
```

0 commit comments

Comments
 (0)