summaryrefslogtreecommitdiff
path: root/rt
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-08-11 19:34:12 -0400
committerOri Bernstein <ori@eigenstate.org>2014-08-11 19:36:19 -0400
commit70ab782a15f19d4ea160304bb35f7cd1defa7d5d (patch)
treebfb3798d0cb0e5757eebccd645217ac88faa252b /rt
parentfb85c50fc2abd8ba903f72b59567b9e7f2e31c1c (diff)
downloadmc-70ab782a15f19d4ea160304bb35f7cd1defa7d5d.tar.gz
Spit out the runtime from libstd.
This will allow linking code without libstd, as well as fixing link order on freebsd. Freebsd seems to be picky aobut where '_start' is searched from.
Diffstat (limited to 'rt')
-rw-r--r--rt/Makefile25
-rw-r--r--rt/start-freebsd.s62
-rw-r--r--rt/start-linux.s74
-rw-r--r--rt/start-osx.s63
4 files changed, 224 insertions, 0 deletions
diff --git a/rt/Makefile b/rt/Makefile
new file mode 100644
index 0000000..160f3c5
--- /dev/null
+++ b/rt/Makefile
@@ -0,0 +1,25 @@
+OBJ = _myrrt.o
+ASMSRC = start.s common.s
+
+all: _myrrt.o
+
+include ../config.mk
+
+_myrrt.o: _myrrt.s
+ as -g -o $@ $^
+
+_myrrt.s: $(ASMSRC)
+ cat $(ASMSRC) > $@
+
+%.s: %-$(SYS).s
+ cp $< $@
+
+install: all
+ mkdir -p $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr)
+ install _myrrt.o $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr)
+
+uninstall:
+ rm -f $(abspath $(DESTDIR)/$(INST_ROOT)/lib/myr/_myrrt.o)
+
+clean:
+ rm -f _myrrt.o _myrrt.s start.s
diff --git a/rt/start-freebsd.s b/rt/start-freebsd.s
new file mode 100644
index 0000000..193d922
--- /dev/null
+++ b/rt/start-freebsd.s
@@ -0,0 +1,62 @@
+.data
+/* std._environment : byte[:][:] */
+.globl std$_environment
+std$_environment:
+.envbase:
+.quad 0 /* env size */
+.envlen:
+.quad 0 /* env ptr */
+
+.globl std$__cenvp
+std$__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,std$__cenvp(%rip)
+ movq %r9,%rax
+ movq %rsp, %rcx
+ movq %r9,.envlen
+ movq %rdx,.envbase
+ 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
+
+ /* enter the main program */
+ call main
+ /* exit(0) */
+ xorq %rdi,%rdi
+ movq $1,%rax
+ syscall
+
diff --git a/rt/start-linux.s b/rt/start-linux.s
new file mode 100644
index 0000000..717d00f
--- /dev/null
+++ b/rt/start-linux.s
@@ -0,0 +1,74 @@
+.data
+/* std._environment : byte[:][:] */
+.globl std$_environment
+std$_environment:
+.envbase:
+.quad 0 /* env size */
+.envlen:
+.quad 0 /* env ptr */
+
+.globl std$__cenvp
+std$__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:
+ /* turn args into a slice */
+ movq %rsp,%rbp
+
+ /* stack allocate sizeof(byte[:])*(argc + len(envp)) */
+ movq (%rbp),%rax
+ leaq 16(%rbp,%rax,8), %rbx /* argp = argv + 8*argc + 8 */
+ call count
+ addq %r9,%rax
+ imulq $16,%rax
+ subq %rax,%rsp
+ movq %rsp, %rdx /* saved args[:] */
+
+ /* stack allocate sizeof(byte[:])*(argc + len(envp)) */
+ movq (%rbp),%rax
+ leaq 16(%rbp,%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 (%rbp),%rax
+ leaq 16(%rbp,%rax,8), %rbx /* envp = argv + 8*argc + 8 */
+ /* store envp for some syscalls to use without converting */
+ movq %rbx,std$__cenvp(%rip)
+ movq %r9,%rax
+ movq %rsp, %rcx
+ movq %r9,.envlen
+ movq %rdx,.envbase
+ call cvt
+ movq %rcx,%rdx
+
+ /* convert argc, argv to byte[:][:] for args. */
+ movq (%rbp), %rax /* argc */
+ leaq 8(%rbp), %rbx /* argv */
+ movq (%rbp), %rsi /* saved argc */
+ call cvt
+ pushq %rsi
+ pushq %rdx
+
+ /* enter the main program */
+ call main
+ /* exit(0) */
+ xorq %rdi,%rdi
+ movq $60,%rax
+ syscall
+
diff --git a/rt/start-osx.s b/rt/start-osx.s
new file mode 100644
index 0000000..04bdf43
--- /dev/null
+++ b/rt/start-osx.s
@@ -0,0 +1,63 @@
+.data
+/* std._environment : byte[:][:] */
+.globl _std$_environment
+_std$_environment:
+.envbase:
+.quad 0 /* env size */
+.envlen:
+.quad 0 /* env ptr */
+
+.globl _std$__cenvp
+_std$__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:
+ /* turn args into a slice */
+ movq %rsp,%rbp
+ /* stack allocate sizeof(byte[:])*(argc + len(envp)) */
+ movq (%rbp),%rax
+ leaq 16(%rbp,%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 (%rbp),%rax
+ leaq 16(%rbp,%rax,8), %rbx /* envp = argv + 8*argc + 8 */
+ movq %rbx,_std$__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 (%rbp), %rax /* argc */
+ leaq 8(%rbp), %rbx /* argv */
+ movq (%rbp), %rsi /* saved argc */
+ call cvt
+ pushq %rsi
+ pushq %rdx
+
+ /* enter the main program */
+ call _main
+ /* exit */
+ xorq %rdi,%rdi
+ movq $0x2000001,%rax
+ syscall
+