source: dev/BasicCompiler32/MakePeHdr.cpp@ 95

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

RegisterGlobalRootsの自動生成に対応

File size: 53.9 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 extern LABEL *pLabelNames;
355 extern int MaxLabelNum;
356 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
357 MaxLabelNum=0;
358
359 //Gotoラベルスケジュール
360 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
361 extern int GotoLabelScheduleNum;
362 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
363 GotoLabelScheduleNum=0;
364
365 //With情報のメモリを確保
366 extern WITHINFO WithInfo;
367 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
368 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
369 WithInfo.num=0;
370
371 //Continueアドレスを初期化
372 extern DWORD dwContinueAddress;
373 dwContinueAddress=-1;
374
375
376 //未完成
377 //OpBuffer[obp++]=(char)0xCC;
378
379 //push ebp
380 op_push(REG_EBP);
381
382 if(bDebugCompile){
383 //デバッグ用の変数を定義
384 DebugVariable();
385 }
386
387 //GC用の変数を定義
388 InitGCVariables();
389
390 //_System_StartupProgramの呼び出し
391 op_call(pSub_System_StartupProgram);
392
393 //クラスに属する静的メンバを定義
394 CMember::InitStaticMember();
395
396 //グローバル実行領域をコンパイル開始
397 CompileBuffer(0,0);
398
399 //ラベル用のメモリを解放
400 for(i=0;i<MaxLabelNum;i++){
401 if(pLabelNames[i].pName) HeapDefaultFree(pLabelNames[i].pName);
402 }
403 HeapDefaultFree(pLabelNames);
404
405 //Goto未知ラベルスケジュールを解放
406 for(i=0;i<GotoLabelScheduleNum;i++){
407 if(pGotoLabelSchedule[i].pName){
408 SetError(6,pGotoLabelSchedule[i].pName,pGotoLabelSchedule[i].now_cp);
409 HeapDefaultFree(pGotoLabelSchedule[i].pName);
410 }
411 else{
412 sprintf(temporary,"%d",pGotoLabelSchedule[i].line);
413 SetError(6,temporary,pGotoLabelSchedule[i].now_cp);
414 }
415 }
416 HeapDefaultFree(pGotoLabelSchedule);
417
418
419 ///////////////////////////////////////
420 // グローバル文字列変数の解放処理
421 ///////////////////////////////////////
422
423 //call _System_End
424 extern UserProc *pSub_System_End;
425 op_call(pSub_System_End);
426
427
428 //xor eax,eax(eaxを0に初期化する)
429 op_zero_reg(REG_EAX);
430
431 //pop ebp
432 op_pop(REG_EBP);
433
434 //ret
435 OpBuffer[obp++]=(char)0xC3;
436
437 //グローバル実行領域のコードサイズ
438 GlobalOpBufferSize=obp;
439
440 //With情報のメモリを解放
441 for(i=0;i<WithInfo.num;i++){
442 SetError(22,"With",WithInfo.pWithCp[i]);
443 HeapDefaultFree(WithInfo.ppName[i]);
444 }
445 HeapDefaultFree(WithInfo.ppName);
446 HeapDefaultFree(WithInfo.pWithCp);
447 }
448 else{
449 ////////////////
450 // DLLの場合
451 ////////////////
452
453 }
454
455 //重複エラー情報管理のメモリを解放(グローバル領域用)
456 for(i=0;i<SynonymErrorNum;i++) HeapDefaultFree(SynonymErrorWords[i]);
457 HeapDefaultFree(SynonymErrorWords);
458 SynonymErrorWords=0;
459
460
461 StepCompileProgress();
462
463
464 /////////////////////
465 // ローカル実行領域
466 /////////////////////
467
468 //プロシージャをコンパイル開始
469 cp=0;
470 CompileLocal();
471
472 // 終了
473 ///////////////////////
474
475 StepCompileProgress();
476
477
478
479 ////////////////////////////////
480 // ここで一旦ログを取る
481 ////////////////////////////////
482 Diagnose();
483
484
485
486 ////////////////////////////////
487 // 使用するDLL関数のチェック
488 ////////////////////////////////
489 for(i=0;i<MAX_HASH;i++){
490 extern DllProc **ppDeclareHash;
491 const DllProc *pDllProc = ppDeclareHash[i];
492 while(pDllProc){
493 if( !pDllProc->IsUsing() ){
494 pDllProc=pDllProc->pNextData;
495 continue;
496 }
497
498 //エラーチェック
499 HINSTANCE hLib;
500 hLib=LoadLibrary( pDllProc->GetDllFileName().c_str() );
501 if(!hLib){
502 extern char OutputFileName[MAX_PATH];
503 char temp2[MAX_PATH],temp3[MAX_PATH];
504 _splitpath(OutputFileName,temp2,temp3,NULL,NULL);
505 lstrcat(temp2,temp3);
506 lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
507 GetFullPath(temp3,temp2);
508 hLib=LoadLibrary(temp3);
509
510 if(!hLib){
511 SetError(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetCodePos());
512 }
513 }
514
515 if(hLib){
516 if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
517 FreeLibrary(hLib);
518 SetError(-107,pDllProc->GetAlias(),pDllProc->GetCodePos());
519 }
520 FreeLibrary(hLib);
521 }
522
523 pDllProc=pDllProc->pNextData;
524 }
525 }
526
527
528
529 /////////////////////////////
530 // リソースデータを読み込む
531 /////////////////////////////
532 extern char ResourceFileName[MAX_PATH];
533 GetResourceData(ResourceFileName);
534
535
536 //////////////////////////////
537 // エクスポート情報(DLLのみ)
538 //////////////////////////////
539 IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
540 DWORD *lpdwExportAddressTable;
541 DWORD *lpdwExportNamePointerTable;
542 WORD *lpwExportOrdinalTable;
543 char lpExportNames[8192];
544 int ExportNamesLength;
545 int ExportNum;
546 int DllMain_EntryPoint;
547
548 DllMain_EntryPoint=-1;
549 ExportNum=0;
550 ExportNamesLength=0;
551 lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
552 lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
553 lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
554
555 if(bUse_ExportSection){
556 _splitpath(OutputFileName,NULL,NULL,lpExportNames,temporary);
557 lstrcat(lpExportNames,temporary);
558 ExportNamesLength=lstrlen(lpExportNames)+1;
559
560 extern UserProc **ppSubHash;
561 UserProc *pUserProc,*psi2;
562 while(1){
563 //辞書順にサーチ
564 temporary[0]=0;
565 for(i=0,psi2=0;i<MAX_HASH;i++){
566 pUserProc=ppSubHash[i];
567 while(pUserProc){
568 if(pUserProc->IsExport()){
569 if(temporary[0]=='\0'){
570 lstrcpy(temporary,pUserProc->GetName().c_str());
571 psi2=pUserProc;
572 }
573 else{
574 i3=lstrlen(temporary);
575 i4=(int)pUserProc->GetName().size();
576 if(i3<i4) i3=i4;
577 if(memcmp(temporary,pUserProc->GetName().c_str(),i3)>0){
578 lstrcpy(temporary,pUserProc->GetName().c_str());
579 psi2=pUserProc;
580 }
581 }
582 }
583 pUserProc=pUserProc->pNextData;
584 }
585 }
586 if(psi2==0) break;
587 pUserProc=psi2;
588
589 pUserProc->ExportOff();
590
591 if( pUserProc->GetName() == "DllMain" ){
592 DllMain_EntryPoint=pUserProc->beginOpAddress;
593 }
594
595 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
596 lpdwExportAddressTable[ExportNum]=pUserProc->beginOpAddress;
597
598 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
599 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
600
601 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
602 lpwExportOrdinalTable[ExportNum]=ExportNum;
603
604 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
605 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
606
607 ExportNum++;
608 }
609 for(i=0;i<ExportNum;i++){
610 lpdwExportNamePointerTable[i]+=
611 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
612 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
613 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
614 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
615 }
616
617 ImageExportDirectory.Characteristics=0;
618 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
619 ImageExportDirectory.MajorVersion=0;
620 ImageExportDirectory.MinorVersion=0;
621 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
622 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
623 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
624 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
625 ImageExportDirectory.Base=1;
626 ImageExportDirectory.NumberOfFunctions=ExportNum;
627 ImageExportDirectory.NumberOfNames=ExportNum;
628 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
629 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
630 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
631 }
632
633
634 /////////////////////
635 //インポートDLL情報
636 /////////////////////
637 extern DllProc **ppDeclareHash;
638 char **ppDllNames;
639 int ImportDllNum;
640
641 /*
642 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
643 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
644 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
645 ...
646 Dll名1[size=0x10]
647 Dll名2[size=0x10]
648 Dll名3[size=0x10]
649 ...
650 ルックアップ テーブル(ヒントを示すRVA)
651 ヒントテーブル
652 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
653 */
654
655 ppDllNames=(char **)HeapAlloc(hHeap,0,1);
656
657 ImportDllNum=0;
658 for(i=0;i<MAX_HASH;i++){
659 const DllProc *pDllProc=ppDeclareHash[i];
660 while(pDllProc){
661 if( !pDllProc->IsUsing() ){
662 pDllProc=pDllProc->pNextData;
663 continue;
664 }
665
666 if( pDllProc->GetDllFileName().size() > 16 ){
667 SetError(7,NULL,cp);
668 break;
669 }
670 for(i2=0;i2<ImportDllNum;i2++){
671 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
672 break;
673 }
674 }
675 if(i2==ImportDllNum){
676 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
677 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
678 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
679 ImportDllNum++;
680 }
681
682 pDllProc=pDllProc->pNextData;
683 }
684 }
685
686 //インポート テーブル、及びルックアップ テーブル サイズの計算
687 IMAGE_IMPORT_DESCRIPTOR *pImportTable;
688 int LookupSize;
689 LookupSize=0;
690 pImportTable=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
691 i3=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
692 for(i=0;i<ImportDllNum;i++){
693 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
694 pImportTable[i].OriginalFirstThunk=i3+(ImportDllNum*0x10)+LookupSize;
695 pImportTable[i].TimeDateStamp=0;
696 pImportTable[i].ForwarderChain=0;
697 pImportTable[i].Name=i3+i*0x10;
698 for(i2=0;i2<MAX_HASH;i2++){
699 const DllProc *pDllProc=ppDeclareHash[i2];
700 while(pDllProc){
701 if( !pDllProc->IsUsing() ){
702 pDllProc=pDllProc->pNextData;
703 continue;
704 }
705
706 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
707 //ルックアップデータのサイズを追加
708 LookupSize+=sizeof(DWORD);
709 }
710
711 pDllProc=pDllProc->pNextData;
712 }
713 }
714 LookupSize+=sizeof(DWORD); //NULL用
715 }
716 memset(&pImportTable[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
717
718 //ルックアップ テーブル、ヒント テーブル
719 DWORD *pLookupTable;
720 pLookupTable=(DWORD *)HeapAlloc(hHeap,0,LookupSize*sizeof(DWORD)+1);
721 char *pHintTable;
722 int HintSize,HintAllocSize;
723 HintSize=0;
724 HintAllocSize=128*2;
725 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
726 i3+=ImportDllNum*0x10;
727 for(i=0,i5=0;i<ImportDllNum;i++){
728 for(i2=0;i2<MAX_HASH;i2++){
729 DllProc *pDllProc=ppDeclareHash[i2];
730 while(pDllProc){
731 if( !pDllProc->IsUsing() ){
732 pDllProc=pDllProc->pNextData;
733 continue;
734 }
735
736 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
737 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
738 pDllProc->SetLookupAddress( i3+(i5*sizeof(DWORD)) );
739
740 //ルックアップ テーブル
741 pLookupTable[i5++]=i3+LookupSize+HintSize;
742
743 //ヒント テーブル
744 pHintTable[HintSize++]=0;
745 pHintTable[HintSize++]=0;
746 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
747 i4=(int)pDllProc->GetAlias().size();
748 HintSize+=i4+1;
749 if(i4%2==0) pHintTable[HintSize++]=0;
750
751 if(HintAllocSize<HintSize+128){
752 HintAllocSize+=128;
753 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
754 }
755 }
756
757 pDllProc=pDllProc->pNextData;
758 }
759 }
760 pLookupTable[i5++]=0;
761 }
762
763
764 if(bDll){
765 //DLLの場合はリロケーション情報を仮生成
766 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
767 pobj_Reloc->ResetRelocBuffer();
768 }
769
770
771
772 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
773 //(デバッグ情報で利用される)
774 extern int AllInitGlobalVarSize;
775 foreach( Variable *pVar, globalVars ){
776 if(pVar->offset&0x80000000){
777 pVar->offset=(pVar->offset&0x7FFFFFFF)+AllInitGlobalVarSize;
778 }
779 }
780
781
782
783 ////////////////////////////////////
784 // デバッグセクションを生成
785 ////////////////////////////////////
786
787 //機械語コードとBasicコードの関係を整理
788 extern LINEINFO *pLineInfo;
789 extern int MaxLineInfoNum;
790 int MinObp,MaxObp;
791 LINEINFO *pTempLineInfo;
792 pTempLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,MaxLineInfoNum*sizeof(LINEINFO));
793 i2=0;
794 MaxObp=0;
795 while(1){
796 for(i3=0,MinObp=0x0FFFFFFF;i3<MaxLineInfoNum;i3++){
797 if(pLineInfo[i3].TopObp<MinObp&&MaxObp<pLineInfo[i3].TopObp){
798 MinObp=pLineInfo[i3].TopObp;
799 i4=i3;
800 }
801 }
802 if(MinObp==0x0FFFFFFF) break;
803 pTempLineInfo[i2]=pLineInfo[i4];
804 MaxObp=pTempLineInfo[i2].TopObp;
805 i2++;
806 }
807 HeapDefaultFree(pLineInfo);
808 pLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,i2*sizeof(LINEINFO));
809 memcpy(pLineInfo,pTempLineInfo,i2*sizeof(LINEINFO));
810 MaxLineInfoNum=i2;
811 HeapDefaultFree(pTempLineInfo);
812
813 //デバッグセクションを生成
814 CDebugSection *pobj_DebugSection;
815 pobj_DebugSection=new CDebugSection();
816 extern BOOL bDebugCompile;
817 extern BOOL bError;
818 if(bDebugCompile&&bError==0){
819 pobj_DebugSection->make();
820 }
821
822
823
824 /////////////////////////////////////
825 // 各セクションの位置とサイズを計算
826 /////////////////////////////////////
827
828 //コードセッションのファイル上のサイズ
829 if(obp%FILE_ALIGNMENT) FileSize_CodeSection=obp+(FILE_ALIGNMENT-obp%FILE_ALIGNMENT);
830 else FileSize_CodeSection=obp;
831 if(FileSize_CodeSection) bUse_CodeSection=1;
832 else bUse_CodeSection=0;
833
834 //エクスポートセクションのファイル上のサイズ
835 i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
836 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
837 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
838 ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
839 ExportNamesLength; //シンボル名バッファ
840 if(bUse_ExportSection){
841 if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
842 else FileSize_ExportSection=i;
843 }
844 else FileSize_ExportSection=0;
845
846 //インポートセクションのファイル上のサイズ
847 i=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
848 16*ImportDllNum+ //DLL名
849 LookupSize+ //ルックアップテーブル
850 HintSize+ //ヒント名(関数名)テーブル
851 LookupSize; //インポート アドレス テーブル
852 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
853 else FileSize_ImportSection=i;
854 bUse_ImportSection=1; //インポートセクションは必ず存在する
855
856 //データセクションのファイル上のサイズ
857 if(dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=dataTable.GetSize()+(FILE_ALIGNMENT-dataTable.GetSize()%FILE_ALIGNMENT);
858 else FileSize_DataSection=dataTable.GetSize();
859 if(FileSize_DataSection) bUse_DataSection=1;
860 else bUse_DataSection=0;
861
862 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
863 if(AllInitGlobalVarSize%FILE_ALIGNMENT) FileSize_RWSection=AllInitGlobalVarSize+(FILE_ALIGNMENT-AllInitGlobalVarSize%FILE_ALIGNMENT);
864 else{
865 if(AllInitGlobalVarSize) FileSize_RWSection=AllInitGlobalVarSize;
866 else FileSize_RWSection=FILE_ALIGNMENT;
867 }
868 bUse_RWSection=1;
869
870 //リソースセクションのファイル上のサイズ
871 char *RSrcSectionBuffer;
872 int RSrcSectionSize;
873 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
874 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
875 else FileSize_RSrcSection=RSrcSectionSize;
876 if(FileSize_RSrcSection) bUse_RSrcSection=1;
877 else bUse_RSrcSection=0;
878
879 //リロケーションセクションのファイル上のサイズ
880 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
881 else FileSize_RelocSection=pobj_Reloc->length;
882 if(FileSize_RelocSection) bUse_RelocSection=1;
883 else bUse_RelocSection=0;
884
885 //デバッグセクションのファイル上のサイズ
886 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
887 else FileSize_DebugSection=pobj_DebugSection->length;
888 if(FileSize_DebugSection) bUse_DebugSection=1;
889 else bUse_DebugSection=0;
890
891
892 //各セッションのファイル上の位置
893 FilePos_CodeSection= EXE_HEADER_SIZE;
894 FilePos_ExportSection= FilePos_CodeSection+
895 FileSize_CodeSection;
896 FilePos_ImportSection= FilePos_ExportSection+
897 FileSize_ExportSection;
898 FilePos_DataSection= FilePos_ImportSection+
899 FileSize_ImportSection;
900 FilePos_RWSection= FilePos_DataSection+
901 FileSize_DataSection;
902 FilePos_RSrcSection= FilePos_RWSection+
903 FileSize_RWSection;
904 FilePos_RelocSection= FilePos_RSrcSection+
905 FileSize_RSrcSection;
906 FilePos_DebugSection= FilePos_RelocSection+
907 FileSize_RelocSection;
908
909
910 //コードセッションのメモリ上のサイズ
911 if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
912 else MemSize_CodeSection=FileSize_CodeSection;
913
914 //エクスポートセクションのメモリ上のサイズ
915 if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
916 else MemSize_ExportSection=FileSize_ExportSection;
917
918 //インポートセクションのメモリ上のサイズ
919 if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
920 else MemSize_ImportSection=FileSize_ImportSection;
921
922 //データセクションのメモリ上のサイズ
923 if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
924 else MemSize_DataSection=FileSize_DataSection;
925
926 //リライタブルセクションのメモリ上のサイズ
927 extern int AllGlobalVarSize;
928 i=AllInitGlobalVarSize+AllGlobalVarSize;
929 if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
930 else MemSize_RWSection=i;
931
932 //リソースセクションのメモリ上のサイズ
933 if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
934 else MemSize_RSrcSection=FileSize_RSrcSection;
935
936 //リロケーションセクションのメモリ上のサイズ
937 if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
938 else MemSize_RelocSection=FileSize_RelocSection;
939
940 //デバッグセクションのメモリ上のサイズ
941 if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
942 else MemSize_DebugSection=FileSize_DebugSection;
943
944
945 //各セッションのメモリ上の位置
946 if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
947 MemSize_CodeSection;
948 else MemPos_ExportSection=0;
949 if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
950 MemSize_CodeSection+
951 MemSize_ExportSection;
952 else MemPos_ImportSection=0;
953 if(bUse_DataSection) MemPos_DataSection= 0x1000+
954 MemSize_CodeSection+
955 MemSize_ExportSection+
956 MemSize_ImportSection;
957 else MemPos_DataSection=0;
958 if(bUse_RWSection) MemPos_RWSection= 0x1000+
959 MemSize_CodeSection+
960 MemSize_ExportSection+
961 MemSize_ImportSection+
962 MemSize_DataSection;
963 else MemPos_RWSection=0;
964 if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
965 MemSize_CodeSection+
966 MemSize_ExportSection+
967 MemSize_ImportSection+
968 MemSize_DataSection+
969 MemSize_RWSection;
970 else MemPos_RSrcSection=0;
971 if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
972 MemSize_CodeSection+
973 MemSize_ExportSection+
974 MemSize_ImportSection+
975 MemSize_DataSection+
976 MemSize_RWSection+
977 MemSize_RSrcSection;
978 else MemPos_RelocSection=0;
979 if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
980 MemSize_CodeSection+
981 MemSize_ExportSection+
982 MemSize_ImportSection+
983 MemSize_DataSection+
984 MemSize_RWSection+
985 MemSize_RSrcSection+
986 MemSize_RelocSection;
987 else MemPos_DebugSection=0;
988
989
990
991 /////////////////////////////
992 // GC用の変数スケジュール
993 /////////////////////////////
994 PerformedGcVarSchedule();
995
996
997
998 ////////////////////////////
999 // エクスポート情報の再配置
1000 ////////////////////////////
1001 if(bUse_ExportSection){
1002 for(i=0;i<ExportNum;i++){
1003 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1004 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1005 }
1006
1007 ImageExportDirectory.Name+= MemPos_ExportSection;
1008 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1009 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1010 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1011 }
1012
1013
1014 ////////////////////////////
1015 // インポート情報の再配置
1016 ////////////////////////////
1017 for(i=0,i5=0;i<ImportDllNum;i++){
1018 //インポート テーブル(IMAGE_IMPORT_DESCRIPTOR)
1019 pImportTable[i].OriginalFirstThunk+=MemPos_ImportSection;
1020 pImportTable[i].Name+=MemPos_ImportSection;
1021
1022 //インポート アドレス テーブル(ルックアップとヒントを飛び越す)
1023 pImportTable[i].FirstThunk=pImportTable[i].OriginalFirstThunk+
1024 LookupSize+ //ルックアップテーブル
1025 HintSize; //ヒント名(関数名)テーブル
1026
1027 for(i2=0;i2<MAX_HASH;i2++){
1028 const DllProc *pDllProc=ppDeclareHash[i2];
1029 while(pDllProc){
1030 if( !pDllProc->IsUsing() ){
1031 pDllProc=pDllProc->pNextData;
1032 continue;
1033 }
1034
1035 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1036 //ルックアップ テーブル
1037 pLookupTable[i5++]+=MemPos_ImportSection;
1038 }
1039
1040 pDllProc=pDllProc->pNextData;
1041 }
1042 }
1043 i5++;
1044 }
1045
1046
1047 ////////////////////////////////////////
1048 //仮想関数データテーブルスケジュール
1049 pobj_DBClass->ActionVtblSchedule(ImageBase,MemPos_CodeSection);
1050
1051
1052 //////////////////////////////
1053 // データテーブルスケジュール
1054 for(i=0;i<pobj_DataTableSchedule->num;i++){
1055 *((long *)(OpBuffer+pobj_DataTableSchedule->pObpValues[i]))+=ImageBase+MemPos_DataSection;
1056 }
1057 delete pobj_DataTableSchedule;
1058
1059
1060 //////////////////////////////////
1061 // インポートテーブルスケジュール(インポート アドレス テーブルを指し示させる)
1062 for(i=0;i<pobj_ImportAddrSchedule->num;i++){
1063 const DllProc *pDllProc=pobj_ImportAddrSchedule->ppdi[i];
1064 *((long *)(OpBuffer+pobj_ImportAddrSchedule->pObpValues[i]))=
1065 ImageBase+
1066 MemPos_ImportSection+
1067 LookupSize+ //ルックアップテーブル
1068 HintSize+ //ヒント名(関数名)テーブル
1069 pDllProc->GetLookupAddress();
1070 }
1071 delete pobj_ImportAddrSchedule;
1072
1073
1074 ////////////////////////////////////
1075 // プロシージャポインタスケジュール
1076 for(i=0;i<pobj_SubAddrSchedule->num;i++){
1077 if(pobj_SubAddrSchedule->ppsi[i]->beginOpAddress==0
1078 &&pobj_SubAddrSchedule->ppsi[i]->endOpAddress==0){
1079 SetError(300,NULL,-1);
1080 }
1081
1082 if(pobj_SubAddrSchedule->pbCall[i]){
1083 *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
1084 pobj_SubAddrSchedule->ppsi[i]->beginOpAddress-(pobj_SubAddrSchedule->pObpValues[i]+sizeof(long));
1085 }
1086 else{
1087 *((long *)(OpBuffer+pobj_SubAddrSchedule->pObpValues[i]))=
1088 pobj_SubAddrSchedule->ppsi[i]->beginOpAddress+ImageBase+MemPos_CodeSection;
1089 }
1090 }
1091 delete pobj_SubAddrSchedule;
1092
1093
1094 if(bDll){
1095 //DLLの場合はリロケーション情報を生成
1096 pobj_Reloc->ResetRelocBuffer();
1097 }
1098
1099
1100
1101 //////////////////////////////////////
1102 // グローバル変数アドレススケジュール
1103 //////////////////////////////////////
1104
1105 /*
1106 GlobalArea1 - 初期バッファあり
1107 GlobalArea2 - 初期バッファなし
1108 */
1109 for(i=0;i<pobj_GlobalVarSchedule->num;i++){
1110 if(*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x80000000){
1111 //初期バッファなし
1112 *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))=
1113 AllInitGlobalVarSize+
1114 (*((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i])) & 0x7FFFFFFF)+
1115 ImageBase+MemPos_RWSection;
1116 }
1117 else{
1118 //初期バッファあり
1119 *((long *)(OpBuffer+pobj_GlobalVarSchedule->pObpValues[i]))+=
1120 ImageBase+MemPos_RWSection;
1121 }
1122 }
1123 delete pobj_GlobalVarSchedule;
1124
1125
1126
1127 ////////////////////////////////
1128 // リソースアドレススケジュール
1129 extern DWORD *lpdwRSrcAddrSchedule;
1130 extern int RSrcAddrScheduleNum;
1131 for(i=0;i<RSrcAddrScheduleNum;i++){
1132 *(DWORD *)(RSrcSectionBuffer+lpdwRSrcAddrSchedule[i])+=MemPos_RSrcSection;
1133 }
1134 HeapDefaultFree(lpdwRSrcAddrSchedule);
1135
1136
1137 //Dosスタブ
1138 char *DosStubBuffer;
1139 int DosStubSize;
1140 extern char BasicSystemDir[MAX_PATH];
1141 sprintf(temporary,"%sSubOperation\\dosstub.pgm",BasicSystemDir);
1142 hFile=CreateFile(temporary,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
1143 if(hFile==INVALID_HANDLE_VALUE){
1144 MessageBox(hOwnerEditor,"dosstub.pgmの読み込みに失敗","error",MB_OK);
1145 goto EndWriteOpcode;
1146 }
1147 DosStubSize=GetFileSize(hFile,NULL);
1148 DosStubBuffer=(char *)HeapAlloc(hHeap,0,DosStubSize);
1149 ReadFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1150 CloseHandle(hFile);
1151
1152
1153 extern BOOL bError;
1154 if(bError) goto EndWriteOpcode;
1155
1156
1157 ////////////////////////////
1158 // EXEファイルのヘッダ情報
1159 ////////////////////////////
1160
1161 IMAGE_DOS_HEADER ImageDosHeader;
1162 ImageDosHeader.e_magic= 0x5A4D;
1163 ImageDosHeader.e_cblp= 0x0090;
1164 ImageDosHeader.e_cp= 0x0003;
1165 ImageDosHeader.e_crlc= 0;
1166 ImageDosHeader.e_cparhdr=4;
1167 ImageDosHeader.e_minalloc=0x0000;
1168 ImageDosHeader.e_maxalloc=0xFFFF;
1169 ImageDosHeader.e_ss= 0x0000;
1170 ImageDosHeader.e_sp= 0x00B8;
1171 ImageDosHeader.e_csum= 0x0000;
1172 ImageDosHeader.e_ip= 0x0000;
1173 ImageDosHeader.e_cs= 0x0000;
1174 ImageDosHeader.e_lfarlc=0x0040;
1175 ImageDosHeader.e_ovno= 0x0000;
1176 ImageDosHeader.e_res[0]=0;
1177 ImageDosHeader.e_res[1]=0;
1178 ImageDosHeader.e_res[2]=0;
1179 ImageDosHeader.e_res[3]=0;
1180 ImageDosHeader.e_oemid= 0x0000;
1181 ImageDosHeader.e_oeminfo=0x0000;
1182 ImageDosHeader.e_res2[0]=0;
1183 ImageDosHeader.e_res2[1]=0;
1184 ImageDosHeader.e_res2[2]=0;
1185 ImageDosHeader.e_res2[3]=0;
1186 ImageDosHeader.e_res2[4]=0;
1187 ImageDosHeader.e_res2[5]=0;
1188 ImageDosHeader.e_res2[6]=0;
1189 ImageDosHeader.e_res2[7]=0;
1190 ImageDosHeader.e_res2[8]=0;
1191 ImageDosHeader.e_res2[9]=0;
1192 ImageDosHeader.e_lfanew=0x0100; //PEヘッダの位置
1193
1194
1195 /////////////////////////////////////////////
1196 // PEヘッダ
1197 /////////////////////////////////////////////
1198
1199 IMAGE_NT_HEADERS ImagePeHdr;
1200 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1201
1202 //マシンタイプ
1203 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_I386;
1204
1205 ImagePeHdr.FileHeader.NumberOfSections= bUse_CodeSection+
1206 bUse_ExportSection+
1207 bUse_ImportSection+
1208 bUse_DataSection+
1209 bUse_RWSection+
1210 bUse_RSrcSection+
1211 bUse_RelocSection+
1212 bUse_DebugSection; //セクション数
1213 ImagePeHdr.FileHeader.TimeDateStamp= (DWORD)time(NULL);
1214 ImagePeHdr.FileHeader.PointerToSymbolTable= 0x00000000;
1215 ImagePeHdr.FileHeader.NumberOfSymbols= 0x00000000;
1216 ImagePeHdr.FileHeader.SizeOfOptionalHeader= IMAGE_SIZEOF_NT_OPTIONAL32_HEADER;
1217 if(bDll){
1218 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1219 IMAGE_FILE_32BIT_MACHINE|
1220 IMAGE_FILE_LINE_NUMS_STRIPPED|
1221 IMAGE_FILE_LOCAL_SYMS_STRIPPED|
1222 IMAGE_FILE_DLL;
1223 }
1224 else{
1225 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1226 IMAGE_FILE_32BIT_MACHINE|
1227 IMAGE_FILE_LINE_NUMS_STRIPPED|
1228 IMAGE_FILE_LOCAL_SYMS_STRIPPED;
1229 }
1230
1231 ImagePeHdr.OptionalHeader.Magic= 0x010B;
1232 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 4;
1233 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1234 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1235 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1236 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1237 if(bDll){
1238 if(DllMain_EntryPoint==-1)
1239 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1240 else
1241 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1242 }
1243 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1244 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1245 ImagePeHdr.OptionalHeader.BaseOfData= MemPos_DataSection; //.dataのアドレス
1246
1247 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1248 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1249 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1250 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1251 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1252 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1253 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1254 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1255 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1256 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1257 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1258 MemSize_CodeSection+
1259 MemSize_ExportSection+
1260 MemSize_ImportSection+
1261 MemSize_DataSection+
1262 MemSize_RWSection+
1263 MemSize_RSrcSection+
1264 MemSize_RelocSection+
1265 MemSize_DebugSection;//すべてのイメージサイズ
1266 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1267 ImagePeHdr.OptionalHeader.CheckSum= 0;
1268 extern unsigned short TypeOfSubSystem;
1269 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1270 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1271 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1272 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1273 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1274 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1275 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1276 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1277
1278 //データ ディクショナリ
1279 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1280 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1281 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress=MemPos_ImportSection;//インポートテーブル
1282 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=FileSize_ImportSection;
1283 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1284 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1285 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1286 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1287 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1288 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1289 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1290 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1291 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1292 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1293 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1294 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1295 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1296 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1297 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1298 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1299 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1300 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1301 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1302 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1303 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection+
1304 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1305 16*ImportDllNum+ //DLL名
1306 LookupSize+ //ルックアップテーブル
1307 HintSize; //ヒント名(関数名)テーブル
1308 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1309 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1310 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1311 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1312 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1313 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1314 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1315
1316
1317 //コードセクションヘッダ
1318 IMAGE_SECTION_HEADER CodeSectionHeader;
1319 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1320 lstrcpy((char *)CodeSectionHeader.Name,".text");
1321 CodeSectionHeader.Misc.VirtualSize= MemSize_CodeSection;
1322 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1323 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1324 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1325 CodeSectionHeader.PointerToRelocations= 0;
1326 CodeSectionHeader.PointerToLinenumbers= 0;
1327 CodeSectionHeader.NumberOfRelocations= 0;
1328 CodeSectionHeader.NumberOfLinenumbers= 0;
1329 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1330 IMAGE_SCN_MEM_READ|
1331 IMAGE_SCN_CNT_CODE;
1332
1333 //エクスポートセクションヘッダ
1334 IMAGE_SECTION_HEADER ExportSectionHeader;
1335 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1336 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1337 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1338 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1339 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1340 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1341 ExportSectionHeader.PointerToRelocations= 0;
1342 ExportSectionHeader.PointerToLinenumbers= 0;
1343 ExportSectionHeader.NumberOfRelocations= 0;
1344 ExportSectionHeader.NumberOfLinenumbers= 0;
1345 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1346 IMAGE_SCN_MEM_READ;
1347
1348 //インポートセクションヘッダ
1349 IMAGE_SECTION_HEADER ImportSectionHeader;
1350 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1351 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1352 ImportSectionHeader.Misc.VirtualSize= MemSize_ImportSection;
1353 ImportSectionHeader.VirtualAddress= MemPos_ImportSection; //開始アドレス
1354 ImportSectionHeader.SizeOfRawData= FileSize_ImportSection; //サイズ
1355 ImportSectionHeader.PointerToRawData= FilePos_ImportSection; //ファイル上の開始アドレス
1356 ImportSectionHeader.PointerToRelocations= 0;
1357 ImportSectionHeader.PointerToLinenumbers= 0;
1358 ImportSectionHeader.NumberOfRelocations= 0;
1359 ImportSectionHeader.NumberOfLinenumbers= 0;
1360 ImportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1361 IMAGE_SCN_MEM_READ;
1362
1363 //データセクションヘッダ
1364 IMAGE_SECTION_HEADER DataSectionHeader;
1365 memset((char *)DataSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1366 lstrcpy((char *)DataSectionHeader.Name,".sdata");
1367 DataSectionHeader.Misc.VirtualSize= MemSize_DataSection;
1368 DataSectionHeader.VirtualAddress= MemPos_DataSection;
1369 DataSectionHeader.SizeOfRawData= FileSize_DataSection;
1370 DataSectionHeader.PointerToRawData= FilePos_DataSection;
1371 DataSectionHeader.PointerToRelocations= 0;
1372 DataSectionHeader.PointerToLinenumbers= 0;
1373 DataSectionHeader.NumberOfRelocations= 0;
1374 DataSectionHeader.NumberOfLinenumbers= 0;
1375 DataSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1376 IMAGE_SCN_MEM_READ|
1377 IMAGE_SCN_MEM_WRITE;
1378
1379 //リライタブルセクションヘッダ
1380 IMAGE_SECTION_HEADER RWSectionHeader;
1381 memset((char *)RWSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1382 lstrcpy((char *)RWSectionHeader.Name,".data");
1383 RWSectionHeader.Misc.VirtualSize= AllInitGlobalVarSize+AllGlobalVarSize;
1384 RWSectionHeader.VirtualAddress= MemPos_RWSection;
1385 RWSectionHeader.SizeOfRawData= FileSize_RWSection;
1386 RWSectionHeader.PointerToRawData= FilePos_RWSection;
1387 RWSectionHeader.PointerToRelocations= 0;
1388 RWSectionHeader.PointerToLinenumbers= 0;
1389 RWSectionHeader.NumberOfRelocations= 0;
1390 RWSectionHeader.NumberOfLinenumbers= 0;
1391 RWSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1392 IMAGE_SCN_MEM_READ|
1393 IMAGE_SCN_MEM_WRITE;
1394
1395 //リソースセクションヘッダ
1396 IMAGE_SECTION_HEADER RSrcSectionHeader;
1397 memset((char *)RSrcSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1398 lstrcpy((char *)RSrcSectionHeader.Name,".rsrc");
1399 RSrcSectionHeader.Misc.VirtualSize= RSrcSectionSize;
1400 RSrcSectionHeader.VirtualAddress= MemPos_RSrcSection;
1401 RSrcSectionHeader.SizeOfRawData= FileSize_RSrcSection;
1402 RSrcSectionHeader.PointerToRawData= FilePos_RSrcSection;
1403 RSrcSectionHeader.PointerToRelocations= 0;
1404 RSrcSectionHeader.PointerToLinenumbers= 0;
1405 RSrcSectionHeader.NumberOfRelocations= 0;
1406 RSrcSectionHeader.NumberOfLinenumbers= 0;
1407 RSrcSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1408 IMAGE_SCN_MEM_READ;
1409
1410 //リロケーションセクションヘッダ
1411 IMAGE_SECTION_HEADER RelocSectionHeader;
1412 memset((char *)RelocSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1413 lstrcpy((char *)RelocSectionHeader.Name,".reloc");
1414 RelocSectionHeader.Misc.VirtualSize= pobj_Reloc->length;
1415 RelocSectionHeader.VirtualAddress= MemPos_RelocSection; //開始アドレス
1416 RelocSectionHeader.SizeOfRawData= FileSize_RelocSection; //サイズ
1417 RelocSectionHeader.PointerToRawData= FilePos_RelocSection; //ファイル上の開始アドレス
1418 RelocSectionHeader.PointerToRelocations= 0;
1419 RelocSectionHeader.PointerToLinenumbers= 0;
1420 RelocSectionHeader.NumberOfRelocations= 0;
1421 RelocSectionHeader.NumberOfLinenumbers= 0;
1422 RelocSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1423 IMAGE_SCN_MEM_DISCARDABLE|
1424 IMAGE_SCN_MEM_READ;
1425
1426 //デバッグセクションヘッダ
1427 IMAGE_SECTION_HEADER DebugSectionHeader;
1428 memset((char *)DebugSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1429 lstrcpy((char *)DebugSectionHeader.Name,".debug");
1430 DebugSectionHeader.Misc.VirtualSize= pobj_DebugSection->length;
1431 DebugSectionHeader.VirtualAddress= MemPos_DebugSection; //開始アドレス
1432 DebugSectionHeader.SizeOfRawData= FileSize_DebugSection; //サイズ
1433 DebugSectionHeader.PointerToRawData= FilePos_DebugSection; //ファイル上の開始アドレス
1434 DebugSectionHeader.PointerToRelocations= 0;
1435 DebugSectionHeader.PointerToLinenumbers= 0;
1436 DebugSectionHeader.NumberOfRelocations= 0;
1437 DebugSectionHeader.NumberOfLinenumbers= 0;
1438 DebugSectionHeader.Characteristics= IMAGE_SCN_MEM_DISCARDABLE|
1439 IMAGE_SCN_MEM_READ;
1440
1441
1442 hFile=CreateFile(OutputFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
1443 if(hFile==INVALID_HANDLE_VALUE){
1444 SetError(53,OutputFileName,-1);
1445 goto EndWriteOpcode;
1446 }
1447
1448 //ヘッダ
1449 WriteFile(hFile,(void *)&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),(DWORD *)&i2,NULL);
1450 i=i2;
1451
1452 //Dosスタブ
1453 WriteFile(hFile,DosStubBuffer,DosStubSize,(DWORD *)&i2,NULL);
1454 i+=i2;
1455
1456 //0x0100までNULLを並べる
1457 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,0x0100-i);
1458 WriteFile(hFile,temp2,0x0100-i,(DWORD *)&i2,NULL);
1459 HeapDefaultFree(temp2);
1460 i+=i2;
1461
1462 //PEヘッダ
1463 WriteFile(hFile,&ImagePeHdr,sizeof(IMAGE_NT_HEADERS),(DWORD *)&i2,NULL);
1464 i+=i2;
1465
1466 //コード セクション ヘッダ
1467 WriteFile(hFile,&CodeSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1468 i+=i2;
1469
1470 if(bUse_ExportSection){
1471 //エクスポート セクション ヘッダ
1472 WriteFile(hFile,&ExportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1473 i+=i2;
1474 }
1475 if(bUse_ImportSection){
1476 //インポート セクション ヘッダ
1477 WriteFile(hFile,&ImportSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1478 i+=i2;
1479 }
1480 if(bUse_DataSection){
1481 //データ セクション ヘッダ
1482 WriteFile(hFile,&DataSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1483 i+=i2;
1484 }
1485 if(bUse_RWSection){
1486 //リライタブルセクションヘッダ
1487 WriteFile(hFile,&RWSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1488 i+=i2;
1489 }
1490 if(bUse_RSrcSection){
1491 //リソースセクションヘッダ
1492 WriteFile(hFile,&RSrcSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1493 i+=i2;
1494 }
1495 if(bUse_RelocSection){
1496 //リロケーションセクションヘッダ
1497 WriteFile(hFile,&RelocSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1498 i+=i2;
1499 }
1500 if(bUse_DebugSection){
1501 //デバッグセクションヘッダ
1502 WriteFile(hFile,&DebugSectionHeader,sizeof(IMAGE_SECTION_HEADER),(DWORD *)&i2,NULL);
1503 i+=i2;
1504 }
1505
1506 //EXE_HEADER_SIZEまでNULLを並べる
1507 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,EXE_HEADER_SIZE-i);
1508 WriteFile(hFile,temp2,EXE_HEADER_SIZE-i,(DWORD *)&i2,NULL);
1509 HeapDefaultFree(temp2);
1510 i+=i2;
1511
1512 //コード
1513 WriteFile(hFile,OpBuffer,obp,(DWORD *)&i2,NULL);
1514 i+=i2;
1515
1516 //FilePos_ExportSectionまでNULLを並べる
1517 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ExportSection-i);
1518 WriteFile(hFile,temp2,FilePos_ExportSection-i,(DWORD *)&i2,NULL);
1519 HeapDefaultFree(temp2);
1520 i+=i2;
1521
1522 if(bUse_ExportSection){
1523 //エクスポート ディレクトリ テーブル
1524 WriteFile(hFile,&ImageExportDirectory,sizeof(IMAGE_EXPORT_DIRECTORY),(DWORD *)&i2,NULL);
1525 i+=i2;
1526
1527 //エクスポート アドレス テーブル
1528 WriteFile(hFile,lpdwExportAddressTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1529 i+=i2;
1530
1531 //エクスポート名ポインタ テーブル
1532 WriteFile(hFile,lpdwExportNamePointerTable,ExportNum*sizeof(DWORD),(DWORD *)&i2,NULL);
1533 i+=i2;
1534
1535 //エクスポート序数テーブル
1536 WriteFile(hFile,lpwExportOrdinalTable,ExportNum*sizeof(WORD),(DWORD *)&i2,NULL);
1537 i+=i2;
1538
1539 //シンボル名
1540 WriteFile(hFile,lpExportNames,ExportNamesLength,(DWORD *)&i2,NULL);
1541 i+=i2;
1542 }
1543
1544 //FilePos_ImportSectionまでNULLを並べる
1545 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_ImportSection-i);
1546 WriteFile(hFile,temp2,FilePos_ImportSection-i,(DWORD *)&i2,NULL);
1547 HeapDefaultFree(temp2);
1548 i+=i2;
1549
1550 if(bUse_ImportSection){
1551 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1552 for(i3=0;i3<(ImportDllNum+1);i3++){
1553 WriteFile(hFile,&pImportTable[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1554 i+=i2;
1555 }
1556
1557 //DLL名
1558 for(i3=0;i3<ImportDllNum;i3++){
1559 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1560 i+=i2;
1561 }
1562
1563 //ルックアップ テーブル
1564 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1565 i+=i2;
1566
1567 //ヒント テーブル
1568 WriteFile(hFile,pHintTable,HintSize,(DWORD *)&i2,NULL);
1569 i+=i2;
1570
1571 //インポート アドレス テーブル
1572 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1573 i+=i2;
1574 }
1575
1576 //FilePos_DataSectionまでNULLを並べる
1577 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_DataSection-i);
1578 WriteFile(hFile,temp2,FilePos_DataSection-i,(DWORD *)&i2,NULL);
1579 HeapDefaultFree(temp2);
1580 i+=i2;
1581
1582 if(bUse_DataSection){
1583 //データ テーブル
1584 WriteFile(hFile,dataTable.GetPtr(),dataTable.GetSize(),(DWORD *)&i2,NULL);
1585 i+=i2;
1586 }
1587
1588 //FilePos_RWSectionまでNULLを並べる
1589 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RWSection-i);
1590 WriteFile(hFile,temp2,FilePos_RWSection-i,(DWORD *)&i2,NULL);
1591 HeapDefaultFree(temp2);
1592 i+=i2;
1593
1594 if(bUse_RWSection){
1595 //リライタブル データ テーブル(グローバル変数の初期バッファ)
1596 initGlobalBuf=(BYTE *)HeapReAlloc(hHeap,
1597 HEAP_ZERO_MEMORY,
1598 initGlobalBuf,
1599 FileSize_RWSection);
1600 WriteFile(hFile,initGlobalBuf,FileSize_RWSection,(DWORD *)&i2,NULL);
1601 i+=i2;
1602 }
1603
1604 //FilePos_RSrcSectionまでNULLを並べる
1605 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1606 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1607 HeapDefaultFree(temp2);
1608 i+=i2;
1609
1610 if(bUse_RSrcSection){
1611 //リソースバッファ
1612 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1613 i+=i2;
1614 }
1615
1616 //FilePos_RelocSectionまでNULLを並べる
1617 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1618 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1619 HeapDefaultFree(temp2);
1620 i+=i2;
1621
1622 if(bUse_RelocSection){
1623 //リロケーション情報
1624 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1625 i+=i2;
1626 }
1627
1628 //ファイルアラインメントを考慮
1629 if(i%FILE_ALIGNMENT){
1630 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1631 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1632 HeapDefaultFree(temp2);
1633 i+=i2;
1634 }
1635
1636 if(bUse_DebugSection){
1637 //デバッグセクション
1638 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1639 i+=i2;
1640 }
1641
1642 //ファイルアラインメントを考慮
1643 if(i%FILE_ALIGNMENT){
1644 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1645 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1646 HeapDefaultFree(temp2);
1647 i+=i2;
1648 }
1649
1650 //書き込み終了
1651 CloseHandle(hFile);
1652
1653
1654EndWriteOpcode:
1655
1656 //Dosスタブ用のメモリを解放
1657 HeapDefaultFree(DosStubBuffer);
1658
1659 //エクスポート テーブル情報を解放
1660 HeapDefaultFree(lpdwExportAddressTable);
1661 HeapDefaultFree(lpdwExportNamePointerTable);
1662 HeapDefaultFree(lpwExportOrdinalTable);
1663
1664 //インポートDLL情報を解放
1665 HeapDefaultFree(pImportTable);
1666 for(i=0;i<ImportDllNum;i++)
1667 HeapDefaultFree(ppDllNames[i]);
1668 HeapDefaultFree(ppDllNames);
1669
1670 //ルックアップテーブルに関する情報を解放
1671 HeapDefaultFree(pLookupTable);
1672
1673 //ヒントテーブルに関する情報を解放
1674 HeapDefaultFree(pHintTable);
1675
1676 //グローバル変数の初期バッファを解放
1677 HeapDefaultFree(initGlobalBuf);
1678
1679 //コードバッファを解放
1680 HeapDefaultFree(OpBuffer);
1681 OpBuffer=0;
1682
1683 //リソースセクションバッファを解放
1684 HeapDefaultFree(RSrcSectionBuffer);
1685
1686 //デバッグセクションを開放
1687 delete pobj_DebugSection;
1688
1689 //リロケーション情報を解放
1690 delete pobj_Reloc;
1691
1692 //列挙体に関する情報の破棄
1693 CEnumParent::DestroyEnum();
1694
1695 //クラスに関するメモリを解放
1696 delete pobj_DBClass;
1697 pobj_DBClass=0;
1698}
Note: See TracBrowser for help on using the repository browser.