From 6ff627711025af0bb58842228f1800bd08d28bd3 Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Sat, 18 Apr 2015 13:39:27 +0000
Subject: [PATCH] Updated zipr callbacks to build libc for x86-64.

---
 .gitattributes         |  4 ++++
 configure              | 14 +++++++++++---
 configure.in           | 14 +++++++++++---
 configure_for_rss      |  4 ++++
 libc/include/syscall.h |  6 +++++-
 libc/src/Makefile.in   | 23 +++++++++++++++--------
 libc/src/malloc.c      |  3 ++-
 libc/src/syscall.s     | 15 +++++++++++++++
 rss/Makefile.in        | 28 ++++++++++++++++++++++++++++
 rss/rss_callbacks.c    | 33 +++++++++++++++++++++++++++++++++
 10 files changed, 128 insertions(+), 16 deletions(-)
 create mode 100755 configure_for_rss
 create mode 100644 libc/src/syscall.s
 create mode 100644 rss/Makefile.in
 create mode 100644 rss/rss_callbacks.c

diff --git a/.gitattributes b/.gitattributes
index 2700a80..b41843a 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -4,6 +4,7 @@
 /configure.in -text
 /configure_for_cgc -text
 /configure_for_cinderella -text
+/configure_for_rss -text
 example/Makefile.in -text
 example/callback.c -text
 inferfn/Makefile.in -text
@@ -33,9 +34,12 @@ libc/src/itoa.c -text
 libc/src/malloc.c -text
 libc/src/read.c -text
 libc/src/strlen.c -text
+libc/src/syscall.s -text
 libc/src/write.c -text
 p1/Makefile.in -text
 p1/p1callbacks.c -text
+rss/Makefile.in -text
+rss/rss_callbacks.c -text
 watchsyscall/Makefile.in -text
 watchsyscall/cgc.s -text
 watchsyscall/wa_callbacks.c -text
diff --git a/configure b/configure
index 48a7aa0..aa46b1e 100755
--- a/configure
+++ b/configure
@@ -4908,13 +4908,20 @@ CFLAGS="$OPTIMIZE -nostdinc -fPIE -fPIC -I$ZIPR_CALLBACKS/libc/include  -fno-sta
 LD=gcc
 LDFLAGS="-r -nostdlib -fPIC -fPIE"
 AS=nasm
-ASFLAGS=""
-
+if test $(uname -p) = "x86_64" ; then
+	echo "Using 64-bit mode"
+	ASFLAGS="-felf64"
+	CFLAGS="$CFLAGS -DLINUX64"
+else
+	echo "Using 32-bit mode"
+	ASFLAGS="-felf"
+	CFLAGS="$CFLAGS -DLINUX32"
+fi
 
 if test "$enable_cgc" = yes; then
 	echo Enabling CGC build.
 	CFLAGS="$CFLAGS -DCGC"
-	ASFLAGS="$ASFLAGS -DCGC -felf"
+	ASFLAGS="$ASFLAGS -DCGC"
 fi
 
 # output lib's makefile, which is always needed.
@@ -6084,6 +6091,7 @@ enable_dir example
 enable_dir watchsyscall
 enable_dir p1
 enable_dir inferfn
+enable_dir rss
 
 ac_config_files="$ac_config_files Makefile"
 
diff --git a/configure.in b/configure.in
index bd8b5f0..63faf14 100644
--- a/configure.in
+++ b/configure.in
@@ -44,13 +44,20 @@ CFLAGS="$OPTIMIZE -nostdinc -fPIE -fPIC -I$ZIPR_CALLBACKS/libc/include  -fno-sta
 LD=gcc
 LDFLAGS="-r -nostdlib -fPIC -fPIE"
 AS=nasm
-ASFLAGS=""
-
+if test $(uname -p) = "x86_64" ; then
+	echo "Using 64-bit mode"
+	ASFLAGS="-felf64"
+	CFLAGS="$CFLAGS -DLINUX64"
+else
+	echo "Using 32-bit mode"
+	ASFLAGS="-felf"
+	CFLAGS="$CFLAGS -DLINUX32"
+fi
 
 if test "$enable_cgc" = yes; then
 	echo Enabling CGC build.
 	CFLAGS="$CFLAGS -DCGC"
-	ASFLAGS="$ASFLAGS -DCGC -felf"
+	ASFLAGS="$ASFLAGS -DCGC"
 fi
 
 # output lib's makefile, which is always needed.
@@ -62,6 +69,7 @@ enable_dir example
 enable_dir watchsyscall
 enable_dir p1
 enable_dir inferfn
+enable_dir rss
 
 AC_OUTPUT(Makefile)
 
