llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.53k stars 11.79k forks source link

[RISCV] Cannot generate RISCV executable binary from RISCV assembly code with Clang/LLVM #74081

Open Alexix24 opened 10 months ago

Alexix24 commented 10 months ago

Hi, I'm trying to generate an executable riscv binary (kernel.bin) from a riscv assembly code (kernel.s).

riscv-none-embed-as kernel.s -o kernel.o riscv-none-embed-ld kernel.o -o kernel.elf riscv-none-embed-objcopy -O binary kernel.elf kernel.bin


- But when I try to do the same process with Clang/LLVM (v17.0.5), the binary is not executed on my device:
```sh

clang -c -target riscv32 -march=rv32ia kernel.s -o kernel.elf
llvm-objcopy kernel.elf -O binary kernel.bin

Maybe I'm using the wrong Clang/LLVM commands, or with the wrong arguments? I see that the two .elf files are different.

Thanks !

llvmbot commented 10 months ago

@llvm/issue-subscribers-backend-risc-v

Author: None (Alexix24)

Hi, I'm trying to generate an executable riscv binary (kernel.bin) from a riscv assembly code (kernel.s). - Using the riscv-none-embed-gcc toolchain, the binary is succesfully generated and I can run it on my device: ```sh riscv-none-embed-as kernel.s -o kernel.o riscv-none-embed-ld kernel.o -o kernel.elf riscv-none-embed-objcopy -O binary kernel.elf kernel.bin ``` - But when I try to do the same process with Clang/LLVM (v17.0.5), the binary is not executed on my device: ```sh clang -c -target riscv32 -march=rv32ia kernel.s -o kernel.elf llvm-objcopy kernel.elf -O binary kernel.bin ``` Maybe I'm using the wrong Clang/LLVM commands, or with the wrong arguments? I see that the two .elf files are different. Thanks !
dtcxzyw commented 10 months ago

cc @MaskRay

topperc commented 10 months ago

I think your clang command isn't running the linker. Probably need to run ld separately or remove the -c?

Alexix24 commented 10 months ago

I think your clang command isn't running the linker. Probably need to run ld separately or remove the -c?

Hi, I removed the -c argument to get the linker running : clang -v -target riscv32 -march=rv32ia --gcc-toolchain=/usr/local/bin/gcc/7.5.0/ kernel.s -o kernel.bin

I now receive an error at link time:

clang version 17.0.5
Target: riscv32--
Thread model: posix
InstalledDir: /usr/local/bin/llvm/bin
"/usr/local/bin/llvm/bin/clang-17" -cc1as -triple riscv32-- -filetype obj -main-file-name kernelFunction.s -target-cpu generic-rv32 -target-feature +a ......."
/usr/bin/ld: unrecognised emulation mode: elf32lriscv
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu i386linux elf_l1om elf_k1om
clang: error: ld command failed with exit code 1 (use -v to see invocation)

Thank you for your help !

wangpc-pp commented 10 months ago

I think your clang command isn't running the linker. Probably need to run ld separately or remove the -c?

Hi, I removed the -c argument to get the linker running : clang -v -target riscv32 -march=rv32ia --gcc-toolchain=/usr/local/bin/gcc/7.5.0/ kernel.s -o kernel.bin

You are using system linker, not the one for riscv. The path to riscv gnu toolchain is incorrect. --gcc-toolchain should be a path to riscv gnu toolchain installation.

I now receive an error at link time:

clang version 17.0.5
Target: riscv32--
Thread model: posix
InstalledDir: /usr/local/bin/llvm/bin
"/usr/local/bin/llvm/bin/clang-17" -cc1as -triple riscv32-- -filetype obj -main-file-name kernelFunction.s -target-cpu generic-rv32 -target-feature +a ......."
/usr/bin/ld: unrecognised emulation mode: elf32lriscv
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu i386linux elf_l1om elf_k1om
clang: error: ld command failed with exit code 1 (use -v to see invocation)

Thank you for your help !