This is an archive of the discontinued LLVM Phabricator instance.

[ModuleUtils] Assert correct linkage and visibility of structors' COMDAT key
AbandonedPublic

Authored by melver on Feb 9 2023, 2:27 AM.

Details

Summary

Currently structors of various sanitizers have internal linkage, even
with a COMDAT set. This means that semantics is not the same with LTO,
because LTO uses linkage to deduplicate and not the COMDAT. The result
is that a target built with LTO currently has numerous duplicate
redundant constructors and destructors generated by the sanitizers.

This can be fixed by marking the structor as having external linkage.
However, care must be taken to also set hidden visibility, otherwise a
DSO may not call its own structor but another, which is usually wrong.

To allow the caller full flexibility over precise linkage and
visibility, we can't just set it for the caller.

Add an assertion that checks the various rules.

Along the way, we now have to fix up ASan, HWASan, and SanCov to emit
the right linkage.

See also: https://maskray.me/blog/2021-07-25-comdat-and-section-group#grp_comdat

Diff Detail

Event Timeline

melver created this revision.Feb 9 2023, 2:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 9 2023, 2:27 AM
melver requested review of this revision.Feb 9 2023, 2:27 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 9 2023, 2:27 AM
melver added a comment.Feb 9 2023, 2:30 AM

This is currently more an RFC - there might be other side-effects not yet accounted for, so please review carefully.

Although I have been able to reproduce the issue with an LTO and ASan build quite easily. If this is a common usecase for us, we might potentially save a good amount of .text size because of the 1000s of redundant structors.

pcc added a comment.Feb 9 2023, 11:20 AM

I've always considered that this should be fixed by extending the resolution-based LTO API to also include resolutions for comdats.

melver abandoned this revision.May 26 2023, 9:43 AM