<div dir="ltr"><div>Just to make sure this is NOT the issue: You are compiling and running "main" on the same machine [physical, virtual or docker], and the compiler & libraries you use are built for/on the machine you are running "main" on? Not copying [or sharing over network filesystem] so that "main" is being run on a different version of Linux? I've seen similar issues from trying to use "wrong" versions of library on different versions/variants of Linux (presumably because system calls, ABI or something else changed between Release X of Linux A and Release Y of Linux B).<br><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 27 June 2018 at 07:44, Rui Ueyama via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">It works for me, but there might be some bug in lld that occurs in your environment but not in mine. Could you run the same command line with -Wl,--reproduce=repro.tar and share a resulting repro.tar with me? repro.tar contanis all input files fed to the linker, so I can run the same command with the same input as yours on my machine.</div><div class="HOEnZb"><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Wed, Jun 27, 2018 at 2:59 PM Wink Saville via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks everyone, using "clang++ -v -static main.cpp" as a model worked,<br>
see: <a href="https://github.com/winksaville/explore-cpp-static-linking" rel="noreferrer" target="_blank">https://github.com/<wbr>winksaville/explore-cpp-<wbr>static-linking</a><br>
<br>
But a curious thing, if I ask to use lld linker it compiles and links<br>
fine, but it<br>
fails with Segmentation fault. What have I done wrong now?<br>
<br>
$ clang++ -v -fuse-ld=lld -static main.cpp -o main<br>
clang version 6.0.0 (tags/RELEASE_600/final)<br>
Target: x86_64-pc-linux-gnu<br>
Thread model: posix<br>
InstalledDir: /usr/bin<br>
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-<wbr>linux-gnu/8.1.1<br>
Found candidate GCC installation:<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1<br>
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-<wbr>gnu/8.1.1<br>
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-<wbr>linux-gnu/8.1.1<br>
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1<br>
Candidate multilib: .;@m64<br>
Candidate multilib: 32;@m32<br>
Selected multilib: .;@m64<br>
 "/usr/bin/clang-6.0" -cc1 -triple x86_64-pc-linux-gnu -emit-obj<br>
-mrelax-all -disable-free -disable-llvm-verifier -discard-value-names<br>
-main-file-name main.cpp -static-define -mrelocation-model pic<br>
-pic-level 2 -pic-is-pie -mthread-model posix -mdisable-fp-elim<br>
-fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables<br>
-fuse-init-array -target-cpu x86-64 -dwarf-column-info<br>
-debugger-tuning=gdb -v -resource-dir /usr/lib/clang/6.0.0<br>
-internal-isystem<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/include/c++/8.1.1<br>
-internal-isystem<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/include/c++/8.1.1/x86_64-pc-<wbr>linux-gnu<br>
-internal-isystem<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/include/c++/8.1.1/backward<br>
-internal-isystem /usr/local/include -internal-isystem<br>
/usr/lib/clang/6.0.0/include -internal-externc-isystem /include<br>
-internal-externc-isystem /usr/include -fdeprecated-macro<br>
-fdebug-compilation-dir /home/wink/prgs/explore-cpp-<wbr>static-linking<br>
-ferror-limit 19 -fmessage-length 121 -stack-protector 2<br>
-fobjc-runtime=gcc -fcxx-exceptions -fexceptions<br>
-fdiagnostics-show-option -fcolor-diagnostics -o /tmp/main-e6f2d0.o -x<br>
c++ main.cpp<br>
clang -cc1 version 6.0.0 based upon LLVM 6.0.0 default target<br>
x86_64-pc-linux-gnu<br>
ignoring nonexistent directory "/include"<br>
#include "..." search starts here:<br>
#include <...> search starts here:<br>
 /usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/include/c++/8.1.1<br>
 /usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/include/c++/8.1.1/x86_64-pc-<wbr>linux-gnu<br>
 /usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/include/c++/8.1.1/backward<br>
 /usr/local/include<br>
 /usr/lib/clang/6.0.0/include<br>
 /usr/include<br>
End of search list.<br>
 "/usr/bin/ld.lld" -m elf_x86_64 -static -o main<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/lib64/crt1.o<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/lib64/crti.o<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/crtbeginT.o<br>
-L/usr/bin/../lib64/gcc/x86_<wbr>64-pc-linux-gnu/8.1.1<br>
-L/usr/bin/../lib64/gcc/x86_<wbr>64-pc-linux-gnu/8.1.1/../../..<wbr>/../lib64<br>
-L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64<br>
-L/usr/bin/../lib64/gcc/x86_<wbr>64-pc-linux-gnu/8.1.1/../../..<br>
-L/usr/bin/../lib -L/lib -L/usr/lib /tmp/main-e6f2d0.o -lstdc++ -lm<br>
--start-group -lgcc -lgcc_eh -lc --end-group<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/crtend.o<br>
/usr/bin/../lib64/gcc/x86_64-<wbr>pc-linux-gnu/8.1.1/../../../..<wbr>/lib64/crtn.o<br>
<br>
$ ./main 1 2 3<br>
Segmentation fault (core dumped)<br>
<br>
$ coredumpctl gdb<br>
           PID: 5870 (main)<br>
           UID: 1000 (wink)<br>
           GID: 100 (users)<br>
        Signal: 11 (SEGV)<br>
     Timestamp: Tue 2018-06-26 22:40:56 PDT (6s ago)<br>
  Command Line: ./main 1 2 3<br>
    Executable: /home/wink/prgs/explore-cpp-<wbr>static-linking/main<br>
 Control Group: /user.slice/user-1000.slice/<wbr>session-c2.scope<br>
          Unit: session-c2.scope<br>
         Slice: user-1000.slice<br>
       Session: c2<br>
     Owner UID: 1000 (wink)<br>
       Boot ID: 65910f0244bf4b62905c0198842b62<wbr>d4<br>
    Machine ID: 8f80fd742eae4659baed812cd07a94<wbr>39<br>
      Hostname: wink-desktop<br>
       Storage:<br>
/var/lib/systemd/coredump/<wbr>core.main.1000.<wbr>65910f0244bf4b62905c0198842b62<wbr>d4.5870.1530078056000000.lz4<br>
       Message: Process 5870 (main) of user 1000 dumped core.<br>
<br>
                Stack trace of thread 5870:<br>
                #0  0x0000000000383dc6 n/a<br>
(/home/wink/prgs/explore-cpp-<wbr>static-linking/main)<br>
<br>
GNU gdb (GDB) 8.1<br>
Copyright (C) 2018 Free Software Foundation, Inc.<br>
License GPLv3+: GNU GPL version 3 or later <<a href="http://gnu.org/licenses/gpl.html" rel="noreferrer" target="_blank">http://gnu.org/licenses/gpl.<wbr>html</a>><br>
This is free software: you are free to change and redistribute it.<br>
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"<br>
and "show warranty" for details.<br>
This GDB was configured as "x86_64-pc-linux-gnu".<br>
Type "show configuration" for configuration details.<br>
For bug reporting instructions, please see:<br>
<<a href="http://www.gnu.org/software/gdb/bugs/" rel="noreferrer" target="_blank">http://www.gnu.org/software/<wbr>gdb/bugs/</a>>.<br>
Find the GDB manual and other documentation resources online at:<br>
<<a href="http://www.gnu.org/software/gdb/documentation/" rel="noreferrer" target="_blank">http://www.gnu.org/software/<wbr>gdb/documentation/</a>>.<br>
For help, type "help".<br>
Type "apropos word" to search for commands related to "word"...<br>
Reading symbols from<br>
/home/wink/prgs/explore-cpp-<wbr>static-linking/main...(no debugging<br>
symbols found)...done.<br>
[New LWP 5870]<br>
Core was generated by `./main 1 2 3'.<br>
Program terminated with signal SIGSEGV, Segmentation fault.<br>
#0  0x0000000000383dc6 in _dl_get_origin ()<br>
(gdb) bt<br>
#0  0x0000000000383dc6 in _dl_get_origin ()<br>
#1  0x000000000038342f in _dl_non_dynamic_init ()<br>
#2  0x00000000002eecd1 in __libc_init_first ()<br>
#3  0x00000000002ee9c7 in __libc_start_main ()<br>
#4  0x000000000025302a in _start ()<br>
<br>
On Tue, Jun 26, 2018 at 2:58 PM, Joerg Sonnenberger via cfe-dev<br>
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
> On Tue, Jun 26, 2018 at 09:36:18PM +0000, Shoaib Meenai via cfe-dev wrote:<br>
>> I do see that you're linking all the start files yourself, but the<br>
>> ordering can also make a difference.<br>
><br>
> crt0.o / crt1.o was missing.<br>
><br>
> Joerg<br>
> ______________________________<wbr>_________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</blockquote></div>
</div></div><br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>