This is an archive of the discontinued LLVM Phabricator instance.

COFF/ELF: Place llvm.global_ctors elements in llvm.used if comdat is used
ClosedPublic

Authored by MaskRay on Jul 27 2021, 5:05 PM.

Details

Summary

On ELF, an SHT_INIT_ARRAY outside a section group is a GC root. The current
codegen abuses SHT_INIT_ARRAY in a section group to mean a GC root.

On PE/COFF, the dynamic initialization for __declspec(selectany) in a comdat
can be garbage collected by -opt:ref.

Call addUsedGlobal for the two cases to fix the abuse/bug.

Diff Detail

Event Timeline

MaskRay requested review of this revision.Jul 27 2021, 5:05 PM
MaskRay created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2021, 5:05 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
rnk accepted this revision.Jul 28 2021, 9:33 AM

lgtm

So the main impact here is that, on ELF, linker GC will no longer be able to GC vague linkage global variables with dynamic initializers. Those are things like

  • C++17 inline globals
  • selectany globals
  • static data members of class template instantiations

Seems reasonable to me.

clang/lib/CodeGen/CGDeclCXX.cpp
559

My first thought is that the selectany attribute should control the GVA_Linkage in the AST, but it looks like that doesn't happen right now. This is just refactoring the condition below.

This revision is now accepted and ready to land.Jul 28 2021, 9:33 AM

lgtm

So the main impact here is that, on ELF, linker GC will no longer be able to GC vague linkage global variables with dynamic initializers. Those are things like

  • C++17 inline globals
  • selectany globals
  • static data members of class template instantiations

Seems reasonable to me.

That is it! The ELF linker GC is similar to link.exe GC in this regard: ctor sections in a group/COMDAT are not special (GC roots).