| 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 |  | 
|---|
| 26 | class Crst; | 
|---|
| 27 | class PEImage; | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | typedef VPTR(class PEImageLayout) PTR_PEImageLayout; | 
|---|
| 31 |  | 
|---|
| 32 | class PEImageLayout : public PEDecoder | 
|---|
| 33 | { | 
|---|
| 34 | VPTR_BASE_CONCRETE_VTABLE_CLASS(PEImageLayout) | 
|---|
| 35 | friend class PEModule; | 
|---|
| 36 | public: | 
|---|
| 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 |  | 
|---|
| 50 | public: | 
|---|
| 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 |  | 
|---|
| 75 | public: | 
|---|
| 76 | #ifdef DACCESS_COMPILE | 
|---|
| 77 | void EnumMemoryRegions(CLRDataEnumMemoryFlags flags); | 
|---|
| 78 | #endif | 
|---|
| 79 |  | 
|---|
| 80 | private: | 
|---|
| 81 | Volatile<LONG> m_refCount; | 
|---|
| 82 | public: | 
|---|
| 83 | PEImage* m_pOwner; | 
|---|
| 84 | DWORD m_Layout; | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | typedef ReleaseHolder<PEImageLayout> PEImageLayoutHolder; | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 | //RawImageView is built on external data, does not need cleanup | 
|---|
| 91 | class RawImageLayout: public PEImageLayout | 
|---|
| 92 | { | 
|---|
| 93 | VPTR_VTABLE_CLASS(RawImageLayout,PEImageLayout) | 
|---|
| 94 | protected: | 
|---|
| 95 | CLRMapViewHolder m_DataCopy; | 
|---|
| 96 | #ifndef FEATURE_PAL | 
|---|
| 97 | HModuleHolder m_LibraryHolder; | 
|---|
| 98 | #endif // !FEATURE_PAL | 
|---|
| 99 |  | 
|---|
| 100 | public: | 
|---|
| 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 | 
|---|
| 106 | class ConvertedImageLayout: public PEImageLayout | 
|---|
| 107 | { | 
|---|
| 108 | VPTR_VTABLE_CLASS(ConvertedImageLayout,PEImageLayout) | 
|---|
| 109 | protected: | 
|---|
| 110 | HandleHolder m_FileMap; | 
|---|
| 111 | CLRMapViewHolder m_FileView; | 
|---|
| 112 | public: | 
|---|
| 113 | #ifndef DACCESS_COMPILE | 
|---|
| 114 | ConvertedImageLayout(PEImageLayout* source); | 
|---|
| 115 | #endif | 
|---|
| 116 | }; | 
|---|
| 117 |  | 
|---|
| 118 | class MappedImageLayout: public PEImageLayout | 
|---|
| 119 | { | 
|---|
| 120 | VPTR_VTABLE_CLASS(MappedImageLayout,PEImageLayout) | 
|---|
| 121 | VPTR_UNIQUE(0x15) | 
|---|
| 122 | protected: | 
|---|
| 123 | #ifndef FEATURE_PAL | 
|---|
| 124 | HandleHolder m_FileMap; | 
|---|
| 125 | CLRMapViewHolder m_FileView; | 
|---|
| 126 | #else | 
|---|
| 127 | PALPEFileHolder m_LoadedFile; | 
|---|
| 128 | #endif | 
|---|
| 129 | public: | 
|---|
| 130 | #ifndef DACCESS_COMPILE | 
|---|
| 131 | MappedImageLayout(HANDLE hFile, PEImage* pOwner); | 
|---|
| 132 | #endif | 
|---|
| 133 | }; | 
|---|
| 134 |  | 
|---|
| 135 | #if !defined(CROSSGEN_COMPILE) && !defined(FEATURE_PAL) | 
|---|
| 136 | class LoadedImageLayout: public PEImageLayout | 
|---|
| 137 | { | 
|---|
| 138 | VPTR_VTABLE_CLASS(LoadedImageLayout,PEImageLayout) | 
|---|
| 139 | protected: | 
|---|
| 140 | HINSTANCE m_Module; | 
|---|
| 141 | public: | 
|---|
| 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 |  | 
|---|
| 160 | class FlatImageLayout: public PEImageLayout | 
|---|
| 161 | { | 
|---|
| 162 | VPTR_VTABLE_CLASS(FlatImageLayout,PEImageLayout) | 
|---|
| 163 | VPTR_UNIQUE(0x59) | 
|---|
| 164 | protected: | 
|---|
| 165 | HandleHolder m_FileMap; | 
|---|
| 166 | CLRMapViewHolder m_FileView; | 
|---|
| 167 | public: | 
|---|
| 168 | #ifndef DACCESS_COMPILE | 
|---|
| 169 | FlatImageLayout(HANDLE hFile, PEImage* pOwner); | 
|---|
| 170 | #endif | 
|---|
| 171 |  | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 |  | 
|---|
| 176 | #endif  // PEIMAGELAYOUT_H_ | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|