【发布时间】:2018-03-25 18:08:46
【问题描述】:
我在 Windows 7-64 位平台上的最新 GCC for RISCV 上为 RISCV 进行基本编译和链接时遇到了困难。
安装的工具:7.1.1-2-20170912-2255 来自https://github.com/gnu-mcu-eclipse/riscv-none-gcc/releases/
平台:Windows 7,64 位,无 cygwin
程序:
#include <stdint.h>
int32_t iBlahblah;
int main (void)
{
while(1)
iBlahblah++;
return 0;
}
命令行:
"c:\Program Files\GNU MCU Eclipse\RISC-V Embedded GCC\7.1.1-2-20170912-2255\bin\riscv64-unknown-elf-gcc.exe" -c hello.c -o hello -march=rv32imac -mabi=ilp32 -Os
"c:\Program Files\GNU MCU Eclipse\RISC-V Embedded GCC\7.1.1-2-20170912-2255\bin\riscv64-unknown-elf-gcc.exe" -o hello.elf -march=rv32imac -mabi=ilp32 -Os -Wl,-Map=hello.lst hello.o
输出:
c:/program files/gnu mcu eclipse/risc-v embedded gcc/7.1.1-2-20170912-2255/bin/../lib/gcc/riscv64-unknown-elf/7.1.1/../../../../riscv64-unknown-elf/bin/ld.exe: hello.o: ABI is incompatible with that of the selected emulation: target emulation `elf64-littleriscv' does not match `elf32-littleriscv'
c:/program files/gnu mcu eclipse/risc-v embedded gcc/7.1.1-2-20170912-2255/bin/../lib/gcc/riscv64-unknown-elf/7.1.1/../../../../riscv64-unknown-elf/bin/ld.exe: failed to merge target specific data of file hello.o
c:/program files/gnu mcu eclipse/risc-v embedded gcc/7.1.1-2-20170912-2255/bin/../lib/gcc/riscv64-unknown-elf/7.1.1/../../../../riscv64-unknown-elf/lib/rv32imac/ilp32\libg.a(lib_a-exit.o): In function `.L0 ': exit.c:(.text.exit+0x1e): undefined reference to `_exit'
c:/program files/gnu mcu eclipse/risc-v embedded gcc/7.1.1-2-20170912-2255/bin/../lib/gcc/riscv64-unknown-elf/7.1.1/../../../../riscv64-unknown-elf/bin/ld.exe: hello.elf(.text): relocation "iBlahblah+0x0 (type R_RISCV_HI20)" goes out of range
c:/program files/gnu mcu eclipse/risc-v embedded gcc/7.1.1-2-20170912-2255/bin/../lib/gcc/riscv64-unknown-elf/7.1.1/../../../../riscv64-unknown-elf/bin/ld.exe: hello.o: file class ELFCLASS64 incompatible with ELFCLASS32
c:/program files/gnu mcu eclipse/risc-v embedded gcc/7.1.1-2-20170912-2255/bin/../lib/gcc/riscv64-unknown-elf/7.1.1/../../../../riscv64-unknown-elf/bin/ld.exe: final link failed: File in wrong format
collect2.exe: error: ld returned 1 exit status
最大的问题是如何解决“ABI 与所选仿真的不兼容”?我们可以忽略有关重定位、退出等的其他问题,因为我更大的构建环境会处理这些问题(它可以为许多平台构建,但目前还不是 RISCV)。
【问题讨论】:
-
您将 gcc 用于 riscv64 并希望它以 32 位模式编译。但是这个 gcc 会将 64 位模式的 c 运行时库添加到链接阶段(将 -v 选项添加到 gcc 以查看 gcc 添加的其他 crt 文件)这是错误的(32 位 elf 和 64 位 elf 对象无法链接一起)。您应该将 gcc 用于具有 64 位模式的 64 位目标;和 gcc 用于具有 32 位模式的 32 位目标(实际上您需要 32 位 crt;它可能包含在您的 gcc 中,但它使用了错误的版本)
-
@osgx gnu-mcu-eclipse.github.io/toolchain/riscv/install 的文档指出 riscv64-unknown-elf-gcc.exe 可以针对 32 位和 64 位。如何提示它执行 32 位库?
-
使用 riscv32-unknown-elf-gcc.exe 将使用正确的 32 位 CRT 路径。比较
riscv64-unknown-elf-gcc.exe -v ...与riscv32-unknown-elf-gcc.exe -v ...的输出以使用 CRT 找到确切的目录。 -
您的 gcc 声明支持 multilib 以便能够从 riscv64-gcc:gnu-mcu-eclipse.github.io/toolchain/riscv/#multiple-libraries 搜索 32 位 crt 库,但出现问题,它没有选择正确的 multilib 目录。检查实际的
gcc/config/riscv/t-elf-multilibgithub.com/riscv/riscv-gcc/blob/… 会很有用。 gnu-mcu-eclipse.github.io/blog/2017/09/13/… 说“支持 March/mabi 的组合......并非所有组合都有库。” -
@osgx 我认为该工具有轻微损坏。如果我通过 riscv-unknown-elf-ld.exe 使用适当的
-melf32lriscv调用它就没有问题。我认为 gcc.exe 没有将正确的东西传递给 ld.exe 来告诉它“这是带有 rv32imac 指令集的 ilp32 abi”
标签: gcc cross-compiling toolchain riscv