commit 68547e656cc3bfd615d84911dca5a8ecd2c03845 parent 0c50c9b1b8f124d83ab152ad0240b801f992b370 Author: Dimitris Papastamos <dimitris.papastamos@arm.com> Date: Fri, 7 Dec 2018 15:11:00 +0000 Initial darwin port Change-Id: Ie16c3b820392fc9e97cef25f51115e4bca21e020 Signed-off-by: Dimitris Papastamos <dimitris.papastamos@arm.com> Diffstat:
23 files changed, 295 insertions(+), 6 deletions(-)
diff --git a/README b/README @@ -5,6 +5,12 @@ PATH=<path-to-aarch64-elf-toolchain>:$PATH export PATH CROSS_COMPILE=aarch64-elf- make +Building on macOS +================= + +sudo /usr/sbin/DevToolsSecurity --enable +make -e CONF=amd64-darwin-clang + Running rcode ============= diff --git a/config/amd64-darwin-clang.mk b/config/amd64-darwin-clang.mk @@ -0,0 +1,12 @@ +ARCH = amd64 +SYS = darwin +ABI = amd64-darwin +MODE = hosted +SYSASFLAGS = -fintegrated-as -c +SYSCFLAGS = -g -static -nostdinc -ffreestanding -std=c99 -fno-stack-protector -MD -Wall -mno-sse +SYSLDFLAGS = -static +RANLIBFLAGS = -c +COMP = clang +ASM = clang +LINKER = ld +OBJDUMP = objdump diff --git a/include/bits/darwin/sys.h b/include/bits/darwin/sys.h @@ -0,0 +1,19 @@ +#define O_RDONLY 0x00000000 +#define O_WRONLY 0x00000001 +#define O_RDWR 0x00000002 + +#define O_TRUNC 0x00000400 +#define O_APPEND 0x00000008 +#define O_CREAT 0x00000200 + +typedef int pid_t; + +struct sigaction { + void (*sa_handler)(int); + int sa_mask; + int sa_flags; +}; + +extern pid_t _getpid(void); +extern int _kill(pid_t pid, int signum); +extern int _sigaction(int sig, struct sigaction *new, struct sigaction *old); diff --git a/include/bits/darwin/sys/errno.h b/include/bits/darwin/sys/errno.h @@ -0,0 +1,111 @@ +extern int errno; +extern char *const _sys_errlist[]; +extern const int _sys_nerr; + +#define EPERM 1 +#define ENOENT 2 +#define ESRCH 3 +#define EINTR 4 +#define EIO 5 +#define ENXIO 6 +#define E2BIG 7 +#define ENOEXEC 8 +#define EBADF 9 +#define ECHILD 10 +#define EDEADLK 11 +#define ENOMEM 12 +#define EACCES 13 +#define EFAULT 14 +#define ENOTBLK 15 +#define EBUSY 16 +#define EEXIST 17 +#define EXDEV 18 +#define ENODEV 19 +#define ENOTDIR 20 +#define EISDIR 21 +#define EINVAL 22 +#define ENFILE 23 +#define EMFILE 24 +#define ENOTTY 25 +#define ETXTBSY 26 +#define EFBIG 27 +#define ENOSPC 28 +#define ESPIPE 29 +#define EROFS 30 +#define EMLINK 31 +#define EPIPE 32 +#define EDOM 33 +#define ERANGE 34 +#define EAGAIN 35 +#define EINPROGRESS 36 +#define EALREADY 37 +#define ENOTSOCK 38 +#define EDESTADDRREQ 39 +#define EMSGSIZE 40 +#define EPROTOTYPE 41 +#define ENOPROTOOPT 42 +#define EPROTONOSUPPORT 43 +#define ESOCKTNOSUPPORT 44 +#define ENOTSUP 45 +#define EPFNOSUPPORT 46 +#define EAFNOSUPPORT 47 +#define EADDRINUSE 48 +#define EADDRNOTAVAIL 49 +#define ENETDOWN 50 +#define ENETUNREACH 51 +#define ENETRESET 52 +#define ECONNABORTED 53 +#define ECONNRESET 54 +#define ENOBUFS 55 +#define EISCONN 56 +#define ENOTCONN 57 +#define ESHUTDOWN 58 +#define ETOOMANYREFS 59 +#define ETIMEDOUT 60 +#define ECONNREFUSED 61 +#define ELOOP 62 +#define ENAMETOOLONG 63 +#define EHOSTDOWN 64 +#define EHOSTUNREACH 65 +#define ENOTEMPTY 66 +#define EPROCLIM 67 +#define EUSERS 68 +#define EDQUOT 69 +#define ESTALE 70 +#define EREMOTE 71 +#define EBADRPC 72 +#define ERPCMISMATCH 73 +#define EPROGUNAVAIL 74 +#define EPROGMISMATCH 75 +#define EPROCUNAVAIL 76 +#define ENOLCK 77 +#define ENOSYS 78 +#define EFTYPE 79 +#define EAUTH 80 +#define ENEEDAUTH 81 +#define EPWROFF 82 +#define EDEVERR 83 +#define EOVERFLOW 84 +#define EBADEXEC 85 +#define EBADARCH 86 +#define ESHLIBVERS 87 +#define EBADMACHO 88 +#define ECANCELED 89 +#define EIDRM 90 +#define ENOMSG 91 +#define EILSEQ 92 +#define ENOATTR 93 +#define EBADMSG 94 +#define EMULTIHOP 95 +#define ENODATA 96 +#define ENOLINK 97 +#define ENOSR 98 +#define ENOSTR 99 +#define EPROTO 100 +#define ETIME 101 +#define EOPNOTSUPP 102 +#define ENOPOLICY 103 +#define ENOTRECOVERABLE 104 +#define EOWNERDEAD 105 +#define EQFULL 106 +#define EUNKNOWN 107 diff --git a/include/bits/darwin/sys/signal.h b/include/bits/darwin/sys/signal.h @@ -0,0 +1,27 @@ +typedef int sig_atomic_t; + +#define SIG_ERR ((void (*)(int))-1) +#define SIG_DFL ((void (*)(int)) 0) +#define SIG_IGN ((void (*)(int)) 1) + +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGABRT 6 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGSEGV 11 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGSTOP 17 +#define SIGTSTP 18 +#define SIGCONT 19 +#define SIGCHLD 20 +#define SIGTTIN 21 +#define SIGTTOU 22 +#define SIGUSR1 30 +#define SIGUSR2 31 + +#define __NR_SIGNALS 32 diff --git a/scripts/rules.mk b/scripts/rules.mk @@ -1,4 +1,4 @@ -CONF = rmode-rcode +CONF = rmode-rcode include $(PROJECTDIR)/config/$(CONF).mk INCDIR = $(PROJECTDIR)/include diff --git a/src/libc/Makefile b/src/libc/Makefile @@ -23,6 +23,7 @@ objlst: FORCE $(TARGET): objlst xargs $(AR) $(ARFLAGS) $@ < objlst + ranlib $(RANLIBFLAGS) $@ clean: $(FORALL) diff --git a/src/libc/arch/amd64/Makefile b/src/libc/arch/amd64/Makefile @@ -3,7 +3,7 @@ PROJECTDIR =../../../.. include $(PROJECTDIR)/scripts/rules.mk OBJS = longjmp.o setjmp.o -DIRS = netbsd openbsd dragonfly linux +DIRS = netbsd openbsd dragonfly linux darwin all: $(OBJS) $(SYS) diff --git a/src/libc/arch/amd64/darwin/Makefile b/src/libc/arch/amd64/darwin/Makefile @@ -0,0 +1,34 @@ +.POSIX: +PROJECTDIR =../../../../.. +include $(PROJECTDIR)/scripts/rules.mk +include ../../rules.mk + +OBJS = __Exit.o \ + __close.o \ + __getpid.o \ + __kill.o \ + __lseek.o \ + __open.o \ + __read.o \ + __write.o \ + __sigaction.o\ + _getheap.o \ + _tzone.o \ + getenv.o \ + raise.o \ + signal.o \ + time.o \ + _sys_errlist.o \ + +all: syscall + $(MAKE) objs + +objs: $(OBJS) + +syscall: syscall.lst + ./gensys.sh syscall.lst + touch syscall + +clean: + rm -f `awk '$$2 ~ /^__/ {print $$2".s"}' syscall.lst` + rm -f syscall _sys_errlist.c diff --git a/src/libc/arch/amd64/darwin/_getheap.c b/src/libc/arch/amd64/darwin/_getheap.c @@ -0,0 +1,18 @@ +static char heap[16384]; + +void * +_getheap(void) +{ + return heap; +} + +void * +_brk(void *addr) +{ + static char *cur = heap; + char *p = addr; + + if (p < heap || p > &heap[sizeof(heap) - 1]) + return (void *)-1; + return cur = p; +} diff --git a/src/libc/arch/amd64/darwin/_tzone.c b/src/libc/arch/amd64/darwin/_tzone.c @@ -0,0 +1 @@ +#include "../../posix/_tzone.c" diff --git a/src/libc/arch/amd64/darwin/gensys.sh b/src/libc/arch/amd64/darwin/gensys.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# +# This job is very easy because app and kernel ABI are identical +# until the 4th parameter, so we only have to set the syscall +# number in rax + +awk 'NF == 2 {printf("0x%x\t%s\n", 33554432 + $1, $2)}' syscall.lst | +while read num name +do +cat <<EOF > $name.s + .file "$name.s" + + .globl $name +$name: + movq \$$num,%rax + syscall + jb 1f + retq + +1: movq %rax,_errno(%rip) + movq \$-1,%rax + retq +EOF +done diff --git a/src/libc/arch/amd64/darwin/getenv.c b/src/libc/arch/amd64/darwin/getenv.c @@ -0,0 +1 @@ +#include "../../posix/getenv.c" diff --git a/src/libc/arch/amd64/darwin/raise.c b/src/libc/arch/amd64/darwin/raise.c @@ -0,0 +1 @@ +#include "../../posix/raise.c" diff --git a/src/libc/arch/amd64/darwin/signal.c b/src/libc/arch/amd64/darwin/signal.c @@ -0,0 +1 @@ +#include "../../posix/signal.c" diff --git a/src/libc/arch/amd64/darwin/syscall.lst b/src/libc/arch/amd64/darwin/syscall.lst @@ -0,0 +1,9 @@ +1 __Exit +3 __read +4 __write +5 __open +6 __close +20 __getpid +46 __sigaction +37 __kill +199 __lseek diff --git a/src/libc/arch/amd64/darwin/time.c b/src/libc/arch/amd64/darwin/time.c @@ -0,0 +1 @@ +#include "../../posix/time.c" diff --git a/src/libc/arch/amd64/longjmp.s b/src/libc/arch/amd64/longjmp.s @@ -1,7 +1,8 @@ .file "longjmp" .text - .globl longjmp + .globl longjmp,_longjmp +_longjmp: longjmp: mov %rsi,%rax test %rax,%rax diff --git a/src/libc/arch/amd64/setjmp.s b/src/libc/arch/amd64/setjmp.s @@ -1,7 +1,8 @@ .file "setjmp.s" .text - .globl setjmp + .globl setjmp,_setjmp +_setjmp: setjmp: mov %rbx,(%rdi) mov %rbp,8(%rdi) diff --git a/src/libc/stdlib/malloc.c b/src/libc/stdlib/malloc.c @@ -81,7 +81,7 @@ sbrk(uintptr_t inc) return ERRADDR; new = old + inc; p = _brk(new); - if (p == old || p < 0) + if (p == old || p == ERRADDR) return ERRADDR; heap = new; diff --git a/src/libc/stdlib/malloc.h b/src/libc/stdlib/malloc.h @@ -1,7 +1,7 @@ #include <stdlib.h> /* minimum amount of required units */ -#define NALLOC 10000 +#define NALLOC 16 typedef union header Header; union header { diff --git a/target/hosted/crt/crt-amd64-darwin.s b/target/hosted/crt/crt-amd64-darwin.s @@ -0,0 +1,19 @@ + .bss + .globl __environ +__environ: + .quad 0 + + .text + .global start +start: + movq %rsp,%rbp + + /* load argc, argv, envp from stack */ + movq (%rbp),%rdi /* argc */ + leaq 8(%rbp),%rsi /* argv */ + leaq 16(%rbp,%rdi,8),%rdx /* envp = argv + 8*argc + 8 */ + movq %rdx,__environ(%rip) + + call _main + movl %eax,%edi + jmp _exit diff --git a/target/hosted/crt/crt-darwin.s b/target/hosted/crt/crt-darwin.s @@ -0,0 +1 @@ + .file "crt-darwin.s"