[10397] | 1 | /* $Id: winimagepeldr.h,v 1.21 2004-01-15 10:39:15 sandervl Exp $ */
|
---|
[3074] | 2 |
|
---|
| 3 | /*
|
---|
| 4 | * Win32 PE loader Image base class
|
---|
| 5 | *
|
---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 10 | *
|
---|
| 11 | */
|
---|
| 12 | #ifndef __WINIMAGEPELDR_H__
|
---|
| 13 | #define __WINIMAGEPELDR_H__
|
---|
| 14 |
|
---|
[21916] | 15 | #include "winimagebase.h"
|
---|
[3074] | 16 |
|
---|
| 17 | #define SINGLE_PAGE 0 //commit single page
|
---|
| 18 | #define COMPLETE_SECTION 1 //commit entire section
|
---|
| 19 | #define SECTION_PAGES 2 //commit default nr of pages
|
---|
| 20 |
|
---|
| 21 | #define DEFAULT_NR_PAGES 16 //default nr of pages to commit during exception
|
---|
| 22 |
|
---|
| 23 | //SvL: To load a dll/exe for i.e. getting a single resource (GetVersionSize/Resource)
|
---|
[4474] | 24 | #define FLAG_PELDR_LOADASDATAFILE 1 //also implies FLAG_PELDR_SKIPIMPORTS
|
---|
| 25 | #define FLAG_PELDR_SKIPIMPORTS 2
|
---|
[3074] | 26 |
|
---|
| 27 | //SvL: Amount of memory the peldr dll reserves for win32 exes without fixups
|
---|
| 28 | //(most of them need to be loaded at 4 MB; except MS Office apps of course)
|
---|
| 29 | #define PELDR_RESERVEDMEMSIZE 32*1024*1024
|
---|
| 30 |
|
---|
| 31 | #define ERROR_INTERNAL 1
|
---|
| 32 |
|
---|
| 33 | #define SECTION_CODE 1
|
---|
| 34 | #define SECTION_INITDATA 2
|
---|
| 35 | #define SECTION_UNINITDATA 4
|
---|
| 36 | #define SECTION_READONLYDATA 8
|
---|
| 37 | #define SECTION_IMPORT 16
|
---|
| 38 | #define SECTION_RESOURCE 32
|
---|
| 39 | #define SECTION_RELOC 64
|
---|
| 40 | #define SECTION_EXPORT 128
|
---|
| 41 | #define SECTION_DEBUG 256
|
---|
| 42 | #define SECTION_TLS 512
|
---|
| 43 |
|
---|
| 44 | #define PAGE_SIZE 4096
|
---|
| 45 |
|
---|
| 46 | typedef struct {
|
---|
| 47 | ULONG rawoffset;
|
---|
| 48 | ULONG rawsize;
|
---|
| 49 | ULONG virtaddr;
|
---|
| 50 | ULONG realvirtaddr; //as allocated in OS/2
|
---|
| 51 | ULONG virtualsize;
|
---|
| 52 | ULONG type;
|
---|
| 53 | ULONG pageflags;
|
---|
| 54 | ULONG flags; //psh[i].Characteristics
|
---|
| 55 | } Section;
|
---|
| 56 |
|
---|
[5862] | 57 | typedef struct {
|
---|
[5837] | 58 | ULONG virtaddr;
|
---|
| 59 | ULONG ordinal;
|
---|
| 60 | ULONG nlength;
|
---|
| 61 | char name[4];
|
---|
| 62 | } NameExport;
|
---|
| 63 |
|
---|
[5862] | 64 | typedef struct {
|
---|
[5837] | 65 | ULONG virtaddr;
|
---|
| 66 | ULONG ordinal;
|
---|
| 67 | } OrdExport;
|
---|
| 68 |
|
---|
[3074] | 69 | class Win32DllBase;
|
---|
| 70 | class Win32MemMap;
|
---|
| 71 |
|
---|
| 72 | class Win32PeLdrImage : public virtual Win32ImageBase
|
---|
| 73 | {
|
---|
| 74 | public:
|
---|
[4474] | 75 | Win32PeLdrImage(char *szFileName, BOOL isExe);
|
---|
[3074] | 76 | virtual ~Win32PeLdrImage();
|
---|
| 77 |
|
---|
[8913] | 78 | //reservedMem: address of memory reserved in peldr.dll (allocated before
|
---|
| 79 | // any dlls are loaded, so that exes without fixups can be
|
---|
| 80 | // loaded at a low address)
|
---|
| 81 | //ulPEOffset: offset in file where real PE image starts
|
---|
[9537] | 82 | virtual DWORD init(ULONG reservedMem, ULONG ulPEOffset = 0);
|
---|
[3074] | 83 |
|
---|
[3483] | 84 | virtual BOOL insideModule(ULONG address);
|
---|
| 85 | virtual BOOL insideModuleCode(ULONG address);
|
---|
| 86 |
|
---|
[3609] | 87 | virtual ULONG getImageSize();
|
---|
| 88 |
|
---|
[3074] | 89 | //Returns required OS version for this image
|
---|
| 90 | virtual ULONG getVersion();
|
---|
| 91 |
|
---|
[4474] | 92 | //tell loader to only process resources and ignore the rest
|
---|
| 93 | void setLoadAsDataFile() { dwFlags |= FLAG_PELDR_LOADASDATAFILE; };
|
---|
| 94 | void disableImportHandling() { dwFlags |= FLAG_PELDR_SKIPIMPORTS; };
|
---|
| 95 |
|
---|
[3074] | 96 | //commits image page(s) when an access violation exception is dispatched
|
---|
| 97 | BOOL commitPage(ULONG virtAddress, BOOL fWriteAccess, int fPageCmd = SECTION_PAGES);
|
---|
| 98 |
|
---|
| 99 | protected:
|
---|
[5266] | 100 | void StoreImportByOrd(Win32ImageBase *WinImage, ULONG ordinal, ULONG impaddr);
|
---|
| 101 | void StoreImportByName(Win32ImageBase *WinImage, char *impname, ULONG impaddr);
|
---|
[3074] | 102 |
|
---|
| 103 | void addSection(ULONG type, ULONG rawoffset, ULONG rawsize, ULONG virtaddress, ULONG virtsize, ULONG flags);
|
---|
| 104 | BOOL allocSections(ULONG reservedMem);
|
---|
| 105 | BOOL allocFixedMem(ULONG reservedMem);
|
---|
| 106 | Section *findSection(ULONG type);
|
---|
| 107 | Section *findSectionByAddr(ULONG addr);
|
---|
| 108 | Section *findSectionByOS2Addr(ULONG addr);
|
---|
| 109 | Section *findPreviousSectionByOS2Addr(ULONG addr);
|
---|
| 110 |
|
---|
| 111 | BOOL setMemFlags();
|
---|
| 112 | BOOL setFixups(PIMAGE_BASE_RELOCATION prel);
|
---|
| 113 | BOOL setFixups(ULONG virtAddress, ULONG size);
|
---|
| 114 | void AddOff32Fixup(ULONG fixupaddr);
|
---|
| 115 | void AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup);
|
---|
| 116 |
|
---|
[9997] | 117 | BOOL processImports();
|
---|
| 118 | BOOL processExports();
|
---|
[3074] | 119 |
|
---|
[10397] | 120 | BOOL loadForwarder(ULONG virtaddr, char *apiname, ULONG ordinal);
|
---|
| 121 |
|
---|
[5837] | 122 | Win32DllBase *loadDll(char *pszCurModule);
|
---|
[4422] | 123 |
|
---|
[3074] | 124 | ULONG nrsections, imageSize, imageVirtBase, imageVirtEnd;
|
---|
| 125 | //OS/2 virtual base address
|
---|
[10397] | 126 | ULONG realBaseAddress, originalBaseAddress;
|
---|
| 127 | Section *section;
|
---|
| 128 | ULONG Characteristics;
|
---|
[3074] | 129 |
|
---|
[8913] | 130 | //offset in executable image where real PE file starts (default 0)
|
---|
| 131 | ULONG ulPEOffset;
|
---|
| 132 |
|
---|
[4474] | 133 | //internal flags (see FLAGS_PELDR_*)
|
---|
| 134 | DWORD dwFlags;
|
---|
[3074] | 135 |
|
---|
| 136 | HFILE hFile;
|
---|
| 137 |
|
---|
| 138 | PIMAGE_BASE_RELOCATION pFixups;
|
---|
[3761] | 139 | DWORD dwFixupSize;
|
---|
[3074] | 140 |
|
---|
| 141 | Win32MemMap *memmap;
|
---|
[9997] | 142 | LPVOID peview;
|
---|
[3074] | 143 | private:
|
---|
| 144 | };
|
---|
| 145 |
|
---|
| 146 | #endif //__WINIMAGEPELDR_H__
|
---|
| 147 |
|
---|