[cfe-dev] Static linking a program

Wink Saville via cfe-dev cfe-dev at lists.llvm.org
Tue Jul 3 09:37:00 PDT 2018


So I thought I'd compile clang, llvm, compiler-rt, libc++ ... everything
but openmp.

I then compiled my main.cpp in dynamically and all was well:

$ PATH=/home/wink/prgs/llvm/dist/bin:$PATH clang++ -std=c++17 -v
-fuse-ld=lld -stdlib=libc++ -lc++abi -pthread main.cpp -o main
clang version 7.0.0 (git at github.com:llvm-mirror/clang.git
833ad29a88de7d88bd69234b5069bbfaea0235d1) (git at github.com:llvm-mirror/llvm
c55ef4741ac75872f0e692c87eac70745b3ce167)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/wink/prgs/llvm/dist/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Selected GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/home/wink/prgs/llvm/dist/bin/clang-7" -cc1 -triple
x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free
-disable-llvm-verifier -discard-value-names -main-file-name main.cpp
-mrelocation-model static -mthread-model posix -mdisable-fp-elim
-fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables
-fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb
-v -resource-dir /home/wink/prgs/llvm/dist/lib/clang/7.0.0
-internal-isystem /home/wink/prgs/llvm/dist/bin/../include/c++/v1
-internal-isystem /usr/local/include -internal-isystem
/home/wink/prgs/llvm/dist/lib/clang/7.0.0/include -internal-externc-isystem
/include -internal-externc-isystem /usr/include -std=c++17
-fdeprecated-macro -fdebug-compilation-dir
/home/wink/prgs/explore-cpp-static-linking -ferror-limit 19
-fmessage-length 114 -pthread -fobjc-runtime=gcc -fcxx-exceptions
-fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o
/tmp/main-09ff4c.o -x c++ main.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0svn default target
x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/wink/prgs/llvm/dist/bin/../include/c++/v1
 /usr/local/include
 /home/wink/prgs/llvm/dist/lib/clang/7.0.0/include
 /usr/include
End of search list.
 "/home/wink/prgs/llvm/dist/bin/ld.lld" --eh-frame-hdr -m elf_x86_64
-dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crt1.o
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crti.o
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/crtbegin.o
-L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
-L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64
-L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../..
-L/home/wink/prgs/llvm/dist/bin/../lib -L/lib -L/usr/lib -lc++abi
/tmp/main-09ff4c.o -lc++ -lm -lgcc_s -lgcc -lpthread -lc -lgcc_s -lgcc
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/crtend.o
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crtn.o

wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ ldd ./main
linux-vdso.so.1 (0x00007fffb3f27000)
libc++abi.so.1 => not found
libc++.so.1 => not found
libm.so.6 => /usr/lib/libm.so.6 (0x00007f2b3e244000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f2b3e02c000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f2b3de0e000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007f2b3da52000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2
(0x00007f2b3e5d9000)

wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ LD_LIBRARY_PATH=/home/wink/prgs/llvm/dist/lib ./main 1 2 3
argv[0]: ./main
argv[1]: 1
argv[2]: 2
argv[3]: 3



I then statically linked it and added --reproduce as it fails the same as
before:

wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ PATH=/home/wink/prgs/llvm/dist/bin:$PATH clang++ -std=c++17 -v
-fuse-ld=lld -stdlib=libc++ -lc++abi -pthread main.cpp -o main -static
-Wl,--reproduce=repro.tar
clang version 7.0.0 (git at github.com:llvm-mirror/clang.git
833ad29a88de7d88bd69234b5069bbfaea0235d1) (git at github.com:llvm-mirror/llvm
c55ef4741ac75872f0e692c87eac70745b3ce167)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/wink/prgs/llvm/dist/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/8.1.1
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Selected GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/home/wink/prgs/llvm/dist/bin/clang-7" -cc1 -triple
x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free
-disable-llvm-verifier -discard-value-names -main-file-name main.cpp
-static-define -mrelocation-model static -mthread-model posix
-mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases
-munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info
-debugger-tuning=gdb -v -resource-dir
/home/wink/prgs/llvm/dist/lib/clang/7.0.0 -internal-isystem
/home/wink/prgs/llvm/dist/bin/../include/c++/v1 -internal-isystem
/usr/local/include -internal-isystem
/home/wink/prgs/llvm/dist/lib/clang/7.0.0/include -internal-externc-isystem
/include -internal-externc-isystem /usr/include -std=c++17
-fdeprecated-macro -fdebug-compilation-dir
/home/wink/prgs/explore-cpp-static-linking -ferror-limit 19
-fmessage-length 114 -pthread -fobjc-runtime=gcc -fcxx-exceptions
-fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o
/tmp/main-35f7e5.o -x c++ main.cpp
clang -cc1 version 7.0.0 based upon LLVM 7.0.0svn default target
x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/wink/prgs/llvm/dist/bin/../include/c++/v1
 /usr/local/include
 /home/wink/prgs/llvm/dist/lib/clang/7.0.0/include
 /usr/include
End of search list.
 "/home/wink/prgs/llvm/dist/bin/ld.lld" --eh-frame-hdr -m elf_x86_64
-static -o main
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crt1.o
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crti.o
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/crtbeginT.o
-L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1
-L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64
-L/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../..
-L/home/wink/prgs/llvm/dist/bin/../lib -L/lib -L/usr/lib -lc++abi
/tmp/main-35f7e5.o --reproduce=repro.tar -lc++ -lm --start-group -lgcc
-lgcc_eh -lpthread -lc --end-group
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/crtend.o
/usr/lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../lib64/crtn.o

wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ ldd ./main
not a dynamic executable

wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ ./main 1 2 3
Segmentation fault (core dumped)

wink at wink-desktop:~/prgs/explore-cpp-static-linking (master)
$ coredumpctl gdb
           PID: 30644 (main)
           UID: 1000 (wink)
           GID: 100 (users)
        Signal: 11 (SEGV)
     Timestamp: Tue 2018-07-03 09:31:18 PDT (4s ago)
  Command Line: ./main 1 2 3
    Executable: /home/wink/prgs/explore-cpp-static-linking/main
 Control Group: /user.slice/user-1000.slice/session-c2.scope
          Unit: session-c2.scope
         Slice: user-1000.slice
       Session: c2
     Owner UID: 1000 (wink)
       Boot ID: a39916a44af64ce3836e760848efaaac
    Machine ID: 8f80fd742eae4659baed812cd07a9439
      Hostname: wink-desktop
       Storage:
/var/lib/systemd/coredump/core.main.1000.a39916a44af64ce3836e760848efaaac.30644.1530635478000000.lz4
       Message: Process 30644 (main) of user 1000 dumped core.

                Stack trace of thread 30644:
                #0  0x0000000000376736 n/a
(/home/wink/prgs/explore-cpp-static-linking/main)

GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/wink/prgs/explore-cpp-static-linking/main...done.
[New LWP 30644]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Core was generated by `./main 1 2 3'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000376736 in _dl_get_origin ()
(gdb) bt
#0  0x0000000000376736 in _dl_get_origin ()
#1  0x000000000037846f in _dl_non_dynamic_init ()
#2  0x00000000002cd7e1 in __libc_init_first ()
#3  0x00000000002cd4d7 in __libc_start_main ()
#4  0x000000000025302a in _start ()
(gdb)


I've attached repo-master.tar.bzip2, let me know what else
I might be able to do to diagnose the problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180703/fed1b236/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: repro-master.tar.bzip2
Type: application/octet-stream
Size: 6398857 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180703/fed1b236/attachment.obj>


More information about the cfe-dev mailing list