summaryrefslogtreecommitdiff
path: root/packaging/0001-Extract-PEImage-CreateLayoutMapped-and-PEImage-Creat.patch
blob: d51706bbcce10292984a1de2721be0ee5dd197f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
From 550c59a96baa6714a32dd649795eb4e5294df18b Mon Sep 17 00:00:00 2001
From: Ruben Ayrapetyan <r.ayrapetyan@samsung.com>
Date: Fri, 7 Apr 2017 13:25:42 +0300
Subject: [PATCH 01/32] Extract PEImage::CreateLayoutMapped and
 PEImage::CreateLayoutFlat from PEImage::GetLayoutInternal.

---
 src/vm/peimage.cpp | 138 +++++++++++++++++++++++++++++++++--------------------
 src/vm/peimage.h   |   6 +++
 2 files changed, 92 insertions(+), 52 deletions(-)

diff --git a/src/vm/peimage.cpp b/src/vm/peimage.cpp
index 3367ef9..1462c94 100644
--- a/src/vm/peimage.cpp
+++ b/src/vm/peimage.cpp
@@ -911,68 +911,102 @@ PTR_PEImageLayout PEImage::GetLayoutInternal(DWORD imageLayoutMask,DWORD flags)
 
         if (imageLayoutMask&PEImageLayout::LAYOUT_MAPPED)
         {
-            PEImageLayout * pLoadLayout = NULL;
+          pRetVal = PEImage::CreateLayoutMapped();
+        }
+        else
+        {
+          pRetVal = PEImage::CreateLayoutFlat();
+        }
+    }
 
-            if (m_bIsTrustedNativeImage || IsFile())
-            {
-                // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using 
-                // regular mapping - but not for native images.
-                pLoadLayout = PEImageLayout::Load(this, TRUE /* bNTSafeLoad */, m_bIsTrustedNativeImage /* bThrowOnError */);
-            }
+    if (pRetVal != NULL)
+    {
+        pRetVal->AddRef();
+    }
 
-            if (pLoadLayout != NULL)
-            {
-                SetLayout(IMAGE_MAPPED,pLoadLayout);
-                pLoadLayout->AddRef();
-                SetLayout(IMAGE_LOADED,pLoadLayout);
-                pRetVal=pLoadLayout;
-            }
-            else
-            if (IsFile())
-            {
-                PEImageLayoutHolder pLayout(PEImageLayout::Map(GetFileHandle(),this));
-
-                bool fMarkAnyCpuImageAsLoaded = false;
-                // Avoid mapping another image if we can.   We can only do this for IL-ONLY images
-                // since LoadLibrary is needed if we are to actually load code.  
-                if (pLayout->HasCorHeader() && pLayout->IsILOnly())
-                {    
-                    // For CoreCLR, IL only images will always be mapped. We also dont bother doing the conversion of PE header on 64bit,
-                    // as done below for the desktop case, as there is no appcompat burden for CoreCLR on 64bit to have that conversion done.
-                    fMarkAnyCpuImageAsLoaded = true;
-                }
+    return pRetVal;
+}
 
