diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp index c490e36484928..e1d02a526018d 100644 --- a/lib/SILGen/SILGenExpr.cpp +++ b/lib/SILGen/SILGenExpr.cpp @@ -3628,21 +3628,36 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) { SGF.getLoweredType(UnsafeRawPointer->getDeclaredInterfaceType()); SILType BuiltinRawPtrTy = SILType::getRawPointerType(SGF.getASTContext()); + SILModule &M = SGF.SGM.M; + SILBuilder &B = SGF.B; + + StructInst *S = nullptr; + if (M.getASTContext().LangOpts.Target.isOSWindows()) { + auto ImageBase = M.lookUpGlobalVariable("__ImageBase"); + if (!ImageBase) + ImageBase = + SILGlobalVariable::create(M, SILLinkage::Public, IsNotSerialized, + "__ImageBase", BuiltinRawPtrTy); + + auto ImageBaseAddr = B.createGlobalAddr(SILLoc, ImageBase); + auto ImageBasePointer = + B.createAddressToPointer(SILLoc, ImageBaseAddr, BuiltinRawPtrTy); + S = B.createStruct(SILLoc, UnsafeRawPtrTy, { ImageBasePointer }); + } else { + auto DSOGlobal = M.lookUpGlobalVariable("__dso_handle"); + if (!DSOGlobal) + DSOGlobal = + SILGlobalVariable::create(M, SILLinkage::PublicExternal, + IsNotSerialized, "__dso_handle", + BuiltinRawPtrTy); + + auto DSOAddr = B.createGlobalAddr(SILLoc, DSOGlobal); + auto DSOPointer = + B.createAddressToPointer(SILLoc, DSOAddr, BuiltinRawPtrTy); + S = B.createStruct(SILLoc, UnsafeRawPtrTy, { DSOPointer }); + } - auto DSOGlobal = SGF.SGM.M.lookUpGlobalVariable("__dso_handle"); - if (!DSOGlobal) - DSOGlobal = SILGlobalVariable::create(SGF.SGM.M, - SILLinkage::PublicExternal, - IsNotSerialized, "__dso_handle", - BuiltinRawPtrTy); - auto DSOAddr = SGF.B.createGlobalAddr(SILLoc, DSOGlobal); - - auto DSOPointer = SGF.B.createAddressToPointer(SILLoc, DSOAddr, - BuiltinRawPtrTy); - - auto UnsafeRawPtrStruct = SGF.B.createStruct(SILLoc, UnsafeRawPtrTy, - { DSOPointer }); - return RValue(SGF, E, ManagedValue::forUnmanaged(UnsafeRawPtrStruct)); + return RValue(SGF, E, ManagedValue::forUnmanaged(S)); } } diff --git a/test/SILGen/default_arguments.swift b/test/SILGen/default_arguments.swift index aa4b8db3f0d8b..e02df77f39b56 100644 --- a/test/SILGen/default_arguments.swift +++ b/test/SILGen/default_arguments.swift @@ -199,7 +199,7 @@ func takeDSOHandle(_ handle: UnsafeRawPointer = #dsohandle) { } // CHECK-LABEL: sil hidden [ossa] @$s17default_arguments13testDSOHandleyyF func testDSOHandle() { - // CHECK: [[DSO_HANDLE:%[0-9]+]] = global_addr @__dso_handle : $*Builtin.RawPointer + // CHECK: [[DSO_HANDLE:%[0-9]+]] = global_addr {{@__dso_handle|@__ImageBase}} : $*Builtin.RawPointer takeDSOHandle() } diff --git a/test/SILGen/dso_handle.swift b/test/SILGen/dso_handle.swift index e20b3ac3845e6..e7fec3557f27d 100644 --- a/test/SILGen/dso_handle.swift +++ b/test/SILGen/dso_handle.swift @@ -1,6 +1,6 @@ // RUN: %target-swift-emit-silgen -Xllvm -sil-full-demangle %s | %FileCheck %s -// CHECK: sil_global [[DSO:@__dso_handle]] : $Builtin.RawPointer +// CHECK: sil_global [[DSO:@__dso_handle|@__ImageBase]] : $Builtin.RawPointer // CHECK-LABEL: sil [ossa] @main : $@convention(c) // CHECK: bb0