[Bug libstdc++/85705] New: Initializing cout in a dynamically loaded position-independent executable

gcc at foxcub dot org gcc-bugzilla@gcc.gnu.org
Tue May 8 19:05:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85705

            Bug ID: 85705
           Summary: Initializing cout in a dynamically loaded
                    position-independent executable
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at foxcub dot org
  Target Milestone: ---

Created attachment 44091
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44091&action=edit
main.cpp

I build a position-independent executable (puppet.cpp attached), dynamically
load it via dlopen, get its main via dlsym, and call it. This works fine, as
long as this PIE doesn't use iostream, specifically cout. If it does, the code
segfaults with the stack trace below.

If I do roughly the same thing, but compile puppet.cpp as a shared library
(puppet-lib.cpp, renaming main to f), everything works fine.

The sources are attached. Backtrace is below.

This problem occurs only on Linux; on a Mac this works fine.

The culprit seems to be the initialization of cout. Its addresses reported from
main.cpp and puppet-lib.cpp are the same, but it differs in puppet.cpp.

Is there a way to work around this problem?

Thanks.
Dmitriy


Output:
0x55f327a180e0
Hello from main
0x55f327a180e0
Hello from shared library puppet
0x7f1d966e5060
[1]    11996 segmentation fault (core dumped)  ./main

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff795da56 in std::ostream::sentry::sentry (this=0x7fffffffe070,
__os=...) at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:46
46     
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:
No such file or directory.
(gdb) back
#0  0x00007ffff795da56 in std::ostream::sentry::sentry (this=0x7fffffffe070,
__os=...) at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream.tcc:46
#1  0x00007ffff795e109 in std::__ostream_insert<char, std::char_traits<char> >
(__out=..., __s=__s@entry=0x7ffff6ce0bf9 "Hello from puppet", __n=17)
    at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ostream_insert.h:76
#2  0x00007ffff795e5a9 in std::operator<< <std::char_traits<char> > (__out=...,
__s=0x7ffff6ce0bf9 "Hello from puppet")
    at
/build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:320
#3  0x00007ffff6ce0aef in main (argc=0, argv=0x0) at puppet.cpp:6
#4  0x00005555555551ca in main () at main.cpp:43


More information about the Gcc-bugs mailing list