source: dev/trunk/ab5.0/abdev/compiler_x86/MakePeHdr.cpp@ 648

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

複数のlibの依存関係解決後、使用しているDLL関数を正確にインポートできるようにした。

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