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

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

コンパイラ組み込みテンプレートエンジンを実装。
静的リンクライブラリ、デバッグ情報の内部形式をテキストからバイナリに変更した。

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