source: dev/trunk/abdev/BasicCompiler32/MakePeHdr.cpp@ 357

Last change on this file since 357 was 357, checked in by dai_9181, 17 years ago

例外処理機構実装中...

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