Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault with static build #32

Closed
Kunzol opened this issue Jan 16, 2019 · 3 comments
Closed

Segmentation fault with static build #32

Kunzol opened this issue Jan 16, 2019 · 3 comments
Labels
Help Wanted Extra attention is needed

Comments

@Kunzol
Copy link

Kunzol commented Jan 16, 2019

I tried to build oatpp static (need it for a test in qemu for arm) but accessing it throws a segmentation fault.

For testing I did it on Linux amd64 with g++.

I did the build according to #18 building the static liboatpp.a with cmake:

cd lib/oatpp
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DOATPP_INSTALL=OFF -DOATPP_BUILD_TEST=on
make

This creates the liboatpp.a in the build directory.
Now I use the oatpp-starter (in src) for testing the lib.

g++ -std=gnu++11 \
-pthread \
-I "./lib" \
-I "./src" \
-L "./lib/oatpp/build" \
-D OATPP_USE_TARGET \
-D OATPP_TARGET_APP \
-D OATPP_DISABLE_ENV_OBJECT_COUNTERS \
-O2 \
-Wall \
-static \
./src/App.cpp \
-o starter_static \
-l oatpp

Which creates the static binary of the oatpp-starter as expected.

$ file starter_static 
starter_static: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=95769d0e5026886e36ffd21eed915e0ec6748f3c, not stripped

Running it works, but as soon as I try to access it, I get the sementation fault.
To find out more, I run it in gdb ...

$ gdb ./starter_static
...
Reading symbols from ./starter_static...done.
(gdb) run
Starting program: .../starter_static
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Server:Running on port 8000...
[New Thread 0x7ffff7ff9700 (LWP 2542)]

Thread 1 "starter_static" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00000000004b1605 in std::thread::detach() ()
#2  0x0000000000428e9a in oatpp::web::server::HttpConnectionHandler::handleConnection(std::shared_ptr<oatpp::data::stream::IOStream> const&) ()
#3  0x000000000041889c in oatpp::network::server::Server::mainLoop() ()
#4  0x0000000000401b25 in run() ()
#5  0x0000000000400748 in main ()

The oatpp-starter from the same source compiled with shared libraries works as expected.

$ file starter_shared 
starter_shared: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=89d8576e292cc5aad7c51c3c98c1cdc5cdfa005f, not stripped
$ ldd starter_shared 
        linux-vdso.so.1 =>  (0x00007ffd7e7c4000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb36588a000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb365674000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb365457000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb36508d000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb364d84000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb365c0c000)

Any ideas how to solve the problem with the static compile?

@lganzzzo
Copy link
Member

Hello @Kunzol
From what I found about the issue:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590#c3
With a bit more digging I traced the problem to /usr/include/c++/4.7.0/x86_64-linux-gnu/bits/gthr-posix.h declaring all of its symbols with attribute((weakref)) even when linking statically. Which caused the _gthread symbols to resolve to zero. I found this for some background info http://gcc.gnu.org/ml/gcc/2005-10/msg00235.html*

Also about it here:
https://stackoverflow.com/questions/7090623/c0x-thread-static-linking-problem/31271886#31271886

Some workarounds are:

  1. enable -Wl,--whole-archive flag
    You can try link you app with the following flags:
    -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
  2. use dynamic linking

Regards,
Leonid

@Kunzol
Copy link
Author

Kunzol commented Jan 16, 2019

Cool, thanks !!
It works now!!

I am not a developer (actually I am Sysadmin and need to get code of others running), thus I still have to learn more about cmake and its options. I missed to build libatopp.a with the debug symbols. After finding out how to create it with debug symbols I came the solution nearer. But without your hint, I guess I couldn't find the solution.

To make it complete, here is the complete compile command I used for building it static.

g++ -std=gnu++11 \
-I "./lib" \
-I "./src" \
-L "./lib/oatpp/build" \
-D OATPP_USE_TARGET \
-D OATPP_TARGET_APP \
-D OATPP_DISABLE_ENV_OBJECT_COUNTERS \
-O2 \
-Wall \
-static \
./src/App.cpp \
-o run_app \
-l oatpp \
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive

I promise I will not use it in a production environment :-)

@Kunzol Kunzol closed this as completed Jan 16, 2019
@lganzzzo
Copy link
Member

You are welcome
I am glad to help

@lganzzzo lganzzzo added the Help Wanted Extra attention is needed label Feb 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants