用 riscv64- Linux -gnu-gcc 编译的 c 文件为啥能在 x8664 下运行? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
rookiemaster
V2EX    C

用 riscv64- Linux -gnu-gcc 编译的 c 文件为啥能在 x8664 下运行?

  •  1
     
  •   rookiemaster 2024-04-04 18:10:33 +08:00 3650 次点击
    这是一个创建于 621 天前的主题,其中的信息可能已经有所发展或是发生改变。

    就一个简单的 c 程序:

    #include <stdio.h> int main() { printf("Hello, World\n"); } 

    使用如下命令编译:

    riscv64-linux-gnu-gcc --static hello.c -o hello_riscv64 

    居然可以直接运行

    > ./hello_riscv64 Hello, World 

    使用 qemu 也能运行:

    > qemu-riscv64-static hello_riscv64 Hello, World 

    readelf 结果:

    > readelf -h hello_riscv64 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: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x1054c Start of program headers: 64 (bytes into file) Start of section headers: 501880 (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: 7 Size of section headers: 64 (bytes) Number of section headers: 28 Section header string table index: 27 
    9 条回复    2024-04-05 00:28:59 +08:00
    koebehshian
        1
    koebehshian  
       2024-04-04 18:13:35 +08:00
    又不是裸机,操作系统解释了吧
    SteveLee
        2
    SteveLee  
       2024-04-04 18:21:30 +08:00 via iPhone   2
    可能 binfmt_misc 自动调用 qemu 了,可以 ls /proc/sys/fs/binfmt_misc/ 看下
    rookiemaster
        3
    rookiemaster  
    OP
       2024-04-04 18:32:11 +08:00
    @SteveLee 谢谢,我使用 strace 命令观察系统调用(strace ./hello_riscv64),发了一个系统调用为 readlink("/proc/self/exe", "/usr/bin/qemu-riscv64-static", 4096) = 28 。
    但是不知道是什么意思
    anntt
        4
    anntt  
       2024-04-04 18:36:22 +08:00 via iPhone
    亲测不能运行,不知道题主那里的问题
    rookiemaster
        5
    rookiemaster  
    OP
       2024-04-04 18:48:01 +08:00
    @anntt 我试试在没有装 qemu 的机子上试试
    pagxir
        6
    pagxir  
       2024-04-04 18:54:37 +08:00 via Android
    因为是用 qemu-user 解释执行了。效率很低,arm64/arm 一样可以解释执行。
    peachpeach
        7
    peachpeach  
       2024-04-04 19:04:07 +08:00
    你可以理解 qemu 就是个虚拟机, 虚拟各种 CPU 架构, 甚至可以虚拟嵌入式开发板
    rookiemaster
        8
    rookiemaster  
    OP
       2024-04-04 19:54:43 +08:00
    应该是 qemu 会自动执行,把 qemu 卸载了就会正常报错了
    jim9606
        9
    jim9606  
       2024-04-05 00:28:59 +08:00
    可能是利用了内核的 binfmt_misc 机制自动调用 qemu 运行了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1070 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 18:16 PVG 02:16 LAX 10:16 JFK 13:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86