source: dev/trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp@ 545

Last change on this file since 545 was 545, checked in by dai_9181, 16 years ago

ビルドメッセージを文言変更

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