Created attachment 9707 [details] minimal case for the DLL relocation bug OS: Win7 SP1 Toolchain: Mingw-w64 i686-6.2.0-release-win32-dwarf-rt_v5-rev1 Binutils: 2.25.1 I don't know whether it's a GCC bug or a binutils bug, but I think it's not a mingw bug. I can't submit bug to GCC bugzilla because it is rejecting new account creation. Consider a dll whose base address is overlapped with an existing module, so that the operating system must relocate it, or for another word, rebase it, to work. Relocation is the fundamental part of the security feature ASLR and is enabled by default since Windows Vista. When ASLR is enabled, all executables are relocated. BUT, After relocation, the compiled DLL fails to load at all. That's to say, the compiled DLL is broken. Not only x86-dw2 builds produces the broken DLL, but x86-sjlj, x64-sjlj, x64-seh, all gcc 6.2 builds with binutils 2.25.1 have the same problem. OH MY GOD. I believe previous builds also have the same problem. Minical case: ===================== testexe.c ===================== #include <stdio.h> __declspec(dllimport) int a(); int main() { a(); printf("hello world\n"); return 0; } ===================== testdll.c ===================== __declspec(dllexport) int a() {return 0;} ===================== test.def ===================== EXPORTS a ===================== makefile ===================== all: test.dll test.exe clean: rm test.exe test.dll libtest.a test.exe: testexe.c gcc -o $@ $^ libtest.a -Wl,--image-base,0x400000 test.dll: testdll.c dllwrap -o $@ $^ --def test.def --implib libtest.a -Wl,--image-base,0x400000 ===================== Case end. Also see the attachment. In this case, test.exe refuses to work with error 0xC0000005 because test.dll fails to load. I don't know what's going wrong, GCC or binutils? Nothing is wrong when using Microsoft Visual C++. It's really annoying to see GCC compiled programs showing 0xC0000005 frequently and randomly.
Hi Linda, > Binutils: 2.25.1 Have you tried using a more recent version of the binutils ? We are currently on release 2.27 and release 2.28 is about to happen. Cheers Nick
(In reply to Nick Clifton from comment #1) > Hi Linda, > > > Binutils: 2.25.1 > > Have you tried using a more recent version of the binutils ? We are > currently on release 2.27 and release 2.28 is about to happen. > > Cheers > Nick Hi Nick, Thank you for your reply, but I don't know how to build binutils for mingw so I can only wait for their builds. :( I even don't know whom to ask so I'm asking mingw now because it's their bundle. I found at least in lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o, some NULL pointers were accidently relocated thus lead to SEGFAULT. There may be more errors. Buggy builds from at least 4.8.x to 6.2.
Created attachment 9742 [details] Duplicate .reloc info
(In reply to Linda Zhang from comment #2) > (In reply to Nick Clifton from comment #1) > > Hi Linda, > > > > > Binutils: 2.25.1 > > > > Have you tried using a more recent version of the binutils ? We are > > currently on release 2.27 and release 2.28 is about to happen. > > > > Cheers > > Nick > > Hi Nick, > > Thank you for your reply, but I don't know how to build binutils for mingw > so I can only wait for their builds. :( > I even don't know whom to ask so I'm asking mingw now because it's their > bundle. > > I found at least in lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o, some NULL > pointers were accidently relocated thus lead to SEGFAULT. There may be more > errors. Buggy builds from at least 4.8.x to 6.2. Hi Nick, I think I found the reason. Binutils (it should be) produced duplicate .reloc info into the binary. The wrong one comes first, and the correct one comes after, resulting in double relocation. The new attached picture "bug.png" says all.
(In reply to Linda Zhang from comment #4) > (In reply to Linda Zhang from comment #2) > > (In reply to Nick Clifton from comment #1) > > > Hi Linda, > > > > > > > Binutils: 2.25.1 > > > > > > Have you tried using a more recent version of the binutils ? We are > > > currently on release 2.27 and release 2.28 is about to happen. > > > > > > Cheers > > > Nick > > > > Hi Nick, > > > > Thank you for your reply, but I don't know how to build binutils for mingw > > so I can only wait for their builds. :( > > I even don't know whom to ask so I'm asking mingw now because it's their > > bundle. > > > > I found at least in lib/gcc/i686-w64-mingw32/6.2.0/crtbegin.o, some NULL > > pointers were accidently relocated thus lead to SEGFAULT. There may be more > > errors. Buggy builds from at least 4.8.x to 6.2. > > Hi Nick, > > I think I found the reason. > > Binutils (it should be) produced duplicate .reloc info into the binary. The > wrong one comes first, and the correct one comes after, resulting in double > relocation. > > The new attached picture "bug.png" says all. OK, update, both gcc -shared and ld -shared are OK. The bug is confirmed to be DLLWRAP issues, which I see was deprecated in 2002......
Created attachment 9744 [details] Patch for dllwrap OK I saw dllwrap is not maintained for 15 years..... OMG! So I'd like to submit a patch to warn use of dllwrap to prevent further problems.....
Created attachment 12979 [details] Everything necessary for your test should be in the appendix. Thank you. Everything necessary for your test should be in the appendix. Thank you.
The master branch has been updated by Nick Clifton <nickc@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8881640c8071b04fa2566f21eb2a47e691caf11c commit 8881640c8071b04fa2566f21eb2a47e691caf11c Author: Linda Zhang <lindasc@qq.com> Date: Fri Nov 20 11:07:26 2020 +0000 Add a warning to dllwrap that it is deprecated. PR 20979 * dllwrap.c (main): Deprecate and warn the use of dllwrap.
Wow - sorry for letting this patch slip through, and thanks for the reminder. Patch applied.