[281] | 1 | /* $Id: LX.H,v 1.4 1999-07-07 08:11:11 sandervl Exp $ */
|
---|
[97] | 2 |
|
---|
[4] | 3 | /*
|
---|
| 4 | * PE2LX LX
|
---|
| 5 | *
|
---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 7 | *
|
---|
| 8 | *
|
---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 10 | *
|
---|
| 11 | */
|
---|
| 12 | #ifndef __LX_H__
|
---|
| 13 | #define __LX_H__
|
---|
| 14 |
|
---|
| 15 | #include <win32type.h>
|
---|
| 16 | #include <exe386.h>
|
---|
| 17 |
|
---|
| 18 | #define LXHEADER_OFFSET 0x80
|
---|
| 19 | #define PAGE_SIZE 4096
|
---|
| 20 | #define PAGE_SHIFT 12
|
---|
| 21 |
|
---|
| 22 | #define SYSTEM_CHARACTER 0
|
---|
| 23 | #define SYSTEM_GUI 1
|
---|
| 24 |
|
---|
| 25 | #define SECTION_CODE 1
|
---|
| 26 | #define SECTION_INITDATA 2
|
---|
| 27 | #define SECTION_UNINITDATA 4
|
---|
| 28 | #define SECTION_READONLYDATA 8
|
---|
| 29 | #define SECTION_IMPORT 16
|
---|
| 30 | #define SECTION_STACK 32
|
---|
| 31 | #define SECTION_RESOURCE_ORG 64
|
---|
| 32 | #define SECTION_RESOURCE 128
|
---|
| 33 | #define SECTION_COMBINEDDATA 256
|
---|
| 34 | #define MAX_SECTION 64 /*PLF Mon 98-02-09 23:47:16*/
|
---|
| 35 |
|
---|
| 36 | #define MAX_IMPORT 8192 //this should do for most bloated apps
|
---|
| 37 | #define MAX_RESOURCE 8192
|
---|
| 38 |
|
---|
| 39 | typedef struct {
|
---|
| 40 | char *rawdata;
|
---|
| 41 | int rawsize;
|
---|
| 42 | int virtualsize;
|
---|
| 43 | int curoff;
|
---|
| 44 | int type;
|
---|
| 45 | int address;
|
---|
| 46 | int endaddress;
|
---|
| 47 | int nrpages;
|
---|
| 48 | int nrinvalidpages;
|
---|
| 49 | BOOL fProcessed;
|
---|
| 50 | BOOL fInvalid;
|
---|
| 51 | } Section;
|
---|
| 52 |
|
---|
| 53 | typedef struct {
|
---|
| 54 | int size;
|
---|
| 55 | int flags;
|
---|
| 56 | } DataPage;
|
---|
| 57 |
|
---|
| 58 | #pragma pack(1)
|
---|
| 59 |
|
---|
| 60 | //also defined in USER32\NAMEID.H!
|
---|
| 61 | #define RESID_CONVERTEDNAMES 63*1024
|
---|
| 62 | typedef struct {
|
---|
| 63 | int id;
|
---|
| 64 | char name[1];
|
---|
| 65 | } NameId;
|
---|
| 66 |
|
---|
| 67 | typedef struct {
|
---|
| 68 | unsigned char nr_stype; /* Source type - field shared with new_rlc */
|
---|
| 69 | unsigned char nr_flags; /* Flag byte - field shared with new_rlc */
|
---|
| 70 | short r32_soff; /* Source offset */
|
---|
| 71 | unsigned short r32_objmod; /* Target object number or Module ordinal */
|
---|
| 72 | unsigned short proc; /* Procedure name offset */
|
---|
| 73 | unsigned short srcpage;
|
---|
| 74 | } namefixup;
|
---|
| 75 |
|
---|
| 76 | typedef struct {
|
---|
| 77 | unsigned char nr_stype; /* Source type - field shared with new_rlc */
|
---|
| 78 | unsigned char nr_flags; /* Flag byte - field shared with new_rlc */
|
---|
| 79 | short r32_soff; /* Source offset */
|
---|
| 80 | unsigned short r32_objmod; /* Target object number or Module ordinal */
|
---|
| 81 | unsigned short proc; /* Procedure name offset */
|
---|
| 82 | } realnamefixup;
|
---|
| 83 |
|
---|
| 84 | typedef struct {
|
---|
| 85 | unsigned char nr_stype; /* Source type - field shared with new_rlc */
|
---|
| 86 | unsigned char nr_flags; /* Flag byte - field shared with new_rlc */
|
---|
| 87 | short r32_soff; /* Source offset */
|
---|
| 88 | unsigned short r32_objmod; /* Target object number or Module ordinal */
|
---|
| 89 | unsigned short ord; /* Ordinal */
|
---|
| 90 | unsigned short srcpage;
|
---|
| 91 | } ordfixup;
|
---|
| 92 |
|
---|
| 93 | typedef struct {
|
---|
| 94 | unsigned char nr_stype; /* Source type - field shared with new_rlc */
|
---|
| 95 | unsigned char nr_flags; /* Flag byte - field shared with new_rlc */
|
---|
| 96 | short r32_soff; /* Source offset */
|
---|
| 97 | unsigned short r32_objmod; /* Target object number or Module ordinal */
|
---|
| 98 | unsigned short ord; /* Ordinal */
|
---|
| 99 | } realordfixup;
|
---|
| 100 |
|
---|
| 101 | typedef struct {
|
---|
| 102 | unsigned char nr_stype; /* Source type - field shared with new_rlc */
|
---|
| 103 | unsigned char nr_flags; /* Flag byte - field shared with new_rlc */
|
---|
| 104 | short r32_soff; /* Source offset */
|
---|
| 105 | unsigned short targetobj;
|
---|
| 106 | unsigned long targetaddr;
|
---|
| 107 | unsigned short srcobj;
|
---|
| 108 | unsigned short srcpage;
|
---|
| 109 | } intfixup;
|
---|
| 110 |
|
---|
| 111 | typedef struct {
|
---|
| 112 | unsigned char nr_stype; /* Source type - field shared with new_rlc */
|
---|
| 113 | unsigned char nr_flags; /* Flag byte - field shared with new_rlc */
|
---|
| 114 | short r32_soff; /* Source offset */
|
---|
| 115 | unsigned short targetobj;
|
---|
| 116 | unsigned long targetaddr;
|
---|
| 117 | } realintfixup;
|
---|
| 118 |
|
---|
| 119 | typedef struct
|
---|
| 120 | {
|
---|
| 121 | unsigned char b32_cnt; /* Number of entries in this bundle */
|
---|
| 122 | unsigned char b32_type; /* Bundle type */
|
---|
| 123 | unsigned short b32_obj; /* Object number */
|
---|
| 124 |
|
---|
| 125 | unsigned char e32_flags; /* Entry point flags */
|
---|
| 126 | unsigned long e32_offset; /* 16-bit/32-bit offset entry */
|
---|
| 127 | } exportbundle;
|
---|
| 128 |
|
---|
| 129 | typedef struct
|
---|
| 130 | {
|
---|
| 131 | unsigned char b32_cnt; /* Number of entries in this bundle */
|
---|
| 132 | unsigned char b32_type; /* Bundle type */
|
---|
| 133 | unsigned short b32_obj; /* Object number */
|
---|
| 134 |
|
---|
| 135 | unsigned char e32_flags; /* Entry point flags */
|
---|
| 136 | unsigned short modord; /* Module ordinal number */
|
---|
| 137 | unsigned long value; /* Proc name offset or ordinal */
|
---|
| 138 | } forwardbundle;
|
---|
| 139 |
|
---|
| 140 | #pragma pack()
|
---|
| 141 |
|
---|
| 142 | class LXHeader {
|
---|
| 143 | public:
|
---|
| 144 | LXHeader();
|
---|
| 145 | ~LXHeader();
|
---|
| 146 |
|
---|
[281] | 147 | void SetEntryAddress(int address); //relative
|
---|
| 148 |
|
---|
| 149 | void SetEntryPoint(int address) { EntryAddress = address; }; //absolute virtual address
|
---|
| 150 |
|
---|
[4] | 151 | void SetExeType(BOOL IsEXE);
|
---|
| 152 | void SetNoFixups();
|
---|
| 153 | void SetStackSize(int size);
|
---|
| 154 | void SetResourceSize(int size);
|
---|
| 155 | void SetNrResources(int cnt);
|
---|
| 156 | void StoreSection(char *rawdata, int rawsize, int virtualsize, int address, int type);
|
---|
| 157 | void AlignSections();
|
---|
| 158 | BOOL StoreImportByName(char *modname, int idxmod, char *name, int offset);
|
---|
| 159 | BOOL StoreImportByOrd(int idxmod, int ordinal, int offset);
|
---|
| 160 | void StoreImportModules(char *modules, int nrmod);
|
---|
| 161 | void SetNoNameImports();
|
---|
| 162 | BOOL SaveNewExeFile(char *filename);
|
---|
| 163 | void SetModuleType(int type);
|
---|
[281] | 164 | void AddOff32Fixup(int address, BOOL fLookatStartupCode = FALSE);
|
---|
[4] | 165 | void SetNrOff32Fixups(int nr);
|
---|
| 166 | void AddNameExport(int address, char *name, int ordinal);
|
---|
| 167 | void AddOrdExport(int address, int ordinal);
|
---|
| 168 | void SetNrExtFixups(int nr);
|
---|
| 169 | void AddForwarder(char *name, int ordinal, char *forward);
|
---|
| 170 |
|
---|
| 171 | int GetAddressPage(int address);
|
---|
| 172 | void SetModuleName(char *filename);
|
---|
| 173 | //resource procedures
|
---|
| 174 | void StoreResource(int id, int type, int size, char *resourcedata);
|
---|
| 175 | void StoreWin32Resource(int id, int type, int size, char *resourcedata);
|
---|
| 176 | int ConvertNametoId(char *nameid);
|
---|
| 177 | void SaveConvertedNames();
|
---|
| 178 | void StoreResourceId(int id);
|
---|
| 179 | int GetUniqueId();
|
---|
| 180 |
|
---|
| 181 | //SvL: 18-7-'98: Set version resource id
|
---|
| 182 | void SetVersionResourceId(int id) { VersionResourceId = id; };
|
---|
| 183 |
|
---|
[281] | 184 | void SetTLSAddress(ULONG dwTlsAddress) { tlsAddress = dwTlsAddress; };
|
---|
| 185 | void SetTLSIndexAddress(ULONG dwTlsIndexAddr) { tlsIndexAddr = dwTlsIndexAddr; };
|
---|
| 186 | void SetTLSInitSize(ULONG dwTlsSize) { tlsInitSize = dwTlsSize; };
|
---|
| 187 | void SetTLSTotalSize(ULONG dwTlsSize) { tlsTotalSize = dwTlsSize; };
|
---|
| 188 | void SetTLSCallBackAddr(ULONG dwTlsCallBackAddr) { tlsCallBackAddr = dwTlsCallBackAddr; };
|
---|
| 189 |
|
---|
| 190 | void AddExtraFixups();
|
---|
| 191 |
|
---|
[4] | 192 | private:
|
---|
| 193 | BOOL IsSystemModule(char *mod, int size);
|
---|
| 194 | int FindName(char *table, int index);
|
---|
| 195 | char *StripPath(char *path);
|
---|
| 196 | char *StripExtension(char *fname);
|
---|
| 197 |
|
---|
| 198 | void StoreAndSortImport(namefixup *newfrec);
|
---|
| 199 |
|
---|
| 200 | int GetNrPages();
|
---|
| 201 | int GetNrObjects();
|
---|
| 202 | void UpCase(char *mixedcase);
|
---|
| 203 | int GetSection(int type);
|
---|
| 204 | int GetSectionByAddress(int address);
|
---|
| 205 | int GetRealSectionByAddress(int address);
|
---|
| 206 | int GetSectionLEAddress(int address);
|
---|
| 207 |
|
---|
| 208 | BOOL IsEXE, fConsole;
|
---|
| 209 | ULONG EntryAddress;
|
---|
| 210 | Section PESection[MAX_SECTION];
|
---|
| 211 | Section ResSection;
|
---|
| 212 | DataPage *datapage;
|
---|
| 213 |
|
---|
| 214 | char *impmodules, *impnames, *impnameoff;
|
---|
| 215 | int impmodulesize, impnamesize;
|
---|
| 216 | int fFlags;
|
---|
| 217 | int StackSize;
|
---|
| 218 | int nrsections;
|
---|
| 219 |
|
---|
| 220 | namefixup *impfixuprec;
|
---|
| 221 | int nrimpfixups;
|
---|
| 222 |
|
---|
| 223 | intfixup *intfixuprec;
|
---|
| 224 | int nrintfixups;
|
---|
| 225 |
|
---|
| 226 | rsrc32 *os2resource;
|
---|
| 227 | int nrresources;
|
---|
| 228 | int curresource;
|
---|
| 229 | NameId *cvtname, *curcvtname;
|
---|
| 230 | int nrcvtnames;
|
---|
| 231 | int *resids;
|
---|
| 232 | int nrids;
|
---|
| 233 | int cvtnametableid;
|
---|
| 234 | int orgrestableid;
|
---|
| 235 | ULONG *orgrestable;
|
---|
| 236 | int nrorgres;
|
---|
| 237 |
|
---|
| 238 | char *exports, *curexport;
|
---|
| 239 | int nrexports;
|
---|
| 240 | exportbundle *expbundle, *curexpb;
|
---|
| 241 |
|
---|
| 242 | char modulename[128];
|
---|
| 243 |
|
---|
| 244 | struct e32_exe LXHdr;
|
---|
| 245 |
|
---|
| 246 | static int uniqueId;
|
---|
| 247 |
|
---|
| 248 | int kernel32Object;
|
---|
| 249 |
|
---|
| 250 | //SvL: 18-7-'98: Version resource id
|
---|
| 251 | ULONG VersionResourceId;
|
---|
| 252 |
|
---|
[281] | 253 | ULONG tlsAddress; //address of TLS data
|
---|
| 254 | ULONG tlsIndexAddr; //address of DWORD that receives the TLS index
|
---|
| 255 | ULONG tlsInitSize; //size of initialized TLS memory block
|
---|
| 256 | ULONG tlsTotalSize; //size of TLS memory block
|
---|
| 257 | ULONG tlsCallBackAddr; //ptr to TLS callback array
|
---|
[4] | 258 | };
|
---|
| 259 |
|
---|
| 260 | extern LXHeader OS2Exe;
|
---|
| 261 |
|
---|
| 262 | #endif
|
---|