2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
5 // --------------------------------------------------------------------------------
9 // --------------------------------------------------------------------------------
15 // --------------------------------------------------------------------------------
17 // --------------------------------------------------------------------------------
21 #include <corpolicy.h>
27 #include "loaderheap.h"
32 #include <fusionbind.h>
33 #include "binderngen.h"
35 #include "assemblyspecbase.h"
36 #include "eecontract.h"
37 #include "metadatatracker.h"
38 #include "stackwalktypes.h"
39 #include <specstrings.h>
42 #include "eventtrace.h"
44 #ifdef FEATURE_HOSTED_BINDER
45 #include "clrprivbinderutil.h"
48 // --------------------------------------------------------------------------------
49 // Forward declared classes
50 // --------------------------------------------------------------------------------
53 class EditAndContinueModule;
60 class CLRPrivBinderLoadFile;
62 typedef VPTR(PEModule) PTR_PEModule;
63 typedef VPTR(PEAssembly) PTR_PEAssembly;
65 // --------------------------------------------------------------------------------
67 // --------------------------------------------------------------------------------
69 // --------------------------------------------------------------------------------
70 // A PEFile is an input to the CLR loader. It is produced as a result of
71 // binding, usually through fusion (although there are a few less common methods to
72 // obtain one which do not go through fusion, e.g. IJW loads)
74 // Although a PEFile is usually a disk based PE file (hence the name), it is not
75 // always the case. Thus it is a conscious decision to not export access to the PE
76 // file directly; rather the specific information required should be provided via
77 // individual query API.
79 // There are multiple "flavors" of PEFiles:
81 // 1. HMODULE - these PE Files are loaded in response to "spontaneous" OS callbacks.
82 // These should only occur for .exe main modules and IJW dlls loaded via LoadLibrary
83 // or static imports in umnanaged code.
85 // 2. Fusion loads - these are the most common case. A path is obtained from fusion and
86 // the result is loaded via PEImage.
87 // a. Display name loads - these are metadata-based binds
88 // b. Path loads - these are loaded from an explicit path
90 // 3. Byte arrays - loaded explicitly by user code. These also go through PEImage.
92 // 4. Dynamic - these are not actual PE images at all, but are placeholders
93 // for reflection-based modules.
95 // PEFiles are segmented into two subtypes: PEAssembly and PEModule. The formere
96 // is a file to be loaded as an assembly, and the latter is to be loaded as a module.
98 // See also file:..\inc\corhdr.h#ManagedHeader for more on the format of managed images.
99 // See code:Module for more on modules
100 // --------------------------------------------------------------------------------
102 typedef VPTR(class PEFile) PTR_PEFile;
104 typedef ReleaseHolder<IMDInternalImport> IMDInternalImportHolder;
108 // ------------------------------------------------------------
110 // ------------------------------------------------------------
111 VPTR_BASE_CONCRETE_VTABLE_CLASS(PEFile)
115 // ------------------------------------------------------------
117 // ------------------------------------------------------------
119 STDMETHOD_(ULONG, AddRef)();
120 STDMETHOD_(ULONG, Release)();
122 #ifdef DACCESS_COMPILE
123 virtual void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
131 // ------------------------------------------------------------
133 // ------------------------------------------------------------
135 friend class DomainFile;
136 friend class PEModule;
137 #ifdef DACCESS_COMPILE
138 friend class NativeImageDumper;
141 // Load actually triggers loading side effects of the module. This should ONLY
142 // be done after validation has been passed
143 BOOL CanLoadLibrary();
145 void LoadLibrary(BOOL allowNativeSkip = TRUE);
147 #ifdef FEATURE_MIXEDMODE
149 // Returns TRUE if this file references managed CRT (msvcmNN*).
150 BOOL ReferencesManagedCRT();
152 // Checks for unsupported loads of C++/CLI assemblies into multiple runtimes in this process.
153 void CheckForDisallowedInProcSxSLoad();
154 #endif // FEATURE_MIXEDMODE
157 void CheckForDisallowedInProcSxSLoadWorker();
158 void ValidateImagePlatformNeutrality();
160 // For use inside LoadLibrary callback
161 friend HRESULT ExecuteDLLForAttach(HINSTANCE hInst,
165 void SetLoadedHMODULE(HMODULE hMod);
167 BOOL HasSkipVerification();
168 void SetSkipVerification();
170 // DO NOT USE !!! this is to be removed when we move to new fusion binding API
171 friend class DomainAssembly;
173 // Helper for creating metadata for CreateDynamic
174 friend class Assembly;
175 friend class COMDynamicWrite;
176 friend class AssemblyNative;
177 static void DefineEmitScope(
182 IMDInternalImportHolder GetMDImport();
185 // ------------------------------------------------------------
186 // Generic PEFile - can be used to access metadata
187 // ------------------------------------------------------------
189 static PEFile *Open(PEImage *image);
191 // ------------------------------------------------------------
193 // ------------------------------------------------------------
195 #ifndef DACCESS_COMPILE
196 BOOL Equals(PEFile *pFile);
197 BOOL Equals(PEImage *pImage);
198 #endif // DACCESS_COMPILE
200 #ifndef FEATURE_CORECLR
204 void GetMVID(GUID *pMvid);
206 // ------------------------------------------------------------
207 // Descriptive strings
208 // ------------------------------------------------------------
210 // Path is the file path to the file; empty if not a file
211 const SString &GetPath();
213 #ifdef DACCESS_COMPILE
214 // This is the metadata module name. Used as a hint as file name.
215 const SString &GetModuleFileNameHint();
216 #endif // DACCESS_COMPILE
218 // Full name is the most descriptive name available (path, codebase, or name as appropriate)
219 void GetCodeBaseOrName(SString &result);
221 // Returns security information for the assembly based on the codebase
222 void GetSecurityIdentity(SString &codebase, SecZone *pdwZone, DWORD dwFlags, BYTE *pbUniqueID, DWORD *pcbUniqueID);
223 void InitializeSecurityManager();
226 // This is useful for log messages
227 LPCWSTR GetDebugName();
230 // ------------------------------------------------------------
232 // ------------------------------------------------------------
234 CHECK CheckLoaded(BOOL allowNativeSkip = TRUE);
235 void ValidateForExecution();
236 BOOL IsMarkedAsNoPlatform();
237 BOOL IsMarkedAsContentTypeWindowsRuntime();
240 // ------------------------------------------------------------
242 // ------------------------------------------------------------
244 BOOL IsAssembly() const;
245 PTR_PEAssembly AsAssembly();
246 BOOL IsModule() const;
247 PTR_PEModule AsModule();
248 BOOL IsSystem() const;
249 BOOL IsDynamic() const;
250 BOOL IsResource() const;
251 BOOL IsIStream() const;
252 BOOL IsIntrospectionOnly() const;
253 // Returns self (if assembly) or containing assembly (if module)
254 PEAssembly *GetAssembly() const;
256 // ------------------------------------------------------------
258 // ------------------------------------------------------------
260 #ifndef DACCESS_COMPILE
261 void GetImageBits(SBuffer &result);
262 void GetHash(ALG_ID algorithm, SBuffer &result);
263 #endif // DACCESS_COMPILE
265 void GetSHA1Hash(SBuffer &result);
266 CHECK CheckHash(ALG_ID algorithm, const void *hash, COUNT_T size);
268 // ------------------------------------------------------------
270 // ------------------------------------------------------------
274 IMDInternalImport *GetPersistentMDImport();
275 IMDInternalImport *GetMDImportWithRef();
276 void MakeMDImportPersistent() {m_bHasPersistentMDImport=TRUE;};
278 #ifndef DACCESS_COMPILE
279 IMetaDataEmit *GetEmitter();
280 IMetaDataAssemblyEmit *GetAssemblyEmitter();
281 IMetaDataImport2 *GetRWImporter();
282 IMetaDataAssemblyImport *GetAssemblyImporter();
284 TADDR GetMDInternalRWAddress();
285 #endif // DACCESS_COMPILE
287 LPCUTF8 GetSimpleName();
288 HRESULT GetScopeName(LPCUTF8 * pszName);
289 BOOL IsStrongNameVerified();
290 BOOL IsStrongNamed();
291 const void *GetPublicKey(DWORD *pcbPK);
292 ULONG GetHashAlgId();
293 HRESULT GetVersion(USHORT *pMajor, USHORT *pMinor, USHORT *pBuild, USHORT *pRevision);
296 HRESULT GetFlagsNoTrigger(DWORD * pdwFlags);
297 #ifdef FEATURE_CAS_POLICY
298 COR_TRUST *GetAuthenticodeSignature();
300 // ------------------------------------------------------------
302 // ------------------------------------------------------------
304 BOOL HasSecurityDirectory();
305 BOOL IsIbcOptimized();
307 mdToken GetEntryPointToken(
309 BOOL bAssumeLoaded = FALSE
317 PTR_VOID GetRvaField(RVA field);
318 CHECK CheckRvaField(RVA field);
319 CHECK CheckRvaField(RVA field, COUNT_T size);
321 PCCOR_SIGNATURE GetSignature(RVA signature);
322 RVA GetSignatureRva(PCCOR_SIGNATURE signature);
323 CHECK CheckSignature(PCCOR_SIGNATURE signature);
324 CHECK CheckSignatureRva(RVA signature);
327 BOOL IsRvaFieldTls(RVA field);
328 UINT32 GetFieldTlsOffset(RVA field);
329 UINT32 GetTlsIndex();
331 const void *GetInternalPInvokeTarget(RVA target);
332 CHECK CheckInternalPInvokeTarget(RVA target);
334 IMAGE_COR_VTABLEFIXUP *GetVTableFixups(COUNT_T *pCount = NULL);
335 void *GetVTable(RVA rva);
337 BOOL GetResource(LPCSTR szName, DWORD *cbResource,
338 PBYTE *pbInMemoryResource, DomainAssembly** pAssemblyRef,
339 LPCSTR *szFileName, DWORD *dwLocation,
340 StackCrawlMark *pStackMark, BOOL fSkipSecurityCheck,
341 BOOL fSkipRaiseResolveEvent, DomainAssembly* pDomainAssembly,
342 AppDomain* pAppDomain);
343 #ifndef DACCESS_COMPILE
344 PTR_CVOID GetMetadata(COUNT_T *pSize);
346 PTR_CVOID GetLoadedMetadata(COUNT_T *pSize);
348 void GetPEKindAndMachine(DWORD* pdwKind, DWORD* pdwMachine);
350 ULONG GetILImageTimeDateStamp();
352 #ifdef FEATURE_CAS_POLICY
353 SAFEHANDLE GetSafeHandle();
354 #endif // FEATURE_CAS_POLICY
356 // ------------------------------------------------------------
357 // Image memory access
359 // WARNING: do not abuse these. There are scenarios where the image
360 // is not in memory as an optimization.
362 // In general, you should add an entry point to get the specific info
363 // you are interested in, rather than using these general purpose
364 // entry points. The info can then be extracted from the native image
365 // in the no-IL image case.
366 // ------------------------------------------------------------
368 // For IJW purposes only - this asserts that we have an IJW image.
369 HMODULE GetIJWBase();
371 // The debugger can tolerate a null value here for native only loading cases
372 PTR_VOID GetDebuggerContents(COUNT_T *pSize = NULL);
374 #ifndef DACCESS_COMPILE
375 // Returns the IL image range; may force a LoadLibrary
376 const void *GetManagedFileContents(COUNT_T *pSize = NULL);
377 #endif // DACCESS_COMPILE
379 PTR_CVOID GetLoadedImageContents(COUNT_T *pSize = NULL);
381 // SetInProcSxSLoadVerified can run concurrently as we don't hold locks during LoadLibrary but
382 // it is the only flag that can be set during this phase so no mutual exclusion is necessary.
383 void SetInProcSxSLoadVerified() { LIMITED_METHOD_CONTRACT; m_flags |= PEFILE_SXS_LOAD_VERIFIED; }
384 BOOL IsInProcSxSLoadVerified() { LIMITED_METHOD_CONTRACT; return m_flags & PEFILE_SXS_LOAD_VERIFIED; }
386 // ------------------------------------------------------------
387 // Native image access
388 // ------------------------------------------------------------
390 // Does the loader support using a native image for this file?
391 // Some implementation restrictions prevent native images from being used
393 #ifdef FEATURE_PREJIT
394 BOOL CanUseNativeImage() { LIMITED_METHOD_CONTRACT; return m_fCanUseNativeImage; }
395 void SetCannotUseNativeImage() { LIMITED_METHOD_CONTRACT; m_fCanUseNativeImage = FALSE; }
396 void SetNativeImageUsedExclusively() { LIMITED_METHOD_CONTRACT; m_flags|=PEFILE_NATIVE_IMAGE_USED_EXCLUSIVELY; }
397 BOOL IsNativeImageUsedExclusively() { LIMITED_METHOD_CONTRACT; return m_flags&PEFILE_NATIVE_IMAGE_USED_EXCLUSIVELY; }
398 void SetSafeToHardBindTo() { LIMITED_METHOD_CONTRACT; m_flags|=PEFILE_SAFE_TO_HARDBINDTO; }
399 BOOL IsSafeToHardBindTo() { LIMITED_METHOD_CONTRACT; return m_flags&PEFILE_SAFE_TO_HARDBINDTO; }
401 BOOL IsNativeLoaded();
402 PEImage *GetNativeImageWithRef();
403 PEImage *GetPersistentNativeImage();
405 BOOL HasNativeImage();
406 PTR_PEImageLayout GetLoaded();
407 PTR_PEImageLayout GetLoadedNative();
408 PTR_PEImageLayout GetLoadedIL();
409 PTR_PEImageLayout GetAnyILWithRef(); //AddRefs!
410 IStream * GetPdbStream();
411 void ClearPdbStream();
412 BOOL IsLoaded(BOOL bAllowNativeSkip=TRUE) ;
413 BOOL PassiveDomainOnly();
414 BOOL IsPtrInILImage(PTR_CVOID data);
416 #ifdef DACCESS_COMPILE
417 PEImage *GetNativeImage()
419 LIMITED_METHOD_DAC_CONTRACT;
420 #ifdef FEATURE_PREJIT
421 return m_nativeImage;
428 #ifdef FEATURE_PREJIT
429 // ------------------------------------------------------------
430 // Native image config utilities
431 // ------------------------------------------------------------
433 static CorCompileConfigFlags GetNativeImageConfigFlags(BOOL fForceDebug = FALSE,
434 BOOL fForceProfiling = FALSE,
435 BOOL fForceInstrument = FALSE);
437 static CorCompileConfigFlags GetNativeImageConfigFlagsWithOverrides();
439 #ifdef DEBUGGING_SUPPORTED
440 static void SetNGENDebugFlags(BOOL fAllowOpt);
441 static void GetNGENDebugFlags(BOOL *fAllowOpt);
444 #ifdef FEATURE_TREAT_NI_AS_MSIL_DURING_DIAGNOSTICS
445 static BOOL ShouldTreatNIAsMSIL();
446 #endif // FEATURE_TREAT_NI_AS_MSIL_DURING_DIAGNOSTICS
448 #endif // FEATURE_PREJIT
450 // ------------------------------------------------------------
452 // ------------------------------------------------------------
454 void GetEmbeddedResource(DWORD dwOffset, DWORD *cbResource, PBYTE *pbInMemoryResource);
456 // ------------------------------------------------------------
458 // ------------------------------------------------------------
460 PEAssembly * LoadAssembly(
461 mdAssemblyRef kAssemblyRef,
462 IMDInternalImport * pImport = NULL,
463 LPCUTF8 szWinRtTypeNamespace = NULL,
464 LPCUTF8 szWinRtTypeClassName = NULL);
466 // ------------------------------------------------------------
468 // ------------------------------------------------------------
470 // The format string is intentionally unicode to avoid globalization bugs
471 #ifdef FEATURE_PREJIT
472 void ExternalLog(DWORD facility, DWORD level, const WCHAR *fmt, ...) DAC_EMPTY();
473 void ExternalLog(DWORD level, const WCHAR *fmt, ...) DAC_EMPTY();
474 void ExternalLog(DWORD level, const char *msg) DAC_EMPTY();
475 virtual void ExternalVLog(DWORD facility, DWORD level, const WCHAR *fmt, va_list args) DAC_EMPTY();
476 virtual void FlushExternalLog() DAC_EMPTY();
480 // ------------------------------------------------------------
481 // Internal constants
482 // ------------------------------------------------------------
486 PEFILE_SYSTEM = 0x01,
487 PEFILE_ASSEMBLY = 0x02,
488 PEFILE_MODULE = 0x04,
489 PEFILE_SKIP_VERIFICATION = 0x08,
490 PEFILE_SKIP_MODULE_HASH_CHECKS= 0x10,
491 PEFILE_ISTREAM = 0x100,
492 #ifdef FEATURE_PREJIT
493 PEFILE_HAS_NATIVE_IMAGE_METADATA = 0x200,
494 PEFILE_NATIVE_IMAGE_USED_EXCLUSIVELY =0x1000,
495 PEFILE_SAFE_TO_HARDBINDTO = 0x4000, // NGEN-only flag
497 PEFILE_INTROSPECTIONONLY = 0x400,
498 PEFILE_SXS_LOAD_VERIFIED = 0x2000
501 // ------------------------------------------------------------
503 // ------------------------------------------------------------
505 #ifndef DACCESS_COMPILE
506 PEFile(PEImage *image, BOOL fCheckAuthenticodeSignature = TRUE);
509 virtual void ReleaseIL();
513 void RestoreMDImport(IMDInternalImport* pImport);
514 void OpenMDImport_Unsafe();
516 void OpenAssemblyImporter();
518 void OpenAssemblyEmitter();
520 void ConvertMDInternalToReadWrite();
521 void ReleaseMetadataInterfaces(BOOL bDestructor, BOOL bKeepNativeData=FALSE);
523 #ifdef FEATURE_CAS_POLICY
524 // Check the Authenticode signature of a PE file
525 void CheckAuthenticodeSignature();
526 #endif // FEATURE_CAS_POLICY
529 #ifdef FEATURE_PREJIT
530 void SetNativeImage(PEImage *nativeImage);
531 #ifndef DACCESS_COMPILE
532 virtual void ClearNativeImage();
536 #ifndef DACCESS_COMPILE
537 void EnsureImageOpened();
538 #endif // DACCESS_COMPILE
540 friend class ClrDataAccess;
541 BOOL HasNativeImageMetadata();
543 // ------------------------------------------------------------
545 // ------------------------------------------------------------
548 LPCWSTR m_pDebugName;
553 PTR_PEImage m_identity;
554 // IL image, NULL if we didn't need to open the file
555 PTR_PEImage m_openedILimage;
556 #ifdef FEATURE_PREJIT
558 PTR_PEImage m_nativeImage;
560 BOOL m_fCanUseNativeImage;
562 // This flag is not updated atomically with m_pMDImport. Its fine for debugger usage
563 // but don't rely on it in the runtime. In runtime try QI'ing the m_pMDImport for
564 // IID_IMDInternalImportENC
565 BOOL m_MDImportIsRW_Debugger_Use_Only;
566 Volatile<BOOL> m_bHasPersistentMDImport;
568 #ifndef DACCESS_COMPILE
569 IMDInternalImport *m_pMDImport;
571 IMDInternalImport *m_pMDImport_UseAccessor;
573 IMetaDataImport2 *m_pImporter;
574 IMetaDataEmit *m_pEmitter;
575 #ifndef FEATURE_CORECLR
576 IMetaDataAssemblyImport *m_pAssemblyImporter;
577 IMetaDataAssemblyEmit *m_pAssemblyEmitter;
579 SimpleRWLock *m_pMetadataLock;
580 Volatile<LONG> m_refCount;
581 SBuffer *m_hash; // cached SHA1 hash value
583 BOOL m_fStrongNameVerified;
584 #ifdef FEATURE_CAS_POLICY
585 COR_TRUST *m_certificate;
586 BOOL m_fCheckedCertificate;
587 IInternetSecurityManager *m_pSecurityManager;
588 Crst m_securityManagerLock;
589 #endif // FEATURE_CAS_POLICY
591 #ifdef DEBUGGING_SUPPORTED
592 #ifdef FEATURE_PREJIT
593 SVAL_DECL(DWORD, s_NGENDebugFlags);
598 PTR_PEImage GetILimage()
607 #ifndef DACCESS_COMPILE
608 if (m_openedILimage == NULL && m_identity != NULL)
610 PEImage* pOpenedILimage;
611 m_identity->Clone(MDInternalImport_Default,&pOpenedILimage);
612 if (InterlockedCompareExchangeT(&m_openedILimage,pOpenedILimage,NULL) != NULL)
613 pOpenedILimage->Release();
616 return m_openedILimage;
619 PEImage *GetOpenedILimage()
621 LIMITED_METHOD_DAC_CONTRACT;
622 _ASSERTE(HasOpenedILimage());
623 return m_openedILimage;
627 BOOL HasOpenedILimage()
629 LIMITED_METHOD_DAC_CONTRACT;
630 return m_openedILimage != NULL;
636 LIMITED_METHOD_DAC_CONTRACT;
637 return HasOpenedILimage() && GetOpenedILimage()->HasLoadedLayout();
640 BOOL IsDesignerBindingContext()
642 LIMITED_METHOD_CONTRACT;
644 #ifdef FEATURE_HOSTED_BINDER
645 DWORD binderFlags = BINDER_NONE;
648 if (HasHostAssembly())
649 hr = GetHostAssembly()->GetBinderFlags(&binderFlags);
651 return hr == S_OK ? binderFlags & BINDER_DESIGNER_BINDING_CONTEXT : FALSE;
657 LPCWSTR GetPathForErrorMessages();
659 static PEFile* Dummy();
660 void MarkNativeImageInvalidIfOwned();
661 void ConvertMetadataToRWForEnC();
663 #if defined(FEATURE_VERSIONING) || defined(FEATURE_HOSTED_BINDER)
665 PTR_ICLRPrivAssembly m_pHostAssembly;
668 #ifdef FEATURE_HOSTED_BINDER
671 friend class CLRPrivBinderFusion;
672 #ifndef DACCESS_COMPILE
673 // CLRPrivBinderFusion calls this for Fusion-bound assemblies in AppX processes.
674 void SetHostAssembly(ICLRPrivAssembly * pHostAssembly)
675 { LIMITED_METHOD_CONTRACT; m_pHostAssembly = clr::SafeAddRef(pHostAssembly); }
676 #endif //DACCESS_COMPILE
679 // Returns a non-AddRef'ed ICLRPrivAssembly*
680 PTR_ICLRPrivAssembly GetHostAssembly()
682 STATIC_CONTRACT_LIMITED_METHOD;
683 return m_pHostAssembly;
686 // Returns the ICLRPrivBinder* instance associated with the PEFile
687 PTR_ICLRPrivBinder GetBindingContext();
689 bool HasHostAssembly()
690 { STATIC_CONTRACT_WRAPPER; return GetHostAssembly() != nullptr; }
692 bool CanUseWithBindingCache()
693 { LIMITED_METHOD_CONTRACT; return !HasHostAssembly(); }
694 #endif // FEATURE_HOSTED_BINDER
698 class PEAssembly : public PEFile
700 VPTR_VTABLE_CLASS(PEAssembly, PEFile)
703 // ------------------------------------------------------------
704 // Statics initialization.
705 // ------------------------------------------------------------
709 // ------------------------------------------------------------
711 // ------------------------------------------------------------
713 #if defined(FEATURE_HOSTED_BINDER)
714 #if !defined(FEATURE_CORECLR)
715 static PEAssembly * Open(
716 PEAssembly * pParentAssembly,
717 PEImage * pPEImageIL,
718 PEImage * pPEImageNI,
719 ICLRPrivAssembly * pHostAssembly,
720 BOOL fIsIntrospectionOnly);
722 static PEAssembly * Open(
723 PEAssembly * pParentAssembly,
724 PEImage * pPEImageIL,
725 BOOL isIntrospectionOnly = FALSE);
726 #else //!FEATURE_CORECLR
727 // CoreCLR's PrivBinder PEAssembly creation entrypoint
728 static PEAssembly * Open(
729 PEAssembly * pParent,
730 PEImage * pPEImageIL,
731 PEImage * pPEImageNI,
732 ICLRPrivAssembly * pHostAssembly,
733 BOOL fIsIntrospectionOnly = FALSE);
734 #endif //!FEATURE_CORECLR
735 #endif //FEATURE_HOSTED_BINDER
737 // This opens the canonical mscorlib.dll
738 #ifdef FEATURE_FUSION
739 static PEAssembly *OpenSystem(IApplicationContext *pAppCtx);
741 static PEAssembly *OpenSystem(IUnknown *pAppCtx);
743 #ifdef DACCESS_COMPILE
744 virtual void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
747 #ifdef FEATURE_FUSION
748 static PEAssembly *Open(
749 IAssembly *pIAssembly,
750 IBindResult *pNativeFusionAssembly,
751 IFusionBindLog *pFusionLog = NULL,
752 BOOL isSystemAssembly = FALSE,
753 BOOL isIntrospectionOnly = FALSE);
755 static PEAssembly *Open(
756 IHostAssembly *pIHostAssembly,
757 BOOL isSystemAssembly = FALSE,
758 BOOL isIntrospectionOnly = FALSE);
760 #ifdef FEATURE_MIXEDMODE
761 // Use for main exe loading
762 // NOTE: This may also be used for "spontaneous" (IJW) dll loading where
763 // we need to deliver DllMain callbacks, but we should eliminate this case
765 static PEAssembly *OpenHMODULE(
767 IAssembly *pFusionAssembly,
768 IBindResult *pNativeFusionAssembly,
769 IFusionBindLog *pFusionLog = NULL,
770 BOOL isIntrospectionOnly = FALSE);
771 #endif // FEATURE_MIXEDMODE
773 static PEAssembly *DoOpen(
774 IAssembly *pIAssembly,
775 IBindResult *pNativeFusionAssembly,
776 IFusionBindLog *pFusionLog,
777 BOOL isSystemAssembly,
778 BOOL isIntrospectionOnly = FALSE);
780 static PEAssembly *DoOpen(
781 IHostAssembly *pIHostAssembly,
782 BOOL isSystemAssembly,
783 BOOL isIntrospectionOnly = FALSE);
784 #ifdef FEATURE_MIXEDMODE
785 static PEAssembly *DoOpenHMODULE(
787 IAssembly *pFusionAssembly,
788 IBindResult *pNativeFusionAssembly,
789 IFusionBindLog *pFusionLog,
790 BOOL isIntrospectionOnly = FALSE);
791 #endif // FEATURE_MIXEDMODE
793 static PEAssembly *Open(
794 CoreBindResult* pBindResult,
796 BOOL isIntrospectionOnly);
797 #endif // FEATURE_FUSION
799 static PEAssembly *Create(
800 PEAssembly *pParentAssembly,
801 IMetaDataAssemblyEmit *pEmit,
802 BOOL isIntrospectionOnly);
804 static PEAssembly *OpenMemory(
805 PEAssembly *pParentAssembly,
808 BOOL isIntrospectionOnly = FALSE,
809 CLRPrivBinderLoadFile* pBinderToUse = NULL);
811 static PEAssembly *DoOpenMemory(
812 PEAssembly *pParentAssembly,
815 BOOL isIntrospectionOnly,
816 CLRPrivBinderLoadFile* pBinderToUse);
819 // Private helpers for crufty exception handling reasons
820 #ifdef FEATURE_FUSION
821 static PEAssembly *DoOpenSystem(IApplicationContext *pAppCtx);
823 static PEAssembly *DoOpenSystem(IUnknown *pAppCtx);
828 // ------------------------------------------------------------
830 // ------------------------------------------------------------
833 #ifdef FEATURE_CORECLR
834 BOOL IsProfileAssembly();
835 BOOL IsSilverlightPlatformStrongNameSignature();
836 BOOL IsProfileTestAssembly();
837 BOOL IsTestKeySignature();
838 #endif // FEATURE_CORECLR
840 ULONG HashIdentity();
841 #ifdef FEATURE_FUSION
843 BOOL FusionLoggingEnabled()
845 LIMITED_METHOD_CONTRACT;
846 return m_bFusionLogEnabled && (m_pFusionLog != NULL);
848 void DisableFusionLogging()
850 m_bFusionLogEnabled = FALSE;
853 IFusionBindLog *GetFusionBindLog()
855 LIMITED_METHOD_CONTRACT;
856 #ifdef FEATURE_FUSION
857 return (m_bFusionLogEnabled && (m_pFusionLog != NULL)) ? m_pFusionLog : NULL;
864 BOOL IsBindingCodeBase();
866 BOOL IsSourceDownloadCache();
868 LOADCTX_TYPE GetLoadContext();
869 BOOL IsContextLoad();
871 // Can we avoid exposing these?
872 IAssembly *GetFusionAssembly();
873 IHostAssembly *GetIHostAssembly();
874 IAssemblyName *GetFusionAssemblyName();
875 IAssemblyName *GetFusionAssemblyNameNoCreate();
876 IAssemblyLocation* GetNativeAssemblyLocation();
877 DWORD GetLocationFlags();
878 PEKIND GetFusionProcessorArchitecture();
881 #ifndef DACCESS_COMPILE
882 virtual void ReleaseIL();
885 // ------------------------------------------------------------
887 // ------------------------------------------------------------
889 BOOL NeedsModuleHashChecks();
891 BOOL HasStrongNameSignature();
892 BOOL IsFullySigned();
894 void SetStrongNameBypassed();
895 void VerifyStrongName();
897 // ------------------------------------------------------------
898 // Descriptive strings
899 // ------------------------------------------------------------
901 // This returns a non-empty path representing the source of the assembly; it may
902 // be the parent assembly for dynamic or memory assemblies
903 const SString &GetEffectivePath();
905 // Codebase is the fusion codebase or path for the assembly. It is in URL format.
906 // Note this may be obtained from the parent PEFile if we don't have a path or fusion
909 // fCopiedName means to get the "shadow copied" path rather than the original path, if applicable
910 void GetCodeBase(SString &result, BOOL fCopiedName = FALSE);
911 // Get the fully qualified assembly name from its metadata token
912 static void GetFullyQualifiedAssemblyName(IMDInternalImport* pImport, mdAssembly mda, SString &result, DWORD flags = 0);
914 // Display name is the fusion binding name for an assembly
915 void GetDisplayName(SString &result, DWORD flags = 0);
917 // ------------------------------------------------------------
919 // ------------------------------------------------------------
921 LPCUTF8 GetSimpleName();
923 // ------------------------------------------------------------
925 // ------------------------------------------------------------
927 static void PathToUrl(SString &string);
928 static void UrlToPath(SString &string);
929 static BOOL FindLastPathSeparator(const SString &path, SString::Iterator &i);
931 // ------------------------------------------------------------
933 // ------------------------------------------------------------
934 #ifdef FEATURE_PREJIT
935 void ExternalVLog(DWORD facility, DWORD level, const WCHAR *fmt, va_list args) DAC_EMPTY();
936 void FlushExternalLog() DAC_EMPTY();
937 #ifdef FEATURE_FUSION
938 void ETWTraceLogMessage(DWORD dwETWLogCategory, PEAssembly *pAsm)
940 LIMITED_METHOD_CONTRACT
941 if (FusionLoggingEnabled() &&
942 (ETW_TRACING_CATEGORY_ENABLED(MICROSOFT_WINDOWS_DOTNETRUNTIME_PRIVATE_PROVIDER_Context, TRACE_LEVEL_INFORMATION, CLR_PRIVATEFUSION_KEYWORD)))
944 m_pFusionLog->ETWTraceLogMessage(dwETWLogCategory, (pAsm?pAsm->m_pFusionAssemblyName:NULL));
947 ULONGLONG GetBindingID()
949 LIMITED_METHOD_CONTRACT;
950 ULONGLONG ullBindingID = 0;
951 if (FusionLoggingEnabled())
952 m_pFusionLog->GetBindingID(&ullBindingID);
958 #ifndef FEATURE_CORECLR
959 BOOL IsReportedToUsageLog();
960 void SetReportedToUsageLog();
961 #endif // !FEATURE_CORECLR
965 #ifndef DACCESS_COMPILE
966 #ifdef FEATURE_FUSION
969 IMetaDataEmit *pEmit,
970 IAssembly *pIAssembly,
971 IBindResult *pNativeFusionAssembly,
973 IFusionBindLog *pFusionLog,
974 IHostAssembly *pIHostAssembly,
977 BOOL introspectionOnly = FALSE,
978 ICLRPrivAssembly * pHostAssembly = NULL);
981 CoreBindResult* pBindResultInfo,
982 IMetaDataEmit *pEmit,
985 BOOL introspectionOnly = FALSE
986 #ifdef FEATURE_HOSTED_BINDER
988 PEImage * pPEImageIL = NULL,
989 PEImage * pPEImageNI = NULL,
990 ICLRPrivAssembly * pHostAssembly = NULL
994 virtual ~PEAssembly();
997 // ------------------------------------------------------------
999 // ------------------------------------------------------------
1001 friend class DomainAssembly;
1002 #ifdef FEATURE_PREJIT
1004 #ifdef FEATURE_FUSION
1005 void SetNativeImage(IBindResult *pNativeFusionAssembly);
1007 void SetNativeImage(PEImage *image);
1010 BOOL CheckNativeImageVersion(PEImage *image);
1013 #ifdef FEATURE_FUSION
1014 void ClearNativeImage();
1015 void SetNativeImageClosure(IAssemblyBindingClosure *pClosure)
1017 LIMITED_METHOD_CONTRACT;
1018 if (m_pNativeImageClosure!=NULL)
1019 m_pNativeImageClosure->Release();
1022 m_pNativeImageClosure=pClosure;
1024 BOOL HasEqualNativeClosure(DomainAssembly* pDomainAssembly);
1025 #endif //FEATURE_FUSION
1027 #endif // FEATURE_PREJIT
1030 // Check both the StrongName and Authenticode signature of an assembly. If the application is using
1031 // strong name bypass, then this call may not result in a strong name verificaiton. VerifyStrongName
1032 // should be called if a strong name must be forced to verify.
1033 void DoLoadSignatureChecks();
1037 // ------------------------------------------------------------
1039 // ------------------------------------------------------------
1041 PTR_PEFile m_creator;
1042 #ifdef FEATURE_FUSION
1043 IAssemblyName *m_pFusionAssemblyName;
1044 IAssembly *m_pFusionAssembly;
1045 IFusionBindLog *m_pFusionLog;
1046 BOOL m_bFusionLogEnabled;
1047 IHostAssembly *m_pIHostAssembly;
1048 IAssemblyLocation *m_pNativeAssemblyLocation;
1049 IAssemblyBindingClosure *m_pNativeImageClosure; //present only for shared
1050 LOADCTX_TYPE m_loadContext;
1051 DWORD m_dwLocationFlags;
1054 BOOL m_bIsOnTpaList;
1055 // Using a separate entry and not m_pHostAssembly because otherwise
1056 // HasHostAssembly becomes true that trips various other code paths resulting in bad
1058 SString m_sTextualIdentity;
1060 #ifdef FEATURE_CORECLR
1061 int m_fProfileAssembly; // Tri-state cache
1063 BOOL m_fStrongNameBypassed;
1067 PTR_PEFile GetCreator()
1068 { LIMITED_METHOD_CONTRACT; return m_creator; }
1070 // Returns TRUE if the assembly is .winmd file (WinRT assembly)
1071 bool IsWindowsRuntime();
1073 // Used to determine if this assembly has an identity that may be used for
1074 // binding purposes. Currently this is true for standard .NET assemblies
1075 // and false for WinRT assemblies (where assemblies are identified by their
1077 bool HasBindableIdentity();
1079 // Indicates if the assembly can be cached in a binding cache such as AssemblySpecBindingCache.
1080 inline bool CanUseWithBindingCache()
1082 #if defined(FEATURE_HOSTED_BINDER)
1083 STATIC_CONTRACT_WRAPPER;
1084 #if !defined(FEATURE_APPX_BINDER)
1085 return (HasBindableIdentity());
1087 return (PEFile::CanUseWithBindingCache() && HasBindableIdentity());
1088 #endif // FEATURE_CORECLR
1090 STATIC_CONTRACT_LIMITED_METHOD;
1092 #endif // FEATURE_HOSTED_BINDER
1096 #ifdef FEATURE_MULTIMODULE_ASSEMBLIES
1098 class PEModule : public PEFile
1100 VPTR_VTABLE_CLASS(PEModule, PEFile)
1104 // ------------------------------------------------------------
1106 // ------------------------------------------------------------
1108 static PEModule *Open(PEAssembly *assembly, mdFile token,
1109 const SString &fileName);
1111 static PEModule *OpenMemory(PEAssembly *assembly, mdFile kToken,
1112 const void *flat, COUNT_T size);
1114 static PEModule *Create(PEAssembly *assembly, mdFile kToken, IMetaDataEmit *pEmit);
1116 #ifdef DACCESS_COMPILE
1117 virtual void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
1121 // Private helpers for crufty exception handling reasons
1122 static PEModule *DoOpen(PEAssembly *assembly, mdFile token,
1123 const SString &fileName);
1125 static PEModule *DoOpenMemory(PEAssembly *assembly, mdFile kToken,
1126 const void *flat, COUNT_T size);
1129 // ------------------------------------------------------------
1131 // ------------------------------------------------------------
1133 PEAssembly *GetAssembly();
1137 LPCUTF8 GetSimpleName();
1139 // ------------------------------------------------------------
1141 // ------------------------------------------------------------
1142 #ifdef FEATURE_PREJIT
1143 void ExternalVLog(DWORD facility, DWORD level, const WCHAR *fmt, va_list args) DAC_EMPTY();
1144 void FlushExternalLog() DAC_EMPTY();
1147 // ------------------------------------------------------------
1148 // Loader access API
1149 // ------------------------------------------------------------
1151 friend class DomainModule;
1152 #ifdef FEATURE_PREJIT
1153 void SetNativeImage(const SString &fullPath);
1154 #endif // FEATURE_PREJIT
1158 #ifndef DACCESS_COMPILE
1159 PEModule(PEImage *image, PEAssembly *assembly, mdFile token, IMetaDataEmit *pEmit);
1160 virtual ~PEModule();
1163 // ------------------------------------------------------------
1165 // ------------------------------------------------------------
1167 PTR_PEAssembly m_assembly;
1171 #endif // FEATURE_MULTIMODULE_ASSEMBLIES
1173 typedef ReleaseHolder<PEFile> PEFileHolder;
1175 typedef ReleaseHolder<PEAssembly> PEAssemblyHolder;
1177 #ifdef FEATURE_MULTIMODULE_ASSEMBLIES
1178 typedef ReleaseHolder<PEModule> PEModuleHolder;
1179 #endif // FEATURE_MULTIMODULE_ASSEMBLIES
1182 // A small shim around PEAssemblies/IBindResult that allow us to write Fusion/CLR-agnostic
1183 // for logging native bind failures to the Fusion log/CLR log.
1185 // These structures are stack-based, non-thread-safe and created for the duration of a single RuntimeVerify call.
1186 // The methods are expected to compute their data lazily as they are only used in bind failures or in checked builds.
1187 class LoggablePEAssembly : public LoggableAssembly
1190 virtual SString DisplayString()
1192 STANDARD_VM_CONTRACT;
1194 return m_peAssembly->GetPath();
1197 #ifdef FEATURE_FUSION
1198 virtual IAssemblyName* FusionAssemblyName()
1200 STANDARD_VM_CONTRACT;
1202 return m_peAssembly->GetFusionAssemblyName();
1205 virtual IFusionBindLog* FusionBindLog()
1207 STANDARD_VM_CONTRACT;
1209 return m_peAssembly->GetFusionBindLog();
1211 #endif // FEATURE_FUSION
1213 LoggablePEAssembly(PEAssembly *peAssembly)
1215 LIMITED_METHOD_CONTRACT;
1217 m_peAssembly = peAssembly;
1218 peAssembly->AddRef();
1221 ~LoggablePEAssembly()
1223 LIMITED_METHOD_CONTRACT;
1225 m_peAssembly->Release();
1229 PEAssembly *m_peAssembly;
1233 // ================================================================================
1234 // Inline definitions
1235 // ================================================================================