title: Why is __attribute__((constructor)) in my shared library not working?
body: <p>I have this code in ctor.c: </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  static void __attribute__((constructor)) ctor() {
          printf("HAHA");
  }
  </code></pre>
  
  <p>I compile it with: </p>
  
  <pre><code>clang -o shared.so -fPIC ctor.c -shared
  </code></pre>
  
  <p>then I run:</p>
  
  <pre><code>LD_PRELOAD=shared.so echo Hallo
  </code></pre>
  
  <p>it prints</p>
  
  <pre><code>Hallo
  </code></pre>
  
  <p>I was expecting to see:</p>
  
  <pre><code>HAHAHallo
  </code></pre>
  
  <p>Why is this not working?</p>
  
  <p>Some info about the binary:</p>
  
  <pre><code>&gt; objdump -s -j .init_array shared.so
  
  shared.so:     file format elf64-x86-64
  
  Contents of section .init_array:
   200798 b0060000 00000000 e0060000 00000000  ................
  
  &gt; nm shared.so
  00000000002007b8 d _DYNAMIC
  00000000002009b0 d _GLOBAL_OFFSET_TABLE_
                   w _ITM_deregisterTMCloneTable
                   w _ITM_registerTMCloneTable
                   w _Jv_RegisterClasses
  0000000000000790 r __FRAME_END__
  00000000002007b0 d __JCR_END__
  00000000002007b0 d __JCR_LIST__
  00000000002009e8 d __TMC_END__
  00000000002009e8 B __bss_start
                   w __cxa_finalize@@GLIBC_2.2.5
  0000000000000670 t __do_global_dtors_aux
  00000000002007a8 t __do_global_dtors_aux_fini_array_entry
  00000000002009e0 d __dso_handle
  0000000000200798 t __frame_dummy_init_array_entry
                   w __gmon_start__
  00000000002009e8 D _edata
  00000000002009f0 B _end
  0000000000000700 T _fini
  0000000000000578 T _init
  00000000002009e8 b completed.6661
  00000000000006e0 t ctor
  00000000000005e0 t deregister_tm_clones
  00000000000006b0 t frame_dummy
                   U printf@@GLIBC_2.2.5
  0000000000000620 t register_tm_clones
  </code></pre>
  
  <p>This indicates: </p>
  
  <p>(a) the ctor function is present. It is not removed by the linker, as for some people with similar problems.</p>
  
  <p>(b) The .init_array looks okay to me. There are two entries (instead of one), and they point to frame_dummy and ctor. (I used this answer: <a href="https://reverseengineering.stackexchange.com/a/12793">https://reverseengineering.stackexchange.com/a/12793</a>)</p>
  
  <p>(c) <a href="https://stackoverflow.com/questions/9472519/shared-library-constructor-not-working">shared library constructor not working</a> solved the problem by compiling and linking in one step, which I am already doing</p>
  
  <p>PS: It also does not work when I compile with gcc. </p>