In order to minimize copying musl source files that invoke syscall()
into libc/ folder, we employ following scheme that works in simple cases:
single .c files uses single syscall type.

The scheme involves new header file - libc/syscall_to_function.h -
that defines simple macros specific to each source file
that override syscall() or __syscall() function call.

So instead of copying and modifying musl file,
we instead compile with --include option pointing to the
new syscall_to_function.h header and passing extra
preprocessor parameter that specifies specic syscall type.
The former is done to avoid collisions of same macro name
definitions in single header file.

Please nore it is RFC so this patch for example does
not delete files from libc/ folder as the final patch should.

Please also note commented out generic macro attempt
that does not work as macro definition can not have
nested preprocessor directives (preprocessor runs once).

Signed-off-by: Waldemar Kozaczuk <jwkozac...@gmail.com>
---
 Makefile                   | 10 +++++++---
 libc/aliases.ld            |  3 +++
 libc/syscall_to_function.h | 24 ++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 3 deletions(-)
 create mode 100644 libc/syscall_to_function.h

diff --git a/Makefile b/Makefile
index 78590d40..f6eb21e0 100644
--- a/Makefile
+++ b/Makefile
@@ -1341,6 +1341,7 @@ musl += multibyte/wcstombs.o
 musl += multibyte/wctob.o
 musl += multibyte/wctomb.o
 
+#SHOULD it go away?
 $(out)/libc/multibyte/mbsrtowcs.o: CFLAGS += -Imusl/src/multibyte
 
 musl += network/htonl.o
@@ -1417,10 +1418,12 @@ libc += stdio/__fopen_rb_ca.o
 libc += stdio/__fprintf_chk.o
 libc += stdio/__lockfile.o
 musl += stdio/__overflow.o
-libc += stdio/__stdio_close.o
+musl += stdio/__stdio_close.o
+$(out)/musl/src/stdio/__stdio_close.o: CFLAGS += -D__OSV_SYS_close_TO_close 
--include libc/syscall_to_function.h
 musl += stdio/__stdio_exit.o
 libc += stdio/__stdio_read.o
-libc += stdio/__stdio_seek.o
+musl += stdio/__stdio_seek.o
+$(out)/musl/src/stdio/__stdio_seek.o: CFLAGS += -D__OSV_SYS_lseek_TO_lseek 
--include libc/syscall_to_function.h
 libc += stdio/__stdio_write.o
 libc += stdio/__stdout_write.o
 musl += stdio/__string_read.o
@@ -1454,7 +1457,8 @@ musl += stdio/fputwc.o
 musl += stdio/fputws.o
 musl += stdio/fread.o
 libc += stdio/__fread_chk.o
-libc += stdio/freopen.o
+musl += stdio/freopen.o
+$(out)/musl/src/stdio/freopen.o: CFLAGS += -D__OSV_SYS_fcntl_TO_fcntl 
--include libc/syscall_to_function.h
 musl += stdio/fscanf.o
 musl += stdio/fseek.o
 musl += stdio/fsetpos.o
diff --git a/libc/aliases.ld b/libc/aliases.ld
index a44b7572..ba5e87ff 100644
--- a/libc/aliases.ld
+++ b/libc/aliases.ld
@@ -23,3 +23,6 @@ __pow_finite = pow;
 __finite = finite;
 __finitef = finitef;
 __finitel = finitel;
+
+/* unistd */
+__dup3 = dup3;
diff --git a/libc/syscall_to_function.h b/libc/syscall_to_function.h
new file mode 100644
index 00000000..68922cbf
--- /dev/null
+++ b/libc/syscall_to_function.h
@@ -0,0 +1,24 @@
+#include <bits/syscall.h>
+#include <unistd.h>
+
+#ifdef __OSV_SYS_close_TO_close
+#define syscall(syscall_number, fd) close(fd)
+#endif
+
+#ifdef __OSV_SYS_lseek_TO_lseek
+#define syscall(syscall_number, file, off, whence) lseek(file, off, whence)
+#endif
+
+#ifdef __OSV_SYS_fcntl_TO_fcntl
+#define syscall(syscall_number, fd, cmd, args) fcntl(fd, cmd, args)
+#define __syscall(syscall_number, fd, cmd, args) fcntl(fd, cmd, args)
+#endif
+
+/*
+#define syscall(syscall_number, ...) \
+  #if syscall_number == SYS_close \
+    close(___VA_ARGS__) \
+  #else \
+    #error Unsupported syscall number! \
+  #endif
+*/
-- 
2.25.1

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/20200810050225.13067-1-jwkozaczuk%40gmail.com.

Reply via email to