|
| 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