This is an archive of the discontinued LLVM Phabricator instance.

Add a command line option `fregister_global_dtors_with_atexit` to register destructor functions annotated with __attribute__((destructor)) using __cxa_atexit or atexit.
ClosedPublic

Authored by ahatanak on Apr 12 2018, 10:12 AM.

Details

Summary

The command line option makes IRGen register destructor functions
annotated with __attribute__((destructor)) calling __cxa_atexit in a
synthesized constructor function instead of emitting references to
the destructor functions in a special section (__mod_term_funcs on
Darwin).

The primary reason for adding this option is that we are planning to
deprecate __mod_term_funcs section on Darwin in the future. This feature
is enabled by default on Darwin and can be disabled using command line
option fno_register_global_dtors_with_atexit.

rdar://problem/33887655

Diff Detail

Repository
rL LLVM

Event Timeline

ahatanak created this revision.Apr 12 2018, 10:12 AM
ahatanak updated this revision to Diff 142218.Apr 12 2018, 10:46 AM
ahatanak edited the summary of this revision. (Show Details)

Fix an error in emitGlobalDtorWithCXAAtExit and make sure @dso_handle is hidden.

rjmccall added inline comments.Apr 13 2018, 12:29 AM
include/clang/Driver/Options.td
1613 ↗(On Diff #142218)

Probably worth spelling out that this is for *global* destructors, at least in the help text and maybe in the option names. At the very least, the option name should say "dtors" instead of just "dtor".

include/clang/Frontend/CodeGenOptions.def
46 ↗(On Diff #142218)

Same thing: worth saying in the option name that this is just global dtors.

lib/CodeGen/CodeGenModule.h
1339 ↗(On Diff #142218)

There's an llvm::TinyPtrVector which would be useful here.

lib/CodeGen/ItaniumCXXABI.cpp
2213 ↗(On Diff #142218)

This is iterating over a DenseMap and therefore making the emission order dependent on details like how we hash ints for DenseMap.

Also, is this a correct way of handling Priority? You should at least justify that in comments.

ahatanak updated this revision to Diff 142597.Apr 15 2018, 10:43 PM
ahatanak marked 4 inline comments as done.
ahatanak retitled this revision from Add a command line option 'fregister_dtor_with_atexit' to register destructor functions annotated with __attribute__((destructor)) using __cxa_atexit or atexit. to Add a command line option `fregister_global_dtors_with_atexit` to register destructor functions annotated with __attribute__((destructor)) using __cxa_atexit or atexit..
ahatanak edited the summary of this revision. (Show Details)

Address review comments.

ahatanak added inline comments.Apr 15 2018, 10:45 PM
include/clang/Driver/Options.td
1613 ↗(On Diff #142218)

I renamed the option to -fregister-global-dtors-with-atexit.

lib/CodeGen/ItaniumCXXABI.cpp
2213 ↗(On Diff #142218)

I added a comment in registerGlobalDtorsWithAtExit that explains why destructor functions will be run in non-ascending order of their priority, which is what is expected.

rjmccall accepted this revision.Apr 16 2018, 2:39 AM

Thank you. Minor request and then LGTM.

lib/CodeGen/ItaniumCXXABI.cpp
2202 ↗(On Diff #142597)

You should probably mention that using null here is okay because this value is just passed back as an argument to the destructor function.

This revision is now accepted and ready to land.Apr 16 2018, 2:39 AM
This revision was automatically updated to reflect the committed changes.