@@ -1607,7 +1607,7 @@ DValue *DtoSymbolAddress(const Loc &loc, Type *type, Declaration *decl) {
1607
1607
if (SymbolDeclaration *sdecl = decl->isSymbolDeclaration ()) {
1608
1608
// this is the static initialiser (init symbol) for aggregates
1609
1609
AggregateDeclaration *ad = sdecl->dsym ;
1610
- IF_LOG Logger::print (" Sym: ad= %s\n " , ad->toChars ());
1610
+ IF_LOG Logger::print (" init symbol of %s\n " , ad->toChars ());
1611
1611
DtoResolveDsymbol (ad);
1612
1612
auto sd = ad->isStructDeclaration ();
1613
1613
@@ -1733,24 +1733,33 @@ static bool isDefaultLibSymbol(Dsymbol *sym) {
1733
1733
(md->packages .length > 1 && md->packages .ptr [1 ] == Id::io)));
1734
1734
}
1735
1735
1736
- bool defineOnDeclare (Dsymbol* sym, bool isFunction) {
1737
- if (global.params .linkonceTemplates )
1738
- return sym->isInstantiated ();
1739
-
1740
- // With -dllimport=defaultLibsOnly, an instantiated data symbol from a
1741
- // druntime/Phobos template may be assigned to an arbitrary binary (and culled
1742
- // from others via `needsCodegen()`). Define it in each referencing CU and
1743
- // never dllimport.
1744
- return !isFunction && global.params .dllimport == DLLImport::defaultLibsOnly &&
1745
- sym->isInstantiated () && isDefaultLibSymbol (sym);
1736
+ bool defineOnDeclare (Dsymbol* sym, bool ) {
1737
+ return global.params .linkonceTemplates && sym->isInstantiated ();
1746
1738
}
1747
1739
1748
1740
bool dllimportDataSymbol (Dsymbol *sym) {
1749
- return sym->isExport () || global.params .dllimport == DLLImport::all ||
1750
- (global.params .dllimport == DLLImport::defaultLibsOnly &&
1751
- // exclude instantiated symbols from druntime/Phobos templates (see
1752
- // `defineOnDeclare()`)
1753
- !sym->isInstantiated () && isDefaultLibSymbol (sym));
1741
+ if (!global.params .targetTriple ->isOSWindows ())
1742
+ return false ;
1743
+
1744
+ if (sym->isExport () || global.params .dllimport == DLLImport::all ||
1745
+ (global.params .dllimport == DLLImport::defaultLibsOnly &&
1746
+ isDefaultLibSymbol (sym))) {
1747
+ // Okay, this symbol is a candidate. Use dllimport unless we have a
1748
+ // guaranteed-codegen'd definition in a root module.
1749
+ if (auto mod = sym->isModule ()) {
1750
+ return !mod->isRoot (); // non-root ModuleInfo symbol
1751
+ } else if (sym->inNonRoot ()) {
1752
+ return true ; // not instantiated, and defined in non-root
1753
+ } else if (!global.params .linkonceTemplates &&
1754
+ sym->isInstantiated ()) {
1755
+ return true ; // instantiated but potentially culled
1756
+ } else if (auto vd = sym->isVarDeclaration ()) {
1757
+ if (vd->storage_class & STCextern)
1758
+ return true ; // externally defined global variable
1759
+ }
1760
+ }
1761
+
1762
+ return false ;
1754
1763
}
1755
1764
1756
1765
llvm::GlobalVariable *declareGlobal (const Loc &loc, llvm::Module &module,
0 commit comments