ARMとRISC-Vでも実行出来る様に
諏訪子 <suwako@076.moe>、2025-12-15 16:44
コミット 98b54d9861ebb14b28826bc257f235aa9dd1287d
・親 3b5dc8f
・ファイルを見る
2個のファイル、+109 -16
変更
README.md
| @@ -1,13 +1,28 @@ | ||
| 1 | -此れはlibcなしの「こんにちは、世界」のデモです。\ | |
| 2 | -Linux、FreeBSD、OpenBSD、NetBSD、及びmacOSで実行出来ますが、IntelやAMD CPUが必須に成ります。 | |
| 1 | +此れはlibcなしの「こんにちは、世界」のデモです。 | |
| 3 | 2 | |
| 4 | -This is a libc-less "hello, world" demo.\ | |
| 5 | -It runs on Linux, FreeBSD, OpenBSD, NetBSD, and macOS, but requires an Intel or AMD CPU. | |
| 3 | +This is a libc-less "hello, world" demo. | |
| 6 | 4 | |
| 5 | +## 対応 | Support | |
| 6 | +| OS | CPU | 対応 | テスト済み | | |
| 7 | +|---------|---------|------|------------| | |
| 8 | +| Linux | x86-64 | 〇 | 〇 | | |
| 9 | +| Linux | AArch64 | 〇 | 〇 | | |
| 10 | +| Linux | RISC-V | 〇 | 〇 | | |
| 11 | +| FreeBSD | x86-64 | 〇 | 〇 | | |
| 12 | +| OpenBSD | x86-64 | ✕ | ✕ | | |
| 13 | +| NetBSD | x86-64 | 〇 | ✕ | | |
| 14 | +| macOS | x86-64 | 〇 | ✕ | | |
| 15 | +| macOS | AArch64 | 〇 | ✕ | | |
| 16 | +| Windows | x86-64 | ✕ | ✕ | | |
| 17 | +| Windows | AArch64 | ✕ | ✕ | | |
| 18 | + | |
| 19 | +## 使い方 | How to use | |
| 20 | + | |
| 21 | +### x86-64 | |
| 7 | 22 | ```sh |
| 8 | 23 | $ cc -nostdlib -static -masm=intel -o main main.c |
| 9 | 24 | $ ls -thal main |
| 10 | --rwxr-xr-x 1 suwako suwako 1.6K 12月 15 15:57 main | |
| 25 | +-rwxr-xr-x 1 suwako suwako 1.5K 12月 15 15:57 main | |
| 11 | 26 | $ file main |
| 12 | 27 | main: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically linked, not stripped |
| 13 | 28 | $ objdump -M intel -d main |
| @@ -29,4 +44,36 @@ | ||
| 29 | 44 | 201213: 0f 05 syscall |
| 30 | 45 | 201215: 5d pop rbp |
| 31 | 46 | 201216: c3 ret |
| 47 | +$ ./main | |
| 48 | +こんにちは、世界! | |
| 49 | +``` | |
| 50 | + | |
| 51 | +### AArch64、RISC-V | |
| 52 | +```sh | |
| 53 | +$ cc -nostdlib -static -o main main.c | |
| 54 | +$ ls -thal main | |
| 55 | +-rwxrwxr-x 1 suwako suwako 5.5K 12月 15 16:54 main | |
| 56 | +$ file main | |
| 57 | +main: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, BuildID[sha1]=〇〇, not stripped | |
| 58 | +$ objdump -d main | |
| 59 | + | |
| 60 | +main: file format elf64-littleriscv | |
| 61 | + | |
| 62 | + | |
| 63 | +Disassembly of section .text: | |
| 64 | + | |
| 65 | +000000000001017c <_start>: | |
| 66 | + 1017c: 00001797 auipc a5,0x1 | |
| 67 | + 10180: e8478793 addi a5,a5,-380 # 11000 <msg> | |
| 68 | + 10184: 04000893 li a7,64 | |
| 69 | + 10188: 4505 li a0,1 | |
| 70 | + 1018a: 85be mv a1,a5 | |
| 71 | + 1018c: 4671 li a2,28 | |
| 72 | + 1018e: 00000073 ecall | |
| 73 | + 10192: 05d00893 li a7,93 | |
| 74 | + 10196: 4501 li a0,0 | |
| 75 | + 10198: 00000073 ecall | |
| 76 | + 1019c: 0001 nop | |
| 77 | +$ ./main | |
| 78 | +こんにちは、世界! | |
| 32 | 79 | ``` |
変更
main.c
| @@ -1,33 +1,79 @@ | ||
| 1 | 1 | #if defined(__linux__) |
| 2 | -#define SYS_WRITE 1 | |
| 3 | -#define SYS_EXIT 60 | |
| 2 | + #if defined(__x86_64__) | |
| 3 | + #define SYS_WRITE 1 | |
| 4 | + #define SYS_EXIT 60 | |
| 5 | + #define SYSCALL_INST "syscall" | |
| 6 | + #elif defined(__aarch64__) | |
| 7 | + #define SYS_WRITE 64 | |
| 8 | + #define SYS_EXIT 93 | |
| 9 | + #define SYSCALL_INST "svc #0" | |
| 10 | + #elif defined(__riscv) && __riscv_xlen == 64 | |
| 11 | + #define SYS_WRITE 64 | |
| 12 | + #define SYS_EXIT 93 | |
| 13 | + #define SYSCALL_INST "ecall" | |
| 14 | + #endif | |
| 4 | 15 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) |
| 5 | -#define SYS_WRITE 4 | |
| 6 | -#define SYS_EXIT 1 | |
| 16 | + #define SYS_WRITE 4 | |
| 17 | + #define SYS_EXIT 1 | |
| 18 | + #define SYSCALL_INST "syscall" | |
| 7 | 19 | #elif defined(__APPLE__) |
| 8 | -#define SYS_WRITE 0x2000004 | |
| 9 | -#define SYS_EXIT 0x2000001 | |
| 20 | + #define SYS_WRITE 0x2000004 | |
| 21 | + #define SYS_EXIT 0x2000001 | |
| 22 | + #if defined(__x86_64__) | |
| 23 | + #define SYSCALL_INST "syscall" | |
| 24 | + #elif defined(__aarch64__) | |
| 25 | + #define SYSCALL_INST "svc #0" | |
| 26 | + #endif | |
| 10 | 27 | #endif |
| 11 | 28 | |
| 12 | 29 | char msg[] = "こんにちは、世界!\n"; |
| 13 | 30 | |
| 31 | +__attribute((naked)) | |
| 14 | 32 | void _start() { |
| 15 | 33 | asm volatile( |
| 34 | + #if defined(__x86_64__) | |
| 16 | 35 | ".intel_syntax noprefix\n" |
| 17 | 36 | "mov eax, %[sys_write]\n" |
| 18 | 37 | "mov edi, 1\n" |
| 19 | 38 | "mov rsi, offset msg\n" |
| 20 | 39 | "mov edx, %[len]\n" |
| 21 | - "syscall\n" | |
| 40 | + SYSCALL_INST "\n" | |
| 22 | 41 | |
| 23 | 42 | "mov eax, %[sys_exit]\n" |
| 24 | 43 | "xor edi, edi\n" |
| 25 | - "syscall\n" | |
| 44 | + SYSCALL_INST "\n" | |
| 45 | + #elif defined(__aarch64__) | |
| 46 | + "mov x8, %[sys_write]\n" | |
| 47 | + "mov x0, #1\n" | |
| 48 | + "mov x1, %[msg]\n" | |
| 49 | + "mov x2, %[len]\n" | |
| 50 | + SYSCALL_INST "\n" | |
| 26 | 51 | |
| 27 | - ".att_syntax prefix\n" | |
| 52 | + "mov x8, %[sys_exit]\n" | |
| 53 | + "mov x0, #0\n" | |
| 54 | + SYSCALL_INST "\n" | |
| 55 | + #elif defined(__riscv) && __riscv_xlen == 64 | |
| 56 | + "li a7, %[sys_write]\n" | |
| 57 | + "li a0, 1\n" | |
| 58 | + "mv a1, %[msg]\n" | |
| 59 | + "li a2, %[len]\n" | |
| 60 | + SYSCALL_INST "\n" | |
| 61 | + | |
| 62 | + "li a7, %[sys_exit]\n" | |
| 63 | + "li a0, 0\n" | |
| 64 | + SYSCALL_INST "\n" | |
| 65 | + #endif | |
| 28 | 66 | :: [sys_write] "i" (SYS_WRITE), |
| 29 | 67 | [sys_exit] "i" (SYS_EXIT), |
| 30 | - [len] "i" (sizeof(msg) - 1) | |
| 31 | - : "rax", "rdi", "rsi", "rdx", "rcx", "r11" | |
| 68 | + [len] "i" (sizeof(msg) - 1), | |
| 69 | + [msg] "r" (msg) | |
| 70 | + : "memory", | |
| 71 | + #if defined(__x86_64__) | |
| 72 | + "rax", "rdi", "rsi", "rdx" // x86-64 | |
| 73 | + #elif defined(__aarch64__) | |
| 74 | + "x0", "x1", "x2", "x8" // AArch64 | |
| 75 | + #elif defined(__riscv) && __riscv_xlen == 64 | |
| 76 | + "a0", "a1", "a2", "a7" // RISC-V | |
| 77 | + #endif | |
| 32 | 78 | ); |
| 33 | 79 | } |