Fix for issue 3146: dotnet build is slower on Linux than Windows (#5524)
authorFadi Hanna <fadim@microsoft.com>
Tue, 7 Jun 2016 16:32:01 +0000 (09:32 -0700)
committerFadi Hanna <fadim@microsoft.com>
Tue, 7 Jun 2016 16:32:01 +0000 (09:32 -0700)
Fixing the loader logic on Unix to mimic the behavior on Windows,
in order to be able to successfully load and use R2R images on
Unix where the file extension does not have the ".ni." portion.

src/vm/peimagelayout.cpp

index b5d50b5..2b50ade 100644 (file)
@@ -446,6 +446,7 @@ MappedImageLayout::MappedImageLayout(HANDLE hFile, PEImage* pOwner)
     PEFingerprintVerificationHolder verifyHolder(pOwner);  // Do not remove: This holder ensures the IL file hasn't changed since the runtime started making assumptions about it.
 
 #ifndef FEATURE_PAL
+
 #ifndef FEATURE_CORECLR
     ETWLoaderMappingPhaseHolder loaderMappingPhaseHolder;
     if (ETW_TRACING_CATEGORY_ENABLED(MICROSOFT_WINDOWS_DOTNETRUNTIME_PRIVATE_PROVIDER_Context, TRACE_LEVEL_INFORMATION, CLR_PRIVATEBINDING_KEYWORD)) {
@@ -575,28 +576,34 @@ MappedImageLayout::MappedImageLayout(HANDLE hFile, PEImage* pOwner)
 
 #else //!FEATURE_PAL
 
-#ifdef FEATURE_PREJIT
-    if (pOwner->IsTrustedNativeImage())
+    m_FileView = PAL_LOADLoadPEFile(hFile);
+    if (m_FileView == NULL)
     {
-        m_FileView = PAL_LOADLoadPEFile(hFile);
-        if (m_FileView == NULL)
+        // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using 
+        // regular mapping - but not for native images.
+        if (pOwner->IsTrustedNativeImage())
             ThrowHR(E_FAIL); // we don't have any indication of what kind of failure. Possibly a corrupt image.
+        return;
+    }
 
-        LOG((LF_LOADER, LL_INFO1000, "PEImage: image %S (hFile %p) mapped @ %p\n",
-            (LPCWSTR) GetPath(), hFile, (void*)m_FileView));
+    LOG((LF_LOADER, LL_INFO1000, "PEImage: image %S (hFile %p) mapped @ %p\n",
+        (LPCWSTR) GetPath(), hFile, (void*)m_FileView));
 
-        TESTHOOKCALL(ImageMapped(GetPath(),m_FileView,IM_IMAGEMAP));            
-        IfFailThrow(Init((void *) m_FileView));
+    TESTHOOKCALL(ImageMapped(GetPath(),m_FileView,IM_IMAGEMAP));            
+    IfFailThrow(Init((void *) m_FileView));
 
-        if (!IsNativeMachineFormat() || !HasCorHeader() || (!HasNativeHeader() && !HasReadyToRunHeader()))
-             ThrowHR(COR_E_BADIMAGEFORMAT);
+    if (!HasCorHeader())
+        ThrowHR(COR_E_BADIMAGEFORMAT);
 
+    if (HasNativeHeader() || HasReadyToRunHeader())
+    {
         //Do base relocation for PE, if necessary.
+        if (!IsNativeMachineFormat())
+            ThrowHR(COR_E_BADIMAGEFORMAT);
+
         ApplyBaseRelocations();
+        SetRelocated();
     }
-#else //FEATURE_PREJIT
-    //Do nothing.  The file cannot be mapped unless it is an ngen image.
-#endif //FEATURE_PREJIT
 
 #endif // !FEATURE_PAL
 }