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

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

[530][583]を64bit版にマージ。

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