Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross compiling for RISCV32 fails as compiler flags are not supplied by board but must be in target.cmake #17743

Closed
egcd32 opened this issue Jul 24, 2019 · 8 comments
Assignees
Labels
area: RISCV RISCV Architecture (32-bit & 64-bit) bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@egcd32
Copy link

egcd32 commented Jul 24, 2019

Describe the bug
When cross-compiling the Zephyr hello world sample on Windows 10 using Freedom Studio 4.7.2 2019-03, which includes riscv64-unknown-elf-gcc-8.2.0-2019.02.0, for a SiFive HiFive1 rev B board, ninja reports compiler warnings on pointer/int conversion and the link step fails due to ABI incompatibility. This is because the compiler flags needed for RISCV32 are not being supplied to gcc. Freedom Studio's gcc can compile for either 64 or 32 but defaults to 64 I had thought that would be set for the board, but the board file was being loaded too late and was not taking effect.

Similarly, changing zephyr/arch/riscv/CMakeLists.txt did not seem to address the issue either. I believe this is because zephyr/cmake/compiler/gcc/target.cmake has
COMMAND ${CMAKE_C_COMPILER} ${TOOLCHAIN_C_FLAGS} --print-libgcc-file-name
to determine the gcc directory for TOOLCHAIN_LIBS, but zephyr/arch/riscv/CMakeLists.txt is not parsed until later.

I was able to work around this issue by changing the zephyr/cmake/compiler/gcc/target.cmake file and adding at about line 72, after the other two architecture specific compiler flag sections, and before the reference to TOOLCHAIN_C_FLAGS:

elseif("${ARCH}" STREQUAL "riscv")

list(APPEND TOOLCHAIN_C_FLAGS
   -march=rv32imac -mabi=ilp32 -mcmodel=medlow
   )

To Reproduce
Steps to reproduce the behavior:

  1. Install a compiler for RISCV32 and RISCV64.
  2. Point to that compiler with set ZEPHYR_TOOLCHAIN_VARIANT=cross-compile
    set CROSS_COMPILE=C:/.../FreedomStudio-4.7.2.2019-03-4/SiFive/toolchain/riscv64-unknown-elf-gcc-8.2.0-2019.02.0/bin/riscv64-unknown-elf-
  3. cd samples cd hello_world mkdir build cd build
  4. cmake -GNinja -DBOARD=hifive1_revb ..
  5. ninja -j 1
  6. See error ABI is incompatible with that of the selected emulation:
    target emulation elf64-littleriscv' does not match elf32-littleriscv'

Expected behavior
Compiles to an ELF file for RISCV32.

Impact
Would prefer if those compiler flags were in target.cmake in Zephyr for RISCV32, similar to ARM architecture.

Environment (please complete the following information):

  • OS: Windows 10
  • Toolchain: GCC riscv64-unknown-elf-gcc-8.2.0 packaged by SiFive in FreedomStudio-4.7.2.2019-03-4 for the HiFive1 rev B board
@egcd32 egcd32 added the bug The issue is a bug, or the PR is fixing a bug label Jul 24, 2019
@ioannisg
Copy link
Member

@SebastianBoe can you help here?

@ioannisg ioannisg added priority: medium Medium impact/importance bug area: RISCV RISCV Architecture (32-bit & 64-bit) labels Jul 30, 2019
@SebastianBoe
Copy link
Collaborator

Hi,

a patch to this effect submitted as a PR would be welcome.

Although perhaps it should be applied in zephyr/arch/riscv32/CMakeLists.txt to be consistent with how the -mabi and -march is set in ARM.

@galak
Copy link
Collaborator

galak commented Aug 10, 2019

Is this still an issue with the recent qemu_riscv64 merge we now set these flags:

https://github.com/zephyrproject-rtos/zephyr/blob/master/cmake/compiler/gcc/target.cmake#L72

@galak galak added priority: low Low impact/importance bug and removed priority: medium Medium impact/importance bug labels Aug 10, 2019
@egcd32
Copy link
Author

egcd32 commented Aug 10, 2019

Thanks. There is still an issue, though it appears to be specific to cross compiling with the GCC 8.2.0 packaged in SiFive FreedomStudio-4.7.2.2019-05.0-win32. The above change to target.cmake uses -mabi=ilp32 -march=rv32ima however SiFive doesn't have a GCC library for rv32ima. (It does have rv32i, rv32im, rv32imac, rv32imafc...)
As a result, in that cross-compiler,
gcc -mabi=ilp32 -march=rv32ima --print-gcc-file-name
returns an elf64-littleriscv libgcc.a that isn't correct, while
gcc -mabi=ilp32 -march=rv32imac --print-gcc-file-name
returns an elf32-littleriscv libgcc.a

@jaronkelleher
Copy link
Contributor

Does Pull Request #23265 solve this?

@github-actions
Copy link

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

@kbrunham-intel
Copy link

I still see this issue when using the SiFive GNU Embedded Toolchain v2020.12.8 and release v2.7.1. Please reopen this issue.

@SebastianBoe SebastianBoe reopened this Jan 31, 2022
@github-actions github-actions bot removed the Stale label Feb 1, 2022
@mbolivar-nordic
Copy link
Contributor

Closing -- please reopen if it reproduces on an officially supported toolchain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: RISCV RISCV Architecture (32-bit & 64-bit) bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

No branches or pull requests

8 participants