diff --git a/configure_for_rss b/configure_for_rss
new file mode 100755
index 0000000..159ad5b
--- /dev/null
+++ b/configure_for_rss
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+# pickup the CGC syscalls
+./configure --enable-rss --enable-libc --prefix=$ZIPR_INSTALL
diff --git a/libc/include/syscall.h b/libc/include/syscall.h
index 4320935..1fdd0a7 100644
--- a/libc/include/syscall.h
+++ b/libc/include/syscall.h
@@ -18,6 +18,10 @@
  *
  */
 
-
+#ifdef LINUX64
+#define SYS_read 0
+#define SYS_write 1
+#else
 #define SYS_read 3
 #define SYS_write 4
+#endif
diff --git a/libc/src/Makefile.in b/libc/src/Makefile.in
index 6c66c3d..02d6a60 100644
--- a/libc/src/Makefile.in
+++ b/libc/src/Makefile.in
@@ -7,22 +7,29 @@ AR=@AR@
 AS=@AS@
 ASFLAGS=@ASFLAGS@
 
-OBJS=cgc.o malloc.o read.o write.o itoa.o strlen.o
+OBJS=malloc.o read.o write.o itoa.o strlen.o cgc.o
+
+# note:  cgc.s is empty when -DCGC is not in ASFLAGS, so it's an empty object file that's created..
 
 
 .SUFFIXES: .o .s .c
 
+all: 	$(OBJS)
+	$(AR) -r $(LIB) $(OBJS)
+
+clean:
+	rm -f *.o
+	
+
 .c.o:
 	$(CC) $(CFLAGS) $(INCLUDE) -D__$(ARCH) -c $<
 
-.s.o:
-	$(AS) $(ASFLAGS) $<
 
+# bad hack for 32-bit vrs 64-bit compatibility for linux and CGC/decree.
+cgc.o:  cgc.s syscall.s
+	if [ `uname -p` = "x86_64" ]; then $(AS) $(ASFLAGS) syscall.s -o $@; else  $(AS) $(ASFLAGS) cgc.s -o $@; fi
 
-all: 	$(OBJS)
-	$(AR) -r $(LIB) $(OBJS)
+.s.o:
+	$(AS) $(ASFLAGS) $<
 
-clean:
-	rm *.o
-	
 
diff --git a/libc/src/malloc.c b/libc/src/malloc.c
index 637aa11..8059409 100644
--- a/libc/src/malloc.c
+++ b/libc/src/malloc.c
@@ -20,6 +20,7 @@
 
 #include "stdint.h"
 #include "stdlib.h"
+#include <assert.h>
 
 void *malloc(size_t size)
 {
@@ -39,8 +40,8 @@ void *malloc(size_t size)
 void free(void* ptr)
 {
 	ptr=ptr-sizeof(size_t);
-	cgc_deallocate(ptr,*(size_t*)ptr);
 #ifdef CGC
+	cgc_deallocate(ptr,*(size_t*)ptr);
 #else
 	assert(0);
 #endif
diff --git a/libc/src/syscall.s b/libc/src/syscall.s
new file mode 100644
index 0000000..711a1a9
--- /dev/null
+++ b/libc/src/syscall.s
@@ -0,0 +1,15 @@
+BITS 64
+
+; probably broken, needs porting.
+global syscall
+syscall:
+	mov    rax, rdi
+	mov    rdi, rsi
+	mov    rsi, rdx
+	mov    rdx, rcx
+	mov    r10, r8
+	mov    r8, r9
+	mov    r9, QWORD [rsp+0x8]
+	syscall 
+	ret    
+
diff --git a/rss/Makefile.in b/rss/Makefile.in
new file mode 100644
index 0000000..bcea64e
--- /dev/null
+++ b/rss/Makefile.in
@@ -0,0 +1,28 @@
+
+
+CC=@CC@
+CFLAGS=@CFLAGS@
+LIB=@LIB@
+AR=@AR@
+AS=@AS@
+ASFLAGS=@ASFLAGS@
+
+OBJS=rss_callbacks.o
+
+
+.SUFFIXES: .o .s .c
+
+.c.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -D__$(ARCH) -c $<
+
+.s.o:
+	$(AS) $(ASFLAGS) $<
+
+
+all: 	$(OBJS)
+	$(AR) -r $(LIB) $(OBJS)
+
+clean:
+	rm *.o
+	
+
diff --git a/rss/rss_callbacks.c b/rss/rss_callbacks.c
new file mode 100644
index 0000000..07770bd
--- /dev/null
+++ b/rss/rss_callbacks.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#include <stdint.h>
+
+void zipr_push_stack(uintptr_t ret)
+{
+	char str[]="zipr_push_callback\n";
+	write(1,str,sizeof(str));
+}
+
+void zipr_pop_stack(uintptr_t ret)
+{
+	char str[]="zipr_pop_callback\n";
+	write(1,str,sizeof(str));
+}
-- 
GitLab