-                pLayout.SuppressRelease();
+PTR_PEImageLayout PEImage::CreateLayoutMapped()
+{
+    CONTRACTL
+    {
+        THROWS;
+        GC_TRIGGERS;
+        MODE_ANY;
+        PRECONDITION(m_pLayoutLock->IsWriterLock());
+    }
+    CONTRACTL_END;
 
-                SetLayout(IMAGE_MAPPED,pLayout);
-                if (fMarkAnyCpuImageAsLoaded)
-                {
-                    pLayout->AddRef();
-                    SetLayout(IMAGE_LOADED, pLayout);
-                }
-                pRetVal=pLayout;
-            }
-            else
-            {
-                PEImageLayoutHolder flatPE(GetLayoutInternal(PEImageLayout::LAYOUT_FLAT,LAYOUT_CREATEIFNEEDED));
-                if (!flatPE->CheckFormat())
-                    ThrowFormat(COR_E_BADIMAGEFORMAT);
-                pRetVal=PEImageLayout::LoadFromFlat(flatPE);
-                SetLayout(IMAGE_MAPPED,pRetVal);
-            }
+    PTR_PEImageLayout pRetVal;
+
+    PEImageLayout * pLoadLayout = NULL;
+
+    if (m_bIsTrustedNativeImage || IsFile())
+    {
+        // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using 
+        // regular mapping - but not for native images.
+        pLoadLayout = PEImageLayout::Load(this, TRUE /* bNTSafeLoad */, m_bIsTrustedNativeImage /* bThrowOnError */);
+    }
+
+    if (pLoadLayout != NULL)
+    {
+        SetLayout(IMAGE_MAPPED,pLoadLayout);
+        pLoadLayout->AddRef();
+        SetLayout(IMAGE_LOADED,pLoadLayout);
+        pRetVal=pLoadLayout;
+    }
+    else if (IsFile())
+    {
+        PEImageLayoutHolder pLayout(PEImageLayout::Map(GetFileHandle(),this));
+
+        bool fMarkAnyCpuImageAsLoaded = false;
+        // Avoid mapping another image if we can. We can only do this for IL-ONLY images
+        // since LoadLibrary is needed if we are to actually load code
+        if (pLayout->HasCorHeader() && pLayout->IsILOnly())
+        {
+            // For CoreCLR, IL only images will always be mapped. We also dont bother doing the conversion of PE header on 64bit,
+            // as done below for the desktop case, as there is no appcompat burden for CoreCLR on 64bit to have that conversion done.
+            fMarkAnyCpuImageAsLoaded = true;
         }
-        else
-        if (imageLayoutMask&PEImageLayout::LAYOUT_FLAT)
+
+        pLayout.SuppressRelease();
+
+        SetLayout(IMAGE_MAPPED,pLayout);
+        if (fMarkAnyCpuImageAsLoaded)
         {
-            pRetVal=PEImageLayout::LoadFlat(GetFileHandle(),this);
-            m_pLayouts[IMAGE_FLAT]=pRetVal;
+            pLayout->AddRef();
+            SetLayout(IMAGE_LOADED, pLayout);
         }
-        
+        pRetVal=pLayout;
     }
-    if (pRetVal)
+    else
     {
-        pRetVal->AddRef();
+        PEImageLayoutHolder flatPE(GetLayoutInternal(PEImageLayout::LAYOUT_FLAT,LAYOUT_CREATEIFNEEDED));
+        if (!flatPE->CheckFormat())
+            ThrowFormat(COR_E_BADIMAGEFORMAT);
+        pRetVal=PEImageLayout::LoadFromFlat(flatPE);
+        SetLayout(IMAGE_MAPPED,pRetVal);
     }
+
+    return pRetVal;
+}
+
+PTR_PEImageLayout PEImage::CreateLayoutFlat()
+{
+    CONTRACTL
+    {
+        GC_TRIGGERS;
+        MODE_ANY;
+        PRECONDITION(m_pLayoutLock->IsWriterLock());
+    }
+    CONTRACTL_END;
+
+    PTR_PEImageLayout pRetVal;
+
+    pRetVal = PEImageLayout::LoadFlat(GetFileHandle(),this);
+    m_pLayouts[IMAGE_FLAT] = pRetVal;
+
     return pRetVal;
 }
 
diff --git a/src/vm/peimage.h b/src/vm/peimage.h
index 3245621..f61e185 100644
--- a/src/vm/peimage.h
+++ b/src/vm/peimage.h
@@ -257,6 +257,12 @@ private:
 #ifndef DACCESS_COMPILE
     // Get or create the layout corresponding to the mask, with an AddRef
     PTR_PEImageLayout GetLayoutInternal(DWORD imageLayoutMask, DWORD flags); 
+
+    // Create the mapped layout
+    PTR_PEImageLayout CreateLayoutMapped();
+
+    // Create the flat layout
+    PTR_PEImageLayout CreateLayoutFlat();
 #endif
     // Get an existing layout corresponding to the mask, no AddRef
     PTR_PEImageLayout GetExistingLayoutInternal(DWORD imageLayoutMask);
-- 
2.7.4