Open Bug 779649 Opened 14 years ago Updated 4 years ago

MinGW build of NSPR fails to link due to _Interlock* intrinsics

Categories

(NSPR :: NSPR, defect)

4.9.2
defect

Tracking

(Not tracked)

People

(Reporter: matt, Unassigned)

Details

Attachments

(1 file, 1 obsolete file)

User Agent: Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1 Build ID: 20120717190829 Steps to reproduce: Installed latest version of MinGW compiler + MSYS. Followed instructions found here: https://developer.mozilla.org/en/NSPR_build_instructions Configured with: ../gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1- with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.7.0 (GCC) matthew@mattb-PC /D/nspr-4.9.2/build $ ../mozilla/nsprpub/configure --enable-win32-target=WIN95 matthew@mattb-PC /D/nspr-4.9.2/build $ make > build.results.txt 2>&1 Actual results: threads/./prtpd.o: In function `PR_NewThreadPrivateIndex': d:\nspr-4.9.2\build\pr\src\threads/../../../../mozilla/nsprpub/pr/src/threads/prtpd.c:108: undefined reference to `__InterlockedIncrement' misc/./prinit.o: In function `PR_CallOnce': d:\nspr-4.9.2\build\pr\src\misc/../../../../mozilla/nsprpub/pr/src/misc/prinit.c:774: undefined reference to `__InterlockedExchange' misc/./prinit.o: In function `PR_CallOnceWithArg': d:\nspr-4.9.2\build\pr\src\misc/../../../../mozilla/nsprpub/pr/src/misc/prinit.c:803: undefined reference to `__InterlockedExchange' threads/combined/./pruthr.o: In function `_PR_CreateThread': d:\nspr-4.9.2\build\pr\src\threads\combined/../../../../../mozilla/nsprpub/pr/sr c/threads/combined/pruthr.c:1137: undefined reference to `__InterlockedIncrement' d:\nspr-4.9.2\build\pr\src\threads\combined/../../../../../mozilla/nsprpub/pr/src/threads/combined/pruthr.c:1139: undefined reference to `__InterlockedIncrement' collect2.exe: error: ld returned 1 exit status make[3]: *** [nspr4.dll] Error 1 Expected results: It seems like pratom.h has an error in it. MinGW defines _WIN32, but does not define _MSC_VER so the #if condition is satisfied, and goes on to process lines 84 to 87 of pratom.h. The intrinsic versions of these atomic operations are defined in the header, however, MinGW seems to be treating them as a regular C function, and appends an understore to the name, as seen in the linker errors mentioned.
I forgot to mention, the build was performed under Windows 7 Professional, with MSYS.
Jacek: could you take a look at this bug report? Do you know why Matt can't build NSPR with MinGW? Thanks.
Matt, it seems like you compile with plain mingw, not mingw-w64. I've just checked it and plain mingw missing proper support for _Interlocked* intrinsics. The problem here is following: In Windows, both Interlocked* and _Interlocked* functions exist. Not underscored version is exported from kernel32.dll and that's about it. Headers, instead of declaring it, define InterlockedIncrement to _InterlockedIncrement, which is an intrinsic. That's true on MSVC and mingw-w64 (which doesn't have it as an intrinsic, but uses an inline function to emulate the behavior). Yet plain mingw does it the wrong way and just declares InterlockedInrement, so there is no support for underscored version. A few words about mingw-w64. It's a fork of original mingw. The name may be confusing, but it supports both 32-bit and 64-bit targets. It improves quickly, while plain mingw is stagnated and far less advanced than mingw-w64. Currently more and more people are switching to mingw-w64 (eg. Fedora completely replaced one by another, Cygwin is on its way). That said, the problem would be best solved in plain mingw, since it's their incompatibility that causes problems here. Otherwise, if we want to fix it on nspr end, we need to use not underscored variant of Interlocked* functions or default PR_Atomic* variant on plain mingw (AFAIR PR_Atomic* variant caused problems with compiling Firefox at some point due to type incompatibilities, but Firefox compilation with plain mingw is hopeless, while mingw-w64 does it quite well).
Thanks for the information about MinGW-w64, I'll switch to that in the mean time. Is it reasonable to assume that all compilers targeting Windows will implement MSVC itrinsics? My opinion is that it would be good to support the Interlocked* APIs as a fallback if certain compilers that support intrinsics aren't detected. (ie: _WIN32 && !_MSC_VER)
I'd say it's sane to expect standard, basic features of WIN32 to be supported in sane toolchains... As for the fix for plain mingw, the cleanest way IMO would be including winbase.h in pratom.h and always use not underscored versions (underscored ones would be still used on MSVC and mingw-w64, but that would be handled by macros from winbase.h). This would get rid of rather hackish solution of declaring functions ourselves, but I guess there was a reason it wasn't done this way originally.
Jacek: in the early days of NSPR, we discovered that the inclusion of <windows.h> in public NSPR headers slowed down the compilation of NSPR-based files significantly. So we removed any inclusion of <windows.h> from public NSPR headers. I suspect this policy is still maintained today. Re: winbase.h: Microsoft documentation on MSDN always recommends including <windows.h> rather than a smaller <winxxx.h> header. So I am not sure if it's a good idea to include <winbase.h>.
Status: UNCONFIRMED → NEW
Ever confirmed: true
(In reply to Wan-Teh Chang from comment #6) > Jacek: in the early days of NSPR, we discovered that the inclusion of > <windows.h> > in public NSPR headers slowed down the compilation of NSPR-based files > significantly. So we removed any inclusion of <windows.h> from public NSPR > headers. I suspect this policy is still maintained today. Yeah, including windows.h pulls a lot of useless stuff, winbase.h alone is pretty light. > Re: winbase.h: Microsoft documentation on MSDN always recommends including > <windows.h> rather than a smaller <winxxx.h> header. So I am not sure if > it's a good idea to include <winbase.h>. Well, msdn is full of misleading information... I worked quite a lot with those headers and I've never seen a problem with it. In Wine we used #include <windef.h> #include <winbase.h> instead of windows.h to make compilation faster for years. It seems common enough that Microsoft has to keep backward compatibility. But I understand the decision to avoid it. I'll prepare a patch tomorrow.
I've attached a patch using GCC builtin __sync operations on mingw, just like it's done for other GCC targets. I didn't add additional arch checks like __APPLE__ and __linux__ cases because for them to matter, mingw would have to be an extreme corner case. Similar solution was proposed in bug 672035, but now the context has changed and the rest of m-c code works fine with it. Matt, can you please test it in your configuration (I've tested only with mingw-w64 for 32-bit and 64-bit targets).
Attached patch patch (obsolete) — — Splinter Review
Really attached this time
Comment on attachment 650847 [details] [diff] [review] patch Jacek: thank you for the patch. If you think it's better to use the GCC builtin __sync operations instead, we should change the _MD_ATOMIC_xxx macro definitions in _win95.h to match: http://bonsai.mozilla.org/cvsblame.cgi?file=mozilla/nsprpub/pr/include/md/_win95.h&rev=3.41&mark=308-310,312-314,316#306 (The _MD_ATOMIC_xxx macro definitions in _winnt.h also need to be updated in the same way to support the WINNT build configuration. I can take care of that.) This is due to the issue described in the comment below: > ** IMPORTANT NOTE TO NSPR MAINTAINERS: > ** Implement these macros with compiler intrinsics only on platforms > ** where the PR_AtomicXXX functions are truly atomic (i.e., where the > ** configuration macro _PR_HAVE_ATOMIC_OPS is defined). Otherwise, > ** the macros and functions won't be compatible and can't be used > ** interchangeably. I think the GCC builtin __sync operations should be compatible with the Windows Interlocked functions, but it seems safer to use the same set of atomic functions.
It seems this was proposed before? See https://bugzilla.mozilla.org/show_bug.cgi?id=672035 The patch did not work with: matthew@mattb-PC /d/nspr-4.9.2/build $ ../mozilla/nsprpub/configure --enable-win32-target=WIN95 Creating library file: ./nspr4.a threads/./prtpd.o:prtpd.c:(.text+0x4c): undefined reference to `___sync_add_and_ fetch_4' collect2.exe: error: ld returned 1 exit status But it does work with: matthew@mattb-PC /d/nspr-4.9.2/build $ ../mozilla/nsprpub/configure --with-arch=i486 --enable-win32-target=WIN95
Attached patch patch — — Splinter Review
Attachment #650847 - Attachment is obsolete: true
(In reply to Wan-Teh Chang from comment #10) > Comment on attachment 650847 [details] [diff] [review] > patch > > Jacek: thank you for the patch. > > If you think it's better to use the GCC builtin __sync operations instead, > we should change the _MD_ATOMIC_xxx macro definitions in _win95.h to match: > > http://bonsai.mozilla.org/cvsblame.cgi?file=mozilla/nsprpub/pr/include/md/ > _win95.h&rev=3.41&mark=308-310,312-314,316#306 > > (The _MD_ATOMIC_xxx macro definitions in _winnt.h also need to be updated > in the same way to support the WINNT build configuration. I can take care > of that.) > > This is due to the issue described in the comment below: > > > ** IMPORTANT NOTE TO NSPR MAINTAINERS: > > ** Implement these macros with compiler intrinsics only on platforms > > ** where the PR_AtomicXXX functions are truly atomic (i.e., where the > > ** configuration macro _PR_HAVE_ATOMIC_OPS is defined). Otherwise, > > ** the macros and functions won't be compatible and can't be used > > ** interchangeably. > > I think the GCC builtin __sync operations should be compatible with the > Windows Interlocked functions, but it seems safer to use the same set > of atomic functions. Thank you for review. I've changed the patch to fix both _win95.h, _winnt.h and ntmisc.c. In ntmisc.c, I've got rid of the assembly code for GCC and assume that mingw always supports it (if it doesn't, that's a GCC bug). I think it's time to also reconsider MSVC variant. From the comment: * We don't use InterlockedIncrement and InterlockedDecrement * because on NT 3.51 and Win95, they return a number with * the same sign as the incremented/decremented result, rather * than the result itself. On NT 4.0 these functions do return * the incremented/decremented result. I don't know the exact minimal supported platform versions, but NT 3.51/win95 don't seem worth the trouble those days...
(In reply to Matt Boney from comment #11) > It seems this was proposed before? See > https://bugzilla.mozilla.org/show_bug.cgi?id=672035 Yes, I was opposed back then for a few reasons: - that bug was part of the series that tried to add support for plain mingw in the whole m-c tree, without fixing upstream, which was unrealistic - I though mingw-w64 will take plain mingw.org's user base much faster, so that plain mingw.org wouldn't be worth the trouble - I didn't know about any interest in alone (as in not as part of Firefox) build of NSPR on plain mingw (and this bug represents such interest) > The patch did not work with: > > matthew@mattb-PC /d/nspr-4.9.2/build > $ ../mozilla/nsprpub/configure --enable-win32-target=WIN95 > > Creating library file: ./nspr4.a > threads/./prtpd.o:prtpd.c:(.text+0x4c): undefined reference to > `___sync_add_and_ > fetch_4' > collect2.exe: error: ld returned 1 exit status > > But it does work with: > > matthew@mattb-PC /d/nspr-4.9.2/build > $ ../mozilla/nsprpub/configure --with-arch=i486 --enable-win32-target=WIN95 Thanks for testing. It seems like compiling without --with-arch=i486 falls into the situation described in GCC docs (http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html): > Not all operations are supported by all target processors. If a particular operation cannot > be implemented on the target processor, a warning will be generated and a call an external > function will be generated. The external function will carry the same name as the builtin, > with an additional suffix `_n' where n is the size of the data type. Your toolchain should provide implementations for these functions if it claims to support older arch. If it doesn't, that's really mingw's fault and it should be fixed there. I'm tempted to ignore it on NSPR side - you may always use --with-arch=i486. I've tested the patch with mingw-w64 and it works both with WIN95 and WINNT targets. Can you please test the new version with plain mingw.
Jacek, thanks for the patch. 1. Built fine with --with-arch=i486 using plain MinGW and attachment 651339 [details] [diff] [review]. 2. Ran the unit tests, and the atomic test passed.
Attachment #651339 - Flags: review?(wtc)

The bug assignee is inactive on Bugzilla, so the assignee is being reset.

Assignee: wtc → nobody
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: