When a header(A) is precompiled and included from another header(B), compiling a source file using B results in invalid assembly. The message from the assembler is "file number X already allocated".
Created attachment 17192 [details] Test case for precompiled header inclusion This test case is a simple scenario where a precompiled header is included by another header. Run 'cause-bug.sh' to precompile a header and then attempt to build a source indirectly using that header-
CXX=g++-4.3 ./cause-error.sh In file included from <built-in>:0: <built-in>:0: internal compiler error: Segmentation fault Please submit a full bug report, with preprocessed source if appropriate. See <http://bugs.opensuse.org/> for instructions. This looks related to https://bugzilla.novell.com/show_bug.cgi?id=432433 and https://bugzilla.novell.com/show_bug.cgi?id=444153 -> PCH is broken wrt location information.
No ICE here (gcc version 4.2.4 (Gentoo 4.2.4 p1.0)), but the underlying cause might be the same.
I'm fairly sure it's the same underlying problem. The emitted file numbers (in the .s file for debug info) are in garbage collected memory. Hence the directives emitted when the PCH is not yet loaded and those emitted after it's loaded might use the same file number. In this case you use -g3, meaning to emit directives for defined macros. These are emitted (obviously) as soon as the include directive is seen. This will allocate numbers 1 and 2 for main.cpp and main.h. Then the PCH file is loaded, overwriting the internally (in dwarf2out.c:struct dwarf_file_data) stored file numbers, so we start counting from 2 again (we overwrite only data from after the main file), which is given to main.cpp again. Hence the assembler gives the error message. Later compilers simply segfault already earlier in the process, because location info is also overwritten but still referenced, as detailed in the https://bugzilla.novell.com/show_bug.cgi?id=444153 . Also, as detailed there, it seems to be a fundamental design choice that the PCH has to be included from the main file, as the PCH machinery uses the garbage collection engine to actually retrieve the memory state like it was when precompiling the file. This obviously can only work if no stale references exist to then overwritten information and this in turn can only be ensured when there isn't "too much" information before loading the PCH file at all. No idea how easy this would be to fix, from a cursory look at the whole machinery it looks quite difficult (at least if one wants to really fix this, and not just work-around the cases that people happen to hit)
This is a preprocessor bug. We should not use PCHs when it is not the very first preprocessing directive in the TU that opens it. Thus, doc/invoke.texi " @item A precompiled header can't be used once the first C token is seen. You can have preprocessor directives before a precompiled header; you can even include a precompiled header from inside another header, so long as there are no C tokens before the @code{#include}. " is very overly optimistic about conditions a PCH is valid. Tom, do you have an idea where to best restrict the use of PCHs this way?
Arguably, that makes precompiled headers less useful - in scenarios where you have two big libraries with complicated headers you can at most only use PCHs for one of them. Anyhow, that issue is a separate matter.
Subject: Re: Including a precompiled header from another header causes invalid assembly to be generated On Tue, 24 Feb 2009, frank dot richter at gmail dot com wrote: > ------- Comment #6 from frank dot richter at gmail dot com 2009-02-24 14:50 ------- > Arguably, that makes precompiled headers less useful - in scenarios where you > have two big libraries with complicated headers you can at most only use PCHs > for one of them. Anyhow, that issue is a separate matter. Using more than one PCH was never possible. Richard.
*** Bug 39854 has been marked as a duplicate of this bug. ***
Subject: Bug 38987 Author: rguenth Date: Tue Sep 22 08:37:31 2009 New Revision: 151970 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=151970 Log: 2009-09-22 Richard Guenther <rguenther@suse.de> PR pch/38987 * files.c (pch_open_file): Disallow non-toplevel PCH inclusion. Modified: trunk/libcpp/ChangeLog trunk/libcpp/files.c
Fixed.
*** Bug 40272 has been marked as a duplicate of this bug. ***
Author: rguenth Date: Wed Mar 7 09:55:26 2012 New Revision: 185029 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=185029 Log: 2012-03-07 Richard Guenther <rguenther@suse.de> PR pch/52518 PR pch/38987 * doc/invoke.texi (Precompiled Headers): Remove sentence that suggests you can include PCHs from inside another header. Modified: trunk/gcc/ChangeLog trunk/gcc/doc/invoke.texi
*** Bug 64414 has been marked as a duplicate of this bug. ***