1 | #include "../BasicCompiler_Common/common.h"
|
---|
2 | #include "Opcode.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | ////////////////////////////
|
---|
6 | // 特殊関数の構造体ポインタ
|
---|
7 | ////////////////////////////
|
---|
8 | UserProc
|
---|
9 | *pSub_System_StartupProgram,
|
---|
10 | *pSub_DebugSys_StartProc,
|
---|
11 | *pSub_DebugSys_EndProc,
|
---|
12 | *pSub_DebugSys_SaveContext,
|
---|
13 | *pSub_System_GetEip,
|
---|
14 | *pSub_System_InitDllGlobalVariables,
|
---|
15 | *pSub_System_InitStaticLocalVariables,
|
---|
16 | *pSub_System_Call_Destructor_of_GlobalObject,
|
---|
17 | *pSub_System_GetSp,
|
---|
18 | *pSub_pow,
|
---|
19 | *pSub_calloc,
|
---|
20 | *pSub_realloc,
|
---|
21 | *pSub_free,
|
---|
22 | *pSub_System_GC_malloc_ForObject,
|
---|
23 | *pSub_System_GC_malloc_ForObjectPtr,
|
---|
24 | *pSub_System_GC_free_for_SweepingDelete,
|
---|
25 | *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
|
---|
26 |
|
---|
27 |
|
---|
28 | //////////////////////////////
|
---|
29 | // 各セクションの位置とサイズ
|
---|
30 | int FileSize_CodeSection,
|
---|
31 | FileSize_ExportSection,
|
---|
32 | FileSize_ImportSection,
|
---|
33 | FileSize_DataSection,
|
---|
34 | FileSize_RWSection,
|
---|
35 | FileSize_RSrcSection,
|
---|
36 | FileSize_RelocSection,
|
---|
37 | FileSize_DebugSection;
|
---|
38 | int FilePos_CodeSection,
|
---|
39 | FilePos_ExportSection,
|
---|
40 | FilePos_ImportSection,
|
---|
41 | FilePos_DataSection,
|
---|
42 | FilePos_RWSection,
|
---|
43 | FilePos_RSrcSection,
|
---|
44 | FilePos_RelocSection,
|
---|
45 | FilePos_DebugSection;
|
---|
46 | int MemSize_CodeSection,
|
---|
47 | MemSize_ExportSection,
|
---|
48 | MemSize_ImportSection,
|
---|
49 | MemSize_DataSection,
|
---|
50 | MemSize_RWSection,
|
---|
51 | MemSize_RSrcSection,
|
---|
52 | MemSize_RelocSection,
|
---|
53 | MemSize_DebugSection;
|
---|
54 | int MemPos_CodeSection,
|
---|
55 | MemPos_ExportSection,
|
---|
56 | MemPos_ImportSection,
|
---|
57 | MemPos_DataSection,
|
---|
58 | MemPos_RWSection,
|
---|
59 | MemPos_RSrcSection,
|
---|
60 | MemPos_RelocSection,
|
---|
61 | MemPos_DebugSection;
|
---|
62 | int bUse_CodeSection,
|
---|
63 | bUse_ExportSection,
|
---|
64 | bUse_ImportSection,
|
---|
65 | bUse_DataSection,
|
---|
66 | bUse_RWSection,
|
---|
67 | bUse_RSrcSection,
|
---|
68 | bUse_RelocSection,
|
---|
69 | bUse_DebugSection;
|
---|
70 |
|
---|
71 |
|
---|
72 | void DebugVariable(void){
|
---|
73 | char temporary[255];
|
---|
74 | sprintf(temporary,"_DebugSys_dwThreadID[255]%c%cDWord",1,ESC_AS);
|
---|
75 | OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
|
---|
76 | sprintf(temporary,"_DebugSys_ProcNum[255]%c%cDWord",1,ESC_AS);
|
---|
77 | OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
|
---|
78 | sprintf(temporary,"_DebugSys_lplpObp[255]%c%c*ULONG_PTR",1,ESC_AS);
|
---|
79 | OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
|
---|
80 | sprintf(temporary,"_DebugSys_lplpSpBase[255]%c%c*ULONG_PTR",1,ESC_AS);
|
---|
81 | OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void Compile(void){
|
---|
85 | extern HWND hMainDlg;
|
---|
86 | extern HWND hOwnerEditor;
|
---|
87 | extern HANDLE hHeap;
|
---|
88 | extern BOOL bDebugCompile;
|
---|
89 | extern BOOL bDll;
|
---|
90 | extern DWORD ImageBase;
|
---|
91 | extern int obp_AllocSize;
|
---|
92 | extern char *basbuf;
|
---|
93 | extern char OutputFileName[MAX_PATH];
|
---|
94 | int i,i2,i3,i4,i5;
|
---|
95 | char temporary[MAX_PATH],*temp2;
|
---|
96 | HANDLE hFile;
|
---|
97 |
|
---|
98 |
|
---|
99 | //コードセクションのメモリ上の位置
|
---|
100 | MemPos_CodeSection= 0x1000;
|
---|
101 |
|
---|
102 | //データセクションのメモリ上の位置(仮定)
|
---|
103 | MemPos_DataSection= 0x10000000;
|
---|
104 |
|
---|
105 |
|
---|
106 | //エクスポート セクションを利用するかどうか
|
---|
107 | if(bDll) bUse_ExportSection=1;
|
---|
108 | else bUse_ExportSection=0;
|
---|
109 |
|
---|
110 |
|
---|
111 | //////////////////
|
---|
112 | // データテーブル
|
---|
113 | dataTable.Init();
|
---|
114 | if(bDebugCompile){
|
---|
115 | dataTable.Add( (long)0x00000002 );
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | //////////////////////
|
---|
120 | // コード生成前の準備
|
---|
121 | //////////////////////
|
---|
122 |
|
---|
123 | //重複エラー情報管理のメモリを確保(グローバル領域コンパイル用)
|
---|
124 | extern char **SynonymErrorWords;
|
---|
125 | extern int SynonymErrorNum;
|
---|
126 | SynonymErrorNum=0;
|
---|
127 | SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
|
---|
128 |
|
---|
129 | //列挙体に関する情報の初期化
|
---|
130 | CEnumParent::InitEnum();
|
---|
131 |
|
---|
132 | //列挙体からクラスコードを生成
|
---|
133 | char *temp;
|
---|
134 | temp=CEnumParent::GenerateCodes();
|
---|
135 | AddSourceCode(temp);
|
---|
136 | HeapDefaultFree(temp);
|
---|
137 |
|
---|
138 | //関数ポインタ情報を初期化
|
---|
139 | Smoothie::Meta::procPointers.clear();
|
---|
140 |
|
---|
141 | //クラス名を取得(詳細情報はGetAllClassInfoで取得)
|
---|
142 | // GetSubInfo関数の中で参照されるオブジェクト名を事前に取得する。
|
---|
143 | // ※オブジェクトの内容までは取得しない
|
---|
144 | pobj_DBClass=new CDBClass();
|
---|
145 | pobj_DBClass->InitNames();
|
---|
146 |
|
---|
147 | //TypeDef情報を初期化
|
---|
148 | Smoothie::Meta::typeDefs.Init();
|
---|
149 |
|
---|
150 | //定数情報を取得
|
---|
151 | GetConstInfo();
|
---|
152 |
|
---|
153 | //サブルーチン(ユーザー定義、DLL関数)の識別子、アドレスを取得
|
---|
154 | pobj_CompilingClass=0;
|
---|
155 | GetSubInfo();
|
---|
156 |
|
---|
157 | //クラス情報を取得(※注 - GetSubInfoの後に呼び出す)
|
---|
158 | pobj_DBClass->GetAllClassInfo();
|
---|
159 |
|
---|
160 | //コードと行番号の関係
|
---|
161 | extern LINEINFO *pLineInfo;
|
---|
162 | extern int MaxLineInfoNum;
|
---|
163 | pLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,1);
|
---|
164 | MaxLineInfoNum=0;
|
---|
165 |
|
---|
166 |
|
---|
167 | ///////////////////////////
|
---|
168 | // 最低限必要な関数を取得
|
---|
169 | ///////////////////////////
|
---|
170 | cp=-1;
|
---|
171 |
|
---|
172 | if(pSub_System_StartupProgram=GetSubHash("_System_StartupProgram",1))
|
---|
173 | pSub_System_StartupProgram->Using();
|
---|
174 |
|
---|
175 | if(pSub_DebugSys_StartProc=GetSubHash("_DebugSys_StartProc",1))
|
---|
176 | pSub_DebugSys_StartProc->Using();
|
---|
177 |
|
---|
178 | if(pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc",1))
|
---|
179 | pSub_DebugSys_EndProc->Using();
|
---|
180 |
|
---|
181 | if(pSub_DebugSys_SaveContext=GetSubHash("_DebugSys_SaveContext",1))
|
---|
182 | pSub_DebugSys_SaveContext->Using();
|
---|
183 |
|
---|
184 | if(pSub_System_GetEip=GetSubHash("_System_GetEip",1)){
|
---|
185 | pSub_System_GetEip->Using();
|
---|
186 | pSub_System_GetEip->ThisIsSystemProc();
|
---|
187 | }
|
---|
188 |
|
---|
189 | if(pSub_System_InitDllGlobalVariables=GetSubHash("_System_InitDllGlobalVariables",1)){
|
---|
190 | pSub_System_InitDllGlobalVariables->Using();
|
---|
191 | pSub_System_InitDllGlobalVariables->ThisIsSystemProc();
|
---|
192 | }
|
---|
193 |
|
---|
194 | if(pSub_System_InitStaticLocalVariables=GetSubHash("_System_InitStaticLocalVariables",1)){
|
---|
195 | pSub_System_InitStaticLocalVariables->Using();
|
---|
196 | pSub_System_InitStaticLocalVariables->ThisIsSystemProc();
|
---|
197 | }
|
---|
198 |
|
---|
199 | if(pSub_System_Call_Destructor_of_GlobalObject=GetSubHash("_System_Call_Destructor_of_GlobalObject",1)){
|
---|
200 | pSub_System_Call_Destructor_of_GlobalObject->Using();
|
---|
201 | pSub_System_Call_Destructor_of_GlobalObject->ThisIsSystemProc();
|
---|
202 | }
|
---|
203 |
|
---|
204 | if(pSub_System_GetSp=GetSubHash("_System_GetSp",1)){
|
---|
205 | pSub_System_GetSp->Using();
|
---|
206 | pSub_System_GetSp->ThisIsSystemProc();
|
---|
207 | }
|
---|
208 |
|
---|
209 | if(pSub_pow=GetSubHash("pow",1))
|
---|
210 | pSub_pow->Using();
|
---|
211 |
|
---|
212 | if(pSub_calloc=GetSubHash("calloc",1))
|
---|
213 | pSub_calloc->Using();
|
---|
214 |
|
---|
215 | if(pSub_realloc=GetSubHash("realloc",1))
|
---|
216 | pSub_realloc->Using();
|
---|
217 |
|
---|
218 | if(pSub_free=GetSubHash("free",1))
|
---|
219 | pSub_free->Using();
|
---|
220 |
|
---|
221 | if( pSub_System_GC_malloc_ForObject = GetSubHash( "_System_GC_malloc_ForObject",1 ) )
|
---|
222 | pSub_System_GC_malloc_ForObject->Using();
|
---|
223 |
|
---|
224 | if( pSub_System_GC_malloc_ForObjectPtr = GetSubHash( "_System_GC_malloc_ForObjectPtr",1 ) )
|
---|
225 | pSub_System_GC_malloc_ForObjectPtr->Using();
|
---|
226 |
|
---|
227 | if( pSub_System_GC_free_for_SweepingDelete = GetSubHash( "_System_GC_free_for_SweepingDelete",1 ) )
|
---|
228 | pSub_System_GC_free_for_SweepingDelete->Using();
|
---|
229 |
|
---|
230 | if( pSubStaticMethod_System_TypeBase_InitializeUserTypes = GetSubHash( "_System_TypeBase.InitializeUserTypes",1 ) ){
|
---|
231 | pSubStaticMethod_System_TypeBase_InitializeUserTypes->Using();
|
---|
232 | pSubStaticMethod_System_TypeBase_InitializeUserTypes->ThisIsAutoGenerationProc();
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 |
|
---|
237 | //データテーブルスケジュール
|
---|
238 | pobj_DataTableSchedule=new CSchedule();
|
---|
239 | pobj_DataTableSchedule->SetFlag(SCHEDULE_FLAG_RELOC);
|
---|
240 |
|
---|
241 | //インポートテーブルスケジュール
|
---|
242 | //※x64はRPI(実行中アドレス)からのオフセットになるため、再配置が不要
|
---|
243 | pobj_ImportAddrSchedule=new CImportAddrSchedule();
|
---|
244 |
|
---|
245 | //プロシージャポインタスケジュール
|
---|
246 | pobj_SubAddrSchedule=new CSubAddrSchedule();
|
---|
247 |
|
---|
248 | //グローバル変数アドレススケジュール
|
---|
249 | pobj_GlobalVarSchedule=new CSchedule();
|
---|
250 | pobj_GlobalVarSchedule->SetFlag(SCHEDULE_FLAG_RELOC);
|
---|
251 |
|
---|
252 | //グローバル変数の初期バッファ
|
---|
253 | extern BYTE *initGlobalBuf;
|
---|
254 | initGlobalBuf=(BYTE *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,1);
|
---|
255 |
|
---|
256 | //リロケーション情報
|
---|
257 | pobj_Reloc=new CReloc();
|
---|
258 |
|
---|
259 | //一時スケジューリングポインタ
|
---|
260 | pobj_TempSchedule=new CTempSchedule();
|
---|
261 |
|
---|
262 | obp_AllocSize=8192*2;
|
---|
263 | OpBuffer=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,obp_AllocSize);
|
---|
264 | obp=0;
|
---|
265 |
|
---|
266 | //レジスタのブロッキングを管理するためのオブジェクトを生成
|
---|
267 | pobj_BlockReg=new CBlockReg;
|
---|
268 |
|
---|
269 | //レキシカルスコープ情報を初期化
|
---|
270 | obj_LexScopes.Init(obp);
|
---|
271 |
|
---|
272 |
|
---|
273 | /////////////////////////////////////////////////////////////////
|
---|
274 | // デバッグコンパイル用のログを生成する
|
---|
275 | /////////////////////////////////////////////////////////////////
|
---|
276 | #ifdef _DEBUG
|
---|
277 | {
|
---|
278 | ofstream ofs("middle_code.txt");
|
---|
279 | ofs << basbuf << endl;
|
---|
280 | ofs.close();
|
---|
281 | }
|
---|
282 | #endif
|
---|
283 |
|
---|
284 |
|
---|
285 | //////////////////////
|
---|
286 | // グローバル実行領域
|
---|
287 | //////////////////////
|
---|
288 |
|
---|
289 | cp=-1;
|
---|
290 | UserProc::CompileStartForGlobalArea();
|
---|
291 |
|
---|
292 | if(!bDll){
|
---|
293 | //ラベル用のメモリを確保
|
---|
294 | extern LABEL *pLabelNames;
|
---|
295 | extern int MaxLabelNum;
|
---|
296 | pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
|
---|
297 | MaxLabelNum=0;
|
---|
298 |
|
---|
299 | //Gotoラベルスケジュール
|
---|
300 | extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
|
---|
301 | extern int GotoLabelScheduleNum;
|
---|
302 | pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
|
---|
303 | GotoLabelScheduleNum=0;
|
---|
304 |
|
---|
305 | //With情報のメモリを確保
|
---|
306 | extern WITHINFO WithInfo;
|
---|
307 | WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
|
---|
308 | WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
|
---|
309 | WithInfo.num=0;
|
---|
310 |
|
---|
311 | //Continueアドレスを初期化
|
---|
312 | extern DWORD dwContinueAddress;
|
---|
313 | dwContinueAddress=-1;
|
---|
314 |
|
---|
315 | //スタックフレーム管理用クラスを初期化
|
---|
316 | extern CStackFrame *pobj_sf;
|
---|
317 | pobj_sf=new CStackFrame();
|
---|
318 |
|
---|
319 |
|
---|
320 | //未完成
|
---|
321 | //OpBuffer[obp++]=(char)0xCC;
|
---|
322 |
|
---|
323 | //sub rsp,スタックフレームサイズ
|
---|
324 | int StackFrameSchedule;
|
---|
325 | op_sub_rsp(0xFFFFFFFF);
|
---|
326 | StackFrameSchedule=obp-sizeof(long);
|
---|
327 |
|
---|
328 | if(bDebugCompile){
|
---|
329 | //デバッグ用の変数を定義
|
---|
330 | DebugVariable();
|
---|
331 | }
|
---|
332 |
|
---|
333 | //GC用の変数を定義
|
---|
334 | InitGCVariables();
|
---|
335 |
|
---|
336 | //_System_StartupProgramの呼び出し
|
---|
337 | op_call(pSub_System_StartupProgram);
|
---|
338 |
|
---|
339 | //クラスに属する静的メンバを定義
|
---|
340 | CMember::InitStaticMember();
|
---|
341 |
|
---|
342 | //グローバル実行領域をコンパイル開始
|
---|
343 | CompileBuffer(0,0);
|
---|
344 |
|
---|
345 | //ラベル用のメモリを解放
|
---|
346 | for(i=0;i<MaxLabelNum;i++){
|
---|
347 | if(pLabelNames[i].pName) HeapDefaultFree(pLabelNames[i].pName);
|
---|
348 | }
|
---|
349 | HeapDefaultFree(pLabelNames);
|
---|
350 |
|
---|
351 | //Goto未知ラベルスケジュールを解放
|
---|
352 | for(i=0;i<GotoLabelScheduleNum;i++){
|
---|
353 | if(pGotoLabelSchedule[i].pName){
|
---|
354 | SetError(6,pGotoLabelSchedule[i].pName,pGotoLabelSchedule[i].now_cp);
|
---|
355 | HeapDefaultFree(pGotoLabelSchedule[i].pName);
|
---|
356 | }
|
---|
357 | else{
|
---|
358 | sprintf(temporary,"%d",pGotoLabelSchedule[i].line);
|
---|
359 | SetError(6,temporary,pGotoLabelSchedule[i].now_cp);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | HeapDefaultFree(pGotoLabelSchedule);
|
---|
363 |
|
---|
364 |
|
---|
365 | ///////////////////////////////////////
|
---|
366 | // グローバル文字列変数の解放処理
|
---|
367 | ///////////////////////////////////////
|
---|
368 |
|
---|
369 | //call _System_Call_Destructor_of_GlobalObject
|
---|
370 | extern UserProc *pSub_System_Call_Destructor_of_GlobalObject;
|
---|
371 | op_call(pSub_System_Call_Destructor_of_GlobalObject);
|
---|
372 |
|
---|
373 |
|
---|
374 | //add rsp,スタックフレームサイズ
|
---|
375 | op_add_RV(REG_RSP,pobj_sf->GetFrameSize());
|
---|
376 |
|
---|
377 | //xor rax,rax(raxを0に初期化する)
|
---|
378 | op_zero_reg(REG_RAX);
|
---|
379 |
|
---|
380 | //ret
|
---|
381 | OpBuffer[obp++]=(char)0xC3;
|
---|
382 |
|
---|
383 | //グローバル実行領域のコードサイズ
|
---|
384 | extern int GlobalOpBufferSize;
|
---|
385 | GlobalOpBufferSize=obp;
|
---|
386 |
|
---|
387 | //スタックフレームスケジュール(subコマンドに渡す値)
|
---|
388 | *((long *)(OpBuffer+StackFrameSchedule))=pobj_sf->GetFrameSize();
|
---|
389 |
|
---|
390 | //スタックフレームスケジュールを実行
|
---|
391 | pobj_sf->RunningSchedule();
|
---|
392 | delete pobj_sf;
|
---|
393 | pobj_sf=0;
|
---|
394 |
|
---|
395 | //With情報のメモリを解放
|
---|
396 | for(i=0;i<WithInfo.num;i++){
|
---|
397 | SetError(22,"With",WithInfo.pWithCp[i]);
|
---|
398 | HeapDefaultFree(WithInfo.ppName[i]);
|
---|
399 | }
|
---|
400 | HeapDefaultFree(WithInfo.ppName);
|
---|
401 | HeapDefaultFree(WithInfo.pWithCp);
|
---|
402 | }
|
---|
403 | else{
|
---|
404 | ////////////////
|
---|
405 | // DLLの場合
|
---|
406 | ////////////////
|
---|
407 |
|
---|
408 |
|
---|
409 | }
|
---|
410 |
|
---|
411 | //重複エラー情報管理のメモリを解放(グローバル領域用)
|
---|
412 | for(i=0;i<SynonymErrorNum;i++) HeapDefaultFree(SynonymErrorWords[i]);
|
---|
413 | HeapDefaultFree(SynonymErrorWords);
|
---|
414 |
|
---|
415 |
|
---|
416 | StepCompileProgress();
|
---|
417 |
|
---|
418 |
|
---|
419 | /////////////////////
|
---|
420 | // ローカル実行領域
|
---|
421 | /////////////////////
|
---|
422 |
|
---|
423 | //プロシージャをコンパイル開始
|
---|
424 | cp=0;
|
---|
425 | CompileLocal();
|
---|
426 |
|
---|
427 | // 終了
|
---|
428 | ///////////////////////
|
---|
429 |
|
---|
430 | StepCompileProgress();
|
---|
431 |
|
---|
432 |
|
---|
433 | //レジスタのブロッキングを管理するためのオブジェクトを破棄
|
---|
434 | delete pobj_BlockReg;
|
---|
435 |
|
---|
436 | //ネイティブコード生成はここまで
|
---|
437 | //////////////////////////////////////////////////////////
|
---|
438 |
|
---|
439 |
|
---|
440 |
|
---|
441 | ////////////////////////////////
|
---|
442 | // 使用するDLL関数のチェック
|
---|
443 | ////////////////////////////////
|
---|
444 | for(i=0;i<MAX_HASH;i++){
|
---|
445 | extern DllProc **ppDeclareHash;
|
---|
446 | const DllProc *pDllProc = ppDeclareHash[i];
|
---|
447 | while(pDllProc){
|
---|
448 | if( !pDllProc->IsUsing() ){
|
---|
449 | pDllProc=pDllProc->pNextData;
|
---|
450 | continue;
|
---|
451 | }
|
---|
452 |
|
---|
453 | //エラーチェック
|
---|
454 | HINSTANCE hLib;
|
---|
455 | hLib=LoadLibrary( pDllProc->GetDllFileName().c_str() );
|
---|
456 | if(!hLib){
|
---|
457 | extern char OutputFileName[MAX_PATH];
|
---|
458 | char temp2[MAX_PATH],temp3[MAX_PATH];
|
---|
459 | _splitpath(OutputFileName,temp2,temp3,NULL,NULL);
|
---|
460 | lstrcat(temp2,temp3);
|
---|
461 | lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
|
---|
462 | GetFullPath(temp3,temp2);
|
---|
463 | hLib=LoadLibrary(temp3);
|
---|
464 |
|
---|
465 | if(!hLib){
|
---|
466 | SetError(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetCodePos());
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | if(hLib){
|
---|
471 | if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
|
---|
472 | FreeLibrary(hLib);
|
---|
473 | SetError(-107,pDllProc->GetAlias(),pDllProc->GetCodePos());
|
---|
474 | }
|
---|
475 | FreeLibrary(hLib);
|
---|
476 | }
|
---|
477 |
|
---|
478 | pDllProc=pDllProc->pNextData;
|
---|
479 | }
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 |
|
---|
484 | /////////////////////////////
|
---|
485 | // リソースデータを読み込む
|
---|
486 | /////////////////////////////
|
---|
487 | extern char ResourceFileName[MAX_PATH];
|
---|
488 | GetResourceData(ResourceFileName);
|
---|
489 |
|
---|
490 |
|
---|
491 | //////////////////////////////
|
---|
492 | // エクスポート情報(DLLのみ)
|
---|
493 | //////////////////////////////
|
---|
494 | IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
|
---|
495 | DWORD *lpdwExportAddressTable;
|
---|
496 | DWORD *lpdwExportNamePointerTable;
|
---|
497 | WORD *lpwExportOrdinalTable;
|
---|
498 | char lpExportNames[8192];
|
---|
499 | int ExportNamesLength;
|
---|
500 | int ExportNum;
|
---|
501 | int DllMain_EntryPoint;
|
---|
502 |
|
---|
503 | DllMain_EntryPoint=-1;
|
---|
504 | ExportNum=0;
|
---|
505 | ExportNamesLength=0;
|
---|
506 | lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
|
---|
507 | lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
|
---|
508 | lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
|
---|
509 |
|
---|
510 | if(bUse_ExportSection){
|
---|
511 | _splitpath(OutputFileName,NULL,NULL,lpExportNames,temporary);
|
---|
512 | lstrcat(lpExportNames,temporary);
|
---|
513 | ExportNamesLength=lstrlen(lpExportNames)+1;
|
---|
514 |
|
---|
515 | extern UserProc **ppSubHash;
|
---|
516 | UserProc *pUserProc,*psi2;
|
---|
517 | while(1){
|
---|
518 | //辞書順にサーチ
|
---|
519 | temporary[0]=0;
|
---|
520 | for(i=0,psi2=0;i<MAX_HASH;i++){
|
---|
521 | pUserProc=ppSubHash[i];
|
---|
522 | while(pUserProc){
|
---|
523 | if(pUserProc->IsExport()){
|
---|
524 | if(temporary[0]=='\0'){
|
---|
525 | lstrcpy(temporary,pUserProc->GetName().c_str());
|
---|
526 | psi2=pUserProc;
|
---|
527 | }
|
---|
528 | else{
|
---|
529 | i3=lstrlen(temporary);
|
---|
530 | i4=(int)pUserProc->GetName().size();
|
---|
531 | if(i3<i4) i3=i4;
|
---|
532 | if(memcmp(temporary,pUserProc->GetName().c_str(),i3)>0){
|
---|
533 | lstrcpy(temporary,pUserProc->GetName().c_str());
|
---|
534 | psi2=pUserProc;
|
---|
535 | }
|
---|
536 | }
|
---|
537 | }
|
---|
538 | pUserProc=pUserProc->pNextData;
|
---|
539 | }
|
---|
540 | }
|
---|
541 | if(psi2==0) break;
|
---|
542 | pUserProc=psi2;
|
---|
543 |
|
---|
544 | pUserProc->ExportOff();
|
---|
545 |
|
---|
546 | if( pUserProc->GetName() == "DllMain" ){
|
---|
547 | DllMain_EntryPoint=pUserProc->beginOpAddress;
|
---|
548 | }
|
---|
549 |
|
---|
550 | lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
|
---|
551 | lpdwExportAddressTable[ExportNum]=pUserProc->beginOpAddress;
|
---|
552 |
|
---|
553 | lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
|
---|
554 | lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
|
---|
555 |
|
---|
556 | lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
|
---|
557 | lpwExportOrdinalTable[ExportNum]=ExportNum;
|
---|
558 |
|
---|
559 | lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
|
---|
560 | ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
|
---|
561 |
|
---|
562 | ExportNum++;
|
---|
563 | }
|
---|
564 | for(i=0;i<ExportNum;i++){
|
---|
565 | lpdwExportNamePointerTable[i]+=
|
---|
566 | sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
|
---|
567 | ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
|
---|
568 | ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
|
---|
569 | ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
|
---|
570 | }
|
---|
571 |
|
---|
572 | ImageExportDirectory.Characteristics=0;
|
---|
573 | ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
|
---|
574 | ImageExportDirectory.MajorVersion=0;
|
---|
575 | ImageExportDirectory.MinorVersion=0;
|
---|
576 | ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
|
---|
577 | ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
|
---|
578 | ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
|
---|
579 | ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
|
---|
580 | ImageExportDirectory.Base=1;
|
---|
581 | ImageExportDirectory.NumberOfFunctions=ExportNum;
|
---|
582 | ImageExportDirectory.NumberOfNames=ExportNum;
|
---|
583 | ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
|
---|
584 | ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
|
---|
585 | ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | /////////////////////
|
---|
590 | //インポートDLL情報
|
---|
591 | /////////////////////
|
---|
592 | extern DllProc **ppDeclareHash;
|
---|
593 | DllProc *pDllProc;
|
---|
594 | char **ppDllNames;
|
---|
595 | int ImportDllNum;
|
---|
596 |
|
---|
597 | /*
|
---|
598 | インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
|
---|
599 | ルックアップ テーブル(ヒントを示すRVA)
|
---|
600 | IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
|
---|
601 | IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
|
---|
602 | IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
|
---|
603 | ...
|
---|
604 | Dll名1[size=0x10]
|
---|
605 | Dll名2[size=0x10]
|
---|
606 | Dll名3[size=0x10]
|
---|
607 | ...
|
---|
608 | ヒントテーブル(関数名を羅列したもの)
|
---|
609 | */
|
---|
610 |
|
---|
611 | /*
|
---|
612 | IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
|
---|
613 | IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
|
---|
614 | IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
|
---|
615 | ...
|
---|
616 | Dll名1[size=0x10]
|
---|
617 | Dll名2[size=0x10]
|
---|
618 | Dll名3[size=0x10]
|
---|
619 | ...
|
---|
620 | ルックアップ テーブル(ヒントを示すRVA)
|
---|
621 | ヒントテーブル
|
---|
622 | インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
|
---|
623 | */
|
---|
624 |
|
---|
625 | ppDllNames=(char **)HeapAlloc(hHeap,0,1);
|
---|
626 |
|
---|
627 | ImportDllNum=0;
|
---|
628 | for(i=0;i<MAX_HASH;i++){
|
---|
629 | const DllProc *pDllProc=ppDeclareHash[i];
|
---|
630 | while(pDllProc){
|
---|
631 | if( !pDllProc->IsUsing() ){
|
---|
632 | pDllProc=pDllProc->pNextData;
|
---|
633 | continue;
|
---|
634 | }
|
---|
635 |
|
---|
636 | if( pDllProc->GetDllFileName().size() > 16 ){
|
---|
637 | SetError(7,NULL,cp);
|
---|
638 | break;
|
---|
639 | }
|
---|
640 | for(i2=0;i2<ImportDllNum;i2++){
|
---|
641 | if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
|
---|
642 | break;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | if(i2==ImportDllNum){
|
---|
646 | ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
|
---|
647 | ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
|
---|
648 | lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
|
---|
649 | ImportDllNum++;
|
---|
650 | }
|
---|
651 |
|
---|
652 | pDllProc=pDllProc->pNextData;
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | //IMAGE_IMPORT_DESCRIPTOR、及びルックアップ テーブル サイズの計算
|
---|
657 | IMAGE_IMPORT_DESCRIPTOR *pImportDescriptor;
|
---|
658 | int LookupSize;
|
---|
659 | LookupSize=0;
|
---|
660 | pImportDescriptor=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
|
---|
661 | for(i=0;i<ImportDllNum;i++){
|
---|
662 | //IMAGE_IMPORT_DESCRIPTOR
|
---|
663 | pImportDescriptor[i].OriginalFirstThunk=LookupSize; //※すぐ下で再計算
|
---|
664 | pImportDescriptor[i].TimeDateStamp=0;
|
---|
665 | pImportDescriptor[i].ForwarderChain=0;
|
---|
666 | pImportDescriptor[i].Name=i*0x10; //※すぐ下で再計算
|
---|
667 | for(i2=0;i2<MAX_HASH;i2++){
|
---|
668 | pDllProc=ppDeclareHash[i2];
|
---|
669 | while(pDllProc){
|
---|
670 | if( !pDllProc->IsUsing() ){
|
---|
671 | pDllProc=pDllProc->pNextData;
|
---|
672 | continue;
|
---|
673 | }
|
---|
674 |
|
---|
675 | if( pDllProc->GetDllFileName() == ppDllNames[i] ){
|
---|
676 | //ルックアップデータのサイズを追加
|
---|
677 | LookupSize+=sizeof(_int64);
|
---|
678 | }
|
---|
679 |
|
---|
680 | pDllProc=pDllProc->pNextData;
|
---|
681 | }
|
---|
682 | }
|
---|
683 | LookupSize+=sizeof(_int64); //NULL用
|
---|
684 | }
|
---|
685 | memset(&pImportDescriptor[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
|
---|
686 | for(i=0;i<ImportDllNum;i++){
|
---|
687 | //インポートアドレステーブル分の差分を追加
|
---|
688 | pImportDescriptor[i].OriginalFirstThunk+=LookupSize;
|
---|
689 |
|
---|
690 | //DLL名への差分を追加
|
---|
691 | pImportDescriptor[i].Name+=
|
---|
692 | LookupSize+ //インポート アドレス テーブル
|
---|
693 | LookupSize+ //ルックアップテーブル
|
---|
694 | (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR); //IMAGE_IMPORT_DESCRIPTOR
|
---|
695 | }
|
---|
696 |
|
---|
697 | //ルックアップ テーブル、ヒント テーブル
|
---|
698 | unsigned _int64 *pLookupTable;
|
---|
699 | pLookupTable=(unsigned _int64 *)HeapAlloc(hHeap,0,LookupSize*sizeof(_int64)+1);
|
---|
700 | char *pHintTable;
|
---|
701 | int HintSize,HintAllocSize;
|
---|
702 | HintSize=0;
|
---|
703 | HintAllocSize=128*2;
|
---|
704 | pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
|
---|
705 | for(i=0,i5=0;i<ImportDllNum;i++){
|
---|
706 | for(i2=0;i2<MAX_HASH;i2++){
|
---|
707 | pDllProc=ppDeclareHash[i2];
|
---|
708 | while(pDllProc){
|
---|
709 | if( !pDllProc->IsUsing() ){
|
---|
710 | pDllProc=pDllProc->pNextData;
|
---|
711 | continue;
|
---|
712 | }
|
---|
713 |
|
---|
714 | if( pDllProc->GetDllFileName() == ppDllNames[i] ){
|
---|
715 | //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
|
---|
716 | pDllProc->SetLookupAddress( i5*sizeof(_int64) );
|
---|
717 |
|
---|
718 | //ルックアップ テーブルの値をセット
|
---|
719 | pLookupTable[i5++]=
|
---|
720 | LookupSize+ //インポート アドレス テーブル
|
---|
721 | LookupSize+ //ルックアップテーブル
|
---|
722 | (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+ //IMAGE_IMPORT_DESCRIPTOR
|
---|
723 | ImportDllNum*0x10+ //DLL名の羅列
|
---|
724 | HintSize; //ヒント名へのオフセット
|
---|
725 |
|
---|
726 | //ヒント テーブル
|
---|
727 | pHintTable[HintSize++]=0;
|
---|
728 | pHintTable[HintSize++]=0;
|
---|
729 | lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
|
---|
730 | i4=(int)pDllProc->GetAlias().size();
|
---|
731 | HintSize+=i4+1;
|
---|
732 | if(i4%2==0) pHintTable[HintSize++]=0;
|
---|
733 |
|
---|
734 | if(HintAllocSize<HintSize+128){
|
---|
735 | HintAllocSize+=128;
|
---|
736 | pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | pDllProc=pDllProc->pNextData;
|
---|
741 | }
|
---|
742 | }
|
---|
743 | pLookupTable[i5++]=0;
|
---|
744 | }
|
---|
745 |
|
---|
746 |
|
---|
747 | if(bDll){
|
---|
748 | //DLLの場合はリロケーション情報を仮生成
|
---|
749 | //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
|
---|
750 | pobj_Reloc->ResetRelocBuffer();
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 |
|
---|
755 | //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
|
---|
756 | //(デバッグ情報で利用される)
|
---|
757 | extern int AllInitGlobalVarSize;
|
---|
758 | foreach( Variable *pVar, globalVars ){
|
---|
759 | if(pVar->offset&0x80000000){
|
---|
760 | pVar->offset=(pVar->offset&0x7FFFFFFF)+AllInitGlobalVarSize;
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 |
|
---|
765 |
|
---|
766 | ////////////////////////////////////
|
---|
767 | // デバッグセクションを生成
|
---|
768 | ////////////////////////////////////
|
---|
769 |
|
---|
770 | //機械語コードとBasicコードの関係を整理
|
---|
771 | extern LINEINFO *pLineInfo;
|
---|
772 | extern int MaxLineInfoNum;
|
---|
773 | int MinObp,MaxObp;
|
---|
774 | LINEINFO *pTempLineInfo;
|
---|
775 | pTempLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,MaxLineInfoNum*sizeof(LINEINFO));
|
---|
776 | i2=0;
|
---|
777 | MaxObp=0;
|
---|
778 | while(1){
|
---|
779 | for(i3=0,MinObp=0x0FFFFFFF;i3<MaxLineInfoNum;i3++){
|
---|
780 | if(pLineInfo[i3].TopObp<MinObp&&MaxObp<pLineInfo[i3].TopObp){
|
---|
781 | MinObp=pLineInfo[i3].TopObp;
|
---|
782 | i4=i3;
|
---|
783 | }
|
---|
784 | }
|
---|
785 | if(MinObp==0x0FFFFFFF) break;
|
---|
786 | pTempLineInfo[i2]=pLineInfo[i4];
|
---|
787 | MaxObp=pTempLineInfo[i2].TopObp;
|
---|
788 | i2++;
|
---|
789 | }
|
---|
790 | HeapDefaultFree(pLineInfo);
|
---|
791 | pLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,i2*sizeof(LINEINFO));
|
---|
792 | memcpy(pLineInfo,pTempLineInfo,i2*sizeof(LINEINFO));
|
---|
793 | MaxLineInfoNum=i2;
|
---|
794 | HeapDefaultFree(pTempLineInfo);
|
---|
795 |
|
---|
796 | //デバッグセクションを生成
|
---|
797 | CDebugSection *pobj_DebugSection;
|
---|
798 | pobj_DebugSection=new CDebugSection();
|
---|
799 | extern BOOL bDebugCompile;
|
---|
800 | extern BOOL bError;
|
---|
801 | if(bDebugCompile&&bError==0){
|
---|
802 | pobj_DebugSection->make();
|
---|
803 | }
|
---|
804 |
|
---|
805 |
|
---|
806 |
|
---|
807 | /////////////////////////////////////
|
---|
808 | // 各セクションの位置とサイズを計算
|
---|
809 | /////////////////////////////////////
|
---|
810 |
|
---|
811 | //コードセッションのファイル上のサイズ
|
---|
812 | if(obp%FILE_ALIGNMENT) FileSize_CodeSection=obp+(FILE_ALIGNMENT-obp%FILE_ALIGNMENT);
|
---|
813 | else FileSize_CodeSection=obp;
|
---|
814 | if(FileSize_CodeSection) bUse_CodeSection=1;
|
---|
815 | else bUse_CodeSection=0;
|
---|
816 |
|
---|
817 | //エクスポートセクションのファイル上のサイズ
|
---|
818 | i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
|
---|
819 | ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
|
---|
820 | ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
|
---|
821 | ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
|
---|
822 | ExportNamesLength; //シンボル名バッファ
|
---|
823 | if(bUse_ExportSection){
|
---|
824 | if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
|
---|
825 | else FileSize_ExportSection=i;
|
---|
826 | }
|
---|
827 | else FileSize_ExportSection=0;
|
---|
828 |
|
---|
829 | //インポートセクションのファイル上のサイズ
|
---|
830 | i=
|
---|
831 | LookupSize+ //インポート アドレス テーブル
|
---|
832 | LookupSize+ //ルックアップテーブル
|
---|
833 | (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+ //IMAGE_IMPORT_DESCRIPTOR
|
---|
834 | ImportDllNum*0x10+ //DLL名の羅列
|
---|
835 | HintSize; //ヒント名
|
---|
836 | if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
|
---|
837 | else FileSize_ImportSection=i;
|
---|
838 | bUse_ImportSection=1; //インポートセクションは必ず存在する
|
---|
839 |
|
---|
840 | //データセクションのファイル上のサイズ
|
---|
841 | if(dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=dataTable.GetSize()+(FILE_ALIGNMENT-dataTable.GetSize()%FILE_ALIGNMENT);
|
---|
842 | else FileSize_DataSection=dataTable.GetSize();
|
---|
843 | if(FileSize_DataSection) bUse_DataSection=1;
|
---|
844 | else bUse_DataSection=0;
|
---|
845 |
|
---|
846 | //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
|
---|
847 | extern int AllInitGlobalVarSize;
|
---|
848 | if(AllInitGlobalVarSize%FILE_ALIGNMENT) FileSize_RWSection=AllInitGlobalVarSize+(FILE_ALIGNMENT-AllInitGlobalVarSize%FILE_ALIGNMENT);
|
---|
849 | else{
|
---|
850 | if(AllInitGlobalVarSize) FileSize_RWSection=AllInitGlobalVarSize;
|
---|
851 | else FileSize_RWSection=FILE_ALIGNMENT;
|
---|
852 | }
|
---|
853 | bUse_RWSection=1;
|
---|
854 |
|
---|
855 | //リソースセクションのファイル上のサイズ
|
---|
856 | char *RSrcSectionBuffer;
|
---|
857 | int RSrcSectionSize;
|
---|
858 | RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
|
---|
859 | if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
|
---|
860 | else FileSize_RSrcSection=RSrcSectionSize;
|
---|
861 | if(FileSize_RSrcSection) bUse_RSrcSection=1;
|
---|
862 | else bUse_RSrcSection=0;
|
---|
863 |
|
---|
864 | //リロケーションセクションのファイル上のサイズ
|
---|
865 | if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
|
---|
866 | else FileSize_RelocSection=pobj_Reloc->length;
|
---|
867 | if(FileSize_RelocSection) bUse_RelocSection=1;
|
---|
868 | else bUse_RelocSection=0;
|
---|
869 |
|
---|
870 | //デバッグセクションのファイル上のサイズ
|
---|
871 | if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
|
---|
872 | else FileSize_DebugSection=pobj_DebugSection->length;
|
---|
873 | if(FileSize_DebugSection) bUse_DebugSection=1;
|
---|
874 | else bUse_DebugSection=0;
|
---|
875 |
|
---|
876 |
|
---|
877 | //各セッションのファイル上の位置
|
---|
878 | FilePos_CodeSection= EXE_HEADER_SIZE;
|
---|
879 | FilePos_ExportSection= FilePos_CodeSection+
|
---|
880 | FileSize_CodeSection;
|
---|
881 | FilePos_ImportSection= FilePos_ExportSection+
|
---|
882 | FileSize_ExportSection;
|
---|
883 | FilePos_DataSection= FilePos_ImportSection+
|
---|
884 | FileSize_ImportSection;
|
---|
885 | FilePos_RWSection= FilePos_DataSection+
|
---|
886 | FileSize_DataSection;
|
---|
887 | FilePos_RSrcSection= FilePos_RWSection+
|
---|
888 | FileSize_RWSection;
|
---|
889 | FilePos_RelocSection= FilePos_RSrcSection+
|
---|
890 | FileSize_RSrcSection;
|
---|
891 | FilePos_DebugSection= FilePos_RelocSection+
|
---|
892 | FileSize_RelocSection;
|
---|
893 |
|
---|
894 | //コードセッションのメモリ上のサイズ
|
---|
895 | if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
|
---|
896 | else MemSize_CodeSection=FileSize_CodeSection;
|
---|
897 |
|
---|
898 | //エクスポートセクションのメモリ上のサイズ
|
---|
899 | if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
|
---|
900 | else MemSize_ExportSection=FileSize_ExportSection;
|
---|
901 |
|
---|
902 | //インポートセクションのメモリ上のサイズ
|
---|
903 | if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
|
---|
904 | else MemSize_ImportSection=FileSize_ImportSection;
|
---|
905 |
|
---|
906 | //データセクションのメモリ上のサイズ
|
---|
907 | if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
|
---|
908 | else MemSize_DataSection=FileSize_DataSection;
|
---|
909 |
|
---|
910 | //リライタブルセクションのメモリ上のサイズ
|
---|
911 | extern int AllGlobalVarSize;
|
---|
912 | i=AllInitGlobalVarSize+AllGlobalVarSize;
|
---|
913 | if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
|
---|
914 | else MemSize_RWSection=i;
|
---|
915 |
|
---|
916 | //リソースセクションのメモリ上のサイズ
|
---|
917 | if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
|
---|
918 | else MemSize_RSrcSection=FileSize_RSrcSection;
|
---|
919 |
|
---|
920 | //リロケーションセクションのメモリ上のサイズ
|
---|
921 | if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
|
---|
922 | else MemSize_RelocSection=FileSize_RelocSection;
|
---|
923 |
|
---|
924 | //デバッグセクションのメモリ上のサイズ
|
---|
925 | if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
|
---|
926 | else MemSize_DebugSection=FileSize_DebugSection;
|
---|
927 |
|
---|
928 |
|
---|
929 | //各セッションのメモリ上の位置
|
---|
930 | if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
|
---|
931 | MemSize_CodeSection;
|
---|
932 | else MemPos_ExportSection=0;
|
---|
933 | if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
|
---|
934 | MemSize_CodeSection+
|
---|
935 | MemSize_ExportSection;
|
---|
936 | else MemPos_ImportSection=0;
|
---|
937 | if(bUse_DataSection) MemPos_DataSection= 0x1000+
|
---|
938 | MemSize_CodeSection+
|
---|
939 | MemSize_ExportSection+
|
---|
940 | MemSize_ImportSection;
|
---|
941 | else MemPos_DataSection=0;
|
---|
942 | if(bUse_RWSection) MemPos_RWSection= 0x1000+
|
---|
943 | MemSize_CodeSection+
|
---|
944 | MemSize_ExportSection+
|
---|
945 | MemSize_ImportSection+
|
---|
946 | MemSize_DataSection;
|
---|
947 | else MemPos_RWSection=0;
|
---|
948 | if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
|
---|
949 | MemSize_CodeSection+
|
---|
950 | MemSize_ExportSection+
|
---|
951 | MemSize_ImportSection+
|
---|
952 | MemSize_DataSection+
|
---|
953 | MemSize_RWSection;
|
---|
954 | else MemPos_RSrcSection=0;
|
---|
955 | if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
|
---|
956 | MemSize_CodeSection+
|
---|
957 | MemSize_ExportSection+
|
---|
958 | MemSize_ImportSection+
|
---|
959 | MemSize_DataSection+
|
---|
960 | MemSize_RWSection+
|
---|
961 | MemSize_RSrcSection;
|
---|
962 | else MemPos_RelocSection=0;
|
---|
963 | if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
|
---|
964 | MemSize_CodeSection+
|
---|
965 | MemSize_ExportSection+
|
---|
966 | MemSize_ImportSection+
|
---|
967 | MemSize_DataSection+
|
---|
968 | MemSize_RWSection+
|
---|
969 | MemSize_RSrcSection+
|
---|
970 | MemSize_RelocSection;
|
---|
971 | else MemPos_DebugSection=0;
|
---|
972 |
|
---|
973 |
|
---|
974 |
|
---|
975 | /////////////////////////////
|
---|
976 | // GC用の変数スケジュール
|
---|
977 | /////////////////////////////
|
---|
978 | PerformedGcVarSchedule();
|
---|
979 |
|
---|
980 |
|
---|
981 | ////////////////////////////
|
---|
982 | // エクスポート情報の再配置
|
---|
983 | ////////////////////////////
|
---|
984 | if(bUse_ExportSection){
|
---|
985 | for(i=0;i<ExportNum;i++){
|
---|
986 | lpdwExportAddressTable[i]+=MemPos_CodeSection;
|
---|
987 | lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
|
---|
988 | }
|
---|
989 |
|
---|
990 | ImageExportDirectory.Name+= MemPos_ExportSection;
|
---|
991 | ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
|
---|
992 | ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
|
---|
993 | ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
|
---|
994 | }
|
---|
995 |
|
---|
996 |
|
---|
997 | ////////////////////////////
|
---|
998 | // インポート情報の再配置
|
---|
999 | ////////////////////////////
|
---|
1000 | for(i=0,i5=0;i<ImportDllNum;i++){
|
---|
1001 | //IMAGE_IMPORT_DESCRIPTOR
|
---|
1002 | pImportDescriptor[i].OriginalFirstThunk+=MemPos_ImportSection;
|
---|
1003 | pImportDescriptor[i].Name+=MemPos_ImportSection;
|
---|
1004 |
|
---|
1005 | //インポート アドレス テーブル(ルックアップ テーブルを差し引く)
|
---|
1006 | pImportDescriptor[i].FirstThunk=pImportDescriptor[i].OriginalFirstThunk-
|
---|
1007 | LookupSize; //ルックアップテーブル
|
---|
1008 |
|
---|
1009 | for(i2=0;i2<MAX_HASH;i2++){
|
---|
1010 | const DllProc *pDllProc=ppDeclareHash[i2];
|
---|
1011 | while(pDllProc){
|
---|
1012 | if( !pDllProc->IsUsing() ){
|
---|
1013 | pDllProc=pDllProc->pNextData;
|
---|
1014 | continue;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if( pDllProc->GetDllFileName() == ppDllNames[i] ){
|
---|
1018 | //ルックアップ テーブル
|
---|
1019 | pLookupTable[i5++]+=MemPos_ImportSection;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | pDllProc=pDllProc->pNextData;
|
---|
1023 | }
|
---|
1024 | }
|
---|
1025 | i5++;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 |
|
---|
1029 | ////////////////////////////////////////
|
---|
1030 | //仮想関数データテーブルスケジュール
|
---|
1031 | pobj_DBClass->ActionVtblSchedule(ImageBase,MemPos_CodeSection);
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | //////////////////////////////
|
---|
1035 | // データテーブルスケジュール
|
---|
1036 | for(i=0;i<pobj_DataTableSchedule->num;i++){
|
---|
1037 | *((long *)(OpBuffer+pobj_DataTableSchedule->pObpValues[i]))+=ImageBase+MemPos_DataSection;
|
---|
1038 | }
|
---|
1039 | delete pobj_DataTableSchedule;
|
---|
1040 |
|
---|
1041 |
|
---|
1042 | //////////////////////////////////
|
---|
1043 | // インポートテーブルスケジュール(インポート アドレス テーブルを指し示させる)
|
---|
1044 | // ※x86はRVAからのオフセット。x64はRPI(実行中アドレス)からのオフセット
|
---|
1045 | for(i=0;i<pobj_ImportAddrSchedule->num;i++){
|
---|
1046 | DllProc *pDllProc;
|
---|
1047 | pDllProc=pobj_ImportAddrSchedule->ppdi[i];
|
---|
1048 | *((long *)(OpBuffer+pobj_ImportAddrSchedule->pObpValues[i]))=
|
---|
1049 | MemPos_ImportSection+
|
---|
1050 | pDllProc->GetLookupAddress()
|
---|
1051 | -(MemPos_CodeSection+pobj_ImportAddrSchedule->pObpValues[i]+sizeof(long));
|
---|
1052 | }
|
---|
1053 | delete pobj_ImportAddrSchedule;
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | ////////////////////////////////////
|
---|
1057 | // プロシージャポインタスケジュール
|
---|
1058 | for(i=0;i<pobj_SubAddrSchedule->num;i++){
|
---|
1059 | if(pobj_SubAddrSchedule->ppsi[i]->beginOpAddress==0
|
---|
1060 | &&pobj_SubAddrSchedule->ppsi[i]->endOpAddress==0){
|
---|
1061 | SetError(300,NULL,-1);
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | if(pobj_SubAddrSchedule->pbCall[i]){
|
---|
1065 | *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
|
---|
1066 | pobj_SubAddrSchedule->ppsi[i]->beginOpAddress-(pobj_SubAddrSchedule->pObpValues[i]+sizeof(long));
|
---|
1067 | }
|
---|
1068 | else{
|
---|
1069 | *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
|
---|
1070 | pobj_SubAddrSchedule->ppsi[i]->beginOpAddress+ImageBase+MemPos_CodeSection;
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 | delete pobj_SubAddrSchedule;
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | if(bDll){
|
---|
1077 | //DLLの場合はリロケーション情報を生成
|
---|
1078 | pobj_Reloc->ResetRelocBuffer();
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | delete pobj_TempSchedule;
|
---|
1082 |
|
---|
1083 |
|
---|
1084 |
|
---|
1085 | //////////////////////////////////////
|
---|
1086 | // グローバル変数アドレススケジュール
|
---|
1087 | //////////////////////////////////////
|
---|
1088 |
|
---|
1089 | /*
|
---|
1090 | GlobalArea1 - 初期バッファあり
|
---|
1091 | GlobalArea2 - 初期バッファなし
|
---|
1092 | */
|
---|
1093 | for(i=0;i<pobj_GlobalVarSchedule->num;i++){
|
---|
1094 | if(*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x80000000){
|
---|
1095 | //初期バッファなし
|
---|
1096 | *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))=
|
---|
1097 | AllInitGlobalVarSize+
|
---|
1098 | (*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x7FFFFFFF)+
|
---|
1099 | ImageBase+MemPos_RWSection;
|
---|
1100 | }
|
---|
1101 | else{
|
---|
1102 | //初期バッファあり
|
---|
1103 | *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))+=
|
---|
1104 | ImageBase+MemPos_RWSection;
|
---|
1105 | }
|
---|
1106 | }
|
---|
1107 | delete pobj_GlobalVarSchedule;
|
---|
1108 |
|
---|
1109 |
|
---|
1110 | ////////////////////////////////
|
---|
1111 | // リソースアドレススケジュール
|
---|
1112 | extern DWORD *lpdwRSrcAddrSchedule;
|
---|
1113 | extern int RSrcAddrScheduleNum;
|
---|
1114 | for(i=0;i<RSrcAddrScheduleNum;i++){
|
---|
1115 | *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
|
---|
1116 | }
|
---|
1117 | HeapDefaultFree(lpdwRSrcAddrSchedule);
|
---|
1118 |
|
---|
1119 |
|
---|
1120 | //Dosスタブ
|
---|
1121 | char *DosStubBuffer;
|
---|
1122 | int DosStubSize;
|
---|
1123 | extern char BasicSystemDir[MAX_PATH];
|
---|
1124 | sprintf(temporary,"%sSubOperation\\dosstub.pgm",BasicSystemDir);
|
---|
1125 | hFile=CreateFile(temporary,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
|
---|
1126 | if(hFile==INVALID_HANDLE_VALUE){
|
---|
1127 | MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
|
---|
1128 | goto EndWriteOpcode;
|
---|
1129 | }
|
---|
1130 | DosStubSize=GetFileSize(hFile,NULL);
|
---|
1131 | DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
|
---|
1132 | ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
|
---|
1133 | CloseHandle(hFile);
|
---|
1134 |
|
---|
1135 |
|
---|
1136 | extern BOOL bError;
|
---|
1137 | if(bError) goto EndWriteOpcode;
|
---|
1138 |
|
---|
1139 |
|
---|
1140 | ////////////////////////////
|
---|
1141 | // EXEファイルのヘッダ情報
|
---|
1142 | ////////////////////////////
|
---|
1143 |
|
---|
1144 | IMAGE_DOS_HEADER ImageDosHeader;
|
---|
1145 | ImageDosHeader.e_magic= 0x5A4D;
|
---|
1146 | ImageDosHeader.e_cblp= 0x0090;
|
---|
1147 | ImageDosHeader.e_cp= 0x0003;
|
---|
1148 | ImageDosHeader.e_crlc= 0;
|
---|
1149 | ImageDosHeader.e_cparhdr=4;
|
---|
1150 | ImageDosHeader.e_minalloc=0x0000;
|
---|
1151 | ImageDosHeader.e_maxalloc=0xFFFF;
|
---|
1152 | ImageDosHeader.e_ss= 0x0000;
|
---|
1153 | ImageDosHeader.e_sp= 0x00B8;
|
---|
1154 | ImageDosHeader.e_csum= 0x0000;
|
---|
1155 | ImageDosHeader.e_ip= 0x0000;
|
---|
1156 | ImageDosHeader.e_cs= 0x0000;
|
---|
1157 | ImageDosHeader.e_lfarlc=0x0040;
|
---|
1158 | ImageDosHeader.e_ovno= 0x0000;
|
---|
1159 | ImageDosHeader.e_res[0]=0;
|
---|
1160 | ImageDosHeader.e_res[1]=0;
|
---|
1161 | ImageDosHeader.e_res[2]=0;
|
---|
1162 | ImageDosHeader.e_res[3]=0;
|
---|
1163 | ImageDosHeader.e_oemid= 0x0000;
|
---|
1164 | ImageDosHeader.e_oeminfo=0x0000;
|
---|
1165 | ImageDosHeader.e_res2[0]=0;
|
---|
1166 | ImageDosHeader.e_res2[1]=0;
|
---|
1167 | ImageDosHeader.e_res2[2]=0;
|
---|
1168 | ImageDosHeader.e_res2[3]=0;
|
---|
1169 | ImageDosHeader.e_res2[4]=0;
|
---|
1170 | ImageDosHeader.e_res2[5]=0;
|
---|
1171 | ImageDosHeader.e_res2[6]=0;
|
---|
1172 | ImageDosHeader.e_res2[7]=0;
|
---|
1173 | ImageDosHeader.e_res2[8]=0;
|
---|
1174 | ImageDosHeader.e_res2[9]=0;
|
---|
1175 | ImageDosHeader.e_lfanew=0x0100; //PEヘッダの位置
|
---|
1176 |
|
---|
1177 |
|
---|
1178 | /////////////////////////////////////////////
|
---|
1179 | // PEヘッダ
|
---|
1180 | /////////////////////////////////////////////
|
---|
1181 |
|
---|
1182 | IMAGE_NT_HEADERS64 ImagePeHdr;
|
---|
1183 | ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
|
---|
1184 |
|
---|
1185 | //マシンタイプ
|
---|
1186 | ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_AMD64;
|
---|
1187 |
|
---|
1188 | ImagePeHdr.FileHeader.NumberOfSections= bUse_CodeSection+
|
---|
1189 | bUse_ExportSection+
|
---|
1190 | bUse_ImportSection+
|
---|
1191 | bUse_DataSection+
|
---|
1192 | bUse_RWSection+
|
---|
1193 | bUse_RSrcSection+
|
---|
1194 | bUse_RelocSection+
|
---|
1195 | bUse_DebugSection; //セクション数
|
---|
1196 | ImagePeHdr.FileHeader.TimeDateStamp= (DWORD)time(NULL);
|
---|
1197 | ImagePeHdr.FileHeader.PointerToSymbolTable= 0x00000000;
|
---|
1198 | ImagePeHdr.FileHeader.NumberOfSymbols= 0x00000000;
|
---|
1199 | ImagePeHdr.FileHeader.SizeOfOptionalHeader= IMAGE_SIZEOF_NT_OPTIONAL64_HEADER;
|
---|
1200 | if(bDll){
|
---|
1201 | ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
|
---|
1202 | IMAGE_FILE_DLL|
|
---|
1203 | IMAGE_FILE_LARGE_ADDRESS_AWARE;
|
---|
1204 | }
|
---|
1205 | else{
|
---|
1206 | ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_RELOCS_STRIPPED|
|
---|
1207 | IMAGE_FILE_EXECUTABLE_IMAGE|
|
---|
1208 | IMAGE_FILE_LARGE_ADDRESS_AWARE;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | ImagePeHdr.OptionalHeader.Magic= IMAGE_NT_OPTIONAL_HDR64_MAGIC;
|
---|
1212 | ImagePeHdr.OptionalHeader.MajorLinkerVersion= 5;
|
---|
1213 | ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
|
---|
1214 | ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
|
---|
1215 | ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
|
---|
1216 | ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
|
---|
1217 | if(bDll){
|
---|
1218 | if(DllMain_EntryPoint==-1)
|
---|
1219 | ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
|
---|
1220 | else
|
---|
1221 | ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
|
---|
1222 | }
|
---|
1223 | else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
|
---|
1224 | ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
|
---|
1225 |
|
---|
1226 | ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
|
---|
1227 | ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
|
---|
1228 | ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
|
---|
1229 | ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
|
---|
1230 | ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
|
---|
1231 | ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
|
---|
1232 | ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
|
---|
1233 | ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
|
---|
1234 | ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
|
---|
1235 | ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
|
---|
1236 | ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
|
---|
1237 | MemSize_CodeSection+
|
---|
1238 | MemSize_ExportSection+
|
---|
1239 | MemSize_ImportSection+
|
---|
1240 | MemSize_DataSection+
|
---|
1241 | MemSize_RWSection+
|
---|
1242 | MemSize_RSrcSection+
|
---|
1243 | MemSize_RelocSection+
|
---|
1244 | MemSize_DebugSection;//すべてのイメージサイズ
|
---|
1245 | ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
|
---|
1246 | ImagePeHdr.OptionalHeader.CheckSum= 0;
|
---|
1247 | extern unsigned short TypeOfSubSystem;
|
---|
1248 | ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
|
---|
1249 | ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
|
---|
1250 | ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
|
---|
1251 | ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
|
---|
1252 | ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
|
---|
1253 | ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
|
---|
1254 | ImagePeHdr.OptionalHeader.LoaderFlags= 0;
|
---|
1255 | ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
|
---|
1256 |
|
---|
1257 | //データ ディクショナリ
|
---|
1258 | ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
|
---|
1259 | ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
|
---|
1260 | ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress= //IMAGE_IMPORT_DESCRIPTORを指す
|
---|
1261 | MemPos_ImportSection+
|
---|
1262 | LookupSize+ //インポート アドレス テーブル
|
---|
1263 | LookupSize; //ルックアップ テーブル
|
---|
1264 | ImagePeHdr.OptionalHeader.DataDirectory[1].Size=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
|
---|
1265 | ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
|
---|
1266 | ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
|
---|
1267 | ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
|
---|
1268 | ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
|
---|
1269 | ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
|
---|
1270 | ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
|
---|
1271 | ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
|
---|
1272 | ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
|
---|
1273 | ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
|
---|
1274 | ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
|
---|
1275 | ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
|
---|
1276 | ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
|
---|
1277 | ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
|
---|
1278 | ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
|
---|
1279 | ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
|
---|
1280 | ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
|
---|
1281 | ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
|
---|
1282 | ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
|
---|
1283 | ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
|
---|
1284 | ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
|
---|
1285 | ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection;//インポート アドレス テーブル
|
---|
1286 | ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
|
---|
1287 | ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
|
---|
1288 | ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
|
---|
1289 | ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
|
---|
1290 | ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
|
---|
1291 | ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
|
---|
1292 | ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
|
---|
1293 |
|
---|
1294 |
|
---|
1295 | //コードセクションヘッダ
|
---|
1296 | IMAGE_SECTION_HEADER CodeSectionHeader;
|
---|
1297 | memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1298 | lstrcpy((char *)CodeSectionHeader.Name,".text");
|
---|
1299 | CodeSectionHeader.Misc.VirtualSize= obp;
|
---|
1300 | CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
|
---|
1301 | CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
|
---|
1302 | CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
|
---|
1303 | CodeSectionHeader.PointerToRelocations= 0;
|
---|
1304 | CodeSectionHeader.PointerToLinenumbers= 0;
|
---|
1305 | CodeSectionHeader.NumberOfRelocations= 0;
|
---|
1306 | CodeSectionHeader.NumberOfLinenumbers= 0;
|
---|
1307 | CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
|
---|
1308 | IMAGE_SCN_MEM_READ|
|
---|
1309 | IMAGE_SCN_CNT_CODE;
|
---|
1310 |
|
---|
1311 | //エクスポートセクションヘッダ
|
---|
1312 | IMAGE_SECTION_HEADER ExportSectionHeader;
|
---|
1313 | memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1314 | lstrcpy((char *)ExportSectionHeader.Name,".edata");
|
---|
1315 | ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
|
---|
1316 | ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
|
---|
1317 | ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
|
---|
1318 | ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
|
---|
1319 | ExportSectionHeader.PointerToRelocations= 0;
|
---|
1320 | ExportSectionHeader.PointerToLinenumbers= 0;
|
---|
1321 | ExportSectionHeader.NumberOfRelocations= 0;
|
---|
1322 | ExportSectionHeader.NumberOfLinenumbers= 0;
|
---|
1323 | ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
|
---|
1324 | IMAGE_SCN_MEM_READ;
|
---|
1325 |
|
---|
1326 | //インポートセクションヘッダ
|
---|
1327 | IMAGE_SECTION_HEADER ImportSectionHeader;
|
---|
1328 | memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1329 | lstrcpy((char *)ImportSectionHeader.Name,".idata");
|
---|
1330 | ImportSectionHeader.Misc.VirtualSize=
|
---|
1331 | LookupSize+ //インポートアドレステーブル
|
---|
1332 | LookupSize+ //ルックアップテーブル
|
---|
1333 | (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
|
---|
1334 | 16*ImportDllNum+ //DLL名
|
---|
1335 | HintSize; //ヒント名(関数名)テーブル
|
---|
1336 | ImportSectionHeader.VirtualAddress= MemPos_ImportSection; //開始アドレス
|
---|
1337 | ImportSectionHeader.SizeOfRawData= FileSize_ImportSection; //サイズ
|
---|
1338 | ImportSectionHeader.PointerToRawData= FilePos_ImportSection; //ファイル上の開始アドレス
|
---|
1339 | ImportSectionHeader.PointerToRelocations= 0;
|
---|
1340 | ImportSectionHeader.PointerToLinenumbers= 0;
|
---|
1341 | ImportSectionHeader.NumberOfRelocations= 0;
|
---|
1342 | ImportSectionHeader.NumberOfLinenumbers= 0;
|
---|
1343 | ImportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
|
---|
1344 | IMAGE_SCN_MEM_READ;
|
---|
1345 |
|
---|
1346 | //データセクションヘッダ
|
---|
1347 | IMAGE_SECTION_HEADER DataSectionHeader;
|
---|
1348 | memset((char *)DataSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1349 | lstrcpy((char *)DataSectionHeader.Name,".sdata");
|
---|
1350 | DataSectionHeader.Misc.VirtualSize= MemSize_DataSection;
|
---|
1351 | DataSectionHeader.VirtualAddress= MemPos_DataSection;
|
---|
1352 | DataSectionHeader.SizeOfRawData= FileSize_DataSection;
|
---|
1353 | DataSectionHeader.PointerToRawData= FilePos_DataSection;
|
---|
1354 | DataSectionHeader.PointerToRelocations= 0;
|
---|
1355 | DataSectionHeader.PointerToLinenumbers= 0;
|
---|
1356 | DataSectionHeader.NumberOfRelocations= 0;
|
---|
1357 | DataSectionHeader.NumberOfLinenumbers= 0;
|
---|
1358 | DataSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
|
---|
1359 | IMAGE_SCN_MEM_READ|
|
---|
1360 | IMAGE_SCN_MEM_WRITE;
|
---|
1361 |
|
---|
1362 | //リライタブルセクションヘッダ
|
---|
1363 | IMAGE_SECTION_HEADER RWSectionHeader;
|
---|
1364 | memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1365 | lstrcpy((char *)RWSectionHeader.Name,".data");
|
---|
1366 | RWSectionHeader.Misc.VirtualSize= AllInitGlobalVarSize+AllGlobalVarSize;
|
---|
1367 | RWSectionHeader.VirtualAddress= MemPos_RWSection;
|
---|
1368 | RWSectionHeader.SizeOfRawData= FileSize_RWSection;
|
---|
1369 | RWSectionHeader.PointerToRawData= FilePos_RWSection;
|
---|
1370 | RWSectionHeader.PointerToRelocations= 0;
|
---|
1371 | RWSectionHeader.PointerToLinenumbers= 0;
|
---|
1372 | RWSectionHeader.NumberOfRelocations= 0;
|
---|
1373 | RWSectionHeader.NumberOfLinenumbers= 0;
|
---|
1374 | RWSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
|
---|
1375 | IMAGE_SCN_MEM_READ|
|
---|
1376 | IMAGE_SCN_MEM_WRITE;
|
---|
1377 |
|
---|
1378 | //リソースセクションヘッダ
|
---|
1379 | IMAGE_SECTION_HEADER RSrcSectionHeader;
|
---|
1380 | memset((char *)RSrcSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1381 | lstrcpy((char *)RSrcSectionHeader.Name,".rsrc");
|
---|
1382 | RSrcSectionHeader.Misc.VirtualSize= RSrcSectionSize;
|
---|
1383 | RSrcSectionHeader.VirtualAddress= MemPos_RSrcSection;
|
---|
1384 | RSrcSectionHeader.SizeOfRawData= FileSize_RSrcSection;
|
---|
1385 | RSrcSectionHeader.PointerToRawData= FilePos_RSrcSection;
|
---|
1386 | RSrcSectionHeader.PointerToRelocations= 0;
|
---|
1387 | RSrcSectionHeader.PointerToLinenumbers= 0;
|
---|
1388 | RSrcSectionHeader.NumberOfRelocations= 0;
|
---|
1389 | RSrcSectionHeader.NumberOfLinenumbers= 0;
|
---|
1390 | RSrcSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
|
---|
1391 | IMAGE_SCN_MEM_READ;
|
---|
1392 |
|
---|
1393 | //リロケーションセクションヘッダ
|
---|
1394 | IMAGE_SECTION_HEADER RelocSectionHeader;
|
---|
1395 | memset((char *)RelocSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1396 | lstrcpy((char *)RelocSectionHeader.Name,".reloc");
|
---|
1397 | RelocSectionHeader.Misc.VirtualSize= pobj_Reloc->length;
|
---|
1398 | RelocSectionHeader.VirtualAddress= MemPos_RelocSection; //開始アドレス
|
---|
1399 | RelocSectionHeader.SizeOfRawData= FileSize_RelocSection; //サイズ
|
---|
1400 | RelocSectionHeader.PointerToRawData= FilePos_RelocSection; //ファイル上の開始アドレス
|
---|
1401 | RelocSectionHeader.PointerToRelocations= 0;
|
---|
1402 | RelocSectionHeader.PointerToLinenumbers= 0;
|
---|
1403 | RelocSectionHeader.NumberOfRelocations= 0;
|
---|
1404 | RelocSectionHeader.NumberOfLinenumbers= 0;
|
---|
1405 | RelocSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
|
---|
1406 | IMAGE_SCN_MEM_DISCARDABLE|
|
---|
1407 | IMAGE_SCN_MEM_READ;
|
---|
1408 |
|
---|
1409 | //デバッグセクションヘッダ
|
---|
1410 | IMAGE_SECTION_HEADER DebugSectionHeader;
|
---|
1411 | memset((char *)DebugSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
|
---|
1412 | lstrcpy((char *)DebugSectionHeader.Name,".debug");
|
---|
1413 | DebugSectionHeader.Misc.VirtualSize= pobj_DebugSection->length;
|
---|
1414 | DebugSectionHeader.VirtualAddress= MemPos_DebugSection; //開始アドレス
|
---|
1415 | DebugSectionHeader.SizeOfRawData= FileSize_DebugSection; //サイズ
|
---|
1416 | DebugSectionHeader.PointerToRawData= FilePos_DebugSection; //ファイル上の開始アドレス
|
---|
1417 | DebugSectionHeader.PointerToRelocations= 0;
|
---|
1418 | DebugSectionHeader.PointerToLinenumbers= 0;
|
---|
1419 | DebugSectionHeader.NumberOfRelocations= 0;
|
---|
1420 | DebugSectionHeader.NumberOfLinenumbers= 0;
|
---|
1421 | DebugSectionHeader.Characteristics= IMAGE_SCN_MEM_DISCARDABLE|
|
---|
1422 | IMAGE_SCN_MEM_READ;
|
---|
1423 |
|
---|
1424 |
|
---|
1425 | hFile=CreateFile(OutputFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
|
---|
1426 | if(hFile==INVALID_HANDLE_VALUE){
|
---|
1427 | SetError(53,OutputFileName,-1);
|
---|
1428 | goto EndWriteOpcode;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | //ヘッダ
|
---|
1432 | WriteFile(hFile,(void *)&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),(DWORD *)&i2,NULL);
|
---|
1433 | i=i2;
|
---|
1434 |
|
---|
1435 | //Dosスタブ
|
---|
1436 | WriteFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
|
---|
1437 | i+=i2;
|
---|
1438 |
|
---|
1439 | //0x0100までNULLを並べる
|
---|
1440 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,0x0100-i);
|
---|
1441 | WriteFile(hFile,temp2,0x0100-i,(DWORD *)&i2,NULL);
|
---|
1442 | HeapDefaultFree(temp2);
|
---|
1443 | i+=i2;
|
---|
1444 |
|
---|
1445 | //PEヘッダ
|
---|
1446 | WriteFile(hFile,&ImagePeHdr,sizeof(IMAGE_NT_HEADERS64),(DWORD *)&i2,NULL);
|
---|
1447 | i+=i2;
|
---|
1448 |
|
---|
1449 | //コード セクション ヘッダ
|
---|
1450 | WriteFile(hFile,&CodeSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1451 | i+=i2;
|
---|
1452 |
|
---|
1453 | if(bUse_ExportSection){
|
---|
1454 | //エクスポート セクション ヘッダ
|
---|
1455 | WriteFile(hFile,&ExportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1456 | i+=i2;
|
---|
1457 | }
|
---|
1458 | if(bUse_ImportSection){
|
---|
1459 | //インポート セクション ヘッダ
|
---|
1460 | WriteFile(hFile,&ImportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1461 | i+=i2;
|
---|
1462 | }
|
---|
1463 | if(bUse_DataSection){
|
---|
1464 | //データ セクション ヘッダ
|
---|
1465 | WriteFile(hFile,&DataSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1466 | i+=i2;
|
---|
1467 | }
|
---|
1468 | if(bUse_RWSection){
|
---|
1469 | //リライタブルセクションヘッダ
|
---|
1470 | WriteFile(hFile,&RWSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1471 | i+=i2;
|
---|
1472 | }
|
---|
1473 | if(bUse_RSrcSection){
|
---|
1474 | //リソースセクションヘッダ
|
---|
1475 | WriteFile(hFile,&RSrcSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1476 | i+=i2;
|
---|
1477 | }
|
---|
1478 | if(bUse_RelocSection){
|
---|
1479 | //リロケーションセクションヘッダ
|
---|
1480 | WriteFile(hFile,&RelocSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1481 | i+=i2;
|
---|
1482 | }
|
---|
1483 | if(bUse_DebugSection){
|
---|
1484 | //デバッグセクションヘッダ
|
---|
1485 | WriteFile(hFile,&DebugSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
|
---|
1486 | i+=i2;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | //EXE_HEADER_SIZEまでNULLを並べる
|
---|
1490 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,EXE_HEADER_SIZE-i);
|
---|
1491 | WriteFile(hFile,temp2,EXE_HEADER_SIZE-i,(DWORD *)&i2,NULL);
|
---|
1492 | HeapDefaultFree(temp2);
|
---|
1493 | i+=i2;
|
---|
1494 |
|
---|
1495 | //コード
|
---|
1496 | WriteFile(hFile,OpBuffer,obp,(DWORD *)&i2,NULL);
|
---|
1497 | i+=i2;
|
---|
1498 |
|
---|
1499 | //FilePos_ExportSectionまでNULLを並べる
|
---|
1500 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ExportSection-i);
|
---|
1501 | WriteFile(hFile,temp2,FilePos_ExportSection-i,(DWORD *)&i2,NULL);
|
---|
1502 | HeapDefaultFree(temp2);
|
---|
1503 | i+=i2;
|
---|
1504 |
|
---|
1505 | if(bUse_ExportSection){
|
---|
1506 | //エクスポート ディレクトリ テーブル
|
---|
1507 | WriteFile(hFile,&ImageExportDirectory,sizeof(IMAGE_EXPORT_DIRECTORY),(DWORD *)&i2,NULL);
|
---|
1508 | i+=i2;
|
---|
1509 |
|
---|
1510 | //エクスポート アドレス テーブル
|
---|
1511 | WriteFile(hFile,lpdwExportAddressTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
|
---|
1512 | i+=i2;
|
---|
1513 |
|
---|
1514 | //エクスポート名ポインタ テーブル
|
---|
1515 | WriteFile(hFile,lpdwExportNamePointerTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
|
---|
1516 | i+=i2;
|
---|
1517 |
|
---|
1518 | //エクスポート序数テーブル
|
---|
1519 | WriteFile(hFile,lpwExportOrdinalTable,ExportNum*sizeof(WORD),(DWORD *)&i2,NULL);
|
---|
1520 | i+=i2;
|
---|
1521 |
|
---|
1522 | //シンボル名
|
---|
1523 | WriteFile(hFile,lpExportNames,ExportNamesLength,(DWORD *)&i2,NULL);
|
---|
1524 | i+=i2;
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | //FilePos_ImportSectionまでNULLを並べる
|
---|
1528 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ImportSection-i);
|
---|
1529 | WriteFile(hFile,temp2,FilePos_ImportSection-i,(DWORD *)&i2,NULL);
|
---|
1530 | HeapDefaultFree(temp2);
|
---|
1531 | i+=i2;
|
---|
1532 |
|
---|
1533 | if(bUse_ImportSection){
|
---|
1534 | //インポート アドレス テーブル
|
---|
1535 | WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
|
---|
1536 | i+=i2;
|
---|
1537 |
|
---|
1538 | //ルックアップ テーブル
|
---|
1539 | WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
|
---|
1540 | i+=i2;
|
---|
1541 |
|
---|
1542 | //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
|
---|
1543 | for(i3=0;i3<(ImportDllNum+1);i3++){
|
---|
1544 | WriteFile(hFile,&pImportDescriptor[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
|
---|
1545 | i+=i2;
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | //DLL名
|
---|
1549 | for(i3=0;i3<ImportDllNum;i3++){
|
---|
1550 | WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
|
---|
1551 | i+=i2;
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | //ヒント テーブル
|
---|
1555 | WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
|
---|
1556 | i+=i2;
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | //FilePos_DataSectionまでNULLを並べる
|
---|
1560 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_DataSection-i);
|
---|
1561 | WriteFile(hFile,temp2,FilePos_DataSection-i,(DWORD *)&i2,NULL);
|
---|
1562 | HeapDefaultFree(temp2);
|
---|
1563 | i+=i2;
|
---|
1564 |
|
---|
1565 | if(bUse_DataSection){
|
---|
1566 | //データ テーブル
|
---|
1567 | WriteFile(hFile,dataTable.GetPtr(),dataTable.GetSize(),(DWORD *)&i2,NULL);
|
---|
1568 | i+=i2;
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | //FilePos_RWSectionまでNULLを並べる
|
---|
1572 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RWSection-i);
|
---|
1573 | WriteFile(hFile,temp2,FilePos_RWSection-i,(DWORD *)&i2,NULL);
|
---|
1574 | HeapDefaultFree(temp2);
|
---|
1575 | i+=i2;
|
---|
1576 |
|
---|
1577 | if(bUse_RWSection){
|
---|
1578 | //リライタブル データ テーブル(グローバル変数の初期バッファ)
|
---|
1579 | initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
|
---|
1580 | HEAP_ZERO_MEMORY,
|
---|
1581 | initGlobalBuf,
|
---|
1582 | FileSize_RWSection);
|
---|
1583 | WriteFile(hFile,initGlobalBuf,FileSize_RWSection,(DWORD *)&i2,NULL);
|
---|
1584 | i+=i2;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | //FilePos_RSrcSectionまでNULLを並べる
|
---|
1588 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
|
---|
1589 | WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
|
---|
1590 | HeapDefaultFree(temp2);
|
---|
1591 | i+=i2;
|
---|
1592 |
|
---|
1593 | if(bUse_RSrcSection){
|
---|
1594 | //リソースバッファ
|
---|
1595 | WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
|
---|
1596 | i+=i2;
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | //FilePos_RelocSectionまでNULLを並べる
|
---|
1600 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
|
---|
1601 | WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
|
---|
1602 | HeapDefaultFree(temp2);
|
---|
1603 | i+=i2;
|
---|
1604 |
|
---|
1605 | if(bUse_RelocSection){
|
---|
1606 | //リロケーション情報
|
---|
1607 | WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
|
---|
1608 | i+=i2;
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | //ファイルアラインメントを考慮
|
---|
1612 | if(i%FILE_ALIGNMENT){
|
---|
1613 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
|
---|
1614 | WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
|
---|
1615 | HeapDefaultFree(temp2);
|
---|
1616 | i+=i2;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | if(bUse_DebugSection){
|
---|
1620 | //デバッグセクション
|
---|
1621 | WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
|
---|
1622 | i+=i2;
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | //ファイルアラインメントを考慮
|
---|
1626 | if(i%FILE_ALIGNMENT){
|
---|
1627 | temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
|
---|
1628 | WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
|
---|
1629 | HeapDefaultFree(temp2);
|
---|
1630 | i+=i2;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | //書き込み終了
|
---|
1634 | CloseHandle(hFile);
|
---|
1635 |
|
---|
1636 |
|
---|
1637 | EndWriteOpcode:
|
---|
1638 |
|
---|
1639 | //Dosスタブ用のメモリを解放
|
---|
1640 | HeapDefaultFree(DosStubBuffer);
|
---|
1641 |
|
---|
1642 | //エクスポート テーブル情報を解放
|
---|
1643 | HeapDefaultFree(lpdwExportAddressTable);
|
---|
1644 | HeapDefaultFree(lpdwExportNamePointerTable);
|
---|
1645 | HeapDefaultFree(lpwExportOrdinalTable);
|
---|
1646 |
|
---|
1647 | //インポートDLL情報を解放
|
---|
1648 | HeapDefaultFree(pImportDescriptor);
|
---|
1649 | for(i=0;i<ImportDllNum;i++)
|
---|
1650 | HeapDefaultFree(ppDllNames[i]);
|
---|
1651 | HeapDefaultFree(ppDllNames);
|
---|
1652 |
|
---|
1653 | //ルックアップテーブルに関する情報を解放
|
---|
1654 | HeapDefaultFree(pLookupTable);
|
---|
1655 |
|
---|
1656 | //ヒントテーブルに関する情報を解放
|
---|
1657 | HeapDefaultFree(pHintTable);
|
---|
1658 |
|
---|
1659 | //グローバル変数の初期バッファを解放
|
---|
1660 | HeapDefaultFree(initGlobalBuf);
|
---|
1661 |
|
---|
1662 | //コードバッファを解放
|
---|
1663 | HeapDefaultFree(OpBuffer);
|
---|
1664 | OpBuffer=0;
|
---|
1665 |
|
---|
1666 | //リソースセクションバッファを解放
|
---|
1667 | HeapDefaultFree(RSrcSectionBuffer);
|
---|
1668 |
|
---|
1669 | //デバッグセクションを開放
|
---|
1670 | delete pobj_DebugSection;
|
---|
1671 |
|
---|
1672 | //リロケーション情報を解放
|
---|
1673 | delete pobj_Reloc;
|
---|
1674 |
|
---|
1675 | //列挙体に関する情報の破棄
|
---|
1676 | CEnumParent::DestroyEnum();
|
---|
1677 |
|
---|
1678 | //クラスに関するメモリを解放
|
---|
1679 | delete pobj_DBClass;
|
---|
1680 | pobj_DBClass=0;
|
---|
1681 | }
|
---|