Skip to content

Commit 1957297

Browse files
authored
Merge pull request #64743 from compnerd/handling
IRGen: attribute correct linkage to Windows DSO handle
2 parents f7e4797 + c8f8817 commit 1957297

File tree

5 files changed

+23
-39
lines changed

5 files changed

+23
-39
lines changed

include/swift/IRGen/Linking.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ bool useDllStorage(const llvm::Triple &triple);
4545
class UniversalLinkageInfo {
4646
public:
4747
bool IsELFObject;
48+
bool IsMSVCEnvironment;
4849
bool UseDLLStorage;
4950
bool Internalize;
5051

lib/FrontendTool/TBD.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,6 @@ bool swift::writeTBD(ModuleDecl *M, StringRef OutputFilename,
5656
return false;
5757
}
5858

59-
/// Determine if a symbol name is ignored when validating the TBD's contents
60-
/// against the IR's.
61-
///
62-
/// \param name The name of the symbol in question.
63-
/// \param IRModule The module being validated.
64-
///
65-
/// \returns Whether or not the presence or absence of the symbol named \a name
66-
/// should be ignored (instead of potentially producing a diagnostic.)
67-
static bool isSymbolIgnored(const StringRef& name,
68-
const llvm::Module &IRModule) {
69-
if (llvm::Triple(IRModule.getTargetTriple()).isOSWindows()) {
70-
// https://github.com/apple/swift/issues/58199
71-
// Error when referencing #dsohandle in a Swift test on Windows.
72-
// On Windows, ignore the lack of __ImageBase in the TBD file.
73-
if (name == "__ImageBase") {
74-
return true;
75-
}
76-
}
77-
78-
return false;
79-
}
80-
8159
static bool validateSymbols(DiagnosticEngine &diags,
8260
const std::vector<std::string> &symbols,
8361
const llvm::Module &IRModule,
@@ -99,11 +77,6 @@ static bool validateSymbols(DiagnosticEngine &diags,
9977
// with what TBDGen created.
10078
auto unmangledName = nameValue.getKey();
10179

102-
if (isSymbolIgnored(unmangledName, IRModule)) {
103-
// This symbol should not affect validation. Skip it.
104-
continue;
105-
}
106-
10780
SmallString<128> name;
10881
llvm::Mangler::getNameWithPrefix(name, unmangledName,
10982
IRModule.getDataLayout());

lib/IRGen/GenDecl.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,14 +2167,20 @@ void IRGenerator::emitEntryPointInfo() {
21672167
}
21682168

21692169
static IRLinkage
2170-
getIRLinkage(const UniversalLinkageInfo &info, SILLinkage linkage,
2171-
ForDefinition_t isDefinition, bool isWeakImported,
2172-
bool isKnownLocal = false) {
2170+
getIRLinkage(StringRef name, const UniversalLinkageInfo &info,
2171+
SILLinkage linkage, ForDefinition_t isDefinition,
2172+
bool isWeakImported, bool isKnownLocal = false) {
21732173
#define RESULT(LINKAGE, VISIBILITY, DLL_STORAGE) \
21742174
IRLinkage{llvm::GlobalValue::LINKAGE##Linkage, \
21752175
llvm::GlobalValue::VISIBILITY##Visibility, \
21762176
llvm::GlobalValue::DLL_STORAGE##StorageClass}
21772177

2178+
// This is a synthetic symbol that is referenced for `#dsohandle` and is never
2179+
// a definition but needs to be handled as a definition as it will be provided
2180+
// by the linker. This is a MSVC extension that is honoured by lld as well.
2181+
if (info.IsMSVCEnvironment && name == "__ImageBase")
2182+
return RESULT(External, Default, Default);
2183+
21782184
// Use protected visibility for public symbols we define on ELF. ld.so
21792185
// doesn't support relative relocations at load time, which interferes with
21802186
// our metadata formats. Default visibility should suffice for other object
@@ -2208,7 +2214,7 @@ getIRLinkage(const UniversalLinkageInfo &info, SILLinkage linkage,
22082214

22092215
case SILLinkage::Private: {
22102216
if (info.forcePublicDecls() && !isDefinition)
2211-
return getIRLinkage(info, SILLinkage::PublicExternal, isDefinition,
2217+
return getIRLinkage(name, info, SILLinkage::PublicExternal, isDefinition,
22122218
isWeakImported, isKnownLocal);
22132219

22142220
auto linkage = info.needLinkerToMergeDuplicateSymbols()
@@ -2262,8 +2268,9 @@ void irgen::updateLinkageForDefinition(IRGenModule &IGM,
22622268
isKnownLocal = IGM.getSwiftModule() == MD || MD->isStaticLibrary();
22632269

22642270
auto IRL =
2265-
getIRLinkage(linkInfo, entity.getLinkage(ForDefinition),
2266-
ForDefinition, weakImported, isKnownLocal);
2271+
getIRLinkage(global->hasName() ? global->getName() : StringRef(),
2272+
linkInfo, entity.getLinkage(ForDefinition), ForDefinition,
2273+
weakImported, isKnownLocal);
22672274
ApplyIRLinkage(IRL).to(global);
22682275

22692276
LinkInfo link = LinkInfo::get(IGM, entity, ForDefinition);
@@ -2297,8 +2304,9 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo,
22972304
}
22982305

22992306
bool weakImported = entity.isWeakImported(swiftModule);
2300-
result.IRL = getIRLinkage(linkInfo, entity.getLinkage(isDefinition),
2301-
isDefinition, weakImported, isKnownLocal);
2307+
result.IRL = getIRLinkage(result.Name, linkInfo,
2308+
entity.getLinkage(isDefinition), isDefinition,
2309+
weakImported, isKnownLocal);
23022310
result.ForDefinition = isDefinition;
23032311
return result;
23042312
}
@@ -2308,8 +2316,8 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo, StringRef name,
23082316
bool isWeakImported) {
23092317
LinkInfo result;
23102318
result.Name += name;
2311-
result.IRL = getIRLinkage(linkInfo, linkage, isDefinition, isWeakImported,
2312-
linkInfo.Internalize);
2319+
result.IRL = getIRLinkage(name, linkInfo, linkage, isDefinition,
2320+
isWeakImported, linkInfo.Internalize);
23132321
result.ForDefinition = isDefinition;
23142322
return result;
23152323
}

lib/IRGen/Linking.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ UniversalLinkageInfo::UniversalLinkageInfo(const llvm::Triple &triple,
8888
bool forcePublicDecls,
8989
bool isStaticLibrary)
9090
: IsELFObject(triple.isOSBinFormatELF()),
91+
IsMSVCEnvironment(triple.isWindowsMSVCEnvironment()),
9192
UseDLLStorage(useDllStorage(triple)), Internalize(isStaticLibrary),
9293
HasMultipleIGMs(hasMultipleIGMs), ForcePublicDecls(forcePublicDecls) {}
9394

lib/SILGen/SILGenExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,8 +4163,9 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
41634163
auto ImageBase = M.lookUpGlobalVariable("__ImageBase");
41644164
if (!ImageBase)
41654165
ImageBase =
4166-
SILGlobalVariable::create(M, SILLinkage::Public, IsNotSerialized,
4167-
"__ImageBase", BuiltinRawPtrTy);
4166+
SILGlobalVariable::create(M, SILLinkage::DefaultForDeclaration,
4167+
IsNotSerialized, "__ImageBase",
4168+
BuiltinRawPtrTy);
41684169
ModuleBase = B.createGlobalAddr(SILLoc, ImageBase);
41694170
} else {
41704171
auto DSOHandle = M.lookUpGlobalVariable("__dso_handle");

0 commit comments

Comments
 (0)