diff --git a/libmac/libc/darwin_errno_codes.h b/libmac/libc/darwin_errno_codes.h new file mode 100644 index 000000000..2d18724e0 --- /dev/null +++ b/libmac/libc/darwin_errno_codes.h @@ -0,0 +1,115 @@ +#ifndef DARWIN_ERRNO_CODES_H +#define DARWIN_ERRNO_CODES_H + +/* $ awk '/#define[ \t]([A-Z]+)[\t ]+([0-9]+)/ { print "#define DARWIN_" $2 " " $3; }' < errno.h */ + +#define DARWIN_EPERM 1 +#define DARWIN_ENOENT 2 +#define DARWIN_ESRCH 3 +#define DARWIN_EINTR 4 +#define DARWIN_EIO 5 +#define DARWIN_ENXIO 6 +#define DARWIN_ENOEXEC 8 +#define DARWIN_EBADF 9 +#define DARWIN_ECHILD 10 +#define DARWIN_EDEADLK 11 +#define DARWIN_ENOMEM 12 +#define DARWIN_EACCES 13 +#define DARWIN_EFAULT 14 +#define DARWIN_ENOTBLK 15 +#define DARWIN_EBUSY 16 +#define DARWIN_EEXIST 17 +#define DARWIN_EXDEV 18 +#define DARWIN_ENODEV 19 +#define DARWIN_ENOTDIR 20 +#define DARWIN_EISDIR 21 +#define DARWIN_EINVAL 22 +#define DARWIN_ENFILE 23 +#define DARWIN_EMFILE 24 +#define DARWIN_ENOTTY 25 +#define DARWIN_ETXTBSY 26 +#define DARWIN_EFBIG 27 +#define DARWIN_ENOSPC 28 +#define DARWIN_ESPIPE 29 +#define DARWIN_EROFS 30 +#define DARWIN_EMLINK 31 +#define DARWIN_EPIPE 32 +#define DARWIN_EDOM 33 +#define DARWIN_ERANGE 34 +#define DARWIN_EAGAIN 35 +#define DARWIN_EINPROGRESS 36 +#define DARWIN_EALREADY 37 +#define DARWIN_ENOTSOCK 38 +#define DARWIN_EDESTADDRREQ 39 +#define DARWIN_EMSGSIZE 40 +#define DARWIN_EPROTOTYPE 41 +#define DARWIN_ENOPROTOOPT 42 +#define DARWIN_EPROTONOSUPPORT 43 +#define DARWIN_ESOCKTNOSUPPORT 44 +#define DARWIN_ENOTSUP 45 +#define DARWIN_EPFNOSUPPORT 46 +#define DARWIN_EAFNOSUPPORT 47 +#define DARWIN_EADDRINUSE 48 +#define DARWIN_EADDRNOTAVAIL 49 +#define DARWIN_ENETDOWN 50 +#define DARWIN_ENETUNREACH 51 +#define DARWIN_ENETRESET 52 +#define DARWIN_ECONNABORTED 53 +#define DARWIN_ECONNRESET 54 +#define DARWIN_ENOBUFS 55 +#define DARWIN_EISCONN 56 +#define DARWIN_ENOTCONN 57 +#define DARWIN_ESHUTDOWN 58 +#define DARWIN_ETOOMANYREFS 59 +#define DARWIN_ETIMEDOUT 60 +#define DARWIN_ECONNREFUSED 61 +#define DARWIN_ELOOP 62 +#define DARWIN_ENAMETOOLONG 63 +#define DARWIN_EHOSTDOWN 64 +#define DARWIN_EHOSTUNREACH 65 +#define DARWIN_ENOTEMPTY 66 +#define DARWIN_EPROCLIM 67 +#define DARWIN_EUSERS 68 +#define DARWIN_EDQUOT 69 +#define DARWIN_ESTALE 70 +#define DARWIN_EREMOTE 71 +#define DARWIN_EBADRPC 72 +#define DARWIN_ERPCMISMATCH 73 +#define _DARWIN_EPROGUNAVAIL 74 +#define _DARWIN_EPROGMISMATCH 75 +#define _DARWIN_EPROCUNAVAIL 76 +#define DARWIN_ENOLCK 77 +#define DARWIN_ENOSYS 78 +#define _DARWIN_EFTYPE 79 +#define _DARWIN_EAUTH 80 +#define _DARWIN_ENEEDAUTH 81 +#define _DARWIN_EPWROFF 82 +#define _DARWIN_EDEVERR 83 +#define DARWIN_EOVERFLOW 84 +#define _DARWIN_EBADEXEC 85 +#define _DARWIN_EBADARCH 86 +#define _DARWIN_ESHLIBVERS 87 +#define _DARWIN_EBADMACHO 88 +#define DARWIN_ECANCELED 89 +#define DARWIN_EIDRM 90 +#define DARWIN_ENOMSG 91 +#define DARWIN_EILSEQ 92 +#define _DARWIN_ENOATTR 93 +#define DARWIN_EBADMSG 94 +#define DARWIN_EMULTIHOP 95 +#define DARWIN_ENODATA 96 +#define DARWIN_ENOLINK 97 +#define DARWIN_ENOSR 98 +#define DARWIN_ENOSTR 99 +#define DARWIN_EPROTO 100 +#define DARWIN_ETIME 101 +#define DARWIN_EOPNOTSUPP 102 +#define _DARWIN_ENOPOLICY 103 +#define DARWIN_ENOTRECOVERABLE 104 +#define DARWIN_EOWNERDEAD 105 +#define _DARWIN_EQFULL 106 +#define _DARWIN_ELAST 106 +#define _DARWIN_ECVCERORR 256 +#define _DARWIN_ECVPERORR 512 + +#endif diff --git a/libmac/libc/directmap.lst b/libmac/libc/directmap.lst new file mode 100644 index 000000000..1f7a5948e --- /dev/null +++ b/libmac/libc/directmap.lst @@ -0,0 +1,2 @@ +__error;__errno_location + diff --git a/libmac/libc/errno.cpp b/libmac/libc/errno.cpp new file mode 100644 index 000000000..6c80d62a1 --- /dev/null +++ b/libmac/libc/errno.cpp @@ -0,0 +1,71 @@ +#define _POSIX_C_SOURCE 200112L +#undef _GNU_SOURCE // for proper strerror_r +#include +#include +#include +#include "errno.h" +#include "darwin_errno_codes.h" + +static void initErrnoMappingTable() __attribute__ ((constructor)); +static int darwin_to_linux[130]; +static int linux_to_darwin[130]; + +// This is deprecated, we don't bother to support that +extern "C" const char * const __darwin_sys_errlist[0]; +extern "C" const int __darwin_sys_nerr = 0; + +int cthread_errno() +{ + return errno; +} + +void initErrnoMappingTable() +{ + ::memset(darwin_to_linux, 0, sizeof(darwin_to_linux)); + ::memset(linux_to_darwin, 0, sizeof(linux_to_darwin)); + +# include "darwin_to_linux.map" +# include "linux_to_darwin.map" + + darwin_to_linux[_DARWIN_EBADEXEC] = ENOEXEC; + darwin_to_linux[_DARWIN_EBADARCH] = ENOEXEC; + darwin_to_linux[_DARWIN_EBADMACHO] = ENOEXEC; +} + +int errnoDarwinToLinux(int err) +{ + if (err < sizeof(darwin_to_linux)/sizeof(darwin_to_linux[0])-1) + return darwin_to_linux[err]; + else + return 0; +} + +int errnoLinuxToDarwin(int err) +{ + if (err < sizeof(linux_to_darwin)/sizeof(linux_to_darwin[0])-1) + return linux_to_darwin[err]; + else + return 0; +} + +char* __darwin_strerror(int errnum) +{ + errnum = errnoDarwinToLinux(errnum); + return ::strerror(errnum); +} + +int __darwin_strerror_r(int errnum, char *strerrbuf, size_t buflen) +{ + errnum = errnoDarwinToLinux(errnum); + return ::strerror_r(errnum, strerrbuf, buflen); +} + +void __darwin_perror(const char *s) +{ + // First map the error back + errno = errnoDarwinToLinux(errno); + ::perror(s); + // Now map it back again + errno = errnoLinuxToDarwin(errno); +} + diff --git a/libmac/libc/errno.h b/libmac/libc/errno.h new file mode 100644 index 000000000..aee18b565 --- /dev/null +++ b/libmac/libc/errno.h @@ -0,0 +1,16 @@ +#ifndef DARWIN_ERRNO_H +#define DARWIN_ERRNO_H + +extern "C" +{ + // int *__error(); /* generated code */ + int cthread_errno(); + + char* __darwin_strerror(int errnum); + int __darwin_strerror_r(int errnum, char *strerrbuf, size_t buflen); +} + +int errnoDarwinToLinux(int err); +int errnoLinuxToDarwin(int err); + +#endif diff --git a/tools/gendirectmapassembly b/tools/gendirectmapassembly new file mode 100755 index 000000000..5264d1689 --- /dev/null +++ b/tools/gendirectmapassembly @@ -0,0 +1,23 @@ +#!/bin/bash + +# Generates a direct mapping between two functions in assembly code + +( +echo "section .text" +echo + +while read line; do + arrIN=(${line//;/ }) + + if [ ${#arrIN[@]} -ne 2 ]; then + continue + fi + + echo "global ${arrIN[0]}" + echo "extern ${arrIN[1]}" + echo "${arrIN[0]}:" + echo " jmp ${arrIN[1]}" + echo +done < "$1" +) > "${1%.lst}.asm" + diff --git a/tools/generrnomap b/tools/generrnomap new file mode 100755 index 000000000..f191ea67b --- /dev/null +++ b/tools/generrnomap @@ -0,0 +1,6 @@ +#!/bin/sh + +awk '/#define DARWIN_([A-Z]+) ([0-9]+)/ { printf("darwin_to_linux[%s] = %s;\n", $2, substr($2, 8)); }' < "$1" > "darwin_to_linux.map" + +awk '/#define DARWIN_([A-Z]+) ([0-9]+)/ { printf("linux_to_darwin[%s] = %s;\n", substr($2, 8), $2); }' < "$1" > "linux_to_darwin.map" +