This is an archive of the discontinued LLVM Phabricator instance.

[SanitizerBinaryMetadata] Make module_[cd]tor external
ClosedPublic

Authored by MaskRay on Feb 7 2023, 3:37 PM.

Details

Summary

If a COMDAT key has a local linkage, it behaves as comdat nodeduplicate and
llvm/lib/Linker/LinkModules.cpp does not deduplicate its members.
This is not intended. Switch to an external linkage to allow deduplication.

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

Diff Detail

Event Timeline

MaskRay created this revision.Feb 7 2023, 3:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 3:37 PM
MaskRay requested review of this revision.Feb 7 2023, 3:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 3:37 PM
MaskRay updated this revision to Diff 495660.Feb 7 2023, 3:39 PM
MaskRay edited the summary of this revision. (Show Details)

Add a source comment

melver accepted this revision.Feb 8 2023, 12:46 AM

Thanks!

This revision is now accepted and ready to land.Feb 8 2023, 12:46 AM
This revision was automatically updated to reflect the committed changes.
melver added inline comments.Feb 8 2023, 2:24 AM
llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
227

Is there any usecase where having the COMDAT key internal linkage is valid? Should we add an assert() to appendToGlobalCtors()?

I'm guessing that will uncover a whole bunch of latent issues in other sanitizers as well.

(Unfortunately we can't make the ctor/dtor created in createSanitizerCtor always ExternalLinkage because e.g. ASan does actually need per-TU ctor/dtor for globals in some cases.)

MaskRay added inline comments.Feb 8 2023, 11:56 AM
llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
227

comdat nodeduplicate can use a local linkage key. For non-LTO use cases, a local linkage key works since all linkers use the symbol name for deduplication, ignoring the symbol binding. If we make the comdat key hidden, after linking it's similar to a local linkage. I don't have a strong opinion whether IR verifier or another place should reject some cases.