cinvoke-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cinvoke-svn] r47 - in trunk/cinvoke: . lib lib/arch


From: will
Subject: [cinvoke-svn] r47 - in trunk/cinvoke: . lib lib/arch
Date: 26 Jun 2006 00:11:31 -0400

Author: will
Date: 2006-06-26 00:11:31 -0400 (Mon, 26 Jun 2006)
New Revision: 47

Added:
   trunk/cinvoke/lib/Makefile.x64
   trunk/cinvoke/lib/Makefile.x86
   trunk/cinvoke/lib/arch/gcc_x64_unix.c
   trunk/cinvoke/lib/arch/gcc_x64_unix.h
Removed:
   trunk/cinvoke/lib/Makefile
Modified:
   trunk/cinvoke/Makefile
   trunk/cinvoke/lib/arch/cl_x86_win.h
   trunk/cinvoke/lib/cinvoke-arch.h
   trunk/cinvoke/targets.sh
Log:
start of x64 port

Modified: trunk/cinvoke/Makefile
===================================================================
--- trunk/cinvoke/Makefile      2006-06-25 16:16:18 UTC (rev 46)
+++ trunk/cinvoke/Makefile      2006-06-26 04:11:31 UTC (rev 47)
@@ -4,12 +4,17 @@
        sh targets.sh
 
 x86:
-       cd lib && $(MAKE) BUILDARCH=-DARCH_GCC_X86_UNIX
+       cd lib && $(MAKE) -f Makefile.x86 BUILDARCH=-DARCH_GCC_X86_UNIX
        cd test && $(MAKE) BUILDARCH=-DARCH_GCC_X86_UNIX
+       cp -f lib/arch/gcc_x86_unix.h lib/cinvoke-archspec.h
 
+x64:
+       cd lib && $(MAKE) -f Makefile.x64 BUILDARG=-DARCH_GCC_X64_UNIX
+       cd test && $(MAKE) BUILDARG=-DARCH_GCC_X64_UNIX
+       cp -f lib/arch/gcc_x64_unix.h lib/cinvoke-archspec.h
+
 install:
-       install lib/cinvoke.h lib/cinvoke-arch.h $(PREFIX)/include
-       install lib/arch/gcc_x86_unix.h $(PREFIX)/include/cinvoke-archspec.h
+       install lib/cinvoke.h lib/cinvoke-arch.h lib/cinvoke-archspec.h 
$(PREFIX)/include
        install lib/libcinvoke.a $(PREFIX)/lib
 
 clean:
@@ -18,6 +23,3 @@
 
 test:
        cd test && $(MAKE) test
-
-todo:
-       find . -name '*.c' -or -name '*.h' | xargs grep -n XXX

Deleted: trunk/cinvoke/lib/Makefile
===================================================================
--- trunk/cinvoke/lib/Makefile  2006-06-25 16:16:18 UTC (rev 46)
+++ trunk/cinvoke/lib/Makefile  2006-06-26 04:11:31 UTC (rev 47)
@@ -1,27 +0,0 @@
-TARGET = libcinvoke.a
-
-all: $(TARGET)
-
-clean:
-       rm -f *.o $(TARGET)
-
-SRCS = cinvoke.c structure.c hashtable.c arch/gcc_x86_unix.c
-
-OBJS = $(SRCS:.c=.o)
-
-$(TARGET): $(OBJS)
-       rm -f $(TARGET)
-       ar rs $(TARGET) $(OBJS)
-       ranlib $(TARGET)
-
-.c.o:
-       gcc -g -Wall -Werror $(BUILDARCH) -DCINVOKE_BUILD -c $< -o $@
-
-cinvoke.o: cinvoke.c cinvoke.h cinvoke-arch.h arch/gcc_x86_unix.h \
-  cinvoke-private.h hashtable.h
-structure.o: structure.c cinvoke.h cinvoke-arch.h arch/gcc_x86_unix.h \
-  cinvoke-private.h hashtable.h
-hashtable.o: hashtable.c hashtable.h
-arch/gcc_x86_unix.o: arch/gcc_x86_unix.c arch/../cinvoke.h \
-  arch/../cinvoke-arch.h arch/../arch/gcc_x86_unix.h \
-  arch/../cinvoke-private.h arch/../hashtable.h

Copied: trunk/cinvoke/lib/Makefile.x64 (from rev 46, trunk/cinvoke/lib/Makefile)
===================================================================
--- trunk/cinvoke/lib/Makefile.x64                              (rev 0)
+++ trunk/cinvoke/lib/Makefile.x64      2006-06-26 04:11:31 UTC (rev 47)
@@ -0,0 +1,27 @@
+TARGET = libcinvoke.a
+
+all: $(TARGET)
+
+clean:
+       rm -f *.o $(TARGET)
+
+SRCS = cinvoke.c structure.c hashtable.c arch/gcc_x64_unix.c
+
+OBJS = $(SRCS:.c=.o)
+
+$(TARGET): $(OBJS)
+       rm -f $(TARGET)
+       ar rs $(TARGET) $(OBJS)
+       ranlib $(TARGET)
+
+.c.o:
+       gcc -g -Wall -Werror $(BUILDARCH) -DCINVOKE_BUILD -c $< -o $@
+
+cinvoke.o: cinvoke.c cinvoke.h cinvoke-arch.h arch/gcc_x64_unix.h \
+  cinvoke-private.h hashtable.h
+structure.o: structure.c cinvoke.h cinvoke-arch.h arch/gcc_x64_unix.h \
+  cinvoke-private.h hashtable.h
+hashtable.o: hashtable.c hashtable.h
+arch/gcc_x64_unix.o: arch/gcc_x64_unix.c arch/../cinvoke.h \
+  arch/../cinvoke-arch.h arch/../arch/gcc_x64_unix.h \
+  arch/../cinvoke-private.h arch/../hashtable.h

Copied: trunk/cinvoke/lib/Makefile.x86 (from rev 46, trunk/cinvoke/lib/Makefile)
===================================================================
--- trunk/cinvoke/lib/Makefile.x86                              (rev 0)
+++ trunk/cinvoke/lib/Makefile.x86      2006-06-26 04:11:31 UTC (rev 47)
@@ -0,0 +1,27 @@
+TARGET = libcinvoke.a
+
+all: $(TARGET)
+
+clean:
+       rm -f *.o $(TARGET)
+
+SRCS = cinvoke.c structure.c hashtable.c arch/gcc_x86_unix.c
+
+OBJS = $(SRCS:.c=.o)
+
+$(TARGET): $(OBJS)
+       rm -f $(TARGET)
+       ar rs $(TARGET) $(OBJS)
+       ranlib $(TARGET)
+
+.c.o:
+       gcc -g -Wall -Werror $(BUILDARCH) -DCINVOKE_BUILD -c $< -o $@
+
+cinvoke.o: cinvoke.c cinvoke.h cinvoke-arch.h arch/gcc_x86_unix.h \
+  cinvoke-private.h hashtable.h
+structure.o: structure.c cinvoke.h cinvoke-arch.h arch/gcc_x86_unix.h \
+  cinvoke-private.h hashtable.h
+hashtable.o: hashtable.c hashtable.h
+arch/gcc_x86_unix.o: arch/gcc_x86_unix.c arch/../cinvoke.h \
+  arch/../cinvoke-arch.h arch/../arch/gcc_x86_unix.h \
+  arch/../cinvoke-private.h arch/../hashtable.h

Modified: trunk/cinvoke/lib/arch/cl_x86_win.h
===================================================================
--- trunk/cinvoke/lib/arch/cl_x86_win.h 2006-06-25 16:16:18 UTC (rev 46)
+++ trunk/cinvoke/lib/arch/cl_x86_win.h 2006-06-26 04:11:31 UTC (rev 47)
@@ -57,7 +57,9 @@
 #define CINV_NOMEM_NEEDSFREE 1
 #define CINV_E_INVAL ((cinv_int32_t)ERROR_INVALID_PARAMETER)
 
-#define CINV_CC_DEFAULT CINV_CC_CDECL // XXX find out whats up with 
undecorated stdcall function names in windows system dlls
+// setting this to stdcall makes the win32 api work, but user-compiled
+// dlls break; you can't win
+#define CINV_CC_DEFAULT CINV_CC_CDECL
 #define CINV_T_2BYTE CINV_T_SHORT
 #define CINV_T_4BYTE CINV_T_INT
 #define CINV_T_8BYTE CINV_T_EXTRALONG

Added: trunk/cinvoke/lib/arch/gcc_x64_unix.c
===================================================================
--- trunk/cinvoke/lib/arch/gcc_x64_unix.c                               (rev 0)
+++ trunk/cinvoke/lib/arch/gcc_x64_unix.c       2006-06-26 04:11:31 UTC (rev 47)
@@ -0,0 +1,215 @@
+/*
+C/Invoke Source Code File
+
+Copyright (c) 2006 Will Weisser
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+   1. Redistributions of source code must retain the above copyright notice,
+this list of conditions and the following disclaimer.
+   2. Redistributions in binary form must reproduce the above copyright notice,
+this list of conditions and the following disclaimer in the documentation
+and/or other materials provided with the distribution.
+   3. The name of the author may not be used to endorse or promote products
+derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+*/
+#ifdef CINVOKE_BUILD
+#include "../cinvoke.h"
+#include "../cinvoke-private.h"
+#else
+#include "cinvoke.h"
+#include "cinvoke-private.h"
+#endif
+
+#include <dlfcn.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+void arch_free_errstr(char *str) {}
+
+cinv_status_t arch_library_create(CInvContext *context, const char *path,
+       ArchLibrary *library_out) {
+       void *dl = dlopen(path, RTLD_LAZY);
+       if (!dl) {
+               context_set_error(context, -1, (char*)dlerror(), 0);
+               return CINV_ERROR;
+       }
+               
+       library_out->dl = dl;
+
+       return CINV_SUCCESS;
+}
+cinv_status_t arch_library_get_entrypoint(CInvContext *context,
+       ArchLibrary *library, const char *name, void **entrypoint_out) {
+       void *sym = dlsym(library->dl, name);
+       if (!sym) {
+               context_set_error(context, -1, (char*)dlerror(), 0);
+               return CINV_ERROR;
+       }
+
+       *entrypoint_out = sym;
+
+       return CINV_SUCCESS;
+}
+cinv_status_t arch_library_delete(CInvContext *context, ArchLibrary *library) {
+       if (dlclose(library->dl)) {
+               context_set_error(context, -1, (char*)dlerror(), 0);
+               return CINV_ERROR;
+       }
+
+       return CINV_SUCCESS;
+}
+
+const static int LEN = 4096;
+// XXX
+char *arch_callback_stub(void *functionp, void *param,
+       short stacksize, cinv_callconv_t cc) {
+       char *ret = mmap(0, LEN, PROT_EXEC|PROT_READ|PROT_WRITE,
+               MAP_ANON|MAP_PRIVATE, -1, 0);
+       if (ret == MAP_FAILED)
+               return NULL;
+       
+       // void f() { ((void (*)(void *))0xAAAAAAAA)((void *)0xBBBBBBBB); }
+       memcpy(ret,
+               "\x55\x89\xe5\x83\xec\x08\x83\xec\x0c\x68"
+               "\xbb\xbb\xbb\xbb\xb8\xaa\xaa\xaa\xaa\xff"
+               "\xd0\x83\xc4\x10\xc9\xc3",
+               26);
+
+       memcpy(ret + 10, &param, 4);
+       memcpy(ret + 15, &functionp, 4);
+       
+       return ret;
+}
+void arch_free_stub(char *stub) {
+       munmap(stub, LEN);
+}
+
+////////////////////////
+// XXX
+int arch_is_register_parm(cinv_callconv_t callingconvention, int index,
+       int num_params, cinv_type_t types[]) { return 0; }
+void arch_set_register_parms(ArchRegParms *regparms, 
+       cinv_callconv_t callingconvention, int num_params, void *parameters[], 
+       cinv_type_t types[]) {}
+void arch_get_register_parms(ArchRegParms *regparms,
+       cinv_callconv_t callingconvention, int num_params, void 
*parameters_out[],
+       cinv_type_t types[]) {}
+////////////////////////
+
+void arch_getval_char(ArchRetValue *archval, char *outval) {
+       *outval = archval->ival;
+}
+void arch_getval_short(ArchRetValue *archval, short *outval) {
+       *outval = archval->ival
+}
+void arch_getval_int(ArchRetValue *archval, int *outval) {
+       *outval = archval->ival;
+}
+void arch_getval_long(ArchRetValue *archval, long int *outval) {
+       *outval = archval->ival;
+}
+void arch_getval_extralong(ArchRetValue *archval, long long int *outval) {
+       *outval = archval->ival;
+}
+void arch_getval_float(ArchRetValue *archval, float *outval) {
+       double d = *(double *)&archval->dval;
+       *outval = (float)d;
+}
+void arch_getval_double(ArchRetValue *archval, double *outval) {
+       double d = *(double *)&archval->dval;
+       *outval = d;
+}
+void arch_getval_ptr(ArchRetValue *archval, void **outval) {
+       *outval = (void *)archval->ival;
+}
+
+void arch_setval_char(ArchRetValue *archval, char val) {
+       archval->ival = val;
+}
+void arch_setval_short(ArchRetValue *archval, short val) {
+       archval->ival = val;
+}
+void arch_setval_int(ArchRetValue *archval, int val) {
+       archval->ival = val;
+}
+void arch_setval_long(ArchRetValue *archval, long int val) {
+       archval->ival = val;
+}
+void arch_setval_extralong(ArchRetValue *archval, long long int val) {
+       archval->ival = val;
+}
+void arch_setval_float(ArchRetValue *archval, float val) {
+       double d = (double)val;
+       archval->dval = *(long *)&d;
+}
+void arch_setval_double(ArchRetValue *archval, double val) {
+       archval->dval = *(long *)&val;
+}
+void arch_setval_ptr(ArchRetValue *archval, void *val) {
+       archval->ival = (long)val;
+}
+
+void arch_size_char(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 1;
+}
+void arch_size_short(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 2;
+       *structalign_out = 2;
+}
+void arch_size_int(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 4;
+       *structalign_out = 4;
+}
+void arch_size_long(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 8;
+       *structalign_out = 8;
+}
+void arch_size_extralong(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 8;
+       *structalign_out = 8;
+}
+void arch_size_float(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 4;
+       *structsize_out = 4;
+       *structalign_out = 4;
+}
+void arch_size_double(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 8;
+       *structalign_out = 8;
+}
+void arch_size_ptr(int *stacksize_out, int *structsize_out,
+       int *stackalign_out, int *structalign_out) {
+       *stacksize_out = 8;
+       *structsize_out = 8;
+       *structalign_out = 8;
+}

Added: trunk/cinvoke/lib/arch/gcc_x64_unix.h
===================================================================
--- trunk/cinvoke/lib/arch/gcc_x64_unix.h                               (rev 0)
+++ trunk/cinvoke/lib/arch/gcc_x64_unix.h       2006-06-26 04:11:31 UTC (rev 47)
@@ -0,0 +1,97 @@
+/*
+C/Invoke Source Code File
+
+Copyright (c) 2006 Will Weisser
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+   1. Redistributions of source code must retain the above copyright notice,
+this list of conditions and the following disclaimer.
+   2. Redistributions in binary form must reproduce the above copyright notice,
+this list of conditions and the following disclaimer in the documentation
+and/or other materials provided with the distribution.
+   3. The name of the author may not be used to endorse or promote products
+derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+*/
+#ifndef _ARCH_GCC_X86_LINUX_H
+#define _ARCH_GCC_X86_LINUX_H
+
+#include <errno.h>
+
+typedef struct _ArchLibrary {
+       void *dl;
+} ArchLibrary;
+
+typedef struct _ArchRetValue {
+       long ival;
+       long dval;
+} ArchRetValue;
+
+typedef struct _ArchRegParms {} ArchRegParms; // XXX
+
+typedef char cinv_int8_t;
+typedef short cinv_int16_t;
+typedef int cinv_int32_t;
+typedef long cinv_int64_t;
+
+#define CINV_E_NOMEM ((cinv_int32_t)ENOMEM)
+#define CINV_S_NOMEM (strerror(ENOMEM))
+#define CINV_NOMEM_NEEDSFREE 1
+#define CINV_E_INVAL ((cinv_int32_t)EINVAL)
+
+#define CINV_CC_DEFAULT CINV_CC_CDECL
+#define CINV_T_2BYTE CINV_T_SHORT
+#define CINV_T_4BYTE CINV_T_INT
+#define CINV_T_8BYTE CINV_T_EXTRALONG
+
+// XXX aligning stack on 16-byte boundary may be important
+
+/////////////////////////////////////
+// macros
+/////////////////////////////////////
+#define ARCH_SAVE_REGPARMS(regparms) // XXX
+
+#define ARCH_CALL(regparms, ep) ((void (*)())ep)(); // XXX
+
+#define ARCH_SAVE_RETURN(archvalue) \
+       __asm__("movq %%rax, %0; \
+               movsd %xmm0, %1" : \
+                       "=m" ((archvalue).ival), \
+                       "=m" ((archvalue).dval));
+
+#define ARCH_SET_RETURN(archvalue) \
+       __asm__("movq %0, %%rax; \
+               movsd %1, %%xmm0" :: \
+                       "m" ((archvalue).ival), \
+                       "m" ((archvalue).dval) : \
+                       "%rax", "%xmm0");
+
+#define ARCH_PUT_STACK_BYTES(bcount) \
+       __asm__("subq %0, %%rsp" :: "m" (bcount) : "%rsp");
+
+#define ARCH_REMOVE_STACK_BYTES(bcount) \
+       __asm__("addq %0, %%rsp" :: "m" (bcount) : "%rsp");
+
+#define ARCH_GET_STACK(sp) \
+       __asm__("movq %%rsp, %0" : "=m" (sp));
+
+#define ARCH_GET_FRAME_PTR(fp) \
+       __asm__("movq %%rbp, %0" : "=m" (fp));
+
+#define ARCH_CALLBACK_ARG_OFFSET (4*10) // XXX
+
+#define ARCH_STACK_GROWS_DOWN 1
+
+#endif

Modified: trunk/cinvoke/lib/cinvoke-arch.h
===================================================================
--- trunk/cinvoke/lib/cinvoke-arch.h    2006-06-25 16:16:18 UTC (rev 46)
+++ trunk/cinvoke/lib/cinvoke-arch.h    2006-06-26 04:11:31 UTC (rev 47)
@@ -70,6 +70,9 @@
 #ifdef ARCH_GCC_X86_UNIX
 #include "arch/gcc_x86_unix.h"
 #endif
+#ifdef ARCH_GCC_X64_UNIX
+#include "arch/gcc_x64_unix.h"
+#endif
 #ifdef ARCH_CL_X86_WIN
 #include "arch/cl_x86_win.h"
 #endif

Modified: trunk/cinvoke/targets.sh
===================================================================
--- trunk/cinvoke/targets.sh    2006-06-25 16:16:18 UTC (rev 46)
+++ trunk/cinvoke/targets.sh    2006-06-26 04:11:31 UTC (rev 47)
@@ -1,4 +1,4 @@
 #!/bin/sh
 
 echo "Please choose a target and enter: make <targetname>"
-echo "Options: x86"
+echo "Options: x86 x64"





reply via email to

[Prev in Thread] Current Thread [Next in Thread]