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

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

[684]を64bit版にマージ。

File size: 56.7 KB
Line 
1#include "stdafx.h"
2
3#include "../BasicCompiler_Common/common.h"
4#include "../BasicCompiler_Common/DebugSection.h"
5#include "Opcode.h"
6
7
8////////////////////////////
9// 特殊関数の構造体ポインタ
10////////////////////////////
11
12// グローバル関数、静的メソッド
13const UserProc
14 *pSub_System_StartupProgram,
15 *pSub_System_GlobalArea,
16 *pSub_DebugSys_StartProc,
17 *pSub_DebugSys_EndProc,
18 *pSub_DebugSys_SaveContext,
19 *pSub_System_GetEip,
20 *pSub_System_InitDllGlobalVariables,
21 *pSub_System_InitStaticLocalVariables,
22 *pSub_System_Call_Destructor_of_GlobalObject,
23 *pSub_System_End,
24 *pSub_pow,
25 *pSub_calloc,
26 *pSub_realloc,
27 *pSub_free,
28 *pSub_System_GC_malloc_ForObject,
29 *pSub_System_GC_malloc_ForObjectPtr,
30 *pSub_System_GC_free_for_SweepingDelete,
31 *pSub_System_AddNeedFreeTempStructure,
32 *pSub_System_FreeTempStructure,
33 *pSubStaticMethod_System_TypeBase_InitializeUserTypes,
34 *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType;
35
36// 動的メソッド
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 extern SourceLines oldSourceLines;
228 oldSourceLines.clear();
229
230
231 ///////////////////////////
232 // 最低限必要な関数を取得
233 ///////////////////////////
234 cp=-1;
235
236 if(pSub_System_StartupProgram=GetSubHash("_System_StartupProgram",1))
237 pSub_System_StartupProgram->Using();
238
239 if(pSub_System_GlobalArea=GetSubHash(compiler.globalAreaProcName.c_str(),1))
240 {
241 pSub_System_GlobalArea->Using();
242 pSub_System_GlobalArea->ThisIsAutoGenerationProc();
243 }
244
245 if(pSub_DebugSys_StartProc=GetSubHash("_DebugSys_StartProc",1))
246 pSub_DebugSys_StartProc->Using();
247
248 if(pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc",1))
249 pSub_DebugSys_EndProc->Using();
250
251 if(pSub_DebugSys_SaveContext=GetSubHash("_DebugSys_SaveContext",1))
252 pSub_DebugSys_SaveContext->Using();
253
254 if(pSub_System_GetEip=GetSubHash("_System_GetEip",1)){
255 pSub_System_GetEip->Using();
256 pSub_System_GetEip->ThisIsAutoGenerationSystemProc();
257 }
258
259 if(pSub_System_InitDllGlobalVariables=GetSubHash("_System_InitDllGlobalVariables",1)){
260 pSub_System_InitDllGlobalVariables->Using();
261 pSub_System_InitDllGlobalVariables->ThisIsAutoGenerationSystemProc();
262 }
263
264 if(pSub_System_InitStaticLocalVariables=GetSubHash("_System_InitStaticLocalVariables",1)){
265 pSub_System_InitStaticLocalVariables->Using();
266 pSub_System_InitStaticLocalVariables->ThisIsAutoGenerationSystemProc();
267 }
268
269 if(pSub_System_Call_Destructor_of_GlobalObject=GetSubHash("_System_Call_Destructor_of_GlobalObject",1)){
270 pSub_System_Call_Destructor_of_GlobalObject->Using();
271 pSub_System_Call_Destructor_of_GlobalObject->ThisIsAutoGenerationSystemProc();
272 }
273
274 if(pSub_System_End=GetSubHash("_System_End",1)){
275 pSub_System_End->Using();
276 }
277
278 if(pSub_pow=GetSubHash("pow",1))
279 pSub_pow->Using();
280
281 if(pSub_calloc=GetSubHash("calloc",1))
282 pSub_calloc->Using();
283
284 if(pSub_realloc=GetSubHash("realloc",1))
285 pSub_realloc->Using();
286
287 if(pSub_free=GetSubHash("free",1))
288 pSub_free->Using();
289
290 if( pSub_System_GC_malloc_ForObject = GetSubHash( "_System_GC_malloc_ForObject",1 ) )
291 pSub_System_GC_malloc_ForObject->Using();
292
293 if( pSub_System_GC_malloc_ForObjectPtr = GetSubHash( "_System_GC_malloc_ForObjectPtr",1 ) )
294 pSub_System_GC_malloc_ForObjectPtr->Using();
295
296 if( pSub_System_GC_free_for_SweepingDelete = GetSubHash( "_System_GC_free_for_SweepingDelete",1 ) )
297 pSub_System_GC_free_for_SweepingDelete->Using();
298
299 if( pSub_System_AddNeedFreeTempStructure = GetSubHash( "_System_AddNeedFreeTempStructure",1 ) )
300 {
301 pSub_System_AddNeedFreeTempStructure->Using();
302 }
303 if( pSub_System_FreeTempStructure = GetSubHash( "_System_FreeTempStructure",1 ) )
304 {
305 pSub_System_FreeTempStructure->Using();
306 }
307
308 if( pSubStaticMethod_System_TypeBase_InitializeUserTypes = GetSubHash( "ActiveBasic.Core._System_TypeBase.InitializeUserTypes",1 ) ){
309 pSubStaticMethod_System_TypeBase_InitializeUserTypes->Using();
310 pSubStaticMethod_System_TypeBase_InitializeUserTypes->ThisIsAutoGenerationProc();
311 }
312
313 if( pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType = GetSubHash( "ActiveBasic.Core._System_TypeBase.InitializeUserTypesForBaseType",1 ) ){
314 pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType->Using();
315 pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType->ThisIsAutoGenerationProc();
316 }
317
318 if( pUserProc_System_CGarbageCollection_RegisterGlobalRoots = GetClassMethod( "_System_CGarbageCollection", "RegisterGlobalRoots" ) ){
319 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->Using();
320 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->ThisIsAutoGenerationProc();
321 }
322
323 //リロケーション情報
324 pobj_Reloc=new CReloc();
325
326 //レジスタのブロッキングを管理するためのオブジェクトを生成
327 pobj_BlockReg=new CBlockReg;
328
329 //レキシカルスコープ情報を初期化
330 compiler.codeGenerator.lexicalScopes.Init(0);
331
332 // 名前空間が初期化されているかをチェック
333 if( compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().size() )
334 {
335 compiler.errorMessenger.OutputFatalError();
336 }
337
338
339 /////////////////////////////////////////////////////////////////
340 // デバッグコンパイル用のログを生成する
341 /////////////////////////////////////////////////////////////////
342#ifdef _DEBUG
343 {
344 std::ofstream ofs( ( Jenga::Common::Environment::GetAppDir() + "\\middle_code.txt" ).c_str() );
345 ofs << basbuf << std::endl;
346 ofs.close();
347 }
348#endif
349
350
351 //"コンパイル中..."
352 compiler.messenger.Output(STRING_COMPILE_COMPILING);
353
354
355 //////////////////////
356 // グローバル実行領域
357 //////////////////////
358
359 cp=-1;
360 compiler.StartGlobalAreaCompile();
361
362 if( !compiler.IsDll() )
363 {
364 //ラベル管理オブジェクトを初期化
365 compiler.codeGenerator.gotoLabels.clear();
366
367 //Gotoラベルスケジュール
368 compiler.codeGenerator.gotoLabelSchedules.clear();
369
370 //With情報を初期化
371 extern WithInfos withInfos;
372 withInfos.clear();
373
374 //Continueアドレスを初期化
375 compiler.codeGenerator.ClearContinueArea();
376
377 //スタックフレーム管理用クラスを選択
378 pobj_sf=new StackFrame();
379
380 // コード生成対象を選択
381 compiler.codeGenerator.Select( compiler.GetObjectModule().globalNativeCode );
382
383 trace_for_sourcecodestep( "★★★ グローバル領域のコンパイルを開始" );
384
385 if( compiler.IsCore() )
386 {
387 //sub rsp,スタックフレームサイズ
388 compiler.codeGenerator.op_sub_RV( sizeof(_int64), REG_RSP, 0x108 );
389
390 if( compiler.IsDebug() )
391 {
392 //デバッグ用の変数を定義
393 DebugVariable();
394 }
395
396 //GC用の変数を定義
397 InitGCVariables();
398
399 //_System_StartupProgramの呼び出し
400 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
401 }
402
403 // _System_GlobalArea の呼び出し
404 compiler.codeGenerator.op_call( pSub_System_GlobalArea );
405
406 if( !compiler.IsSll() )
407 {
408 ///////////////////////////////////////
409 // グローバル文字列変数の解放処理
410 ///////////////////////////////////////
411
412 //call _System_End
413 extern const UserProc *pSub_System_End;
414 compiler.codeGenerator.op_call(pSub_System_End);
415
416
417 //add rsp,スタックフレームサイズ
418 compiler.codeGenerator.op_add_RV( REG_RSP, 0x108 );
419
420 //xor rax,rax(raxを0に初期化する)
421 compiler.codeGenerator.op_zero_reg(REG_RAX);
422
423 //ret
424 compiler.codeGenerator.op_ret();
425 }
426
427 //スタックフレームスケジュールを実行
428 pobj_sf->RunningSchedule( 0x100 );
429 delete pobj_sf;
430 pobj_sf=0;
431
432 //グローバル実行領域のコードサイズ
433 extern int GlobalOpBufferSize;
434 GlobalOpBufferSize = compiler.linker.GetNativeCode().GetSize();
435
436 // 名前空間が正しく閉じられているかをチェック
437 if( compiler.GetNamespaceSupporter().GetLivingNamespaceScopes().size() ){
438 compiler.errorMessenger.Output(63,NULL,-1);
439 }
440
441 }
442 else{
443 ////////////////
444 // DLLの場合
445 ////////////////
446
447
448 }
449
450
451 StepCompileProgress();
452
453
454 /////////////////////
455 // ローカル実行領域
456 /////////////////////
457
458 //プロシージャをコンパイル開始
459 cp=0;
460 CompileLocal();
461
462 // 終了
463 ///////////////////////
464
465 StepCompileProgress();
466
467
468 //レジスタのブロッキングを管理するためのオブジェクトを破棄
469 delete pobj_BlockReg;
470
471 //ネイティブコード生成はここまで
472 //////////////////////////////////////////////////////////
473
474
475
476 trace( "コード生成が終了しました。" );
477
478
479 if( compiler.IsSll() )
480 {
481 // 静的リンクライブラリ
482
483 // 外部参照をlibから取り除く
484 ActiveBasic::Common::Environment::SetRemoveExternalMark( true );
485
486 // 格納先ディレクトリを作る
487 Jenga::Common::Path path( program.GetOutputFilePath() );
488 Jenga::Common::Directory dir( path.GetDriveName() + path.GetDirName(), true );
489
490 // 書き込む
491 if( !compiler.GetObjectModule().Write( program.GetOutputFilePath() ) )
492 {
493 MessageBox(0,"XML書き込みに失敗","test",0);
494 }
495 return;
496 }
497
498 if( !compiler.errorMessenger.HasError() )
499 {
500 compiler.messenger.Output( "リンク中..." );
501 }
502
503
504 compiler.linker.Link( compiler.GetObjectModule() );
505
506 extern SourceLines oldSourceLines;
507 oldSourceLines = compiler.linker.GetNativeCode().GetSourceLines();
508
509
510 /////////////////////////////////////////////////////////////////
511 // vtblの構築
512 /////////////////////////////////////////////////////////////////
513
514 ActiveBasic::Compiler::VtblGenerator::GenerateVTablesForAllClasses(
515 compiler.GetObjectModule().meta.GetClasses()
516 );
517
518
519
520 ////////////////////////////////
521 // ここで一旦ログを取る
522 ////////////////////////////////
523 Diagnose();
524
525
526
527 ////////////////////////////////
528 // 使用するDLL関数のチェック
529 ////////////////////////////////
530 BOOST_FOREACH( const Schedule &schedule, compiler.linker.GetNativeCode().GetSchedules() )
531 {
532 if( schedule.GetType() == Schedule::DllProc )
533 {
534 schedule.GetDllProc().Using();
535 }
536 }
537 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
538 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
539 {
540 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
541
542 if( !pDllProc->IsUsing() ){
543 continue;
544 }
545/*
546 //エラーチェック
547 HINSTANCE hLib;
548 hLib=LoadLibrary( pDllProc->GetDllFileName().c_str() );
549 if(!hLib){
550 char temp2[MAX_PATH],temp3[MAX_PATH];
551 _splitpath(program.GetOutputFilePath().c_str(),temp2,temp3,NULL,NULL);
552 lstrcat(temp2,temp3);
553 lstrcpy(temp3,pDllProc->GetDllFileName().c_str());
554 GetFullPath(temp3,temp2);
555 hLib=LoadLibrary(temp3);
556
557 if(!hLib){
558 compiler.errorMessenger.Output(-106,pDllProc->GetDllFileName().c_str(),pDllProc->GetSourceCodePosition().GetPos());
559 }
560 }
561
562 if(hLib){
563 if(!GetProcAddress(hLib,pDllProc->GetAlias().c_str())){
564 FreeLibrary(hLib);
565 compiler.errorMessenger.Output(-107,pDllProc->GetAlias(),pDllProc->GetSourceCodePosition().GetPos());
566 }
567 FreeLibrary(hLib);
568 }
569*/
570 }
571
572
573
574 /////////////////////////////
575 // リソースデータを読み込む
576 /////////////////////////////
577 extern char ResourceFileName[MAX_PATH];
578 compiler.resourceManager.Load( ResourceFileName );
579
580
581 //////////////////////////////
582 // エクスポート情報(DLLのみ)
583 //////////////////////////////
584 IMAGE_EXPORT_DIRECTORY ImageExportDirectory;
585 DWORD *lpdwExportAddressTable;
586 DWORD *lpdwExportNamePointerTable;
587 WORD *lpwExportOrdinalTable;
588 char lpExportNames[8192];
589 int ExportNamesLength;
590 int ExportNum;
591 int DllMain_EntryPoint;
592
593 DllMain_EntryPoint=-1;
594 ExportNum=0;
595 ExportNamesLength=0;
596 lpdwExportAddressTable=(DWORD *)HeapAlloc(hHeap,0,1);
597 lpdwExportNamePointerTable=(DWORD *)HeapAlloc(hHeap,0,1);
598 lpwExportOrdinalTable=(WORD *)HeapAlloc(hHeap,0,1);
599
600 if(bUse_ExportSection){
601 _splitpath(program.GetOutputFilePath().c_str(),NULL,NULL,lpExportNames,temporary);
602 lstrcat(lpExportNames,temporary);
603 ExportNamesLength=lstrlen(lpExportNames)+1;
604
605 while(1)
606 {
607 UserProc *pUserProc = NULL;
608
609 //辞書順にサーチ
610 temporary[0]=0;
611 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
612 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
613 {
614 UserProc *pTempUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
615 if(pTempUserProc->IsExport()){
616 if(temporary[0]=='\0'){
617 lstrcpy(temporary,pTempUserProc->GetName().c_str());
618 pUserProc = pTempUserProc;
619 }
620 else{
621 i3=lstrlen(temporary);
622 i4=(int)pTempUserProc->GetName().size();
623 if(i3<i4) i3=i4;
624 if(memcmp(temporary,pTempUserProc->GetName().c_str(),i3)>0){
625 lstrcpy(temporary,pTempUserProc->GetName().c_str());
626 pUserProc = pTempUserProc;
627 }
628 }
629 }
630 }
631 if( pUserProc == NULL )
632 {
633 break;
634 }
635
636 pUserProc->ExportOff();
637
638 if( pUserProc->GetName() == "DllMain" ){
639 DllMain_EntryPoint = pUserProc->GetBeginOpAddress();
640 }
641
642 lpdwExportAddressTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportAddressTable,(ExportNum+1)*sizeof(DWORD));
643 lpdwExportAddressTable[ExportNum] = pUserProc->GetBeginOpAddress();
644
645 lpdwExportNamePointerTable=(DWORD *)HeapReAlloc(hHeap,0,lpdwExportNamePointerTable,(ExportNum+1)*sizeof(DWORD));
646 lpdwExportNamePointerTable[ExportNum]=ExportNamesLength;
647
648 lpwExportOrdinalTable=(WORD *)HeapReAlloc(hHeap,0,lpwExportOrdinalTable,(ExportNum+1)*sizeof(WORD));
649 lpwExportOrdinalTable[ExportNum]=ExportNum;
650
651 lstrcpy(lpExportNames+ExportNamesLength,pUserProc->GetName().c_str());
652 ExportNamesLength+=lstrlen(lpExportNames+ExportNamesLength)+1;
653
654 ExportNum++;
655 }
656 for(i=0;i<ExportNum;i++){
657 lpdwExportNamePointerTable[i]+=
658 sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
659 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
660 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
661 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
662 }
663
664 ImageExportDirectory.Characteristics=0;
665 ImageExportDirectory.TimeDateStamp=(DWORD)time(NULL);
666 ImageExportDirectory.MajorVersion=0;
667 ImageExportDirectory.MinorVersion=0;
668 ImageExportDirectory.Name=sizeof(IMAGE_EXPORT_DIRECTORY)+
669 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
670 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
671 ExportNum*sizeof(WORD); //エクスポート序数テーブルを飛び越す
672 ImageExportDirectory.Base=1;
673 ImageExportDirectory.NumberOfFunctions=ExportNum;
674 ImageExportDirectory.NumberOfNames=ExportNum;
675 ImageExportDirectory.AddressOfFunctions=sizeof(IMAGE_EXPORT_DIRECTORY);
676 ImageExportDirectory.AddressOfNames=ImageExportDirectory.AddressOfFunctions+ExportNum*sizeof(DWORD);
677 ImageExportDirectory.AddressOfNameOrdinals=ImageExportDirectory.AddressOfNames+ExportNum*sizeof(DWORD);
678 }
679
680
681 /////////////////////
682 //インポートDLL情報
683 /////////////////////
684
685 /*
686 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
687 ルックアップ テーブル(ヒントを示すRVA)
688 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
689 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
690 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
691 ...
692 Dll名1[size=0x10]
693 Dll名2[size=0x10]
694 Dll名3[size=0x10]
695 ...
696 ヒントテーブル(関数名を羅列したもの)
697 */
698
699 /*
700 IMAGE_IMPORT_DESCRIPTOR1[size=0x14] インポートヘッダ
701 IMAGE_IMPORT_DESCRIPTOR2[size=0x14]
702 IMAGE_IMPORT_DESCRIPTOR3[size=0x14]
703 ...
704 Dll名1[size=0x10]
705 Dll名2[size=0x10]
706 Dll名3[size=0x10]
707 ...
708 ルックアップ テーブル(ヒントを示すRVA)
709 ヒントテーブル
710 インポート アドレス テーブル(ルックアップと同じ内容だが、プログラム実行時に実行アドレスに書き換えられる)
711 */
712
713 char **ppDllNames=(char **)HeapAlloc(hHeap,0,1);
714
715 int ImportDllNum=0;
716
717 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
718 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
719 {
720 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
721
722 if( !pDllProc->IsUsing() ){
723 continue;
724 }
725
726 if( pDllProc->GetDllFileName().size() > 16 ){
727 compiler.errorMessenger.Output(7,NULL,cp);
728 break;
729 }
730 for(i2=0;i2<ImportDllNum;i2++){
731 if( pDllProc->GetDllFileName() == ppDllNames[i2] ){
732 break;
733 }
734 }
735 if(i2==ImportDllNum){
736 ppDllNames=(char **)HeapReAlloc(hHeap,0,ppDllNames,(ImportDllNum+1)*sizeof(char **));
737 ppDllNames[ImportDllNum]=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,16);
738 lstrcpy(ppDllNames[ImportDllNum],pDllProc->GetDllFileName().c_str());
739 ImportDllNum++;
740 }
741 }
742
743 //IMAGE_IMPORT_DESCRIPTOR、及びルックアップ テーブル サイズの計算
744 IMAGE_IMPORT_DESCRIPTOR *pImportDescriptor;
745 int LookupSize;
746 LookupSize=0;
747 pImportDescriptor=(IMAGE_IMPORT_DESCRIPTOR *)HeapAlloc(hHeap,0,(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR));
748 for(i=0;i<ImportDllNum;i++){
749 //IMAGE_IMPORT_DESCRIPTOR
750 pImportDescriptor[i].OriginalFirstThunk=LookupSize; //※すぐ下で再計算
751 pImportDescriptor[i].TimeDateStamp=0;
752 pImportDescriptor[i].ForwarderChain=0;
753 pImportDescriptor[i].Name=i*0x10; //※すぐ下で再計算
754
755 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
756 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
757 {
758 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
759
760 if( !pDllProc->IsUsing() ){
761 continue;
762 }
763
764 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
765 //ルックアップデータのサイズを追加
766 LookupSize+=sizeof(_int64);
767 }
768 }
769 LookupSize+=sizeof(_int64); //NULL用
770 }
771 memset(&pImportDescriptor[i],0,sizeof(IMAGE_IMPORT_DESCRIPTOR));
772 for(i=0;i<ImportDllNum;i++){
773 //インポートアドレステーブル分の差分を追加
774 pImportDescriptor[i].OriginalFirstThunk+=LookupSize;
775
776 //DLL名への差分を追加
777 pImportDescriptor[i].Name+=
778 LookupSize+ //インポート アドレス テーブル
779 LookupSize+ //ルックアップテーブル
780 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR); //IMAGE_IMPORT_DESCRIPTOR
781 }
782
783 //ルックアップ テーブル、ヒント テーブル
784 unsigned _int64 *pLookupTable;
785 pLookupTable=(unsigned _int64 *)HeapAlloc(hHeap,0,LookupSize*sizeof(_int64)+1);
786 char *pHintTable;
787 int HintSize,HintAllocSize;
788 HintSize=0;
789 HintAllocSize=128*2;
790 pHintTable=(char *)HeapAlloc(hHeap,0,HintAllocSize);
791 for(i=0,i5=0;i<ImportDllNum;i++){
792 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
793 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
794 {
795 DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
796
797 if( !pDllProc->IsUsing() ){
798 continue;
799 }
800
801 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
802 //ルックアップの位置をセット(後にインポート アドレス テーブルにセットしなおされる)
803 pDllProc->SetLookupAddress( i5*sizeof(_int64) );
804
805 //ルックアップ テーブルの値をセット
806 pLookupTable[i5++]=
807 LookupSize+ //インポート アドレス テーブル
808 LookupSize+ //ルックアップテーブル
809 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+ //IMAGE_IMPORT_DESCRIPTOR
810 ImportDllNum*0x10+ //DLL名の羅列
811 HintSize; //ヒント名へのオフセット
812
813 //ヒント テーブル
814 pHintTable[HintSize++]=0;
815 pHintTable[HintSize++]=0;
816 lstrcpy(pHintTable+HintSize,pDllProc->GetAlias().c_str());
817 i4=(int)pDllProc->GetAlias().size();
818 HintSize+=i4+1;
819 if(i4%2==0) pHintTable[HintSize++]=0;
820
821 if(HintAllocSize<HintSize+128){
822 HintAllocSize+=128;
823 pHintTable=(char *)HeapReAlloc(hHeap,0,pHintTable,HintAllocSize);
824 }
825 }
826 }
827 pLookupTable[i5++]=0;
828 }
829
830
831 if( compiler.IsDll() ){
832 //DLLの場合はリロケーション情報を仮生成
833 //※正式な生成は各セクションのメモリ上のサイズが決定してから再度行う。
834 pobj_Reloc->ResetRelocBuffer();
835 }
836
837
838
839 //グローバル変数情報を扱う構造体も初期バッファの有無による配置を行う
840 //(デバッグ情報で利用される)
841 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
842 if(pVar->GetOffsetAddress()&0x80000000){
843 pVar->SetOffsetAddress(
844 (pVar->GetOffsetAddress()&0x7FFFFFFF)
845 + compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
846 );
847 }
848 }
849
850
851
852 ////////////////////////////////////
853 // デバッグセクションを生成
854 ////////////////////////////////////
855
856 //デバッグセクションを生成
857 CDebugSection *pobj_DebugSection;
858 pobj_DebugSection=new CDebugSection();
859 if( compiler.IsDebug() && !compiler.errorMessenger.HasError() )
860 {
861 compiler.messenger.Output( "デバッグ情報を生成しています。" );
862
863 pobj_DebugSection->make();
864
865 compiler.messenger.Output( "デバッグ情報の生成が完了しました。" );
866 }
867
868
869
870 /////////////////////////////////////
871 // 各セクションの位置とサイズを計算
872 /////////////////////////////////////
873
874 //コードセッションのファイル上のサイズ
875 if(compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT)
876 {
877 FileSize_CodeSection =
878 compiler.linker.GetNativeCode().GetSize()
879 + (FILE_ALIGNMENT-compiler.linker.GetNativeCode().GetSize()%FILE_ALIGNMENT);
880 }
881 else
882 {
883 FileSize_CodeSection = compiler.linker.GetNativeCode().GetSize();
884 }
885 if(FileSize_CodeSection) bUse_CodeSection=1;
886 else bUse_CodeSection=0;
887
888 //エクスポートセクションのファイル上のサイズ
889 i= sizeof(IMAGE_EXPORT_DIRECTORY)+ //エクスポートディレクトリテーブルを飛び越す
890 ExportNum*sizeof(DWORD)+ //エクスポート アドレス テーブルを飛び越す
891 ExportNum*sizeof(DWORD)+ //エクスポート名ポインタ テーブルを飛び越す
892 ExportNum*sizeof(WORD)+ //エクスポート序数テーブルを飛び越す
893 ExportNamesLength; //シンボル名バッファ
894 if(bUse_ExportSection){
895 if(i%FILE_ALIGNMENT) FileSize_ExportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
896 else FileSize_ExportSection=i;
897 }
898 else FileSize_ExportSection=0;
899
900 //インポートセクションのファイル上のサイズ
901 i=
902 LookupSize+ //インポート アドレス テーブル
903 LookupSize+ //ルックアップテーブル
904 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+ //IMAGE_IMPORT_DESCRIPTOR
905 ImportDllNum*0x10+ //DLL名の羅列
906 HintSize; //ヒント名
907 if(i%FILE_ALIGNMENT) FileSize_ImportSection=i+(FILE_ALIGNMENT-i%FILE_ALIGNMENT);
908 else FileSize_ImportSection=i;
909 bUse_ImportSection=1; //インポートセクションは必ず存在する
910
911 //データセクションのファイル上のサイズ
912 if(compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT) FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize()+(FILE_ALIGNMENT-compiler.GetObjectModule().dataTable.GetSize()%FILE_ALIGNMENT);
913 else FileSize_DataSection=compiler.GetObjectModule().dataTable.GetSize();
914 if(FileSize_DataSection) bUse_DataSection=1;
915 else bUse_DataSection=0;
916
917 //リライタブルセクションのファイル上のサイズ(グローバル変数の初期情報のみを格納)
918 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() % FILE_ALIGNMENT )
919 {
920 FileSize_RWSection =
921 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
922 + (FILE_ALIGNMENT-compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()%FILE_ALIGNMENT);
923 }
924 else{
925 if( compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize() )
926 {
927 FileSize_RWSection = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize();
928 }
929 else FileSize_RWSection=FILE_ALIGNMENT;
930 }
931 bUse_RWSection=1;
932
933 //リソースセクションのファイル上のサイズ
934 char *RSrcSectionBuffer;
935 int RSrcSectionSize;
936 RSrcSectionBuffer=GetRSrcSectionBuffer(&RSrcSectionSize);
937 if(RSrcSectionSize%FILE_ALIGNMENT) FileSize_RSrcSection=RSrcSectionSize+(FILE_ALIGNMENT-RSrcSectionSize%FILE_ALIGNMENT);
938 else FileSize_RSrcSection=RSrcSectionSize;
939 if(FileSize_RSrcSection) bUse_RSrcSection=1;
940 else bUse_RSrcSection=0;
941
942 //リロケーションセクションのファイル上のサイズ
943 if(pobj_Reloc->length%FILE_ALIGNMENT) FileSize_RelocSection=pobj_Reloc->length+(FILE_ALIGNMENT-pobj_Reloc->length%FILE_ALIGNMENT);
944 else FileSize_RelocSection=pobj_Reloc->length;
945 if(FileSize_RelocSection) bUse_RelocSection=1;
946 else bUse_RelocSection=0;
947
948 //デバッグセクションのファイル上のサイズ
949 if(pobj_DebugSection->length%FILE_ALIGNMENT) FileSize_DebugSection=pobj_DebugSection->length+(FILE_ALIGNMENT-pobj_DebugSection->length%FILE_ALIGNMENT);
950 else FileSize_DebugSection=pobj_DebugSection->length;
951 if(FileSize_DebugSection) bUse_DebugSection=1;
952 else bUse_DebugSection=0;
953
954
955 //各セッションのファイル上の位置
956 FilePos_CodeSection= EXE_HEADER_SIZE;
957 FilePos_ExportSection= FilePos_CodeSection+
958 FileSize_CodeSection;
959 FilePos_ImportSection= FilePos_ExportSection+
960 FileSize_ExportSection;
961 FilePos_DataSection= FilePos_ImportSection+
962 FileSize_ImportSection;
963 FilePos_RWSection= FilePos_DataSection+
964 FileSize_DataSection;
965 FilePos_RSrcSection= FilePos_RWSection+
966 FileSize_RWSection;
967 FilePos_RelocSection= FilePos_RSrcSection+
968 FileSize_RSrcSection;
969 FilePos_DebugSection= FilePos_RelocSection+
970 FileSize_RelocSection;
971
972 //コードセッションのメモリ上のサイズ
973 if(FileSize_CodeSection%MEM_ALIGNMENT) MemSize_CodeSection=FileSize_CodeSection+(MEM_ALIGNMENT-FileSize_CodeSection%MEM_ALIGNMENT);
974 else MemSize_CodeSection=FileSize_CodeSection;
975
976 //エクスポートセクションのメモリ上のサイズ
977 if(FileSize_ExportSection%MEM_ALIGNMENT) MemSize_ExportSection=FileSize_ExportSection+(MEM_ALIGNMENT-FileSize_ExportSection%MEM_ALIGNMENT);
978 else MemSize_ExportSection=FileSize_ExportSection;
979
980 //インポートセクションのメモリ上のサイズ
981 if(FileSize_ImportSection%MEM_ALIGNMENT) MemSize_ImportSection=FileSize_ImportSection+(MEM_ALIGNMENT-FileSize_ImportSection%MEM_ALIGNMENT);
982 else MemSize_ImportSection=FileSize_ImportSection;
983
984 //データセクションのメモリ上のサイズ
985 if(FileSize_DataSection%MEM_ALIGNMENT) MemSize_DataSection=FileSize_DataSection+(MEM_ALIGNMENT-FileSize_DataSection%MEM_ALIGNMENT);
986 else MemSize_DataSection=FileSize_DataSection;
987
988 //リライタブルセクションのメモリ上のサイズ
989 i = compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
990 + compiler.GetObjectModule().meta.GetGlobalVars().GetAllSize();
991 if(i%MEM_ALIGNMENT) MemSize_RWSection=i+(MEM_ALIGNMENT-i%MEM_ALIGNMENT);
992 else MemSize_RWSection=i;
993
994 //リソースセクションのメモリ上のサイズ
995 if(FileSize_RSrcSection%MEM_ALIGNMENT) MemSize_RSrcSection=FileSize_RSrcSection+(MEM_ALIGNMENT-FileSize_RSrcSection%MEM_ALIGNMENT);
996 else MemSize_RSrcSection=FileSize_RSrcSection;
997
998 //リロケーションセクションのメモリ上のサイズ
999 if(FileSize_RelocSection%MEM_ALIGNMENT) MemSize_RelocSection=FileSize_RelocSection+(MEM_ALIGNMENT-FileSize_RelocSection%MEM_ALIGNMENT);
1000 else MemSize_RelocSection=FileSize_RelocSection;
1001
1002 //デバッグセクションのメモリ上のサイズ
1003 if(FileSize_DebugSection%MEM_ALIGNMENT) MemSize_DebugSection=FileSize_DebugSection+(MEM_ALIGNMENT-FileSize_DebugSection%MEM_ALIGNMENT);
1004 else MemSize_DebugSection=FileSize_DebugSection;
1005
1006
1007 //各セッションのメモリ上の位置
1008 if(bUse_ExportSection) MemPos_ExportSection= 0x1000+
1009 MemSize_CodeSection;
1010 else MemPos_ExportSection=0;
1011 if(bUse_ImportSection) MemPos_ImportSection= 0x1000+
1012 MemSize_CodeSection+
1013 MemSize_ExportSection;
1014 else MemPos_ImportSection=0;
1015 if(bUse_DataSection) MemPos_DataSection= 0x1000+
1016 MemSize_CodeSection+
1017 MemSize_ExportSection+
1018 MemSize_ImportSection;
1019 else MemPos_DataSection=0;
1020 if(bUse_RWSection) MemPos_RWSection= 0x1000+
1021 MemSize_CodeSection+
1022 MemSize_ExportSection+
1023 MemSize_ImportSection+
1024 MemSize_DataSection;
1025 else MemPos_RWSection=0;
1026 if(bUse_RSrcSection) MemPos_RSrcSection= 0x1000+
1027 MemSize_CodeSection+
1028 MemSize_ExportSection+
1029 MemSize_ImportSection+
1030 MemSize_DataSection+
1031 MemSize_RWSection;
1032 else MemPos_RSrcSection=0;
1033 if(bUse_RelocSection) MemPos_RelocSection= 0x1000+
1034 MemSize_CodeSection+
1035 MemSize_ExportSection+
1036 MemSize_ImportSection+
1037 MemSize_DataSection+
1038 MemSize_RWSection+
1039 MemSize_RSrcSection;
1040 else MemPos_RelocSection=0;
1041 if(bUse_DebugSection) MemPos_DebugSection= 0x1000+
1042 MemSize_CodeSection+
1043 MemSize_ExportSection+
1044 MemSize_ImportSection+
1045 MemSize_DataSection+
1046 MemSize_RWSection+
1047 MemSize_RSrcSection+
1048 MemSize_RelocSection;
1049 else MemPos_DebugSection=0;
1050
1051
1052
1053 ////////////////////////////
1054 // エクスポート情報の再配置
1055 ////////////////////////////
1056 if(bUse_ExportSection){
1057 for(i=0;i<ExportNum;i++){
1058 lpdwExportAddressTable[i]+=MemPos_CodeSection;
1059 lpdwExportNamePointerTable[i]+=MemPos_ExportSection;
1060 }
1061
1062 ImageExportDirectory.Name+= MemPos_ExportSection;
1063 ImageExportDirectory.AddressOfFunctions+= MemPos_ExportSection;
1064 ImageExportDirectory.AddressOfNames+= MemPos_ExportSection;
1065 ImageExportDirectory.AddressOfNameOrdinals+= MemPos_ExportSection;
1066 }
1067
1068
1069 ////////////////////////////
1070 // インポート情報の再配置
1071 ////////////////////////////
1072 for(i=0,i5=0;i<ImportDllNum;i++){
1073 //IMAGE_IMPORT_DESCRIPTOR
1074 pImportDescriptor[i].OriginalFirstThunk+=MemPos_ImportSection;
1075 pImportDescriptor[i].Name+=MemPos_ImportSection;
1076
1077 //インポート アドレス テーブル(ルックアップ テーブルを差し引く)
1078 pImportDescriptor[i].FirstThunk=pImportDescriptor[i].OriginalFirstThunk-
1079 LookupSize; //ルックアップテーブル
1080
1081 compiler.GetObjectModule().meta.GetDllProcs().Iterator_Reset();
1082 while( compiler.GetObjectModule().meta.GetDllProcs().Iterator_HasNext() )
1083 {
1084 const DllProc *pDllProc = compiler.GetObjectModule().meta.GetDllProcs().Iterator_GetNext();
1085
1086 if( !pDllProc->IsUsing() ){
1087 continue;
1088 }
1089
1090 if( pDllProc->GetDllFileName() == ppDllNames[i] ){
1091 //ルックアップ テーブル
1092 pLookupTable[i5++]+=MemPos_ImportSection;
1093 }
1094 }
1095 i5++;
1096 }
1097
1098
1099 ////////////////////////////////////////
1100 //仮想関数データテーブルスケジュール
1101 ActiveBasic::Compiler::VtblGenerator::ActionVtblScheduleForAllClasses(
1102 compiler.GetObjectModule().meta.GetClasses(),
1103 ImageBase,
1104 MemPos_CodeSection,
1105 MemPos_DataSection
1106 );
1107
1108
1109 if( compiler.IsDll() ){
1110 //DLLの場合はリロケーション情報を生成
1111 pobj_Reloc->ResetRelocBuffer();
1112 }
1113
1114 compiler.linker.SetDataTable( compiler.GetObjectModule().dataTable );
1115
1116 compiler.linker.SetImageBase( ImageBase );
1117 compiler.linker.ResolveDataTableSchedules( MemPos_DataSection );
1118 compiler.linker.ResolveCatchAddressSchedules( MemPos_CodeSection );
1119 compiler.linker.ResolveDllProcSchedules( MemPos_CodeSection, MemPos_ImportSection, LookupSize, HintSize );
1120 compiler.linker.ResolveUserProcSchedules( MemPos_CodeSection );
1121 compiler.linker.ResolveGlobalVarSchedules( MemPos_RWSection );
1122 compiler.linker.ResolveVtblSchedule( MemPos_DataSection );
1123 compiler.linker.ResolveTypeInfoSchedule( MemPos_DataSection );
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_HEADERS64 ImagePeHdr;
1201 ImagePeHdr.Signature=IMAGE_NT_SIGNATURE;
1202
1203 //マシンタイプ
1204 ImagePeHdr.FileHeader.Machine= IMAGE_FILE_MACHINE_AMD64;
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_OPTIONAL64_HEADER;
1218 if( compiler.IsDll() ){
1219 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_EXECUTABLE_IMAGE|
1220 IMAGE_FILE_DLL|
1221 IMAGE_FILE_LARGE_ADDRESS_AWARE;
1222 }
1223 else{
1224 ImagePeHdr.FileHeader.Characteristics= IMAGE_FILE_RELOCS_STRIPPED|
1225 IMAGE_FILE_EXECUTABLE_IMAGE|
1226 IMAGE_FILE_LARGE_ADDRESS_AWARE;
1227 }
1228
1229 ImagePeHdr.OptionalHeader.Magic= IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1230 ImagePeHdr.OptionalHeader.MajorLinkerVersion= 5;
1231 ImagePeHdr.OptionalHeader.MinorLinkerVersion= 0;
1232 ImagePeHdr.OptionalHeader.SizeOfCode= FileSize_CodeSection; //コードサイズ(.textのセッションサイズ)
1233 ImagePeHdr.OptionalHeader.SizeOfInitializedData=FileSize_DataSection; //データサイズ(.dataのセッションサイズ)
1234 ImagePeHdr.OptionalHeader.SizeOfUninitializedData=0; //未初期化データのサイズ(なし)
1235 if( compiler.IsDll() ){
1236 if(DllMain_EntryPoint==-1)
1237 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=0;
1238 else
1239 ImagePeHdr.OptionalHeader.AddressOfEntryPoint=MemPos_CodeSection+DllMain_EntryPoint;
1240 }
1241 else ImagePeHdr.OptionalHeader.AddressOfEntryPoint= MemPos_CodeSection;
1242 ImagePeHdr.OptionalHeader.BaseOfCode= MemPos_CodeSection; //.textのアドレス
1243
1244 ImagePeHdr.OptionalHeader.ImageBase= ImageBase; //イメージベース
1245 ImagePeHdr.OptionalHeader.SectionAlignment= MEM_ALIGNMENT; //セクションアラインメント
1246 ImagePeHdr.OptionalHeader.FileAlignment= FILE_ALIGNMENT;
1247 ImagePeHdr.OptionalHeader.MajorOperatingSystemVersion=4;
1248 ImagePeHdr.OptionalHeader.MinorOperatingSystemVersion=0;
1249 ImagePeHdr.OptionalHeader.MajorImageVersion= 0;
1250 ImagePeHdr.OptionalHeader.MinorImageVersion= 0;
1251 ImagePeHdr.OptionalHeader.MajorSubsystemVersion=4;
1252 ImagePeHdr.OptionalHeader.MinorSubsystemVersion=0;
1253 ImagePeHdr.OptionalHeader.Win32VersionValue= 0;
1254 ImagePeHdr.OptionalHeader.SizeOfImage= EXE_HEADER_SIZE+
1255 MemSize_CodeSection+
1256 MemSize_ExportSection+
1257 MemSize_ImportSection+
1258 MemSize_DataSection+
1259 MemSize_RWSection+
1260 MemSize_RSrcSection+
1261 MemSize_RelocSection+
1262 MemSize_DebugSection;//すべてのイメージサイズ
1263 ImagePeHdr.OptionalHeader.SizeOfHeaders= EXE_HEADER_SIZE;//ヘッダサイズ
1264 ImagePeHdr.OptionalHeader.CheckSum= 0;
1265 extern unsigned short TypeOfSubSystem;
1266 ImagePeHdr.OptionalHeader.Subsystem= TypeOfSubSystem;
1267 ImagePeHdr.OptionalHeader.DllCharacteristics= 0;
1268 ImagePeHdr.OptionalHeader.SizeOfStackReserve= 0x00100000;
1269 ImagePeHdr.OptionalHeader.SizeOfStackCommit= 0x00001000;
1270 ImagePeHdr.OptionalHeader.SizeOfHeapReserve= 0x00100000;
1271 ImagePeHdr.OptionalHeader.SizeOfHeapCommit= 0x00001000;
1272 ImagePeHdr.OptionalHeader.LoaderFlags= 0;
1273 ImagePeHdr.OptionalHeader.NumberOfRvaAndSizes= IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1274
1275 //データ ディクショナリ
1276 ImagePeHdr.OptionalHeader.DataDirectory[0].VirtualAddress=MemPos_ExportSection;
1277 ImagePeHdr.OptionalHeader.DataDirectory[0].Size=FileSize_ExportSection;
1278 ImagePeHdr.OptionalHeader.DataDirectory[1].VirtualAddress= //IMAGE_IMPORT_DESCRIPTORを指す
1279 MemPos_ImportSection+
1280 LookupSize+ //インポート アドレス テーブル
1281 LookupSize; //ルックアップ テーブル
1282 ImagePeHdr.OptionalHeader.DataDirectory[1].Size=(ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR);
1283 ImagePeHdr.OptionalHeader.DataDirectory[2].VirtualAddress=MemPos_RSrcSection;
1284 ImagePeHdr.OptionalHeader.DataDirectory[2].Size=RSrcSectionSize;
1285 ImagePeHdr.OptionalHeader.DataDirectory[3].VirtualAddress=0;
1286 ImagePeHdr.OptionalHeader.DataDirectory[3].Size=0;
1287 ImagePeHdr.OptionalHeader.DataDirectory[4].VirtualAddress=0;
1288 ImagePeHdr.OptionalHeader.DataDirectory[4].Size=0;
1289 ImagePeHdr.OptionalHeader.DataDirectory[5].VirtualAddress=MemPos_RelocSection;
1290 ImagePeHdr.OptionalHeader.DataDirectory[5].Size=pobj_Reloc->length;
1291 ImagePeHdr.OptionalHeader.DataDirectory[6].VirtualAddress=0;
1292 ImagePeHdr.OptionalHeader.DataDirectory[6].Size=0;
1293 ImagePeHdr.OptionalHeader.DataDirectory[7].VirtualAddress=0;
1294 ImagePeHdr.OptionalHeader.DataDirectory[7].Size=0;
1295 ImagePeHdr.OptionalHeader.DataDirectory[8].VirtualAddress=0;
1296 ImagePeHdr.OptionalHeader.DataDirectory[8].Size=0;
1297 ImagePeHdr.OptionalHeader.DataDirectory[9].VirtualAddress=0;
1298 ImagePeHdr.OptionalHeader.DataDirectory[9].Size=0;
1299 ImagePeHdr.OptionalHeader.DataDirectory[10].VirtualAddress=0;
1300 ImagePeHdr.OptionalHeader.DataDirectory[10].Size=0;
1301 ImagePeHdr.OptionalHeader.DataDirectory[11].VirtualAddress=0;
1302 ImagePeHdr.OptionalHeader.DataDirectory[11].Size=0;
1303 ImagePeHdr.OptionalHeader.DataDirectory[12].VirtualAddress=MemPos_ImportSection;//インポート アドレス テーブル
1304 ImagePeHdr.OptionalHeader.DataDirectory[12].Size=LookupSize;
1305 ImagePeHdr.OptionalHeader.DataDirectory[13].VirtualAddress=0;
1306 ImagePeHdr.OptionalHeader.DataDirectory[13].Size=0;
1307 ImagePeHdr.OptionalHeader.DataDirectory[14].VirtualAddress=0;
1308 ImagePeHdr.OptionalHeader.DataDirectory[14].Size=0;
1309 ImagePeHdr.OptionalHeader.DataDirectory[15].VirtualAddress=0;
1310 ImagePeHdr.OptionalHeader.DataDirectory[15].Size=0;
1311
1312
1313 //コードセクションヘッダ
1314 IMAGE_SECTION_HEADER CodeSectionHeader;
1315 memset((char *)CodeSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1316 lstrcpy((char *)CodeSectionHeader.Name,".text");
1317 CodeSectionHeader.Misc.VirtualSize= MemSize_CodeSection;
1318 CodeSectionHeader.VirtualAddress= MemPos_CodeSection; //開始アドレス
1319 CodeSectionHeader.SizeOfRawData= FileSize_CodeSection;
1320 CodeSectionHeader.PointerToRawData= FilePos_CodeSection; //ファイル上の開始アドレス
1321 CodeSectionHeader.PointerToRelocations= 0;
1322 CodeSectionHeader.PointerToLinenumbers= 0;
1323 CodeSectionHeader.NumberOfRelocations= 0;
1324 CodeSectionHeader.NumberOfLinenumbers= 0;
1325 CodeSectionHeader.Characteristics= IMAGE_SCN_MEM_EXECUTE|
1326 IMAGE_SCN_MEM_READ|
1327 IMAGE_SCN_CNT_CODE;
1328
1329 //エクスポートセクションヘッダ
1330 IMAGE_SECTION_HEADER ExportSectionHeader;
1331 memset((char *)ExportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1332 lstrcpy((char *)ExportSectionHeader.Name,".edata");
1333 ExportSectionHeader.Misc.VirtualSize= MemSize_ExportSection;
1334 ExportSectionHeader.VirtualAddress= MemPos_ExportSection; //開始アドレス
1335 ExportSectionHeader.SizeOfRawData= FileSize_ExportSection; //サイズ
1336 ExportSectionHeader.PointerToRawData= FilePos_ExportSection; //ファイル上の開始アドレス
1337 ExportSectionHeader.PointerToRelocations= 0;
1338 ExportSectionHeader.PointerToLinenumbers= 0;
1339 ExportSectionHeader.NumberOfRelocations= 0;
1340 ExportSectionHeader.NumberOfLinenumbers= 0;
1341 ExportSectionHeader.Characteristics= IMAGE_SCN_CNT_INITIALIZED_DATA|
1342 IMAGE_SCN_MEM_READ;
1343
1344 //インポートセクションヘッダ
1345 IMAGE_SECTION_HEADER ImportSectionHeader;
1346 memset((char *)ImportSectionHeader.Name,0,IMAGE_SIZEOF_SHORT_NAME);
1347 lstrcpy((char *)ImportSectionHeader.Name,".idata");
1348 ImportSectionHeader.Misc.VirtualSize=
1349 LookupSize+ //インポートアドレステーブル
1350 LookupSize+ //ルックアップテーブル
1351 (ImportDllNum+1)*sizeof(IMAGE_IMPORT_DESCRIPTOR)+
1352 16*ImportDllNum+ //DLL名
1353 HintSize; //ヒント名(関数名)テーブル
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_HEADERS64),(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 //インポート アドレス テーブル
1560 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1561 i+=i2;
1562
1563 //ルックアップ テーブル
1564 WriteFile(hFile,pLookupTable,LookupSize,(DWORD *)&i2,NULL);
1565 i+=i2;
1566
1567 //インポート ディレクトリ テーブル(Nullディレクトリ テーブルを含む)
1568 for(i3=0;i3<(ImportDllNum+1);i3++){
1569 WriteFile(hFile,&pImportDescriptor[i3],sizeof(IMAGE_IMPORT_DESCRIPTOR),(DWORD *)&i2,NULL);
1570 i+=i2;
1571 }
1572
1573 //DLL名
1574 for(i3=0;i3<ImportDllNum;i3++){
1575 WriteFile(hFile,ppDllNames[i3],16,(DWORD *)&i2,NULL);
1576 i+=i2;
1577 }
1578
1579 //ヒント テーブル
1580 WriteFile(hFile,pHintTable,HintSize,(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 char *temp = (char *)calloc( FileSize_RWSection, 1 );
1611 memcpy(
1612 temp,
1613 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetBuffer(),
1614 compiler.GetObjectModule().meta.GetGlobalVars().initAreaBuffer.GetSize()
1615 );
1616
1617 WriteFile(hFile,temp,FileSize_RWSection,(DWORD *)&i2,NULL);
1618 i+=i2;
1619
1620 free( temp );
1621 }
1622
1623 //FilePos_RSrcSectionまでNULLを並べる
1624 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RSrcSection-i);
1625 WriteFile(hFile,temp2,FilePos_RSrcSection-i,(DWORD *)&i2,NULL);
1626 HeapDefaultFree(temp2);
1627 i+=i2;
1628
1629 if(bUse_RSrcSection){
1630 //リソースバッファ
1631 WriteFile(hFile,RSrcSectionBuffer,RSrcSectionSize,(DWORD *)&i2,NULL);
1632 i+=i2;
1633 }
1634
1635 //FilePos_RelocSectionまでNULLを並べる
1636 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FilePos_RelocSection-i);
1637 WriteFile(hFile,temp2,FilePos_RelocSection-i,(DWORD *)&i2,NULL);
1638 HeapDefaultFree(temp2);
1639 i+=i2;
1640
1641 if(bUse_RelocSection){
1642 //リロケーション情報
1643 WriteFile(hFile,pobj_Reloc->buffer,pobj_Reloc->length,(DWORD *)&i2,NULL);
1644 i+=i2;
1645 }
1646
1647 //ファイルアラインメントを考慮
1648 if(i%FILE_ALIGNMENT){
1649 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1650 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1651 HeapDefaultFree(temp2);
1652 i+=i2;
1653 }
1654
1655 if(bUse_DebugSection){
1656 //デバッグセクション
1657 WriteFile(hFile,pobj_DebugSection->buffer,pobj_DebugSection->length,(DWORD *)&i2,NULL);
1658 i+=i2;
1659 }
1660
1661 //ファイルアラインメントを考慮
1662 if(i%FILE_ALIGNMENT){
1663 temp2=(char *)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,FILE_ALIGNMENT-i%FILE_ALIGNMENT);
1664 WriteFile(hFile,temp2,FILE_ALIGNMENT-i%FILE_ALIGNMENT,(DWORD *)&i2,NULL);
1665 HeapDefaultFree(temp2);
1666 i+=i2;
1667 }
1668
1669 //書き込み終了
1670 CloseHandle(hFile);
1671
1672
1673EndWriteOpcode:
1674
1675 //Dosスタブ用のメモリを解放
1676 HeapDefaultFree(DosStubBuffer);
1677
1678 //エクスポート テーブル情報を解放
1679 HeapDefaultFree(lpdwExportAddressTable);
1680 HeapDefaultFree(lpdwExportNamePointerTable);
1681 HeapDefaultFree(lpwExportOrdinalTable);
1682
1683 //インポートDLL情報を解放
1684 HeapDefaultFree(pImportDescriptor);
1685 for(i=0;i<ImportDllNum;i++)
1686 HeapDefaultFree(ppDllNames[i]);
1687 HeapDefaultFree(ppDllNames);
1688
1689 //ルックアップテーブルに関する情報を解放
1690 HeapDefaultFree(pLookupTable);
1691
1692 //ヒントテーブルに関する情報を解放
1693 HeapDefaultFree(pHintTable);
1694
1695 //リソースセクションバッファを解放
1696 HeapDefaultFree(RSrcSectionBuffer);
1697
1698 //デバッグセクションを開放
1699 delete pobj_DebugSection;
1700
1701 //リロケーション情報を解放
1702 delete pobj_Reloc;
1703}
Note: See TracBrowser for help on using the repository browser.