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

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

・WithInfo周りをリファクタリング。

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