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

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