@@ -815,7 +815,7 @@ class ModuleAddressSanitizer {
815
815
private:
816
816
void initializeCallbacks (Module &M);
817
817
818
- void InstrumentGlobals (IRBuilder<> &IRB, Module &M, bool *CtorComdat);
818
+ bool InstrumentGlobals (IRBuilder<> &IRB, Module &M, bool *CtorComdat);
819
819
void InstrumentGlobalsCOFF (IRBuilder<> &IRB, Module &M,
820
820
ArrayRef<GlobalVariable *> ExtendedGlobals,
821
821
ArrayRef<Constant *> MetadataInitializers);
@@ -2235,7 +2235,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsELF(
2235
2235
2236
2236
// We also need to unregister globals at the end, e.g., when a shared library
2237
2237
// gets closed.
2238
- if (DestructorKind != AsanDtorKind::None && !MetadataGlobals. empty () ) {
2238
+ if (DestructorKind != AsanDtorKind::None) {
2239
2239
IRBuilder<> IrbDtor (CreateAsanModuleDtor (M));
2240
2240
IrbDtor.CreateCall (AsanUnregisterElfGlobals,
2241
2241
{IRB.CreatePointerCast (RegisteredFlag, IntptrTy),
@@ -2341,8 +2341,10 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2341
2341
// redzones and inserts this function into llvm.global_ctors.
2342
2342
// Sets *CtorComdat to true if the global registration code emitted into the
2343
2343
// asan constructor is comdat-compatible.
2344
- void ModuleAddressSanitizer::InstrumentGlobals (IRBuilder<> &IRB, Module &M,
2344
+ bool ModuleAddressSanitizer::InstrumentGlobals (IRBuilder<> &IRB, Module &M,
2345
2345
bool *CtorComdat) {
2346
+ *CtorComdat = false ;
2347
+
2346
2348
// Build set of globals that are aliased by some GA, where
2347
2349
// getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
2348
2350
SmallPtrSet<const GlobalVariable *, 16 > AliasedGlobalExclusions;
@@ -2360,6 +2362,11 @@ void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
2360
2362
}
2361
2363
2362
2364
size_t n = GlobalsToChange.size ();
2365
+ if (n == 0 ) {
2366
+ *CtorComdat = true ;
2367
+ return false ;
2368
+ }
2369
+
2363
2370
auto &DL = M.getDataLayout ();
2364
2371
2365
2372
// A global is described by a structure
@@ -2382,11 +2389,8 @@ void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
2382
2389
2383
2390
// We shouldn't merge same module names, as this string serves as unique
2384
2391
// module ID in runtime.
2385
- GlobalVariable *ModuleName =
2386
- n != 0
2387
- ? createPrivateGlobalForString (M, M.getModuleIdentifier (),
2388
- /* AllowMerging*/ false , kAsanGenPrefix )
2389
- : nullptr ;
2392
+ GlobalVariable *ModuleName = createPrivateGlobalForString (
2393
+ M, M.getModuleIdentifier (), /* AllowMerging*/ false , kAsanGenPrefix );
2390
2394
2391
2395
for (size_t i = 0 ; i < n; i++) {
2392
2396
GlobalVariable *G = GlobalsToChange[i];
@@ -2511,34 +2515,27 @@ void ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
2511
2515
}
2512
2516
appendToCompilerUsed (M, ArrayRef<GlobalValue *>(GlobalsToAddToUsedList));
2513
2517
2514
- if (UseGlobalsGC && TargetTriple.isOSBinFormatELF ()) {
2515
- // Use COMDAT and register globals even if n == 0 to ensure that (a) the
2516
- // linkage unit will only have one module constructor, and (b) the register
2517
- // function will be called. The module destructor is not created when n ==
2518
- // 0.
2518
+ std::string ELFUniqueModuleId =
2519
+ (UseGlobalsGC && TargetTriple.isOSBinFormatELF ()) ? getUniqueModuleId (&M)
2520
+ : " " ;
2521
+
2522
+ if (!ELFUniqueModuleId.empty ()) {
2523
+ InstrumentGlobalsELF (IRB, M, NewGlobals, Initializers, ELFUniqueModuleId);
2519
2524
*CtorComdat = true ;
2520
- InstrumentGlobalsELF (IRB, M, NewGlobals, Initializers,
2521
- getUniqueModuleId (&M));
2522
- } else if (n == 0 ) {
2523
- // When UseGlobalsGC is false, COMDAT can still be used if n == 0, because
2524
- // all compile units will have identical module constructor/destructor.
2525
- *CtorComdat = TargetTriple.isOSBinFormatELF ();
2525
+ } else if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF ()) {
2526
+ InstrumentGlobalsCOFF (IRB, M, NewGlobals, Initializers);
2527
+ } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection ()) {
2528
+ InstrumentGlobalsMachO (IRB, M, NewGlobals, Initializers);
2526
2529
} else {
2527
- *CtorComdat = false ;
2528
- if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF ()) {
2529
- InstrumentGlobalsCOFF (IRB, M, NewGlobals, Initializers);
2530
- } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection ()) {
2531
- InstrumentGlobalsMachO (IRB, M, NewGlobals, Initializers);
2532
- } else {
2533
- InstrumentGlobalsWithMetadataArray (IRB, M, NewGlobals, Initializers);
2534
- }
2530
+ InstrumentGlobalsWithMetadataArray (IRB, M, NewGlobals, Initializers);
2535
2531
}
2536
2532
2537
2533
// Create calls for poisoning before initializers run and unpoisoning after.
2538
2534
if (HasDynamicallyInitializedGlobals)
2539
2535
createInitializerPoisonCalls (M, ModuleName);
2540
2536
2541
2537
LLVM_DEBUG (dbgs () << M);
2538
+ return true ;
2542
2539
}
2543
2540
2544
2541
uint64_t
0 commit comments