[llvm-bugs] [Bug 51278] New: __attribute__((constructor)) doesn't run on AVR because __do_global_ctors isn't linked

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jul 29 22:39:35 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51278

            Bug ID: 51278
           Summary: __attribute__((constructor)) doesn't run on AVR
                    because __do_global_ctors isn't linked
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Driver
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mhjacobson at me.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210730/b02baaae/attachment-0001.html>


More information about the llvm-bugs mailing list