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

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

・デバッグトレース時、グローバル領域の終端行でステップインまたはステップアウトしたときにデバッグ情報の取得に失敗して強制終了してしまう不具合を修正。
・グローバル領域のデバッグ実行ができなくなっている不具合を修正。

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