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

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

BoostSerializationSupportのクラステンプレートインスタンスを明示的に生成するようにした(コンパイル時間の短縮)

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