This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A toy implementation of the kill command with a few simplifications | |
- Passing PID in argv[1] | |
- Hard code the signal number to 9 (for SIGKILL) | |
- Exit the program with the return value of the kill system call | |
Solving https://blog.codingconfessions.com/p/x86-assembly-exercise-1 , but in ARM64 assembly | |
Compiles to a 211 byte binary! | |
`as -o kill.o kill.S && ld -s -o kk kill.o` to compile. Run strip / sstrip to remove the section headers | |
*/ | |
.equ SYS_KILL, 129 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from http://wiki.osdev.org/QEMU_AArch64_Virt_Bare_Bones and https://krinkinmu.github.io/2020/11/29/PL011.html | |
// Uses UEFI shell to load the kernel | |
// Run on qemu using: `qemu-system-aarch64 -machine virt -cpu max -drive if=pflash,format=raw,file=./OVMF.fd -drive format=raw,file=fat:rw:./root -net none -serial telnet:localhost:1234,server -monitor telnet:localhost:1235,server,nowait -serial mon:stdio -nographic` | |
.equ UART_ADDR, 0x9000000 | |
.data | |
msg: | |
.ascii "Hello World!\n"; | |
len = . - msg | |
.text | |
.global _start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// as -o /tmp/calc.o calc.S && ld -s -o /tmp/calc /tmp/calc.o && strip --strip-section-headers /tmp/calc | |
.equ SYS_MEMFD_CREATE, 279 | |
.equ SYS_FTRUNCATE, 46 | |
.equ SYS_MMAP, 222 | |
.equ SYS_READ, 63 | |
.equ SYS_WRITE, 64 | |
.equ SYS_EXIT, 93 | |
.equ BUF_SIZE, 64 | |
.equ PAGE_SIZE, 4096 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Teensy Elf using GNU as and objcopy | |
as -o teensy.o teensy.S && objcopy -O binary teensy.o teensy.bin && chmod +x teensy.bin | |
References: | |
Elf Format details: https://gist.github.com/x0nu11byt3/bcb35c3de461e5fb66173071a2379779 | |
Build a Teensy Elf (x86) file: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html | |
Adding Elf headers using GNU as: https://stackoverflow.com/q/77244360 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.equ BUF_SIZE, 0x1000 | |
.section .text | |
.global _start | |
_start: | |
sub sp, sp, #BUF_SIZE // Reserve 4096 bytes on the stack | |
mov x1, sp // x1 -> buffer, also passed to write | |
mov x2, #BUF_SIZE // x2 = total size, also to be passed to write syscall | |
movz w3, #0x0A79 // lower 16 bits | |
mov x4, #0 // x4 = counter | |
fill_loop: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.equ SYS_clone3, 435 | |
.equ SYS_write, 64 | |
.equ SYS_exit, 93 | |
.equ CHILD_STACK_SIZE, 0x4000 // 16 KB per child | |
.equ CLONE_VM, 0x00000100 | |
.equ CLONE_FS, 0x00000200 | |
.equ CLONE_FILES, 0x00000400 | |
.equ CLONE_SIGHAND, 0x00000800 | |
.equ CLONE_PARENT, 0x00008000 | |
.equ CLONE_THREAD, 0x00010000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.equ SYS_READ, 63 | |
.equ SYS_WRITE, 64 | |
.equ SYS_EXIT, 93 | |
.equ SYS_SOCKET, 198 | |
.equ SYS_BIND, 200 | |
.equ SYS_LISTEN, 201 | |
.equ SYS_ACCEPT, 202 | |
.equ SYS_CLOSE, 57 | |
.equ SYS_OPENAT, 56 | |
.equ SYS_SENDFILE, 71 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snake for Linux terminal | |
// Fits in a QR code! | |
// Compile with: | |
// zig build-exe snek.zig -O ReleaseSmall -target x86_64-linux -fstrip -flto -fsingle-threaded -femit-bin=snek_x64 | |
// zig build-exe snek.zig -O ReleaseSmall -target aarch64-linux -fstrip -flto -fsingle-threaded -femit-bin=snek_aarch64 | |
// Run `sstrip -z` from https://www.muppetlabs.com/~breadbox/software/elfkickers.html to reduce binary size even further | |
// Currently, snek_x64 is 2616 bytes and snek_aarch64 is 2941 bytes | |
// Threshold for a binary QR code is 2953 bytes | |
// Encode using: | |
// qrencode -8 -r snek_aarch64 -o qr_aarch64.png |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Compile with one of the following: | |
zig cc -v -s -Os -target aarch64-linux-musl -nostdlib -flto -static shell.c -o csh | |
zig cc -v -s -Os -target aarch64-linux-gnu -nostdlib -flto -static shell.c -o csh | |
CLANG: | |
clang -v -s -Oz -ffreestanding -nostdlib -fno-stack-protector -Wl,--entry=_start -Wl,--gc-sections -Wl,-z,now -flto -static -o csh shell.c | |
musl-clang -v -s -Os -nostdlib -nostartfiles -fno-stack-protector -Wl,--entry=_start -Wl,--gc-sections -Wl,-z,now -flto -nostdinc -static -o csh shell.c | |
GCC (some things DO NOT WORK like `pwd`): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import os | |
os.environ["KMP_DUPLICATE_LIB_OK"] ="TRUE" | |
import sys | |
import re | |
import time | |
import torch as t | |
import numpy as np | |
from pathlib import Path |
NewerOlder