source: dev/BasicCompiler32/MakePeHdr.cpp@ 103

Last change on this file since 103 was 103, checked in by dai_9181, 17 years ago

名前空間機能をグローバル変数、定数と列挙型に適用。
一部、クラスの静的メンバと名前空間の相性が悪いコードが潜んでいるため、要改修

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