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

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

[636][637][640][641][642]を64bit版にマージ。

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