I'm compiling postgresql 9.0.2 using msys + mingw + gcc 4.5.2 (latest
official release from mingw). This is on Windows 7 64-bit.
Unfortunately the built dlls, at least libpq.dll, crash if they need to
be relocated. This happens to me when loading libpq.dll into a project
that has a number of other dll requirements. Note this does NOT happen
when building with gcc 3.4.5.
Using GDB to track down the problem, the error occurs in
__gcc_register_frame and looks to be the same error described here:
http://old.nabble.com/Bad-DLL-relocation---reproducible-w--test-case-td18292380.html
Note a similar sounding error described, and fixed, in newer releases of
binutils (which mingw provides and I am using) is described here:
http://lists-archives.org/mingw-users/11369-error-0xc0000005-is-now-fixed.html
Looking at the postgresql Makefiles, the dlls are built using dllwrap.
In particular, see src/Makefile.shlib line 413 and src/Makefile.port
line 25.
When I change these to not use dllwrap and instead just use gcc -shared
the problem goes away. Therefore I'd like to propose:
1. Change line 413 in src/Makefile.shlib
$(DLLWRAP) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def
$(DLL_DEFFILE) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
To:
$(CC) -shared -o $@ $(DLL_DEFFILE) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL)
$(SHLIB_LINK)
2. Changle line 73 in src/Makefile.port:
$(DLLWRAP) -o $@ --def $*.def $< $(LDFLAGS) $(LDFLAGS_SL) $(BE_DLLLIBS)
To:
$(CC) -shared -o $@ $*.def $< $(LDFLAGS) $(LDFLAGS_SL) $(BE_DLLLIBS)
I tested this by intentionally compiling libpq.dll using the link flag
-Wl,--image-base to make sure that its base address conflicted with
another dll loaded by my program. With the proposed changes, windows
successfully relocated libpq.dll without causing a segmentation fault.
I don't claim to know why dllwrap is producing dlls that can't be
relocated while gcc -shared is. But it appears to have been deprecated
back in 2006 according to the binutils mailing list:
http://www.mail-archive.com/bug-binutils@gnu.org/msg01470.html
So between being deprecated and not producing relocatable dlls, it seems
like its best to stop using dllwrap. If this seems like a reasonable
change I can put together a quick patch if that helps.
Thanks,
Charlie