Skip to content

Instantly share code, notes, and snippets.

@xen0n
Last active March 29, 2024 10:12
Show Gist options
  • Save xen0n/5ee04aaa6cecc5c7794b9a0c3b65fc7f to your computer and use it in GitHub Desktop.
Save xen0n/5ee04aaa6cecc5c7794b9a0c3b65fc7f to your computer and use it in GitHub Desktop.
Tiny helper checking for LoongArch new world UAPI
# SPDX-License-Identifier: GPL-2.0-or-later OR MIT
#
# how to build this:
#
# $ as -o check-new-world.tiny.o check-new-world.tiny.s
# $ ld check-new-world.tiny.o -o check-new-world.tiny.tmp
# $ objcopy -O binary --only-section=.blob ./check-new-world.tiny.tmp ./check-new-world.tiny
.section ".blob", "aw", @progbits
filestart:
# e_ident
.ascii "\177ELF"
.byte 0x02 # ELFCLASS64
.byte 0x01 # ELFDATA2LSB
.byte 0x01 # EV_CURRENT
.byte 0x00 # ELFOSABI_NONE
.byte 0x00 # EI_ABIVERSION = 0
.rept 7
.byte 0
.endr
# a random base address that's big enough for even 64KiB-page kernels
.set base_addr, 0x200000
.short 2 # ET_EXEC
.short 0x102 # EM_LOONGARCH
.word 1 # e_version = 1
.dword base_addr + entry - filestart # e_entry
.dword phdr - filestart # e_phoff
.dword 0 # e_shoff
.word 0x41 # objabi v1, soft-float
.short ehsize # e_ehsize
.short phentsize # e_phentsize
.short 1 # e_phnum
.short 0 # e_shentsize
.short 0 # e_shnum
.short 0 # e_shstrndx
.set ehsize, . - filestart
phdr:
.word 1 # p_type = PT_LOAD
.word 0x5 # p_flags = PF_R | PF_X
.dword 0 # p_offset
.dword base_addr # p_vaddr
.dword base_addr # p_paddr
.dword text_size # p_filesz
.dword text_size # p_memsz
.dword 0x10000 # p_align = 64KiB
.set phentsize, . - phdr
entry:
# rt_sigaction(SIGUSR1, NULL, NULL, 8)
li.w $a0, 10
move $a1, $zero
move $a2, $zero
li.w $a3, 8
li.w $a7, 134
syscall 0
# a0 = -EINVAL if running on old world, 0 otherwise
# a0 = a0 ? 1 : 0
# not actually necessary
#sltui $a0, $a0, 1
#xori $a0, $a0, 1
# exit_group(a0)
li.w $a7, 94
syscall 0
# signature line in base64 ;-)
stptr.d $t5, $t5, 32120
.set text_size, . - entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment