[Gc] mingw32ce patch

Vincent R. forumer at smartmobili.com
Wed Sep 30 02:16:34 PDT 2009


Hi,

I am trying to compile boehm gc on a windows ce devices and I have some
remarks/feedback.

1) cegcc is a cross compiler project based on gcc and targetting wince
devices.
Project is available here : http://cegcc.sourceforge.net/.
If you want to compile it yourself : 
svn co https://cegcc.svn.sourceforge.net/svnroot/cegcc/trunk/cegcc cegcc
cd cegcc/src/scripts
mkdir build; cd build
../build-mingw32ce.sh -j2
Then to use it, add it to your PATH : export PATH=$PATH:/opt/mingw32ce/bin

To compile boehm with this toolchain:
./configure --host=arm-mingw32ce
make


Unfortunately Boehm-gc doesn't not support it. 
You need to know that compiler exports the following defines :
-D__COREDLL__ -D__MINGW32__ -D__MINGW32CE__

You will find attached a patch to fix it.

However now I have one error left : 
/cygdrive/c/downloads/gc-7.2alpha2.tar/gc-7.2alpha2_NEW/misc.c:765:
undefined reference to `GC_thr_init'

but I don't know exactly what to do with it so before to go any further I
would like your comments.

Oh I forget one last thing by reading very quickly source code I could see
that you need to find stack address on different platforms but it seems
that on existing code for MSWINCE you are using an approximative method

Microsoft release sources of windows ce OS(see Platform Builder) 
and from it you can see that to retrieve stack bottom you can use the
following code :

void *os_query_stackBottom()
{
   const LPBYTE PUserKData = cast(LPBYTE)0xFFFFC800;

    DWORD dwStackBase;
    BOOL bKmode;

    bKmode = SetKMode(TRUE);
    dwStackBase = *(cast(DWORD*) (*(cast(DWORD*)(PUserKData + 0x094)) +
0x1C));

    SetKMode(bKmode);
    return cast(void *)dwStackBase;
}

You just need to know that wince kernel is always loaded at the same
address on ARM architecture
(0xFFFFC800) and that from it we can get stack address.
If my memory serves me rightn, stack is descending (from high address to
low address) so
function is called stackBottom but could be renamed stackBound.
I could give you more information if you need to.

Regards

Vincent Richomme
-------------- next part --------------
diff -ru gc-7.2alpha2/dbg_mlc.c gc-7.2alpha2_NEW/dbg_mlc.c
--- gc-7.2alpha2/dbg_mlc.c	2009-06-13 00:59:34.000000000 +0200
+++ gc-7.2alpha2_NEW/dbg_mlc.c	2009-09-29 11:53:32.562500000 +0200
@@ -15,7 +15,10 @@
  * modified is included with the above copyright notice.
  */
 
-#include <errno.h>
+#ifndef __MINGW32CE__
+# include <errno.h>
+#endif
+
 #include <string.h>
 #include "private/dbg_mlc.h"
 
@@ -644,7 +647,9 @@
     if (str == NULL) return NULL;
     copy = GC_debug_malloc_atomic(strlen(str) + 1, OPT_RA s, i);
     if (copy == NULL) {
+#ifndef __MINGW32CE__
       errno = ENOMEM;
+#endif
       return NULL;
     }
     strcpy(copy, str);
diff -ru gc-7.2alpha2/include/private/gcconfig.h gc-7.2alpha2_NEW/include/private/gcconfig.h
--- gc-7.2alpha2/include/private/gcconfig.h	2009-06-12 20:38:08.000000000 +0200
+++ gc-7.2alpha2_NEW/include/private/gcconfig.h	2009-09-29 14:35:56.218750000 +0200
@@ -69,7 +69,7 @@
 /* Determine the machine type: */
 # if defined(__arm__) || defined(__thumb__)
 #    define ARM32
-#    if !defined(LINUX) && !defined(NETBSD) && !defined(DARWIN)
+#    if !defined(LINUX) && !defined(NETBSD) && !defined(DARWIN) && !defined(__MINGW32CE__)
 #      define NOSYS
 #      define mach_type_known
 #    endif
@@ -366,7 +366,7 @@
     /* DGUX defined */
 #   define mach_type_known
 # endif
-# if defined(_WIN32_WCE)
+# if defined(_WIN32_WCE) || defined(__MINGW32CE__)
     /* SH3, SH4, MIPS already defined for corresponding architectures */
 #   if defined(SH3) || defined(SH4)
 #     define SH
@@ -408,7 +408,7 @@
 #   define CYGWIN32
 #   define mach_type_known
 # endif
-# if defined(__MINGW32__) && !defined(mach_type_known)
+# if defined(__MINGW32__) && !defined(__MINGW32CE__) && !defined(mach_type_known)
 #   define I386
 #   define MSWIN32
 #   define mach_type_known
@@ -2203,7 +2203,7 @@
 # if defined(GC_GNU_THREADS) && !defined(HURD)
 	--> inconsistent configuration
 # endif
-# if defined(GC_WIN32_THREADS) && !defined(MSWIN32) && !defined(CYGWIN32)
+# if defined(GC_WIN32_THREADS) && !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
 	--> inconsistent configuration
 # endif
diff -ru gc-7.2alpha2/malloc.c gc-7.2alpha2_NEW/malloc.c
--- gc-7.2alpha2/malloc.c	2009-03-01 01:25:38.000000000 +0100
+++ gc-7.2alpha2_NEW/malloc.c	2009-09-29 12:02:19.234375000 +0200
@@ -15,7 +15,11 @@
  
 #include <stdio.h>
 #include <string.h>
-#include <errno.h>
+
+#ifndef __MINGW32CE__
+# include <errno.h>
+#endif /* __MINGW32CE__ */
+
 #include "private/gc_priv.h"
 
 extern void * GC_clear_stack(void *);	/* in misc.c, behaves like identity */
@@ -239,7 +243,9 @@
 
   if (s == NULL) return NULL;
   if ((copy = GC_malloc_atomic(strlen(s) + 1)) == NULL) {
+#ifndef __MINGW32CE__
     errno = ENOMEM;
+#endif
     return NULL;
   }
   strcpy(copy, s);
@@ -370,7 +376,9 @@
     size_t len = strlen(s) + 1;
     char * result = ((char *)REDIRECT_MALLOC(len+1));
     if (result == 0) {
+#ifndef __MINGW32CE__
       errno = ENOMEM;
+#endif
       return 0;
     }
     BCOPY(s, result, len+1);
diff -ru gc-7.2alpha2/misc.c gc-7.2alpha2_NEW/misc.c
--- gc-7.2alpha2/misc.c	2009-06-12 23:41:00.000000000 +0200
+++ gc-7.2alpha2_NEW/misc.c	2009-09-30 10:56:31.359375000 +0200
@@ -18,7 +18,7 @@
 #include <stdio.h>
 #include <limits.h>
 #include <stdarg.h>
-#ifndef _WIN32_WCE
+#if !defined(_WIN32_WCE) && !defined(__MINGW32CE__)
 #include <signal.h>
 #endif
 
@@ -507,7 +507,10 @@
 #   if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
       if (!GC_is_initialized) {
         BOOL (WINAPI *pfn) (LPCRITICAL_SECTION, DWORD) = NULL;
+
+#ifndef __MINGW32CE__
         HMODULE hK32 = GetModuleHandleA("kernel32.dll");
+
         if (hK32)
   	  pfn = (BOOL (WINAPI *) (LPCRITICAL_SECTION, DWORD))
   		GetProcAddress (hK32,
@@ -515,6 +518,7 @@
         if (pfn)
             pfn(&GC_allocate_ml, 4000);
         else
+#endif
   	  InitializeCriticalSection (&GC_allocate_ml);
       }
 #endif /* MSWIN32 */
@@ -877,6 +881,96 @@
 # define IF_NEED_TO_LOCK(x)
 #endif
 
+
+#ifdef __MINGW32CE__
+
+wchar_t *
+char_to_wchar(const char *text)
+{
+  wchar_t *wtext;
+  int      wsize;
+
+  wsize = MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, NULL, 0);
+  if ((wsize == 0) ||
+      (wsize > (int)(ULONG_MAX / sizeof(wchar_t))))
+    return NULL;
+
+  wtext = malloc(wsize * sizeof(wchar_t));
+  if (wtext)
+    if (!MultiByteToWideChar(CP_ACP, 0, text, (int)strlen(text) + 1, wtext, wsize))
+      return NULL;
+
+  return wtext;
+}
+
+char *
+wchar_to_char(const wchar_t *text)
+{
+  char  *atext;
+  size_t size;
+  int    asize;
+
+  size = wcslen(text) + 1;
+
+  asize = WideCharToMultiByte(CP_ACP, 0, text, (int)size, NULL, 0, NULL, NULL);
+  if (asize == 0)
+    return NULL;
+
+  atext = (char*)malloc((asize + 1) * sizeof(char));
+
+  if (atext)
+    if (!WideCharToMultiByte(CP_ACP, 0, text, (int)size, atext, asize, NULL, NULL))
+      return NULL;
+  atext[asize] = '\0';
+
+  return atext;
+}
+
+HANDLE CreateFileA( LPCSTR filename,
+		    DWORD  access,
+		    DWORD sharing,
+		    LPSECURITY_ATTRIBUTES sa,
+		    DWORD creation,
+		    DWORD attributes,
+		    HANDLE template)
+{
+  HANDLE hFile = NULL;
+  wchar_t* filenameW;
+
+  if (!filename)
+    return NULL;
+
+  filenameW = char_to_wchar(filename);
+  if (filenameW) {
+      hFile = CreateFileW(filenameW, access, sharing, sa, creation, attributes, template);
+      free(filenameW);
+    }
+
+  return hFile;
+}
+
+DWORD  GetModuleFileNameA(HMODULE hModule,
+			  LPSTR lpFilename,
+			  DWORD nSize)
+{
+  DWORD dwRet = 0;
+  wchar_t* lpFilenameW;
+
+  if (!lpFilename)
+    return 0;
+
+  lpFilenameW = char_to_wchar(lpFilename);
+  if (lpFilenameW) {
+    dwRet = GetModuleFileNameW(hModule, lpFilenameW, nSize);
+    free(lpFilenameW);
+  }
+  
+  return dwRet;
+}
+
+
+#endif
+
   int GC_write(const char *buf, size_t len)
   {
       BOOL tmp;
diff -ru gc-7.2alpha2/os_dep.c gc-7.2alpha2_NEW/os_dep.c
--- gc-7.2alpha2/os_dep.c	2009-06-05 05:32:39.000000000 +0200
+++ gc-7.2alpha2_NEW/os_dep.c	2009-09-29 18:06:46.859375000 +0200
@@ -1380,6 +1380,8 @@
     }
     return p;
   }
+#else
+   GC_bool GC_wnt = FALSE;
 # endif
 
 # ifndef REDIRECT_MALLOC


More information about the Gc mailing list