[gclist] Boehm collector as Windows DLL with gnu-win32

Fergus Henderson fjh@cs.mu.oz.au
Fri, 1 Aug 1997 05:19:46 -1400 (EST)


Hi,

Attached below is a patch to the Boehm collector so that you can build
it as a DLL on Windows using gnu-win32.

To use, just apply this patch to the version 4.11 of the collector,
and then type `make test_dll'.

Applications that use the DLL
	(1) must be compiled with -DGC_USE_DLL
	(2) must call GC_INIT() from the main executable at startup,
	    just as is necessary on Solaris
	(3) must access global variables in the GC library only
	    by #including the relevent header file, not by manually
	    declaring them.

Cheers,
	Fergus.

diff -u mercury-rotd-1997-07-28/boehm_gc/Makefile mercury-new/boehm_gc/Makefile
--- mercury-rotd-1997-07-28/boehm_gc/Makefile	Mon Feb 10 00:10:13 1997
+++ mercury-new/boehm_gc/Makefile	Fri Aug 01 04:24:53 1997
@@ -375,6 +375,14 @@
 gctest_irix_dyn_link: test.o libirixgc.so
 	$(CC) -L$(ABSDIR) -o gctest_irix_dyn_link test.o -lirixgc
 
+test_dll.o: test.c
+	$(CC) $(CFLAGS) -DGC_USE_DLL -c test.c -o test_dll.o
+
+test_dll: test_dll.o libgc.dll libgc_dll.a
+	$(CC) test_dll.o -L$(ABSDIR) -lgc_dll -o test_dll
+
+include Makefile.DLLs
+
 reserved_namespace: $(SRCS)
 	for file in $(SRCS) test.c test_cpp.cc; do \
 		sed s/GC_/_GC_/g < $$file > tmp; \
diff -u mercury-rotd-1997-07-28/boehm_gc/gc.h mercury-new/boehm_gc/gc.h
--- mercury-rotd-1997-07-28/boehm_gc/gc.h	Sat Dec 07 00:01:15 1996
+++ mercury-new/boehm_gc/gc.h	Fri Aug 01 04:43:09 1997
@@ -32,6 +32,19 @@
 # define __GC
 # include <stddef.h>
 
+/*
+ * If the GC library is compiled into a DLLs on Windows using gcc,
+ * then for clients of the DLL, every exported global data symbol `foo'
+ * needs to be #defined as `GC_GLOBAL(foo)'.  Such clients must be
+ * compiled with `-DGC_USE_DLL'.
+ *
+ */
+#if defined(__GNUC__) && defined(_WIN32) && defined(GC_USE_DLL)
+#  define GC_IMP(name) 		__imp_##name
+#  define GC_GLOBAL(name)	(*GC_IMP(name))
+#else
+#  define GC_GLOBAL(name)	name
+#endif
 
 #if defined(_MSC_VER) && defined(_DLL)
 #ifdef GC_BUILD
@@ -71,12 +84,16 @@
 
 /* Public read-only variables */
 
+#define GC_gc_no GC_GLOBAL(GC_gc_no)
+
 GC_API GC_word GC_gc_no;/* Counter incremented per collection.  	*/
 			/* Includes empty GCs at startup.		*/
 			
 
 /* Public R/W variables */
 
+#define GC_oom_fn GC_GLOBAL(GC_gc_oom_fn)
+
 GC_API void * (*GC_oom_fn) GC_PROTO((size_t bytes_requested));
 			/* When there is insufficient memory to satisfy */
 			/* an allocation request, we return		*/
@@ -86,26 +103,38 @@
 			/* pointer to a previously allocated heap 	*/
 			/* object.					*/
 
+#define GC_quiet GC_GLOBAL(GC_quiet)
+
 GC_API int GC_quiet;	/* Disable statistics output.  Only matters if	*/
 			/* collector has been compiled with statistics	*/
 			/* enabled.  This involves a performance cost,	*/
 			/* and is thus not the default.			*/
 
+#define GC_dont_gc GC_GLOBAL(GC_dont_gc)
+
 GC_API int GC_dont_gc;	/* Dont collect unless explicitly requested, e.g. */
 			/* because it's not safe.			  */
 
+#define GC_dont_expand GC_GLOBAL(GC_dont_expand)
+
 GC_API int GC_dont_expand;
 			/* Dont expand heap unless explicitly requested */
 			/* or forced to.				*/
 
+#define GC_full_freq GC_GLOBAL(GC_full_freq)
+
 GC_API int GC_full_freq;    /* Number of partial collections between	*/
 			    /* full collections.  Matters only if	*/
 			    /* GC_incremental is set.			*/
 			
+#define GC_non_gc_bytes GC_GLOBAL(GC_non_gc_bytes)
+
 GC_API GC_word GC_non_gc_bytes;
 			/* Bytes not considered candidates for collection. */
 			/* Used only to control scheduling of collections. */
 
+#define GC_free_space_divisor GC_GLOBAL(GC_free_space_divisor)
+
 GC_API GC_word GC_free_space_divisor;
 			/* We try to make sure that we allocate at 	*/
 			/* least N/GC_free_space_divisor bytes between	*/
@@ -119,6 +148,8 @@
 			/* GC_free_space_divisor = 1 will effectively	*/
 			/* disable collections.				*/
 
+#define GC_max_retries GC_GLOBAL(GC_max_retries)
+
 GC_API GC_word GC_max_retries;
 			/* The maximum number of GCs attempted before	*/
 			/* reporting out of memory after heap		*/
@@ -568,11 +599,15 @@
 #endif
 
 /* Fynctions called to report pointer checking errors */
+#define GC_same_obj_print_proc GC_GLOBAL(GC_same_obj_print_proc)
 GC_API void (*GC_same_obj_print_proc) GC_PROTO((GC_PTR p, GC_PTR q));
 
+#define GC_is_valid_displacement_print_proc \
+	GC_GLOBAL(GC_is_valid_displacement_print_proc)
 GC_API void (*GC_is_valid_displacement_print_proc)
 	GC_PROTO((GC_PTR p));
 
+#define GC_is_visible_print_proc GC_GLOBAL(GC_is_visible_print_proc)
 GC_API void (*GC_is_visible_print_proc)
 	GC_PROTO((GC_PTR p));
 
@@ -638,6 +673,8 @@
 #   define GC_INIT() { extern end, etext; \
 		       extern void GC_noop(void *, void *); \
 		       GC_noop(&end, &etext); }
+#elif defined(__CYGWIN32__) && defined(GC_USE_DLL)
+#   define GC_INIT() { GC_add_roots(DATASTART, DATAEND); }
 #else
 #   define GC_INIT()
 #endif
diff -u mercury-rotd-1997-07-28/boehm_gc/gc_priv.h mercury-new/boehm_gc/gc_priv.h
--- mercury-rotd-1997-07-28/boehm_gc/gc_priv.h	Thu May 29 02:11:27 1997
+++ mercury-new/boehm_gc/gc_priv.h	Fri Aug 01 03:26:05 1997
@@ -933,6 +933,7 @@
 #endif
 };
 
+#define GC_arrays GC_GLOBAL(GC_arrays)
 GC_API GC_FAR struct _GC_arrays GC_arrays; 
 
 # define GC_objfreelist GC_arrays._objfreelist
@@ -980,10 +981,12 @@
 # define beginGC_arrays ((ptr_t)(&GC_arrays))
 # define endGC_arrays (((ptr_t)(&GC_arrays)) + (sizeof GC_arrays))
 
+#define GC_fo_entries GC_GLOBAL(GC_fo_entries)
 GC_API word GC_fo_entries;
 
 # define MAXOBJKINDS 16
 
+#define GC_obj_kinds GC_GLOBAL(GC_obj_kinds)
 /* Object kinds: */
 extern struct obj_kind {
    ptr_t *ok_freelist;	/* Array of free listheaders for this kind of object */
@@ -1013,51 +1016,68 @@
 #   define IS_UNCOLLECTABLE(k) ((k) == UNCOLLECTABLE)
 # endif
 
+#define GC_n_kinds GC_GLOBAL(GC_n_kinds)
 extern int GC_n_kinds;
 
+#define GC_n_heap_sects GC_GLOBAL(GC_n_heap_sects)
 extern word GC_n_heap_sects;	/* Number of separately added heap	*/
 				/* sections.				*/
 
 # ifdef MSWIN32
+#define GC_n_heap_bases GC_GLOBAL(GC_n_heap_bases)
 extern word GC_n_heap_bases;	/* See GC_heap_bases.	*/
 # endif
 
+#define GC_total_black_listed GC_GLOBAL(GC_total_black_listed)
 extern word GC_total_black_listed;
 			/* Number of bytes on stack blacklist. 	*/
 
+#define GC_black_list_spacing GC_GLOBAL(GC_black_list_spacing)
 extern word GC_black_list_spacing;
 			/* Average number of bytes between blacklisted	*/
 			/* blocks. Approximate.				*/
 
+#define GC_invalid_map GC_GLOBAL(GC_invalid_map)
 extern char * GC_invalid_map;
 			/* Pointer to the nowhere valid hblk map */
 			/* Blocks pointing to this map are free. */
 
+#define GC_hblkfreelist GC_GLOBAL(GC_hblkfreelist)
 extern struct hblk * GC_hblkfreelist;
 				/* List of completely empty heap blocks	*/
 				/* Linked through hb_next field of 	*/
 				/* header structure associated with	*/
 				/* block.				*/
 
+#define GC_is_initialized GC_GLOBAL(GC_is_initialized)
 extern bool GC_is_initialized;		/* GC_init() has been run.	*/
 
+#define GC_objects_are_marked GC_GLOBAL(GC_objects_are_marked)
 extern bool GC_objects_are_marked;	/* There are marked objects in  */
 					/* the heap.			*/
 
+#define GC_incremental GC_GLOBAL(GC_incremental)
 extern int GC_incremental;  /* Using incremental/generational collection. */
 
+#define GC_dirty_maintained GC_GLOBAL(GC_dirty_maintained)
 extern bool GC_dirty_maintained;/* Dirty bits are being maintained, 	*/
 				/* either for incremental collection,	*/
 				/* or to limit the root set.		*/
 
 # ifndef PCR
+    #define GC_stackbottom GC_GLOBAL(GC_stackbottom)
     extern ptr_t GC_stackbottom;	/* Cool end of user stack	*/
 # endif
 
+#define GC_root_size GC_GLOBAL(GC_root_size)
 extern word GC_root_size;	/* Total size of registered root sections */
 
+#define GC_debugging_started GC_GLOBAL(GC_debugging_started)
 extern bool GC_debugging_started;	/* GC_debug_malloc has been called. */ 
 
+#define GC_least_plausible_heap_addr GC_GLOBAL(GC_least_plausible_heap_addr)
+#define GC_greatest_plausible_heap_addr \
+				GC_GLOBAL(GC_greatest_plausible_heap_addr)
 extern ptr_t GC_least_plausible_heap_addr;
 extern ptr_t GC_greatest_plausible_heap_addr;
 			/* Bounds on the heap.  Guaranteed valid	*/
--- /dev/null	Fri Aug  1 04:58:17 1997
+++ /home/pgrad/fjh/tmp/Makefile.DLLs	Fri Aug  1 04:58:50 1997
@@ -0,0 +1,85 @@
+#-----------------------------------------------------------------------------#
+
+# Makefile.DLLs, version 0.3.
+
+# This Makefile contains rules for creating DLLs on Windows using gnu-win32.
+# This has been tested using GNU-win32 version beta 18.
+
+#-----------------------------------------------------------------------------#
+
+# This rule creates a `.def' file, which lists the symbols that are exported
+# from the DLL.  We use `nm' to get a list of all the exported text (`T')
+# symbols and data symbols -- including uninitialized data (`B'),
+# initialized data (`D'), read-only data (`R'), and common blocks (`C').
+# Beware that you need to use macros to access such global data:
+# the user of the DLL must refer to `foo' as `(*__imp_foo)'.
+
+%.def: %.a
+	echo EXPORTS > $@
+	nm $< | grep '^........ [BCDRT] _' | sed 's/[^_]*_//' >> $@
+
+# This rule creates the export object file (`foo.exp') which contains the
+# jump table array; this export object file becomes part of the DLL. 
+# This rule also creates the import library (`foo_dll.a') which contains small
+# stubs for all the functions exported by the DLL which jump to them via the
+# jump table.  Executables that will use the DLL must be linked against this
+# stub library.
+%.exp %_dll.a : %.def
+	dlltool $(DLLTOOLFLAGS) $(DLLTOOLFLAGS-$*)		\
+		--def $<					\
+		--dllname $*.dll				\
+		--output-exp $*.exp				\
+		--output-lib $*_dll.a
+
+# The `sed' commands below are to convert DOS-style `C:\foo\bar'
+# pathnames into Unix-style `//c/foo/bar' pathnames.
+CYGWIN32_LIBS = $(shell echo					\
+	-L`dirname \`gcc -print-file-name=libgcc.a |		\
+	sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
+	-L`dirname \`gcc -print-file-name=libcygwin.a |	\
+	sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
+	-L`dirname \`gcc -print-file-name=libkernel32.a | \
+	sed -e 's@^\\\\([A-Za-z]\\\\):@//\\\\1@g' -e 's@\\\\\\\\@/@g' \` ` \
+	-lgcc -lcygwin -lkernel32 -lgcc)
+
+RELOCATABLE=yes
+
+ifeq "$(strip $(RELOCATABLE))" "yes"
+
+# to create relocatable DLLs, we need to do two passes
+# (warning: this is untested)
+%.dll: %.exp %.a dll_fixup.o dll_init.o
+	$(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll -o $*.base			\
+		-e _dll_entry@12 dll_init.o				\
+		dll_fixup.o $*.exp $*.a					\
+		$(LDLIBS) $(LDLIBS-$*)					\
+		$(CYGWIN32_LIBS)
+	$(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll --base-file $*.base -o $@	\
+		-e _dll_entry@12 dll_init.o				\
+		dll_fixup.o $*.exp $*.a					\
+		$(LDLIBS) $(LDLIBS-$*)					\
+		$(CYGWIN32_LIBS)
+	rm -f $*.base
+else
+
+%.dll: %.exp %.a dll_fixup.o dll_init.o
+	$(LD) $(LDFLAGS) $(LDFLAGS-$*) --dll -o $@			\
+		-e _dll_entry@12 dll_init.o				\
+		dll_fixup.o $*.exp $*.a					\
+		$(LDLIBS) $(LDLIBS-$*)					\
+		$(CYGWIN32_LIBS)
+
+endif
+
+# This black magic piece of assembler needs to be linked in in order to
+# properly terminate the list of imported DLLs.
+dll_fixup.s:
+	echo '.section .idata$$3' 	> dll_fixup.s
+	echo '.long 0,0,0,0, 0,0,0,0'	>> dll_fixup.s
+
+dll_init.c:
+	echo '__attribute__((stdcall))' > dll_init.c
+	echo 'int dll_entry(int handle, int reason, void *ptr)' >> dll_init.c
+	echo '{return 1; }' >> dll_init.c
+
+dont_throw_away: dll_fixup.o dll_init.o


-- 
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.