How to build toolchain for riscv64gc-unknown-linux-musl on x86_64-unknown-linux-gnu?

I have built musl, gcc, binutils on target riscv64-linux-gnu. I want to use std.

you want to cross build the riscv64gc toolchain or cross compile your code using riscv64gc toolchain ? (which you seem to have already according to your message).

I want to compile my rust code to riscv64gc-unknown-linux-musl on x86_64-unknown-linux-gnu.

Something like this may work:

$ rustup target add riscv64gc-unknown-linux-musl
$ cargo build --target riscv64gc-unknown-linux-musl

You may need to switch the linker to lld.

According to this, it doesn't have support for std.

Right, it is a tier 3 target so rustup target add won't work. I would have expected it to support std though. If it really doesn't you will have to implement support for it in libc and libstd.