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

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

[645]をマージ。

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