riscv64-linux-gnu-gcc编译生成32位cpu二进制
2023-12-13 16:51:41
- x86平台下64位设备,默认情况下编译的是elf64,比如简单的helloworld代码如下,使用readelf查看elf头部信息,格式为ELF64。
? cat helloworld.c
#include <stdio.h>
int main()
{
printf("hello!!!\n");
return 0;
}
? gcc -Wall helloworld.c -g -o helloworld
? readelf -h helloworld
ELF 头:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
类别: ELF64
数据: 2 补码,小端序 (little endian)
Version: 1 (current)
OS/ABI: UNIX - System V
ABI 版本: 0
类型: DYN (Position-Independent Executable file)
系统架构: Advanced Micro Devices X86-64
版本: 0x1
入口点地址: 0x1040
程序头起点: 64 (bytes into file)
Start of section headers: 14080 (bytes into file)
标志: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 13
Size of section headers: 64 (bytes)
Number of section headers: 36
Section header string table index: 35
- 要生成32位代码时只需要指定 -m 32即可。
? gcc -Wall -m32 helloworld.c -g -o helloworld
? readelf -h helloworld
ELF 头:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
类别: ELF32
数据: 2 补码,小端序 (little endian)
Version: 1 (current)
OS/ABI: UNIX - System V
ABI 版本: 0
类型: DYN (Position-Independent Executable file)
系统架构: Intel 80386
版本: 0x1
入口点地址: 0x1060
程序头起点: 52 (bytes into file)
Start of section headers: 13996 (bytes into file)
标志: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 12
Size of section headers: 40 (bytes)
Number of section headers: 36
Section header string table index: 35
- 我的设备是MANJARO发行版本,通过包管理器安装了risc-v编译工具链riscv64-linux-gnu-gcc
? uname -a
Linux mt-pc 5.10.202-1-MANJARO #1 SMP PREEMPT Tue Nov 28 19:17:16 UTC 2023 x86_64 GNU/Linux
? pacman -Q riscv64-linux-gnu-gcc
riscv64-linux-gnu-gcc 12.2.0-1
- 对于riscv汇编程序,默认生成elf64二进制。
? cat test.s
# Add
# Format:
# ADD RD, RS1, RS2
# Description:
# The contents of RS1 is added to the contents of RS2 and the result is
# placed in RD.
.text # Define beginning of text section
.global _start # Define entry _start
_start:
li x6, 1 # x6 = 1
li x7, 2 # x7 = 2
add x5, x6, x7 # x5 = x6 + x7
stop:
j stop # Infinite loop to stop execution
.end # End of file
? riscv64-linux-gnu-gcc -nostdlib -fno-builtin -g -Wall test.s -o test
? riscv64-linux-gnu-readelf -h test
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Position-Independent Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x29a
Start of program headers: 64 (bytes into file)
Start of section headers: 5536 (bytes into file)
Flags: 0x5, RVC, double-float ABI
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 8
Size of section headers: 64 (bytes)
Number of section headers: 19
Section header string table index: 18
- 参照x86下的方式,使用 -m 32 发现没这个选项。查看github官方说明文档,发现–print-multi-lib可以打印出支持的架构,执行发现啥也没打印出来。
? riscv64-linux-gnu-gcc --print-multi-lib
.;
- 继续看文档,发现有个 -march/-mabi 选项,继续搜查之。发现gnu官方文档-march gnu官方文档-mabi有关于这两个选项的说明。
- 查看默认情况下这两个选项的值为 ‘-march=rv64gc’ ‘-mabi=lp64d’。使用readelf查看header信息,生成的是elf64。
? riscv64-linux-gnu-gcc -v -nostdlib -fno-builtin -g -Wall test.s -o test
...
COLLECT_GCC_OPTIONS='-v' '-nostdlib' '-fno-builtin' '-g' '-Wall' '-o' 'test' '-march=rv64gc' '-mabi=lp64d' '-misa-spec=20191213' '-march=rv64imafdc_zicsr_zifencei' '-dumpdir' 'test.'
...
? tmp riscv64-linux-gnu-readelf -h test
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Position-Independent Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x29a
Start of program headers: 64 (bytes into file)
Start of section headers: 5536 (bytes into file)
Flags: 0x5, RVC, double-float ABI
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 8
Size of section headers: 64 (bytes)
Number of section headers: 19
Section header string table index: 18
- 基于以上信息,生成32二进制选项可设置为 “-march=rv32ima -mabi=ilp32”。
? riscv64-linux-gnu-gcc -nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall test.s -o test
? riscv64-linux-gnu-readelf -h test
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Position-Independent Executable file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x1b8
Start of program headers: 52 (bytes into file)
Start of section headers: 5220 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 8
Size of section headers: 40 (bytes)
Number of section headers: 19
Section header string table index: 18
文章来源:https://blog.csdn.net/MT1232/article/details/134973839
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!