ARMとRISC-Vでも実行出来る様に
This commit is contained in:
57
README.md
57
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 <msg>
|
||||
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
|
||||
こんにちは、世界!
|
||||
```
|
||||
|
||||
56
main.c
56
main.c
@@ -1,33 +1,79 @@
|
||||
#if defined(__linux__)
|
||||
#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 SYSCALL_INST "syscall"
|
||||
#elif defined(__APPLE__)
|
||||
#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
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user