Skip to content

Instantly share code, notes, and snippets.

@ephemient
Last active June 9, 2016 22:18
Show Gist options
  • Save ephemient/974564c92ff76d24edb6f0d7f854aaa7 to your computer and use it in GitHub Desktop.
Save ephemient/974564c92ff76d24edb6f0d7f854aaa7 to your computer and use it in GitHub Desktop.
mem.S
LDFLAGS += -Wl,--build-id=none -nostdlib -s
run: mem
strace -eraw=all ./mem >/dev/null
mem: mem.S
$(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_MACH) $^ $(LOADLIBES) $(LDLIBS) -o $@
mem-translated: mem-translated.c
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_MACH) $^ $(LOADLIBES) $(LDLIBS) -o $@
.PHONY: run
#include <asm/param.h>
#include <sys/syscall.h>
#ifndef _syscall1
# define _syscall1(type, name, arg1) ({ \
long __syscall_rc; \
__asm__ volatile ( \
"syscall" \
: "=a" (__syscall_rc) \
: "a" (SYS_##name), "D" (arg1) \
: "%rcx", "%r11", "memory"); \
(type) __syscall_rc; \
})
#endif
#ifndef _syscall2
# define _syscall2(type, name, arg1, arg2) ({ \
long __syscall_rc; \
__asm__ volatile ( \
"syscall" \
: "=a" (__syscall_rc) \
: "a" (SYS_##name), "D" (arg1), "S" (arg2) \
: "%rcx", "%r11", "memory"); \
(type) __syscall_rc; \
})
#endif
#ifndef _syscall3
# define _syscall3(type, name, arg1, arg2, arg3) ({ \
long __syscall_rc; \
__asm__ volatile ( \
"syscall" \
: "=a" (__syscall_rc) \
: "a" (SYS_##name), "D" (arg1), "S" (arg2), "d" (arg3) \
: "%rcx", "%r11", "memory"); \
(type) __syscall_rc; \
})
#endif
static char proc_self_maps[] = "/proc/self/maps";
static char buffer[EXEC_PAGESIZE];
int _start() {
char c;
int rc, sz, i, r;
long val, save;
rc = _syscall2(int, open, proc_self_maps, 0);
if (rc >= 0) {
sz = _syscall3(int, read, rc, buffer, EXEC_PAGESIZE);
if (sz >= 0) {
_syscall1(int, close, rc);
i = 0;
while (i < sz) {
save = val;
val = 0;
while (1) {
c = buffer[i++];
r = 1 << (c & 0x1f);
if (!(r & 0x3ff007e)) break;
if (r & 0x7e) c -= 7;
val = (val << 4) + (c & 0xf);
}
if (c == '-') continue;
while (i < sz && buffer[i++] != '\n');
while (save < val) {
_syscall3(int, write, 1, save, EXEC_PAGESIZE);
save += EXEC_PAGESIZE;
}
}
rc = 0;
}
}
_syscall1(void, exit, rc);
}
#include <asm/param.h>
#include <sys/syscall.h>
.data
proc_self_maps:
.asciz "/proc/self/maps"
.bss
buffer:
.skip EXEC_PAGESIZE
.text
.globl _start
_start:
mov $SYS_open, %rax
mov $proc_self_maps, %rdi
mov $0, %rsi
syscall
mov %rax, %rdi
test $0, %rdi
jl die
mov $SYS_read, %rax
mov $buffer, %rsi
mov $EXEC_PAGESIZE, %rdx
syscall
mov %rax, %r12
cmp $0, %r12
jl die
mov $SYS_close, %rax
syscall
mov $0, %r13
line0:
cmp %r12, %r13
jge exit
mov %rdx, %rbx
mov $0, %rdx
hex0:
mov buffer(%r13), %cl
inc %r13
mov $1, %eax
rol %cl, %eax
test $0x3ff007e, %eax
jz hex2
test $0x7e, %eax
jz hex1
sub $7, %cl
hex1:
and $0xf, %rcx
shl $4, %rdx
add %rcx, %rdx
jmp hex0
hex2:
cmp $'-', %cl
jz line0
mov $'\n', %al
mov $buffer, %rdi
add %r13, %rdi
mov %r12, %rcx
sub %r13, %rcx
mov $buffer, %rsi
repne scasb (%rdi)
sub $buffer, %rdi
mov %rdi, %r13
mov $1, %rdi
mov %rbx, %rsi
mov %rdx, %rbx
mov $EXEC_PAGESIZE, %rdx
line1:
cmp %rsi, %rbx
jle line0
mov $SYS_write, %rax
syscall
add $EXEC_PAGESIZE, %rsi
jmp line1
exit:
mov $0, %rdi
die:
mov $SYS_exit, %rax
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment