Thread: MingW + GCC 4.5.2 + Relocate libpq.dll = SegFault

MingW + GCC 4.5.2 + Relocate libpq.dll = SegFault

From
Charlie Savage
Date:
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


Re: MingW + GCC 4.5.2 + Relocate libpq.dll = SegFault

From
Pavel Golub
Date:
Hello, Charlie.

Can you please express your opinion about my request "Warning
compiling pg_dump (MinGW, Windows XP)" to pgsql-hackers on Thu, 13 Jan
2011. Do you have the same warnings using MinGW environment?

You wrote:

CS> I'm compiling postgresql 9.0.2 using msys + mingw + gcc 4.5.2 (latest 
CS> official release from mingw).  This is on Windows 7 64-bit.

CS> Unfortunately the built dlls, at least libpq.dll, crash if they need to
CS> be relocated.  This happens to me when loading libpq.dll into a project
CS> that has a number of other dll requirements.  Note this does NOT happen
CS> when building with gcc 3.4.5.

CS> Using GDB to track down the problem, the error occurs in 
CS> __gcc_register_frame and looks to be the same error described here:

CS> http://old.nabble.com/Bad-DLL-relocation---reproducible-w--test-case-td18292380.html

CS> Note a similar sounding error described, and fixed, in newer releases of
CS> binutils (which mingw provides and I am using) is described here:

CS> http://lists-archives.org/mingw-users/11369-error-0xc0000005-is-now-fixed.html

CS> Looking at the postgresql Makefiles, the dlls are built using dllwrap.
CS> In particular, see src/Makefile.shlib line 413 and src/Makefile.port 
CS> line 25.

CS> When I change these to not use dllwrap and instead just use gcc -shared
CS> the problem goes away.  Therefore I'd like to propose:

CS> 1.  Change line 413 in src/Makefile.shlib

CS> $(DLLWRAP) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def 
CS> $(DLL_DEFFILE) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)

CS> To:

CS> $(CC) -shared -o $@ $(DLL_DEFFILE) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) 
CS> $(SHLIB_LINK)

CS> 2.  Changle line 73 in src/Makefile.port:

CS> $(DLLWRAP) -o $@ --def $*.def $< $(LDFLAGS) $(LDFLAGS_SL) $(BE_DLLLIBS)

CS> To:

CS> $(CC) -shared -o $@ $*.def $< $(LDFLAGS) $(LDFLAGS_SL) $(BE_DLLLIBS)

CS> I tested this by intentionally compiling libpq.dll using the link flag
CS> -Wl,--image-base to make sure that its base address conflicted with 
CS> another dll loaded by my program.  With the proposed changes, windows 
CS> successfully relocated libpq.dll without causing a segmentation fault.

CS> I don't claim to know why dllwrap is producing dlls that can't be 
CS> relocated while gcc -shared is.  But it appears to have been deprecated
CS> back in 2006 according to the binutils mailing list:

CS> http://www.mail-archive.com/bug-binutils@gnu.org/msg01470.html

CS> So between being deprecated and not producing relocatable dlls, it seems
CS> like its best to stop using dllwrap.  If this seems like a reasonable 
CS> change I can put together a quick patch if that helps.

CS> Thanks,

CS> Charlie




-- 
With best wishes,Pavel                          mailto:pavel@gf.microolap.com