首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从c代码和汇编代码RISCV生成十六进制文件

如何从c代码和汇编代码RISCV生成十六进制文件
EN

Stack Overflow用户
提问于 2020-08-05 17:39:19
回答 1查看 602关注 0票数 0

我有一些C代码/汇编代码,我想编译它并为RISCV pulppissimo生成十六进制文件。有没有人能帮我完成这些步骤。当前正在运行hello代码

代码语言:javascript
复制
#include <stdio.h>

int main()
{
  printf("Hello !\n");

  return 0;
}

在使用riscv32进行编译时,使用命令riscv32-unknown-elf-gcc hello.c

遇到这样的错误

代码语言:javascript
复制
RISCV/lib/gcc/riscv32-unknown-elf/7.1.1/../../../../riscv32-unknown-elf/bin/ld: cannot open linker script file riscv.ld: No such file or directory
collect2: error: ld returned 1 exit status
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-05 23:47:24

git 存储库中有一个完美的示例boot code,可用于回答您的问题。由于我没有您正在使用的工具链,因此我使用的是从 bootlin 网站下载的工具链。

代码语言:javascript
复制
wget https://toolchains.bootlin.com/downloads/releases/toolchains/riscv32-ilp32d/tarballs/riscv32-ilp32d--glibc--bleeding-edge-2020.02-2.tar.bz2
mkdir -p /opt/bootlin
tar jxvf riscv32-ilp32d--glibc--bleeding-edge-2020.02-2.tar.bz2 -C /opt/bootlin

git clone git clone https://github.com/pulp-platform/boot-code
cd boot-code

编辑Makefile,并调整以下行,以反映正在使用的工具链的位置:

代码语言:javascript
复制
PULP_CC = riscv32-unknown-elf-gcc
PULP_LD = riscv32-unknown-elf-gcc

当使用bootlin工具链时,这将导致以下两行:

代码语言:javascript
复制
PULP_CC = /opt/bootlin/riscv32-ilp32d--glibc--bleeding-edge-2020.02-2/bin/riscv32-buildroot-linux-gnu-gcc
PULP_LD = /opt/bootlin/riscv32-ilp32d--glibc--bleeding-edge-2020.02-2/bin/riscv32-buildroot-linux-gnu-gcc

同样的Makefile,替换下面这一行--代码不能像我想的那样编译:

代码语言:javascript
复制
CFLAGS += -Os -g -fno-jump-tables -I$(CURDIR)/include 

出自:

代码语言:javascript
复制
CFLAGS += -Os -g -fno-jump-tables -I$(CURDIR)/include -DMCHAN_CMD_ILE_BIT=21 -DMCHAN_CMD_ELE_BIT=20

现在可以构建build/bootcode可执行文件:

代码语言:javascript
复制
make
CC  boot_code.c
In file included from /home/frant/mnt/git/boot-code/include/hal/chips/pulp/pulp.h:20,
                 from /home/frant/mnt/git/boot-code/include/hal/pulp.h:27,
                 from boot_code.c:18:
/home/frant/mnt/git/boot-code/include/hal/riscv/riscv_v5.h: In function ‘hal_cluster_id’:
/home/frant/mnt/git/boot-code/include/hal/riscv/riscv_v5.h:172:10: warning: implicit declaration of function ‘__builtin_pulp_ClusterId’ [-Wimplicit-function-declaration]
  172 |   return __builtin_pulp_ClusterId();
      |          ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/frant/mnt/git/boot-code/include/hal/chips/pulp/pulp.h:21,
                 from /home/frant/mnt/git/boot-code/include/hal/pulp.h:27,
                 from boot_code.c:18:
/home/frant/mnt/git/boot-code/include/hal/eu/eu_v3.h: In function ‘evt_read32’:
/home/frant/mnt/git/boot-code/include/hal/eu/eu_v3.h:43:11: warning: implicit declaration of function ‘__builtin_pulp_event_unit_read’ [-Wimplicit-function-declaration]
   43 |   value = __builtin_pulp_event_unit_read((int *)base, offset);
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CC  crt0.S
LD  /home/frant/mnt/git/boot-code/build/bootcode
./stim_utils.py  \
        --binary=/home/frant/mnt/git/boot-code/build/bootcode \
        --stim-bin=rom.bin \
        --area=0x1a000000:0x01000000
Created stimuli generator
  Added binary: /home/frant/mnt/git/boot-code/build/bootcode
  Added target area: [0x1a000000 -> 0x1b000000]
  Handling section (base: 0x1a000000, size: 0x57c)
  Bypassing section (base:make
CC  boot_code.c
In file included from /home/frant/mnt/git/boot-code/include/hal/chips/pulp/pulp.h:20,
                 from /home/frant/mnt/git/boot-code/include/hal/pulp.h:27,
                 from boot_code.c:18:
/home/frant/mnt/git/boot-code/include/hal/riscv/riscv_v5.h: In function ‘hal_cluster_id’:
/home/frant/mnt/git/boot-code/include/hal/riscv/riscv_v5.h:172:10: warning: implicit declaration of function ‘__builtin_pulp_ClusterId’ [-Wimplicit-function-declaration]
  172 |   return __builtin_pulp_ClusterId();
      |          ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/frant/mnt/git/boot-code/include/hal/chips/pulp/pulp.h:21,
                 from /home/frant/mnt/git/boot-code/include/hal/pulp.h:27,
                 from boot_code.c:18:
/home/frant/mnt/git/boot-code/include/hal/eu/eu_v3.h: In function ‘evt_read32’:
/home/frant/mnt/git/boot-code/include/hal/eu/eu_v3.h:43:11: warning: implicit declaration of function ‘__builtin_pulp_event_unit_read’ [-Wimplicit-function-declaration]
   43 |   value = __builtin_pulp_event_unit_read((int *)base, offset);
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CC  crt0.S
LD  /home/frant/mnt/git/boot-code/build/bootcode
./stim_utils.py  \
        --binary=/home/frant/mnt/git/boot-code/build/bootcode \
        --stim-bin=rom.bin \
        --area=0x1a000000:0x01000000
Created stimuli generator
  Added binary: /home/frant/mnt/git/boot-code/build/bootcode
  Added target area: [0x1a000000 -> 0x1b000000]
  Handling section (base: 0x1a000000, size: 0x57c)
  Bypassing section (base: 0x1c000000, size: 0x1928)
objcopy --srec-len 1 --output-target=srec /home/frant/mnt/git/boot-code/build/bootcode /home/frant/mnt/git/boot-code/build/bootcode.s19
./s19toboot.py /home/frant/mnt/git/boot-code/build/bootcode.s19 boot_code.cde pulp 0x1c000000, size: 0x1928)
objcopy --srec-len 1 --output-target=srec /home/frant/mnt/git/boot-code/build/bootcode /home/frant/mnt/git/boot-code/build/bootcode.s19
./s19toboot.py /home/frant/mnt/git/boot-code/build/bootcode.s19 boot_code.cde pulp

并将其转换为Intel十六进制格式:

代码语言:javascript
复制
/opt/bootlin/riscv32-ilp32d--glibc--bleeding-edge-2020.02-2/bin/riscv32-buildroot-linux-gnu-objcopy  --output-target=ihex build/bootcode bootcode.ihex

cat bootcode.ihex
:020000041A00E0
:100000006F0080086F0040086F0000086F00C00795
:100010006F0080076F0040076F0000076F00C00689
../...
:081920000000000000000000BF
:040000051A000000DD
:00000001FF

现在你已经有了一个我认为是你的目标的纸浆平台的工作示例,你应该能够根据你的需求来调整它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63262419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档