>Submitter-Id: net >Originator: Nathan Myers >Organization: ITA Software, Inc. >Confidential: no >Synopsis: array static-init failure under -fpic, -fPIC >Severity: serious >Priority: medium >Category: c++ >Class: wrong-code >Release: 3.3.2 20030812 (Debian prerelease) (Debian testing/unstable) >Environment: System: Linux ipc 2.4.21-rc1-ac2 #3 SMP Mon Apr 28 18:55:42 EDT 2003 i686 unknown unknown GNU/Linux Architecture: i686
host: i486-pc-linux-gnu build: i486-pc-linux-gnu target: i486-pc-linux-gnu configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux >Description: Static linker initialization fails to initialize struct members, under hard-to-describe circumstances. Specifically: in a static array initialization where array elements are structs, and where some struct members are implicitly initialized to zero, certain struct members that are *explicitly* initialized get zero-initialized anyhow. To reproduce the failure, it appears that: o "-fpic" or "-fPIC" is necessary (but we have failures w/o -fpic). o letting the compiler implicitly zero-initialize at least one struct member in all elements is necessary. o a forward-reference to the initialized value is necessary (or a string literal). o fewer than five array elements is necessary (but we have seen it with more). Under "-O0" the failure does not seem to occur. In gcc-3.0 the failure does not seem to occur. In gcc-3.4 snapshots the failure *does* still occur. >How-To-Repeat: Paste lines below into a shell. Correct output is 1 This is a message 2 This is a message Erroneous output looks like 1 This is a message 2 <NULL> ---------------- sed 's/^ //' >initbug.cc <<' EOF' // Compile this with 'g++ -O1 -fpic', and see what happens // compiled as C, the problem does not occur. #include <stdio.h> struct StringHolder { int m_index; char const * const message_pointer; int extra; // this member is necessary to expose bug }; extern char const * const Message; StringHolder array_of_holders[] = { { 1, Message }, // default-initializing Stringholder::extra { 2, Message }, { -1, 0 } }; int main() { StringHolder* ptr = array_of_holders; while(ptr->m_index >= 0) { printf("%d\t", ptr->m_index); if (ptr->message_pointer == NULL) printf("<NULL>\n"); else printf("%s\n", ptr->message_pointer); ++ptr; } } char const * const Message = "This is a message"; EOF g++ -O1 -fpic initbug.cc ./a.out >Fix: