1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// --------------------------------------------------------------------------------
5// PEImageLayout.h
6//
7
8// --------------------------------------------------------------------------------
9
10
11#ifndef PEIMAGELAYOUT_H_
12#define PEIMAGELAYOUT_H_
13
14// --------------------------------------------------------------------------------
15// Required headers
16// --------------------------------------------------------------------------------
17
18#include "clrtypes.h"
19#include "pedecoder.h"
20#include "holder.h"
21
22// --------------------------------------------------------------------------------
23// Forward declarations
24// --------------------------------------------------------------------------------
25
26class Crst;
27class PEImage;
28
29
30typedef VPTR(class PEImageLayout) PTR_PEImageLayout;
31
32class PEImageLayout : public PEDecoder
33{
34 VPTR_BASE_CONCRETE_VTABLE_CLASS(PEImageLayout)
35 friend class PEModule;
36public:
37 // ------------------------------------------------------------
38 // Public constants
39 // ------------------------------------------------------------
40 enum
41 {
42 LAYOUT_MAPPED =1,
43 LAYOUT_FLAT =2,
44 LAYOUT_LOADED =4,
45 LAYOUT_LOADED_FOR_INTROSPECTION =8,
46 LAYOUT_ANY =0xf
47 };
48
49
50public:
51#ifndef DACCESS_COMPILE
52 static PEImageLayout* CreateFlat(const void *flat, COUNT_T size,PEImage* pOwner);
53 static PEImageLayout* CreateFromStream(IStream* pIStream, PEImage* pOwner);
54 static PEImageLayout* CreateFromHMODULE(HMODULE mappedbase,PEImage* pOwner, BOOL bTakeOwnership);
55 static PEImageLayout* LoadFromFlat(PEImageLayout* pflatimage);
56 static PEImageLayout* Load(PEImage* pOwner, BOOL bNTSafeLoad, BOOL bThrowOnError = TRUE);
57 static PEImageLayout* LoadFlat(HANDLE hFile, PEImage* pOwner);
58 static PEImageLayout* Map (HANDLE hFile, PEImage* pOwner);
59#endif
60 PEImageLayout();
61 virtual ~PEImageLayout();
62 static void Startup();
63 static CHECK CheckStartup();
64 static BOOL CompareBase(UPTR path, UPTR mapping);
65
66 // Refcount above images.
67 void AddRef();
68 ULONG Release();
69 const SString& GetPath();
70
71#ifdef FEATURE_PREJIT
72 void ApplyBaseRelocations();
73#endif
74
75public:
76#ifdef DACCESS_COMPILE
77 void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
78#endif
79
80private:
81 Volatile<LONG> m_refCount;
82public:
83 PEImage* m_pOwner;
84 DWORD m_Layout;
85};
86
87typedef ReleaseHolder<PEImageLayout> PEImageLayoutHolder;
88
89
90//RawImageView is built on external data, does not need cleanup
91class RawImageLayout: public PEImageLayout
92{
93 VPTR_VTABLE_CLASS(RawImageLayout,PEImageLayout)
94protected:
95 CLRMapViewHolder m_DataCopy;
96#ifndef FEATURE_PAL
97 HModuleHolder m_LibraryHolder;
98#endif // !FEATURE_PAL
99
100public:
101 RawImageLayout(const void *flat, COUNT_T size,PEImage* pOwner);
102 RawImageLayout(const void *mapped, PEImage* pOwner, BOOL bTakeOwnerShip, BOOL bFixedUp);
103};
104
105// ConvertedImageView is for the case when we manually layout a flat image
106class ConvertedImageLayout: public PEImageLayout
107{
108 VPTR_VTABLE_CLASS(ConvertedImageLayout,PEImageLayout)
109protected:
110 HandleHolder m_FileMap;
111 CLRMapViewHolder m_FileView;
112public:
113#ifndef DACCESS_COMPILE
114 ConvertedImageLayout(PEImageLayout* source);
115#endif
116};
117
118class MappedImageLayout: public PEImageLayout
119{
120 VPTR_VTABLE_CLASS(MappedImageLayout,PEImageLayout)
121 VPTR_UNIQUE(0x15)
122protected:
123#ifndef FEATURE_PAL
124 HandleHolder m_FileMap;
125 CLRMapViewHolder m_FileView;
126#else
127 PALPEFileHolder m_LoadedFile;
128#endif
129public:
130#ifndef DACCESS_COMPILE
131 MappedImageLayout(HANDLE hFile, PEImage* pOwner);
132#endif
133};
134
135#if !defined(CROSSGEN_COMPILE) && !defined(FEATURE_PAL)
136class LoadedImageLayout: public PEImageLayout
137{
138 VPTR_VTABLE_CLASS(LoadedImageLayout,PEImageLayout)
139protected:
140 HINSTANCE m_Module;
141public:
142#ifndef DACCESS_COMPILE
143 LoadedImageLayout(PEImage* pOwner, BOOL bNTSafeLoad, BOOL bThrowOnError);
144 ~LoadedImageLayout()
145 {
146 CONTRACTL
147 {
148 NOTHROW;
149 GC_TRIGGERS;
150 MODE_ANY;
151 }
152 CONTRACTL_END;
153 if (m_Module)
154 CLRFreeLibrary(m_Module);
155 }
156#endif // !DACCESS_COMPILE
157};
158#endif // !CROSSGEN_COMPILE && !FEATURE_PAL
159
160class FlatImageLayout: public PEImageLayout
161{
162 VPTR_VTABLE_CLASS(FlatImageLayout,PEImageLayout)
163 VPTR_UNIQUE(0x59)
164protected:
165 HandleHolder m_FileMap;
166 CLRMapViewHolder m_FileView;
167public:
168#ifndef DACCESS_COMPILE
169 FlatImageLayout(HANDLE hFile, PEImage* pOwner);
170#endif
171
172};
173
174
175
176#endif // PEIMAGELAYOUT_H_
177
178