diff options
author | Ori Bernstein <ori@eigenstate.org> | 2014-08-11 19:34:12 -0400 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2014-08-11 19:36:19 -0400 |
commit | ee441ecb766f47f0d903bbf224b199b67055c9dc (patch) | |
tree | bfb3798d0cb0e5757eebccd645217ac88faa252b | |
parent | 96fbc5f09cedc83bb93df50e13db036892c83917 (diff) | |
download | mc-ee441ecb766f47f0d903bbf224b199b67055c9dc.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.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | libstd/Makefile | 1 | ||||
-rw-r--r-- | myrbuild/myrbuild.c | 34 | ||||
-rw-r--r-- | rt/Makefile | 25 | ||||
-rw-r--r-- | rt/start-freebsd.s (renamed from libstd/start-freebsd.s) | 61 | ||||
-rw-r--r-- | rt/start-linux.s (renamed from libstd/start-linux.s) | 65 | ||||
-rw-r--r-- | rt/start-osx.s (renamed from libstd/start-osx.s) | 56 | ||||
-rwxr-xr-x | test/runtest.sh | 2 |
8 files changed, 65 insertions, 180 deletions
@@ -3,6 +3,7 @@ SUB = parse \ 6 \ muse \ myrbuild \ + rt \ libstd \ doc diff --git a/libstd/Makefile b/libstd/Makefile index 5a1469a..364cd9f 100644 --- a/libstd/Makefile +++ b/libstd/Makefile @@ -53,7 +53,6 @@ MYRSRC= \ waitstatus.myr \ ASMSRC= \ - start.s \ syscall.s \ util.s diff --git a/myrbuild/myrbuild.c b/myrbuild/myrbuild.c index a25978a..cb1951d 100644 --- a/myrbuild/myrbuild.c +++ b/myrbuild/myrbuild.c @@ -30,6 +30,8 @@ char *as = "as"; char *ar = "ar"; char *ld = "ld"; char *muse = "muse"; +char *runtime = "_myrrt.o"; +char *runtimepath; /* the name of the output file */ char *libname; char *binname; @@ -194,7 +196,6 @@ FILE *openlib(char *lib) char buf[1024]; size_t i; - snprintf(buf, sizeof buf, "%s/%s/%s", Instroot, "/lib/myr", lib); f = fopen(buf, "r"); if (f) return f; @@ -404,6 +405,10 @@ void linkobj(char **files, size_t nfiles) lappend(&args, &nargs, strdup(buf)); } + if (runtimepath) { + lappend(&args, &nargs, strdup(runtimepath)); + } + /* ld -T ldscript -o outfile foo.o bar.o baz.o */ for (i = 0; i < nfiles; i++) { if (hassuffix(files[i], ".myr")) @@ -420,8 +425,6 @@ void linkobj(char **files, size_t nfiles) snprintf(buf, sizeof buf, "-L%s", incpaths[i]); lappend(&args, &nargs, strdup(buf)); } - snprintf(buf, sizeof buf, "-L%s%s", Instroot, "/lib/myr"); - lappend(&args, &nargs, strdup(buf)); /* ld -T ldscript -o outfile foo.o bar.o baz.o -L/path1 -L/path2 -llib1 -llib2*/ addlibs(&args, &nargs, libgraph); @@ -442,6 +445,24 @@ void linkobj(char **files, size_t nfiles) lfree(&args, &nargs); } +void findruntime() +{ + char buf[2048]; + size_t i; + + if (!strcmp(runtime, "none")) + runtimepath = NULL; + for (i = 0; i < nincpaths; i++) { + snprintf(buf, sizeof buf, "%s/%s", incpaths[i], runtime); + if (access(buf, R_OK) == 0) { + printf("Got %s\n", buf); + runtimepath = strdup(buf); + return; + } + } + err(1, "Could not find runtime %s", runtime); +} + int main(int argc, char **argv) { int opt; @@ -450,7 +471,8 @@ int main(int argc, char **argv) if (uname(&name) == 0) sysname = strdup(name.sysname); - while ((opt = getopt(argc, argv, "hb:l:s:SI:C:A:M:L:R:")) != -1) { + lappend(&incpaths, &nincpaths, strdup(Instroot "/lib/myr")); + while ((opt = getopt(argc, argv, "hb:l:s:r:SI:C:A:M:L:R:")) != -1) { switch (opt) { case 'b': binname = optarg; break; case 'l': libname = optarg; break; @@ -461,8 +483,9 @@ int main(int argc, char **argv) case 'M': muse = optarg; break; case 'L': ld = optarg; break; case 'R': ar = optarg; break; + case 'r': runtime = optarg; break; case 'I': - lappend(&incpaths, &nincpaths, optarg); + lappend(&incpaths, &nincpaths, strdup(optarg)); break; case 'h': usage(argv[0]); @@ -481,6 +504,7 @@ int main(int argc, char **argv) libgraph = mkht(strhash, streq); compiled = mkht(strhash, streq); loopdetect = mkht(strhash, streq); + findruntime(); regcomp(&usepat, "^[[:space:]]*use[[:space:]]+([^[:space:]]+)", REG_EXTENDED); for (i = optind; i < argc; i++) compile(argv[i]); 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/libstd/start-freebsd.s b/rt/start-freebsd.s index d81d242..193d922 100644 --- a/libstd/start-freebsd.s +++ b/rt/start-freebsd.s @@ -13,67 +13,6 @@ std$__cenvp: .text /* - * counts the length of the string pointed to - * by %r8, returning len in %r9. Does not modify - * any registers outside of %r9 - */ -cstrlen: - xorq %r9,%r9 - jmp .lentest - - .lenloop: - incq %r9 - .lentest: - cmpb $0,(%r8,%r9) - jne .lenloop - ret - - -/* - * Counts the size of the null terminated string vector - * pointed to by %rbx. Clobbers %r10,%r11 - */ -count: - xorq %r9,%r9 - movq %rbx,%r11 -.countloop: - movq (%r11),%r10 - testq %r10,%r10 - jz .countdone - addq $1,%r9 - addq $8,%r11 - jmp .countloop -.countdone: - ret - -/* - * iterate over the strings for argc, and put - * them into the args array. - * - * Args: - * %rax: holds argc - * %rbx: holds argv - * %rcx: output destination - * Clobbers: - * %r8, %r9 - */ -cvt: - jmp .cvttest -.cvtloop: - subq $1,%rax - movq (%rbx),%r8 - call cstrlen - movq %r8, (%rcx) - movq %r9, 8(%rcx) - addq $8, %rbx - addq $16, %rcx -.cvttest: - testq %rax,%rax - jnz .cvtloop -.cvtdone: - ret - -/* * The entry point for the whole program. * This is called by the OS. In order, it: * - Sets up all argc entries as slices diff --git a/libstd/start-linux.s b/rt/start-linux.s index 48d37d8..717d00f 100644 --- a/libstd/start-linux.s +++ b/rt/start-linux.s @@ -13,62 +13,6 @@ std$__cenvp: .text /* - * counts the length of the string pointed to - * by %r8, returning len in %r9. Does not modify - * any registers outside of %r9 - */ -cstrlen: - xorq %r9,%r9 - jmp .lentest - - .lenloop: - incq %r9 - .lentest: - cmpb $0,(%r8,%r9) - jne .lenloop - ret - - -/* - * Counts the size of the null terminated string vector - * pointed to by %rbx. Clobbers %r10,%r11 - */ -count: - xorq %r9,%r9 - movq %rbx,%r11 -.countloop: - movq (%r11),%r10 - testq %r10,%r10 - jz .countdone - addq $1,%r9 - addq $8,%r11 - jmp .countloop -.countdone: - ret - -/* - * iterate over the strings for argc, and put - * them into the args array. - * - * argc in %rax, argv in %rbx, dest vector in %rcx - */ -cvt: - jmp .cvttest -.cvtloop: - subq $1,%rax - movq (%rbx),%r8 - call cstrlen - movq %r8, (%rcx) - movq %r9, 8(%rcx) - addq $8, %rbx - addq $16, %rcx -.cvttest: - testq %rax,%rax - jnz .cvtloop -.cvtdone: - ret - -/* * The entry point for the whole program. * This is called by the OS. In order, it: * - Sets up all argc entries as slices @@ -92,6 +36,15 @@ _start: 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 */ diff --git a/libstd/start-osx.s b/rt/start-osx.s index 27e3316..04bdf43 100644 --- a/libstd/start-osx.s +++ b/rt/start-osx.s @@ -13,62 +13,6 @@ _std$__cenvp: .text /* - * counts the length of the string pointed to - * by %r8, returning len in %r9. Does not modify - * any registers outside of %r9 - */ -cstrlen: - xorq %r9,%r9 - jmp .lentest - - .lenloop: - incq %r9 - .lentest: - cmpb $0,(%r8,%r9) - jne .lenloop - ret - - -/* - * Counts the size of the null terminated string vector - * pointed to by %rbx. Clobbers %r10,%r11 - */ -count: - xorq %r9,%r9 - movq %rbx,%r11 -.countloop: - movq (%r11),%r10 - testq %r10,%r10 - jz .countdone - addq $1,%r9 - addq $8,%r11 - jmp .countloop -.countdone: - ret - -/* - * iterate over the strings for argc, and put - * them into the args array. - * - * argc in %rax, argv in %rbx, dest vector in %rcx - */ -cvt: - jmp .cvttest -.cvtloop: - subq $1,%rax - movq (%rbx),%r8 - call cstrlen - movq %r8, (%rcx) - movq %r9, 8(%rcx) - addq $8, %rbx - addq $16, %rcx -.cvttest: - testq %rax,%rax - jnz .cvtloop -.cvtdone: - ret - -/* * The entry point for the whole program. * This is called by the OS. In order, it: * - Sets up all argc entries as slices diff --git a/test/runtest.sh b/test/runtest.sh index 420ad41..8ec0a63 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -16,7 +16,7 @@ function use { function build { rm -f $1 $1.o $1.s $1.use - ../myrbuild/myrbuild -b $1 -C../6/6m -M../muse/muse -I../libstd $1.myr + ../myrbuild/myrbuild -b $1 -C../6/6m -M../muse/muse -I../libstd -I../rt $1.myr } function pass { |