source: dev/trunk/abdev/BasicCompiler32/MakePeHdr.cpp@ 195

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