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

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