Skip to content

Instantly share code, notes, and snippets.

@tribals
Last active September 25, 2022 09:44
Show Gist options
  • Save tribals/54122e0eb46ee6af09e12a6c23b81b4b to your computer and use it in GitHub Desktop.
Save tribals/54122e0eb46ee6af09e12a6c23b81b4b to your computer and use it in GitHub Desktop.
Elves
elf
object-replacement.char
new-elf

Evaluate

I preffer to manage my stuff using Guix, see: Installation.

Then:

[env]$ guix shell --container
[env]$ ./elf.sh
[env]$ make new-elf
[env]$ hexdump -C new-elf

Try to copy it to clipboard:

$ xsel -b < new-elf
$ xsel -b | hexdump -C
; source: https://nathanotterness.com/2021/10/tiny_elf_modernized.html
[bits 64]
file_load_va: equ 4096 * 40
db 0x7f, 'E', 'L', 'F'
db 2
db 1
db 1
db 0
entry_point:
xor eax, eax
inc eax
mov edi, eax
jmp code_chunk_2
dw 2
dw 0x3e
dd 1
dq entry_point + file_load_va
dq program_headers_start
code_chunk_2:
mov esi, file_load_va + message
xor edx, edx
mov dl, message_length
jmp code_chunk_3
db 0
dw 64
dw 0x38
dw 1
; We simply deleted the three two-byte fields that used to be here. The only
; one that mattered, the number of section headers, will still be zero due to
; the upper two bytes of the field at the start of the program header being
; zero.
program_headers_start:
; These next two fields also serve as the final six bytes of the ELF header.
dd 1
dd 5
dq 0
dq file_load_va
code_chunk_3:
syscall
mov al, 60
xor edi, edi
syscall
dq file_end
dq file_end
message: db `Hello, world!\n`
message_length: equ $ - message
file_end:
#!/bin/sh
make elf
chmod +x elf
exec ./elf
elf: elf.S
nasm -f bin -o elf elf.S
object-replacement.char:
printf '\xef\xbf\xbc' > object-replacement.char # is U+FFFC in UTF-8
new-elf: elf object-replacement.char
cat object-replacement.char elf > new-elf
.PHONY: clean
clean:
rm -f elf object-replacement.char new-elf
(use-modules
(guix profiles)
(guix config)
(gnu packages base)
(gnu packages assembly)
(gnu packages linux))
(packages->manifest
(list
gnu-make
nasm
util-linux ; printf
coreutils)) ; rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment