Skip to content

Instantly share code, notes, and snippets.

@shyoshyo
Last active March 9, 2019 07:52
Show Gist options
  • Save shyoshyo/2519823bbc34a2e00ffef6e46d91739f to your computer and use it in GitHub Desktop.
Save shyoshyo/2519823bbc34a2e00ffef6e46d91739f to your computer and use it in GitHub Desktop.
#! /bin/sh
as sort_exec.s -o sort_exec.o
ld --oformat binary sort_exec.o -o sort_exec.bin
~/small žœ ls -al ./sort_exec.bin
-rwxrwxr-x 1 shyo shyo 120 Mar 8 20:19 ./sort_exec.bin*
~/small žœ ./sort_exec.bin list
1
2
2
2
3
5
5
10
28
48
2921
9999
10000
10283
~/small žœ cat list
5
2
3
5
10
28
2
1
48
2
10000
9999
2921
10283
~/small žœ strace ./sort_exec.bin list 2>&1 >/dev/null | head
execve("./sort_exec.bin", ["./sort_exec.bin", "list"], [/* 34 vars */]) = 0
execve("/usr/bin/sort", ["-n", "-n", "list"], NULL) = 0
brk(NULL) = 0x24ad000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=163472, ...}) = 0
mmap(NULL, 163472, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f56d4135000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
.globl _start
_start:
popq %rbx
popq %rbx
pushq %rbx
pushq %rbx
movl $28205, (%rbx) # "-n"
movb $59, %al # sys_exec
movl $.LC0, %edi
movq %rsp, %rsi
syscall
.LC0:
.string "/usr/bin/sort"
.org 0x000000
ehdr: # Elf64_Ehdr
.byte 0x7f
.ascii "ELF"
###########################################
start1:
popq %rbx
popq %rbx
pushq %rbx
pushq %rbx
movl $28205, (%rbx)
jmp start4
.org 0x10
start1end:
##########################################
.word 2 # e_type
.word 0x3e # e_machine
##########################################
start4:
movb $59, %al
jmp start5
.org 0x18
start4end:
##########################################
# .long 0x90###1 # e_version
.quad start1 #; e_entry
.quad phdr - ehdr #; e_phoff
##########################################
start5:
movl $.LC0, %edi
movq %rsp, %rsi
syscall
.org 0x36
start5end:
##########################################
.word phdrsize # e_phentsize
.word 1 # e_phnum
.org 0x40
ehdrsize = (. - ehdr)
phdr: # Elf64_Phdr
.long 1 # p_type
.long 5 # p_flags
.quad 0 # p_offset
.quad ehdr # p_vaddr
##########################################
start2:
.LC0:
.ascii "/usr/bin"
.org 0x60
start2end:
##########################################
.byte '/', 's', 'o', 'r', 't', 0x0, 0x0, 0x0
.byte '/', 's', 'o', 'r', 't', 0x0, 0x0, 0x0
.org 0x70
phdrsize = 0x38
.org 0x78
filesize = (. - 0x78)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment