<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - __attribute__((constructor)) doesn't run on AVR because __do_global_ctors isn't linked"
   href="https://bugs.llvm.org/show_bug.cgi?id=51278">51278</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__attribute__((constructor)) doesn't run on AVR because __do_global_ctors isn't linked
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Driver
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>mhjacobson@me.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Compiling and running this program:

__attribute__((constructor))
void ctor(void) {
        printf("ctor\n");
}

int main(void) {
        printf("hello\n");
        for (;;);
}

results in "ctor" *not* being printed.  

===

This is because `__do_global_ctors`, the symbol from libgcc.a responsible for
calling `ctor()`, is not emitted into the binary.

In turn, that is because nothing requests any symbol from the object file
containing `__do_global_ctors`.  Other bits from libgcc.a are used, but not
these:

$ nm -n /opt/local/lib/gcc/avr/10.3.0/avr5/libgcc.a
...
_ctors.o:
         U __ctors_end
         U __ctors_start
         U __tablejump2__
00000000 T __do_global_ctors

_dtors.o:
         U __dtors_end
         U __dtors_start
         U __tablejump2__
00000000 T __do_global_dtors
...

===

The way this works in GCC is that, when it sees a constructor function, it
emits:

.globl __do_global_ctors

into the assembly.  This causes the linker to resolve the resulting undefined
symbol.

===

One way to work around this is to pass
`-Wl,--require-defined,__do_global_ctors` to the driver.  I'm not sure if
that's the right fix here (i.e., change the driver always to pass
`--require-defined __do_global_ctors` to the linker).  Or perhaps this should
be fixed in the backend, so that `__do_global_ctors` is only linked in the
presence of a constructor function.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>