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

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


I wrote:

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

I guess I sent that off prematurely -- here's a much cleaner patch that
achieves the same effect.  (My apologies for the waste of bandwidth.)

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 06:34:19 1997
@@ -375,6 +375,15 @@
 gctest_irix_dyn_link: test.o libirixgc.so
 	$(CC) -L$(ABSDIR) -o gctest_irix_dyn_link test.o -lirixgc
 
+test_dll.o: test.c libgc_globals.h
+	$(CC) $(CFLAGS) -DGC_USE_DLL -c test.c -o test_dll.o
+
+test_dll: test_dll.o libgc_dll.a libgc.dll
+	$(CC) test_dll.o -L$(ABSDIR) -lgc_dll -o test_dll
+
+SYM_PREFIX-libgc=GC
+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 06:30:47 1997
@@ -32,6 +32,9 @@
 # define __GC
 # include <stddef.h>
 
+#if defined(__CYGWIN32__) && defined(GC_USE_DLL)
+#include "libgc_globals.h"
+#endif
 
 #if defined(_MSC_VER) && defined(_DLL)
 #ifdef GC_BUILD
@@ -638,6 +640,11 @@
 #   define GC_INIT() { extern end, etext; \
 		       extern void GC_noop(void *, void *); \
 		       GC_noop(&end, &etext); }
+/*
+ * Similarly for gnu-win32 DLLs
+ */
+#elif defined(__CYGWIN32__) && defined(GC_USE_DLL)
+#   define GC_INIT() { GC_add_roots(DATASTART, DATAEND); }
 #else
 #   define GC_INIT()
 #endif
--- /dev/null	Fri Aug  1 06:50:01 1997
+++ Makefile.DLLs	Fri Aug  1 06:51:56 1997
@@ -0,0 +1,105 @@
+#-----------------------------------------------------------------------------#
+
+# Makefile.DLLs, version 0.4.
+
+# This Makefile contains rules for creating DLLs on Windows using gnu-win32.
+
+#-----------------------------------------------------------------------------#
+
+# 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').
+%.def: %.a
+	echo EXPORTS > $@
+	nm $< | grep '^........ [BCDRT] _' | sed 's/[^_]*_//' >> $@
+
+# We need to use macros to access global data:
+# the user of the DLL must refer to `foo' as `(*__imp_foo)'.
+# This rule creates a `_globals.h' file, which contains macros
+# for doing this.
+
+SYM_PREFIX = $(firstword $(SYM_PREFIX-$*) $*)
+DLL_MACRO = $(SYM_PREFIX)_USE_DLL
+IMP_MACRO = $(SYM_PREFIX)_IMP
+GLOBAL_MACRO = $(SYM_PREFIX)_GLOBAL
+
+%_globals.h: %.a
+	echo "/* automatically generated by Makefile.DLLs */"	> $@
+	echo "#if defined(__GNUC__) && defined(_WIN32) \\"	>> $@
+	echo "	&& defined($(DLL_MACRO))"			>> $@
+	echo "#  define $(IMP_MACRO)(name)	__imp_##name" 	>> $@
+	echo "#  define $(GLOBAL_MACRO)(name)	(*$(IMP_MACRO)(name))" >> $@
+	echo "#else"						>> $@
+	echo "#  define $(GLOBAL_MACRO)(name)	name"		>> $@
+	echo "#endif"						>> $@
+	echo ""							>> $@
+	for sym in `nm $< | grep '^........ [BCDR] _' | sed 's/[^_]*_//'`; do \
+		echo "#define $$sym	$(GLOBAL_MACRO)($$sym)"	>> $@; \
+	done
+
+# 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
+%.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
+
+# This bit is necessary to provide an initialization function for the DLL.
+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.