From 98b54d9861ebb14b28826bc257f235aa9dd1287d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Mon, 15 Dec 2025 16:44:57 +0900 Subject: [PATCH] =?UTF-8?q?ARM=E3=81=A8RISC-V=E3=81=A7=E3=82=82=E5=AE=9F?= =?UTF-8?q?=E8=A1=8C=E5=87=BA=E6=9D=A5=E3=82=8B=E6=A7=98=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 57 ++++++++++++++++++++++++++++++++++++++++++---- main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8f4c533..eeaa7d3 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,28 @@ -此れはlibcなしの「こんにちは、世界」のデモです。\ -Linux、FreeBSD、OpenBSD、NetBSD、及びmacOSで実行出来ますが、IntelやAMD CPUが必須に成ります。 +此れはlibcなしの「こんにちは、世界」のデモです。 -This is a libc-less "hello, world" demo.\ -It runs on Linux, FreeBSD, OpenBSD, NetBSD, and macOS, but requires an Intel or AMD CPU. +This is a libc-less "hello, world" demo. +## 対応 | Support +| OS | CPU | 対応 | テスト済み | +|---------|---------|------|------------| +| Linux | x86-64 | 〇 | 〇 | +| Linux | AArch64 | 〇 | 〇 | +| Linux | RISC-V | 〇 | 〇 | +| FreeBSD | x86-64 | 〇 | 〇 | +| OpenBSD | x86-64 | ✕ | ✕ | +| NetBSD | x86-64 | 〇 | ✕ | +| macOS | x86-64 | 〇 | ✕ | +| macOS | AArch64 | 〇 | ✕ | +| Windows | x86-64 | ✕ | ✕ | +| Windows | AArch64 | ✕ | ✕ | + +## 使い方 | How to use + +### x86-64 ```sh $ cc -nostdlib -static -masm=intel -o main main.c $ ls -thal main --rwxr-xr-x 1 suwako suwako 1.6K 12月 15 15:57 main +-rwxr-xr-x 1 suwako suwako 1.5K 12月 15 15:57 main $ file main main: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically linked, not stripped $ objdump -M intel -d main @@ -29,4 +44,36 @@ Disassembly of section .text: 201213: 0f 05 syscall 201215: 5d pop rbp 201216: c3 ret +$ ./main +こんにちは、世界! +``` + +### AArch64、RISC-V +```sh +$ cc -nostdlib -static -o main main.c +$ ls -thal main +-rwxrwxr-x 1 suwako suwako 5.5K 12月 15 16:54 main +$ file main +main: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), statically linked, BuildID[sha1]=〇〇, not stripped +$ objdump -d main + +main: file format elf64-littleriscv + + +Disassembly of section .text: + +000000000001017c <_start>: + 1017c: 00001797 auipc a5,0x1 + 10180: e8478793 addi a5,a5,-380 # 11000 + 10184: 04000893 li a7,64 + 10188: 4505 li a0,1 + 1018a: 85be mv a1,a5 + 1018c: 4671 li a2,28 + 1018e: 00000073 ecall + 10192: 05d00893 li a7,93 + 10196: 4501 li a0,0 + 10198: 00000073 ecall + 1019c: 0001 nop +$ ./main +こんにちは、世界! ``` diff --git a/main.c b/main.c index ba3c836..427ead5 100644 --- a/main.c +++ b/main.c @@ -1,33 +1,79 @@ #if defined(__linux__) -#define SYS_WRITE 1 -#define SYS_EXIT 60 + #if defined(__x86_64__) + #define SYS_WRITE 1 + #define SYS_EXIT 60 + #define SYSCALL_INST "syscall" + #elif defined(__aarch64__) + #define SYS_WRITE 64 + #define SYS_EXIT 93 + #define SYSCALL_INST "svc #0" + #elif defined(__riscv) && __riscv_xlen == 64 + #define SYS_WRITE 64 + #define SYS_EXIT 93 + #define SYSCALL_INST "ecall" + #endif #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) -#define SYS_WRITE 4 -#define SYS_EXIT 1 + #define SYS_WRITE 4 + #define SYS_EXIT 1 + #define SYSCALL_INST "syscall" #elif defined(__APPLE__) -#define SYS_WRITE 0x2000004 -#define SYS_EXIT 0x2000001 + #define SYS_WRITE 0x2000004 + #define SYS_EXIT 0x2000001 + #if defined(__x86_64__) + #define SYSCALL_INST "syscall" + #elif defined(__aarch64__) + #define SYSCALL_INST "svc #0" + #endif #endif char msg[] = "こんにちは、世界!\n"; +__attribute((naked)) void _start() { asm volatile( + #if defined(__x86_64__) ".intel_syntax noprefix\n" "mov eax, %[sys_write]\n" "mov edi, 1\n" "mov rsi, offset msg\n" "mov edx, %[len]\n" - "syscall\n" + SYSCALL_INST "\n" "mov eax, %[sys_exit]\n" "xor edi, edi\n" - "syscall\n" + SYSCALL_INST "\n" + #elif defined(__aarch64__) + "mov x8, %[sys_write]\n" + "mov x0, #1\n" + "mov x1, %[msg]\n" + "mov x2, %[len]\n" + SYSCALL_INST "\n" - ".att_syntax prefix\n" + "mov x8, %[sys_exit]\n" + "mov x0, #0\n" + SYSCALL_INST "\n" + #elif defined(__riscv) && __riscv_xlen == 64 + "li a7, %[sys_write]\n" + "li a0, 1\n" + "mv a1, %[msg]\n" + "li a2, %[len]\n" + SYSCALL_INST "\n" + + "li a7, %[sys_exit]\n" + "li a0, 0\n" + SYSCALL_INST "\n" + #endif :: [sys_write] "i" (SYS_WRITE), [sys_exit] "i" (SYS_EXIT), - [len] "i" (sizeof(msg) - 1) - : "rax", "rdi", "rsi", "rdx", "rcx", "r11" + [len] "i" (sizeof(msg) - 1), + [msg] "r" (msg) + : "memory", + #if defined(__x86_64__) + "rax", "rdi", "rsi", "rdx" // x86-64 + #elif defined(__aarch64__) + "x0", "x1", "x2", "x8" // AArch64 + #elif defined(__riscv) && __riscv_xlen == 64 + "a0", "a1", "a2", "a7" // RISC-V + #endif ); }