source: dev/trunk/ab5.0/abdev/compiler_x64/MakePeHdr.cpp@ 749

Last change on this file since 749 was 749, checked in by dai, 16 years ago

「フォルダを開く」コマンドで、ファイル選択を可能にした。

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