diff options
author | Andrew Chambers <andrewchamberss@gmail.com> | 2016-01-16 13:35:11 +1300 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2016-05-08 17:20:43 +1200 |
commit | 79c93065bf8467896e078459a91e36dc66ddaaeb (patch) | |
tree | 029fdf5f2761fc8a82c112d8bde84d9212cb287e /rt | |
parent | b332e98485d1c22cb93f3302b83ead9e004d5160 (diff) | |
download | mc-79c93065bf8467896e078459a91e36dc66ddaaeb.tar.gz |
Add initial openbsd skeleton
Diffstat (limited to 'rt')
-rw-r--r-- | rt/abort-openbsd.s | 42 | ||||
-rw-r--r-- | rt/start-openbsd.s | 65 |
2 files changed, 107 insertions, 0 deletions
diff --git a/rt/abort-openbsd.s b/rt/abort-openbsd.s new file mode 100644 index 0000000..a4b12bb --- /dev/null +++ b/rt/abort-openbsd.s @@ -0,0 +1,42 @@ +.text + +.globl _rt$abort_oob +.globl __rt$abort_oob +_rt$abort_oob: +__rt$abort_oob: + /* format pc */ + movq (%rsp),%rax + movq $15,%rdx + leaq .digitchars(%rip),%r8 + leaq .pcstr(%rip),%r9 +.loop: + movq %rax, %rcx + andq $0xf, %rcx + movb (%r8,%rcx),%r10b + movb %r10b,(%r9,%rdx) + subq $1, %rdx + shrq $4, %rax + jnz .loop + /* write abort message */ + movq $4, %rax /* write(fd=%rdi, msg=%rsi, len=%rdx) */ + movq $2, %rdi /* fd */ + leaq .msg(%rip), %rsi /* msg */ + movq $(.msgend-.msg), %rdx /* length */ + syscall + /* kill self */ + movq $20,%rax /* getpid */ + syscall + movq %rax, %rdi /* save pid */ + movq $37, %rax /* kill(pid=%rdi, sig=%rsi) */ + movq $6, %rsi + syscall +.data +.msg: /* pc name: */ + .ascii "0x" +.pcstr: + .ascii "0000000000000000" + .ascii ": out of bounds access\n" +.msgend: + +.digitchars: + .ascii "0123456789abcdef" diff --git a/rt/start-openbsd.s b/rt/start-openbsd.s new file mode 100644 index 0000000..54cd0ce --- /dev/null +++ b/rt/start-openbsd.s @@ -0,0 +1,65 @@ +.data +/* std._environment : byte[:][:] */ +.globl sys$__environment +sys$__environment: +.envbase: +.quad 0 /* env size */ +.envlen: +.quad 0 /* env ptr */ + +.globl sys$__cenvp +sys$__cenvp: +.quad 0 + +.text +/* + * The entry point for the whole program. + * This is called by the OS. In order, it: + * - Sets up all argc entries as slices + * - Sets up all envp entries as slices + * - Converts argc/argv to a slice + * - Stashes envp in std._environment + * - Stashes a raw envp copy in __cenvp (for syscalls to use) + * - Calls main() + */ +.globl _start +_start: + /* stack allocate sizeof(byte[:])*(argc + len(envp)) */ + movq (%rdi),%rax + leaq 16(%rdi,%rax,8), %rbx /* argp = argv + 8*argc + 8 */ + call count + addq %r9,%rax + imulq $16,%rax + subq %rax,%rsp + movq %rsp, %rdx /* saved args[:] */ + + /* convert envp to byte[:][:] for std._environment */ + movq (%rdi),%rax + leaq 16(%rdi,%rax,8), %rbx /* envp = argv + 8*argc + 8 */ + /* store envp for some syscalls to use without spurious conversion. */ + movq %rbx,sys$__cenvp(%rip) + movq %r9,%rax + movq %rsp, %rcx + movq %r9,.envlen(%rip) + movq %rdx,.envbase(%rip) + call cvt + movq %rcx,%rdx + + /* convert argc, argv to byte[:][:] for args. */ + movq (%rdi), %rax /* argc */ + leaq 8(%rdi), %rbx /* argv */ + movq (%rdi), %rsi /* saved argc */ + call cvt + pushq %rsi + pushq %rdx + + /* call pre-main initializers */ + call __init__ + + /* enter the main program */ + call main + /* exit(0) */ + xorq %rdi,%rdi + movq $1,%rax + syscall + |