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

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

静的リンクライブラリプロジェクトの作成(IDE側)に対応。

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